blob: 81c3ca278a6ee657894e0ca16cb6fc5a8605ba36 [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 Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200116static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200125/* When using exists() don't auto-load a script. */
126static int no_autoload = FALSE;
127
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
156
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157/*
158 * Structure to hold info for a user function.
159 */
160typedef struct ufunc ufunc_T;
161
162struct ufunc
163{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000164 int uf_varargs; /* variable nr of arguments */
165 int uf_flags;
166 int uf_calls; /* nr of active calls */
167 garray_T uf_args; /* arguments */
168 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000169#ifdef FEAT_PROFILE
170 int uf_profiling; /* TRUE when func is being profiled */
171 /* profiling the function as a whole */
172 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000173 proftime_T uf_tm_total; /* time spent in function + children */
174 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000175 proftime_T uf_tm_children; /* time spent in children this call */
176 /* profiling the function per line */
177 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T *uf_tml_total; /* time spent in a line + children */
179 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tml_start; /* start time for current line */
181 proftime_T uf_tml_children; /* time spent in children for this line */
182 proftime_T uf_tml_wait; /* start wait time for current line */
183 int uf_tml_idx; /* index of line being timed; -1 if none */
184 int uf_tml_execed; /* line being timed was executed */
185#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000186 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000188 int uf_refcount; /* for numbered function: reference count */
189 char_u uf_name[1]; /* name of function (actually longer); can
190 start with <SNR>123_ (<SNR> is K_SPECIAL
191 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192};
193
194/* function flags */
195#define FC_ABORT 1 /* abort function on error */
196#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000197#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000200 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000202static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000204/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000205static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
206
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207/* list heads for garbage collection */
208static dict_T *first_dict = NULL; /* list of all dicts */
209static list_T *first_list = NULL; /* list of all lists */
210
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000211/* From user function to hashitem and back. */
212static ufunc_T dumuf;
213#define UF2HIKEY(fp) ((fp)->uf_name)
214#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
215#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
216
217#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
218#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
Bram Moolenaar33570922005-01-25 22:26:29 +0000220#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
221#define VAR_SHORT_LEN 20 /* short variable name length */
222#define FIXVAR_CNT 12 /* number of fixed variables */
223
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000225typedef struct funccall_S funccall_T;
226
227struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 ufunc_T *func; /* function being called */
230 int linenr; /* next line to be executed */
231 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000232 struct /* fixed variables for arguments */
233 {
234 dictitem_T var; /* variable (without room for name) */
235 char_u room[VAR_SHORT_LEN]; /* room for the name */
236 } fixvar[FIXVAR_CNT];
237 dict_T l_vars; /* l: local function variables */
238 dictitem_T l_vars_var; /* variable for l: scope */
239 dict_T l_avars; /* a: argument variables */
240 dictitem_T l_avars_var; /* variable for a: scope */
241 list_T l_varlist; /* list for a:000 */
242 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
243 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 linenr_T breakpoint; /* next line with breakpoint or zero */
245 int dbg_tick; /* debug_tick when breakpoint was set */
246 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000247#ifdef FEAT_PROFILE
248 proftime_T prof_child; /* time spent in a child */
249#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000250 funccall_T *caller; /* calling function or NULL */
251};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252
253/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000254 * Info used by a ":for" loop.
255 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000256typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257{
258 int fi_semicolon; /* TRUE if ending in '; var]' */
259 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000260 listwatch_T fi_lw; /* keep an eye on the item used. */
261 list_T *fi_list; /* list being used */
262} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000263
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000265 * Struct used by trans_function_name()
266 */
267typedef struct
268{
Bram Moolenaar33570922005-01-25 22:26:29 +0000269 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000270 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000271 dictitem_T *fd_di; /* Dictionary item used */
272} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273
Bram Moolenaara7043832005-01-21 11:56:39 +0000274
275/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 * Array to hold the value of v: variables.
277 * The value is in a dictitem, so that it can also be used in the v: scope.
278 * The reason to use this table anyway is for very quick access to the
279 * variables with the VV_ defines.
280 */
281#include "version.h"
282
283/* values for vv_flags: */
284#define VV_COMPAT 1 /* compatible, also used without "v:" */
285#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000286#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000288#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000289
290static struct vimvar
291{
292 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000293 dictitem_T vv_di; /* value and name for key */
294 char vv_filler[16]; /* space for LONGEST name below!!! */
295 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
296} vimvars[VV_LEN] =
297{
298 /*
299 * The order here must match the VV_ defines in vim.h!
300 * Initializing a union does not work, leave tv.vval empty to get zero's.
301 */
302 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("count1", VAR_NUMBER), VV_RO},
304 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
305 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
306 {VV_NAME("warningmsg", VAR_STRING), 0},
307 {VV_NAME("statusmsg", VAR_STRING), 0},
308 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
309 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
310 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
312 {VV_NAME("termresponse", VAR_STRING), VV_RO},
313 {VV_NAME("fname", VAR_STRING), VV_RO},
314 {VV_NAME("lang", VAR_STRING), VV_RO},
315 {VV_NAME("lc_time", VAR_STRING), VV_RO},
316 {VV_NAME("ctype", VAR_STRING), VV_RO},
317 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
318 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
319 {VV_NAME("fname_in", VAR_STRING), VV_RO},
320 {VV_NAME("fname_out", VAR_STRING), VV_RO},
321 {VV_NAME("fname_new", VAR_STRING), VV_RO},
322 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
323 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
324 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
325 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
326 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
327 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
328 {VV_NAME("progname", VAR_STRING), VV_RO},
329 {VV_NAME("servername", VAR_STRING), VV_RO},
330 {VV_NAME("dying", VAR_NUMBER), VV_RO},
331 {VV_NAME("exception", VAR_STRING), VV_RO},
332 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
333 {VV_NAME("register", VAR_STRING), VV_RO},
334 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
335 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000336 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
337 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000338 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000339 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
340 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000341 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
342 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
343 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
344 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
345 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000346 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000347 {VV_NAME("swapname", VAR_STRING), VV_RO},
348 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000349 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200350 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000351 {VV_NAME("mouse_win", VAR_NUMBER), 0},
352 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
353 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000354 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000355 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000356 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200357 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
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
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200368static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000369#define vimvarht vimvardict.dv_hashtab
370
Bram Moolenaara40058a2005-07-11 22:42:07 +0000371static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
372static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000373static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
374static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
375static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000376static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
377static void list_glob_vars __ARGS((int *first));
378static void list_buf_vars __ARGS((int *first));
379static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000380#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000381static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000382#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_vim_vars __ARGS((int *first));
384static void list_script_vars __ARGS((int *first));
385static void list_func_vars __ARGS((int *first));
386static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000387static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
388static int check_changedtick __ARGS((char_u *arg));
389static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
390static void clear_lval __ARGS((lval_T *lp));
391static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
392static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
394static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
395static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
396static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
397static void item_lock __ARGS((typval_T *tv, int deep, int lock));
398static int tv_islocked __ARGS((typval_T *tv));
399
Bram Moolenaar33570922005-01-25 22:26:29 +0000400static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
401static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000406static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
407static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000408
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000409static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000414static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100416static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
417static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
418static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000419static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000421static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
423static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000424static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000425static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100426static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000427static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000428static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200429static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
431static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000432static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
437static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000439#ifdef FEAT_FLOAT
440static int string2float __ARGS((char_u *text, float_T *value));
441#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
443static int find_internal_func __ARGS((char_u *name));
444static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
445static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200446static int call_func __ARGS((char_u *funcname, 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 +0000447static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000448static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000450#ifdef FEAT_FLOAT
451static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200452static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100455static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200461static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200463static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#ifdef FEAT_FLOAT
477static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
478#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000479static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000482static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000484#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000485static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000486static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
488#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000491#ifdef FEAT_FLOAT
492static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200493static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
498static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200509#ifdef FEAT_FLOAT
510static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000514static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000520#ifdef FEAT_FLOAT
521static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000524#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000525static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000534static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000536static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000542static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000550static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000551static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000552static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000553static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000554static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200556static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000557static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000565static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000579static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100584static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000586static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000598#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200599static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000600static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
601#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200602#ifdef FEAT_LUA
603static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
604#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000609static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000610static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000611static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000613static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000617#ifdef vim_mkdir
618static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
619#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100621#ifdef FEAT_MZSCHEME
622static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
623#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100626static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000627static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000628#ifdef FEAT_FLOAT
629static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
630#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000632static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000633static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200634#ifdef FEAT_PYTHON3
635static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
637#ifdef FEAT_PYTHON
638static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000641static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000642static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000654#ifdef FEAT_FLOAT
655static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
656#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200657static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100659static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000662static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000664static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000671static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000672static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000673static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000674static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200676static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000677static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100679#ifdef FEAT_CRYPT
680static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
681#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000682static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200683static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000685#ifdef FEAT_FLOAT
686static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200687static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000688#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000690static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000691static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
695static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
696static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
697#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000698static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200699static void f_strchars __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));
Bram Moolenaardc536092010-07-18 15:45:49 +0200709static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200710static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000711static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000716static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200717static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000718static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000719static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000720static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000721static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000722static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000723static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000725static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200726#ifdef FEAT_FLOAT
727static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
729#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#ifdef FEAT_FLOAT
734static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200737static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200738static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100742static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000743static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000749static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000752static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100753static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000755static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000756static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000757static int get_env_len __ARGS((char_u **arg));
758static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000759static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000760static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
761#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
762#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
763 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000764static 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 +0000765static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000766static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000767static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
768static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static typval_T *alloc_tv __ARGS((void));
770static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static void init_tv __ARGS((typval_T *varp));
772static long get_tv_number __ARGS((typval_T *varp));
773static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000774static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static char_u *get_tv_string __ARGS((typval_T *varp));
776static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000777static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200779static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
781static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
782static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000783static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
784static 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 +0000785static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
786static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000787static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200788static int var_check_func_name __ARGS((char_u *name, int new_var));
789static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000790static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000791static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
793static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
794static int eval_fname_script __ARGS((char_u *p));
795static int eval_fname_sid __ARGS((char_u *p));
796static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static ufunc_T *find_func __ARGS((char_u *name));
798static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000799static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800#ifdef FEAT_PROFILE
801static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000802static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
803static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
804static int
805# ifdef __BORLANDC__
806 _RTLENTRYF
807# endif
808 prof_total_cmp __ARGS((const void *s1, const void *s2));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200815static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000816static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000817static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000818static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000819static 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 +0000820static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
821static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000823static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
824static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000825static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000826static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000827static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200829
830#ifdef EBCDIC
831static int compare_func_name __ARGS((const void *s1, const void *s2));
832static void sortFunctions __ARGS(());
833#endif
834
Bram Moolenaar33570922005-01-25 22:26:29 +0000835/*
836 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000837 */
838 void
839eval_init()
840{
Bram Moolenaar33570922005-01-25 22:26:29 +0000841 int i;
842 struct vimvar *p;
843
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200844 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
845 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200846 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000847 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000848 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000849
850 for (i = 0; i < VV_LEN; ++i)
851 {
852 p = &vimvars[i];
853 STRCPY(p->vv_di.di_key, p->vv_name);
854 if (p->vv_flags & VV_RO)
855 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
856 else if (p->vv_flags & VV_RO_SBX)
857 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
858 else
859 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000860
861 /* add to v: scope dict, unless the value is not always available */
862 if (p->vv_type != VAR_UNKNOWN)
863 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000864 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000865 /* add to compat scope dict */
866 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000868 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200869 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200870
871#ifdef EBCDIC
872 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100873 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200874 */
875 sortFunctions();
876#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000877}
878
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000879#if defined(EXITFREE) || defined(PROTO)
880 void
881eval_clear()
882{
883 int i;
884 struct vimvar *p;
885
886 for (i = 0; i < VV_LEN; ++i)
887 {
888 p = &vimvars[i];
889 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000890 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000891 vim_free(p->vv_str);
892 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000893 }
894 else if (p->vv_di.di_tv.v_type == VAR_LIST)
895 {
896 list_unref(p->vv_list);
897 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000898 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000899 }
900 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000901 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000902 hash_clear(&compat_hashtab);
903
Bram Moolenaard9fba312005-06-26 22:34:35 +0000904 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100905# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200906 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100907# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908
909 /* global variables */
910 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000911
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000912 /* autoloaded script names */
913 ga_clear_strings(&ga_loaded);
914
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200915 /* script-local variables */
916 for (i = 1; i <= ga_scripts.ga_len; ++i)
917 {
918 vars_clear(&SCRIPT_VARS(i));
919 vim_free(SCRIPT_SV(i));
920 }
921 ga_clear(&ga_scripts);
922
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000923 /* unreferenced lists and dicts */
924 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000925
926 /* functions */
927 free_all_functions();
928 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000929}
930#endif
931
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 * Return the name of the executed function.
934 */
935 char_u *
936func_name(cookie)
937 void *cookie;
938{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000939 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940}
941
942/*
943 * Return the address holding the next breakpoint line for a funccall cookie.
944 */
945 linenr_T *
946func_breakpoint(cookie)
947 void *cookie;
948{
Bram Moolenaar33570922005-01-25 22:26:29 +0000949 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950}
951
952/*
953 * Return the address holding the debug tick for a funccall cookie.
954 */
955 int *
956func_dbg_tick(cookie)
957 void *cookie;
958{
Bram Moolenaar33570922005-01-25 22:26:29 +0000959 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960}
961
962/*
963 * Return the nesting level for a funccall cookie.
964 */
965 int
966func_level(cookie)
967 void *cookie;
968{
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970}
971
972/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000973funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000975/* pointer to list of previously used funccal, still around because some
976 * item in it is still being used. */
977funccall_T *previous_funccal = NULL;
978
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979/*
980 * Return TRUE when a function was ended by a ":return" command.
981 */
982 int
983current_func_returned()
984{
985 return current_funccal->returned;
986}
987
988
989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 * Set an internal variable to a string value. Creates the variable if it does
991 * not already exist.
992 */
993 void
994set_internal_string_var(name, value)
995 char_u *name;
996 char_u *value;
997{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000998 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000999 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
1001 val = vim_strsave(value);
1002 if (val != NULL)
1003 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001004 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001005 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001007 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001008 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 }
1010 }
1011}
1012
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001013static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001014static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001015static char_u *redir_endp = NULL;
1016static char_u *redir_varname = NULL;
1017
1018/*
1019 * Start recording command output to a variable
1020 * Returns OK if successfully completed the setup. FAIL otherwise.
1021 */
1022 int
1023var_redir_start(name, append)
1024 char_u *name;
1025 int append; /* append to an existing variable */
1026{
1027 int save_emsg;
1028 int err;
1029 typval_T tv;
1030
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001031 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001032 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001033 {
1034 EMSG(_(e_invarg));
1035 return FAIL;
1036 }
1037
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001038 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001039 redir_varname = vim_strsave(name);
1040 if (redir_varname == NULL)
1041 return FAIL;
1042
1043 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1044 if (redir_lval == NULL)
1045 {
1046 var_redir_stop();
1047 return FAIL;
1048 }
1049
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001050 /* The output is stored in growarray "redir_ga" until redirection ends. */
1051 ga_init2(&redir_ga, (int)sizeof(char), 500);
1052
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001053 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001054 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1055 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1057 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001058 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 if (redir_endp != NULL && *redir_endp != NUL)
1060 /* Trailing characters are present after the variable name */
1061 EMSG(_(e_trailing));
1062 else
1063 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001064 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 var_redir_stop();
1066 return FAIL;
1067 }
1068
1069 /* check if we can write to the variable: set it to or append an empty
1070 * string */
1071 save_emsg = did_emsg;
1072 did_emsg = FALSE;
1073 tv.v_type = VAR_STRING;
1074 tv.vval.v_string = (char_u *)"";
1075 if (append)
1076 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1077 else
1078 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001079 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001081 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 if (err)
1083 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001084 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 var_redir_stop();
1086 return FAIL;
1087 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088
1089 return OK;
1090}
1091
1092/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001093 * Append "value[value_len]" to the variable set by var_redir_start().
1094 * The actual appending is postponed until redirection ends, because the value
1095 * appended may in fact be the string we write to, changing it may cause freed
1096 * memory to be used:
1097 * :redir => foo
1098 * :let foo
1099 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100 */
1101 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001102var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001104 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001106 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107
1108 if (redir_lval == NULL)
1109 return;
1110
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001111 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001112 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001114 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 {
1118 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 }
1121 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123}
1124
1125/*
1126 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001127 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128 */
1129 void
1130var_redir_stop()
1131{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001132 typval_T tv;
1133
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 if (redir_lval != NULL)
1135 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001136 /* If there was no error: assign the text to the variable. */
1137 if (redir_endp != NULL)
1138 {
1139 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1140 tv.v_type = VAR_STRING;
1141 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001142 /* Call get_lval() again, if it's inside a Dict or List it may
1143 * have changed. */
1144 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1145 FALSE, FALSE, FALSE, FNE_CHECK_START);
1146 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1147 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1148 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001149 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001150
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001151 /* free the collected output */
1152 vim_free(redir_ga.ga_data);
1153 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001154
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155 vim_free(redir_lval);
1156 redir_lval = NULL;
1157 }
1158 vim_free(redir_varname);
1159 redir_varname = NULL;
1160}
1161
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162# if defined(FEAT_MBYTE) || defined(PROTO)
1163 int
1164eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1165 char_u *enc_from;
1166 char_u *enc_to;
1167 char_u *fname_from;
1168 char_u *fname_to;
1169{
1170 int err = FALSE;
1171
1172 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1173 set_vim_var_string(VV_CC_TO, enc_to, -1);
1174 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1175 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1176 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1177 err = TRUE;
1178 set_vim_var_string(VV_CC_FROM, NULL, -1);
1179 set_vim_var_string(VV_CC_TO, NULL, -1);
1180 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1181 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1182
1183 if (err)
1184 return FAIL;
1185 return OK;
1186}
1187# endif
1188
1189# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1190 int
1191eval_printexpr(fname, args)
1192 char_u *fname;
1193 char_u *args;
1194{
1195 int err = FALSE;
1196
1197 set_vim_var_string(VV_FNAME_IN, fname, -1);
1198 set_vim_var_string(VV_CMDARG, args, -1);
1199 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1200 err = TRUE;
1201 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1202 set_vim_var_string(VV_CMDARG, NULL, -1);
1203
1204 if (err)
1205 {
1206 mch_remove(fname);
1207 return FAIL;
1208 }
1209 return OK;
1210}
1211# endif
1212
1213# if defined(FEAT_DIFF) || defined(PROTO)
1214 void
1215eval_diff(origfile, newfile, outfile)
1216 char_u *origfile;
1217 char_u *newfile;
1218 char_u *outfile;
1219{
1220 int err = FALSE;
1221
1222 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1223 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1224 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1225 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1226 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1227 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1228 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1229}
1230
1231 void
1232eval_patch(origfile, difffile, outfile)
1233 char_u *origfile;
1234 char_u *difffile;
1235 char_u *outfile;
1236{
1237 int err;
1238
1239 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1240 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1241 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1242 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1243 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1244 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1245 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1246}
1247# endif
1248
1249/*
1250 * Top level evaluation function, returning a boolean.
1251 * Sets "error" to TRUE if there was an error.
1252 * Return TRUE or FALSE.
1253 */
1254 int
1255eval_to_bool(arg, error, nextcmd, skip)
1256 char_u *arg;
1257 int *error;
1258 char_u **nextcmd;
1259 int skip; /* only parse, don't execute */
1260{
Bram Moolenaar33570922005-01-25 22:26:29 +00001261 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 int retval = FALSE;
1263
1264 if (skip)
1265 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001266 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 else
1269 {
1270 *error = FALSE;
1271 if (!skip)
1272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001273 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001274 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
1276 }
1277 if (skip)
1278 --emsg_skip;
1279
1280 return retval;
1281}
1282
1283/*
1284 * Top level evaluation function, returning a string. If "skip" is TRUE,
1285 * only parsing to "nextcmd" is done, without reporting errors. Return
1286 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1287 */
1288 char_u *
1289eval_to_string_skip(arg, nextcmd, skip)
1290 char_u *arg;
1291 char_u **nextcmd;
1292 int skip; /* only parse, don't execute */
1293{
Bram Moolenaar33570922005-01-25 22:26:29 +00001294 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 char_u *retval;
1296
1297 if (skip)
1298 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001299 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 retval = NULL;
1301 else
1302 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 retval = vim_strsave(get_tv_string(&tv));
1304 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 }
1306 if (skip)
1307 --emsg_skip;
1308
1309 return retval;
1310}
1311
1312/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001313 * Skip over an expression at "*pp".
1314 * Return FAIL for an error, OK otherwise.
1315 */
1316 int
1317skip_expr(pp)
1318 char_u **pp;
1319{
Bram Moolenaar33570922005-01-25 22:26:29 +00001320 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001321
1322 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001323 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001324}
1325
1326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001328 * When "convert" is TRUE convert a List into a sequence of lines and convert
1329 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * Return pointer to allocated memory, or NULL for failure.
1331 */
1332 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001333eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 char_u *arg;
1335 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
Bram Moolenaar33570922005-01-25 22:26:29 +00001338 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001340 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001341#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001342 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001345 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 retval = NULL;
1347 else
1348 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001350 {
1351 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001352 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001353 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001354 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001355 if (tv.vval.v_list->lv_len > 0)
1356 ga_append(&ga, NL);
1357 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001358 ga_append(&ga, NUL);
1359 retval = (char_u *)ga.ga_data;
1360 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001361#ifdef FEAT_FLOAT
1362 else if (convert && tv.v_type == VAR_FLOAT)
1363 {
1364 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1365 retval = vim_strsave(numbuf);
1366 }
1367#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001368 else
1369 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001370 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 }
1372
1373 return retval;
1374}
1375
1376/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001377 * Call eval_to_string() without using current local variables and using
1378 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 */
1380 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001381eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 char_u *arg;
1383 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001384 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385{
1386 char_u *retval;
1387 void *save_funccalp;
1388
1389 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001390 if (use_sandbox)
1391 ++sandbox;
1392 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001393 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394 if (use_sandbox)
1395 --sandbox;
1396 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 restore_funccal(save_funccalp);
1398 return retval;
1399}
1400
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401/*
1402 * Top level evaluation function, returning a number.
1403 * Evaluates "expr" silently.
1404 * Returns -1 for an error.
1405 */
1406 int
1407eval_to_number(expr)
1408 char_u *expr;
1409{
Bram Moolenaar33570922005-01-25 22:26:29 +00001410 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001412 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413
1414 ++emsg_off;
1415
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001416 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 retval = -1;
1418 else
1419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001420 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001421 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 }
1423 --emsg_off;
1424
1425 return retval;
1426}
1427
Bram Moolenaara40058a2005-07-11 22:42:07 +00001428/*
1429 * Prepare v: variable "idx" to be used.
1430 * Save the current typeval in "save_tv".
1431 * When not used yet add the variable to the v: hashtable.
1432 */
1433 static void
1434prepare_vimvar(idx, save_tv)
1435 int idx;
1436 typval_T *save_tv;
1437{
1438 *save_tv = vimvars[idx].vv_tv;
1439 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1440 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1441}
1442
1443/*
1444 * Restore v: variable "idx" to typeval "save_tv".
1445 * When no longer defined, remove the variable from the v: hashtable.
1446 */
1447 static void
1448restore_vimvar(idx, save_tv)
1449 int idx;
1450 typval_T *save_tv;
1451{
1452 hashitem_T *hi;
1453
Bram Moolenaara40058a2005-07-11 22:42:07 +00001454 vimvars[idx].vv_tv = *save_tv;
1455 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1456 {
1457 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1458 if (HASHITEM_EMPTY(hi))
1459 EMSG2(_(e_intern2), "restore_vimvar()");
1460 else
1461 hash_remove(&vimvarht, hi);
1462 }
1463}
1464
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001465#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001466/*
1467 * Evaluate an expression to a list with suggestions.
1468 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001469 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001470 */
1471 list_T *
1472eval_spell_expr(badword, expr)
1473 char_u *badword;
1474 char_u *expr;
1475{
1476 typval_T save_val;
1477 typval_T rettv;
1478 list_T *list = NULL;
1479 char_u *p = skipwhite(expr);
1480
1481 /* Set "v:val" to the bad word. */
1482 prepare_vimvar(VV_VAL, &save_val);
1483 vimvars[VV_VAL].vv_type = VAR_STRING;
1484 vimvars[VV_VAL].vv_str = badword;
1485 if (p_verbose == 0)
1486 ++emsg_off;
1487
1488 if (eval1(&p, &rettv, TRUE) == OK)
1489 {
1490 if (rettv.v_type != VAR_LIST)
1491 clear_tv(&rettv);
1492 else
1493 list = rettv.vval.v_list;
1494 }
1495
1496 if (p_verbose == 0)
1497 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001498 restore_vimvar(VV_VAL, &save_val);
1499
1500 return list;
1501}
1502
1503/*
1504 * "list" is supposed to contain two items: a word and a number. Return the
1505 * word in "pp" and the number as the return value.
1506 * Return -1 if anything isn't right.
1507 * Used to get the good word and score from the eval_spell_expr() result.
1508 */
1509 int
1510get_spellword(list, pp)
1511 list_T *list;
1512 char_u **pp;
1513{
1514 listitem_T *li;
1515
1516 li = list->lv_first;
1517 if (li == NULL)
1518 return -1;
1519 *pp = get_tv_string(&li->li_tv);
1520
1521 li = li->li_next;
1522 if (li == NULL)
1523 return -1;
1524 return get_tv_number(&li->li_tv);
1525}
1526#endif
1527
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001528/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001529 * Top level evaluation function.
1530 * Returns an allocated typval_T with the result.
1531 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001532 */
1533 typval_T *
1534eval_expr(arg, nextcmd)
1535 char_u *arg;
1536 char_u **nextcmd;
1537{
1538 typval_T *tv;
1539
1540 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001541 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001542 {
1543 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001544 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001545 }
1546
1547 return tv;
1548}
1549
1550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001552 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001553 * Uses argv[argc] for the function arguments. Only Number and String
1554 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001555 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001557 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001558call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 char_u *func;
1560 int argc;
1561 char_u **argv;
1562 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001563 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
Bram Moolenaar33570922005-01-25 22:26:29 +00001566 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 long n;
1568 int len;
1569 int i;
1570 int doesrange;
1571 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001572 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001574 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
1578 for (i = 0; i < argc; i++)
1579 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001580 /* Pass a NULL or empty argument as an empty string */
1581 if (argv[i] == NULL || *argv[i] == NUL)
1582 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001583 argvars[i].v_type = VAR_STRING;
1584 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001585 continue;
1586 }
1587
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001588 if (str_arg_only)
1589 len = 0;
1590 else
1591 /* Recognize a number argument, the others must be strings. */
1592 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 if (len != 0 && len == (int)STRLEN(argv[i]))
1594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001595 argvars[i].v_type = VAR_NUMBER;
1596 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598 else
1599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001600 argvars[i].v_type = VAR_STRING;
1601 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 }
1603 }
1604
1605 if (safe)
1606 {
1607 save_funccalp = save_funccal();
1608 ++sandbox;
1609 }
1610
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1612 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (safe)
1616 {
1617 --sandbox;
1618 restore_funccal(save_funccalp);
1619 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 vim_free(argvars);
1621
1622 if (ret == FAIL)
1623 clear_tv(rettv);
1624
1625 return ret;
1626}
1627
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001628/*
1629 * Call vimL function "func" and return the result as a number.
1630 * Returns -1 when calling the function fails.
1631 * Uses argv[argc] for the function arguments.
1632 */
1633 long
1634call_func_retnr(func, argc, argv, safe)
1635 char_u *func;
1636 int argc;
1637 char_u **argv;
1638 int safe; /* use the sandbox */
1639{
1640 typval_T rettv;
1641 long retval;
1642
1643 /* All arguments are passed as strings, no conversion to number. */
1644 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1645 return -1;
1646
1647 retval = get_tv_number_chk(&rettv, NULL);
1648 clear_tv(&rettv);
1649 return retval;
1650}
1651
1652#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1653 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1654
Bram Moolenaar4f688582007-07-24 12:34:30 +00001655# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001656/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001657 * Call vimL function "func" and return the result as a string.
1658 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659 * Uses argv[argc] for the function arguments.
1660 */
1661 void *
1662call_func_retstr(func, argc, argv, safe)
1663 char_u *func;
1664 int argc;
1665 char_u **argv;
1666 int safe; /* use the sandbox */
1667{
1668 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001669 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001670
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001671 /* All arguments are passed as strings, no conversion to number. */
1672 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673 return NULL;
1674
1675 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 return retval;
1678}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001679# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001681/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001682 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001684 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 */
1686 void *
1687call_func_retlist(func, argc, argv, safe)
1688 char_u *func;
1689 int argc;
1690 char_u **argv;
1691 int safe; /* use the sandbox */
1692{
1693 typval_T rettv;
1694
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001695 /* All arguments are passed as strings, no conversion to number. */
1696 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001697 return NULL;
1698
1699 if (rettv.v_type != VAR_LIST)
1700 {
1701 clear_tv(&rettv);
1702 return NULL;
1703 }
1704
1705 return rettv.vval.v_list;
1706}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707#endif
1708
1709/*
1710 * Save the current function call pointer, and set it to NULL.
1711 * Used when executing autocommands and for ":source".
1712 */
1713 void *
1714save_funccal()
1715{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001716 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 current_funccal = NULL;
1719 return (void *)fc;
1720}
1721
1722 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001723restore_funccal(vfc)
1724 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001726 funccall_T *fc = (funccall_T *)vfc;
1727
1728 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729}
1730
Bram Moolenaar05159a02005-02-26 23:04:13 +00001731#if defined(FEAT_PROFILE) || defined(PROTO)
1732/*
1733 * Prepare profiling for entering a child or something else that is not
1734 * counted for the script/function itself.
1735 * Should always be called in pair with prof_child_exit().
1736 */
1737 void
1738prof_child_enter(tm)
1739 proftime_T *tm; /* place to store waittime */
1740{
1741 funccall_T *fc = current_funccal;
1742
1743 if (fc != NULL && fc->func->uf_profiling)
1744 profile_start(&fc->prof_child);
1745 script_prof_save(tm);
1746}
1747
1748/*
1749 * Take care of time spent in a child.
1750 * Should always be called after prof_child_enter().
1751 */
1752 void
1753prof_child_exit(tm)
1754 proftime_T *tm; /* where waittime was stored */
1755{
1756 funccall_T *fc = current_funccal;
1757
1758 if (fc != NULL && fc->func->uf_profiling)
1759 {
1760 profile_end(&fc->prof_child);
1761 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1762 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1763 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1764 }
1765 script_prof_restore(tm);
1766}
1767#endif
1768
1769
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#ifdef FEAT_FOLDING
1771/*
1772 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1773 * it in "*cp". Doesn't give error messages.
1774 */
1775 int
1776eval_foldexpr(arg, cp)
1777 char_u *arg;
1778 int *cp;
1779{
Bram Moolenaar33570922005-01-25 22:26:29 +00001780 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 int retval;
1782 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001783 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1784 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
1786 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001787 if (use_sandbox)
1788 ++sandbox;
1789 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 retval = 0;
1793 else
1794 {
1795 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 if (tv.v_type == VAR_NUMBER)
1797 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001798 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 retval = 0;
1800 else
1801 {
1802 /* If the result is a string, check if there is a non-digit before
1803 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001804 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (!VIM_ISDIGIT(*s) && *s != '-')
1806 *cp = *s++;
1807 retval = atol((char *)s);
1808 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001809 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 }
1811 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001812 if (use_sandbox)
1813 --sandbox;
1814 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815
1816 return retval;
1817}
1818#endif
1819
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 * ":let" list all variable values
1822 * ":let var1 var2" list variable values
1823 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001824 * ":let var += expr" assignment command.
1825 * ":let var -= expr" assignment command.
1826 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 */
1829 void
1830ex_let(eap)
1831 exarg_T *eap;
1832{
1833 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001835 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 int var_count = 0;
1838 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001839 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001840 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001841 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaardb552d602006-03-23 22:59:57 +00001843 argend = skip_var_list(arg, &var_count, &semicolon);
1844 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001846 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1847 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001848 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001849 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001851 /*
1852 * ":let" without "=": list variables
1853 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001854 if (*arg == '[')
1855 EMSG(_(e_invarg));
1856 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001858 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001860 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001862 list_glob_vars(&first);
1863 list_buf_vars(&first);
1864 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001865#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001866 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001867#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 list_script_vars(&first);
1869 list_func_vars(&first);
1870 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 eap->nextcmd = check_nextcmd(arg);
1873 }
1874 else
1875 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 op[0] = '=';
1877 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001878 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 {
1880 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1881 op[0] = expr[-1]; /* +=, -= or .= */
1882 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (eap->skip)
1886 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (eap->skip)
1889 {
1890 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 --emsg_skip;
1893 }
1894 else if (i != FAIL)
1895 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 }
1900 }
1901}
1902
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903/*
1904 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1905 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001906 * When "nextchars" is not NULL it points to a string with characters that
1907 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1908 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 * Returns OK or FAIL;
1910 */
1911 static int
1912ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1913 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001914 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 int copy; /* copy values from "tv", don't move */
1916 int semicolon; /* from skip_var_list() */
1917 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001918 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919{
1920 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 listitem_T *item;
1924 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925
1926 if (*arg != '[')
1927 {
1928 /*
1929 * ":let var = expr" or ":for var in list"
1930 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 return FAIL;
1933 return OK;
1934 }
1935
1936 /*
1937 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1938 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001939 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940 {
1941 EMSG(_(e_listreq));
1942 return FAIL;
1943 }
1944
1945 i = list_len(l);
1946 if (semicolon == 0 && var_count < i)
1947 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001948 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 return FAIL;
1950 }
1951 if (var_count - semicolon > i)
1952 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001953 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 return FAIL;
1955 }
1956
1957 item = l->lv_first;
1958 while (*arg != ']')
1959 {
1960 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962 item = item->li_next;
1963 if (arg == NULL)
1964 return FAIL;
1965
1966 arg = skipwhite(arg);
1967 if (*arg == ';')
1968 {
1969 /* Put the rest of the list (may be empty) in the var after ';'.
1970 * Create a new list for this. */
1971 l = list_alloc();
1972 if (l == NULL)
1973 return FAIL;
1974 while (item != NULL)
1975 {
1976 list_append_tv(l, &item->li_tv);
1977 item = item->li_next;
1978 }
1979
1980 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001981 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 ltv.vval.v_list = l;
1983 l->lv_refcount = 1;
1984
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1986 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 clear_tv(&ltv);
1988 if (arg == NULL)
1989 return FAIL;
1990 break;
1991 }
1992 else if (*arg != ',' && *arg != ']')
1993 {
1994 EMSG2(_(e_intern2), "ex_let_vars()");
1995 return FAIL;
1996 }
1997 }
1998
1999 return OK;
2000}
2001
2002/*
2003 * Skip over assignable variable "var" or list of variables "[var, var]".
2004 * Used for ":let varvar = expr" and ":for varvar in expr".
2005 * For "[var, var]" increment "*var_count" for each variable.
2006 * for "[var, var; var]" set "semicolon".
2007 * Return NULL for an error.
2008 */
2009 static char_u *
2010skip_var_list(arg, var_count, semicolon)
2011 char_u *arg;
2012 int *var_count;
2013 int *semicolon;
2014{
2015 char_u *p, *s;
2016
2017 if (*arg == '[')
2018 {
2019 /* "[var, var]": find the matching ']'. */
2020 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002021 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 {
2023 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2024 s = skip_var_one(p);
2025 if (s == p)
2026 {
2027 EMSG2(_(e_invarg2), p);
2028 return NULL;
2029 }
2030 ++*var_count;
2031
2032 p = skipwhite(s);
2033 if (*p == ']')
2034 break;
2035 else if (*p == ';')
2036 {
2037 if (*semicolon == 1)
2038 {
2039 EMSG(_("Double ; in list of variables"));
2040 return NULL;
2041 }
2042 *semicolon = 1;
2043 }
2044 else if (*p != ',')
2045 {
2046 EMSG2(_(e_invarg2), p);
2047 return NULL;
2048 }
2049 }
2050 return p + 1;
2051 }
2052 else
2053 return skip_var_one(arg);
2054}
2055
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002056/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002057 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002058 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002059 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002060 static char_u *
2061skip_var_one(arg)
2062 char_u *arg;
2063{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002064 if (*arg == '@' && arg[1] != NUL)
2065 return arg + 2;
2066 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2067 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002068}
2069
Bram Moolenaara7043832005-01-21 11:56:39 +00002070/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002071 * List variables for hashtab "ht" with prefix "prefix".
2072 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002073 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002074 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002075list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002076 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002077 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002079 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080{
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 hashitem_T *hi;
2082 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 int todo;
2084
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002085 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2087 {
2088 if (!HASHITEM_EMPTY(hi))
2089 {
2090 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 di = HI2DI(hi);
2092 if (empty || di->di_tv.v_type != VAR_STRING
2093 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002094 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002095 }
2096 }
2097}
2098
2099/*
2100 * List global variables.
2101 */
2102 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002103list_glob_vars(first)
2104 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002105{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002106 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002107}
2108
2109/*
2110 * List buffer variables.
2111 */
2112 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113list_buf_vars(first)
2114 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002116 char_u numbuf[NUMBUFLEN];
2117
Bram Moolenaar429fa852013-04-15 12:27:36 +02002118 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120
2121 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2123 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002124}
2125
2126/*
2127 * List window variables.
2128 */
2129 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130list_win_vars(first)
2131 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002132{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002133 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002135}
2136
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002137#ifdef FEAT_WINDOWS
2138/*
2139 * List tab page variables.
2140 */
2141 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142list_tab_vars(first)
2143 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002144{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002145 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147}
2148#endif
2149
Bram Moolenaara7043832005-01-21 11:56:39 +00002150/*
2151 * List Vim variables.
2152 */
2153 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154list_vim_vars(first)
2155 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158}
2159
2160/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002161 * List script-local variables, if there is a script.
2162 */
2163 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164list_script_vars(first)
2165 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002166{
2167 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2169 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002170}
2171
2172/*
2173 * List function variables, if there is a function.
2174 */
2175 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176list_func_vars(first)
2177 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002178{
2179 if (current_funccal != NULL)
2180 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002182}
2183
2184/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 * List variables in "arg".
2186 */
2187 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 exarg_T *eap;
2190 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192{
2193 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002194 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002196 char_u *name_start;
2197 char_u *arg_subsc;
2198 char_u *tofree;
2199 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200
2201 while (!ends_excmd(*arg) && !got_int)
2202 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002205 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2207 {
2208 emsg_severe = TRUE;
2209 EMSG(_(e_trailing));
2210 break;
2211 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 /* get_name_len() takes care of expanding curly braces */
2216 name_start = name = arg;
2217 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2218 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 /* This is mainly to keep test 49 working: when expanding
2221 * curly braces fails overrule the exception error message. */
2222 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002224 emsg_severe = TRUE;
2225 EMSG2(_(e_invarg2), arg);
2226 break;
2227 }
2228 error = TRUE;
2229 }
2230 else
2231 {
2232 if (tofree != NULL)
2233 name = tofree;
2234 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 else
2237 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 /* handle d.key, l[idx], f(expr) */
2239 arg_subsc = arg;
2240 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002241 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002248 case 'g': list_glob_vars(first); break;
2249 case 'b': list_buf_vars(first); break;
2250 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002251#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002252 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002253#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002254 case 'v': list_vim_vars(first); break;
2255 case 's': list_script_vars(first); break;
2256 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 default:
2258 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002259 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002260 }
2261 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 {
2263 char_u numbuf[NUMBUFLEN];
2264 char_u *tf;
2265 int c;
2266 char_u *s;
2267
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002268 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 c = *arg;
2270 *arg = NUL;
2271 list_one_var_a((char_u *)"",
2272 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002273 tv.v_type,
2274 s == NULL ? (char_u *)"" : s,
2275 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 *arg = c;
2277 vim_free(tf);
2278 }
2279 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 }
2282 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283
2284 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286
2287 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 }
2289
2290 return arg;
2291}
2292
2293/*
2294 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2295 * Returns a pointer to the char just after the var name.
2296 * Returns NULL if there is an error.
2297 */
2298 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002299ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002301 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002303 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002304 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305{
2306 int c1;
2307 char_u *name;
2308 char_u *p;
2309 char_u *arg_end = NULL;
2310 int len;
2311 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002312 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313
2314 /*
2315 * ":let $VAR = expr": Set environment variable.
2316 */
2317 if (*arg == '$')
2318 {
2319 /* Find the end of the name. */
2320 ++arg;
2321 name = arg;
2322 len = get_env_len(&arg);
2323 if (len == 0)
2324 EMSG2(_(e_invarg2), name - 1);
2325 else
2326 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002327 if (op != NULL && (*op == '+' || *op == '-'))
2328 EMSG2(_(e_letwrong), op);
2329 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002330 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002332 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 {
2334 c1 = name[len];
2335 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002336 p = get_tv_string_chk(tv);
2337 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 {
2339 int mustfree = FALSE;
2340 char_u *s = vim_getenv(name, &mustfree);
2341
2342 if (s != NULL)
2343 {
2344 p = tofree = concat_str(s, p);
2345 if (mustfree)
2346 vim_free(s);
2347 }
2348 }
2349 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002350 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002351 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 if (STRICMP(name, "HOME") == 0)
2353 init_homedir();
2354 else if (didset_vim && STRICMP(name, "VIM") == 0)
2355 didset_vim = FALSE;
2356 else if (didset_vimruntime
2357 && STRICMP(name, "VIMRUNTIME") == 0)
2358 didset_vimruntime = FALSE;
2359 arg_end = arg;
2360 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002362 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 }
2364 }
2365 }
2366
2367 /*
2368 * ":let &option = expr": Set option value.
2369 * ":let &l:option = expr": Set local option value.
2370 * ":let &g:option = expr": Set global option value.
2371 */
2372 else if (*arg == '&')
2373 {
2374 /* Find the end of the name. */
2375 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002376 if (p == NULL || (endchars != NULL
2377 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 EMSG(_(e_letunexp));
2379 else
2380 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 long n;
2382 int opt_type;
2383 long numval;
2384 char_u *stringval = NULL;
2385 char_u *s;
2386
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 c1 = *p;
2388 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002389
2390 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002391 s = get_tv_string_chk(tv); /* != NULL if number or string */
2392 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393 {
2394 opt_type = get_option_value(arg, &numval,
2395 &stringval, opt_flags);
2396 if ((opt_type == 1 && *op == '.')
2397 || (opt_type == 0 && *op != '.'))
2398 EMSG2(_(e_letwrong), op);
2399 else
2400 {
2401 if (opt_type == 1) /* number */
2402 {
2403 if (*op == '+')
2404 n = numval + n;
2405 else
2406 n = numval - n;
2407 }
2408 else if (opt_type == 0 && stringval != NULL) /* string */
2409 {
2410 s = concat_str(stringval, s);
2411 vim_free(stringval);
2412 stringval = s;
2413 }
2414 }
2415 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002416 if (s != NULL)
2417 {
2418 set_option_value(arg, n, s, opt_flags);
2419 arg_end = p;
2420 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002422 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 }
2424 }
2425
2426 /*
2427 * ":let @r = expr": Set register contents.
2428 */
2429 else if (*arg == '@')
2430 {
2431 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 if (op != NULL && (*op == '+' || *op == '-'))
2433 EMSG2(_(e_letwrong), op);
2434 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002435 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 EMSG(_(e_letunexp));
2437 else
2438 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002439 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 char_u *s;
2441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 p = get_tv_string_chk(tv);
2443 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002445 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 if (s != NULL)
2447 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002448 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 vim_free(s);
2450 }
2451 }
2452 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002453 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 arg_end = arg + 1;
2456 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002457 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 }
2459 }
2460
2461 /*
2462 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002465 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002467 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002469 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2473 EMSG(_(e_letunexp));
2474 else
2475 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002476 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 arg_end = p;
2478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 }
2482
2483 else
2484 EMSG2(_(e_invarg2), arg);
2485
2486 return arg_end;
2487}
2488
2489/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2491 */
2492 static int
2493check_changedtick(arg)
2494 char_u *arg;
2495{
2496 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2497 {
2498 EMSG2(_(e_readonlyvar), arg);
2499 return TRUE;
2500 }
2501 return FALSE;
2502}
2503
2504/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 * Get an lval: variable, Dict item or List item that can be assigned a value
2506 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2507 * "name.key", "name.key[expr]" etc.
2508 * Indexing only works if "name" is an existing List or Dictionary.
2509 * "name" points to the start of the name.
2510 * If "rettv" is not NULL it points to the value to be assigned.
2511 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2512 * wrong; must end in space or cmd separator.
2513 *
2514 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002515 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 */
2518 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002519get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002521 typval_T *rettv;
2522 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 int unlet;
2524 int skip;
2525 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002526 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 char_u *expr_start, *expr_end;
2530 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002531 dictitem_T *v;
2532 typval_T var1;
2533 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542
2543 if (skip)
2544 {
2545 /* When skipping just find the end of the name. */
2546 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002547 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 }
2549
2550 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002551 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 if (expr_start != NULL)
2553 {
2554 /* Don't expand the name when we already know there is an error. */
2555 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2556 && *p != '[' && *p != '.')
2557 {
2558 EMSG(_(e_trailing));
2559 return NULL;
2560 }
2561
2562 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2563 if (lp->ll_exp_name == NULL)
2564 {
2565 /* Report an invalid expression in braces, unless the
2566 * expression evaluation has been cancelled due to an
2567 * aborting error, an interrupt, or an exception. */
2568 if (!aborting() && !quiet)
2569 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002570 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 EMSG2(_(e_invarg2), name);
2572 return NULL;
2573 }
2574 }
2575 lp->ll_name = lp->ll_exp_name;
2576 }
2577 else
2578 lp->ll_name = name;
2579
2580 /* Without [idx] or .key we are done. */
2581 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2582 return p;
2583
2584 cc = *p;
2585 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002586 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 if (v == NULL && !quiet)
2588 EMSG2(_(e_undefvar), lp->ll_name);
2589 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002590 if (v == NULL)
2591 return NULL;
2592
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 /*
2594 * Loop until no more [idx] or .key is following.
2595 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002596 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2600 && !(lp->ll_tv->v_type == VAR_DICT
2601 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (!quiet)
2604 EMSG(_("E689: Can only index a List or Dictionary"));
2605 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (!quiet)
2610 EMSG(_("E708: [:] must come last"));
2611 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002613
Bram Moolenaar8c711452005-01-14 21:53:12 +00002614 len = -1;
2615 if (*p == '.')
2616 {
2617 key = p + 1;
2618 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2619 ;
2620 if (len == 0)
2621 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 if (!quiet)
2623 EMSG(_(e_emptykey));
2624 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002625 }
2626 p = key + len;
2627 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628 else
2629 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 if (*p == ':')
2633 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 else
2635 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 empty1 = FALSE;
2637 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002639 if (get_tv_string_chk(&var1) == NULL)
2640 {
2641 /* not a number or string */
2642 clear_tv(&var1);
2643 return NULL;
2644 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 }
2646
2647 /* Optionally get the second index [ :expr]. */
2648 if (*p == ':')
2649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002653 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002654 if (!empty1)
2655 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002657 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (rettv != NULL && (rettv->v_type != VAR_LIST
2659 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (!quiet)
2662 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 if (!empty1)
2664 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 }
2667 p = skipwhite(p + 1);
2668 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 else
2671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (!empty1)
2676 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002679 if (get_tv_string_chk(&var2) == NULL)
2680 {
2681 /* not a number or string */
2682 if (!empty1)
2683 clear_tv(&var1);
2684 clear_tv(&var2);
2685 return NULL;
2686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (!quiet)
2696 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (!empty1)
2698 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
2703
2704 /* Skip to past ']'. */
2705 ++p;
2706 }
2707
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 {
2710 if (len == -1)
2711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002713 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 if (*key == NUL)
2715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (!quiet)
2717 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 }
2721 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002722 lp->ll_list = NULL;
2723 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002724 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002725
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002726 /* When assigning to a scope dictionary check that a function and
2727 * variable name is valid (only variable name unless it is l: or
2728 * g: dictionary). Disallow overwriting a builtin function. */
2729 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002730 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002731 int prevval;
2732 int wrong;
2733
2734 if (len != -1)
2735 {
2736 prevval = key[len];
2737 key[len] = NUL;
2738 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002739 else
2740 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002741 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2742 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002743 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002744 || !valid_varname(key);
2745 if (len != -1)
2746 key[len] = prevval;
2747 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002748 return NULL;
2749 }
2750
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002753 /* Can't add "v:" variable. */
2754 if (lp->ll_dict == &vimvardict)
2755 {
2756 EMSG2(_(e_illvar), name);
2757 return NULL;
2758 }
2759
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002760 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002764 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 if (len == -1)
2766 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 }
2769 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 if (len == -1)
2774 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 p = NULL;
2777 break;
2778 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002779 /* existing variable, need to check if it can be changed */
2780 else if (var_check_ro(lp->ll_di->di_flags, name))
2781 return NULL;
2782
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 if (len == -1)
2784 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 }
2787 else
2788 {
2789 /*
2790 * Get the number and item for the only or first index of the List.
2791 */
2792 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 else
2795 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002796 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 clear_tv(&var1);
2798 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002799 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_list = lp->ll_tv->vval.v_list;
2801 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2802 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002804 if (lp->ll_n1 < 0)
2805 {
2806 lp->ll_n1 = 0;
2807 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2808 }
2809 }
2810 if (lp->ll_li == NULL)
2811 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002814 if (!quiet)
2815 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 }
2818
2819 /*
2820 * May need to find the item or absolute index for the second
2821 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 * When no index given: "lp->ll_empty2" is TRUE.
2823 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002827 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002833 {
2834 if (!quiet)
2835 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002837 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 }
2840
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2842 if (lp->ll_n1 < 0)
2843 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2844 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002845 {
2846 if (!quiet)
2847 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002849 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002850 }
2851
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002854 }
2855
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 return p;
2857}
2858
2859/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002860 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 */
2862 static void
2863clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002864 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865{
2866 vim_free(lp->ll_exp_name);
2867 vim_free(lp->ll_newkey);
2868}
2869
2870/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002871 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002873 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 */
2875 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002876set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002877 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002881 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882{
2883 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 listitem_T *ri;
2885 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886
2887 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002888 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002890 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 cc = *endp;
2892 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002893 if (op != NULL && *op != '=')
2894 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002897 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002898 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002899 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002900 {
2901 if (tv_op(&tv, rettv, op) == OK)
2902 set_var(lp->ll_name, &tv, FALSE);
2903 clear_tv(&tv);
2904 }
2905 }
2906 else
2907 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002911 else if (tv_check_lock(lp->ll_newkey == NULL
2912 ? lp->ll_tv->v_lock
2913 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2914 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 else if (lp->ll_range)
2916 {
2917 /*
2918 * Assign the List values to the list items.
2919 */
2920 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002921 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 if (op != NULL && *op != '=')
2923 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2924 else
2925 {
2926 clear_tv(&lp->ll_li->li_tv);
2927 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2928 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 ri = ri->li_next;
2930 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2931 break;
2932 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002935 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002936 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 ri = NULL;
2938 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002939 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002940 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 lp->ll_li = lp->ll_li->li_next;
2942 ++lp->ll_n1;
2943 }
2944 if (ri != NULL)
2945 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 else if (lp->ll_empty2
2947 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 : lp->ll_n1 != lp->ll_n2)
2949 EMSG(_("E711: List value has not enough items"));
2950 }
2951 else
2952 {
2953 /*
2954 * Assign to a List or Dictionary item.
2955 */
2956 if (lp->ll_newkey != NULL)
2957 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 if (op != NULL && *op != '=')
2959 {
2960 EMSG2(_(e_letwrong), op);
2961 return;
2962 }
2963
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002965 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 if (di == NULL)
2967 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002968 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2969 {
2970 vim_free(di);
2971 return;
2972 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002974 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002975 else if (op != NULL && *op != '=')
2976 {
2977 tv_op(lp->ll_tv, rettv, op);
2978 return;
2979 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002980 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002982
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002983 /*
2984 * Assign the value to the variable or list item.
2985 */
2986 if (copy)
2987 copy_tv(rettv, lp->ll_tv);
2988 else
2989 {
2990 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002991 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002993 }
2994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002995}
2996
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002997/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002998 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2999 * Returns OK or FAIL.
3000 */
3001 static int
3002tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003003 typval_T *tv1;
3004 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 char_u *op;
3006{
3007 long n;
3008 char_u numbuf[NUMBUFLEN];
3009 char_u *s;
3010
3011 /* Can't do anything with a Funcref or a Dict on the right. */
3012 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3013 {
3014 switch (tv1->v_type)
3015 {
3016 case VAR_DICT:
3017 case VAR_FUNC:
3018 break;
3019
3020 case VAR_LIST:
3021 if (*op != '+' || tv2->v_type != VAR_LIST)
3022 break;
3023 /* List += List */
3024 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3025 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3026 return OK;
3027
3028 case VAR_NUMBER:
3029 case VAR_STRING:
3030 if (tv2->v_type == VAR_LIST)
3031 break;
3032 if (*op == '+' || *op == '-')
3033 {
3034 /* nr += nr or nr -= nr*/
3035 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003036#ifdef FEAT_FLOAT
3037 if (tv2->v_type == VAR_FLOAT)
3038 {
3039 float_T f = n;
3040
3041 if (*op == '+')
3042 f += tv2->vval.v_float;
3043 else
3044 f -= tv2->vval.v_float;
3045 clear_tv(tv1);
3046 tv1->v_type = VAR_FLOAT;
3047 tv1->vval.v_float = f;
3048 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003050#endif
3051 {
3052 if (*op == '+')
3053 n += get_tv_number(tv2);
3054 else
3055 n -= get_tv_number(tv2);
3056 clear_tv(tv1);
3057 tv1->v_type = VAR_NUMBER;
3058 tv1->vval.v_number = n;
3059 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003060 }
3061 else
3062 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063 if (tv2->v_type == VAR_FLOAT)
3064 break;
3065
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003066 /* str .= str */
3067 s = get_tv_string(tv1);
3068 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3069 clear_tv(tv1);
3070 tv1->v_type = VAR_STRING;
3071 tv1->vval.v_string = s;
3072 }
3073 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003074
3075#ifdef FEAT_FLOAT
3076 case VAR_FLOAT:
3077 {
3078 float_T f;
3079
3080 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3081 && tv2->v_type != VAR_NUMBER
3082 && tv2->v_type != VAR_STRING))
3083 break;
3084 if (tv2->v_type == VAR_FLOAT)
3085 f = tv2->vval.v_float;
3086 else
3087 f = get_tv_number(tv2);
3088 if (*op == '+')
3089 tv1->vval.v_float += f;
3090 else
3091 tv1->vval.v_float -= f;
3092 }
3093 return OK;
3094#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003095 }
3096 }
3097
3098 EMSG2(_(e_letwrong), op);
3099 return FAIL;
3100}
3101
3102/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003103 * Add a watcher to a list.
3104 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003105 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003106list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003107 list_T *l;
3108 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003109{
3110 lw->lw_next = l->lv_watch;
3111 l->lv_watch = lw;
3112}
3113
3114/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003115 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003116 * No warning when it isn't found...
3117 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003118 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003120 list_T *l;
3121 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122{
Bram Moolenaar33570922005-01-25 22:26:29 +00003123 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124
3125 lwp = &l->lv_watch;
3126 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3127 {
3128 if (lw == lwrem)
3129 {
3130 *lwp = lw->lw_next;
3131 break;
3132 }
3133 lwp = &lw->lw_next;
3134 }
3135}
3136
3137/*
3138 * Just before removing an item from a list: advance watchers to the next
3139 * item.
3140 */
3141 static void
3142list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003143 list_T *l;
3144 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145{
Bram Moolenaar33570922005-01-25 22:26:29 +00003146 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003147
3148 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3149 if (lw->lw_item == item)
3150 lw->lw_item = item->li_next;
3151}
3152
3153/*
3154 * Evaluate the expression used in a ":for var in expr" command.
3155 * "arg" points to "var".
3156 * Set "*errp" to TRUE for an error, FALSE otherwise;
3157 * Return a pointer that holds the info. Null when there is an error.
3158 */
3159 void *
3160eval_for_line(arg, errp, nextcmdp, skip)
3161 char_u *arg;
3162 int *errp;
3163 char_u **nextcmdp;
3164 int skip;
3165{
Bram Moolenaar33570922005-01-25 22:26:29 +00003166 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 typval_T tv;
3169 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170
3171 *errp = TRUE; /* default: there is an error */
3172
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 if (fi == NULL)
3175 return NULL;
3176
3177 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3178 if (expr == NULL)
3179 return fi;
3180
3181 expr = skipwhite(expr);
3182 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3183 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003184 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185 return fi;
3186 }
3187
3188 if (skip)
3189 ++emsg_skip;
3190 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3191 {
3192 *errp = FALSE;
3193 if (!skip)
3194 {
3195 l = tv.vval.v_list;
3196 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003197 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003199 clear_tv(&tv);
3200 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201 else
3202 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003203 /* No need to increment the refcount, it's already set for the
3204 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 fi->fi_list = l;
3206 list_add_watch(l, &fi->fi_lw);
3207 fi->fi_lw.lw_item = l->lv_first;
3208 }
3209 }
3210 }
3211 if (skip)
3212 --emsg_skip;
3213
3214 return fi;
3215}
3216
3217/*
3218 * Use the first item in a ":for" list. Advance to the next.
3219 * Assign the values to the variable (list). "arg" points to the first one.
3220 * Return TRUE when a valid item was found, FALSE when at end of list or
3221 * something wrong.
3222 */
3223 int
3224next_for_item(fi_void, arg)
3225 void *fi_void;
3226 char_u *arg;
3227{
Bram Moolenaar33570922005-01-25 22:26:29 +00003228 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003230 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003231
3232 item = fi->fi_lw.lw_item;
3233 if (item == NULL)
3234 result = FALSE;
3235 else
3236 {
3237 fi->fi_lw.lw_item = item->li_next;
3238 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3239 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3240 }
3241 return result;
3242}
3243
3244/*
3245 * Free the structure used to store info used by ":for".
3246 */
3247 void
3248free_for_info(fi_void)
3249 void *fi_void;
3250{
Bram Moolenaar33570922005-01-25 22:26:29 +00003251 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003253 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003254 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003256 list_unref(fi->fi_list);
3257 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258 vim_free(fi);
3259}
3260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3262
3263 void
3264set_context_for_expression(xp, arg, cmdidx)
3265 expand_T *xp;
3266 char_u *arg;
3267 cmdidx_T cmdidx;
3268{
3269 int got_eq = FALSE;
3270 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 if (cmdidx == CMD_let)
3274 {
3275 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003276 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 {
3278 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003279 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 {
3281 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003282 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 if (vim_iswhite(*p))
3284 break;
3285 }
3286 return;
3287 }
3288 }
3289 else
3290 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3291 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 while ((xp->xp_pattern = vim_strpbrk(arg,
3293 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3294 {
3295 c = *xp->xp_pattern;
3296 if (c == '&')
3297 {
3298 c = xp->xp_pattern[1];
3299 if (c == '&')
3300 {
3301 ++xp->xp_pattern;
3302 xp->xp_context = cmdidx != CMD_let || got_eq
3303 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3304 }
3305 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003308 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3309 xp->xp_pattern += 2;
3310
3311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
3313 else if (c == '$')
3314 {
3315 /* environment variable */
3316 xp->xp_context = EXPAND_ENV_VARS;
3317 }
3318 else if (c == '=')
3319 {
3320 got_eq = TRUE;
3321 xp->xp_context = EXPAND_EXPRESSION;
3322 }
3323 else if (c == '<'
3324 && xp->xp_context == EXPAND_FUNCTIONS
3325 && vim_strchr(xp->xp_pattern, '(') == NULL)
3326 {
3327 /* Function name can start with "<SNR>" */
3328 break;
3329 }
3330 else if (cmdidx != CMD_let || got_eq)
3331 {
3332 if (c == '"') /* string */
3333 {
3334 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3335 if (c == '\\' && xp->xp_pattern[1] != NUL)
3336 ++xp->xp_pattern;
3337 xp->xp_context = EXPAND_NOTHING;
3338 }
3339 else if (c == '\'') /* literal string */
3340 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003341 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3343 /* skip */ ;
3344 xp->xp_context = EXPAND_NOTHING;
3345 }
3346 else if (c == '|')
3347 {
3348 if (xp->xp_pattern[1] == '|')
3349 {
3350 ++xp->xp_pattern;
3351 xp->xp_context = EXPAND_EXPRESSION;
3352 }
3353 else
3354 xp->xp_context = EXPAND_COMMANDS;
3355 }
3356 else
3357 xp->xp_context = EXPAND_EXPRESSION;
3358 }
3359 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003360 /* Doesn't look like something valid, expand as an expression
3361 * anyway. */
3362 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 arg = xp->xp_pattern;
3364 if (*arg != NUL)
3365 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3366 /* skip */ ;
3367 }
3368 xp->xp_pattern = arg;
3369}
3370
3371#endif /* FEAT_CMDL_COMPL */
3372
3373/*
3374 * ":1,25call func(arg1, arg2)" function call.
3375 */
3376 void
3377ex_call(eap)
3378 exarg_T *eap;
3379{
3380 char_u *arg = eap->arg;
3381 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003383 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003385 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 linenr_T lnum;
3387 int doesrange;
3388 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003389 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003391 if (eap->skip)
3392 {
3393 /* trans_function_name() doesn't work well when skipping, use eval0()
3394 * instead to skip to any following command, e.g. for:
3395 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003396 ++emsg_skip;
3397 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3398 clear_tv(&rettv);
3399 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003400 return;
3401 }
3402
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003403 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003404 if (fudi.fd_newkey != NULL)
3405 {
3406 /* Still need to give an error message for missing key. */
3407 EMSG2(_(e_dictkey), fudi.fd_newkey);
3408 vim_free(fudi.fd_newkey);
3409 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003410 if (tofree == NULL)
3411 return;
3412
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003413 /* Increase refcount on dictionary, it could get deleted when evaluating
3414 * the arguments. */
3415 if (fudi.fd_dict != NULL)
3416 ++fudi.fd_dict->dv_refcount;
3417
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003418 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003419 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003420 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaar532c7802005-01-27 14:44:31 +00003422 /* Skip white space to allow ":call func ()". Not good, but required for
3423 * backward compatibility. */
3424 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
3427 if (*startarg != '(')
3428 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003429 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 goto end;
3431 }
3432
3433 /*
3434 * When skipping, evaluate the function once, to find the end of the
3435 * arguments.
3436 * When the function takes a range, this is discovered after the first
3437 * call, and the loop is broken.
3438 */
3439 if (eap->skip)
3440 {
3441 ++emsg_skip;
3442 lnum = eap->line2; /* do it once, also with an invalid range */
3443 }
3444 else
3445 lnum = eap->line1;
3446 for ( ; lnum <= eap->line2; ++lnum)
3447 {
3448 if (!eap->skip && eap->addr_count > 0)
3449 {
3450 curwin->w_cursor.lnum = lnum;
3451 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003452#ifdef FEAT_VIRTUALEDIT
3453 curwin->w_cursor.coladd = 0;
3454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 }
3456 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003457 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003458 eap->line1, eap->line2, &doesrange,
3459 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
3461 failed = TRUE;
3462 break;
3463 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003464
3465 /* Handle a function returning a Funcref, Dictionary or List. */
3466 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3467 {
3468 failed = TRUE;
3469 break;
3470 }
3471
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003472 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (doesrange || eap->skip)
3474 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003475
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003477 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003478 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003479 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (aborting())
3481 break;
3482 }
3483 if (eap->skip)
3484 --emsg_skip;
3485
3486 if (!failed)
3487 {
3488 /* Check for trailing illegal characters and a following command. */
3489 if (!ends_excmd(*arg))
3490 {
3491 emsg_severe = TRUE;
3492 EMSG(_(e_trailing));
3493 }
3494 else
3495 eap->nextcmd = check_nextcmd(arg);
3496 }
3497
3498end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003499 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003500 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501}
3502
3503/*
3504 * ":unlet[!] var1 ... " command.
3505 */
3506 void
3507ex_unlet(eap)
3508 exarg_T *eap;
3509{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003510 ex_unletlock(eap, eap->arg, 0);
3511}
3512
3513/*
3514 * ":lockvar" and ":unlockvar" commands
3515 */
3516 void
3517ex_lockvar(eap)
3518 exarg_T *eap;
3519{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003521 int deep = 2;
3522
3523 if (eap->forceit)
3524 deep = -1;
3525 else if (vim_isdigit(*arg))
3526 {
3527 deep = getdigits(&arg);
3528 arg = skipwhite(arg);
3529 }
3530
3531 ex_unletlock(eap, arg, deep);
3532}
3533
3534/*
3535 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3536 */
3537 static void
3538ex_unletlock(eap, argstart, deep)
3539 exarg_T *eap;
3540 char_u *argstart;
3541 int deep;
3542{
3543 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003546 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
3548 do
3549 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003550 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003551 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3552 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003553 if (lv.ll_name == NULL)
3554 error = TRUE; /* error but continue parsing */
3555 if (name_end == NULL || (!vim_iswhite(*name_end)
3556 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003558 if (name_end != NULL)
3559 {
3560 emsg_severe = TRUE;
3561 EMSG(_(e_trailing));
3562 }
3563 if (!(eap->skip || error))
3564 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 break;
3566 }
3567
3568 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003569 {
3570 if (eap->cmdidx == CMD_unlet)
3571 {
3572 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3573 error = TRUE;
3574 }
3575 else
3576 {
3577 if (do_lock_var(&lv, name_end, deep,
3578 eap->cmdidx == CMD_lockvar) == FAIL)
3579 error = TRUE;
3580 }
3581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003583 if (!eap->skip)
3584 clear_lval(&lv);
3585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 arg = skipwhite(name_end);
3587 } while (!ends_excmd(*arg));
3588
3589 eap->nextcmd = check_nextcmd(arg);
3590}
3591
Bram Moolenaar8c711452005-01-14 21:53:12 +00003592 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003593do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003594 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003595 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 int forceit;
3597{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 int ret = OK;
3599 int cc;
3600
3601 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 cc = *name_end;
3604 *name_end = NUL;
3605
3606 /* Normal name or expanded name. */
3607 if (check_changedtick(lp->ll_name))
3608 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003609 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003610 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003612 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003613 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3614 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 else if (lp->ll_range)
3616 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003617 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618
3619 /* Delete a range of List items. */
3620 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3621 {
3622 li = lp->ll_li->li_next;
3623 listitem_remove(lp->ll_list, lp->ll_li);
3624 lp->ll_li = li;
3625 ++lp->ll_n1;
3626 }
3627 }
3628 else
3629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003630 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 /* unlet a List item. */
3632 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003635 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 }
3637
3638 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003639}
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641/*
3642 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003643 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 */
3645 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003648 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649{
Bram Moolenaar33570922005-01-25 22:26:29 +00003650 hashtab_T *ht;
3651 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003652 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003653 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
Bram Moolenaar33570922005-01-25 22:26:29 +00003655 ht = find_var_ht(name, &varname);
3656 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 hi = hash_find(ht, varname);
3659 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003660 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003661 di = HI2DI(hi);
3662 if (var_check_fixed(di->di_flags, name)
3663 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003664 return FAIL;
3665 delete_var(ht, hi);
3666 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003669 if (forceit)
3670 return OK;
3671 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 return FAIL;
3673}
3674
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003675/*
3676 * Lock or unlock variable indicated by "lp".
3677 * "deep" is the levels to go (-1 for unlimited);
3678 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3679 */
3680 static int
3681do_lock_var(lp, name_end, deep, lock)
3682 lval_T *lp;
3683 char_u *name_end;
3684 int deep;
3685 int lock;
3686{
3687 int ret = OK;
3688 int cc;
3689 dictitem_T *di;
3690
3691 if (deep == 0) /* nothing to do */
3692 return OK;
3693
3694 if (lp->ll_tv == NULL)
3695 {
3696 cc = *name_end;
3697 *name_end = NUL;
3698
3699 /* Normal name or expanded name. */
3700 if (check_changedtick(lp->ll_name))
3701 ret = FAIL;
3702 else
3703 {
3704 di = find_var(lp->ll_name, NULL);
3705 if (di == NULL)
3706 ret = FAIL;
3707 else
3708 {
3709 if (lock)
3710 di->di_flags |= DI_FLAGS_LOCK;
3711 else
3712 di->di_flags &= ~DI_FLAGS_LOCK;
3713 item_lock(&di->di_tv, deep, lock);
3714 }
3715 }
3716 *name_end = cc;
3717 }
3718 else if (lp->ll_range)
3719 {
3720 listitem_T *li = lp->ll_li;
3721
3722 /* (un)lock a range of List items. */
3723 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3724 {
3725 item_lock(&li->li_tv, deep, lock);
3726 li = li->li_next;
3727 ++lp->ll_n1;
3728 }
3729 }
3730 else if (lp->ll_list != NULL)
3731 /* (un)lock a List item. */
3732 item_lock(&lp->ll_li->li_tv, deep, lock);
3733 else
3734 /* un(lock) a Dictionary item. */
3735 item_lock(&lp->ll_di->di_tv, deep, lock);
3736
3737 return ret;
3738}
3739
3740/*
3741 * Lock or unlock an item. "deep" is nr of levels to go.
3742 */
3743 static void
3744item_lock(tv, deep, lock)
3745 typval_T *tv;
3746 int deep;
3747 int lock;
3748{
3749 static int recurse = 0;
3750 list_T *l;
3751 listitem_T *li;
3752 dict_T *d;
3753 hashitem_T *hi;
3754 int todo;
3755
3756 if (recurse >= DICT_MAXNEST)
3757 {
3758 EMSG(_("E743: variable nested too deep for (un)lock"));
3759 return;
3760 }
3761 if (deep == 0)
3762 return;
3763 ++recurse;
3764
3765 /* lock/unlock the item itself */
3766 if (lock)
3767 tv->v_lock |= VAR_LOCKED;
3768 else
3769 tv->v_lock &= ~VAR_LOCKED;
3770
3771 switch (tv->v_type)
3772 {
3773 case VAR_LIST:
3774 if ((l = tv->vval.v_list) != NULL)
3775 {
3776 if (lock)
3777 l->lv_lock |= VAR_LOCKED;
3778 else
3779 l->lv_lock &= ~VAR_LOCKED;
3780 if (deep < 0 || deep > 1)
3781 /* recursive: lock/unlock the items the List contains */
3782 for (li = l->lv_first; li != NULL; li = li->li_next)
3783 item_lock(&li->li_tv, deep - 1, lock);
3784 }
3785 break;
3786 case VAR_DICT:
3787 if ((d = tv->vval.v_dict) != NULL)
3788 {
3789 if (lock)
3790 d->dv_lock |= VAR_LOCKED;
3791 else
3792 d->dv_lock &= ~VAR_LOCKED;
3793 if (deep < 0 || deep > 1)
3794 {
3795 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003796 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003797 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3798 {
3799 if (!HASHITEM_EMPTY(hi))
3800 {
3801 --todo;
3802 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3803 }
3804 }
3805 }
3806 }
3807 }
3808 --recurse;
3809}
3810
Bram Moolenaara40058a2005-07-11 22:42:07 +00003811/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003812 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3813 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003814 */
3815 static int
3816tv_islocked(tv)
3817 typval_T *tv;
3818{
3819 return (tv->v_lock & VAR_LOCKED)
3820 || (tv->v_type == VAR_LIST
3821 && tv->vval.v_list != NULL
3822 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3823 || (tv->v_type == VAR_DICT
3824 && tv->vval.v_dict != NULL
3825 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3826}
3827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3829/*
3830 * Delete all "menutrans_" variables.
3831 */
3832 void
3833del_menutrans_vars()
3834{
Bram Moolenaar33570922005-01-25 22:26:29 +00003835 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003836 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837
Bram Moolenaar33570922005-01-25 22:26:29 +00003838 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003839 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003840 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003841 {
3842 if (!HASHITEM_EMPTY(hi))
3843 {
3844 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003845 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3846 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003847 }
3848 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003849 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850}
3851#endif
3852
3853#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3854
3855/*
3856 * Local string buffer for the next two functions to store a variable name
3857 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3858 * get_user_var_name().
3859 */
3860
3861static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3862
3863static char_u *varnamebuf = NULL;
3864static int varnamebuflen = 0;
3865
3866/*
3867 * Function to concatenate a prefix and a variable name.
3868 */
3869 static char_u *
3870cat_prefix_varname(prefix, name)
3871 int prefix;
3872 char_u *name;
3873{
3874 int len;
3875
3876 len = (int)STRLEN(name) + 3;
3877 if (len > varnamebuflen)
3878 {
3879 vim_free(varnamebuf);
3880 len += 10; /* some additional space */
3881 varnamebuf = alloc(len);
3882 if (varnamebuf == NULL)
3883 {
3884 varnamebuflen = 0;
3885 return NULL;
3886 }
3887 varnamebuflen = len;
3888 }
3889 *varnamebuf = prefix;
3890 varnamebuf[1] = ':';
3891 STRCPY(varnamebuf + 2, name);
3892 return varnamebuf;
3893}
3894
3895/*
3896 * Function given to ExpandGeneric() to obtain the list of user defined
3897 * (global/buffer/window/built-in) variable names.
3898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 char_u *
3900get_user_var_name(xp, idx)
3901 expand_T *xp;
3902 int idx;
3903{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003904 static long_u gdone;
3905 static long_u bdone;
3906 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003907#ifdef FEAT_WINDOWS
3908 static long_u tdone;
3909#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003910 static int vidx;
3911 static hashitem_T *hi;
3912 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
3914 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003915 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003916 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003917#ifdef FEAT_WINDOWS
3918 tdone = 0;
3919#endif
3920 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003921
3922 /* Global variables */
3923 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003925 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003926 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003927 else
3928 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003929 while (HASHITEM_EMPTY(hi))
3930 ++hi;
3931 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3932 return cat_prefix_varname('g', hi->hi_key);
3933 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003935
3936 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003937 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003938 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003942 else
3943 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 while (HASHITEM_EMPTY(hi))
3945 ++hi;
3946 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 return (char_u *)"b:changedtick";
3952 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003953
3954 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003955 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003958 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003960 else
3961 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003962 while (HASHITEM_EMPTY(hi))
3963 ++hi;
3964 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003966
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003967#ifdef FEAT_WINDOWS
3968 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003969 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003970 if (tdone < ht->ht_used)
3971 {
3972 if (tdone++ == 0)
3973 hi = ht->ht_array;
3974 else
3975 ++hi;
3976 while (HASHITEM_EMPTY(hi))
3977 ++hi;
3978 return cat_prefix_varname('t', hi->hi_key);
3979 }
3980#endif
3981
Bram Moolenaar33570922005-01-25 22:26:29 +00003982 /* v: variables */
3983 if (vidx < VV_LEN)
3984 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
3986 vim_free(varnamebuf);
3987 varnamebuf = NULL;
3988 varnamebuflen = 0;
3989 return NULL;
3990}
3991
3992#endif /* FEAT_CMDL_COMPL */
3993
3994/*
3995 * types for expressions.
3996 */
3997typedef enum
3998{
3999 TYPE_UNKNOWN = 0
4000 , TYPE_EQUAL /* == */
4001 , TYPE_NEQUAL /* != */
4002 , TYPE_GREATER /* > */
4003 , TYPE_GEQUAL /* >= */
4004 , TYPE_SMALLER /* < */
4005 , TYPE_SEQUAL /* <= */
4006 , TYPE_MATCH /* =~ */
4007 , TYPE_NOMATCH /* !~ */
4008} exptype_T;
4009
4010/*
4011 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004012 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4014 */
4015
4016/*
4017 * Handle zero level expression.
4018 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004020 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 * Return OK or FAIL.
4022 */
4023 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004024eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 char_u **nextcmd;
4028 int evaluate;
4029{
4030 int ret;
4031 char_u *p;
4032
4033 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 if (ret == FAIL || !ends_excmd(*p))
4036 {
4037 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004038 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 /*
4040 * Report the invalid expression unless the expression evaluation has
4041 * been cancelled due to an aborting error, an interrupt, or an
4042 * exception.
4043 */
4044 if (!aborting())
4045 EMSG2(_(e_invexpr2), arg);
4046 ret = FAIL;
4047 }
4048 if (nextcmd != NULL)
4049 *nextcmd = check_nextcmd(p);
4050
4051 return ret;
4052}
4053
4054/*
4055 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004056 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 *
4058 * "arg" must point to the first non-white of the expression.
4059 * "arg" is advanced to the next non-white after the recognized expression.
4060 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004061 * Note: "rettv.v_lock" is not set.
4062 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 * Return OK or FAIL.
4064 */
4065 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004066eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 int evaluate;
4070{
4071 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004072 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073
4074 /*
4075 * Get the first variable.
4076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004077 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 return FAIL;
4079
4080 if ((*arg)[0] == '?')
4081 {
4082 result = FALSE;
4083 if (evaluate)
4084 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004085 int error = FALSE;
4086
4087 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004089 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004090 if (error)
4091 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093
4094 /*
4095 * Get the second variable.
4096 */
4097 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 return FAIL;
4100
4101 /*
4102 * Check for the ":".
4103 */
4104 if ((*arg)[0] != ':')
4105 {
4106 EMSG(_("E109: Missing ':' after '?'"));
4107 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 return FAIL;
4110 }
4111
4112 /*
4113 * Get the third variable.
4114 */
4115 *arg = skipwhite(*arg + 1);
4116 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4117 {
4118 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 return FAIL;
4121 }
4122 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125
4126 return OK;
4127}
4128
4129/*
4130 * Handle first level expression:
4131 * expr2 || expr2 || expr2 logical OR
4132 *
4133 * "arg" must point to the first non-white of the expression.
4134 * "arg" is advanced to the next non-white after the recognized expression.
4135 *
4136 * Return OK or FAIL.
4137 */
4138 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 int evaluate;
4143{
Bram Moolenaar33570922005-01-25 22:26:29 +00004144 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 long result;
4146 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004147 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 /*
4150 * Get the first variable.
4151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 return FAIL;
4154
4155 /*
4156 * Repeat until there is no following "||".
4157 */
4158 first = TRUE;
4159 result = FALSE;
4160 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4161 {
4162 if (evaluate && first)
4163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 if (error)
4168 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 first = FALSE;
4170 }
4171
4172 /*
4173 * Get the second variable.
4174 */
4175 *arg = skipwhite(*arg + 2);
4176 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4177 return FAIL;
4178
4179 /*
4180 * Compute the result.
4181 */
4182 if (evaluate && !result)
4183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 if (error)
4188 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190 if (evaluate)
4191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 rettv->v_type = VAR_NUMBER;
4193 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195 }
4196
4197 return OK;
4198}
4199
4200/*
4201 * Handle second level expression:
4202 * expr3 && expr3 && expr3 logical AND
4203 *
4204 * "arg" must point to the first non-white of the expression.
4205 * "arg" is advanced to the next non-white after the recognized expression.
4206 *
4207 * Return OK or FAIL.
4208 */
4209 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 int evaluate;
4214{
Bram Moolenaar33570922005-01-25 22:26:29 +00004215 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 long result;
4217 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004218 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
4220 /*
4221 * Get the first variable.
4222 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004223 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 return FAIL;
4225
4226 /*
4227 * Repeat until there is no following "&&".
4228 */
4229 first = TRUE;
4230 result = TRUE;
4231 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4232 {
4233 if (evaluate && first)
4234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004235 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 if (error)
4239 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 first = FALSE;
4241 }
4242
4243 /*
4244 * Get the second variable.
4245 */
4246 *arg = skipwhite(*arg + 2);
4247 if (eval4(arg, &var2, evaluate && result) == FAIL)
4248 return FAIL;
4249
4250 /*
4251 * Compute the result.
4252 */
4253 if (evaluate && result)
4254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (error)
4259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261 if (evaluate)
4262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 rettv->v_type = VAR_NUMBER;
4264 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 }
4266 }
4267
4268 return OK;
4269}
4270
4271/*
4272 * Handle third level expression:
4273 * var1 == var2
4274 * var1 =~ var2
4275 * var1 != var2
4276 * var1 !~ var2
4277 * var1 > var2
4278 * var1 >= var2
4279 * var1 < var2
4280 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004281 * var1 is var2
4282 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 *
4284 * "arg" must point to the first non-white of the expression.
4285 * "arg" is advanced to the next non-white after the recognized expression.
4286 *
4287 * Return OK or FAIL.
4288 */
4289 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 int evaluate;
4294{
Bram Moolenaar33570922005-01-25 22:26:29 +00004295 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 char_u *p;
4297 int i;
4298 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004299 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 int len = 2;
4301 long n1, n2;
4302 char_u *s1, *s2;
4303 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4304 regmatch_T regmatch;
4305 int ic;
4306 char_u *save_cpo;
4307
4308 /*
4309 * Get the first variable.
4310 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 return FAIL;
4313
4314 p = *arg;
4315 switch (p[0])
4316 {
4317 case '=': if (p[1] == '=')
4318 type = TYPE_EQUAL;
4319 else if (p[1] == '~')
4320 type = TYPE_MATCH;
4321 break;
4322 case '!': if (p[1] == '=')
4323 type = TYPE_NEQUAL;
4324 else if (p[1] == '~')
4325 type = TYPE_NOMATCH;
4326 break;
4327 case '>': if (p[1] != '=')
4328 {
4329 type = TYPE_GREATER;
4330 len = 1;
4331 }
4332 else
4333 type = TYPE_GEQUAL;
4334 break;
4335 case '<': if (p[1] != '=')
4336 {
4337 type = TYPE_SMALLER;
4338 len = 1;
4339 }
4340 else
4341 type = TYPE_SEQUAL;
4342 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004343 case 'i': if (p[1] == 's')
4344 {
4345 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4346 len = 5;
4347 if (!vim_isIDc(p[len]))
4348 {
4349 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4350 type_is = TRUE;
4351 }
4352 }
4353 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 }
4355
4356 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004357 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 */
4359 if (type != TYPE_UNKNOWN)
4360 {
4361 /* extra question mark appended: ignore case */
4362 if (p[len] == '?')
4363 {
4364 ic = TRUE;
4365 ++len;
4366 }
4367 /* extra '#' appended: match case */
4368 else if (p[len] == '#')
4369 {
4370 ic = FALSE;
4371 ++len;
4372 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004373 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 else
4375 ic = p_ic;
4376
4377 /*
4378 * Get the second variable.
4379 */
4380 *arg = skipwhite(p + len);
4381 if (eval5(arg, &var2, evaluate) == FAIL)
4382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004383 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 return FAIL;
4385 }
4386
4387 if (evaluate)
4388 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004389 if (type_is && rettv->v_type != var2.v_type)
4390 {
4391 /* For "is" a different type always means FALSE, for "notis"
4392 * it means TRUE. */
4393 n1 = (type == TYPE_NEQUAL);
4394 }
4395 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4396 {
4397 if (type_is)
4398 {
4399 n1 = (rettv->v_type == var2.v_type
4400 && rettv->vval.v_list == var2.vval.v_list);
4401 if (type == TYPE_NEQUAL)
4402 n1 = !n1;
4403 }
4404 else if (rettv->v_type != var2.v_type
4405 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4406 {
4407 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004408 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004409 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004410 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004411 clear_tv(rettv);
4412 clear_tv(&var2);
4413 return FAIL;
4414 }
4415 else
4416 {
4417 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004418 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4419 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004420 if (type == TYPE_NEQUAL)
4421 n1 = !n1;
4422 }
4423 }
4424
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004425 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4426 {
4427 if (type_is)
4428 {
4429 n1 = (rettv->v_type == var2.v_type
4430 && rettv->vval.v_dict == var2.vval.v_dict);
4431 if (type == TYPE_NEQUAL)
4432 n1 = !n1;
4433 }
4434 else if (rettv->v_type != var2.v_type
4435 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4436 {
4437 if (rettv->v_type != var2.v_type)
4438 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4439 else
4440 EMSG(_("E736: Invalid operation for Dictionary"));
4441 clear_tv(rettv);
4442 clear_tv(&var2);
4443 return FAIL;
4444 }
4445 else
4446 {
4447 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004448 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4449 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004450 if (type == TYPE_NEQUAL)
4451 n1 = !n1;
4452 }
4453 }
4454
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004455 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4456 {
4457 if (rettv->v_type != var2.v_type
4458 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4459 {
4460 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004461 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004462 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004463 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 clear_tv(rettv);
4465 clear_tv(&var2);
4466 return FAIL;
4467 }
4468 else
4469 {
4470 /* Compare two Funcrefs for being equal or unequal. */
4471 if (rettv->vval.v_string == NULL
4472 || var2.vval.v_string == NULL)
4473 n1 = FALSE;
4474 else
4475 n1 = STRCMP(rettv->vval.v_string,
4476 var2.vval.v_string) == 0;
4477 if (type == TYPE_NEQUAL)
4478 n1 = !n1;
4479 }
4480 }
4481
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004482#ifdef FEAT_FLOAT
4483 /*
4484 * If one of the two variables is a float, compare as a float.
4485 * When using "=~" or "!~", always compare as string.
4486 */
4487 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4488 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4489 {
4490 float_T f1, f2;
4491
4492 if (rettv->v_type == VAR_FLOAT)
4493 f1 = rettv->vval.v_float;
4494 else
4495 f1 = get_tv_number(rettv);
4496 if (var2.v_type == VAR_FLOAT)
4497 f2 = var2.vval.v_float;
4498 else
4499 f2 = get_tv_number(&var2);
4500 n1 = FALSE;
4501 switch (type)
4502 {
4503 case TYPE_EQUAL: n1 = (f1 == f2); break;
4504 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4505 case TYPE_GREATER: n1 = (f1 > f2); break;
4506 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4507 case TYPE_SMALLER: n1 = (f1 < f2); break;
4508 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4509 case TYPE_UNKNOWN:
4510 case TYPE_MATCH:
4511 case TYPE_NOMATCH: break; /* avoid gcc warning */
4512 }
4513 }
4514#endif
4515
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 /*
4517 * If one of the two variables is a number, compare as a number.
4518 * When using "=~" or "!~", always compare as string.
4519 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004520 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004523 n1 = get_tv_number(rettv);
4524 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 switch (type)
4526 {
4527 case TYPE_EQUAL: n1 = (n1 == n2); break;
4528 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4529 case TYPE_GREATER: n1 = (n1 > n2); break;
4530 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4531 case TYPE_SMALLER: n1 = (n1 < n2); break;
4532 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4533 case TYPE_UNKNOWN:
4534 case TYPE_MATCH:
4535 case TYPE_NOMATCH: break; /* avoid gcc warning */
4536 }
4537 }
4538 else
4539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004540 s1 = get_tv_string_buf(rettv, buf1);
4541 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4543 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4544 else
4545 i = 0;
4546 n1 = FALSE;
4547 switch (type)
4548 {
4549 case TYPE_EQUAL: n1 = (i == 0); break;
4550 case TYPE_NEQUAL: n1 = (i != 0); break;
4551 case TYPE_GREATER: n1 = (i > 0); break;
4552 case TYPE_GEQUAL: n1 = (i >= 0); break;
4553 case TYPE_SMALLER: n1 = (i < 0); break;
4554 case TYPE_SEQUAL: n1 = (i <= 0); break;
4555
4556 case TYPE_MATCH:
4557 case TYPE_NOMATCH:
4558 /* avoid 'l' flag in 'cpoptions' */
4559 save_cpo = p_cpo;
4560 p_cpo = (char_u *)"";
4561 regmatch.regprog = vim_regcomp(s2,
4562 RE_MAGIC + RE_STRING);
4563 regmatch.rm_ic = ic;
4564 if (regmatch.regprog != NULL)
4565 {
4566 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004567 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 if (type == TYPE_NOMATCH)
4569 n1 = !n1;
4570 }
4571 p_cpo = save_cpo;
4572 break;
4573
4574 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4575 }
4576 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577 clear_tv(rettv);
4578 clear_tv(&var2);
4579 rettv->v_type = VAR_NUMBER;
4580 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
4582 }
4583
4584 return OK;
4585}
4586
4587/*
4588 * Handle fourth level expression:
4589 * + number addition
4590 * - number subtraction
4591 * . string concatenation
4592 *
4593 * "arg" must point to the first non-white of the expression.
4594 * "arg" is advanced to the next non-white after the recognized expression.
4595 *
4596 * Return OK or FAIL.
4597 */
4598 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 int evaluate;
4603{
Bram Moolenaar33570922005-01-25 22:26:29 +00004604 typval_T var2;
4605 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 int op;
4607 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004608#ifdef FEAT_FLOAT
4609 float_T f1 = 0, f2 = 0;
4610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 char_u *s1, *s2;
4612 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4613 char_u *p;
4614
4615 /*
4616 * Get the first variable.
4617 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004618 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 return FAIL;
4620
4621 /*
4622 * Repeat computing, until no '+', '-' or '.' is following.
4623 */
4624 for (;;)
4625 {
4626 op = **arg;
4627 if (op != '+' && op != '-' && op != '.')
4628 break;
4629
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004630 if ((op != '+' || rettv->v_type != VAR_LIST)
4631#ifdef FEAT_FLOAT
4632 && (op == '.' || rettv->v_type != VAR_FLOAT)
4633#endif
4634 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004635 {
4636 /* For "list + ...", an illegal use of the first operand as
4637 * a number cannot be determined before evaluating the 2nd
4638 * operand: if this is also a list, all is ok.
4639 * For "something . ...", "something - ..." or "non-list + ...",
4640 * we know that the first operand needs to be a string or number
4641 * without evaluating the 2nd operand. So check before to avoid
4642 * side effects after an error. */
4643 if (evaluate && get_tv_string_chk(rettv) == NULL)
4644 {
4645 clear_tv(rettv);
4646 return FAIL;
4647 }
4648 }
4649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 /*
4651 * Get the second variable.
4652 */
4653 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004654 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004656 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 return FAIL;
4658 }
4659
4660 if (evaluate)
4661 {
4662 /*
4663 * Compute the result.
4664 */
4665 if (op == '.')
4666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004667 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4668 s2 = get_tv_string_buf_chk(&var2, buf2);
4669 if (s2 == NULL) /* type error ? */
4670 {
4671 clear_tv(rettv);
4672 clear_tv(&var2);
4673 return FAIL;
4674 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004675 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004676 clear_tv(rettv);
4677 rettv->v_type = VAR_STRING;
4678 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004680 else if (op == '+' && rettv->v_type == VAR_LIST
4681 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004682 {
4683 /* concatenate Lists */
4684 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4685 &var3) == FAIL)
4686 {
4687 clear_tv(rettv);
4688 clear_tv(&var2);
4689 return FAIL;
4690 }
4691 clear_tv(rettv);
4692 *rettv = var3;
4693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 else
4695 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004696 int error = FALSE;
4697
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004698#ifdef FEAT_FLOAT
4699 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004700 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004701 f1 = rettv->vval.v_float;
4702 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004703 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004704 else
4705#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004706 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004707 n1 = get_tv_number_chk(rettv, &error);
4708 if (error)
4709 {
4710 /* This can only happen for "list + non-list". For
4711 * "non-list + ..." or "something - ...", we returned
4712 * before evaluating the 2nd operand. */
4713 clear_tv(rettv);
4714 return FAIL;
4715 }
4716#ifdef FEAT_FLOAT
4717 if (var2.v_type == VAR_FLOAT)
4718 f1 = n1;
4719#endif
4720 }
4721#ifdef FEAT_FLOAT
4722 if (var2.v_type == VAR_FLOAT)
4723 {
4724 f2 = var2.vval.v_float;
4725 n2 = 0;
4726 }
4727 else
4728#endif
4729 {
4730 n2 = get_tv_number_chk(&var2, &error);
4731 if (error)
4732 {
4733 clear_tv(rettv);
4734 clear_tv(&var2);
4735 return FAIL;
4736 }
4737#ifdef FEAT_FLOAT
4738 if (rettv->v_type == VAR_FLOAT)
4739 f2 = n2;
4740#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004742 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004743
4744#ifdef FEAT_FLOAT
4745 /* If there is a float on either side the result is a float. */
4746 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4747 {
4748 if (op == '+')
4749 f1 = f1 + f2;
4750 else
4751 f1 = f1 - f2;
4752 rettv->v_type = VAR_FLOAT;
4753 rettv->vval.v_float = f1;
4754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004756#endif
4757 {
4758 if (op == '+')
4759 n1 = n1 + n2;
4760 else
4761 n1 = n1 - n2;
4762 rettv->v_type = VAR_NUMBER;
4763 rettv->vval.v_number = n1;
4764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768 }
4769 return OK;
4770}
4771
4772/*
4773 * Handle fifth level expression:
4774 * * number multiplication
4775 * / number division
4776 * % number modulo
4777 *
4778 * "arg" must point to the first non-white of the expression.
4779 * "arg" is advanced to the next non-white after the recognized expression.
4780 *
4781 * Return OK or FAIL.
4782 */
4783 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004784eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004788 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
Bram Moolenaar33570922005-01-25 22:26:29 +00004790 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 int op;
4792 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004793#ifdef FEAT_FLOAT
4794 int use_float = FALSE;
4795 float_T f1 = 0, f2;
4796#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
4799 /*
4800 * Get the first variable.
4801 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004802 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 return FAIL;
4804
4805 /*
4806 * Repeat computing, until no '*', '/' or '%' is following.
4807 */
4808 for (;;)
4809 {
4810 op = **arg;
4811 if (op != '*' && op != '/' && op != '%')
4812 break;
4813
4814 if (evaluate)
4815 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004816#ifdef FEAT_FLOAT
4817 if (rettv->v_type == VAR_FLOAT)
4818 {
4819 f1 = rettv->vval.v_float;
4820 use_float = TRUE;
4821 n1 = 0;
4822 }
4823 else
4824#endif
4825 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004826 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004827 if (error)
4828 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 }
4830 else
4831 n1 = 0;
4832
4833 /*
4834 * Get the second variable.
4835 */
4836 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004837 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 return FAIL;
4839
4840 if (evaluate)
4841 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004842#ifdef FEAT_FLOAT
4843 if (var2.v_type == VAR_FLOAT)
4844 {
4845 if (!use_float)
4846 {
4847 f1 = n1;
4848 use_float = TRUE;
4849 }
4850 f2 = var2.vval.v_float;
4851 n2 = 0;
4852 }
4853 else
4854#endif
4855 {
4856 n2 = get_tv_number_chk(&var2, &error);
4857 clear_tv(&var2);
4858 if (error)
4859 return FAIL;
4860#ifdef FEAT_FLOAT
4861 if (use_float)
4862 f2 = n2;
4863#endif
4864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
4866 /*
4867 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004868 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004870#ifdef FEAT_FLOAT
4871 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873 if (op == '*')
4874 f1 = f1 * f2;
4875 else if (op == '/')
4876 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004877# ifdef VMS
4878 /* VMS crashes on divide by zero, work around it */
4879 if (f2 == 0.0)
4880 {
4881 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004882 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004883 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004884 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004885 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004886 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004887 }
4888 else
4889 f1 = f1 / f2;
4890# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004891 /* We rely on the floating point library to handle divide
4892 * by zero to result in "inf" and not a crash. */
4893 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004894# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004898 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899 return FAIL;
4900 }
4901 rettv->v_type = VAR_FLOAT;
4902 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004907 if (op == '*')
4908 n1 = n1 * n2;
4909 else if (op == '/')
4910 {
4911 if (n2 == 0) /* give an error message? */
4912 {
4913 if (n1 == 0)
4914 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4915 else if (n1 < 0)
4916 n1 = -0x7fffffffL;
4917 else
4918 n1 = 0x7fffffffL;
4919 }
4920 else
4921 n1 = n1 / n2;
4922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004924 {
4925 if (n2 == 0) /* give an error message? */
4926 n1 = 0;
4927 else
4928 n1 = n1 % n2;
4929 }
4930 rettv->v_type = VAR_NUMBER;
4931 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 }
4935
4936 return OK;
4937}
4938
4939/*
4940 * Handle sixth level expression:
4941 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004942 * "string" string constant
4943 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 * &option-name option value
4945 * @r register contents
4946 * identifier variable value
4947 * function() function call
4948 * $VAR environment variable
4949 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004950 * [expr, expr] List
4951 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 *
4953 * Also handle:
4954 * ! in front logical NOT
4955 * - in front unary minus
4956 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004957 * trailing [] subscript in String or List
4958 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 *
4960 * "arg" must point to the first non-white of the expression.
4961 * "arg" is advanced to the next non-white after the recognized expression.
4962 *
4963 * Return OK or FAIL.
4964 */
4965 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004966eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004970 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 long n;
4973 int len;
4974 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 char_u *start_leader, *end_leader;
4976 int ret = OK;
4977 char_u *alias;
4978
4979 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004980 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004981 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004983 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984
4985 /*
4986 * Skip '!' and '-' characters. They are handled later.
4987 */
4988 start_leader = *arg;
4989 while (**arg == '!' || **arg == '-' || **arg == '+')
4990 *arg = skipwhite(*arg + 1);
4991 end_leader = *arg;
4992
4993 switch (**arg)
4994 {
4995 /*
4996 * Number constant.
4997 */
4998 case '0':
4999 case '1':
5000 case '2':
5001 case '3':
5002 case '4':
5003 case '5':
5004 case '6':
5005 case '7':
5006 case '8':
5007 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008 {
5009#ifdef FEAT_FLOAT
5010 char_u *p = skipdigits(*arg + 1);
5011 int get_float = FALSE;
5012
5013 /* We accept a float when the format matches
5014 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005015 * strict to avoid backwards compatibility problems.
5016 * Don't look for a float after the "." operator, so that
5017 * ":let vers = 1.2.3" doesn't fail. */
5018 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005020 get_float = TRUE;
5021 p = skipdigits(p + 2);
5022 if (*p == 'e' || *p == 'E')
5023 {
5024 ++p;
5025 if (*p == '-' || *p == '+')
5026 ++p;
5027 if (!vim_isdigit(*p))
5028 get_float = FALSE;
5029 else
5030 p = skipdigits(p + 1);
5031 }
5032 if (ASCII_ISALPHA(*p) || *p == '.')
5033 get_float = FALSE;
5034 }
5035 if (get_float)
5036 {
5037 float_T f;
5038
5039 *arg += string2float(*arg, &f);
5040 if (evaluate)
5041 {
5042 rettv->v_type = VAR_FLOAT;
5043 rettv->vval.v_float = f;
5044 }
5045 }
5046 else
5047#endif
5048 {
5049 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5050 *arg += len;
5051 if (evaluate)
5052 {
5053 rettv->v_type = VAR_NUMBER;
5054 rettv->vval.v_number = n;
5055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
5060 /*
5061 * String constant: "string".
5062 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005063 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 break;
5065
5066 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005070 break;
5071
5072 /*
5073 * List: [expr, expr]
5074 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005075 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 break;
5077
5078 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 * Dictionary: {key: val, key: val}
5080 */
5081 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5082 break;
5083
5084 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005085 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005087 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 break;
5089
5090 /*
5091 * Environment variable: $VAR.
5092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 break;
5095
5096 /*
5097 * Register contents: @r.
5098 */
5099 case '@': ++*arg;
5100 if (evaluate)
5101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005102 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005103 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105 if (**arg != NUL)
5106 ++*arg;
5107 break;
5108
5109 /*
5110 * nested expression: (expression).
5111 */
5112 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005113 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (**arg == ')')
5115 ++*arg;
5116 else if (ret == OK)
5117 {
5118 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005119 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 ret = FAIL;
5121 }
5122 break;
5123
Bram Moolenaar8c711452005-01-14 21:53:12 +00005124 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 break;
5126 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127
5128 if (ret == NOTDONE)
5129 {
5130 /*
5131 * Must be a variable or function name.
5132 * Can also be a curly-braces kind of name: {expr}.
5133 */
5134 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005135 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 if (alias != NULL)
5137 s = alias;
5138
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005139 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140 ret = FAIL;
5141 else
5142 {
5143 if (**arg == '(') /* recursive! */
5144 {
5145 /* If "s" is the name of a variable of type VAR_FUNC
5146 * use its contents. */
5147 s = deref_func_name(s, &len);
5148
5149 /* Invoke the function. */
5150 ret = get_func_tv(s, len, rettv, arg,
5151 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005152 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005153
5154 /* If evaluate is FALSE rettv->v_type was not set in
5155 * get_func_tv, but it's needed in handle_subscript() to parse
5156 * what follows. So set it here. */
5157 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5158 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005159 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005160 rettv->v_type = VAR_FUNC;
5161 }
5162
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 /* Stop the expression evaluation when immediately
5164 * aborting on error, or when an interrupt occurred or
5165 * an exception was thrown but not caught. */
5166 if (aborting())
5167 {
5168 if (ret == OK)
5169 clear_tv(rettv);
5170 ret = FAIL;
5171 }
5172 }
5173 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005174 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005175 else
5176 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005178 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 }
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 *arg = skipwhite(*arg);
5182
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005183 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5184 * expr(expr). */
5185 if (ret == OK)
5186 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187
5188 /*
5189 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5190 */
5191 if (ret == OK && evaluate && end_leader > start_leader)
5192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005193 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005194 int val = 0;
5195#ifdef FEAT_FLOAT
5196 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005197
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005198 if (rettv->v_type == VAR_FLOAT)
5199 f = rettv->vval.v_float;
5200 else
5201#endif
5202 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005203 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005205 clear_tv(rettv);
5206 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 else
5209 {
5210 while (end_leader > start_leader)
5211 {
5212 --end_leader;
5213 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005214 {
5215#ifdef FEAT_FLOAT
5216 if (rettv->v_type == VAR_FLOAT)
5217 f = !f;
5218 else
5219#endif
5220 val = !val;
5221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005223 {
5224#ifdef FEAT_FLOAT
5225 if (rettv->v_type == VAR_FLOAT)
5226 f = -f;
5227 else
5228#endif
5229 val = -val;
5230 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005231 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005232#ifdef FEAT_FLOAT
5233 if (rettv->v_type == VAR_FLOAT)
5234 {
5235 clear_tv(rettv);
5236 rettv->vval.v_float = f;
5237 }
5238 else
5239#endif
5240 {
5241 clear_tv(rettv);
5242 rettv->v_type = VAR_NUMBER;
5243 rettv->vval.v_number = val;
5244 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 }
5247
5248 return ret;
5249}
5250
5251/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005252 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5253 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5255 */
5256 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005257eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005259 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005261 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262{
5263 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005264 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 long len = -1;
5267 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005271 if (rettv->v_type == VAR_FUNC
5272#ifdef FEAT_FLOAT
5273 || rettv->v_type == VAR_FLOAT
5274#endif
5275 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005277 if (verbose)
5278 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 return FAIL;
5280 }
5281
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 /*
5285 * dict.name
5286 */
5287 key = *arg + 1;
5288 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5289 ;
5290 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 }
5294 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 /*
5297 * something[idx]
5298 *
5299 * Get the (first) variable from inside the [].
5300 */
5301 *arg = skipwhite(*arg + 1);
5302 if (**arg == ':')
5303 empty1 = TRUE;
5304 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5305 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5307 {
5308 /* not a number or string */
5309 clear_tv(&var1);
5310 return FAIL;
5311 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312
5313 /*
5314 * Get the second variable from inside the [:].
5315 */
5316 if (**arg == ':')
5317 {
5318 range = TRUE;
5319 *arg = skipwhite(*arg + 1);
5320 if (**arg == ']')
5321 empty2 = TRUE;
5322 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005324 if (!empty1)
5325 clear_tv(&var1);
5326 return FAIL;
5327 }
5328 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5329 {
5330 /* not a number or string */
5331 if (!empty1)
5332 clear_tv(&var1);
5333 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005334 return FAIL;
5335 }
5336 }
5337
5338 /* Check for the ']'. */
5339 if (**arg != ']')
5340 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005341 if (verbose)
5342 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 clear_tv(&var1);
5344 if (range)
5345 clear_tv(&var2);
5346 return FAIL;
5347 }
5348 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 }
5350
5351 if (evaluate)
5352 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 n1 = 0;
5354 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005356 n1 = get_tv_number(&var1);
5357 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 }
5359 if (range)
5360 {
5361 if (empty2)
5362 n2 = -1;
5363 else
5364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005365 n2 = get_tv_number(&var2);
5366 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 }
5368 }
5369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005370 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 {
5372 case VAR_NUMBER:
5373 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005374 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 len = (long)STRLEN(s);
5376 if (range)
5377 {
5378 /* The resulting variable is a substring. If the indexes
5379 * are out of range the result is empty. */
5380 if (n1 < 0)
5381 {
5382 n1 = len + n1;
5383 if (n1 < 0)
5384 n1 = 0;
5385 }
5386 if (n2 < 0)
5387 n2 = len + n2;
5388 else if (n2 >= len)
5389 n2 = len;
5390 if (n1 >= len || n2 < 0 || n1 > n2)
5391 s = NULL;
5392 else
5393 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5394 }
5395 else
5396 {
5397 /* The resulting variable is a string of a single
5398 * character. If the index is too big or negative the
5399 * result is empty. */
5400 if (n1 >= len || n1 < 0)
5401 s = NULL;
5402 else
5403 s = vim_strnsave(s + n1, 1);
5404 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005405 clear_tv(rettv);
5406 rettv->v_type = VAR_STRING;
5407 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 break;
5409
5410 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005411 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412 if (n1 < 0)
5413 n1 = len + n1;
5414 if (!empty1 && (n1 < 0 || n1 >= len))
5415 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005416 /* For a range we allow invalid values and return an empty
5417 * list. A list index out of range is an error. */
5418 if (!range)
5419 {
5420 if (verbose)
5421 EMSGN(_(e_listidx), n1);
5422 return FAIL;
5423 }
5424 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425 }
5426 if (range)
5427 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005428 list_T *l;
5429 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430
5431 if (n2 < 0)
5432 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005433 else if (n2 >= len)
5434 n2 = len - 1;
5435 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005436 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 l = list_alloc();
5438 if (l == NULL)
5439 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 n1 <= n2; ++n1)
5442 {
5443 if (list_append_tv(l, &item->li_tv) == FAIL)
5444 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005445 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 return FAIL;
5447 }
5448 item = item->li_next;
5449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005450 clear_tv(rettv);
5451 rettv->v_type = VAR_LIST;
5452 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005453 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455 else
5456 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005457 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458 clear_tv(rettv);
5459 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005462
5463 case VAR_DICT:
5464 if (range)
5465 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005466 if (verbose)
5467 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005468 if (len == -1)
5469 clear_tv(&var1);
5470 return FAIL;
5471 }
5472 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005474
5475 if (len == -1)
5476 {
5477 key = get_tv_string(&var1);
5478 if (*key == NUL)
5479 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005480 if (verbose)
5481 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005482 clear_tv(&var1);
5483 return FAIL;
5484 }
5485 }
5486
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005487 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005489 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005490 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491 if (len == -1)
5492 clear_tv(&var1);
5493 if (item == NULL)
5494 return FAIL;
5495
5496 copy_tv(&item->di_tv, &var1);
5497 clear_tv(rettv);
5498 *rettv = var1;
5499 }
5500 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 }
5502 }
5503
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 return OK;
5505}
5506
5507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 * Get an option value.
5509 * "arg" points to the '&' or '+' before the option name.
5510 * "arg" is advanced to character after the option name.
5511 * Return OK or FAIL.
5512 */
5513 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005514get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005516 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 int evaluate;
5518{
5519 char_u *option_end;
5520 long numval;
5521 char_u *stringval;
5522 int opt_type;
5523 int c;
5524 int working = (**arg == '+'); /* has("+option") */
5525 int ret = OK;
5526 int opt_flags;
5527
5528 /*
5529 * Isolate the option name and find its value.
5530 */
5531 option_end = find_option_end(arg, &opt_flags);
5532 if (option_end == NULL)
5533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005534 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 EMSG2(_("E112: Option name missing: %s"), *arg);
5536 return FAIL;
5537 }
5538
5539 if (!evaluate)
5540 {
5541 *arg = option_end;
5542 return OK;
5543 }
5544
5545 c = *option_end;
5546 *option_end = NUL;
5547 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
5550 if (opt_type == -3) /* invalid name */
5551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 EMSG2(_("E113: Unknown option: %s"), *arg);
5554 ret = FAIL;
5555 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
5558 if (opt_type == -2) /* hidden string option */
5559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 rettv->v_type = VAR_STRING;
5561 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 }
5563 else if (opt_type == -1) /* hidden number option */
5564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 rettv->v_type = VAR_NUMBER;
5566 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 }
5568 else if (opt_type == 1) /* number option */
5569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 rettv->v_type = VAR_NUMBER;
5571 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
5573 else /* string option */
5574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 rettv->v_type = VAR_STRING;
5576 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
5578 }
5579 else if (working && (opt_type == -2 || opt_type == -1))
5580 ret = FAIL;
5581
5582 *option_end = c; /* put back for error messages */
5583 *arg = option_end;
5584
5585 return ret;
5586}
5587
5588/*
5589 * Allocate a variable for a string constant.
5590 * Return OK or FAIL.
5591 */
5592 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 int evaluate;
5597{
5598 char_u *p;
5599 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 int extra = 0;
5601
5602 /*
5603 * Find the end of the string, skipping backslashed characters.
5604 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005605 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
5607 if (*p == '\\' && p[1] != NUL)
5608 {
5609 ++p;
5610 /* A "\<x>" form occupies at least 4 characters, and produces up
5611 * to 6 characters: reserve space for 2 extra */
5612 if (*p == '<')
5613 extra += 2;
5614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 }
5616
5617 if (*p != '"')
5618 {
5619 EMSG2(_("E114: Missing quote: %s"), *arg);
5620 return FAIL;
5621 }
5622
5623 /* If only parsing, set *arg and return here */
5624 if (!evaluate)
5625 {
5626 *arg = p + 1;
5627 return OK;
5628 }
5629
5630 /*
5631 * Copy the string into allocated memory, handling backslashed
5632 * characters.
5633 */
5634 name = alloc((unsigned)(p - *arg + extra));
5635 if (name == NULL)
5636 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637 rettv->v_type = VAR_STRING;
5638 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 {
5642 if (*p == '\\')
5643 {
5644 switch (*++p)
5645 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 case 'b': *name++ = BS; ++p; break;
5647 case 'e': *name++ = ESC; ++p; break;
5648 case 'f': *name++ = FF; ++p; break;
5649 case 'n': *name++ = NL; ++p; break;
5650 case 'r': *name++ = CAR; ++p; break;
5651 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
5653 case 'X': /* hex: "\x1", "\x12" */
5654 case 'x':
5655 case 'u': /* Unicode: "\u0023" */
5656 case 'U':
5657 if (vim_isxdigit(p[1]))
5658 {
5659 int n, nr;
5660 int c = toupper(*p);
5661
5662 if (c == 'X')
5663 n = 2;
5664 else
5665 n = 4;
5666 nr = 0;
5667 while (--n >= 0 && vim_isxdigit(p[1]))
5668 {
5669 ++p;
5670 nr = (nr << 4) + hex2nr(*p);
5671 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673#ifdef FEAT_MBYTE
5674 /* For "\u" store the number according to
5675 * 'encoding'. */
5676 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 else
5679#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 break;
5683
5684 /* octal: "\1", "\12", "\123" */
5685 case '0':
5686 case '1':
5687 case '2':
5688 case '3':
5689 case '4':
5690 case '5':
5691 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005692 case '7': *name = *p++ - '0';
5693 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005695 *name = (*name << 3) + *p++ - '0';
5696 if (*p >= '0' && *p <= '7')
5697 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 break;
5701
5702 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 if (extra != 0)
5705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 break;
5708 }
5709 /* FALLTHROUGH */
5710
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 break;
5713 }
5714 }
5715 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 *arg = p + 1;
5721
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 return OK;
5723}
5724
5725/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 * Return OK or FAIL.
5728 */
5729 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005730get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 int evaluate;
5734{
5735 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005736 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005737 int reduce = 0;
5738
5739 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005741 */
5742 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 break;
5748 ++reduce;
5749 ++p;
5750 }
5751 }
5752
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 return FAIL;
5757 }
5758
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 if (!evaluate)
5761 {
5762 *arg = p + 1;
5763 return OK;
5764 }
5765
5766 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005768 */
5769 str = alloc((unsigned)((p - *arg) - reduce));
5770 if (str == NULL)
5771 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 rettv->v_type = VAR_STRING;
5773 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005778 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780 break;
5781 ++p;
5782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 *arg = p + 1;
5787
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788 return OK;
5789}
5790
5791/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792 * Allocate a variable for a List and fill it from "*arg".
5793 * Return OK or FAIL.
5794 */
5795 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005796get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005797 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005798 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 int evaluate;
5800{
Bram Moolenaar33570922005-01-25 22:26:29 +00005801 list_T *l = NULL;
5802 typval_T tv;
5803 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804
5805 if (evaluate)
5806 {
5807 l = list_alloc();
5808 if (l == NULL)
5809 return FAIL;
5810 }
5811
5812 *arg = skipwhite(*arg + 1);
5813 while (**arg != ']' && **arg != NUL)
5814 {
5815 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5816 goto failret;
5817 if (evaluate)
5818 {
5819 item = listitem_alloc();
5820 if (item != NULL)
5821 {
5822 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005823 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824 list_append(l, item);
5825 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005826 else
5827 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 }
5829
5830 if (**arg == ']')
5831 break;
5832 if (**arg != ',')
5833 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 goto failret;
5836 }
5837 *arg = skipwhite(*arg + 1);
5838 }
5839
5840 if (**arg != ']')
5841 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843failret:
5844 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005845 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005846 return FAIL;
5847 }
5848
5849 *arg = skipwhite(*arg + 1);
5850 if (evaluate)
5851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005852 rettv->v_type = VAR_LIST;
5853 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 ++l->lv_refcount;
5855 }
5856
5857 return OK;
5858}
5859
5860/*
5861 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005862 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005864 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865list_alloc()
5866{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005867 list_T *l;
5868
5869 l = (list_T *)alloc_clear(sizeof(list_T));
5870 if (l != NULL)
5871 {
5872 /* Prepend the list to the list of lists for garbage collection. */
5873 if (first_list != NULL)
5874 first_list->lv_used_prev = l;
5875 l->lv_used_prev = NULL;
5876 l->lv_used_next = first_list;
5877 first_list = l;
5878 }
5879 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880}
5881
5882/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005883 * Allocate an empty list for a return value.
5884 * Returns OK or FAIL.
5885 */
5886 static int
5887rettv_list_alloc(rettv)
5888 typval_T *rettv;
5889{
5890 list_T *l = list_alloc();
5891
5892 if (l == NULL)
5893 return FAIL;
5894
5895 rettv->vval.v_list = l;
5896 rettv->v_type = VAR_LIST;
5897 ++l->lv_refcount;
5898 return OK;
5899}
5900
5901/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902 * Unreference a list: decrement the reference count and free it when it
5903 * becomes zero.
5904 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005905 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005907 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005909 if (l != NULL && --l->lv_refcount <= 0)
5910 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911}
5912
5913/*
5914 * Free a list, including all items it points to.
5915 * Ignores the reference count.
5916 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005917 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005918list_free(l, recurse)
5919 list_T *l;
5920 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921{
Bram Moolenaar33570922005-01-25 22:26:29 +00005922 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005924 /* Remove the list from the list of lists for garbage collection. */
5925 if (l->lv_used_prev == NULL)
5926 first_list = l->lv_used_next;
5927 else
5928 l->lv_used_prev->lv_used_next = l->lv_used_next;
5929 if (l->lv_used_next != NULL)
5930 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5931
Bram Moolenaard9fba312005-06-26 22:34:35 +00005932 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005934 /* Remove the item before deleting it. */
5935 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005936 if (recurse || (item->li_tv.v_type != VAR_LIST
5937 && item->li_tv.v_type != VAR_DICT))
5938 clear_tv(&item->li_tv);
5939 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 }
5941 vim_free(l);
5942}
5943
5944/*
5945 * Allocate a list item.
5946 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005947 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948listitem_alloc()
5949{
Bram Moolenaar33570922005-01-25 22:26:29 +00005950 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951}
5952
5953/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005954 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005956 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005958 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005960 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961 vim_free(item);
5962}
5963
5964/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005965 * Remove a list item from a List and free it. Also clears the value.
5966 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005967 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005968listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 list_T *l;
5970 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005971{
5972 list_remove(l, item, item);
5973 listitem_free(item);
5974}
5975
5976/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977 * Get the number of items in a list.
5978 */
5979 static long
5980list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005981 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 if (l == NULL)
5984 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005985 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986}
5987
5988/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005989 * Return TRUE when two lists have exactly the same values.
5990 */
5991 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005992list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005993 list_T *l1;
5994 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005995 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005996 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997{
Bram Moolenaar33570922005-01-25 22:26:29 +00005998 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006000 if (l1 == NULL || l2 == NULL)
6001 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006002 if (l1 == l2)
6003 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006004 if (list_len(l1) != list_len(l2))
6005 return FALSE;
6006
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007 for (item1 = l1->lv_first, item2 = l2->lv_first;
6008 item1 != NULL && item2 != NULL;
6009 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006010 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011 return FALSE;
6012 return item1 == NULL && item2 == NULL;
6013}
6014
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006015#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6016 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006017/*
6018 * Return the dictitem that an entry in a hashtable points to.
6019 */
6020 dictitem_T *
6021dict_lookup(hi)
6022 hashitem_T *hi;
6023{
6024 return HI2DI(hi);
6025}
6026#endif
6027
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006028/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006029 * Return TRUE when two dictionaries have exactly the same key/values.
6030 */
6031 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006032dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 dict_T *d1;
6034 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006035 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006036 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006037{
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 hashitem_T *hi;
6039 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006040 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006041
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006042 if (d1 == NULL || d2 == NULL)
6043 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006044 if (d1 == d2)
6045 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006046 if (dict_len(d1) != dict_len(d2))
6047 return FALSE;
6048
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006049 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006050 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006051 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006052 if (!HASHITEM_EMPTY(hi))
6053 {
6054 item2 = dict_find(d2, hi->hi_key, -1);
6055 if (item2 == NULL)
6056 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006057 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006058 return FALSE;
6059 --todo;
6060 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006061 }
6062 return TRUE;
6063}
6064
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006065static int tv_equal_recurse_limit;
6066
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006067/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006068 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006069 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006070 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006071 */
6072 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006073tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006074 typval_T *tv1;
6075 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006076 int ic; /* ignore case */
6077 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006078{
6079 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006080 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006081 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006082 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006083
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006084 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006085 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006087 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006088 * recursiveness to a limit. We guess they are equal then.
6089 * A fixed limit has the problem of still taking an awful long time.
6090 * Reduce the limit every time running into it. That should work fine for
6091 * deeply linked structures that are not recursively linked and catch
6092 * recursiveness quickly. */
6093 if (!recursive)
6094 tv_equal_recurse_limit = 1000;
6095 if (recursive_cnt >= tv_equal_recurse_limit)
6096 {
6097 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006098 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006099 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006100
6101 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006103 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006104 ++recursive_cnt;
6105 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6106 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006107 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006108
6109 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 ++recursive_cnt;
6111 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6112 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006113 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006114
6115 case VAR_FUNC:
6116 return (tv1->vval.v_string != NULL
6117 && tv2->vval.v_string != NULL
6118 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6119
6120 case VAR_NUMBER:
6121 return tv1->vval.v_number == tv2->vval.v_number;
6122
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006123#ifdef FEAT_FLOAT
6124 case VAR_FLOAT:
6125 return tv1->vval.v_float == tv2->vval.v_float;
6126#endif
6127
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006128 case VAR_STRING:
6129 s1 = get_tv_string_buf(tv1, buf1);
6130 s2 = get_tv_string_buf(tv2, buf2);
6131 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006132 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133
6134 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135 return TRUE;
6136}
6137
6138/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139 * Locate item with index "n" in list "l" and return it.
6140 * A negative index is counted from the end; -1 is the last item.
6141 * Returns NULL when "n" is out of range.
6142 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006143 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006144list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146 long n;
6147{
Bram Moolenaar33570922005-01-25 22:26:29 +00006148 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149 long idx;
6150
6151 if (l == NULL)
6152 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006153
6154 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006156 n = l->lv_len + n;
6157
6158 /* Check for index out of range. */
6159 if (n < 0 || n >= l->lv_len)
6160 return NULL;
6161
6162 /* When there is a cached index may start search from there. */
6163 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006165 if (n < l->lv_idx / 2)
6166 {
6167 /* closest to the start of the list */
6168 item = l->lv_first;
6169 idx = 0;
6170 }
6171 else if (n > (l->lv_idx + l->lv_len) / 2)
6172 {
6173 /* closest to the end of the list */
6174 item = l->lv_last;
6175 idx = l->lv_len - 1;
6176 }
6177 else
6178 {
6179 /* closest to the cached index */
6180 item = l->lv_idx_item;
6181 idx = l->lv_idx;
6182 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183 }
6184 else
6185 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006186 if (n < l->lv_len / 2)
6187 {
6188 /* closest to the start of the list */
6189 item = l->lv_first;
6190 idx = 0;
6191 }
6192 else
6193 {
6194 /* closest to the end of the list */
6195 item = l->lv_last;
6196 idx = l->lv_len - 1;
6197 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006198 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006199
6200 while (n > idx)
6201 {
6202 /* search forward */
6203 item = item->li_next;
6204 ++idx;
6205 }
6206 while (n < idx)
6207 {
6208 /* search backward */
6209 item = item->li_prev;
6210 --idx;
6211 }
6212
6213 /* cache the used index */
6214 l->lv_idx = idx;
6215 l->lv_idx_item = item;
6216
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006217 return item;
6218}
6219
6220/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006221 * Get list item "l[idx]" as a number.
6222 */
6223 static long
6224list_find_nr(l, idx, errorp)
6225 list_T *l;
6226 long idx;
6227 int *errorp; /* set to TRUE when something wrong */
6228{
6229 listitem_T *li;
6230
6231 li = list_find(l, idx);
6232 if (li == NULL)
6233 {
6234 if (errorp != NULL)
6235 *errorp = TRUE;
6236 return -1L;
6237 }
6238 return get_tv_number_chk(&li->li_tv, errorp);
6239}
6240
6241/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006242 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6243 */
6244 char_u *
6245list_find_str(l, idx)
6246 list_T *l;
6247 long idx;
6248{
6249 listitem_T *li;
6250
6251 li = list_find(l, idx - 1);
6252 if (li == NULL)
6253 {
6254 EMSGN(_(e_listidx), idx);
6255 return NULL;
6256 }
6257 return get_tv_string(&li->li_tv);
6258}
6259
6260/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006261 * Locate "item" list "l" and return its index.
6262 * Returns -1 when "item" is not in the list.
6263 */
6264 static long
6265list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006266 list_T *l;
6267 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006268{
6269 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006271
6272 if (l == NULL)
6273 return -1;
6274 idx = 0;
6275 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6276 ++idx;
6277 if (li == NULL)
6278 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006279 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006280}
6281
6282/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283 * Append item "item" to the end of list "l".
6284 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006285 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 list_T *l;
6288 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289{
6290 if (l->lv_last == NULL)
6291 {
6292 /* empty list */
6293 l->lv_first = item;
6294 l->lv_last = item;
6295 item->li_prev = NULL;
6296 }
6297 else
6298 {
6299 l->lv_last->li_next = item;
6300 item->li_prev = l->lv_last;
6301 l->lv_last = item;
6302 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006303 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006304 item->li_next = NULL;
6305}
6306
6307/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006308 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006309 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006311 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 list_T *l;
6314 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006316 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317
Bram Moolenaar05159a02005-02-26 23:04:13 +00006318 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006320 copy_tv(tv, &li->li_tv);
6321 list_append(l, li);
6322 return OK;
6323}
6324
6325/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006326 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006327 * Return FAIL when out of memory.
6328 */
6329 int
6330list_append_dict(list, dict)
6331 list_T *list;
6332 dict_T *dict;
6333{
6334 listitem_T *li = listitem_alloc();
6335
6336 if (li == NULL)
6337 return FAIL;
6338 li->li_tv.v_type = VAR_DICT;
6339 li->li_tv.v_lock = 0;
6340 li->li_tv.vval.v_dict = dict;
6341 list_append(list, li);
6342 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006343 return OK;
6344}
6345
6346/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006347 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006348 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006349 * Returns FAIL when out of memory.
6350 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006351 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006352list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006353 list_T *l;
6354 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006355 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006356{
6357 listitem_T *li = listitem_alloc();
6358
6359 if (li == NULL)
6360 return FAIL;
6361 list_append(l, li);
6362 li->li_tv.v_type = VAR_STRING;
6363 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006364 if (str == NULL)
6365 li->li_tv.vval.v_string = NULL;
6366 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006367 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006368 return FAIL;
6369 return OK;
6370}
6371
6372/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006373 * Append "n" to list "l".
6374 * Returns FAIL when out of memory.
6375 */
6376 static int
6377list_append_number(l, n)
6378 list_T *l;
6379 varnumber_T n;
6380{
6381 listitem_T *li;
6382
6383 li = listitem_alloc();
6384 if (li == NULL)
6385 return FAIL;
6386 li->li_tv.v_type = VAR_NUMBER;
6387 li->li_tv.v_lock = 0;
6388 li->li_tv.vval.v_number = n;
6389 list_append(l, li);
6390 return OK;
6391}
6392
6393/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006395 * If "item" is NULL append at the end.
6396 * Return FAIL when out of memory.
6397 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006398 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006399list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006400 list_T *l;
6401 typval_T *tv;
6402 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403{
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006405
6406 if (ni == NULL)
6407 return FAIL;
6408 copy_tv(tv, &ni->li_tv);
6409 if (item == NULL)
6410 /* Append new item at end of list. */
6411 list_append(l, ni);
6412 else
6413 {
6414 /* Insert new item before existing item. */
6415 ni->li_prev = item->li_prev;
6416 ni->li_next = item;
6417 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006418 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006419 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006420 ++l->lv_idx;
6421 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006423 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006425 l->lv_idx_item = NULL;
6426 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006428 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 }
6430 return OK;
6431}
6432
6433/*
6434 * Extend "l1" with "l2".
6435 * If "bef" is NULL append at the end, otherwise insert before this item.
6436 * Returns FAIL when out of memory.
6437 */
6438 static int
6439list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 list_T *l1;
6441 list_T *l2;
6442 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443{
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006445 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006446
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006447 /* We also quit the loop when we have inserted the original item count of
6448 * the list, avoid a hang when we extend a list with itself. */
6449 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006450 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6451 return FAIL;
6452 return OK;
6453}
6454
6455/*
6456 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6457 * Return FAIL when out of memory.
6458 */
6459 static int
6460list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 list_T *l1;
6462 list_T *l2;
6463 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464{
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006467 if (l1 == NULL || l2 == NULL)
6468 return FAIL;
6469
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006471 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006472 if (l == NULL)
6473 return FAIL;
6474 tv->v_type = VAR_LIST;
6475 tv->vval.v_list = l;
6476
6477 /* append all items from the second list */
6478 return list_extend(l, l2, NULL);
6479}
6480
6481/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006482 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006484 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006485 * Returns NULL when out of memory.
6486 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006488list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006491 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492{
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 list_T *copy;
6494 listitem_T *item;
6495 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006496
6497 if (orig == NULL)
6498 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499
6500 copy = list_alloc();
6501 if (copy != NULL)
6502 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006503 if (copyID != 0)
6504 {
6505 /* Do this before adding the items, because one of the items may
6506 * refer back to this list. */
6507 orig->lv_copyID = copyID;
6508 orig->lv_copylist = copy;
6509 }
6510 for (item = orig->lv_first; item != NULL && !got_int;
6511 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512 {
6513 ni = listitem_alloc();
6514 if (ni == NULL)
6515 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006516 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006517 {
6518 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6519 {
6520 vim_free(ni);
6521 break;
6522 }
6523 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006525 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 list_append(copy, ni);
6527 }
6528 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 if (item != NULL)
6530 {
6531 list_unref(copy);
6532 copy = NULL;
6533 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534 }
6535
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536 return copy;
6537}
6538
6539/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006541 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006542 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006543 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006544list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006545 list_T *l;
6546 listitem_T *item;
6547 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548{
Bram Moolenaar33570922005-01-25 22:26:29 +00006549 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551 /* notify watchers */
6552 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006553 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006554 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555 list_fix_watch(l, ip);
6556 if (ip == item2)
6557 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559
6560 if (item2->li_next == NULL)
6561 l->lv_last = item->li_prev;
6562 else
6563 item2->li_next->li_prev = item->li_prev;
6564 if (item->li_prev == NULL)
6565 l->lv_first = item2->li_next;
6566 else
6567 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006568 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569}
6570
6571/*
6572 * Return an allocated string with the string representation of a list.
6573 * May return NULL.
6574 */
6575 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006576list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006578 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579{
6580 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581
6582 if (tv->vval.v_list == NULL)
6583 return NULL;
6584 ga_init2(&ga, (int)sizeof(char), 80);
6585 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006586 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006587 {
6588 vim_free(ga.ga_data);
6589 return NULL;
6590 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591 ga_append(&ga, ']');
6592 ga_append(&ga, NUL);
6593 return (char_u *)ga.ga_data;
6594}
6595
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006596typedef struct join_S {
6597 char_u *s;
6598 char_u *tofree;
6599} join_T;
6600
6601 static int
6602list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6603 garray_T *gap; /* to store the result in */
6604 list_T *l;
6605 char_u *sep;
6606 int echo_style;
6607 int copyID;
6608 garray_T *join_gap; /* to keep each list item string */
6609{
6610 int i;
6611 join_T *p;
6612 int len;
6613 int sumlen = 0;
6614 int first = TRUE;
6615 char_u *tofree;
6616 char_u numbuf[NUMBUFLEN];
6617 listitem_T *item;
6618 char_u *s;
6619
6620 /* Stringify each item in the list. */
6621 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6622 {
6623 if (echo_style)
6624 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6625 else
6626 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6627 if (s == NULL)
6628 return FAIL;
6629
6630 len = (int)STRLEN(s);
6631 sumlen += len;
6632
6633 ga_grow(join_gap, 1);
6634 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6635 if (tofree != NULL || s != numbuf)
6636 {
6637 p->s = s;
6638 p->tofree = tofree;
6639 }
6640 else
6641 {
6642 p->s = vim_strnsave(s, len);
6643 p->tofree = p->s;
6644 }
6645
6646 line_breakcheck();
6647 }
6648
6649 /* Allocate result buffer with its total size, avoid re-allocation and
6650 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6651 if (join_gap->ga_len >= 2)
6652 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6653 if (ga_grow(gap, sumlen + 2) == FAIL)
6654 return FAIL;
6655
6656 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6657 {
6658 if (first)
6659 first = FALSE;
6660 else
6661 ga_concat(gap, sep);
6662 p = ((join_T *)join_gap->ga_data) + i;
6663
6664 if (p->s != NULL)
6665 ga_concat(gap, p->s);
6666 line_breakcheck();
6667 }
6668
6669 return OK;
6670}
6671
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006673 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006674 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006675 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006676 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006677 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006678list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006679 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006680 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006681 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006682 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006683 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006684{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006685 garray_T join_ga;
6686 int retval;
6687 join_T *p;
6688 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006689
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006690 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6691 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6692
6693 /* Dispose each item in join_ga. */
6694 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006695 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006696 p = (join_T *)join_ga.ga_data;
6697 for (i = 0; i < join_ga.ga_len; ++i)
6698 {
6699 vim_free(p->tofree);
6700 ++p;
6701 }
6702 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006704
6705 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006706}
6707
6708/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006709 * Garbage collection for lists and dictionaries.
6710 *
6711 * We use reference counts to be able to free most items right away when they
6712 * are no longer used. But for composite items it's possible that it becomes
6713 * unused while the reference count is > 0: When there is a recursive
6714 * reference. Example:
6715 * :let l = [1, 2, 3]
6716 * :let d = {9: l}
6717 * :let l[1] = d
6718 *
6719 * Since this is quite unusual we handle this with garbage collection: every
6720 * once in a while find out which lists and dicts are not referenced from any
6721 * variable.
6722 *
6723 * Here is a good reference text about garbage collection (refers to Python
6724 * but it applies to all reference-counting mechanisms):
6725 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006726 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006727
6728/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006729 * Do garbage collection for lists and dicts.
6730 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006731 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006732 int
6733garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006734{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006735 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 buf_T *buf;
6737 win_T *wp;
6738 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006739 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006740 int did_free;
6741 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006742#ifdef FEAT_WINDOWS
6743 tabpage_T *tp;
6744#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006745
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006746 /* Only do this once. */
6747 want_garbage_collect = FALSE;
6748 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006749 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006750
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006751 /* We advance by two because we add one for items referenced through
6752 * previous_funccal. */
6753 current_copyID += COPYID_INC;
6754 copyID = current_copyID;
6755
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006756 /*
6757 * 1. Go through all accessible variables and mark all lists and dicts
6758 * with copyID.
6759 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006760
6761 /* Don't free variables in the previous_funccal list unless they are only
6762 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006763 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006764 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6765 {
6766 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6767 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6768 }
6769
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 /* script-local variables */
6771 for (i = 1; i <= ga_scripts.ga_len; ++i)
6772 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6773
6774 /* buffer-local variables */
6775 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006776 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006777
6778 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006779 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006780 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006781#ifdef FEAT_AUTOCMD
6782 if (aucmd_win != NULL)
6783 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6784#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006785
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006786#ifdef FEAT_WINDOWS
6787 /* tabpage-local variables */
6788 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006789 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006790#endif
6791
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006792 /* global variables */
6793 set_ref_in_ht(&globvarht, copyID);
6794
6795 /* function-local variables */
6796 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6797 {
6798 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6799 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6800 }
6801
Bram Moolenaard812df62008-11-09 12:46:09 +00006802 /* v: vars */
6803 set_ref_in_ht(&vimvarht, copyID);
6804
Bram Moolenaar1dced572012-04-05 16:54:08 +02006805#ifdef FEAT_LUA
6806 set_ref_in_lua(copyID);
6807#endif
6808
Bram Moolenaardb913952012-06-29 12:54:53 +02006809#ifdef FEAT_PYTHON
6810 set_ref_in_python(copyID);
6811#endif
6812
6813#ifdef FEAT_PYTHON3
6814 set_ref_in_python3(copyID);
6815#endif
6816
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006817 /*
6818 * 2. Free lists and dictionaries that are not referenced.
6819 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006820 did_free = free_unref_items(copyID);
6821
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006822 /*
6823 * 3. Check if any funccal can be freed now.
6824 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006825 for (pfc = &previous_funccal; *pfc != NULL; )
6826 {
6827 if (can_free_funccal(*pfc, copyID))
6828 {
6829 fc = *pfc;
6830 *pfc = fc->caller;
6831 free_funccal(fc, TRUE);
6832 did_free = TRUE;
6833 did_free_funccal = TRUE;
6834 }
6835 else
6836 pfc = &(*pfc)->caller;
6837 }
6838 if (did_free_funccal)
6839 /* When a funccal was freed some more items might be garbage
6840 * collected, so run again. */
6841 (void)garbage_collect();
6842
6843 return did_free;
6844}
6845
6846/*
6847 * Free lists and dictionaries that are no longer referenced.
6848 */
6849 static int
6850free_unref_items(copyID)
6851 int copyID;
6852{
6853 dict_T *dd;
6854 list_T *ll;
6855 int did_free = FALSE;
6856
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006857 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859 */
6860 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006861 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006863 /* Free the Dictionary and ordinary items it contains, but don't
6864 * recurse into Lists and Dictionaries, they will be in the list
6865 * of dicts or list of lists. */
6866 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 did_free = TRUE;
6868
6869 /* restart, next dict may also have been freed */
6870 dd = first_dict;
6871 }
6872 else
6873 dd = dd->dv_used_next;
6874
6875 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006876 * Go through the list of lists and free items without the copyID.
6877 * But don't free a list that has a watcher (used in a for loop), these
6878 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879 */
6880 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6882 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006884 /* Free the List and ordinary items it contains, but don't recurse
6885 * into Lists and Dictionaries, they will be in the list of dicts
6886 * or list of lists. */
6887 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006888 did_free = TRUE;
6889
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006890 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 ll = first_list;
6892 }
6893 else
6894 ll = ll->lv_used_next;
6895
6896 return did_free;
6897}
6898
6899/*
6900 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6901 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006902 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903set_ref_in_ht(ht, copyID)
6904 hashtab_T *ht;
6905 int copyID;
6906{
6907 int todo;
6908 hashitem_T *hi;
6909
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006910 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 for (hi = ht->ht_array; todo > 0; ++hi)
6912 if (!HASHITEM_EMPTY(hi))
6913 {
6914 --todo;
6915 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6916 }
6917}
6918
6919/*
6920 * Mark all lists and dicts referenced through list "l" with "copyID".
6921 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006922 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923set_ref_in_list(l, copyID)
6924 list_T *l;
6925 int copyID;
6926{
6927 listitem_T *li;
6928
6929 for (li = l->lv_first; li != NULL; li = li->li_next)
6930 set_ref_in_item(&li->li_tv, copyID);
6931}
6932
6933/*
6934 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6935 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006936 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937set_ref_in_item(tv, copyID)
6938 typval_T *tv;
6939 int copyID;
6940{
6941 dict_T *dd;
6942 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006943
6944 switch (tv->v_type)
6945 {
6946 case VAR_DICT:
6947 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006948 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006949 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006950 /* Didn't see this dict yet. */
6951 dd->dv_copyID = copyID;
6952 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006953 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006954 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006955
6956 case VAR_LIST:
6957 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006958 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006959 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 /* Didn't see this list yet. */
6961 ll->lv_copyID = copyID;
6962 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006965 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006966 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006967}
6968
6969/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006970 * Allocate an empty header for a dictionary.
6971 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006972 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006973dict_alloc()
6974{
Bram Moolenaar33570922005-01-25 22:26:29 +00006975 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006976
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006978 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006979 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006980 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981 if (first_dict != NULL)
6982 first_dict->dv_used_prev = d;
6983 d->dv_used_next = first_dict;
6984 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006985 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986
Bram Moolenaar33570922005-01-25 22:26:29 +00006987 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006988 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006989 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006990 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006991 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006992 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006993 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006994}
6995
6996/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006997 * Allocate an empty dict for a return value.
6998 * Returns OK or FAIL.
6999 */
7000 static int
7001rettv_dict_alloc(rettv)
7002 typval_T *rettv;
7003{
7004 dict_T *d = dict_alloc();
7005
7006 if (d == NULL)
7007 return FAIL;
7008
7009 rettv->vval.v_dict = d;
7010 rettv->v_type = VAR_DICT;
7011 ++d->dv_refcount;
7012 return OK;
7013}
7014
7015
7016/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007017 * Unreference a Dictionary: decrement the reference count and free it when it
7018 * becomes zero.
7019 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007020 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007021dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007022 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007023{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007024 if (d != NULL && --d->dv_refcount <= 0)
7025 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007026}
7027
7028/*
7029 * Free a Dictionary, including all items it contains.
7030 * Ignores the reference count.
7031 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007032 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007033dict_free(d, recurse)
7034 dict_T *d;
7035 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007036{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007037 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007039 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007040
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 /* Remove the dict from the list of dicts for garbage collection. */
7042 if (d->dv_used_prev == NULL)
7043 first_dict = d->dv_used_next;
7044 else
7045 d->dv_used_prev->dv_used_next = d->dv_used_next;
7046 if (d->dv_used_next != NULL)
7047 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7048
7049 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007050 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007051 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007052 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007053 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007054 if (!HASHITEM_EMPTY(hi))
7055 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007056 /* Remove the item before deleting it, just in case there is
7057 * something recursive causing trouble. */
7058 di = HI2DI(hi);
7059 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007060 if (recurse || (di->di_tv.v_type != VAR_LIST
7061 && di->di_tv.v_type != VAR_DICT))
7062 clear_tv(&di->di_tv);
7063 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007064 --todo;
7065 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007066 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007067 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007068 vim_free(d);
7069}
7070
7071/*
7072 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007073 * The "key" is copied to the new item.
7074 * Note that the value of the item "di_tv" still needs to be initialized!
7075 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007077 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007078dictitem_alloc(key)
7079 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007080{
Bram Moolenaar33570922005-01-25 22:26:29 +00007081 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007083 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007084 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007085 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007086 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007087 di->di_flags = 0;
7088 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007089 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007090}
7091
7092/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007093 * Make a copy of a Dictionary item.
7094 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007096dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007098{
Bram Moolenaar33570922005-01-25 22:26:29 +00007099 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007100
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007101 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7102 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103 if (di != NULL)
7104 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007105 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007106 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007107 copy_tv(&org->di_tv, &di->di_tv);
7108 }
7109 return di;
7110}
7111
7112/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 * Remove item "item" from Dictionary "dict" and free it.
7114 */
7115 static void
7116dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007117 dict_T *dict;
7118 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119{
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 if (HASHITEM_EMPTY(hi))
7124 EMSG2(_(e_intern2), "dictitem_remove()");
7125 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127 dictitem_free(item);
7128}
7129
7130/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007131 * Free a dict item. Also clears the value.
7132 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007133 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007134dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007136{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007137 clear_tv(&item->di_tv);
7138 vim_free(item);
7139}
7140
7141/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007142 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7143 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007144 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007145 * Returns NULL when out of memory.
7146 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007147 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007148dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007149 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007150 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007151 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152{
Bram Moolenaar33570922005-01-25 22:26:29 +00007153 dict_T *copy;
7154 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007155 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007156 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157
7158 if (orig == NULL)
7159 return NULL;
7160
7161 copy = dict_alloc();
7162 if (copy != NULL)
7163 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007164 if (copyID != 0)
7165 {
7166 orig->dv_copyID = copyID;
7167 orig->dv_copydict = copy;
7168 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007169 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007170 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007171 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007172 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007174 --todo;
7175
7176 di = dictitem_alloc(hi->hi_key);
7177 if (di == NULL)
7178 break;
7179 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007180 {
7181 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7182 copyID) == FAIL)
7183 {
7184 vim_free(di);
7185 break;
7186 }
7187 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007188 else
7189 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7190 if (dict_add(copy, di) == FAIL)
7191 {
7192 dictitem_free(di);
7193 break;
7194 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007195 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007196 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007197
Bram Moolenaare9a41262005-01-15 22:18:47 +00007198 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007199 if (todo > 0)
7200 {
7201 dict_unref(copy);
7202 copy = NULL;
7203 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007204 }
7205
7206 return copy;
7207}
7208
7209/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007211 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007213 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007215 dict_T *d;
7216 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217{
Bram Moolenaar33570922005-01-25 22:26:29 +00007218 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219}
7220
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007222 * Add a number or string entry to dictionary "d".
7223 * When "str" is NULL use number "nr", otherwise use "str".
7224 * Returns FAIL when out of memory and when key already exists.
7225 */
7226 int
7227dict_add_nr_str(d, key, nr, str)
7228 dict_T *d;
7229 char *key;
7230 long nr;
7231 char_u *str;
7232{
7233 dictitem_T *item;
7234
7235 item = dictitem_alloc((char_u *)key);
7236 if (item == NULL)
7237 return FAIL;
7238 item->di_tv.v_lock = 0;
7239 if (str == NULL)
7240 {
7241 item->di_tv.v_type = VAR_NUMBER;
7242 item->di_tv.vval.v_number = nr;
7243 }
7244 else
7245 {
7246 item->di_tv.v_type = VAR_STRING;
7247 item->di_tv.vval.v_string = vim_strsave(str);
7248 }
7249 if (dict_add(d, item) == FAIL)
7250 {
7251 dictitem_free(item);
7252 return FAIL;
7253 }
7254 return OK;
7255}
7256
7257/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007258 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007259 * Returns FAIL when out of memory and when key already exists.
7260 */
7261 int
7262dict_add_list(d, key, list)
7263 dict_T *d;
7264 char *key;
7265 list_T *list;
7266{
7267 dictitem_T *item;
7268
7269 item = dictitem_alloc((char_u *)key);
7270 if (item == NULL)
7271 return FAIL;
7272 item->di_tv.v_lock = 0;
7273 item->di_tv.v_type = VAR_LIST;
7274 item->di_tv.vval.v_list = list;
7275 if (dict_add(d, item) == FAIL)
7276 {
7277 dictitem_free(item);
7278 return FAIL;
7279 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007280 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007281 return OK;
7282}
7283
7284/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007285 * Get the number of items in a Dictionary.
7286 */
7287 static long
7288dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007291 if (d == NULL)
7292 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007293 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007294}
7295
7296/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297 * Find item "key[len]" in Dictionary "d".
7298 * If "len" is negative use strlen(key).
7299 * Returns NULL when not found.
7300 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007301 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007303 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 char_u *key;
7305 int len;
7306{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307#define AKEYLEN 200
7308 char_u buf[AKEYLEN];
7309 char_u *akey;
7310 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 if (len < 0)
7314 akey = key;
7315 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007316 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 tofree = akey = vim_strnsave(key, len);
7318 if (akey == NULL)
7319 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007320 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 else
7322 {
7323 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007324 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 akey = buf;
7326 }
7327
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007329 vim_free(tofree);
7330 if (HASHITEM_EMPTY(hi))
7331 return NULL;
7332 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333}
7334
7335/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007336 * Get a string item from a dictionary.
7337 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007338 * Returns NULL if the entry doesn't exist or out of memory.
7339 */
7340 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007341get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007342 dict_T *d;
7343 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007344 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007345{
7346 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007347 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007348
7349 di = dict_find(d, key, -1);
7350 if (di == NULL)
7351 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007352 s = get_tv_string(&di->di_tv);
7353 if (save && s != NULL)
7354 s = vim_strsave(s);
7355 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007356}
7357
7358/*
7359 * Get a number item from a dictionary.
7360 * Returns 0 if the entry doesn't exist or out of memory.
7361 */
7362 long
7363get_dict_number(d, key)
7364 dict_T *d;
7365 char_u *key;
7366{
7367 dictitem_T *di;
7368
7369 di = dict_find(d, key, -1);
7370 if (di == NULL)
7371 return 0;
7372 return get_tv_number(&di->di_tv);
7373}
7374
7375/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376 * Return an allocated string with the string representation of a Dictionary.
7377 * May return NULL.
7378 */
7379 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007380dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007382 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007383{
7384 garray_T ga;
7385 int first = TRUE;
7386 char_u *tofree;
7387 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007389 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007390 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 return NULL;
7395 ga_init2(&ga, (int)sizeof(char), 80);
7396 ga_append(&ga, '{');
7397
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007398 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007399 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007403 --todo;
7404
7405 if (first)
7406 first = FALSE;
7407 else
7408 ga_concat(&ga, (char_u *)", ");
7409
7410 tofree = string_quote(hi->hi_key, FALSE);
7411 if (tofree != NULL)
7412 {
7413 ga_concat(&ga, tofree);
7414 vim_free(tofree);
7415 }
7416 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007417 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007418 if (s != NULL)
7419 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007421 if (s == NULL)
7422 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007425 if (todo > 0)
7426 {
7427 vim_free(ga.ga_data);
7428 return NULL;
7429 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430
7431 ga_append(&ga, '}');
7432 ga_append(&ga, NUL);
7433 return (char_u *)ga.ga_data;
7434}
7435
7436/*
7437 * Allocate a variable for a Dictionary and fill it from "*arg".
7438 * Return OK or FAIL. Returns NOTDONE for {expr}.
7439 */
7440 static int
7441get_dict_tv(arg, rettv, evaluate)
7442 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007443 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007444 int evaluate;
7445{
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 dict_T *d = NULL;
7447 typval_T tvkey;
7448 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007449 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007452 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007453
7454 /*
7455 * First check if it's not a curly-braces thing: {expr}.
7456 * Must do this without evaluating, otherwise a function may be called
7457 * twice. Unfortunately this means we need to call eval1() twice for the
7458 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007459 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007461 if (*start != '}')
7462 {
7463 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7464 return FAIL;
7465 if (*start == '}')
7466 return NOTDONE;
7467 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468
7469 if (evaluate)
7470 {
7471 d = dict_alloc();
7472 if (d == NULL)
7473 return FAIL;
7474 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007475 tvkey.v_type = VAR_UNKNOWN;
7476 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007477
7478 *arg = skipwhite(*arg + 1);
7479 while (**arg != '}' && **arg != NUL)
7480 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007481 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482 goto failret;
7483 if (**arg != ':')
7484 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007485 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007486 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487 goto failret;
7488 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007489 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007491 key = get_tv_string_buf_chk(&tvkey, buf);
7492 if (key == NULL || *key == NUL)
7493 {
7494 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7495 if (key != NULL)
7496 EMSG(_(e_emptykey));
7497 clear_tv(&tvkey);
7498 goto failret;
7499 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501
7502 *arg = skipwhite(*arg + 1);
7503 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7504 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007505 if (evaluate)
7506 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007507 goto failret;
7508 }
7509 if (evaluate)
7510 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512 if (item != NULL)
7513 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007514 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516 clear_tv(&tv);
7517 goto failret;
7518 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 item = dictitem_alloc(key);
7520 clear_tv(&tvkey);
7521 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007524 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525 if (dict_add(d, item) == FAIL)
7526 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 }
7528 }
7529
7530 if (**arg == '}')
7531 break;
7532 if (**arg != ',')
7533 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007534 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 goto failret;
7536 }
7537 *arg = skipwhite(*arg + 1);
7538 }
7539
7540 if (**arg != '}')
7541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007542 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543failret:
7544 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007545 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007546 return FAIL;
7547 }
7548
7549 *arg = skipwhite(*arg + 1);
7550 if (evaluate)
7551 {
7552 rettv->v_type = VAR_DICT;
7553 rettv->vval.v_dict = d;
7554 ++d->dv_refcount;
7555 }
7556
7557 return OK;
7558}
7559
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007561 * Return a string with the string representation of a variable.
7562 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007563 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007564 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007565 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007566 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007567 */
7568 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007569echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007571 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007572 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007573 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007574{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007575 static int recurse = 0;
7576 char_u *r = NULL;
7577
Bram Moolenaar33570922005-01-25 22:26:29 +00007578 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007579 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007580 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007581 *tofree = NULL;
7582 return NULL;
7583 }
7584 ++recurse;
7585
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007586 switch (tv->v_type)
7587 {
7588 case VAR_FUNC:
7589 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007590 r = tv->vval.v_string;
7591 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007592
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007593 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007594 if (tv->vval.v_list == NULL)
7595 {
7596 *tofree = NULL;
7597 r = NULL;
7598 }
7599 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7600 {
7601 *tofree = NULL;
7602 r = (char_u *)"[...]";
7603 }
7604 else
7605 {
7606 tv->vval.v_list->lv_copyID = copyID;
7607 *tofree = list2string(tv, copyID);
7608 r = *tofree;
7609 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007610 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007611
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007613 if (tv->vval.v_dict == NULL)
7614 {
7615 *tofree = NULL;
7616 r = NULL;
7617 }
7618 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7619 {
7620 *tofree = NULL;
7621 r = (char_u *)"{...}";
7622 }
7623 else
7624 {
7625 tv->vval.v_dict->dv_copyID = copyID;
7626 *tofree = dict2string(tv, copyID);
7627 r = *tofree;
7628 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007629 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007630
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007631 case VAR_STRING:
7632 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007633 *tofree = NULL;
7634 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007635 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007636
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007637#ifdef FEAT_FLOAT
7638 case VAR_FLOAT:
7639 *tofree = NULL;
7640 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7641 r = numbuf;
7642 break;
7643#endif
7644
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007645 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007646 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007648 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007649
7650 --recurse;
7651 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007652}
7653
7654/*
7655 * Return a string with the string representation of a variable.
7656 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7657 * "numbuf" is used for a number.
7658 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007659 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007660 */
7661 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007662tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007663 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007664 char_u **tofree;
7665 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007666 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007667{
7668 switch (tv->v_type)
7669 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007670 case VAR_FUNC:
7671 *tofree = string_quote(tv->vval.v_string, TRUE);
7672 return *tofree;
7673 case VAR_STRING:
7674 *tofree = string_quote(tv->vval.v_string, FALSE);
7675 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007676#ifdef FEAT_FLOAT
7677 case VAR_FLOAT:
7678 *tofree = NULL;
7679 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7680 return numbuf;
7681#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007682 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007683 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007685 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007688 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007689 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007690}
7691
7692/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007693 * Return string "str" in ' quotes, doubling ' characters.
7694 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007696 */
7697 static char_u *
7698string_quote(str, function)
7699 char_u *str;
7700 int function;
7701{
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007703 char_u *p, *r, *s;
7704
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 len = (function ? 13 : 3);
7706 if (str != NULL)
7707 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007708 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 for (p = str; *p != NUL; mb_ptr_adv(p))
7710 if (*p == '\'')
7711 ++len;
7712 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007713 s = r = alloc(len);
7714 if (r != NULL)
7715 {
7716 if (function)
7717 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007719 r += 10;
7720 }
7721 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007723 if (str != NULL)
7724 for (p = str; *p != NUL; )
7725 {
7726 if (*p == '\'')
7727 *r++ = '\'';
7728 MB_COPY_CHAR(p, r);
7729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007731 if (function)
7732 *r++ = ')';
7733 *r++ = NUL;
7734 }
7735 return s;
7736}
7737
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007738#ifdef FEAT_FLOAT
7739/*
7740 * Convert the string "text" to a floating point number.
7741 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7742 * this always uses a decimal point.
7743 * Returns the length of the text that was consumed.
7744 */
7745 static int
7746string2float(text, value)
7747 char_u *text;
7748 float_T *value; /* result stored here */
7749{
7750 char *s = (char *)text;
7751 float_T f;
7752
7753 f = strtod(s, &s);
7754 *value = f;
7755 return (int)((char_u *)s - text);
7756}
7757#endif
7758
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 * Get the value of an environment variable.
7761 * "arg" is pointing to the '$'. It is advanced to after the name.
7762 * If the environment variable was not set, silently assume it is empty.
7763 * Always return OK.
7764 */
7765 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007766get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 int evaluate;
7770{
7771 char_u *string = NULL;
7772 int len;
7773 int cc;
7774 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007775 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776
7777 ++*arg;
7778 name = *arg;
7779 len = get_env_len(arg);
7780 if (evaluate)
7781 {
7782 if (len != 0)
7783 {
7784 cc = name[len];
7785 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007786 /* first try vim_getenv(), fast for normal environment vars */
7787 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007789 {
7790 if (!mustfree)
7791 string = vim_strsave(string);
7792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 else
7794 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007795 if (mustfree)
7796 vim_free(string);
7797
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 /* next try expanding things like $VIM and ${HOME} */
7799 string = expand_env_save(name - 1);
7800 if (string != NULL && *string == '$')
7801 {
7802 vim_free(string);
7803 string = NULL;
7804 }
7805 }
7806 name[len] = cc;
7807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007808 rettv->v_type = VAR_STRING;
7809 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 }
7811
7812 return OK;
7813}
7814
7815/*
7816 * Array with names and number of arguments of all internal functions
7817 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7818 */
7819static struct fst
7820{
7821 char *f_name; /* function name */
7822 char f_min_argc; /* minimal number of arguments */
7823 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007824 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007825 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826} functions[] =
7827{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007828#ifdef FEAT_FLOAT
7829 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007830 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007831#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007832 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007833 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {"append", 2, 2, f_append},
7835 {"argc", 0, 0, f_argc},
7836 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007837 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007839 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007840 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007841 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007844 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 {"bufexists", 1, 1, f_bufexists},
7846 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7847 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7848 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7849 {"buflisted", 1, 1, f_buflisted},
7850 {"bufloaded", 1, 1, f_bufloaded},
7851 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007852 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {"bufwinnr", 1, 1, f_bufwinnr},
7854 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007855 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007857#ifdef FEAT_FLOAT
7858 {"ceil", 1, 1, f_ceil},
7859#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007860 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007861 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007863 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007865#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007866 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007867 {"complete_add", 1, 1, f_complete_add},
7868 {"complete_check", 0, 0, f_complete_check},
7869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007871 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007872#ifdef FEAT_FLOAT
7873 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007874 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007875#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007876 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007878 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007879 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {"delete", 1, 1, f_delete},
7881 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007882 {"diff_filler", 1, 1, f_diff_filler},
7883 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007884 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007886 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"eventhandler", 0, 0, f_eventhandler},
7888 {"executable", 1, 1, f_executable},
7889 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007890#ifdef FEAT_FLOAT
7891 {"exp", 1, 1, f_exp},
7892#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007893 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007894 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007895 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7897 {"filereadable", 1, 1, f_filereadable},
7898 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007899 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007900 {"finddir", 1, 3, f_finddir},
7901 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007902#ifdef FEAT_FLOAT
7903 {"float2nr", 1, 1, f_float2nr},
7904 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007905 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007906#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007907 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 {"fnamemodify", 2, 2, f_fnamemodify},
7909 {"foldclosed", 1, 1, f_foldclosed},
7910 {"foldclosedend", 1, 1, f_foldclosedend},
7911 {"foldlevel", 1, 1, f_foldlevel},
7912 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007913 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007915 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007916 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007917 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007918 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007919 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {"getchar", 0, 1, f_getchar},
7921 {"getcharmod", 0, 0, f_getcharmod},
7922 {"getcmdline", 0, 0, f_getcmdline},
7923 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007924 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007926 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007927 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 {"getfsize", 1, 1, f_getfsize},
7929 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007930 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007931 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007932 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007933 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007934 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007935 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007936 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007937 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007939 {"gettabvar", 2, 3, f_gettabvar},
7940 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 {"getwinposx", 0, 0, f_getwinposx},
7942 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007943 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007944 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007945 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007947 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007948 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007949 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7951 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7952 {"histadd", 2, 2, f_histadd},
7953 {"histdel", 1, 2, f_histdel},
7954 {"histget", 1, 2, f_histget},
7955 {"histnr", 1, 1, f_histnr},
7956 {"hlID", 1, 1, f_hlID},
7957 {"hlexists", 1, 1, f_hlexists},
7958 {"hostname", 0, 0, f_hostname},
7959 {"iconv", 3, 3, f_iconv},
7960 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007961 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007962 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007964 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 {"inputrestore", 0, 0, f_inputrestore},
7966 {"inputsave", 0, 0, f_inputsave},
7967 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007968 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007969 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007971 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007972 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007973 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007974 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007976 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"libcall", 3, 3, f_libcall},
7978 {"libcallnr", 3, 3, f_libcallnr},
7979 {"line", 1, 1, f_line},
7980 {"line2byte", 1, 1, f_line2byte},
7981 {"lispindent", 1, 1, f_lispindent},
7982 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007983#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007984 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007985 {"log10", 1, 1, f_log10},
7986#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007987#ifdef FEAT_LUA
7988 {"luaeval", 1, 2, f_luaeval},
7989#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007991 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007992 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007993 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007994 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007995 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007996 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007997 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007998 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007999 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008000 {"max", 1, 1, f_max},
8001 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008002#ifdef vim_mkdir
8003 {"mkdir", 1, 3, f_mkdir},
8004#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008005 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008006#ifdef FEAT_MZSCHEME
8007 {"mzeval", 1, 1, f_mzeval},
8008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008010 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008011 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008012 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008013#ifdef FEAT_FLOAT
8014 {"pow", 2, 2, f_pow},
8015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008017 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008018 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008019#ifdef FEAT_PYTHON3
8020 {"py3eval", 1, 1, f_py3eval},
8021#endif
8022#ifdef FEAT_PYTHON
8023 {"pyeval", 1, 1, f_pyeval},
8024#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008025 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008026 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008027 {"reltime", 0, 2, f_reltime},
8028 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {"remote_expr", 2, 3, f_remote_expr},
8030 {"remote_foreground", 1, 1, f_remote_foreground},
8031 {"remote_peek", 1, 2, f_remote_peek},
8032 {"remote_read", 1, 1, f_remote_read},
8033 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008034 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008036 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008038 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008039#ifdef FEAT_FLOAT
8040 {"round", 1, 1, f_round},
8041#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008042 {"screenattr", 2, 2, f_screenattr},
8043 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008044 {"screencol", 0, 0, f_screencol},
8045 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008046 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008047 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008048 {"searchpair", 3, 7, f_searchpair},
8049 {"searchpairpos", 3, 7, f_searchpairpos},
8050 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 {"server2client", 2, 2, f_server2client},
8052 {"serverlist", 0, 0, f_serverlist},
8053 {"setbufvar", 3, 3, f_setbufvar},
8054 {"setcmdpos", 1, 1, f_setcmdpos},
8055 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008056 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008057 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008058 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008059 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008061 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008062 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008064#ifdef FEAT_CRYPT
8065 {"sha256", 1, 1, f_sha256},
8066#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008067 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008068 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070#ifdef FEAT_FLOAT
8071 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008072 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008073#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008074 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008075 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008076 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008077 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008078 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079#ifdef FEAT_FLOAT
8080 {"sqrt", 1, 1, f_sqrt},
8081 {"str2float", 1, 1, f_str2float},
8082#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008083 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008084 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008085 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086#ifdef HAVE_STRFTIME
8087 {"strftime", 1, 2, f_strftime},
8088#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008089 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008090 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"strlen", 1, 1, f_strlen},
8092 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008093 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008095 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"submatch", 1, 1, f_submatch},
8097 {"substitute", 4, 4, f_substitute},
8098 {"synID", 3, 3, f_synID},
8099 {"synIDattr", 2, 3, f_synIDattr},
8100 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008101 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008102 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008103 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008104 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008105 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008106 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008107 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008108 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008109#ifdef FEAT_FLOAT
8110 {"tan", 1, 1, f_tan},
8111 {"tanh", 1, 1, f_tanh},
8112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008114 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {"tolower", 1, 1, f_tolower},
8116 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008117 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008118#ifdef FEAT_FLOAT
8119 {"trunc", 1, 1, f_trunc},
8120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008122 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008123 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008124 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"virtcol", 1, 1, f_virtcol},
8126 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008127 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"winbufnr", 1, 1, f_winbufnr},
8129 {"wincol", 0, 0, f_wincol},
8130 {"winheight", 1, 1, f_winheight},
8131 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008132 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008134 {"winrestview", 1, 1, f_winrestview},
8135 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008137 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008138 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139};
8140
8141#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8142
8143/*
8144 * Function given to ExpandGeneric() to obtain the list of internal
8145 * or user defined function names.
8146 */
8147 char_u *
8148get_function_name(xp, idx)
8149 expand_T *xp;
8150 int idx;
8151{
8152 static int intidx = -1;
8153 char_u *name;
8154
8155 if (idx == 0)
8156 intidx = -1;
8157 if (intidx < 0)
8158 {
8159 name = get_user_func_name(xp, idx);
8160 if (name != NULL)
8161 return name;
8162 }
8163 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8164 {
8165 STRCPY(IObuff, functions[intidx].f_name);
8166 STRCAT(IObuff, "(");
8167 if (functions[intidx].f_max_argc == 0)
8168 STRCAT(IObuff, ")");
8169 return IObuff;
8170 }
8171
8172 return NULL;
8173}
8174
8175/*
8176 * Function given to ExpandGeneric() to obtain the list of internal or
8177 * user defined variable or function names.
8178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 char_u *
8180get_expr_name(xp, idx)
8181 expand_T *xp;
8182 int idx;
8183{
8184 static int intidx = -1;
8185 char_u *name;
8186
8187 if (idx == 0)
8188 intidx = -1;
8189 if (intidx < 0)
8190 {
8191 name = get_function_name(xp, idx);
8192 if (name != NULL)
8193 return name;
8194 }
8195 return get_user_var_name(xp, ++intidx);
8196}
8197
8198#endif /* FEAT_CMDL_COMPL */
8199
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008200#if defined(EBCDIC) || defined(PROTO)
8201/*
8202 * Compare struct fst by function name.
8203 */
8204 static int
8205compare_func_name(s1, s2)
8206 const void *s1;
8207 const void *s2;
8208{
8209 struct fst *p1 = (struct fst *)s1;
8210 struct fst *p2 = (struct fst *)s2;
8211
8212 return STRCMP(p1->f_name, p2->f_name);
8213}
8214
8215/*
8216 * Sort the function table by function name.
8217 * The sorting of the table above is ASCII dependant.
8218 * On machines using EBCDIC we have to sort it.
8219 */
8220 static void
8221sortFunctions()
8222{
8223 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8224
8225 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8226}
8227#endif
8228
8229
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230/*
8231 * Find internal function in table above.
8232 * Return index, or -1 if not found
8233 */
8234 static int
8235find_internal_func(name)
8236 char_u *name; /* name of the function */
8237{
8238 int first = 0;
8239 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8240 int cmp;
8241 int x;
8242
8243 /*
8244 * Find the function name in the table. Binary search.
8245 */
8246 while (first <= last)
8247 {
8248 x = first + ((unsigned)(last - first) >> 1);
8249 cmp = STRCMP(name, functions[x].f_name);
8250 if (cmp < 0)
8251 last = x - 1;
8252 else if (cmp > 0)
8253 first = x + 1;
8254 else
8255 return x;
8256 }
8257 return -1;
8258}
8259
8260/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008261 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8262 * name it contains, otherwise return "name".
8263 */
8264 static char_u *
8265deref_func_name(name, lenp)
8266 char_u *name;
8267 int *lenp;
8268{
Bram Moolenaar33570922005-01-25 22:26:29 +00008269 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008270 int cc;
8271
8272 cc = name[*lenp];
8273 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008274 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008275 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008276 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008277 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008278 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008279 {
8280 *lenp = 0;
8281 return (char_u *)""; /* just in case */
8282 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008283 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008284 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008285 }
8286
8287 return name;
8288}
8289
8290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 * Allocate a variable for the result of a function.
8292 * Return OK or FAIL.
8293 */
8294 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008295get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8296 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 char_u *name; /* name of the function */
8298 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 char_u **arg; /* argument, pointing to the '(' */
8301 linenr_T firstline; /* first line of range */
8302 linenr_T lastline; /* last line of range */
8303 int *doesrange; /* return: function handled range */
8304 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008305 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306{
8307 char_u *argp;
8308 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008309 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 int argcount = 0; /* number of arguments found */
8311
8312 /*
8313 * Get the arguments.
8314 */
8315 argp = *arg;
8316 while (argcount < MAX_FUNC_ARGS)
8317 {
8318 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8319 if (*argp == ')' || *argp == ',' || *argp == NUL)
8320 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8322 {
8323 ret = FAIL;
8324 break;
8325 }
8326 ++argcount;
8327 if (*argp != ',')
8328 break;
8329 }
8330 if (*argp == ')')
8331 ++argp;
8332 else
8333 ret = FAIL;
8334
8335 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008336 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008337 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008339 {
8340 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008341 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008342 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008343 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345
8346 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008347 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348
8349 *arg = skipwhite(argp);
8350 return ret;
8351}
8352
8353
8354/*
8355 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008356 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008357 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 */
8359 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008360call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008361 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008362 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008364 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008366 typval_T *argvars; /* vars for arguments, must have "argcount"
8367 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 linenr_T firstline; /* first line of range */
8369 linenr_T lastline; /* last line of range */
8370 int *doesrange; /* return: function handled range */
8371 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
8374 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375#define ERROR_UNKNOWN 0
8376#define ERROR_TOOMANY 1
8377#define ERROR_TOOFEW 2
8378#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008379#define ERROR_DICT 4
8380#define ERROR_NONE 5
8381#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 int error = ERROR_NONE;
8383 int i;
8384 int llen;
8385 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386#define FLEN_FIXED 40
8387 char_u fname_buf[FLEN_FIXED + 1];
8388 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008389 char_u *name;
8390
8391 /* Make a copy of the name, if it comes from a funcref variable it could
8392 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008393 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008394 if (name == NULL)
8395 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396
8397 /*
8398 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8399 * Change <SNR>123_name() to K_SNR 123_name().
8400 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 llen = eval_fname_script(name);
8403 if (llen > 0)
8404 {
8405 fname_buf[0] = K_SPECIAL;
8406 fname_buf[1] = KS_EXTRA;
8407 fname_buf[2] = (int)KE_SNR;
8408 i = 3;
8409 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8410 {
8411 if (current_SID <= 0)
8412 error = ERROR_SCRIPT;
8413 else
8414 {
8415 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8416 i = (int)STRLEN(fname_buf);
8417 }
8418 }
8419 if (i + STRLEN(name + llen) < FLEN_FIXED)
8420 {
8421 STRCPY(fname_buf + i, name + llen);
8422 fname = fname_buf;
8423 }
8424 else
8425 {
8426 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8427 if (fname == NULL)
8428 error = ERROR_OTHER;
8429 else
8430 {
8431 mch_memmove(fname, fname_buf, (size_t)i);
8432 STRCPY(fname + i, name + llen);
8433 }
8434 }
8435 }
8436 else
8437 fname = name;
8438
8439 *doesrange = FALSE;
8440
8441
8442 /* execute the function if no errors detected and executing */
8443 if (evaluate && error == ERROR_NONE)
8444 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008445 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8446 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 error = ERROR_UNKNOWN;
8448
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008449 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 {
8451 /*
8452 * User defined function.
8453 */
8454 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008455
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008457 /* Trigger FuncUndefined event, may load the function. */
8458 if (fp == NULL
8459 && apply_autocmds(EVENT_FUNCUNDEFINED,
8460 fname, fname, TRUE, NULL)
8461 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008463 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 fp = find_func(fname);
8465 }
8466#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008467 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008468 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008469 {
8470 /* loaded a package, search for the function again */
8471 fp = find_func(fname);
8472 }
8473
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 if (fp != NULL)
8475 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008476 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008478 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008480 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008482 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008483 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 else
8485 {
8486 /*
8487 * Call the user function.
8488 * Save and restore search patterns, script variables and
8489 * redo buffer.
8490 */
8491 save_search_patterns();
8492 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008493 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008494 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008495 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008496 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8497 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8498 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008499 /* Function was unreferenced while being used, free it
8500 * now. */
8501 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 restoreRedobuff();
8503 restore_search_patterns();
8504 error = ERROR_NONE;
8505 }
8506 }
8507 }
8508 else
8509 {
8510 /*
8511 * Find the function name in the table, call its implementation.
8512 */
8513 i = find_internal_func(fname);
8514 if (i >= 0)
8515 {
8516 if (argcount < functions[i].f_min_argc)
8517 error = ERROR_TOOFEW;
8518 else if (argcount > functions[i].f_max_argc)
8519 error = ERROR_TOOMANY;
8520 else
8521 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008523 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 error = ERROR_NONE;
8525 }
8526 }
8527 }
8528 /*
8529 * The function call (or "FuncUndefined" autocommand sequence) might
8530 * have been aborted by an error, an interrupt, or an explicitly thrown
8531 * exception that has not been caught so far. This situation can be
8532 * tested for by calling aborting(). For an error in an internal
8533 * function or for the "E132" error in call_user_func(), however, the
8534 * throw point at which the "force_abort" flag (temporarily reset by
8535 * emsg()) is normally updated has not been reached yet. We need to
8536 * update that flag first to make aborting() reliable.
8537 */
8538 update_force_abort();
8539 }
8540 if (error == ERROR_NONE)
8541 ret = OK;
8542
8543 /*
8544 * Report an error unless the argument evaluation or function call has been
8545 * cancelled due to an aborting error, an interrupt, or an exception.
8546 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008547 if (!aborting())
8548 {
8549 switch (error)
8550 {
8551 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008552 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008553 break;
8554 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008555 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008556 break;
8557 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008558 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008559 name);
8560 break;
8561 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008562 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008563 name);
8564 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008565 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008566 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008567 name);
8568 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008569 }
8570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 if (fname != name && fname != fname_buf)
8573 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008574 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575
8576 return ret;
8577}
8578
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008579/*
8580 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008581 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008582 */
8583 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008584emsg_funcname(ermsg, name)
8585 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008586 char_u *name;
8587{
8588 char_u *p;
8589
8590 if (*name == K_SPECIAL)
8591 p = concat_str((char_u *)"<SNR>", name + 3);
8592 else
8593 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008594 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008595 if (p != name)
8596 vim_free(p);
8597}
8598
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008599/*
8600 * Return TRUE for a non-zero Number and a non-empty String.
8601 */
8602 static int
8603non_zero_arg(argvars)
8604 typval_T *argvars;
8605{
8606 return ((argvars[0].v_type == VAR_NUMBER
8607 && argvars[0].vval.v_number != 0)
8608 || (argvars[0].v_type == VAR_STRING
8609 && argvars[0].vval.v_string != NULL
8610 && *argvars[0].vval.v_string != NUL));
8611}
8612
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613/*********************************************
8614 * Implementation of the built-in functions
8615 */
8616
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008617#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008618static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8619
8620/*
8621 * Get the float value of "argvars[0]" into "f".
8622 * Returns FAIL when the argument is not a Number or Float.
8623 */
8624 static int
8625get_float_arg(argvars, f)
8626 typval_T *argvars;
8627 float_T *f;
8628{
8629 if (argvars[0].v_type == VAR_FLOAT)
8630 {
8631 *f = argvars[0].vval.v_float;
8632 return OK;
8633 }
8634 if (argvars[0].v_type == VAR_NUMBER)
8635 {
8636 *f = (float_T)argvars[0].vval.v_number;
8637 return OK;
8638 }
8639 EMSG(_("E808: Number or Float required"));
8640 return FAIL;
8641}
8642
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008643/*
8644 * "abs(expr)" function
8645 */
8646 static void
8647f_abs(argvars, rettv)
8648 typval_T *argvars;
8649 typval_T *rettv;
8650{
8651 if (argvars[0].v_type == VAR_FLOAT)
8652 {
8653 rettv->v_type = VAR_FLOAT;
8654 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8655 }
8656 else
8657 {
8658 varnumber_T n;
8659 int error = FALSE;
8660
8661 n = get_tv_number_chk(&argvars[0], &error);
8662 if (error)
8663 rettv->vval.v_number = -1;
8664 else if (n > 0)
8665 rettv->vval.v_number = n;
8666 else
8667 rettv->vval.v_number = -n;
8668 }
8669}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008670
8671/*
8672 * "acos()" function
8673 */
8674 static void
8675f_acos(argvars, rettv)
8676 typval_T *argvars;
8677 typval_T *rettv;
8678{
8679 float_T f;
8680
8681 rettv->v_type = VAR_FLOAT;
8682 if (get_float_arg(argvars, &f) == OK)
8683 rettv->vval.v_float = acos(f);
8684 else
8685 rettv->vval.v_float = 0.0;
8686}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008687#endif
8688
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008690 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 */
8692 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008693f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008694 typval_T *argvars;
8695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
Bram Moolenaar33570922005-01-25 22:26:29 +00008697 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008699 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008700 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008702 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008703 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008704 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008705 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008706 }
8707 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008708 EMSG(_(e_listreq));
8709}
8710
8711/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008712 * "and(expr, expr)" function
8713 */
8714 static void
8715f_and(argvars, rettv)
8716 typval_T *argvars;
8717 typval_T *rettv;
8718{
8719 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8720 & get_tv_number_chk(&argvars[1], NULL);
8721}
8722
8723/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008724 * "append(lnum, string/list)" function
8725 */
8726 static void
8727f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008728 typval_T *argvars;
8729 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008730{
8731 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008732 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008733 list_T *l = NULL;
8734 listitem_T *li = NULL;
8735 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008736 long added = 0;
8737
Bram Moolenaar0d660222005-01-07 21:51:51 +00008738 lnum = get_tv_lnum(argvars);
8739 if (lnum >= 0
8740 && lnum <= curbuf->b_ml.ml_line_count
8741 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008742 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008743 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008744 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008745 l = argvars[1].vval.v_list;
8746 if (l == NULL)
8747 return;
8748 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008749 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008750 for (;;)
8751 {
8752 if (l == NULL)
8753 tv = &argvars[1]; /* append a string */
8754 else if (li == NULL)
8755 break; /* end of list */
8756 else
8757 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008758 line = get_tv_string_chk(tv);
8759 if (line == NULL) /* type error */
8760 {
8761 rettv->vval.v_number = 1; /* Failed */
8762 break;
8763 }
8764 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008765 ++added;
8766 if (l == NULL)
8767 break;
8768 li = li->li_next;
8769 }
8770
8771 appended_lines_mark(lnum, added);
8772 if (curwin->w_cursor.lnum > lnum)
8773 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008775 else
8776 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777}
8778
8779/*
8780 * "argc()" function
8781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008783f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008784 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008787 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788}
8789
8790/*
8791 * "argidx()" function
8792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008795 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008798 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799}
8800
8801/*
8802 * "argv(nr)" function
8803 */
8804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008805f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008806 typval_T *argvars;
8807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808{
8809 int idx;
8810
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008811 if (argvars[0].v_type != VAR_UNKNOWN)
8812 {
8813 idx = get_tv_number_chk(&argvars[0], NULL);
8814 if (idx >= 0 && idx < ARGCOUNT)
8815 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8816 else
8817 rettv->vval.v_string = NULL;
8818 rettv->v_type = VAR_STRING;
8819 }
8820 else if (rettv_list_alloc(rettv) == OK)
8821 for (idx = 0; idx < ARGCOUNT; ++idx)
8822 list_append_string(rettv->vval.v_list,
8823 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824}
8825
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008826#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008827/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008828 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008829 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008830 static void
8831f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008832 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008833 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008834{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008835 float_T f;
8836
8837 rettv->v_type = VAR_FLOAT;
8838 if (get_float_arg(argvars, &f) == OK)
8839 rettv->vval.v_float = asin(f);
8840 else
8841 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008842}
8843
8844/*
8845 * "atan()" function
8846 */
8847 static void
8848f_atan(argvars, rettv)
8849 typval_T *argvars;
8850 typval_T *rettv;
8851{
8852 float_T f;
8853
8854 rettv->v_type = VAR_FLOAT;
8855 if (get_float_arg(argvars, &f) == OK)
8856 rettv->vval.v_float = atan(f);
8857 else
8858 rettv->vval.v_float = 0.0;
8859}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008860
8861/*
8862 * "atan2()" function
8863 */
8864 static void
8865f_atan2(argvars, rettv)
8866 typval_T *argvars;
8867 typval_T *rettv;
8868{
8869 float_T fx, fy;
8870
8871 rettv->v_type = VAR_FLOAT;
8872 if (get_float_arg(argvars, &fx) == OK
8873 && get_float_arg(&argvars[1], &fy) == OK)
8874 rettv->vval.v_float = atan2(fx, fy);
8875 else
8876 rettv->vval.v_float = 0.0;
8877}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008878#endif
8879
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880/*
8881 * "browse(save, title, initdir, default)" function
8882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008884f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008885 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887{
8888#ifdef FEAT_BROWSE
8889 int save;
8890 char_u *title;
8891 char_u *initdir;
8892 char_u *defname;
8893 char_u buf[NUMBUFLEN];
8894 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008895 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008897 save = get_tv_number_chk(&argvars[0], &error);
8898 title = get_tv_string_chk(&argvars[1]);
8899 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8900 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008902 if (error || title == NULL || initdir == NULL || defname == NULL)
8903 rettv->vval.v_string = NULL;
8904 else
8905 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008906 do_browse(save ? BROWSE_SAVE : 0,
8907 title, defname, NULL, initdir, NULL, curbuf);
8908#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008909 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008910#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008911 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008912}
8913
8914/*
8915 * "browsedir(title, initdir)" function
8916 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008919 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008920 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008921{
8922#ifdef FEAT_BROWSE
8923 char_u *title;
8924 char_u *initdir;
8925 char_u buf[NUMBUFLEN];
8926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008927 title = get_tv_string_chk(&argvars[0]);
8928 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008930 if (title == NULL || initdir == NULL)
8931 rettv->vval.v_string = NULL;
8932 else
8933 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008934 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008938 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939}
8940
Bram Moolenaar33570922005-01-25 22:26:29 +00008941static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008942
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943/*
8944 * Find a buffer by number or exact name.
8945 */
8946 static buf_T *
8947find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008948 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949{
8950 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008952 if (avar->v_type == VAR_NUMBER)
8953 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008954 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008956 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008957 if (buf == NULL)
8958 {
8959 /* No full path name match, try a match with a URL or a "nofile"
8960 * buffer, these don't use the full path. */
8961 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8962 if (buf->b_fname != NULL
8963 && (path_with_url(buf->b_fname)
8964#ifdef FEAT_QUICKFIX
8965 || bt_nofile(buf)
8966#endif
8967 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008968 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008969 break;
8970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 }
8972 return buf;
8973}
8974
8975/*
8976 * "bufexists(expr)" function
8977 */
8978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008980 typval_T *argvars;
8981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984}
8985
8986/*
8987 * "buflisted(expr)" function
8988 */
8989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008990f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008991 typval_T *argvars;
8992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
8994 buf_T *buf;
8995
8996 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008997 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998}
8999
9000/*
9001 * "bufloaded(expr)" function
9002 */
9003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009005 typval_T *argvars;
9006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
9008 buf_T *buf;
9009
9010 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009011 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012}
9013
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009014static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009015
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016/*
9017 * Get buffer by number or pattern.
9018 */
9019 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009020get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009021 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009022 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009024 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 int save_magic;
9026 char_u *save_cpo;
9027 buf_T *buf;
9028
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009029 if (tv->v_type == VAR_NUMBER)
9030 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009031 if (tv->v_type != VAR_STRING)
9032 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 if (name == NULL || *name == NUL)
9034 return curbuf;
9035 if (name[0] == '$' && name[1] == NUL)
9036 return lastbuf;
9037
9038 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9039 save_magic = p_magic;
9040 p_magic = TRUE;
9041 save_cpo = p_cpo;
9042 p_cpo = (char_u *)"";
9043
9044 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009045 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046
9047 p_magic = save_magic;
9048 p_cpo = save_cpo;
9049
9050 /* If not found, try expanding the name, like done for bufexists(). */
9051 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053
9054 return buf;
9055}
9056
9057/*
9058 * "bufname(expr)" function
9059 */
9060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 typval_T *argvars;
9063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
9065 buf_T *buf;
9066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009067 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009069 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009074 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 --emsg_off;
9076}
9077
9078/*
9079 * "bufnr(expr)" function
9080 */
9081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009083 typval_T *argvars;
9084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085{
9086 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009087 int error = FALSE;
9088 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009090 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009092 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009093 --emsg_off;
9094
9095 /* If the buffer isn't found and the second argument is not zero create a
9096 * new buffer. */
9097 if (buf == NULL
9098 && argvars[1].v_type != VAR_UNKNOWN
9099 && get_tv_number_chk(&argvars[1], &error) != 0
9100 && !error
9101 && (name = get_tv_string_chk(&argvars[0])) != NULL
9102 && !error)
9103 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9104
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109}
9110
9111/*
9112 * "bufwinnr(nr)" function
9113 */
9114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009116 typval_T *argvars;
9117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118{
9119#ifdef FEAT_WINDOWS
9120 win_T *wp;
9121 int winnr = 0;
9122#endif
9123 buf_T *buf;
9124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009125 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009127 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128#ifdef FEAT_WINDOWS
9129 for (wp = firstwin; wp; wp = wp->w_next)
9130 {
9131 ++winnr;
9132 if (wp->w_buffer == buf)
9133 break;
9134 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009135 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009137 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138#endif
9139 --emsg_off;
9140}
9141
9142/*
9143 * "byte2line(byte)" function
9144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009147 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009151 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152#else
9153 long boff = 0;
9154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009155 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009157 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009159 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 (linenr_T)0, &boff);
9161#endif
9162}
9163
9164/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009165 * "byteidx()" function
9166 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009168f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009169 typval_T *argvars;
9170 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009171{
9172#ifdef FEAT_MBYTE
9173 char_u *t;
9174#endif
9175 char_u *str;
9176 long idx;
9177
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009178 str = get_tv_string_chk(&argvars[0]);
9179 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009181 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009182 return;
9183
9184#ifdef FEAT_MBYTE
9185 t = str;
9186 for ( ; idx > 0; idx--)
9187 {
9188 if (*t == NUL) /* EOL reached */
9189 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009190 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009191 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009192 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009193#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009194 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009196#endif
9197}
9198
Bram Moolenaardb913952012-06-29 12:54:53 +02009199 int
9200func_call(name, args, selfdict, rettv)
9201 char_u *name;
9202 typval_T *args;
9203 dict_T *selfdict;
9204 typval_T *rettv;
9205{
9206 listitem_T *item;
9207 typval_T argv[MAX_FUNC_ARGS + 1];
9208 int argc = 0;
9209 int dummy;
9210 int r = 0;
9211
9212 for (item = args->vval.v_list->lv_first; item != NULL;
9213 item = item->li_next)
9214 {
9215 if (argc == MAX_FUNC_ARGS)
9216 {
9217 EMSG(_("E699: Too many arguments"));
9218 break;
9219 }
9220 /* Make a copy of each argument. This is needed to be able to set
9221 * v_lock to VAR_FIXED in the copy without changing the original list.
9222 */
9223 copy_tv(&item->li_tv, &argv[argc++]);
9224 }
9225
9226 if (item == NULL)
9227 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9228 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9229 &dummy, TRUE, selfdict);
9230
9231 /* Free the arguments. */
9232 while (argc > 0)
9233 clear_tv(&argv[--argc]);
9234
9235 return r;
9236}
9237
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009238/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009239 * "call(func, arglist)" function
9240 */
9241 static void
9242f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009243 typval_T *argvars;
9244 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009245{
9246 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009247 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009248
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009249 if (argvars[1].v_type != VAR_LIST)
9250 {
9251 EMSG(_(e_listreq));
9252 return;
9253 }
9254 if (argvars[1].vval.v_list == NULL)
9255 return;
9256
9257 if (argvars[0].v_type == VAR_FUNC)
9258 func = argvars[0].vval.v_string;
9259 else
9260 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009261 if (*func == NUL)
9262 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009263
Bram Moolenaare9a41262005-01-15 22:18:47 +00009264 if (argvars[2].v_type != VAR_UNKNOWN)
9265 {
9266 if (argvars[2].v_type != VAR_DICT)
9267 {
9268 EMSG(_(e_dictreq));
9269 return;
9270 }
9271 selfdict = argvars[2].vval.v_dict;
9272 }
9273
Bram Moolenaardb913952012-06-29 12:54:53 +02009274 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009275}
9276
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009277#ifdef FEAT_FLOAT
9278/*
9279 * "ceil({float})" function
9280 */
9281 static void
9282f_ceil(argvars, rettv)
9283 typval_T *argvars;
9284 typval_T *rettv;
9285{
9286 float_T f;
9287
9288 rettv->v_type = VAR_FLOAT;
9289 if (get_float_arg(argvars, &f) == OK)
9290 rettv->vval.v_float = ceil(f);
9291 else
9292 rettv->vval.v_float = 0.0;
9293}
9294#endif
9295
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009296/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009297 * "changenr()" function
9298 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009299 static void
9300f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009301 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009302 typval_T *rettv;
9303{
9304 rettv->vval.v_number = curbuf->b_u_seq_cur;
9305}
9306
9307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 * "char2nr(string)" function
9309 */
9310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009311f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009312 typval_T *argvars;
9313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314{
9315#ifdef FEAT_MBYTE
9316 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009317 {
9318 int utf8 = 0;
9319
9320 if (argvars[1].v_type != VAR_UNKNOWN)
9321 utf8 = get_tv_number_chk(&argvars[1], NULL);
9322
9323 if (utf8)
9324 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9325 else
9326 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 else
9329#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331}
9332
9333/*
9334 * "cindent(lnum)" function
9335 */
9336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009337f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009338 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340{
9341#ifdef FEAT_CINDENT
9342 pos_T pos;
9343 linenr_T lnum;
9344
9345 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009346 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9348 {
9349 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 curwin->w_cursor = pos;
9352 }
9353 else
9354#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356}
9357
9358/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009359 * "clearmatches()" function
9360 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009361 static void
9362f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009363 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009364 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009365{
9366#ifdef FEAT_SEARCH_EXTRA
9367 clear_matches(curwin);
9368#endif
9369}
9370
9371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 * "col(string)" function
9373 */
9374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009376 typval_T *argvars;
9377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
9379 colnr_T col = 0;
9380 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009381 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009383 fp = var2fpos(&argvars[0], FALSE, &fnum);
9384 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 {
9386 if (fp->col == MAXCOL)
9387 {
9388 /* '> can be MAXCOL, get the length of the line then */
9389 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009390 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 else
9392 col = MAXCOL;
9393 }
9394 else
9395 {
9396 col = fp->col + 1;
9397#ifdef FEAT_VIRTUALEDIT
9398 /* col(".") when the cursor is on the NUL at the end of the line
9399 * because of "coladd" can be seen as an extra column. */
9400 if (virtual_active() && fp == &curwin->w_cursor)
9401 {
9402 char_u *p = ml_get_cursor();
9403
9404 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9405 curwin->w_virtcol - curwin->w_cursor.coladd))
9406 {
9407# ifdef FEAT_MBYTE
9408 int l;
9409
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009410 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 col += l;
9412# else
9413 if (*p != NUL && p[1] == NUL)
9414 ++col;
9415# endif
9416 }
9417 }
9418#endif
9419 }
9420 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009421 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422}
9423
Bram Moolenaar572cb562005-08-05 21:35:02 +00009424#if defined(FEAT_INS_EXPAND)
9425/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009426 * "complete()" function
9427 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009428 static void
9429f_complete(argvars, rettv)
9430 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009431 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009432{
9433 int startcol;
9434
9435 if ((State & INSERT) == 0)
9436 {
9437 EMSG(_("E785: complete() can only be used in Insert mode"));
9438 return;
9439 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009440
9441 /* Check for undo allowed here, because if something was already inserted
9442 * the line was already saved for undo and this check isn't done. */
9443 if (!undo_allowed())
9444 return;
9445
Bram Moolenaarade00832006-03-10 21:46:58 +00009446 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9447 {
9448 EMSG(_(e_invarg));
9449 return;
9450 }
9451
9452 startcol = get_tv_number_chk(&argvars[0], NULL);
9453 if (startcol <= 0)
9454 return;
9455
9456 set_completion(startcol - 1, argvars[1].vval.v_list);
9457}
9458
9459/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009460 * "complete_add()" function
9461 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009462 static void
9463f_complete_add(argvars, rettv)
9464 typval_T *argvars;
9465 typval_T *rettv;
9466{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009467 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009468}
9469
9470/*
9471 * "complete_check()" function
9472 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009473 static void
9474f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009475 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009476 typval_T *rettv;
9477{
9478 int saved = RedrawingDisabled;
9479
9480 RedrawingDisabled = 0;
9481 ins_compl_check_keys(0);
9482 rettv->vval.v_number = compl_interrupted;
9483 RedrawingDisabled = saved;
9484}
9485#endif
9486
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487/*
9488 * "confirm(message, buttons[, default [, type]])" function
9489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009492 typval_T *argvars UNUSED;
9493 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494{
9495#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9496 char_u *message;
9497 char_u *buttons = NULL;
9498 char_u buf[NUMBUFLEN];
9499 char_u buf2[NUMBUFLEN];
9500 int def = 1;
9501 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009502 char_u *typestr;
9503 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009505 message = get_tv_string_chk(&argvars[0]);
9506 if (message == NULL)
9507 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009508 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009510 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9511 if (buttons == NULL)
9512 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009513 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009515 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009516 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009518 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9519 if (typestr == NULL)
9520 error = TRUE;
9521 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009523 switch (TOUPPER_ASC(*typestr))
9524 {
9525 case 'E': type = VIM_ERROR; break;
9526 case 'Q': type = VIM_QUESTION; break;
9527 case 'I': type = VIM_INFO; break;
9528 case 'W': type = VIM_WARNING; break;
9529 case 'G': type = VIM_GENERIC; break;
9530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 }
9532 }
9533 }
9534 }
9535
9536 if (buttons == NULL || *buttons == NUL)
9537 buttons = (char_u *)_("&Ok");
9538
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009539 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009540 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009541 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542#endif
9543}
9544
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009545/*
9546 * "copy()" function
9547 */
9548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009550 typval_T *argvars;
9551 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009552{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009553 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009554}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009556#ifdef FEAT_FLOAT
9557/*
9558 * "cos()" function
9559 */
9560 static void
9561f_cos(argvars, rettv)
9562 typval_T *argvars;
9563 typval_T *rettv;
9564{
9565 float_T f;
9566
9567 rettv->v_type = VAR_FLOAT;
9568 if (get_float_arg(argvars, &f) == OK)
9569 rettv->vval.v_float = cos(f);
9570 else
9571 rettv->vval.v_float = 0.0;
9572}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009573
9574/*
9575 * "cosh()" function
9576 */
9577 static void
9578f_cosh(argvars, rettv)
9579 typval_T *argvars;
9580 typval_T *rettv;
9581{
9582 float_T f;
9583
9584 rettv->v_type = VAR_FLOAT;
9585 if (get_float_arg(argvars, &f) == OK)
9586 rettv->vval.v_float = cosh(f);
9587 else
9588 rettv->vval.v_float = 0.0;
9589}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009590#endif
9591
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009593 * "count()" function
9594 */
9595 static void
9596f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009597 typval_T *argvars;
9598 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009599{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009600 long n = 0;
9601 int ic = FALSE;
9602
Bram Moolenaare9a41262005-01-15 22:18:47 +00009603 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009604 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009605 listitem_T *li;
9606 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009607 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009608
Bram Moolenaare9a41262005-01-15 22:18:47 +00009609 if ((l = argvars[0].vval.v_list) != NULL)
9610 {
9611 li = l->lv_first;
9612 if (argvars[2].v_type != VAR_UNKNOWN)
9613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009614 int error = FALSE;
9615
9616 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009617 if (argvars[3].v_type != VAR_UNKNOWN)
9618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009619 idx = get_tv_number_chk(&argvars[3], &error);
9620 if (!error)
9621 {
9622 li = list_find(l, idx);
9623 if (li == NULL)
9624 EMSGN(_(e_listidx), idx);
9625 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009626 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009627 if (error)
9628 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009629 }
9630
9631 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009632 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009633 ++n;
9634 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009635 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009636 else if (argvars[0].v_type == VAR_DICT)
9637 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009638 int todo;
9639 dict_T *d;
9640 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009641
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009642 if ((d = argvars[0].vval.v_dict) != NULL)
9643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009644 int error = FALSE;
9645
Bram Moolenaare9a41262005-01-15 22:18:47 +00009646 if (argvars[2].v_type != VAR_UNKNOWN)
9647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009648 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009649 if (argvars[3].v_type != VAR_UNKNOWN)
9650 EMSG(_(e_invarg));
9651 }
9652
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009653 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009654 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009655 {
9656 if (!HASHITEM_EMPTY(hi))
9657 {
9658 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009659 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009660 ++n;
9661 }
9662 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009663 }
9664 }
9665 else
9666 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009667 rettv->vval.v_number = n;
9668}
9669
9670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9672 *
9673 * Checks the existence of a cscope connection.
9674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009677 typval_T *argvars UNUSED;
9678 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679{
9680#ifdef FEAT_CSCOPE
9681 int num = 0;
9682 char_u *dbpath = NULL;
9683 char_u *prepend = NULL;
9684 char_u buf[NUMBUFLEN];
9685
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009686 if (argvars[0].v_type != VAR_UNKNOWN
9687 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689 num = (int)get_tv_number(&argvars[0]);
9690 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009691 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 }
9694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696#endif
9697}
9698
9699/*
9700 * "cursor(lnum, col)" function
9701 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009702 * Moves the cursor to the specified line and column.
9703 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009707 typval_T *argvars;
9708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709{
9710 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009711#ifdef FEAT_VIRTUALEDIT
9712 long coladd = 0;
9713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009715 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009716 if (argvars[1].v_type == VAR_UNKNOWN)
9717 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009718 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009719
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009720 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009721 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009722 line = pos.lnum;
9723 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009724#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009725 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009726#endif
9727 }
9728 else
9729 {
9730 line = get_tv_lnum(argvars);
9731 col = get_tv_number_chk(&argvars[1], NULL);
9732#ifdef FEAT_VIRTUALEDIT
9733 if (argvars[2].v_type != VAR_UNKNOWN)
9734 coladd = get_tv_number_chk(&argvars[2], NULL);
9735#endif
9736 }
9737 if (line < 0 || col < 0
9738#ifdef FEAT_VIRTUALEDIT
9739 || coladd < 0
9740#endif
9741 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009742 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 if (line > 0)
9744 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 if (col > 0)
9746 curwin->w_cursor.col = col - 1;
9747#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009748 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749#endif
9750
9751 /* Make sure the cursor is in a valid position. */
9752 check_cursor();
9753#ifdef FEAT_MBYTE
9754 /* Correct cursor for multi-byte character. */
9755 if (has_mbyte)
9756 mb_adjust_cursor();
9757#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009758
9759 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009760 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761}
9762
9763/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009764 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 */
9766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009768 typval_T *argvars;
9769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009771 int noref = 0;
9772
9773 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009774 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009775 if (noref < 0 || noref > 1)
9776 EMSG(_(e_invarg));
9777 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009778 {
9779 current_copyID += COPYID_INC;
9780 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782}
9783
9784/*
9785 * "delete()" function
9786 */
9787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009788f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009789 typval_T *argvars;
9790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791{
9792 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796}
9797
9798/*
9799 * "did_filetype()" function
9800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009803 typval_T *argvars UNUSED;
9804 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805{
9806#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808#endif
9809}
9810
9811/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009812 * "diff_filler()" function
9813 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009816 typval_T *argvars UNUSED;
9817 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009818{
9819#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009820 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009821#endif
9822}
9823
9824/*
9825 * "diff_hlID()" function
9826 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009829 typval_T *argvars UNUSED;
9830 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009831{
9832#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009834 static linenr_T prev_lnum = 0;
9835 static int changedtick = 0;
9836 static int fnum = 0;
9837 static int change_start = 0;
9838 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009839 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009840 int filler_lines;
9841 int col;
9842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009843 if (lnum < 0) /* ignore type error in {lnum} arg */
9844 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009845 if (lnum != prev_lnum
9846 || changedtick != curbuf->b_changedtick
9847 || fnum != curbuf->b_fnum)
9848 {
9849 /* New line, buffer, change: need to get the values. */
9850 filler_lines = diff_check(curwin, lnum);
9851 if (filler_lines < 0)
9852 {
9853 if (filler_lines == -1)
9854 {
9855 change_start = MAXCOL;
9856 change_end = -1;
9857 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9858 hlID = HLF_ADD; /* added line */
9859 else
9860 hlID = HLF_CHD; /* changed line */
9861 }
9862 else
9863 hlID = HLF_ADD; /* added line */
9864 }
9865 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009866 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009867 prev_lnum = lnum;
9868 changedtick = curbuf->b_changedtick;
9869 fnum = curbuf->b_fnum;
9870 }
9871
9872 if (hlID == HLF_CHD || hlID == HLF_TXD)
9873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009874 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009875 if (col >= change_start && col <= change_end)
9876 hlID = HLF_TXD; /* changed text */
9877 else
9878 hlID = HLF_CHD; /* changed line */
9879 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009880 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009881#endif
9882}
9883
9884/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009885 * "empty({expr})" function
9886 */
9887 static void
9888f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009889 typval_T *argvars;
9890 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009891{
9892 int n;
9893
9894 switch (argvars[0].v_type)
9895 {
9896 case VAR_STRING:
9897 case VAR_FUNC:
9898 n = argvars[0].vval.v_string == NULL
9899 || *argvars[0].vval.v_string == NUL;
9900 break;
9901 case VAR_NUMBER:
9902 n = argvars[0].vval.v_number == 0;
9903 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009904#ifdef FEAT_FLOAT
9905 case VAR_FLOAT:
9906 n = argvars[0].vval.v_float == 0.0;
9907 break;
9908#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009909 case VAR_LIST:
9910 n = argvars[0].vval.v_list == NULL
9911 || argvars[0].vval.v_list->lv_first == NULL;
9912 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009913 case VAR_DICT:
9914 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009915 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009916 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009917 default:
9918 EMSG2(_(e_intern2), "f_empty()");
9919 n = 0;
9920 }
9921
9922 rettv->vval.v_number = n;
9923}
9924
9925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 * "escape({string}, {chars})" function
9927 */
9928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009929f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009930 typval_T *argvars;
9931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932{
9933 char_u buf[NUMBUFLEN];
9934
Bram Moolenaar758711c2005-02-02 23:11:38 +00009935 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9936 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938}
9939
9940/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009941 * "eval()" function
9942 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009943 static void
9944f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009945 typval_T *argvars;
9946 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009947{
9948 char_u *s;
9949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009950 s = get_tv_string_chk(&argvars[0]);
9951 if (s != NULL)
9952 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009953
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009954 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9955 {
9956 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009957 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009958 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009959 else if (*s != NUL)
9960 EMSG(_(e_trailing));
9961}
9962
9963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 * "eventhandler()" function
9965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009968 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972}
9973
9974/*
9975 * "executable()" function
9976 */
9977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009979 typval_T *argvars;
9980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983}
9984
9985/*
9986 * "exists()" function
9987 */
9988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009989f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009990 typval_T *argvars;
9991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992{
9993 char_u *p;
9994 char_u *name;
9995 int n = FALSE;
9996 int len = 0;
9997
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009998 no_autoload = TRUE;
9999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 if (*p == '$') /* environment variable */
10002 {
10003 /* first try "normal" environment variables (fast) */
10004 if (mch_getenv(p + 1) != NULL)
10005 n = TRUE;
10006 else
10007 {
10008 /* try expanding things like $VIM and ${HOME} */
10009 p = expand_env_save(p);
10010 if (p != NULL && *p != '$')
10011 n = TRUE;
10012 vim_free(p);
10013 }
10014 }
10015 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010017 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010018 if (*skipwhite(p) != NUL)
10019 n = FALSE; /* trailing garbage */
10020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 else if (*p == '*') /* internal or user defined function */
10022 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010023 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 }
10025 else if (*p == ':')
10026 {
10027 n = cmd_exists(p + 1);
10028 }
10029 else if (*p == '#')
10030 {
10031#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010032 if (p[1] == '#')
10033 n = autocmd_supported(p + 2);
10034 else
10035 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036#endif
10037 }
10038 else /* internal variable */
10039 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010040 char_u *tofree;
10041 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010043 /* get_name_len() takes care of expanding curly braces */
10044 name = p;
10045 len = get_name_len(&p, &tofree, TRUE, FALSE);
10046 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010048 if (tofree != NULL)
10049 name = tofree;
10050 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10051 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010053 /* handle d.key, l[idx], f(expr) */
10054 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10055 if (n)
10056 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 }
10058 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010059 if (*p != NUL)
10060 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010062 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 }
10064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010065 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010066
10067 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068}
10069
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010070#ifdef FEAT_FLOAT
10071/*
10072 * "exp()" function
10073 */
10074 static void
10075f_exp(argvars, rettv)
10076 typval_T *argvars;
10077 typval_T *rettv;
10078{
10079 float_T f;
10080
10081 rettv->v_type = VAR_FLOAT;
10082 if (get_float_arg(argvars, &f) == OK)
10083 rettv->vval.v_float = exp(f);
10084 else
10085 rettv->vval.v_float = 0.0;
10086}
10087#endif
10088
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089/*
10090 * "expand()" function
10091 */
10092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010093f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010094 typval_T *argvars;
10095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096{
10097 char_u *s;
10098 int len;
10099 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010100 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010103 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010105 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010106 if (argvars[1].v_type != VAR_UNKNOWN
10107 && argvars[2].v_type != VAR_UNKNOWN
10108 && get_tv_number_chk(&argvars[2], &error)
10109 && !error)
10110 {
10111 rettv->v_type = VAR_LIST;
10112 rettv->vval.v_list = NULL;
10113 }
10114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 if (*s == '%' || *s == '#' || *s == '<')
10117 {
10118 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010119 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010121 if (rettv->v_type == VAR_LIST)
10122 {
10123 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10124 list_append_string(rettv->vval.v_list, result, -1);
10125 else
10126 vim_free(result);
10127 }
10128 else
10129 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 }
10131 else
10132 {
10133 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010134 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010135 if (argvars[1].v_type != VAR_UNKNOWN
10136 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010137 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010138 if (!error)
10139 {
10140 ExpandInit(&xpc);
10141 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010142 if (p_wic)
10143 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010144 if (rettv->v_type == VAR_STRING)
10145 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10146 options, WILD_ALL);
10147 else if (rettv_list_alloc(rettv) != FAIL)
10148 {
10149 int i;
10150
10151 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10152 for (i = 0; i < xpc.xp_numfiles; i++)
10153 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10154 ExpandCleanup(&xpc);
10155 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010156 }
10157 else
10158 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 }
10160}
10161
10162/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010163 * Go over all entries in "d2" and add them to "d1".
10164 * When "action" is "error" then a duplicate key is an error.
10165 * When "action" is "force" then a duplicate key is overwritten.
10166 * Otherwise duplicate keys are ignored ("action" is "keep").
10167 */
10168 void
10169dict_extend(d1, d2, action)
10170 dict_T *d1;
10171 dict_T *d2;
10172 char_u *action;
10173{
10174 dictitem_T *di1;
10175 hashitem_T *hi2;
10176 int todo;
10177
10178 todo = (int)d2->dv_hashtab.ht_used;
10179 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10180 {
10181 if (!HASHITEM_EMPTY(hi2))
10182 {
10183 --todo;
10184 di1 = dict_find(d1, hi2->hi_key, -1);
10185 if (d1->dv_scope != 0)
10186 {
10187 /* Disallow replacing a builtin function in l: and g:.
10188 * Check the key to be valid when adding to any
10189 * scope. */
10190 if (d1->dv_scope == VAR_DEF_SCOPE
10191 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10192 && var_check_func_name(hi2->hi_key,
10193 di1 == NULL))
10194 break;
10195 if (!valid_varname(hi2->hi_key))
10196 break;
10197 }
10198 if (di1 == NULL)
10199 {
10200 di1 = dictitem_copy(HI2DI(hi2));
10201 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10202 dictitem_free(di1);
10203 }
10204 else if (*action == 'e')
10205 {
10206 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10207 break;
10208 }
10209 else if (*action == 'f' && HI2DI(hi2) != di1)
10210 {
10211 clear_tv(&di1->di_tv);
10212 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10213 }
10214 }
10215 }
10216}
10217
10218/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010219 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010220 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010221 */
10222 static void
10223f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010224 typval_T *argvars;
10225 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010226{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010227 char *arg_errmsg = N_("extend() argument");
10228
Bram Moolenaare9a41262005-01-15 22:18:47 +000010229 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010230 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010231 list_T *l1, *l2;
10232 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010233 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010234 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010235
Bram Moolenaare9a41262005-01-15 22:18:47 +000010236 l1 = argvars[0].vval.v_list;
10237 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010238 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010239 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 {
10241 if (argvars[2].v_type != VAR_UNKNOWN)
10242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010243 before = get_tv_number_chk(&argvars[2], &error);
10244 if (error)
10245 return; /* type error; errmsg already given */
10246
Bram Moolenaar758711c2005-02-02 23:11:38 +000010247 if (before == l1->lv_len)
10248 item = NULL;
10249 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010250 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010251 item = list_find(l1, before);
10252 if (item == NULL)
10253 {
10254 EMSGN(_(e_listidx), before);
10255 return;
10256 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010257 }
10258 }
10259 else
10260 item = NULL;
10261 list_extend(l1, l2, item);
10262
Bram Moolenaare9a41262005-01-15 22:18:47 +000010263 copy_tv(&argvars[0], rettv);
10264 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010265 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010266 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10267 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010268 dict_T *d1, *d2;
10269 char_u *action;
10270 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010271
10272 d1 = argvars[0].vval.v_dict;
10273 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010274 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010275 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010276 {
10277 /* Check the third argument. */
10278 if (argvars[2].v_type != VAR_UNKNOWN)
10279 {
10280 static char *(av[]) = {"keep", "force", "error"};
10281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010282 action = get_tv_string_chk(&argvars[2]);
10283 if (action == NULL)
10284 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010285 for (i = 0; i < 3; ++i)
10286 if (STRCMP(action, av[i]) == 0)
10287 break;
10288 if (i == 3)
10289 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010290 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010291 return;
10292 }
10293 }
10294 else
10295 action = (char_u *)"force";
10296
Bram Moolenaara9922d62013-05-30 13:01:18 +020010297 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010298
Bram Moolenaare9a41262005-01-15 22:18:47 +000010299 copy_tv(&argvars[0], rettv);
10300 }
10301 }
10302 else
10303 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010304}
10305
10306/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010307 * "feedkeys()" function
10308 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010309 static void
10310f_feedkeys(argvars, rettv)
10311 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010312 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010313{
10314 int remap = TRUE;
10315 char_u *keys, *flags;
10316 char_u nbuf[NUMBUFLEN];
10317 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010318 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010319
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010320 /* This is not allowed in the sandbox. If the commands would still be
10321 * executed in the sandbox it would be OK, but it probably happens later,
10322 * when "sandbox" is no longer set. */
10323 if (check_secure())
10324 return;
10325
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010326 keys = get_tv_string(&argvars[0]);
10327 if (*keys != NUL)
10328 {
10329 if (argvars[1].v_type != VAR_UNKNOWN)
10330 {
10331 flags = get_tv_string_buf(&argvars[1], nbuf);
10332 for ( ; *flags != NUL; ++flags)
10333 {
10334 switch (*flags)
10335 {
10336 case 'n': remap = FALSE; break;
10337 case 'm': remap = TRUE; break;
10338 case 't': typed = TRUE; break;
10339 }
10340 }
10341 }
10342
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010343 /* Need to escape K_SPECIAL and CSI before putting the string in the
10344 * typeahead buffer. */
10345 keys_esc = vim_strsave_escape_csi(keys);
10346 if (keys_esc != NULL)
10347 {
10348 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010349 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010350 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010351 if (vgetc_busy)
10352 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010353 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010354 }
10355}
10356
10357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 * "filereadable()" function
10359 */
10360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010361f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010362 typval_T *argvars;
10363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010365 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 char_u *p;
10367 int n;
10368
Bram Moolenaarc236c162008-07-13 17:41:49 +000010369#ifndef O_NONBLOCK
10370# define O_NONBLOCK 0
10371#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010372 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010373 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10374 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 {
10376 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010377 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 }
10379 else
10380 n = FALSE;
10381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010382 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383}
10384
10385/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010386 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 * rights to write into.
10388 */
10389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010390f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010391 typval_T *argvars;
10392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010394 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395}
10396
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010397static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010398
10399 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010400findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010401 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010402 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010403 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010404{
10405#ifdef FEAT_SEARCHPATH
10406 char_u *fname;
10407 char_u *fresult = NULL;
10408 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10409 char_u *p;
10410 char_u pathbuf[NUMBUFLEN];
10411 int count = 1;
10412 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010413 int error = FALSE;
10414#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010415
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010416 rettv->vval.v_string = NULL;
10417 rettv->v_type = VAR_STRING;
10418
10419#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010420 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010421
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010422 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010424 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10425 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010426 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010427 else
10428 {
10429 if (*p != NUL)
10430 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010433 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010434 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010435 }
10436
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010437 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10438 error = TRUE;
10439
10440 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010442 do
10443 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010444 if (rettv->v_type == VAR_STRING)
10445 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010446 fresult = find_file_in_path_option(first ? fname : NULL,
10447 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010448 0, first, path,
10449 find_what,
10450 curbuf->b_ffname,
10451 find_what == FINDFILE_DIR
10452 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010453 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010454
10455 if (fresult != NULL && rettv->v_type == VAR_LIST)
10456 list_append_string(rettv->vval.v_list, fresult, -1);
10457
10458 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010459 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010460
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010461 if (rettv->v_type == VAR_STRING)
10462 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010463#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010464}
10465
Bram Moolenaar33570922005-01-25 22:26:29 +000010466static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10467static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010468
10469/*
10470 * Implementation of map() and filter().
10471 */
10472 static void
10473filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010474 typval_T *argvars;
10475 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010476 int map;
10477{
10478 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010479 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010480 listitem_T *li, *nli;
10481 list_T *l = NULL;
10482 dictitem_T *di;
10483 hashtab_T *ht;
10484 hashitem_T *hi;
10485 dict_T *d = NULL;
10486 typval_T save_val;
10487 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010488 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010489 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010490 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10491 char *arg_errmsg = (map ? N_("map() argument")
10492 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010493 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010494 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010495
Bram Moolenaare9a41262005-01-15 22:18:47 +000010496 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010497 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010498 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010499 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010500 return;
10501 }
10502 else if (argvars[0].v_type == VAR_DICT)
10503 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010504 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010505 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010506 return;
10507 }
10508 else
10509 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010510 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010511 return;
10512 }
10513
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010514 expr = get_tv_string_buf_chk(&argvars[1], buf);
10515 /* On type errors, the preceding call has already displayed an error
10516 * message. Avoid a misleading error message for an empty string that
10517 * was not passed as argument. */
10518 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010519 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010520 prepare_vimvar(VV_VAL, &save_val);
10521 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010522
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010523 /* We reset "did_emsg" to be able to detect whether an error
10524 * occurred during evaluation of the expression. */
10525 save_did_emsg = did_emsg;
10526 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010527
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010528 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010529 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010530 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010531 vimvars[VV_KEY].vv_type = VAR_STRING;
10532
10533 ht = &d->dv_hashtab;
10534 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010535 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010538 if (!HASHITEM_EMPTY(hi))
10539 {
10540 --todo;
10541 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010542 if (tv_check_lock(di->di_tv.v_lock,
10543 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544 break;
10545 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010546 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010547 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010548 break;
10549 if (!map && rem)
10550 dictitem_remove(d, di);
10551 clear_tv(&vimvars[VV_KEY].vv_tv);
10552 }
10553 }
10554 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010555 }
10556 else
10557 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010558 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10559
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010560 for (li = l->lv_first; li != NULL; li = nli)
10561 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010562 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010563 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010564 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010565 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010566 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010567 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010568 break;
10569 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010571 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010572 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010573 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010574
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010575 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010576 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010577
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010578 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010579 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580
10581 copy_tv(&argvars[0], rettv);
10582}
10583
10584 static int
10585filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010586 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587 char_u *expr;
10588 int map;
10589 int *remp;
10590{
Bram Moolenaar33570922005-01-25 22:26:29 +000010591 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010593 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010594
Bram Moolenaar33570922005-01-25 22:26:29 +000010595 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010596 s = expr;
10597 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010598 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010599 if (*s != NUL) /* check for trailing chars after expr */
10600 {
10601 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010602 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 }
10604 if (map)
10605 {
10606 /* map(): replace the list item value */
10607 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010608 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010609 *tv = rettv;
10610 }
10611 else
10612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010613 int error = FALSE;
10614
Bram Moolenaare9a41262005-01-15 22:18:47 +000010615 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010616 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010617 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010618 /* On type error, nothing has been removed; return FAIL to stop the
10619 * loop. The error message was given by get_tv_number_chk(). */
10620 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010621 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010622 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010623 retval = OK;
10624theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010625 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010626 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010627}
10628
10629/*
10630 * "filter()" function
10631 */
10632 static void
10633f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010634 typval_T *argvars;
10635 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010636{
10637 filter_map(argvars, rettv, FALSE);
10638}
10639
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010640/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010641 * "finddir({fname}[, {path}[, {count}]])" function
10642 */
10643 static void
10644f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010645 typval_T *argvars;
10646 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010647{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010648 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010649}
10650
10651/*
10652 * "findfile({fname}[, {path}[, {count}]])" function
10653 */
10654 static void
10655f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010656 typval_T *argvars;
10657 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010658{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010659 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010660}
10661
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010662#ifdef FEAT_FLOAT
10663/*
10664 * "float2nr({float})" function
10665 */
10666 static void
10667f_float2nr(argvars, rettv)
10668 typval_T *argvars;
10669 typval_T *rettv;
10670{
10671 float_T f;
10672
10673 if (get_float_arg(argvars, &f) == OK)
10674 {
10675 if (f < -0x7fffffff)
10676 rettv->vval.v_number = -0x7fffffff;
10677 else if (f > 0x7fffffff)
10678 rettv->vval.v_number = 0x7fffffff;
10679 else
10680 rettv->vval.v_number = (varnumber_T)f;
10681 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010682}
10683
10684/*
10685 * "floor({float})" function
10686 */
10687 static void
10688f_floor(argvars, rettv)
10689 typval_T *argvars;
10690 typval_T *rettv;
10691{
10692 float_T f;
10693
10694 rettv->v_type = VAR_FLOAT;
10695 if (get_float_arg(argvars, &f) == OK)
10696 rettv->vval.v_float = floor(f);
10697 else
10698 rettv->vval.v_float = 0.0;
10699}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010700
10701/*
10702 * "fmod()" function
10703 */
10704 static void
10705f_fmod(argvars, rettv)
10706 typval_T *argvars;
10707 typval_T *rettv;
10708{
10709 float_T fx, fy;
10710
10711 rettv->v_type = VAR_FLOAT;
10712 if (get_float_arg(argvars, &fx) == OK
10713 && get_float_arg(&argvars[1], &fy) == OK)
10714 rettv->vval.v_float = fmod(fx, fy);
10715 else
10716 rettv->vval.v_float = 0.0;
10717}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010718#endif
10719
Bram Moolenaar0d660222005-01-07 21:51:51 +000010720/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010721 * "fnameescape({string})" function
10722 */
10723 static void
10724f_fnameescape(argvars, rettv)
10725 typval_T *argvars;
10726 typval_T *rettv;
10727{
10728 rettv->vval.v_string = vim_strsave_fnameescape(
10729 get_tv_string(&argvars[0]), FALSE);
10730 rettv->v_type = VAR_STRING;
10731}
10732
10733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 * "fnamemodify({fname}, {mods})" function
10735 */
10736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010737f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010738 typval_T *argvars;
10739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740{
10741 char_u *fname;
10742 char_u *mods;
10743 int usedlen = 0;
10744 int len;
10745 char_u *fbuf = NULL;
10746 char_u buf[NUMBUFLEN];
10747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010748 fname = get_tv_string_chk(&argvars[0]);
10749 mods = get_tv_string_buf_chk(&argvars[1], buf);
10750 if (fname == NULL || mods == NULL)
10751 fname = NULL;
10752 else
10753 {
10754 len = (int)STRLEN(fname);
10755 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010758 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010760 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010762 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 vim_free(fbuf);
10764}
10765
Bram Moolenaar33570922005-01-25 22:26:29 +000010766static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767
10768/*
10769 * "foldclosed()" function
10770 */
10771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010772foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010773 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010774 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010775 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776{
10777#ifdef FEAT_FOLDING
10778 linenr_T lnum;
10779 linenr_T first, last;
10780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010781 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10783 {
10784 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10785 {
10786 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010787 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010789 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 return;
10791 }
10792 }
10793#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010794 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795}
10796
10797/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010798 * "foldclosed()" function
10799 */
10800 static void
10801f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010802 typval_T *argvars;
10803 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010804{
10805 foldclosed_both(argvars, rettv, FALSE);
10806}
10807
10808/*
10809 * "foldclosedend()" function
10810 */
10811 static void
10812f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010813 typval_T *argvars;
10814 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010815{
10816 foldclosed_both(argvars, rettv, TRUE);
10817}
10818
10819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 * "foldlevel()" function
10821 */
10822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010823f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010824 typval_T *argvars UNUSED;
10825 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826{
10827#ifdef FEAT_FOLDING
10828 linenr_T lnum;
10829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010830 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010832 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834}
10835
10836/*
10837 * "foldtext()" function
10838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010841 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843{
10844#ifdef FEAT_FOLDING
10845 linenr_T lnum;
10846 char_u *s;
10847 char_u *r;
10848 int len;
10849 char *txt;
10850#endif
10851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852 rettv->v_type = VAR_STRING;
10853 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010855 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10856 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10857 <= curbuf->b_ml.ml_line_count
10858 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 {
10860 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010861 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10862 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 {
10864 if (!linewhite(lnum))
10865 break;
10866 ++lnum;
10867 }
10868
10869 /* Find interesting text in this line. */
10870 s = skipwhite(ml_get(lnum));
10871 /* skip C comment-start */
10872 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010875 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010876 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010877 {
10878 s = skipwhite(ml_get(lnum + 1));
10879 if (*s == '*')
10880 s = skipwhite(s + 1);
10881 }
10882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 txt = _("+-%s%3ld lines: ");
10884 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010885 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 + 20 /* for %3ld */
10887 + STRLEN(s))); /* concatenated */
10888 if (r != NULL)
10889 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010890 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10891 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10892 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 len = (int)STRLEN(r);
10894 STRCAT(r, s);
10895 /* remove 'foldmarker' and 'commentstring' */
10896 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010897 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 }
10899 }
10900#endif
10901}
10902
10903/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010904 * "foldtextresult(lnum)" function
10905 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010907f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010908 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010909 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010910{
10911#ifdef FEAT_FOLDING
10912 linenr_T lnum;
10913 char_u *text;
10914 char_u buf[51];
10915 foldinfo_T foldinfo;
10916 int fold_count;
10917#endif
10918
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010919 rettv->v_type = VAR_STRING;
10920 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010921#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010923 /* treat illegal types and illegal string values for {lnum} the same */
10924 if (lnum < 0)
10925 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010926 fold_count = foldedCount(curwin, lnum, &foldinfo);
10927 if (fold_count > 0)
10928 {
10929 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10930 &foldinfo, buf);
10931 if (text == buf)
10932 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010933 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010934 }
10935#endif
10936}
10937
10938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 * "foreground()" function
10940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010942f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010943 typval_T *argvars UNUSED;
10944 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946#ifdef FEAT_GUI
10947 if (gui.in_use)
10948 gui_mch_set_foreground();
10949#else
10950# ifdef WIN32
10951 win32_set_foreground();
10952# endif
10953#endif
10954}
10955
10956/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010957 * "function()" function
10958 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010960f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010961 typval_T *argvars;
10962 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010963{
10964 char_u *s;
Bram Moolenaara1544c02013-05-30 12:35:52 +020010965 char_u *name = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010967 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010968 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010969 EMSG2(_(e_invarg2), s);
Bram Moolenaara1544c02013-05-30 12:35:52 +020010970 /* Don't check an autoload name for existence here, but still expand it
10971 * checking for validity */
10972 else if ((name = get_expanded_name(s, vim_strchr(s, AUTOLOAD_CHAR) == NULL))
10973 == NULL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010974 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010975 else
10976 {
Bram Moolenaara1544c02013-05-30 12:35:52 +020010977 if (name == NULL)
10978 /* Autoload function, need to copy string */
10979 rettv->vval.v_string = vim_strsave(s);
10980 else
10981 /* Function found by get_expanded_name, string allocated by
10982 * trans_function_name: no need to copy */
10983 rettv->vval.v_string = name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010984 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010985 }
10986}
10987
10988/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010989 * "garbagecollect()" function
10990 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010991 static void
10992f_garbagecollect(argvars, rettv)
10993 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010994 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010995{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010996 /* This is postponed until we are back at the toplevel, because we may be
10997 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10998 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010999
11000 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11001 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011002}
11003
11004/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011005 * "get()" function
11006 */
11007 static void
11008f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011009 typval_T *argvars;
11010 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011011{
Bram Moolenaar33570922005-01-25 22:26:29 +000011012 listitem_T *li;
11013 list_T *l;
11014 dictitem_T *di;
11015 dict_T *d;
11016 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011017
Bram Moolenaare9a41262005-01-15 22:18:47 +000011018 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011019 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011020 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011021 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011022 int error = FALSE;
11023
11024 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11025 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011026 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011027 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011028 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011029 else if (argvars[0].v_type == VAR_DICT)
11030 {
11031 if ((d = argvars[0].vval.v_dict) != NULL)
11032 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011033 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011034 if (di != NULL)
11035 tv = &di->di_tv;
11036 }
11037 }
11038 else
11039 EMSG2(_(e_listdictarg), "get()");
11040
11041 if (tv == NULL)
11042 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011043 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011044 copy_tv(&argvars[2], rettv);
11045 }
11046 else
11047 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011048}
11049
Bram Moolenaar342337a2005-07-21 21:11:17 +000011050static 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 +000011051
11052/*
11053 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011054 * Return a range (from start to end) of lines in rettv from the specified
11055 * buffer.
11056 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011057 */
11058 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011059get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011060 buf_T *buf;
11061 linenr_T start;
11062 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011063 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011064 typval_T *rettv;
11065{
11066 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011067
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011068 if (retlist && rettv_list_alloc(rettv) == FAIL)
11069 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011070
11071 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11072 return;
11073
11074 if (!retlist)
11075 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011076 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11077 p = ml_get_buf(buf, start, FALSE);
11078 else
11079 p = (char_u *)"";
11080
11081 rettv->v_type = VAR_STRING;
11082 rettv->vval.v_string = vim_strsave(p);
11083 }
11084 else
11085 {
11086 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011087 return;
11088
11089 if (start < 1)
11090 start = 1;
11091 if (end > buf->b_ml.ml_line_count)
11092 end = buf->b_ml.ml_line_count;
11093 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011094 if (list_append_string(rettv->vval.v_list,
11095 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011096 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011097 }
11098}
11099
11100/*
11101 * "getbufline()" function
11102 */
11103 static void
11104f_getbufline(argvars, rettv)
11105 typval_T *argvars;
11106 typval_T *rettv;
11107{
11108 linenr_T lnum;
11109 linenr_T end;
11110 buf_T *buf;
11111
11112 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11113 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011114 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011115 --emsg_off;
11116
Bram Moolenaar661b1822005-07-28 22:36:45 +000011117 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011118 if (argvars[2].v_type == VAR_UNKNOWN)
11119 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011120 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011121 end = get_tv_lnum_buf(&argvars[2], buf);
11122
Bram Moolenaar342337a2005-07-21 21:11:17 +000011123 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011124}
11125
Bram Moolenaar0d660222005-01-07 21:51:51 +000011126/*
11127 * "getbufvar()" function
11128 */
11129 static void
11130f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011131 typval_T *argvars;
11132 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011133{
11134 buf_T *buf;
11135 buf_T *save_curbuf;
11136 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011137 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011138 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011139
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011140 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11141 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011142 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011143 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011144
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011145 rettv->v_type = VAR_STRING;
11146 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011147
11148 if (buf != NULL && varname != NULL)
11149 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011150 /* set curbuf to be our buf, temporarily */
11151 save_curbuf = curbuf;
11152 curbuf = buf;
11153
Bram Moolenaar0d660222005-01-07 21:51:51 +000011154 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011155 {
11156 if (get_option_tv(&varname, rettv, TRUE) == OK)
11157 done = TRUE;
11158 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011159 else if (STRCMP(varname, "changedtick") == 0)
11160 {
11161 rettv->v_type = VAR_NUMBER;
11162 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011163 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011164 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011165 else
11166 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011167 /* Look up the variable. */
11168 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11169 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11170 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011171 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011172 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011173 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011174 done = TRUE;
11175 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011176 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011177
11178 /* restore previous notion of curbuf */
11179 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011180 }
11181
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011182 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11183 /* use the default value */
11184 copy_tv(&argvars[2], rettv);
11185
Bram Moolenaar0d660222005-01-07 21:51:51 +000011186 --emsg_off;
11187}
11188
11189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190 * "getchar()" function
11191 */
11192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011193f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011194 typval_T *argvars;
11195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196{
11197 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011198 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011200 /* Position the cursor. Needed after a message that ends in a space. */
11201 windgoto(msg_row, msg_col);
11202
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 ++no_mapping;
11204 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011205 for (;;)
11206 {
11207 if (argvars[0].v_type == VAR_UNKNOWN)
11208 /* getchar(): blocking wait. */
11209 n = safe_vgetc();
11210 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11211 /* getchar(1): only check if char avail */
11212 n = vpeekc();
11213 else if (error || vpeekc() == NUL)
11214 /* illegal argument or getchar(0) and no char avail: return zero */
11215 n = 0;
11216 else
11217 /* getchar(0) and char avail: return char */
11218 n = safe_vgetc();
11219 if (n == K_IGNORE)
11220 continue;
11221 break;
11222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 --no_mapping;
11224 --allow_keys;
11225
Bram Moolenaar219b8702006-11-01 14:32:36 +000011226 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11227 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11228 vimvars[VV_MOUSE_COL].vv_nr = 0;
11229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011230 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 if (IS_SPECIAL(n) || mod_mask != 0)
11232 {
11233 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11234 int i = 0;
11235
11236 /* Turn a special key into three bytes, plus modifier. */
11237 if (mod_mask != 0)
11238 {
11239 temp[i++] = K_SPECIAL;
11240 temp[i++] = KS_MODIFIER;
11241 temp[i++] = mod_mask;
11242 }
11243 if (IS_SPECIAL(n))
11244 {
11245 temp[i++] = K_SPECIAL;
11246 temp[i++] = K_SECOND(n);
11247 temp[i++] = K_THIRD(n);
11248 }
11249#ifdef FEAT_MBYTE
11250 else if (has_mbyte)
11251 i += (*mb_char2bytes)(n, temp + i);
11252#endif
11253 else
11254 temp[i++] = n;
11255 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011256 rettv->v_type = VAR_STRING;
11257 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011258
11259#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011260 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011261 {
11262 int row = mouse_row;
11263 int col = mouse_col;
11264 win_T *win;
11265 linenr_T lnum;
11266# ifdef FEAT_WINDOWS
11267 win_T *wp;
11268# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011269 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011270
11271 if (row >= 0 && col >= 0)
11272 {
11273 /* Find the window at the mouse coordinates and compute the
11274 * text position. */
11275 win = mouse_find_win(&row, &col);
11276 (void)mouse_comp_pos(win, &row, &col, &lnum);
11277# ifdef FEAT_WINDOWS
11278 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011279 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011280# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011281 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011282 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11283 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11284 }
11285 }
11286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 }
11288}
11289
11290/*
11291 * "getcharmod()" function
11292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011295 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299}
11300
11301/*
11302 * "getcmdline()" function
11303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011306 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 rettv->v_type = VAR_STRING;
11310 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311}
11312
11313/*
11314 * "getcmdpos()" function
11315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011317f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011318 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322}
11323
11324/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011325 * "getcmdtype()" function
11326 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011327 static void
11328f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011329 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011330 typval_T *rettv;
11331{
11332 rettv->v_type = VAR_STRING;
11333 rettv->vval.v_string = alloc(2);
11334 if (rettv->vval.v_string != NULL)
11335 {
11336 rettv->vval.v_string[0] = get_cmdline_type();
11337 rettv->vval.v_string[1] = NUL;
11338 }
11339}
11340
11341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 * "getcwd()" function
11343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011346 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011349 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011352 rettv->vval.v_string = NULL;
11353 cwd = alloc(MAXPATHL);
11354 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011356 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11357 {
11358 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011360 if (rettv->vval.v_string != NULL)
11361 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011363 }
11364 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 }
11366}
11367
11368/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011369 * "getfontname()" function
11370 */
11371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011373 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011374 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011375{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 rettv->v_type = VAR_STRING;
11377 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011378#ifdef FEAT_GUI
11379 if (gui.in_use)
11380 {
11381 GuiFont font;
11382 char_u *name = NULL;
11383
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011384 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011385 {
11386 /* Get the "Normal" font. Either the name saved by
11387 * hl_set_font_name() or from the font ID. */
11388 font = gui.norm_font;
11389 name = hl_get_font_name();
11390 }
11391 else
11392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011393 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011394 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11395 return;
11396 font = gui_mch_get_font(name, FALSE);
11397 if (font == NOFONT)
11398 return; /* Invalid font name, return empty string. */
11399 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011401 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011402 gui_mch_free_font(font);
11403 }
11404#endif
11405}
11406
11407/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011408 * "getfperm({fname})" function
11409 */
11410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011412 typval_T *argvars;
11413 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011414{
11415 char_u *fname;
11416 struct stat st;
11417 char_u *perm = NULL;
11418 char_u flags[] = "rwx";
11419 int i;
11420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011423 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011424 if (mch_stat((char *)fname, &st) >= 0)
11425 {
11426 perm = vim_strsave((char_u *)"---------");
11427 if (perm != NULL)
11428 {
11429 for (i = 0; i < 9; i++)
11430 {
11431 if (st.st_mode & (1 << (8 - i)))
11432 perm[i] = flags[i % 3];
11433 }
11434 }
11435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011437}
11438
11439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440 * "getfsize({fname})" function
11441 */
11442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011444 typval_T *argvars;
11445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446{
11447 char_u *fname;
11448 struct stat st;
11449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011450 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011452 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453
11454 if (mch_stat((char *)fname, &st) >= 0)
11455 {
11456 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011461
11462 /* non-perfect check for overflow */
11463 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11464 rettv->vval.v_number = -2;
11465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 }
11467 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469}
11470
11471/*
11472 * "getftime({fname})" function
11473 */
11474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011476 typval_T *argvars;
11477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478{
11479 char_u *fname;
11480 struct stat st;
11481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483
11484 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488}
11489
11490/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011491 * "getftype({fname})" function
11492 */
11493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011495 typval_T *argvars;
11496 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011497{
11498 char_u *fname;
11499 struct stat st;
11500 char_u *type = NULL;
11501 char *t;
11502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011505 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011506 if (mch_lstat((char *)fname, &st) >= 0)
11507 {
11508#ifdef S_ISREG
11509 if (S_ISREG(st.st_mode))
11510 t = "file";
11511 else if (S_ISDIR(st.st_mode))
11512 t = "dir";
11513# ifdef S_ISLNK
11514 else if (S_ISLNK(st.st_mode))
11515 t = "link";
11516# endif
11517# ifdef S_ISBLK
11518 else if (S_ISBLK(st.st_mode))
11519 t = "bdev";
11520# endif
11521# ifdef S_ISCHR
11522 else if (S_ISCHR(st.st_mode))
11523 t = "cdev";
11524# endif
11525# ifdef S_ISFIFO
11526 else if (S_ISFIFO(st.st_mode))
11527 t = "fifo";
11528# endif
11529# ifdef S_ISSOCK
11530 else if (S_ISSOCK(st.st_mode))
11531 t = "fifo";
11532# endif
11533 else
11534 t = "other";
11535#else
11536# ifdef S_IFMT
11537 switch (st.st_mode & S_IFMT)
11538 {
11539 case S_IFREG: t = "file"; break;
11540 case S_IFDIR: t = "dir"; break;
11541# ifdef S_IFLNK
11542 case S_IFLNK: t = "link"; break;
11543# endif
11544# ifdef S_IFBLK
11545 case S_IFBLK: t = "bdev"; break;
11546# endif
11547# ifdef S_IFCHR
11548 case S_IFCHR: t = "cdev"; break;
11549# endif
11550# ifdef S_IFIFO
11551 case S_IFIFO: t = "fifo"; break;
11552# endif
11553# ifdef S_IFSOCK
11554 case S_IFSOCK: t = "socket"; break;
11555# endif
11556 default: t = "other";
11557 }
11558# else
11559 if (mch_isdir(fname))
11560 t = "dir";
11561 else
11562 t = "file";
11563# endif
11564#endif
11565 type = vim_strsave((char_u *)t);
11566 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011567 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011568}
11569
11570/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011571 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011572 */
11573 static void
11574f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011575 typval_T *argvars;
11576 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011577{
11578 linenr_T lnum;
11579 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011580 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011581
11582 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011583 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011584 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011585 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011586 retlist = FALSE;
11587 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011588 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011589 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011590 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011591 retlist = TRUE;
11592 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011593
Bram Moolenaar342337a2005-07-21 21:11:17 +000011594 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011595}
11596
11597/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011598 * "getmatches()" function
11599 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011600 static void
11601f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011602 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011603 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011604{
11605#ifdef FEAT_SEARCH_EXTRA
11606 dict_T *dict;
11607 matchitem_T *cur = curwin->w_match_head;
11608
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011609 if (rettv_list_alloc(rettv) == OK)
11610 {
11611 while (cur != NULL)
11612 {
11613 dict = dict_alloc();
11614 if (dict == NULL)
11615 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011616 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11617 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11618 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11619 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11620 list_append_dict(rettv->vval.v_list, dict);
11621 cur = cur->next;
11622 }
11623 }
11624#endif
11625}
11626
11627/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011628 * "getpid()" function
11629 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011630 static void
11631f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011632 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011633 typval_T *rettv;
11634{
11635 rettv->vval.v_number = mch_get_pid();
11636}
11637
11638/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011639 * "getpos(string)" function
11640 */
11641 static void
11642f_getpos(argvars, rettv)
11643 typval_T *argvars;
11644 typval_T *rettv;
11645{
11646 pos_T *fp;
11647 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011648 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011649
11650 if (rettv_list_alloc(rettv) == OK)
11651 {
11652 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011653 fp = var2fpos(&argvars[0], TRUE, &fnum);
11654 if (fnum != -1)
11655 list_append_number(l, (varnumber_T)fnum);
11656 else
11657 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011658 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11659 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011660 list_append_number(l, (fp != NULL)
11661 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011662 : (varnumber_T)0);
11663 list_append_number(l,
11664#ifdef FEAT_VIRTUALEDIT
11665 (fp != NULL) ? (varnumber_T)fp->coladd :
11666#endif
11667 (varnumber_T)0);
11668 }
11669 else
11670 rettv->vval.v_number = FALSE;
11671}
11672
11673/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011674 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011675 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011676 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011677f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011678 typval_T *argvars UNUSED;
11679 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011680{
11681#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011682 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011683#endif
11684
Bram Moolenaar2641f772005-03-25 21:58:17 +000011685#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011686 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011687 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011688 wp = NULL;
11689 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11690 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011691 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011692 if (wp == NULL)
11693 return;
11694 }
11695
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011696 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011697 }
11698#endif
11699}
11700
11701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 * "getreg()" function
11703 */
11704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011705f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011706 typval_T *argvars;
11707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708{
11709 char_u *strregname;
11710 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011711 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011712 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011714 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011716 strregname = get_tv_string_chk(&argvars[0]);
11717 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011718 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011719 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011722 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723 regname = (strregname == NULL ? '"' : *strregname);
11724 if (regname == 0)
11725 regname = '"';
11726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011727 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011728 rettv->vval.v_string = error ? NULL :
11729 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730}
11731
11732/*
11733 * "getregtype()" function
11734 */
11735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011736f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011737 typval_T *argvars;
11738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739{
11740 char_u *strregname;
11741 int regname;
11742 char_u buf[NUMBUFLEN + 2];
11743 long reglen = 0;
11744
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011745 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011746 {
11747 strregname = get_tv_string_chk(&argvars[0]);
11748 if (strregname == NULL) /* type error; errmsg already given */
11749 {
11750 rettv->v_type = VAR_STRING;
11751 rettv->vval.v_string = NULL;
11752 return;
11753 }
11754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755 else
11756 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011757 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758
11759 regname = (strregname == NULL ? '"' : *strregname);
11760 if (regname == 0)
11761 regname = '"';
11762
11763 buf[0] = NUL;
11764 buf[1] = NUL;
11765 switch (get_reg_type(regname, &reglen))
11766 {
11767 case MLINE: buf[0] = 'V'; break;
11768 case MCHAR: buf[0] = 'v'; break;
11769#ifdef FEAT_VISUAL
11770 case MBLOCK:
11771 buf[0] = Ctrl_V;
11772 sprintf((char *)buf + 1, "%ld", reglen + 1);
11773 break;
11774#endif
11775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776 rettv->v_type = VAR_STRING;
11777 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778}
11779
11780/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011781 * "gettabvar()" function
11782 */
11783 static void
11784f_gettabvar(argvars, rettv)
11785 typval_T *argvars;
11786 typval_T *rettv;
11787{
11788 tabpage_T *tp;
11789 dictitem_T *v;
11790 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011791 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011792
11793 rettv->v_type = VAR_STRING;
11794 rettv->vval.v_string = NULL;
11795
11796 varname = get_tv_string_chk(&argvars[1]);
11797 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11798 if (tp != NULL && varname != NULL)
11799 {
11800 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011801 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011802 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011803 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011804 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011805 done = TRUE;
11806 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011807 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011808
11809 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011810 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011811}
11812
11813/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011814 * "gettabwinvar()" function
11815 */
11816 static void
11817f_gettabwinvar(argvars, rettv)
11818 typval_T *argvars;
11819 typval_T *rettv;
11820{
11821 getwinvar(argvars, rettv, 1);
11822}
11823
11824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 * "getwinposx()" function
11826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011829 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011832 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833#ifdef FEAT_GUI
11834 if (gui.in_use)
11835 {
11836 int x, y;
11837
11838 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011839 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 }
11841#endif
11842}
11843
11844/*
11845 * "getwinposy()" function
11846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011848f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011849 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853#ifdef FEAT_GUI
11854 if (gui.in_use)
11855 {
11856 int x, y;
11857
11858 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011859 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 }
11861#endif
11862}
11863
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011864/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011865 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011866 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011867 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011868find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011869 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011870 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011871{
11872#ifdef FEAT_WINDOWS
11873 win_T *wp;
11874#endif
11875 int nr;
11876
11877 nr = get_tv_number_chk(vp, NULL);
11878
11879#ifdef FEAT_WINDOWS
11880 if (nr < 0)
11881 return NULL;
11882 if (nr == 0)
11883 return curwin;
11884
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011885 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11886 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011887 if (--nr <= 0)
11888 break;
11889 return wp;
11890#else
11891 if (nr == 0 || nr == 1)
11892 return curwin;
11893 return NULL;
11894#endif
11895}
11896
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897/*
11898 * "getwinvar()" function
11899 */
11900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011901f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011902 typval_T *argvars;
11903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011905 getwinvar(argvars, rettv, 0);
11906}
11907
11908/*
11909 * getwinvar() and gettabwinvar()
11910 */
11911 static void
11912getwinvar(argvars, rettv, off)
11913 typval_T *argvars;
11914 typval_T *rettv;
11915 int off; /* 1 for gettabwinvar() */
11916{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917 win_T *win, *oldcurwin;
11918 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011919 dictitem_T *v;
Bram Moolenaar105bc352013-05-17 16:03:57 +020011920 tabpage_T *tp, *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011921 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011923#ifdef FEAT_WINDOWS
11924 if (off == 1)
11925 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11926 else
11927 tp = curtab;
11928#endif
11929 win = find_win_by_nr(&argvars[off], tp);
11930 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011931 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011933 rettv->v_type = VAR_STRING;
11934 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935
11936 if (win != NULL && varname != NULL)
11937 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011938 /* Set curwin to be our win, temporarily. Also set the tabpage,
11939 * otherwise the window is not valid. */
11940 switch_win(&oldcurwin, &oldtabpage, win, tp);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011941
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011943 {
11944 if (get_option_tv(&varname, rettv, 1) == OK)
11945 done = TRUE;
11946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947 else
11948 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011949 /* Look up the variable. */
11950 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11951 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011953 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011954 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011955 done = TRUE;
11956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011958
11959 /* restore previous notion of curwin */
Bram Moolenaar105bc352013-05-17 16:03:57 +020011960 restore_win(oldcurwin, oldtabpage);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961 }
11962
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011963 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11964 /* use the default return value */
11965 copy_tv(&argvars[off + 2], rettv);
11966
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 --emsg_off;
11968}
11969
11970/*
11971 * "glob()" function
11972 */
11973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011975 typval_T *argvars;
11976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011978 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011980 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011982 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011983 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011985 if (argvars[1].v_type != VAR_UNKNOWN)
11986 {
11987 if (get_tv_number_chk(&argvars[1], &error))
11988 options |= WILD_KEEP_ALL;
11989 if (argvars[2].v_type != VAR_UNKNOWN
11990 && get_tv_number_chk(&argvars[2], &error))
11991 {
11992 rettv->v_type = VAR_LIST;
11993 rettv->vval.v_list = NULL;
11994 }
11995 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011996 if (!error)
11997 {
11998 ExpandInit(&xpc);
11999 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012000 if (p_wic)
12001 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012002 if (rettv->v_type == VAR_STRING)
12003 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012004 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012005 else if (rettv_list_alloc(rettv) != FAIL)
12006 {
12007 int i;
12008
12009 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12010 NULL, options, WILD_ALL_KEEP);
12011 for (i = 0; i < xpc.xp_numfiles; i++)
12012 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12013
12014 ExpandCleanup(&xpc);
12015 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012016 }
12017 else
12018 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019}
12020
12021/*
12022 * "globpath()" function
12023 */
12024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012025f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012026 typval_T *argvars;
12027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012029 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012031 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012032 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012034 /* When the optional second argument is non-zero, don't remove matches
12035 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12036 if (argvars[2].v_type != VAR_UNKNOWN
12037 && get_tv_number_chk(&argvars[2], &error))
12038 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012039 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012040 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012041 rettv->vval.v_string = NULL;
12042 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012043 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12044 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045}
12046
12047/*
12048 * "has()" function
12049 */
12050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012051f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012052 typval_T *argvars;
12053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054{
12055 int i;
12056 char_u *name;
12057 int n = FALSE;
12058 static char *(has_list[]) =
12059 {
12060#ifdef AMIGA
12061 "amiga",
12062# ifdef FEAT_ARP
12063 "arp",
12064# endif
12065#endif
12066#ifdef __BEOS__
12067 "beos",
12068#endif
12069#ifdef MSDOS
12070# ifdef DJGPP
12071 "dos32",
12072# else
12073 "dos16",
12074# endif
12075#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012076#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 "mac",
12078#endif
12079#if defined(MACOS_X_UNIX)
12080 "macunix",
12081#endif
12082#ifdef OS2
12083 "os2",
12084#endif
12085#ifdef __QNX__
12086 "qnx",
12087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088#ifdef UNIX
12089 "unix",
12090#endif
12091#ifdef VMS
12092 "vms",
12093#endif
12094#ifdef WIN16
12095 "win16",
12096#endif
12097#ifdef WIN32
12098 "win32",
12099#endif
12100#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12101 "win32unix",
12102#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012103#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104 "win64",
12105#endif
12106#ifdef EBCDIC
12107 "ebcdic",
12108#endif
12109#ifndef CASE_INSENSITIVE_FILENAME
12110 "fname_case",
12111#endif
12112#ifdef FEAT_ARABIC
12113 "arabic",
12114#endif
12115#ifdef FEAT_AUTOCMD
12116 "autocmd",
12117#endif
12118#ifdef FEAT_BEVAL
12119 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012120# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12121 "balloon_multiline",
12122# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123#endif
12124#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12125 "builtin_terms",
12126# ifdef ALL_BUILTIN_TCAPS
12127 "all_builtin_terms",
12128# endif
12129#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012130#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12131 || defined(FEAT_GUI_W32) \
12132 || defined(FEAT_GUI_MOTIF))
12133 "browsefilter",
12134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135#ifdef FEAT_BYTEOFF
12136 "byte_offset",
12137#endif
12138#ifdef FEAT_CINDENT
12139 "cindent",
12140#endif
12141#ifdef FEAT_CLIENTSERVER
12142 "clientserver",
12143#endif
12144#ifdef FEAT_CLIPBOARD
12145 "clipboard",
12146#endif
12147#ifdef FEAT_CMDL_COMPL
12148 "cmdline_compl",
12149#endif
12150#ifdef FEAT_CMDHIST
12151 "cmdline_hist",
12152#endif
12153#ifdef FEAT_COMMENTS
12154 "comments",
12155#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012156#ifdef FEAT_CONCEAL
12157 "conceal",
12158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159#ifdef FEAT_CRYPT
12160 "cryptv",
12161#endif
12162#ifdef FEAT_CSCOPE
12163 "cscope",
12164#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012165#ifdef FEAT_CURSORBIND
12166 "cursorbind",
12167#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012168#ifdef CURSOR_SHAPE
12169 "cursorshape",
12170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171#ifdef DEBUG
12172 "debug",
12173#endif
12174#ifdef FEAT_CON_DIALOG
12175 "dialog_con",
12176#endif
12177#ifdef FEAT_GUI_DIALOG
12178 "dialog_gui",
12179#endif
12180#ifdef FEAT_DIFF
12181 "diff",
12182#endif
12183#ifdef FEAT_DIGRAPHS
12184 "digraphs",
12185#endif
12186#ifdef FEAT_DND
12187 "dnd",
12188#endif
12189#ifdef FEAT_EMACS_TAGS
12190 "emacs_tags",
12191#endif
12192 "eval", /* always present, of course! */
12193#ifdef FEAT_EX_EXTRA
12194 "ex_extra",
12195#endif
12196#ifdef FEAT_SEARCH_EXTRA
12197 "extra_search",
12198#endif
12199#ifdef FEAT_FKMAP
12200 "farsi",
12201#endif
12202#ifdef FEAT_SEARCHPATH
12203 "file_in_path",
12204#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012205#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012206 "filterpipe",
12207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208#ifdef FEAT_FIND_ID
12209 "find_in_path",
12210#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012211#ifdef FEAT_FLOAT
12212 "float",
12213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214#ifdef FEAT_FOLDING
12215 "folding",
12216#endif
12217#ifdef FEAT_FOOTER
12218 "footer",
12219#endif
12220#if !defined(USE_SYSTEM) && defined(UNIX)
12221 "fork",
12222#endif
12223#ifdef FEAT_GETTEXT
12224 "gettext",
12225#endif
12226#ifdef FEAT_GUI
12227 "gui",
12228#endif
12229#ifdef FEAT_GUI_ATHENA
12230# ifdef FEAT_GUI_NEXTAW
12231 "gui_neXtaw",
12232# else
12233 "gui_athena",
12234# endif
12235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236#ifdef FEAT_GUI_GTK
12237 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012240#ifdef FEAT_GUI_GNOME
12241 "gui_gnome",
12242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243#ifdef FEAT_GUI_MAC
12244 "gui_mac",
12245#endif
12246#ifdef FEAT_GUI_MOTIF
12247 "gui_motif",
12248#endif
12249#ifdef FEAT_GUI_PHOTON
12250 "gui_photon",
12251#endif
12252#ifdef FEAT_GUI_W16
12253 "gui_win16",
12254#endif
12255#ifdef FEAT_GUI_W32
12256 "gui_win32",
12257#endif
12258#ifdef FEAT_HANGULIN
12259 "hangul_input",
12260#endif
12261#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12262 "iconv",
12263#endif
12264#ifdef FEAT_INS_EXPAND
12265 "insert_expand",
12266#endif
12267#ifdef FEAT_JUMPLIST
12268 "jumplist",
12269#endif
12270#ifdef FEAT_KEYMAP
12271 "keymap",
12272#endif
12273#ifdef FEAT_LANGMAP
12274 "langmap",
12275#endif
12276#ifdef FEAT_LIBCALL
12277 "libcall",
12278#endif
12279#ifdef FEAT_LINEBREAK
12280 "linebreak",
12281#endif
12282#ifdef FEAT_LISP
12283 "lispindent",
12284#endif
12285#ifdef FEAT_LISTCMDS
12286 "listcmds",
12287#endif
12288#ifdef FEAT_LOCALMAP
12289 "localmap",
12290#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012291#ifdef FEAT_LUA
12292# ifndef DYNAMIC_LUA
12293 "lua",
12294# endif
12295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296#ifdef FEAT_MENU
12297 "menu",
12298#endif
12299#ifdef FEAT_SESSION
12300 "mksession",
12301#endif
12302#ifdef FEAT_MODIFY_FNAME
12303 "modify_fname",
12304#endif
12305#ifdef FEAT_MOUSE
12306 "mouse",
12307#endif
12308#ifdef FEAT_MOUSESHAPE
12309 "mouseshape",
12310#endif
12311#if defined(UNIX) || defined(VMS)
12312# ifdef FEAT_MOUSE_DEC
12313 "mouse_dec",
12314# endif
12315# ifdef FEAT_MOUSE_GPM
12316 "mouse_gpm",
12317# endif
12318# ifdef FEAT_MOUSE_JSB
12319 "mouse_jsbterm",
12320# endif
12321# ifdef FEAT_MOUSE_NET
12322 "mouse_netterm",
12323# endif
12324# ifdef FEAT_MOUSE_PTERM
12325 "mouse_pterm",
12326# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012327# ifdef FEAT_MOUSE_SGR
12328 "mouse_sgr",
12329# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012330# ifdef FEAT_SYSMOUSE
12331 "mouse_sysmouse",
12332# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012333# ifdef FEAT_MOUSE_URXVT
12334 "mouse_urxvt",
12335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336# ifdef FEAT_MOUSE_XTERM
12337 "mouse_xterm",
12338# endif
12339#endif
12340#ifdef FEAT_MBYTE
12341 "multi_byte",
12342#endif
12343#ifdef FEAT_MBYTE_IME
12344 "multi_byte_ime",
12345#endif
12346#ifdef FEAT_MULTI_LANG
12347 "multi_lang",
12348#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012349#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012350#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012351 "mzscheme",
12352#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354#ifdef FEAT_OLE
12355 "ole",
12356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357#ifdef FEAT_PATH_EXTRA
12358 "path_extra",
12359#endif
12360#ifdef FEAT_PERL
12361#ifndef DYNAMIC_PERL
12362 "perl",
12363#endif
12364#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012365#ifdef FEAT_PERSISTENT_UNDO
12366 "persistent_undo",
12367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368#ifdef FEAT_PYTHON
12369#ifndef DYNAMIC_PYTHON
12370 "python",
12371#endif
12372#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012373#ifdef FEAT_PYTHON3
12374#ifndef DYNAMIC_PYTHON3
12375 "python3",
12376#endif
12377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378#ifdef FEAT_POSTSCRIPT
12379 "postscript",
12380#endif
12381#ifdef FEAT_PRINTER
12382 "printer",
12383#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012384#ifdef FEAT_PROFILE
12385 "profile",
12386#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012387#ifdef FEAT_RELTIME
12388 "reltime",
12389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390#ifdef FEAT_QUICKFIX
12391 "quickfix",
12392#endif
12393#ifdef FEAT_RIGHTLEFT
12394 "rightleft",
12395#endif
12396#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12397 "ruby",
12398#endif
12399#ifdef FEAT_SCROLLBIND
12400 "scrollbind",
12401#endif
12402#ifdef FEAT_CMDL_INFO
12403 "showcmd",
12404 "cmdline_info",
12405#endif
12406#ifdef FEAT_SIGNS
12407 "signs",
12408#endif
12409#ifdef FEAT_SMARTINDENT
12410 "smartindent",
12411#endif
12412#ifdef FEAT_SNIFF
12413 "sniff",
12414#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012415#ifdef STARTUPTIME
12416 "startuptime",
12417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418#ifdef FEAT_STL_OPT
12419 "statusline",
12420#endif
12421#ifdef FEAT_SUN_WORKSHOP
12422 "sun_workshop",
12423#endif
12424#ifdef FEAT_NETBEANS_INTG
12425 "netbeans_intg",
12426#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012427#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012428 "spell",
12429#endif
12430#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431 "syntax",
12432#endif
12433#if defined(USE_SYSTEM) || !defined(UNIX)
12434 "system",
12435#endif
12436#ifdef FEAT_TAG_BINS
12437 "tag_binary",
12438#endif
12439#ifdef FEAT_TAG_OLDSTATIC
12440 "tag_old_static",
12441#endif
12442#ifdef FEAT_TAG_ANYWHITE
12443 "tag_any_white",
12444#endif
12445#ifdef FEAT_TCL
12446# ifndef DYNAMIC_TCL
12447 "tcl",
12448# endif
12449#endif
12450#ifdef TERMINFO
12451 "terminfo",
12452#endif
12453#ifdef FEAT_TERMRESPONSE
12454 "termresponse",
12455#endif
12456#ifdef FEAT_TEXTOBJ
12457 "textobjects",
12458#endif
12459#ifdef HAVE_TGETENT
12460 "tgetent",
12461#endif
12462#ifdef FEAT_TITLE
12463 "title",
12464#endif
12465#ifdef FEAT_TOOLBAR
12466 "toolbar",
12467#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012468#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12469 "unnamedplus",
12470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471#ifdef FEAT_USR_CMDS
12472 "user-commands", /* was accidentally included in 5.4 */
12473 "user_commands",
12474#endif
12475#ifdef FEAT_VIMINFO
12476 "viminfo",
12477#endif
12478#ifdef FEAT_VERTSPLIT
12479 "vertsplit",
12480#endif
12481#ifdef FEAT_VIRTUALEDIT
12482 "virtualedit",
12483#endif
12484#ifdef FEAT_VISUAL
12485 "visual",
12486#endif
12487#ifdef FEAT_VISUALEXTRA
12488 "visualextra",
12489#endif
12490#ifdef FEAT_VREPLACE
12491 "vreplace",
12492#endif
12493#ifdef FEAT_WILDIGN
12494 "wildignore",
12495#endif
12496#ifdef FEAT_WILDMENU
12497 "wildmenu",
12498#endif
12499#ifdef FEAT_WINDOWS
12500 "windows",
12501#endif
12502#ifdef FEAT_WAK
12503 "winaltkeys",
12504#endif
12505#ifdef FEAT_WRITEBACKUP
12506 "writebackup",
12507#endif
12508#ifdef FEAT_XIM
12509 "xim",
12510#endif
12511#ifdef FEAT_XFONTSET
12512 "xfontset",
12513#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012514#ifdef FEAT_XPM_W32
12515 "xpm_w32",
12516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517#ifdef USE_XSMP
12518 "xsmp",
12519#endif
12520#ifdef USE_XSMP_INTERACT
12521 "xsmp_interact",
12522#endif
12523#ifdef FEAT_XCLIPBOARD
12524 "xterm_clipboard",
12525#endif
12526#ifdef FEAT_XTERM_SAVE
12527 "xterm_save",
12528#endif
12529#if defined(UNIX) && defined(FEAT_X11)
12530 "X11",
12531#endif
12532 NULL
12533 };
12534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012535 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536 for (i = 0; has_list[i] != NULL; ++i)
12537 if (STRICMP(name, has_list[i]) == 0)
12538 {
12539 n = TRUE;
12540 break;
12541 }
12542
12543 if (n == FALSE)
12544 {
12545 if (STRNICMP(name, "patch", 5) == 0)
12546 n = has_patch(atoi((char *)name + 5));
12547 else if (STRICMP(name, "vim_starting") == 0)
12548 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012549#ifdef FEAT_MBYTE
12550 else if (STRICMP(name, "multi_byte_encoding") == 0)
12551 n = has_mbyte;
12552#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012553#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12554 else if (STRICMP(name, "balloon_multiline") == 0)
12555 n = multiline_balloon_available();
12556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557#ifdef DYNAMIC_TCL
12558 else if (STRICMP(name, "tcl") == 0)
12559 n = tcl_enabled(FALSE);
12560#endif
12561#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12562 else if (STRICMP(name, "iconv") == 0)
12563 n = iconv_enabled(FALSE);
12564#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012565#ifdef DYNAMIC_LUA
12566 else if (STRICMP(name, "lua") == 0)
12567 n = lua_enabled(FALSE);
12568#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012569#ifdef DYNAMIC_MZSCHEME
12570 else if (STRICMP(name, "mzscheme") == 0)
12571 n = mzscheme_enabled(FALSE);
12572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573#ifdef DYNAMIC_RUBY
12574 else if (STRICMP(name, "ruby") == 0)
12575 n = ruby_enabled(FALSE);
12576#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012577#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578#ifdef DYNAMIC_PYTHON
12579 else if (STRICMP(name, "python") == 0)
12580 n = python_enabled(FALSE);
12581#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012582#endif
12583#ifdef FEAT_PYTHON3
12584#ifdef DYNAMIC_PYTHON3
12585 else if (STRICMP(name, "python3") == 0)
12586 n = python3_enabled(FALSE);
12587#endif
12588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589#ifdef DYNAMIC_PERL
12590 else if (STRICMP(name, "perl") == 0)
12591 n = perl_enabled(FALSE);
12592#endif
12593#ifdef FEAT_GUI
12594 else if (STRICMP(name, "gui_running") == 0)
12595 n = (gui.in_use || gui.starting);
12596# ifdef FEAT_GUI_W32
12597 else if (STRICMP(name, "gui_win32s") == 0)
12598 n = gui_is_win32s();
12599# endif
12600# ifdef FEAT_BROWSE
12601 else if (STRICMP(name, "browse") == 0)
12602 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12603# endif
12604#endif
12605#ifdef FEAT_SYN_HL
12606 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012607 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608#endif
12609#if defined(WIN3264)
12610 else if (STRICMP(name, "win95") == 0)
12611 n = mch_windows95();
12612#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012613#ifdef FEAT_NETBEANS_INTG
12614 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012615 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617 }
12618
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012619 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620}
12621
12622/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012623 * "has_key()" function
12624 */
12625 static void
12626f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012627 typval_T *argvars;
12628 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012629{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012630 if (argvars[0].v_type != VAR_DICT)
12631 {
12632 EMSG(_(e_dictreq));
12633 return;
12634 }
12635 if (argvars[0].vval.v_dict == NULL)
12636 return;
12637
12638 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012639 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012640}
12641
12642/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012643 * "haslocaldir()" function
12644 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012645 static void
12646f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012647 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012648 typval_T *rettv;
12649{
12650 rettv->vval.v_number = (curwin->w_localdir != NULL);
12651}
12652
12653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 * "hasmapto()" function
12655 */
12656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012657f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012658 typval_T *argvars;
12659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660{
12661 char_u *name;
12662 char_u *mode;
12663 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012664 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012666 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012667 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668 mode = (char_u *)"nvo";
12669 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012671 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012672 if (argvars[2].v_type != VAR_UNKNOWN)
12673 abbr = get_tv_number(&argvars[2]);
12674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675
Bram Moolenaar2c932302006-03-18 21:42:09 +000012676 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012677 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012679 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680}
12681
12682/*
12683 * "histadd()" function
12684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012687 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689{
12690#ifdef FEAT_CMDHIST
12691 int histype;
12692 char_u *str;
12693 char_u buf[NUMBUFLEN];
12694#endif
12695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012696 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012697 if (check_restricted() || check_secure())
12698 return;
12699#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012700 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12701 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 if (histype >= 0)
12703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012704 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 if (*str != NUL)
12706 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012707 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012709 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710 return;
12711 }
12712 }
12713#endif
12714}
12715
12716/*
12717 * "histdel()" function
12718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012720f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012721 typval_T *argvars UNUSED;
12722 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723{
12724#ifdef FEAT_CMDHIST
12725 int n;
12726 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012727 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012729 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12730 if (str == NULL)
12731 n = 0;
12732 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012734 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012735 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012737 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012738 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739 else
12740 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012741 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742 get_tv_string_buf(&argvars[1], buf));
12743 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744#endif
12745}
12746
12747/*
12748 * "histget()" function
12749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012751f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012752 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754{
12755#ifdef FEAT_CMDHIST
12756 int type;
12757 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012758 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012760 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12761 if (str == NULL)
12762 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012764 {
12765 type = get_histtype(str);
12766 if (argvars[1].v_type == VAR_UNKNOWN)
12767 idx = get_history_idx(type);
12768 else
12769 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12770 /* -1 on type error */
12771 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777}
12778
12779/*
12780 * "histnr()" function
12781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012783f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012784 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786{
12787 int i;
12788
12789#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012790 char_u *history = get_tv_string_chk(&argvars[0]);
12791
12792 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793 if (i >= HIST_CMD && i < HIST_COUNT)
12794 i = get_history_idx(i);
12795 else
12796#endif
12797 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799}
12800
12801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802 * "highlightID(name)" function
12803 */
12804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012806 typval_T *argvars;
12807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810}
12811
12812/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012813 * "highlight_exists()" function
12814 */
12815 static void
12816f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012817 typval_T *argvars;
12818 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012819{
12820 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12821}
12822
12823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 * "hostname()" function
12825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012827f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012828 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830{
12831 char_u hostname[256];
12832
12833 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834 rettv->v_type = VAR_STRING;
12835 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836}
12837
12838/*
12839 * iconv() function
12840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012843 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845{
12846#ifdef FEAT_MBYTE
12847 char_u buf1[NUMBUFLEN];
12848 char_u buf2[NUMBUFLEN];
12849 char_u *from, *to, *str;
12850 vimconv_T vimconv;
12851#endif
12852
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 rettv->v_type = VAR_STRING;
12854 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855
12856#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 str = get_tv_string(&argvars[0]);
12858 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12859 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860 vimconv.vc_type = CONV_NONE;
12861 convert_setup(&vimconv, from, to);
12862
12863 /* If the encodings are equal, no conversion needed. */
12864 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868
12869 convert_setup(&vimconv, NULL, NULL);
12870 vim_free(from);
12871 vim_free(to);
12872#endif
12873}
12874
12875/*
12876 * "indent()" function
12877 */
12878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012880 typval_T *argvars;
12881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882{
12883 linenr_T lnum;
12884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012887 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890}
12891
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012892/*
12893 * "index()" function
12894 */
12895 static void
12896f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012897 typval_T *argvars;
12898 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012899{
Bram Moolenaar33570922005-01-25 22:26:29 +000012900 list_T *l;
12901 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012902 long idx = 0;
12903 int ic = FALSE;
12904
12905 rettv->vval.v_number = -1;
12906 if (argvars[0].v_type != VAR_LIST)
12907 {
12908 EMSG(_(e_listreq));
12909 return;
12910 }
12911 l = argvars[0].vval.v_list;
12912 if (l != NULL)
12913 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012914 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012915 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012917 int error = FALSE;
12918
Bram Moolenaar758711c2005-02-02 23:11:38 +000012919 /* Start at specified item. Use the cached index that list_find()
12920 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012921 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012922 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012923 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012924 ic = get_tv_number_chk(&argvars[3], &error);
12925 if (error)
12926 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012927 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012928
Bram Moolenaar758711c2005-02-02 23:11:38 +000012929 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012930 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012931 {
12932 rettv->vval.v_number = idx;
12933 break;
12934 }
12935 }
12936}
12937
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938static int inputsecret_flag = 0;
12939
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012940static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12941
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012943 * This function is used by f_input() and f_inputdialog() functions. The third
12944 * argument to f_input() specifies the type of completion to use at the
12945 * prompt. The third argument to f_inputdialog() specifies the value to return
12946 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947 */
12948 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012949get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012950 typval_T *argvars;
12951 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012952 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012954 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955 char_u *p = NULL;
12956 int c;
12957 char_u buf[NUMBUFLEN];
12958 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012959 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012960 int xp_type = EXPAND_NOTHING;
12961 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012963 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012964 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965
12966#ifdef NO_CONSOLE_INPUT
12967 /* While starting up, there is no place to enter text. */
12968 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970#endif
12971
12972 cmd_silent = FALSE; /* Want to see the prompt. */
12973 if (prompt != NULL)
12974 {
12975 /* Only the part of the message after the last NL is considered as
12976 * prompt for the command line */
12977 p = vim_strrchr(prompt, '\n');
12978 if (p == NULL)
12979 p = prompt;
12980 else
12981 {
12982 ++p;
12983 c = *p;
12984 *p = NUL;
12985 msg_start();
12986 msg_clr_eos();
12987 msg_puts_attr(prompt, echo_attr);
12988 msg_didout = FALSE;
12989 msg_starthere();
12990 *p = c;
12991 }
12992 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012994 if (argvars[1].v_type != VAR_UNKNOWN)
12995 {
12996 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12997 if (defstr != NULL)
12998 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013000 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013001 {
13002 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013003 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013004 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013005
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013006 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013007 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013008
Bram Moolenaar4463f292005-09-25 22:20:24 +000013009 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13010 if (xp_name == NULL)
13011 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013012
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013013 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013014
Bram Moolenaar4463f292005-09-25 22:20:24 +000013015 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13016 &xp_arg) == FAIL)
13017 return;
13018 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013019 }
13020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013021 if (defstr != NULL)
13022 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013023 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13024 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013025 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013026 && argvars[1].v_type != VAR_UNKNOWN
13027 && argvars[2].v_type != VAR_UNKNOWN)
13028 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13029 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013030
13031 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013033 /* since the user typed this, no need to wait for return */
13034 need_wait_return = FALSE;
13035 msg_didout = FALSE;
13036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037 cmd_silent = cmd_silent_save;
13038}
13039
13040/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013041 * "input()" function
13042 * Also handles inputsecret() when inputsecret is set.
13043 */
13044 static void
13045f_input(argvars, rettv)
13046 typval_T *argvars;
13047 typval_T *rettv;
13048{
13049 get_user_input(argvars, rettv, FALSE);
13050}
13051
13052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053 * "inputdialog()" function
13054 */
13055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013056f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013057 typval_T *argvars;
13058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059{
13060#if defined(FEAT_GUI_TEXTDIALOG)
13061 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13062 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13063 {
13064 char_u *message;
13065 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013066 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013068 message = get_tv_string_chk(&argvars[0]);
13069 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013070 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013071 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072 else
13073 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013074 if (message != NULL && defstr != NULL
13075 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013076 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013077 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078 else
13079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013080 if (message != NULL && defstr != NULL
13081 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013082 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013083 rettv->vval.v_string = vim_strsave(
13084 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013086 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013088 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089 }
13090 else
13091#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013092 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093}
13094
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013095/*
13096 * "inputlist()" function
13097 */
13098 static void
13099f_inputlist(argvars, rettv)
13100 typval_T *argvars;
13101 typval_T *rettv;
13102{
13103 listitem_T *li;
13104 int selected;
13105 int mouse_used;
13106
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013107#ifdef NO_CONSOLE_INPUT
13108 /* While starting up, there is no place to enter text. */
13109 if (no_console_input())
13110 return;
13111#endif
13112 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13113 {
13114 EMSG2(_(e_listarg), "inputlist()");
13115 return;
13116 }
13117
13118 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013119 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013120 lines_left = Rows; /* avoid more prompt */
13121 msg_scroll = TRUE;
13122 msg_clr_eos();
13123
13124 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13125 {
13126 msg_puts(get_tv_string(&li->li_tv));
13127 msg_putchar('\n');
13128 }
13129
13130 /* Ask for choice. */
13131 selected = prompt_for_number(&mouse_used);
13132 if (mouse_used)
13133 selected -= lines_left;
13134
13135 rettv->vval.v_number = selected;
13136}
13137
13138
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13140
13141/*
13142 * "inputrestore()" function
13143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013145f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013146 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148{
13149 if (ga_userinput.ga_len > 0)
13150 {
13151 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13153 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013154 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155 }
13156 else if (p_verbose > 1)
13157 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013158 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013159 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 }
13161}
13162
13163/*
13164 * "inputsave()" function
13165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013167f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013168 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013171 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172 if (ga_grow(&ga_userinput, 1) == OK)
13173 {
13174 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13175 + ga_userinput.ga_len);
13176 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013177 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178 }
13179 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013180 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181}
13182
13183/*
13184 * "inputsecret()" function
13185 */
13186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013188 typval_T *argvars;
13189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190{
13191 ++cmdline_star;
13192 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013193 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194 --cmdline_star;
13195 --inputsecret_flag;
13196}
13197
13198/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013199 * "insert()" function
13200 */
13201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013203 typval_T *argvars;
13204 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013205{
13206 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013207 listitem_T *item;
13208 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013209 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013210
13211 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013212 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013213 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013214 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013215 {
13216 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013217 before = get_tv_number_chk(&argvars[2], &error);
13218 if (error)
13219 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013220
Bram Moolenaar758711c2005-02-02 23:11:38 +000013221 if (before == l->lv_len)
13222 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013223 else
13224 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013225 item = list_find(l, before);
13226 if (item == NULL)
13227 {
13228 EMSGN(_(e_listidx), before);
13229 l = NULL;
13230 }
13231 }
13232 if (l != NULL)
13233 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013234 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013235 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013236 }
13237 }
13238}
13239
13240/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013241 * "invert(expr)" function
13242 */
13243 static void
13244f_invert(argvars, rettv)
13245 typval_T *argvars;
13246 typval_T *rettv;
13247{
13248 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13249}
13250
13251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252 * "isdirectory()" function
13253 */
13254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013255f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013256 typval_T *argvars;
13257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013259 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260}
13261
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013262/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013263 * "islocked()" function
13264 */
13265 static void
13266f_islocked(argvars, rettv)
13267 typval_T *argvars;
13268 typval_T *rettv;
13269{
13270 lval_T lv;
13271 char_u *end;
13272 dictitem_T *di;
13273
13274 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013275 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13276 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013277 if (end != NULL && lv.ll_name != NULL)
13278 {
13279 if (*end != NUL)
13280 EMSG(_(e_trailing));
13281 else
13282 {
13283 if (lv.ll_tv == NULL)
13284 {
13285 if (check_changedtick(lv.ll_name))
13286 rettv->vval.v_number = 1; /* always locked */
13287 else
13288 {
13289 di = find_var(lv.ll_name, NULL);
13290 if (di != NULL)
13291 {
13292 /* Consider a variable locked when:
13293 * 1. the variable itself is locked
13294 * 2. the value of the variable is locked.
13295 * 3. the List or Dict value is locked.
13296 */
13297 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13298 || tv_islocked(&di->di_tv));
13299 }
13300 }
13301 }
13302 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013303 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013304 else if (lv.ll_newkey != NULL)
13305 EMSG2(_(e_dictkey), lv.ll_newkey);
13306 else if (lv.ll_list != NULL)
13307 /* List item. */
13308 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13309 else
13310 /* Dictionary item. */
13311 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13312 }
13313 }
13314
13315 clear_lval(&lv);
13316}
13317
Bram Moolenaar33570922005-01-25 22:26:29 +000013318static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013319
13320/*
13321 * Turn a dict into a list:
13322 * "what" == 0: list of keys
13323 * "what" == 1: list of values
13324 * "what" == 2: list of items
13325 */
13326 static void
13327dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013328 typval_T *argvars;
13329 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013330 int what;
13331{
Bram Moolenaar33570922005-01-25 22:26:29 +000013332 list_T *l2;
13333 dictitem_T *di;
13334 hashitem_T *hi;
13335 listitem_T *li;
13336 listitem_T *li2;
13337 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013338 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013339
Bram Moolenaar8c711452005-01-14 21:53:12 +000013340 if (argvars[0].v_type != VAR_DICT)
13341 {
13342 EMSG(_(e_dictreq));
13343 return;
13344 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013345 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013346 return;
13347
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013348 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013349 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013350
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013351 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013352 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013353 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013354 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013355 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013356 --todo;
13357 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013358
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013359 li = listitem_alloc();
13360 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013361 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013362 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013363
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013364 if (what == 0)
13365 {
13366 /* keys() */
13367 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013368 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013369 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13370 }
13371 else if (what == 1)
13372 {
13373 /* values() */
13374 copy_tv(&di->di_tv, &li->li_tv);
13375 }
13376 else
13377 {
13378 /* items() */
13379 l2 = list_alloc();
13380 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013381 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013382 li->li_tv.vval.v_list = l2;
13383 if (l2 == NULL)
13384 break;
13385 ++l2->lv_refcount;
13386
13387 li2 = listitem_alloc();
13388 if (li2 == NULL)
13389 break;
13390 list_append(l2, li2);
13391 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013392 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013393 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13394
13395 li2 = listitem_alloc();
13396 if (li2 == NULL)
13397 break;
13398 list_append(l2, li2);
13399 copy_tv(&di->di_tv, &li2->li_tv);
13400 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013401 }
13402 }
13403}
13404
13405/*
13406 * "items(dict)" function
13407 */
13408 static void
13409f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013410 typval_T *argvars;
13411 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013412{
13413 dict_list(argvars, rettv, 2);
13414}
13415
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013417 * "join()" function
13418 */
13419 static void
13420f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013421 typval_T *argvars;
13422 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013423{
13424 garray_T ga;
13425 char_u *sep;
13426
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013427 if (argvars[0].v_type != VAR_LIST)
13428 {
13429 EMSG(_(e_listreq));
13430 return;
13431 }
13432 if (argvars[0].vval.v_list == NULL)
13433 return;
13434 if (argvars[1].v_type == VAR_UNKNOWN)
13435 sep = (char_u *)" ";
13436 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013437 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013438
13439 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440
13441 if (sep != NULL)
13442 {
13443 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013444 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013445 ga_append(&ga, NUL);
13446 rettv->vval.v_string = (char_u *)ga.ga_data;
13447 }
13448 else
13449 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013450}
13451
13452/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013453 * "keys()" function
13454 */
13455 static void
13456f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013457 typval_T *argvars;
13458 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013459{
13460 dict_list(argvars, rettv, 0);
13461}
13462
13463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464 * "last_buffer_nr()" function.
13465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013467f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013468 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470{
13471 int n = 0;
13472 buf_T *buf;
13473
13474 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13475 if (n < buf->b_fnum)
13476 n = buf->b_fnum;
13477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013478 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013479}
13480
13481/*
13482 * "len()" function
13483 */
13484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013485f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013486 typval_T *argvars;
13487 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013488{
13489 switch (argvars[0].v_type)
13490 {
13491 case VAR_STRING:
13492 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013493 rettv->vval.v_number = (varnumber_T)STRLEN(
13494 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013495 break;
13496 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013497 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013498 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013499 case VAR_DICT:
13500 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13501 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013502 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013503 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013504 break;
13505 }
13506}
13507
Bram Moolenaar33570922005-01-25 22:26:29 +000013508static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013509
13510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013511libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013512 typval_T *argvars;
13513 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013514 int type;
13515{
13516#ifdef FEAT_LIBCALL
13517 char_u *string_in;
13518 char_u **string_result;
13519 int nr_result;
13520#endif
13521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013522 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013523 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013524 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013525
13526 if (check_restricted() || check_secure())
13527 return;
13528
13529#ifdef FEAT_LIBCALL
13530 /* The first two args must be strings, otherwise its meaningless */
13531 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13532 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013533 string_in = NULL;
13534 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013535 string_in = argvars[2].vval.v_string;
13536 if (type == VAR_NUMBER)
13537 string_result = NULL;
13538 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013539 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013540 if (mch_libcall(argvars[0].vval.v_string,
13541 argvars[1].vval.v_string,
13542 string_in,
13543 argvars[2].vval.v_number,
13544 string_result,
13545 &nr_result) == OK
13546 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013547 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013548 }
13549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550}
13551
13552/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013553 * "libcall()" function
13554 */
13555 static void
13556f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013557 typval_T *argvars;
13558 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013559{
13560 libcall_common(argvars, rettv, VAR_STRING);
13561}
13562
13563/*
13564 * "libcallnr()" function
13565 */
13566 static void
13567f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013568 typval_T *argvars;
13569 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013570{
13571 libcall_common(argvars, rettv, VAR_NUMBER);
13572}
13573
13574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575 * "line(string)" function
13576 */
13577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013578f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013579 typval_T *argvars;
13580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581{
13582 linenr_T lnum = 0;
13583 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013584 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013586 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 if (fp != NULL)
13588 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590}
13591
13592/*
13593 * "line2byte(lnum)" function
13594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013596f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013597 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599{
13600#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013601 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602#else
13603 linenr_T lnum;
13604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13610 if (rettv->vval.v_number >= 0)
13611 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612#endif
13613}
13614
13615/*
13616 * "lispindent(lnum)" function
13617 */
13618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013620 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622{
13623#ifdef FEAT_LISP
13624 pos_T pos;
13625 linenr_T lnum;
13626
13627 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13630 {
13631 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013632 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 curwin->w_cursor = pos;
13634 }
13635 else
13636#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638}
13639
13640/*
13641 * "localtime()" function
13642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013645 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013648 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649}
13650
Bram Moolenaar33570922005-01-25 22:26:29 +000013651static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652
13653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013654get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013655 typval_T *argvars;
13656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 int exact;
13658{
13659 char_u *keys;
13660 char_u *which;
13661 char_u buf[NUMBUFLEN];
13662 char_u *keys_buf = NULL;
13663 char_u *rhs;
13664 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013665 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013666 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013667 mapblock_T *mp;
13668 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669
13670 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671 rettv->v_type = VAR_STRING;
13672 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 if (*keys == NUL)
13676 return;
13677
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013678 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013680 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013681 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013682 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013683 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013684 if (argvars[3].v_type != VAR_UNKNOWN)
13685 get_dict = get_tv_number(&argvars[3]);
13686 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688 else
13689 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013690 if (which == NULL)
13691 return;
13692
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 mode = get_map_mode(&which, 0);
13694
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013695 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013696 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013698
13699 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013701 /* Return a string. */
13702 if (rhs != NULL)
13703 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704
Bram Moolenaarbd743252010-10-20 21:23:33 +020013705 }
13706 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13707 {
13708 /* Return a dictionary. */
13709 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13710 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13711 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712
Bram Moolenaarbd743252010-10-20 21:23:33 +020013713 dict_add_nr_str(dict, "lhs", 0L, lhs);
13714 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13715 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13716 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13717 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13718 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13719 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13720 dict_add_nr_str(dict, "mode", 0L, mapmode);
13721
13722 vim_free(lhs);
13723 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 }
13725}
13726
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013727#ifdef FEAT_FLOAT
13728/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013729 * "log()" function
13730 */
13731 static void
13732f_log(argvars, rettv)
13733 typval_T *argvars;
13734 typval_T *rettv;
13735{
13736 float_T f;
13737
13738 rettv->v_type = VAR_FLOAT;
13739 if (get_float_arg(argvars, &f) == OK)
13740 rettv->vval.v_float = log(f);
13741 else
13742 rettv->vval.v_float = 0.0;
13743}
13744
13745/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013746 * "log10()" function
13747 */
13748 static void
13749f_log10(argvars, rettv)
13750 typval_T *argvars;
13751 typval_T *rettv;
13752{
13753 float_T f;
13754
13755 rettv->v_type = VAR_FLOAT;
13756 if (get_float_arg(argvars, &f) == OK)
13757 rettv->vval.v_float = log10(f);
13758 else
13759 rettv->vval.v_float = 0.0;
13760}
13761#endif
13762
Bram Moolenaar1dced572012-04-05 16:54:08 +020013763#ifdef FEAT_LUA
13764/*
13765 * "luaeval()" function
13766 */
13767 static void
13768f_luaeval(argvars, rettv)
13769 typval_T *argvars;
13770 typval_T *rettv;
13771{
13772 char_u *str;
13773 char_u buf[NUMBUFLEN];
13774
13775 str = get_tv_string_buf(&argvars[0], buf);
13776 do_luaeval(str, argvars + 1, rettv);
13777}
13778#endif
13779
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013781 * "map()" function
13782 */
13783 static void
13784f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013785 typval_T *argvars;
13786 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013787{
13788 filter_map(argvars, rettv, TRUE);
13789}
13790
13791/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013792 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793 */
13794 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013795f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013796 typval_T *argvars;
13797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013799 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800}
13801
13802/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013803 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 */
13805 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013806f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013807 typval_T *argvars;
13808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013810 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811}
13812
Bram Moolenaar33570922005-01-25 22:26:29 +000013813static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814
13815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013816find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013817 typval_T *argvars;
13818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 int type;
13820{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013821 char_u *str = NULL;
13822 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 char_u *pat;
13824 regmatch_T regmatch;
13825 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013826 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827 char_u *save_cpo;
13828 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013829 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013830 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013831 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013832 list_T *l = NULL;
13833 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013834 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013835 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836
13837 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13838 save_cpo = p_cpo;
13839 p_cpo = (char_u *)"";
13840
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013841 rettv->vval.v_number = -1;
13842 if (type == 3)
13843 {
13844 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013845 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013846 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013847 }
13848 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013850 rettv->v_type = VAR_STRING;
13851 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013854 if (argvars[0].v_type == VAR_LIST)
13855 {
13856 if ((l = argvars[0].vval.v_list) == NULL)
13857 goto theend;
13858 li = l->lv_first;
13859 }
13860 else
13861 expr = str = get_tv_string(&argvars[0]);
13862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13864 if (pat == NULL)
13865 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013866
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013867 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013869 int error = FALSE;
13870
13871 start = get_tv_number_chk(&argvars[2], &error);
13872 if (error)
13873 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013874 if (l != NULL)
13875 {
13876 li = list_find(l, start);
13877 if (li == NULL)
13878 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013879 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013880 }
13881 else
13882 {
13883 if (start < 0)
13884 start = 0;
13885 if (start > (long)STRLEN(str))
13886 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013887 /* When "count" argument is there ignore matches before "start",
13888 * otherwise skip part of the string. Differs when pattern is "^"
13889 * or "\<". */
13890 if (argvars[3].v_type != VAR_UNKNOWN)
13891 startcol = start;
13892 else
13893 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013894 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013895
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013896 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013897 nth = get_tv_number_chk(&argvars[3], &error);
13898 if (error)
13899 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900 }
13901
13902 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13903 if (regmatch.regprog != NULL)
13904 {
13905 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013906
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013907 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013908 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013909 if (l != NULL)
13910 {
13911 if (li == NULL)
13912 {
13913 match = FALSE;
13914 break;
13915 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013916 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013917 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013918 if (str == NULL)
13919 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013920 }
13921
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013922 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013923
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013924 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013925 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013926 if (l == NULL && !match)
13927 break;
13928
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013929 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013930 if (l != NULL)
13931 {
13932 li = li->li_next;
13933 ++idx;
13934 }
13935 else
13936 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013937#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013938 startcol = (colnr_T)(regmatch.startp[0]
13939 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013940#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013941 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013942#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013943 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013944 }
13945
13946 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013948 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013949 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013950 int i;
13951
13952 /* return list with matched string and submatches */
13953 for (i = 0; i < NSUBEXP; ++i)
13954 {
13955 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013956 {
13957 if (list_append_string(rettv->vval.v_list,
13958 (char_u *)"", 0) == FAIL)
13959 break;
13960 }
13961 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013962 regmatch.startp[i],
13963 (int)(regmatch.endp[i] - regmatch.startp[i]))
13964 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013965 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013966 }
13967 }
13968 else if (type == 2)
13969 {
13970 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013971 if (l != NULL)
13972 copy_tv(&li->li_tv, rettv);
13973 else
13974 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013976 }
13977 else if (l != NULL)
13978 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979 else
13980 {
13981 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013982 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983 (varnumber_T)(regmatch.startp[0] - str);
13984 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013985 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013987 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988 }
13989 }
Bram Moolenaar473de612013-06-08 18:19:48 +020013990 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991 }
13992
13993theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013994 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 p_cpo = save_cpo;
13996}
13997
13998/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013999 * "match()" function
14000 */
14001 static void
14002f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014003 typval_T *argvars;
14004 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014005{
14006 find_some_match(argvars, rettv, 1);
14007}
14008
14009/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014010 * "matchadd()" function
14011 */
14012 static void
14013f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014014 typval_T *argvars UNUSED;
14015 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014016{
14017#ifdef FEAT_SEARCH_EXTRA
14018 char_u buf[NUMBUFLEN];
14019 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14020 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14021 int prio = 10; /* default priority */
14022 int id = -1;
14023 int error = FALSE;
14024
14025 rettv->vval.v_number = -1;
14026
14027 if (grp == NULL || pat == NULL)
14028 return;
14029 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014030 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014031 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014032 if (argvars[3].v_type != VAR_UNKNOWN)
14033 id = get_tv_number_chk(&argvars[3], &error);
14034 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014035 if (error == TRUE)
14036 return;
14037 if (id >= 1 && id <= 3)
14038 {
14039 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14040 return;
14041 }
14042
14043 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14044#endif
14045}
14046
14047/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014048 * "matcharg()" function
14049 */
14050 static void
14051f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014052 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014053 typval_T *rettv;
14054{
14055 if (rettv_list_alloc(rettv) == OK)
14056 {
14057#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014058 int id = get_tv_number(&argvars[0]);
14059 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014060
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014061 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014062 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014063 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14064 {
14065 list_append_string(rettv->vval.v_list,
14066 syn_id2name(m->hlg_id), -1);
14067 list_append_string(rettv->vval.v_list, m->pattern, -1);
14068 }
14069 else
14070 {
14071 list_append_string(rettv->vval.v_list, NUL, -1);
14072 list_append_string(rettv->vval.v_list, NUL, -1);
14073 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014074 }
14075#endif
14076 }
14077}
14078
14079/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014080 * "matchdelete()" function
14081 */
14082 static void
14083f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014084 typval_T *argvars UNUSED;
14085 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014086{
14087#ifdef FEAT_SEARCH_EXTRA
14088 rettv->vval.v_number = match_delete(curwin,
14089 (int)get_tv_number(&argvars[0]), TRUE);
14090#endif
14091}
14092
14093/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014094 * "matchend()" function
14095 */
14096 static void
14097f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014098 typval_T *argvars;
14099 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014100{
14101 find_some_match(argvars, rettv, 0);
14102}
14103
14104/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014105 * "matchlist()" function
14106 */
14107 static void
14108f_matchlist(argvars, rettv)
14109 typval_T *argvars;
14110 typval_T *rettv;
14111{
14112 find_some_match(argvars, rettv, 3);
14113}
14114
14115/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014116 * "matchstr()" function
14117 */
14118 static void
14119f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014120 typval_T *argvars;
14121 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014122{
14123 find_some_match(argvars, rettv, 2);
14124}
14125
Bram Moolenaar33570922005-01-25 22:26:29 +000014126static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014127
14128 static void
14129max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014130 typval_T *argvars;
14131 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014132 int domax;
14133{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014134 long n = 0;
14135 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014136 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014137
14138 if (argvars[0].v_type == VAR_LIST)
14139 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014140 list_T *l;
14141 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014142
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014143 l = argvars[0].vval.v_list;
14144 if (l != NULL)
14145 {
14146 li = l->lv_first;
14147 if (li != NULL)
14148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014150 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014151 {
14152 li = li->li_next;
14153 if (li == NULL)
14154 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014155 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014156 if (domax ? i > n : i < n)
14157 n = i;
14158 }
14159 }
14160 }
14161 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014162 else if (argvars[0].v_type == VAR_DICT)
14163 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014164 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014165 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014166 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014167 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014168
14169 d = argvars[0].vval.v_dict;
14170 if (d != NULL)
14171 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014172 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014173 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014174 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014175 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014176 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014177 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014178 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014179 if (first)
14180 {
14181 n = i;
14182 first = FALSE;
14183 }
14184 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014185 n = i;
14186 }
14187 }
14188 }
14189 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014190 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014191 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014192 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014193}
14194
14195/*
14196 * "max()" function
14197 */
14198 static void
14199f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014200 typval_T *argvars;
14201 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014202{
14203 max_min(argvars, rettv, TRUE);
14204}
14205
14206/*
14207 * "min()" function
14208 */
14209 static void
14210f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014211 typval_T *argvars;
14212 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014213{
14214 max_min(argvars, rettv, FALSE);
14215}
14216
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014217static int mkdir_recurse __ARGS((char_u *dir, int prot));
14218
14219/*
14220 * Create the directory in which "dir" is located, and higher levels when
14221 * needed.
14222 */
14223 static int
14224mkdir_recurse(dir, prot)
14225 char_u *dir;
14226 int prot;
14227{
14228 char_u *p;
14229 char_u *updir;
14230 int r = FAIL;
14231
14232 /* Get end of directory name in "dir".
14233 * We're done when it's "/" or "c:/". */
14234 p = gettail_sep(dir);
14235 if (p <= get_past_head(dir))
14236 return OK;
14237
14238 /* If the directory exists we're done. Otherwise: create it.*/
14239 updir = vim_strnsave(dir, (int)(p - dir));
14240 if (updir == NULL)
14241 return FAIL;
14242 if (mch_isdir(updir))
14243 r = OK;
14244 else if (mkdir_recurse(updir, prot) == OK)
14245 r = vim_mkdir_emsg(updir, prot);
14246 vim_free(updir);
14247 return r;
14248}
14249
14250#ifdef vim_mkdir
14251/*
14252 * "mkdir()" function
14253 */
14254 static void
14255f_mkdir(argvars, rettv)
14256 typval_T *argvars;
14257 typval_T *rettv;
14258{
14259 char_u *dir;
14260 char_u buf[NUMBUFLEN];
14261 int prot = 0755;
14262
14263 rettv->vval.v_number = FAIL;
14264 if (check_restricted() || check_secure())
14265 return;
14266
14267 dir = get_tv_string_buf(&argvars[0], buf);
14268 if (argvars[1].v_type != VAR_UNKNOWN)
14269 {
14270 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014271 prot = get_tv_number_chk(&argvars[2], NULL);
14272 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014273 mkdir_recurse(dir, prot);
14274 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014275 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014276}
14277#endif
14278
Bram Moolenaar0d660222005-01-07 21:51:51 +000014279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 * "mode()" function
14281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014284 typval_T *argvars;
14285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014287 char_u buf[3];
14288
14289 buf[1] = NUL;
14290 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291
14292#ifdef FEAT_VISUAL
14293 if (VIsual_active)
14294 {
14295 if (VIsual_select)
14296 buf[0] = VIsual_mode + 's' - 'v';
14297 else
14298 buf[0] = VIsual_mode;
14299 }
14300 else
14301#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014302 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14303 || State == CONFIRM)
14304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014306 if (State == ASKMORE)
14307 buf[1] = 'm';
14308 else if (State == CONFIRM)
14309 buf[1] = '?';
14310 }
14311 else if (State == EXTERNCMD)
14312 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313 else if (State & INSERT)
14314 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014315#ifdef FEAT_VREPLACE
14316 if (State & VREPLACE_FLAG)
14317 {
14318 buf[0] = 'R';
14319 buf[1] = 'v';
14320 }
14321 else
14322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323 if (State & REPLACE_FLAG)
14324 buf[0] = 'R';
14325 else
14326 buf[0] = 'i';
14327 }
14328 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014331 if (exmode_active)
14332 buf[1] = 'v';
14333 }
14334 else if (exmode_active)
14335 {
14336 buf[0] = 'c';
14337 buf[1] = 'e';
14338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014340 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014342 if (finish_op)
14343 buf[1] = 'o';
14344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014346 /* Clear out the minor mode when the argument is not a non-zero number or
14347 * non-empty string. */
14348 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014349 buf[1] = NUL;
14350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014351 rettv->vval.v_string = vim_strsave(buf);
14352 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353}
14354
Bram Moolenaar429fa852013-04-15 12:27:36 +020014355#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014356/*
14357 * "mzeval()" function
14358 */
14359 static void
14360f_mzeval(argvars, rettv)
14361 typval_T *argvars;
14362 typval_T *rettv;
14363{
14364 char_u *str;
14365 char_u buf[NUMBUFLEN];
14366
14367 str = get_tv_string_buf(&argvars[0], buf);
14368 do_mzeval(str, rettv);
14369}
Bram Moolenaar75676462013-01-30 14:55:42 +010014370
14371 void
14372mzscheme_call_vim(name, args, rettv)
14373 char_u *name;
14374 typval_T *args;
14375 typval_T *rettv;
14376{
14377 typval_T argvars[3];
14378
14379 argvars[0].v_type = VAR_STRING;
14380 argvars[0].vval.v_string = name;
14381 copy_tv(args, &argvars[1]);
14382 argvars[2].v_type = VAR_UNKNOWN;
14383 f_call(argvars, rettv);
14384 clear_tv(&argvars[1]);
14385}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014386#endif
14387
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014389 * "nextnonblank()" function
14390 */
14391 static void
14392f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014393 typval_T *argvars;
14394 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014395{
14396 linenr_T lnum;
14397
14398 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014400 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014401 {
14402 lnum = 0;
14403 break;
14404 }
14405 if (*skipwhite(ml_get(lnum)) != NUL)
14406 break;
14407 }
14408 rettv->vval.v_number = lnum;
14409}
14410
14411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412 * "nr2char()" function
14413 */
14414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014415f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014416 typval_T *argvars;
14417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418{
14419 char_u buf[NUMBUFLEN];
14420
14421#ifdef FEAT_MBYTE
14422 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014423 {
14424 int utf8 = 0;
14425
14426 if (argvars[1].v_type != VAR_UNKNOWN)
14427 utf8 = get_tv_number_chk(&argvars[1], NULL);
14428 if (utf8)
14429 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14430 else
14431 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433 else
14434#endif
14435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014436 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 buf[1] = NUL;
14438 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014439 rettv->v_type = VAR_STRING;
14440 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014441}
14442
14443/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014444 * "or(expr, expr)" function
14445 */
14446 static void
14447f_or(argvars, rettv)
14448 typval_T *argvars;
14449 typval_T *rettv;
14450{
14451 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14452 | get_tv_number_chk(&argvars[1], NULL);
14453}
14454
14455/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014456 * "pathshorten()" function
14457 */
14458 static void
14459f_pathshorten(argvars, rettv)
14460 typval_T *argvars;
14461 typval_T *rettv;
14462{
14463 char_u *p;
14464
14465 rettv->v_type = VAR_STRING;
14466 p = get_tv_string_chk(&argvars[0]);
14467 if (p == NULL)
14468 rettv->vval.v_string = NULL;
14469 else
14470 {
14471 p = vim_strsave(p);
14472 rettv->vval.v_string = p;
14473 if (p != NULL)
14474 shorten_dir(p);
14475 }
14476}
14477
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014478#ifdef FEAT_FLOAT
14479/*
14480 * "pow()" function
14481 */
14482 static void
14483f_pow(argvars, rettv)
14484 typval_T *argvars;
14485 typval_T *rettv;
14486{
14487 float_T fx, fy;
14488
14489 rettv->v_type = VAR_FLOAT;
14490 if (get_float_arg(argvars, &fx) == OK
14491 && get_float_arg(&argvars[1], &fy) == OK)
14492 rettv->vval.v_float = pow(fx, fy);
14493 else
14494 rettv->vval.v_float = 0.0;
14495}
14496#endif
14497
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014498/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014499 * "prevnonblank()" function
14500 */
14501 static void
14502f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014503 typval_T *argvars;
14504 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014505{
14506 linenr_T lnum;
14507
14508 lnum = get_tv_lnum(argvars);
14509 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14510 lnum = 0;
14511 else
14512 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14513 --lnum;
14514 rettv->vval.v_number = lnum;
14515}
14516
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014517#ifdef HAVE_STDARG_H
14518/* This dummy va_list is here because:
14519 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14520 * - locally in the function results in a "used before set" warning
14521 * - using va_start() to initialize it gives "function with fixed args" error */
14522static va_list ap;
14523#endif
14524
Bram Moolenaar8c711452005-01-14 21:53:12 +000014525/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014526 * "printf()" function
14527 */
14528 static void
14529f_printf(argvars, rettv)
14530 typval_T *argvars;
14531 typval_T *rettv;
14532{
14533 rettv->v_type = VAR_STRING;
14534 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014535#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014536 {
14537 char_u buf[NUMBUFLEN];
14538 int len;
14539 char_u *s;
14540 int saved_did_emsg = did_emsg;
14541 char *fmt;
14542
14543 /* Get the required length, allocate the buffer and do it for real. */
14544 did_emsg = FALSE;
14545 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014546 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014547 if (!did_emsg)
14548 {
14549 s = alloc(len + 1);
14550 if (s != NULL)
14551 {
14552 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014553 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014554 }
14555 }
14556 did_emsg |= saved_did_emsg;
14557 }
14558#endif
14559}
14560
14561/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014562 * "pumvisible()" function
14563 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014564 static void
14565f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014566 typval_T *argvars UNUSED;
14567 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014568{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014569#ifdef FEAT_INS_EXPAND
14570 if (pum_visible())
14571 rettv->vval.v_number = 1;
14572#endif
14573}
14574
Bram Moolenaardb913952012-06-29 12:54:53 +020014575#ifdef FEAT_PYTHON3
14576/*
14577 * "py3eval()" function
14578 */
14579 static void
14580f_py3eval(argvars, rettv)
14581 typval_T *argvars;
14582 typval_T *rettv;
14583{
14584 char_u *str;
14585 char_u buf[NUMBUFLEN];
14586
14587 str = get_tv_string_buf(&argvars[0], buf);
14588 do_py3eval(str, rettv);
14589}
14590#endif
14591
14592#ifdef FEAT_PYTHON
14593/*
14594 * "pyeval()" function
14595 */
14596 static void
14597f_pyeval(argvars, rettv)
14598 typval_T *argvars;
14599 typval_T *rettv;
14600{
14601 char_u *str;
14602 char_u buf[NUMBUFLEN];
14603
14604 str = get_tv_string_buf(&argvars[0], buf);
14605 do_pyeval(str, rettv);
14606}
14607#endif
14608
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014609/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014610 * "range()" function
14611 */
14612 static void
14613f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014614 typval_T *argvars;
14615 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014616{
14617 long start;
14618 long end;
14619 long stride = 1;
14620 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014623 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014624 if (argvars[1].v_type == VAR_UNKNOWN)
14625 {
14626 end = start - 1;
14627 start = 0;
14628 }
14629 else
14630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014631 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014632 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014633 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014634 }
14635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636 if (error)
14637 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014638 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014639 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014640 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014641 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014642 else
14643 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014644 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014645 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014646 if (list_append_number(rettv->vval.v_list,
14647 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014648 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014649 }
14650}
14651
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014652/*
14653 * "readfile()" function
14654 */
14655 static void
14656f_readfile(argvars, rettv)
14657 typval_T *argvars;
14658 typval_T *rettv;
14659{
14660 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014661 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014662 char_u *fname;
14663 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014664 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14665 int io_size = sizeof(buf);
14666 int readlen; /* size of last fread() */
14667 char_u *prev = NULL; /* previously read bytes, if any */
14668 long prevlen = 0; /* length of data in prev */
14669 long prevsize = 0; /* size of prev buffer */
14670 long maxline = MAXLNUM;
14671 long cnt = 0;
14672 char_u *p; /* position in buf */
14673 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014674
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014675 if (argvars[1].v_type != VAR_UNKNOWN)
14676 {
14677 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14678 binary = TRUE;
14679 if (argvars[2].v_type != VAR_UNKNOWN)
14680 maxline = get_tv_number(&argvars[2]);
14681 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014682
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014683 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014684 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014685
14686 /* Always open the file in binary mode, library functions have a mind of
14687 * their own about CR-LF conversion. */
14688 fname = get_tv_string(&argvars[0]);
14689 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14690 {
14691 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14692 return;
14693 }
14694
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014695 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014696 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014697 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014698
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014699 /* This for loop processes what was read, but is also entered at end
14700 * of file so that either:
14701 * - an incomplete line gets written
14702 * - a "binary" file gets an empty line at the end if it ends in a
14703 * newline. */
14704 for (p = buf, start = buf;
14705 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14706 ++p)
14707 {
14708 if (*p == '\n' || readlen <= 0)
14709 {
14710 listitem_T *li;
14711 char_u *s = NULL;
14712 long_u len = p - start;
14713
14714 /* Finished a line. Remove CRs before NL. */
14715 if (readlen > 0 && !binary)
14716 {
14717 while (len > 0 && start[len - 1] == '\r')
14718 --len;
14719 /* removal may cross back to the "prev" string */
14720 if (len == 0)
14721 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14722 --prevlen;
14723 }
14724 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014725 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014726 else
14727 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014728 /* Change "prev" buffer to be the right size. This way
14729 * the bytes are only copied once, and very long lines are
14730 * allocated only once. */
14731 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014732 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014733 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014734 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014735 prev = NULL; /* the list will own the string */
14736 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014737 }
14738 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014739 if (s == NULL)
14740 {
14741 do_outofmem_msg((long_u) prevlen + len + 1);
14742 failed = TRUE;
14743 break;
14744 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014745
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014746 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014747 {
14748 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014749 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014750 break;
14751 }
14752 li->li_tv.v_type = VAR_STRING;
14753 li->li_tv.v_lock = 0;
14754 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014755 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014756
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014757 start = p + 1; /* step over newline */
14758 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014759 break;
14760 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014761 else if (*p == NUL)
14762 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014763#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014764 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14765 * when finding the BF and check the previous two bytes. */
14766 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014767 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014768 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14769 * + 1, these may be in the "prev" string. */
14770 char_u back1 = p >= buf + 1 ? p[-1]
14771 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14772 char_u back2 = p >= buf + 2 ? p[-2]
14773 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14774 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014775
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014776 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014777 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014778 char_u *dest = p - 2;
14779
14780 /* Usually a BOM is at the beginning of a file, and so at
14781 * the beginning of a line; then we can just step over it.
14782 */
14783 if (start == dest)
14784 start = p + 1;
14785 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014786 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014787 /* have to shuffle buf to close gap */
14788 int adjust_prevlen = 0;
14789
14790 if (dest < buf)
14791 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014792 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014793 dest = buf;
14794 }
14795 if (readlen > p - buf + 1)
14796 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14797 readlen -= 3 - adjust_prevlen;
14798 prevlen -= adjust_prevlen;
14799 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014800 }
14801 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014802 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014803#endif
14804 } /* for */
14805
14806 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14807 break;
14808 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014809 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014810 /* There's part of a line in buf, store it in "prev". */
14811 if (p - start + prevlen >= prevsize)
14812 {
14813 /* need bigger "prev" buffer */
14814 char_u *newprev;
14815
14816 /* A common use case is ordinary text files and "prev" gets a
14817 * fragment of a line, so the first allocation is made
14818 * small, to avoid repeatedly 'allocing' large and
14819 * 'reallocing' small. */
14820 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014821 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014822 else
14823 {
14824 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014825 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014826 prevsize = grow50pc > growmin ? grow50pc : growmin;
14827 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014828 newprev = prev == NULL ? alloc(prevsize)
14829 : vim_realloc(prev, prevsize);
14830 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014831 {
14832 do_outofmem_msg((long_u)prevsize);
14833 failed = TRUE;
14834 break;
14835 }
14836 prev = newprev;
14837 }
14838 /* Add the line part to end of "prev". */
14839 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014840 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014841 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014842 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014843
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014844 /*
14845 * For a negative line count use only the lines at the end of the file,
14846 * free the rest.
14847 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014848 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014849 while (cnt > -maxline)
14850 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014851 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014852 --cnt;
14853 }
14854
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014855 if (failed)
14856 {
14857 list_free(rettv->vval.v_list, TRUE);
14858 /* readfile doc says an empty list is returned on error */
14859 rettv->vval.v_list = list_alloc();
14860 }
14861
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014862 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014863 fclose(fd);
14864}
14865
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014866#if defined(FEAT_RELTIME)
14867static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14868
14869/*
14870 * Convert a List to proftime_T.
14871 * Return FAIL when there is something wrong.
14872 */
14873 static int
14874list2proftime(arg, tm)
14875 typval_T *arg;
14876 proftime_T *tm;
14877{
14878 long n1, n2;
14879 int error = FALSE;
14880
14881 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14882 || arg->vval.v_list->lv_len != 2)
14883 return FAIL;
14884 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14885 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14886# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014887 tm->HighPart = n1;
14888 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014889# else
14890 tm->tv_sec = n1;
14891 tm->tv_usec = n2;
14892# endif
14893 return error ? FAIL : OK;
14894}
14895#endif /* FEAT_RELTIME */
14896
14897/*
14898 * "reltime()" function
14899 */
14900 static void
14901f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014902 typval_T *argvars UNUSED;
14903 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014904{
14905#ifdef FEAT_RELTIME
14906 proftime_T res;
14907 proftime_T start;
14908
14909 if (argvars[0].v_type == VAR_UNKNOWN)
14910 {
14911 /* No arguments: get current time. */
14912 profile_start(&res);
14913 }
14914 else if (argvars[1].v_type == VAR_UNKNOWN)
14915 {
14916 if (list2proftime(&argvars[0], &res) == FAIL)
14917 return;
14918 profile_end(&res);
14919 }
14920 else
14921 {
14922 /* Two arguments: compute the difference. */
14923 if (list2proftime(&argvars[0], &start) == FAIL
14924 || list2proftime(&argvars[1], &res) == FAIL)
14925 return;
14926 profile_sub(&res, &start);
14927 }
14928
14929 if (rettv_list_alloc(rettv) == OK)
14930 {
14931 long n1, n2;
14932
14933# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014934 n1 = res.HighPart;
14935 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014936# else
14937 n1 = res.tv_sec;
14938 n2 = res.tv_usec;
14939# endif
14940 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14941 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14942 }
14943#endif
14944}
14945
14946/*
14947 * "reltimestr()" function
14948 */
14949 static void
14950f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014951 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014952 typval_T *rettv;
14953{
14954#ifdef FEAT_RELTIME
14955 proftime_T tm;
14956#endif
14957
14958 rettv->v_type = VAR_STRING;
14959 rettv->vval.v_string = NULL;
14960#ifdef FEAT_RELTIME
14961 if (list2proftime(&argvars[0], &tm) == OK)
14962 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14963#endif
14964}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014965
Bram Moolenaar0d660222005-01-07 21:51:51 +000014966#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14967static void make_connection __ARGS((void));
14968static int check_connection __ARGS((void));
14969
14970 static void
14971make_connection()
14972{
14973 if (X_DISPLAY == NULL
14974# ifdef FEAT_GUI
14975 && !gui.in_use
14976# endif
14977 )
14978 {
14979 x_force_connect = TRUE;
14980 setup_term_clip();
14981 x_force_connect = FALSE;
14982 }
14983}
14984
14985 static int
14986check_connection()
14987{
14988 make_connection();
14989 if (X_DISPLAY == NULL)
14990 {
14991 EMSG(_("E240: No connection to Vim server"));
14992 return FAIL;
14993 }
14994 return OK;
14995}
14996#endif
14997
14998#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014999static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015000
15001 static void
15002remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015003 typval_T *argvars;
15004 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015005 int expr;
15006{
15007 char_u *server_name;
15008 char_u *keys;
15009 char_u *r = NULL;
15010 char_u buf[NUMBUFLEN];
15011# ifdef WIN32
15012 HWND w;
15013# else
15014 Window w;
15015# endif
15016
15017 if (check_restricted() || check_secure())
15018 return;
15019
15020# ifdef FEAT_X11
15021 if (check_connection() == FAIL)
15022 return;
15023# endif
15024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015025 server_name = get_tv_string_chk(&argvars[0]);
15026 if (server_name == NULL)
15027 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015028 keys = get_tv_string_buf(&argvars[1], buf);
15029# ifdef WIN32
15030 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15031# else
15032 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15033 < 0)
15034# endif
15035 {
15036 if (r != NULL)
15037 EMSG(r); /* sending worked but evaluation failed */
15038 else
15039 EMSG2(_("E241: Unable to send to %s"), server_name);
15040 return;
15041 }
15042
15043 rettv->vval.v_string = r;
15044
15045 if (argvars[2].v_type != VAR_UNKNOWN)
15046 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015047 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015048 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015049 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015050
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015051 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015052 v.di_tv.v_type = VAR_STRING;
15053 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015054 idvar = get_tv_string_chk(&argvars[2]);
15055 if (idvar != NULL)
15056 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015057 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015058 }
15059}
15060#endif
15061
15062/*
15063 * "remote_expr()" function
15064 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015065 static void
15066f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015067 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015068 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015069{
15070 rettv->v_type = VAR_STRING;
15071 rettv->vval.v_string = NULL;
15072#ifdef FEAT_CLIENTSERVER
15073 remote_common(argvars, rettv, TRUE);
15074#endif
15075}
15076
15077/*
15078 * "remote_foreground()" function
15079 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015080 static void
15081f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015082 typval_T *argvars UNUSED;
15083 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085#ifdef FEAT_CLIENTSERVER
15086# ifdef WIN32
15087 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015088 {
15089 char_u *server_name = get_tv_string_chk(&argvars[0]);
15090
15091 if (server_name != NULL)
15092 serverForeground(server_name);
15093 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015094# else
15095 /* Send a foreground() expression to the server. */
15096 argvars[1].v_type = VAR_STRING;
15097 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15098 argvars[2].v_type = VAR_UNKNOWN;
15099 remote_common(argvars, rettv, TRUE);
15100 vim_free(argvars[1].vval.v_string);
15101# endif
15102#endif
15103}
15104
Bram Moolenaar0d660222005-01-07 21:51:51 +000015105 static void
15106f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015107 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015108 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015109{
15110#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015111 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112 char_u *s = NULL;
15113# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015114 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015115# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015116 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015117
15118 if (check_restricted() || check_secure())
15119 {
15120 rettv->vval.v_number = -1;
15121 return;
15122 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015123 serverid = get_tv_string_chk(&argvars[0]);
15124 if (serverid == NULL)
15125 {
15126 rettv->vval.v_number = -1;
15127 return; /* type error; errmsg already given */
15128 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015130 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015131 if (n == 0)
15132 rettv->vval.v_number = -1;
15133 else
15134 {
15135 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15136 rettv->vval.v_number = (s != NULL);
15137 }
15138# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015139 if (check_connection() == FAIL)
15140 return;
15141
15142 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015143 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015144# endif
15145
15146 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015148 char_u *retvar;
15149
Bram Moolenaar33570922005-01-25 22:26:29 +000015150 v.di_tv.v_type = VAR_STRING;
15151 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015152 retvar = get_tv_string_chk(&argvars[1]);
15153 if (retvar != NULL)
15154 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015155 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015156 }
15157#else
15158 rettv->vval.v_number = -1;
15159#endif
15160}
15161
Bram Moolenaar0d660222005-01-07 21:51:51 +000015162 static void
15163f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015164 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015165 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015166{
15167 char_u *r = NULL;
15168
15169#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015170 char_u *serverid = get_tv_string_chk(&argvars[0]);
15171
15172 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015173 {
15174# ifdef WIN32
15175 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015176 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015177
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015178 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015179 if (n != 0)
15180 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15181 if (r == NULL)
15182# else
15183 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015184 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015185# endif
15186 EMSG(_("E277: Unable to read a server reply"));
15187 }
15188#endif
15189 rettv->v_type = VAR_STRING;
15190 rettv->vval.v_string = r;
15191}
15192
15193/*
15194 * "remote_send()" function
15195 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015196 static void
15197f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015198 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015199 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015200{
15201 rettv->v_type = VAR_STRING;
15202 rettv->vval.v_string = NULL;
15203#ifdef FEAT_CLIENTSERVER
15204 remote_common(argvars, rettv, FALSE);
15205#endif
15206}
15207
15208/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015209 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015210 */
15211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015212f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015213 typval_T *argvars;
15214 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015215{
Bram Moolenaar33570922005-01-25 22:26:29 +000015216 list_T *l;
15217 listitem_T *item, *item2;
15218 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015219 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015220 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015221 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015222 dict_T *d;
15223 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015224 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015225
Bram Moolenaar8c711452005-01-14 21:53:12 +000015226 if (argvars[0].v_type == VAR_DICT)
15227 {
15228 if (argvars[2].v_type != VAR_UNKNOWN)
15229 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015230 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015231 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015233 key = get_tv_string_chk(&argvars[1]);
15234 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015236 di = dict_find(d, key, -1);
15237 if (di == NULL)
15238 EMSG2(_(e_dictkey), key);
15239 else
15240 {
15241 *rettv = di->di_tv;
15242 init_tv(&di->di_tv);
15243 dictitem_remove(d, di);
15244 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015245 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015246 }
15247 }
15248 else if (argvars[0].v_type != VAR_LIST)
15249 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015250 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015251 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015253 int error = FALSE;
15254
15255 idx = get_tv_number_chk(&argvars[1], &error);
15256 if (error)
15257 ; /* type error: do nothing, errmsg already given */
15258 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015259 EMSGN(_(e_listidx), idx);
15260 else
15261 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015262 if (argvars[2].v_type == VAR_UNKNOWN)
15263 {
15264 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015265 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015266 *rettv = item->li_tv;
15267 vim_free(item);
15268 }
15269 else
15270 {
15271 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015272 end = get_tv_number_chk(&argvars[2], &error);
15273 if (error)
15274 ; /* type error: do nothing */
15275 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015276 EMSGN(_(e_listidx), end);
15277 else
15278 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015279 int cnt = 0;
15280
15281 for (li = item; li != NULL; li = li->li_next)
15282 {
15283 ++cnt;
15284 if (li == item2)
15285 break;
15286 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015287 if (li == NULL) /* didn't find "item2" after "item" */
15288 EMSG(_(e_invrange));
15289 else
15290 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015291 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015292 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015293 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015294 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015295 l->lv_first = item;
15296 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015297 item->li_prev = NULL;
15298 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015299 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015300 }
15301 }
15302 }
15303 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015304 }
15305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306}
15307
15308/*
15309 * "rename({from}, {to})" function
15310 */
15311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015312f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015313 typval_T *argvars;
15314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315{
15316 char_u buf[NUMBUFLEN];
15317
15318 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015319 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015321 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15322 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323}
15324
15325/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015326 * "repeat()" function
15327 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015328 static void
15329f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015330 typval_T *argvars;
15331 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015332{
15333 char_u *p;
15334 int n;
15335 int slen;
15336 int len;
15337 char_u *r;
15338 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015339
15340 n = get_tv_number(&argvars[1]);
15341 if (argvars[0].v_type == VAR_LIST)
15342 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015343 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015344 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015345 if (list_extend(rettv->vval.v_list,
15346 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015347 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015348 }
15349 else
15350 {
15351 p = get_tv_string(&argvars[0]);
15352 rettv->v_type = VAR_STRING;
15353 rettv->vval.v_string = NULL;
15354
15355 slen = (int)STRLEN(p);
15356 len = slen * n;
15357 if (len <= 0)
15358 return;
15359
15360 r = alloc(len + 1);
15361 if (r != NULL)
15362 {
15363 for (i = 0; i < n; i++)
15364 mch_memmove(r + i * slen, p, (size_t)slen);
15365 r[len] = NUL;
15366 }
15367
15368 rettv->vval.v_string = r;
15369 }
15370}
15371
15372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 * "resolve()" function
15374 */
15375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015376f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015377 typval_T *argvars;
15378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379{
15380 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015381#ifdef HAVE_READLINK
15382 char_u *buf = NULL;
15383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015385 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386#ifdef FEAT_SHORTCUT
15387 {
15388 char_u *v = NULL;
15389
15390 v = mch_resolve_shortcut(p);
15391 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015392 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015394 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 }
15396#else
15397# ifdef HAVE_READLINK
15398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 char_u *cpy;
15400 int len;
15401 char_u *remain = NULL;
15402 char_u *q;
15403 int is_relative_to_current = FALSE;
15404 int has_trailing_pathsep = FALSE;
15405 int limit = 100;
15406
15407 p = vim_strsave(p);
15408
15409 if (p[0] == '.' && (vim_ispathsep(p[1])
15410 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15411 is_relative_to_current = TRUE;
15412
15413 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015414 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015415 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015417 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419
15420 q = getnextcomp(p);
15421 if (*q != NUL)
15422 {
15423 /* Separate the first path component in "p", and keep the
15424 * remainder (beginning with the path separator). */
15425 remain = vim_strsave(q - 1);
15426 q[-1] = NUL;
15427 }
15428
Bram Moolenaard9462e32011-04-11 21:35:11 +020015429 buf = alloc(MAXPATHL + 1);
15430 if (buf == NULL)
15431 goto fail;
15432
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433 for (;;)
15434 {
15435 for (;;)
15436 {
15437 len = readlink((char *)p, (char *)buf, MAXPATHL);
15438 if (len <= 0)
15439 break;
15440 buf[len] = NUL;
15441
15442 if (limit-- == 0)
15443 {
15444 vim_free(p);
15445 vim_free(remain);
15446 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 goto fail;
15449 }
15450
15451 /* Ensure that the result will have a trailing path separator
15452 * if the argument has one. */
15453 if (remain == NULL && has_trailing_pathsep)
15454 add_pathsep(buf);
15455
15456 /* Separate the first path component in the link value and
15457 * concatenate the remainders. */
15458 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15459 if (*q != NUL)
15460 {
15461 if (remain == NULL)
15462 remain = vim_strsave(q - 1);
15463 else
15464 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015465 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466 if (cpy != NULL)
15467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468 vim_free(remain);
15469 remain = cpy;
15470 }
15471 }
15472 q[-1] = NUL;
15473 }
15474
15475 q = gettail(p);
15476 if (q > p && *q == NUL)
15477 {
15478 /* Ignore trailing path separator. */
15479 q[-1] = NUL;
15480 q = gettail(p);
15481 }
15482 if (q > p && !mch_isFullName(buf))
15483 {
15484 /* symlink is relative to directory of argument */
15485 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15486 if (cpy != NULL)
15487 {
15488 STRCPY(cpy, p);
15489 STRCPY(gettail(cpy), buf);
15490 vim_free(p);
15491 p = cpy;
15492 }
15493 }
15494 else
15495 {
15496 vim_free(p);
15497 p = vim_strsave(buf);
15498 }
15499 }
15500
15501 if (remain == NULL)
15502 break;
15503
15504 /* Append the first path component of "remain" to "p". */
15505 q = getnextcomp(remain + 1);
15506 len = q - remain - (*q != NUL);
15507 cpy = vim_strnsave(p, STRLEN(p) + len);
15508 if (cpy != NULL)
15509 {
15510 STRNCAT(cpy, remain, len);
15511 vim_free(p);
15512 p = cpy;
15513 }
15514 /* Shorten "remain". */
15515 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015516 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517 else
15518 {
15519 vim_free(remain);
15520 remain = NULL;
15521 }
15522 }
15523
15524 /* If the result is a relative path name, make it explicitly relative to
15525 * the current directory if and only if the argument had this form. */
15526 if (!vim_ispathsep(*p))
15527 {
15528 if (is_relative_to_current
15529 && *p != NUL
15530 && !(p[0] == '.'
15531 && (p[1] == NUL
15532 || vim_ispathsep(p[1])
15533 || (p[1] == '.'
15534 && (p[2] == NUL
15535 || vim_ispathsep(p[2]))))))
15536 {
15537 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015538 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539 if (cpy != NULL)
15540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541 vim_free(p);
15542 p = cpy;
15543 }
15544 }
15545 else if (!is_relative_to_current)
15546 {
15547 /* Strip leading "./". */
15548 q = p;
15549 while (q[0] == '.' && vim_ispathsep(q[1]))
15550 q += 2;
15551 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015552 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 }
15554 }
15555
15556 /* Ensure that the result will have no trailing path separator
15557 * if the argument had none. But keep "/" or "//". */
15558 if (!has_trailing_pathsep)
15559 {
15560 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015561 if (after_pathsep(p, q))
15562 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 }
15564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015565 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 }
15567# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015568 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569# endif
15570#endif
15571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015572 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573
15574#ifdef HAVE_READLINK
15575fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015576 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015578 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579}
15580
15581/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015582 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 */
15584 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015585f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015586 typval_T *argvars;
15587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588{
Bram Moolenaar33570922005-01-25 22:26:29 +000015589 list_T *l;
15590 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591
Bram Moolenaar0d660222005-01-07 21:51:51 +000015592 if (argvars[0].v_type != VAR_LIST)
15593 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015594 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015595 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015596 {
15597 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015598 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015599 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015600 while (li != NULL)
15601 {
15602 ni = li->li_prev;
15603 list_append(l, li);
15604 li = ni;
15605 }
15606 rettv->vval.v_list = l;
15607 rettv->v_type = VAR_LIST;
15608 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015609 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611}
15612
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015613#define SP_NOMOVE 0x01 /* don't move cursor */
15614#define SP_REPEAT 0x02 /* repeat to find outer pair */
15615#define SP_RETCOUNT 0x04 /* return matchcount */
15616#define SP_SETPCMARK 0x08 /* set previous context mark */
15617#define SP_START 0x10 /* accept match at start position */
15618#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15619#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015620
Bram Moolenaar33570922005-01-25 22:26:29 +000015621static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015622
15623/*
15624 * Get flags for a search function.
15625 * Possibly sets "p_ws".
15626 * Returns BACKWARD, FORWARD or zero (for an error).
15627 */
15628 static int
15629get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015630 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015631 int *flagsp;
15632{
15633 int dir = FORWARD;
15634 char_u *flags;
15635 char_u nbuf[NUMBUFLEN];
15636 int mask;
15637
15638 if (varp->v_type != VAR_UNKNOWN)
15639 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015640 flags = get_tv_string_buf_chk(varp, nbuf);
15641 if (flags == NULL)
15642 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015643 while (*flags != NUL)
15644 {
15645 switch (*flags)
15646 {
15647 case 'b': dir = BACKWARD; break;
15648 case 'w': p_ws = TRUE; break;
15649 case 'W': p_ws = FALSE; break;
15650 default: mask = 0;
15651 if (flagsp != NULL)
15652 switch (*flags)
15653 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015654 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015655 case 'e': mask = SP_END; break;
15656 case 'm': mask = SP_RETCOUNT; break;
15657 case 'n': mask = SP_NOMOVE; break;
15658 case 'p': mask = SP_SUBPAT; break;
15659 case 'r': mask = SP_REPEAT; break;
15660 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015661 }
15662 if (mask == 0)
15663 {
15664 EMSG2(_(e_invarg2), flags);
15665 dir = 0;
15666 }
15667 else
15668 *flagsp |= mask;
15669 }
15670 if (dir == 0)
15671 break;
15672 ++flags;
15673 }
15674 }
15675 return dir;
15676}
15677
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015679 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015681 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015682search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015683 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015684 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015685 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015686{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015687 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 char_u *pat;
15689 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015690 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 int save_p_ws = p_ws;
15692 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015693 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015694 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015695 proftime_T tm;
15696#ifdef FEAT_RELTIME
15697 long time_limit = 0;
15698#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015699 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015700 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015702 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015703 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015704 if (dir == 0)
15705 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015706 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015707 if (flags & SP_START)
15708 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015709 if (flags & SP_END)
15710 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015711
Bram Moolenaar76929292008-01-06 19:07:36 +000015712 /* Optional arguments: line number to stop searching and timeout. */
15713 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015714 {
15715 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15716 if (lnum_stop < 0)
15717 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015718#ifdef FEAT_RELTIME
15719 if (argvars[3].v_type != VAR_UNKNOWN)
15720 {
15721 time_limit = get_tv_number_chk(&argvars[3], NULL);
15722 if (time_limit < 0)
15723 goto theend;
15724 }
15725#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015726 }
15727
Bram Moolenaar76929292008-01-06 19:07:36 +000015728#ifdef FEAT_RELTIME
15729 /* Set the time limit, if there is one. */
15730 profile_setlimit(time_limit, &tm);
15731#endif
15732
Bram Moolenaar231334e2005-07-25 20:46:57 +000015733 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015734 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015735 * Check to make sure only those flags are set.
15736 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15737 * flags cannot be set. Check for that condition also.
15738 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015739 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015740 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015741 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015742 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015743 goto theend;
15744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015746 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015747 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015748 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015749 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015751 if (flags & SP_SUBPAT)
15752 retval = subpatnum;
15753 else
15754 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015755 if (flags & SP_SETPCMARK)
15756 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015758 if (match_pos != NULL)
15759 {
15760 /* Store the match cursor position */
15761 match_pos->lnum = pos.lnum;
15762 match_pos->col = pos.col + 1;
15763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764 /* "/$" will put the cursor after the end of the line, may need to
15765 * correct that here */
15766 check_cursor();
15767 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015768
15769 /* If 'n' flag is used: restore cursor position. */
15770 if (flags & SP_NOMOVE)
15771 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015772 else
15773 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015774theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015776
15777 return retval;
15778}
15779
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015780#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015781
15782/*
15783 * round() is not in C90, use ceil() or floor() instead.
15784 */
15785 float_T
15786vim_round(f)
15787 float_T f;
15788{
15789 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15790}
15791
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015792/*
15793 * "round({float})" function
15794 */
15795 static void
15796f_round(argvars, rettv)
15797 typval_T *argvars;
15798 typval_T *rettv;
15799{
15800 float_T f;
15801
15802 rettv->v_type = VAR_FLOAT;
15803 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015804 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015805 else
15806 rettv->vval.v_float = 0.0;
15807}
15808#endif
15809
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015810/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015811 * "screenattr()" function
15812 */
15813 static void
15814f_screenattr(argvars, rettv)
15815 typval_T *argvars UNUSED;
15816 typval_T *rettv;
15817{
15818 int row;
15819 int col;
15820 int c;
15821
15822 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15823 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15824 if (row < 0 || row >= screen_Rows
15825 || col < 0 || col >= screen_Columns)
15826 c = -1;
15827 else
15828 c = ScreenAttrs[LineOffset[row] + col];
15829 rettv->vval.v_number = c;
15830}
15831
15832/*
15833 * "screenchar()" function
15834 */
15835 static void
15836f_screenchar(argvars, rettv)
15837 typval_T *argvars UNUSED;
15838 typval_T *rettv;
15839{
15840 int row;
15841 int col;
15842 int off;
15843 int c;
15844
15845 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15846 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15847 if (row < 0 || row >= screen_Rows
15848 || col < 0 || col >= screen_Columns)
15849 c = -1;
15850 else
15851 {
15852 off = LineOffset[row] + col;
15853#ifdef FEAT_MBYTE
15854 if (enc_utf8 && ScreenLinesUC[off] != 0)
15855 c = ScreenLinesUC[off];
15856 else
15857#endif
15858 c = ScreenLines[off];
15859 }
15860 rettv->vval.v_number = c;
15861}
15862
15863/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015864 * "screencol()" function
15865 *
15866 * First column is 1 to be consistent with virtcol().
15867 */
15868 static void
15869f_screencol(argvars, rettv)
15870 typval_T *argvars UNUSED;
15871 typval_T *rettv;
15872{
15873 rettv->vval.v_number = screen_screencol() + 1;
15874}
15875
15876/*
15877 * "screenrow()" function
15878 */
15879 static void
15880f_screenrow(argvars, rettv)
15881 typval_T *argvars UNUSED;
15882 typval_T *rettv;
15883{
15884 rettv->vval.v_number = screen_screenrow() + 1;
15885}
15886
15887/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015888 * "search()" function
15889 */
15890 static void
15891f_search(argvars, rettv)
15892 typval_T *argvars;
15893 typval_T *rettv;
15894{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015895 int flags = 0;
15896
15897 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898}
15899
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015901 * "searchdecl()" function
15902 */
15903 static void
15904f_searchdecl(argvars, rettv)
15905 typval_T *argvars;
15906 typval_T *rettv;
15907{
15908 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015909 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015910 int error = FALSE;
15911 char_u *name;
15912
15913 rettv->vval.v_number = 1; /* default: FAIL */
15914
15915 name = get_tv_string_chk(&argvars[0]);
15916 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015917 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015918 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015919 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15920 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15921 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015922 if (!error && name != NULL)
15923 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015924 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015925}
15926
15927/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015928 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015930 static int
15931searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015932 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015933 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934{
15935 char_u *spat, *mpat, *epat;
15936 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015937 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 int dir;
15939 int flags = 0;
15940 char_u nbuf1[NUMBUFLEN];
15941 char_u nbuf2[NUMBUFLEN];
15942 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015943 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015944 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015945 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015946
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015948 spat = get_tv_string_chk(&argvars[0]);
15949 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15950 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15951 if (spat == NULL || mpat == NULL || epat == NULL)
15952 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015953
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954 /* Handle the optional fourth argument: flags */
15955 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015956 if (dir == 0)
15957 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015958
15959 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015960 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15961 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015962 if ((flags & (SP_END | SP_SUBPAT)) != 0
15963 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015964 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015965 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015966 goto theend;
15967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015969 /* Using 'r' implies 'W', otherwise it doesn't work. */
15970 if (flags & SP_REPEAT)
15971 p_ws = FALSE;
15972
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015973 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015974 if (argvars[3].v_type == VAR_UNKNOWN
15975 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015976 skip = (char_u *)"";
15977 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015978 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015979 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015980 if (argvars[5].v_type != VAR_UNKNOWN)
15981 {
15982 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15983 if (lnum_stop < 0)
15984 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015985#ifdef FEAT_RELTIME
15986 if (argvars[6].v_type != VAR_UNKNOWN)
15987 {
15988 time_limit = get_tv_number_chk(&argvars[6], NULL);
15989 if (time_limit < 0)
15990 goto theend;
15991 }
15992#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015993 }
15994 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015995 if (skip == NULL)
15996 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015998 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015999 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016000
16001theend:
16002 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016003
16004 return retval;
16005}
16006
16007/*
16008 * "searchpair()" function
16009 */
16010 static void
16011f_searchpair(argvars, rettv)
16012 typval_T *argvars;
16013 typval_T *rettv;
16014{
16015 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16016}
16017
16018/*
16019 * "searchpairpos()" function
16020 */
16021 static void
16022f_searchpairpos(argvars, rettv)
16023 typval_T *argvars;
16024 typval_T *rettv;
16025{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016026 pos_T match_pos;
16027 int lnum = 0;
16028 int col = 0;
16029
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016030 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016031 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016032
16033 if (searchpair_cmn(argvars, &match_pos) > 0)
16034 {
16035 lnum = match_pos.lnum;
16036 col = match_pos.col;
16037 }
16038
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016039 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16040 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016041}
16042
16043/*
16044 * Search for a start/middle/end thing.
16045 * Used by searchpair(), see its documentation for the details.
16046 * Returns 0 or -1 for no match,
16047 */
16048 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016049do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16050 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016051 char_u *spat; /* start pattern */
16052 char_u *mpat; /* middle pattern */
16053 char_u *epat; /* end pattern */
16054 int dir; /* BACKWARD or FORWARD */
16055 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016056 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016057 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016058 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016059 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016060{
16061 char_u *save_cpo;
16062 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16063 long retval = 0;
16064 pos_T pos;
16065 pos_T firstpos;
16066 pos_T foundpos;
16067 pos_T save_cursor;
16068 pos_T save_pos;
16069 int n;
16070 int r;
16071 int nest = 1;
16072 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016073 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016074 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016075
16076 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16077 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016078 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016079
Bram Moolenaar76929292008-01-06 19:07:36 +000016080#ifdef FEAT_RELTIME
16081 /* Set the time limit, if there is one. */
16082 profile_setlimit(time_limit, &tm);
16083#endif
16084
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016085 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16086 * start/middle/end (pat3, for the top pair). */
16087 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16088 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16089 if (pat2 == NULL || pat3 == NULL)
16090 goto theend;
16091 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16092 if (*mpat == NUL)
16093 STRCPY(pat3, pat2);
16094 else
16095 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16096 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016097 if (flags & SP_START)
16098 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016099
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 save_cursor = curwin->w_cursor;
16101 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016102 clearpos(&firstpos);
16103 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 pat = pat3;
16105 for (;;)
16106 {
16107 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016108 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16110 /* didn't find it or found the first match again: FAIL */
16111 break;
16112
16113 if (firstpos.lnum == 0)
16114 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016115 if (equalpos(pos, foundpos))
16116 {
16117 /* Found the same position again. Can happen with a pattern that
16118 * has "\zs" at the end and searching backwards. Advance one
16119 * character and try again. */
16120 if (dir == BACKWARD)
16121 decl(&pos);
16122 else
16123 incl(&pos);
16124 }
16125 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016127 /* clear the start flag to avoid getting stuck here */
16128 options &= ~SEARCH_START;
16129
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130 /* If the skip pattern matches, ignore this match. */
16131 if (*skip != NUL)
16132 {
16133 save_pos = curwin->w_cursor;
16134 curwin->w_cursor = pos;
16135 r = eval_to_bool(skip, &err, NULL, FALSE);
16136 curwin->w_cursor = save_pos;
16137 if (err)
16138 {
16139 /* Evaluating {skip} caused an error, break here. */
16140 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016141 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 break;
16143 }
16144 if (r)
16145 continue;
16146 }
16147
16148 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16149 {
16150 /* Found end when searching backwards or start when searching
16151 * forward: nested pair. */
16152 ++nest;
16153 pat = pat2; /* nested, don't search for middle */
16154 }
16155 else
16156 {
16157 /* Found end when searching forward or start when searching
16158 * backward: end of (nested) pair; or found middle in outer pair. */
16159 if (--nest == 1)
16160 pat = pat3; /* outer level, search for middle */
16161 }
16162
16163 if (nest == 0)
16164 {
16165 /* Found the match: return matchcount or line number. */
16166 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016167 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016169 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016170 if (flags & SP_SETPCMARK)
16171 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172 curwin->w_cursor = pos;
16173 if (!(flags & SP_REPEAT))
16174 break;
16175 nest = 1; /* search for next unmatched */
16176 }
16177 }
16178
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016179 if (match_pos != NULL)
16180 {
16181 /* Store the match cursor position */
16182 match_pos->lnum = curwin->w_cursor.lnum;
16183 match_pos->col = curwin->w_cursor.col + 1;
16184 }
16185
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016187 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188 curwin->w_cursor = save_cursor;
16189
16190theend:
16191 vim_free(pat2);
16192 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016193 if (p_cpo == empty_option)
16194 p_cpo = save_cpo;
16195 else
16196 /* Darn, evaluating the {skip} expression changed the value. */
16197 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016198
16199 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200}
16201
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016202/*
16203 * "searchpos()" function
16204 */
16205 static void
16206f_searchpos(argvars, rettv)
16207 typval_T *argvars;
16208 typval_T *rettv;
16209{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016210 pos_T match_pos;
16211 int lnum = 0;
16212 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016213 int n;
16214 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016215
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016216 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016217 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016218
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016219 n = search_cmn(argvars, &match_pos, &flags);
16220 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016221 {
16222 lnum = match_pos.lnum;
16223 col = match_pos.col;
16224 }
16225
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016226 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16227 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016228 if (flags & SP_SUBPAT)
16229 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016230}
16231
16232
Bram Moolenaar0d660222005-01-07 21:51:51 +000016233 static void
16234f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016235 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016238#ifdef FEAT_CLIENTSERVER
16239 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016240 char_u *server = get_tv_string_chk(&argvars[0]);
16241 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242
Bram Moolenaar0d660222005-01-07 21:51:51 +000016243 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016244 if (server == NULL || reply == NULL)
16245 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016246 if (check_restricted() || check_secure())
16247 return;
16248# ifdef FEAT_X11
16249 if (check_connection() == FAIL)
16250 return;
16251# endif
16252
16253 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016255 EMSG(_("E258: Unable to send to client"));
16256 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016258 rettv->vval.v_number = 0;
16259#else
16260 rettv->vval.v_number = -1;
16261#endif
16262}
16263
Bram Moolenaar0d660222005-01-07 21:51:51 +000016264 static void
16265f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016266 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016267 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016268{
16269 char_u *r = NULL;
16270
16271#ifdef FEAT_CLIENTSERVER
16272# ifdef WIN32
16273 r = serverGetVimNames();
16274# else
16275 make_connection();
16276 if (X_DISPLAY != NULL)
16277 r = serverGetVimNames(X_DISPLAY);
16278# endif
16279#endif
16280 rettv->v_type = VAR_STRING;
16281 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282}
16283
16284/*
16285 * "setbufvar()" function
16286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016288f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016289 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016290 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291{
16292 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016295 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 char_u nbuf[NUMBUFLEN];
16297
16298 if (check_restricted() || check_secure())
16299 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016300 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16301 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016302 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303 varp = &argvars[2];
16304
16305 if (buf != NULL && varname != NULL && varp != NULL)
16306 {
16307 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309
16310 if (*varname == '&')
16311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016312 long numval;
16313 char_u *strval;
16314 int error = FALSE;
16315
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016317 numval = get_tv_number_chk(varp, &error);
16318 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016319 if (!error && strval != NULL)
16320 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 }
16322 else
16323 {
16324 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16325 if (bufvarname != NULL)
16326 {
16327 STRCPY(bufvarname, "b:");
16328 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016329 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 vim_free(bufvarname);
16331 }
16332 }
16333
16334 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337}
16338
16339/*
16340 * "setcmdpos()" function
16341 */
16342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016343f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016344 typval_T *argvars;
16345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016347 int pos = (int)get_tv_number(&argvars[0]) - 1;
16348
16349 if (pos >= 0)
16350 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351}
16352
16353/*
16354 * "setline()" function
16355 */
16356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016357f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016358 typval_T *argvars;
16359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360{
16361 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016362 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016363 list_T *l = NULL;
16364 listitem_T *li = NULL;
16365 long added = 0;
16366 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016368 lnum = get_tv_lnum(&argvars[0]);
16369 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016371 l = argvars[1].vval.v_list;
16372 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016374 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016375 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016376
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016377 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016378 for (;;)
16379 {
16380 if (l != NULL)
16381 {
16382 /* list argument, get next string */
16383 if (li == NULL)
16384 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016385 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016386 li = li->li_next;
16387 }
16388
16389 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016390 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016391 break;
16392 if (lnum <= curbuf->b_ml.ml_line_count)
16393 {
16394 /* existing line, replace it */
16395 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16396 {
16397 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016398 if (lnum == curwin->w_cursor.lnum)
16399 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016400 rettv->vval.v_number = 0; /* OK */
16401 }
16402 }
16403 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16404 {
16405 /* lnum is one past the last line, append the line */
16406 ++added;
16407 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16408 rettv->vval.v_number = 0; /* OK */
16409 }
16410
16411 if (l == NULL) /* only one string argument */
16412 break;
16413 ++lnum;
16414 }
16415
16416 if (added > 0)
16417 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418}
16419
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016420static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16421
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016423 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016424 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016425 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016426set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016427 win_T *wp UNUSED;
16428 typval_T *list_arg UNUSED;
16429 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016430 typval_T *rettv;
16431{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016432#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016433 char_u *act;
16434 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016435#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016436
Bram Moolenaar2641f772005-03-25 21:58:17 +000016437 rettv->vval.v_number = -1;
16438
16439#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016440 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016441 EMSG(_(e_listreq));
16442 else
16443 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016444 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016445
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016446 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016447 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016448 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016449 if (act == NULL)
16450 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016451 if (*act == 'a' || *act == 'r')
16452 action = *act;
16453 }
16454
Bram Moolenaar81484f42012-12-05 15:16:47 +010016455 if (l != NULL && set_errorlist(wp, l, action,
16456 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016457 rettv->vval.v_number = 0;
16458 }
16459#endif
16460}
16461
16462/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016463 * "setloclist()" function
16464 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016465 static void
16466f_setloclist(argvars, rettv)
16467 typval_T *argvars;
16468 typval_T *rettv;
16469{
16470 win_T *win;
16471
16472 rettv->vval.v_number = -1;
16473
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016474 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016475 if (win != NULL)
16476 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16477}
16478
16479/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016480 * "setmatches()" function
16481 */
16482 static void
16483f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016484 typval_T *argvars UNUSED;
16485 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016486{
16487#ifdef FEAT_SEARCH_EXTRA
16488 list_T *l;
16489 listitem_T *li;
16490 dict_T *d;
16491
16492 rettv->vval.v_number = -1;
16493 if (argvars[0].v_type != VAR_LIST)
16494 {
16495 EMSG(_(e_listreq));
16496 return;
16497 }
16498 if ((l = argvars[0].vval.v_list) != NULL)
16499 {
16500
16501 /* To some extent make sure that we are dealing with a list from
16502 * "getmatches()". */
16503 li = l->lv_first;
16504 while (li != NULL)
16505 {
16506 if (li->li_tv.v_type != VAR_DICT
16507 || (d = li->li_tv.vval.v_dict) == NULL)
16508 {
16509 EMSG(_(e_invarg));
16510 return;
16511 }
16512 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16513 && dict_find(d, (char_u *)"pattern", -1) != NULL
16514 && dict_find(d, (char_u *)"priority", -1) != NULL
16515 && dict_find(d, (char_u *)"id", -1) != NULL))
16516 {
16517 EMSG(_(e_invarg));
16518 return;
16519 }
16520 li = li->li_next;
16521 }
16522
16523 clear_matches(curwin);
16524 li = l->lv_first;
16525 while (li != NULL)
16526 {
16527 d = li->li_tv.vval.v_dict;
16528 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16529 get_dict_string(d, (char_u *)"pattern", FALSE),
16530 (int)get_dict_number(d, (char_u *)"priority"),
16531 (int)get_dict_number(d, (char_u *)"id"));
16532 li = li->li_next;
16533 }
16534 rettv->vval.v_number = 0;
16535 }
16536#endif
16537}
16538
16539/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016540 * "setpos()" function
16541 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016542 static void
16543f_setpos(argvars, rettv)
16544 typval_T *argvars;
16545 typval_T *rettv;
16546{
16547 pos_T pos;
16548 int fnum;
16549 char_u *name;
16550
Bram Moolenaar08250432008-02-13 11:42:46 +000016551 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016552 name = get_tv_string_chk(argvars);
16553 if (name != NULL)
16554 {
16555 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16556 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016557 if (--pos.col < 0)
16558 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016559 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016560 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016561 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016562 if (fnum == curbuf->b_fnum)
16563 {
16564 curwin->w_cursor = pos;
16565 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016566 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016567 }
16568 else
16569 EMSG(_(e_invarg));
16570 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016571 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16572 {
16573 /* set mark */
16574 if (setmark_pos(name[1], &pos, fnum) == OK)
16575 rettv->vval.v_number = 0;
16576 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016577 else
16578 EMSG(_(e_invarg));
16579 }
16580 }
16581}
16582
16583/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016584 * "setqflist()" function
16585 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016586 static void
16587f_setqflist(argvars, rettv)
16588 typval_T *argvars;
16589 typval_T *rettv;
16590{
16591 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16592}
16593
16594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595 * "setreg()" function
16596 */
16597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016598f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016599 typval_T *argvars;
16600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016601{
16602 int regname;
16603 char_u *strregname;
16604 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016605 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606 int append;
16607 char_u yank_type;
16608 long block_len;
16609
16610 block_len = -1;
16611 yank_type = MAUTO;
16612 append = FALSE;
16613
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016614 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016615 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016617 if (strregname == NULL)
16618 return; /* type error; errmsg already given */
16619 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620 if (regname == 0 || regname == '@')
16621 regname = '"';
16622 else if (regname == '=')
16623 return;
16624
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016625 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016627 stropt = get_tv_string_chk(&argvars[2]);
16628 if (stropt == NULL)
16629 return; /* type error */
16630 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631 switch (*stropt)
16632 {
16633 case 'a': case 'A': /* append */
16634 append = TRUE;
16635 break;
16636 case 'v': case 'c': /* character-wise selection */
16637 yank_type = MCHAR;
16638 break;
16639 case 'V': case 'l': /* line-wise selection */
16640 yank_type = MLINE;
16641 break;
16642#ifdef FEAT_VISUAL
16643 case 'b': case Ctrl_V: /* block-wise selection */
16644 yank_type = MBLOCK;
16645 if (VIM_ISDIGIT(stropt[1]))
16646 {
16647 ++stropt;
16648 block_len = getdigits(&stropt) - 1;
16649 --stropt;
16650 }
16651 break;
16652#endif
16653 }
16654 }
16655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016656 strval = get_tv_string_chk(&argvars[1]);
16657 if (strval != NULL)
16658 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016660 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661}
16662
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016663/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016664 * "settabvar()" function
16665 */
16666 static void
16667f_settabvar(argvars, rettv)
16668 typval_T *argvars;
16669 typval_T *rettv;
16670{
16671 tabpage_T *save_curtab;
16672 char_u *varname, *tabvarname;
16673 typval_T *varp;
16674 tabpage_T *tp;
16675
16676 rettv->vval.v_number = 0;
16677
16678 if (check_restricted() || check_secure())
16679 return;
16680
16681 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16682 varname = get_tv_string_chk(&argvars[1]);
16683 varp = &argvars[2];
16684
16685 if (tp != NULL && varname != NULL && varp != NULL)
16686 {
16687 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016688 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016689
16690 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16691 if (tabvarname != NULL)
16692 {
16693 STRCPY(tabvarname, "t:");
16694 STRCPY(tabvarname + 2, varname);
16695 set_var(tabvarname, varp, TRUE);
16696 vim_free(tabvarname);
16697 }
16698
16699 /* Restore current tabpage */
16700 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016701 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016702 }
16703}
16704
16705/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016706 * "settabwinvar()" function
16707 */
16708 static void
16709f_settabwinvar(argvars, rettv)
16710 typval_T *argvars;
16711 typval_T *rettv;
16712{
16713 setwinvar(argvars, rettv, 1);
16714}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715
16716/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016717 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016720f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016721 typval_T *argvars;
16722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016724 setwinvar(argvars, rettv, 0);
16725}
16726
16727/*
16728 * "setwinvar()" and "settabwinvar()" functions
16729 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016730
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016731 static void
16732setwinvar(argvars, rettv, off)
16733 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016734 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016735 int off;
16736{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737 win_T *win;
16738#ifdef FEAT_WINDOWS
16739 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016740 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741#endif
16742 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016743 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016745 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746
16747 if (check_restricted() || check_secure())
16748 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016749
16750#ifdef FEAT_WINDOWS
16751 if (off == 1)
16752 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16753 else
16754 tp = curtab;
16755#endif
16756 win = find_win_by_nr(&argvars[off], tp);
16757 varname = get_tv_string_chk(&argvars[off + 1]);
16758 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759
16760 if (win != NULL && varname != NULL && varp != NULL)
16761 {
16762#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016763 if (switch_win(&save_curwin, &save_curtab, win, tp) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016764 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765#endif
16766
16767 if (*varname == '&')
16768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016769 long numval;
16770 char_u *strval;
16771 int error = FALSE;
16772
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016774 numval = get_tv_number_chk(varp, &error);
16775 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016776 if (!error && strval != NULL)
16777 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 }
16779 else
16780 {
16781 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16782 if (winvarname != NULL)
16783 {
16784 STRCPY(winvarname, "w:");
16785 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016786 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 vim_free(winvarname);
16788 }
16789 }
16790
16791#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016792 restore_win(save_curwin, save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016793#endif
16794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795}
16796
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016797#ifdef FEAT_CRYPT
16798/*
16799 * "sha256({string})" function
16800 */
16801 static void
16802f_sha256(argvars, rettv)
16803 typval_T *argvars;
16804 typval_T *rettv;
16805{
16806 char_u *p;
16807
16808 p = get_tv_string(&argvars[0]);
16809 rettv->vval.v_string = vim_strsave(
16810 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16811 rettv->v_type = VAR_STRING;
16812}
16813#endif /* FEAT_CRYPT */
16814
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016816 * "shellescape({string})" function
16817 */
16818 static void
16819f_shellescape(argvars, rettv)
16820 typval_T *argvars;
16821 typval_T *rettv;
16822{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016823 rettv->vval.v_string = vim_strsave_shellescape(
16824 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016825 rettv->v_type = VAR_STRING;
16826}
16827
16828/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016829 * shiftwidth() function
16830 */
16831 static void
16832f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016833 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016834 typval_T *rettv;
16835{
16836 rettv->vval.v_number = get_sw_value();
16837}
16838
16839/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016840 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 */
16842 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016843f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016844 typval_T *argvars;
16845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016847 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848
Bram Moolenaar0d660222005-01-07 21:51:51 +000016849 p = get_tv_string(&argvars[0]);
16850 rettv->vval.v_string = vim_strsave(p);
16851 simplify_filename(rettv->vval.v_string); /* simplify in place */
16852 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853}
16854
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016855#ifdef FEAT_FLOAT
16856/*
16857 * "sin()" function
16858 */
16859 static void
16860f_sin(argvars, rettv)
16861 typval_T *argvars;
16862 typval_T *rettv;
16863{
16864 float_T f;
16865
16866 rettv->v_type = VAR_FLOAT;
16867 if (get_float_arg(argvars, &f) == OK)
16868 rettv->vval.v_float = sin(f);
16869 else
16870 rettv->vval.v_float = 0.0;
16871}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016872
16873/*
16874 * "sinh()" function
16875 */
16876 static void
16877f_sinh(argvars, rettv)
16878 typval_T *argvars;
16879 typval_T *rettv;
16880{
16881 float_T f;
16882
16883 rettv->v_type = VAR_FLOAT;
16884 if (get_float_arg(argvars, &f) == OK)
16885 rettv->vval.v_float = sinh(f);
16886 else
16887 rettv->vval.v_float = 0.0;
16888}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016889#endif
16890
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891static int
16892#ifdef __BORLANDC__
16893 _RTLENTRYF
16894#endif
16895 item_compare __ARGS((const void *s1, const void *s2));
16896static int
16897#ifdef __BORLANDC__
16898 _RTLENTRYF
16899#endif
16900 item_compare2 __ARGS((const void *s1, const void *s2));
16901
16902static int item_compare_ic;
16903static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016904static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016905static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906#define ITEM_COMPARE_FAIL 999
16907
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016909 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016911 static int
16912#ifdef __BORLANDC__
16913_RTLENTRYF
16914#endif
16915item_compare(s1, s2)
16916 const void *s1;
16917 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016919 char_u *p1, *p2;
16920 char_u *tofree1, *tofree2;
16921 int res;
16922 char_u numbuf1[NUMBUFLEN];
16923 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016925 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16926 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016927 if (p1 == NULL)
16928 p1 = (char_u *)"";
16929 if (p2 == NULL)
16930 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016931 if (item_compare_ic)
16932 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016934 res = STRCMP(p1, p2);
16935 vim_free(tofree1);
16936 vim_free(tofree2);
16937 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938}
16939
16940 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016941#ifdef __BORLANDC__
16942_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016944item_compare2(s1, s2)
16945 const void *s1;
16946 const void *s2;
16947{
16948 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016949 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016950 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016951 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016953 /* shortcut after failure in previous call; compare all items equal */
16954 if (item_compare_func_err)
16955 return 0;
16956
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016957 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16958 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016959 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16960 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016961
16962 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016963 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016964 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16965 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966 clear_tv(&argv[0]);
16967 clear_tv(&argv[1]);
16968
16969 if (res == FAIL)
16970 res = ITEM_COMPARE_FAIL;
16971 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016972 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16973 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016974 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975 clear_tv(&rettv);
16976 return res;
16977}
16978
16979/*
16980 * "sort({list})" function
16981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016984 typval_T *argvars;
16985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986{
Bram Moolenaar33570922005-01-25 22:26:29 +000016987 list_T *l;
16988 listitem_T *li;
16989 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016990 long len;
16991 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992
Bram Moolenaar0d660222005-01-07 21:51:51 +000016993 if (argvars[0].v_type != VAR_LIST)
16994 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995 else
16996 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016998 if (l == NULL || tv_check_lock(l->lv_lock,
16999 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000 return;
17001 rettv->vval.v_list = l;
17002 rettv->v_type = VAR_LIST;
17003 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004
Bram Moolenaar0d660222005-01-07 21:51:51 +000017005 len = list_len(l);
17006 if (len <= 1)
17007 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008
Bram Moolenaar0d660222005-01-07 21:51:51 +000017009 item_compare_ic = FALSE;
17010 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017011 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017012 if (argvars[1].v_type != VAR_UNKNOWN)
17013 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017014 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017015 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017016 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017017 else
17018 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017019 int error = FALSE;
17020
17021 i = get_tv_number_chk(&argvars[1], &error);
17022 if (error)
17023 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017024 if (i == 1)
17025 item_compare_ic = TRUE;
17026 else
17027 item_compare_func = get_tv_string(&argvars[1]);
17028 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017029
17030 if (argvars[2].v_type != VAR_UNKNOWN)
17031 {
17032 /* optional third argument: {dict} */
17033 if (argvars[2].v_type != VAR_DICT)
17034 {
17035 EMSG(_(e_dictreq));
17036 return;
17037 }
17038 item_compare_selfdict = argvars[2].vval.v_dict;
17039 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041
Bram Moolenaar0d660222005-01-07 21:51:51 +000017042 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017043 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017044 if (ptrs == NULL)
17045 return;
17046 i = 0;
17047 for (li = l->lv_first; li != NULL; li = li->li_next)
17048 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017050 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017051 /* test the compare function */
17052 if (item_compare_func != NULL
17053 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17054 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017055 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057 {
17058 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017059 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017060 item_compare_func == NULL ? item_compare : item_compare2);
17061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017062 if (!item_compare_func_err)
17063 {
17064 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017065 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017066 l->lv_len = 0;
17067 for (i = 0; i < len; ++i)
17068 list_append(l, ptrs[i]);
17069 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017070 }
17071
17072 vim_free(ptrs);
17073 }
17074}
17075
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017076/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017077 * "soundfold({word})" function
17078 */
17079 static void
17080f_soundfold(argvars, rettv)
17081 typval_T *argvars;
17082 typval_T *rettv;
17083{
17084 char_u *s;
17085
17086 rettv->v_type = VAR_STRING;
17087 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017088#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017089 rettv->vval.v_string = eval_soundfold(s);
17090#else
17091 rettv->vval.v_string = vim_strsave(s);
17092#endif
17093}
17094
17095/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017096 * "spellbadword()" function
17097 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017098 static void
17099f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017100 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017101 typval_T *rettv;
17102{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017103 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017104 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017105 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017106
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017107 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017108 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017109
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017110#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017111 if (argvars[0].v_type == VAR_UNKNOWN)
17112 {
17113 /* Find the start and length of the badly spelled word. */
17114 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17115 if (len != 0)
17116 word = ml_get_cursor();
17117 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017118 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017119 {
17120 char_u *str = get_tv_string_chk(&argvars[0]);
17121 int capcol = -1;
17122
17123 if (str != NULL)
17124 {
17125 /* Check the argument for spelling. */
17126 while (*str != NUL)
17127 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017128 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017129 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017130 {
17131 word = str;
17132 break;
17133 }
17134 str += len;
17135 }
17136 }
17137 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017138#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017139
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017140 list_append_string(rettv->vval.v_list, word, len);
17141 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017142 attr == HLF_SPB ? "bad" :
17143 attr == HLF_SPR ? "rare" :
17144 attr == HLF_SPL ? "local" :
17145 attr == HLF_SPC ? "caps" :
17146 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017147}
17148
17149/*
17150 * "spellsuggest()" function
17151 */
17152 static void
17153f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017154 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017155 typval_T *rettv;
17156{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017157#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017158 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017159 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017160 int maxcount;
17161 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017162 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017163 listitem_T *li;
17164 int need_capital = FALSE;
17165#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017166
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017167 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017168 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017169
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017170#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017171 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017172 {
17173 str = get_tv_string(&argvars[0]);
17174 if (argvars[1].v_type != VAR_UNKNOWN)
17175 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017176 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017177 if (maxcount <= 0)
17178 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017179 if (argvars[2].v_type != VAR_UNKNOWN)
17180 {
17181 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17182 if (typeerr)
17183 return;
17184 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017185 }
17186 else
17187 maxcount = 25;
17188
Bram Moolenaar4770d092006-01-12 23:22:24 +000017189 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017190
17191 for (i = 0; i < ga.ga_len; ++i)
17192 {
17193 str = ((char_u **)ga.ga_data)[i];
17194
17195 li = listitem_alloc();
17196 if (li == NULL)
17197 vim_free(str);
17198 else
17199 {
17200 li->li_tv.v_type = VAR_STRING;
17201 li->li_tv.v_lock = 0;
17202 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017203 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017204 }
17205 }
17206 ga_clear(&ga);
17207 }
17208#endif
17209}
17210
Bram Moolenaar0d660222005-01-07 21:51:51 +000017211 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017212f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017213 typval_T *argvars;
17214 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017215{
17216 char_u *str;
17217 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017218 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017219 regmatch_T regmatch;
17220 char_u patbuf[NUMBUFLEN];
17221 char_u *save_cpo;
17222 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017223 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017224 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017225 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017226
17227 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17228 save_cpo = p_cpo;
17229 p_cpo = (char_u *)"";
17230
17231 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017232 if (argvars[1].v_type != VAR_UNKNOWN)
17233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017234 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17235 if (pat == NULL)
17236 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017237 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017238 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017239 }
17240 if (pat == NULL || *pat == NUL)
17241 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017242
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017243 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017245 if (typeerr)
17246 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017247
Bram Moolenaar0d660222005-01-07 21:51:51 +000017248 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17249 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017251 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017252 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017253 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017254 if (*str == NUL)
17255 match = FALSE; /* empty item at the end */
17256 else
17257 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017258 if (match)
17259 end = regmatch.startp[0];
17260 else
17261 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017262 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17263 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017264 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017265 if (list_append_string(rettv->vval.v_list, str,
17266 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017267 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017268 }
17269 if (!match)
17270 break;
17271 /* Advance to just after the match. */
17272 if (regmatch.endp[0] > str)
17273 col = 0;
17274 else
17275 {
17276 /* Don't get stuck at the same match. */
17277#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017278 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017279#else
17280 col = 1;
17281#endif
17282 }
17283 str = regmatch.endp[0];
17284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285
Bram Moolenaar473de612013-06-08 18:19:48 +020017286 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288
Bram Moolenaar0d660222005-01-07 21:51:51 +000017289 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290}
17291
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017292#ifdef FEAT_FLOAT
17293/*
17294 * "sqrt()" function
17295 */
17296 static void
17297f_sqrt(argvars, rettv)
17298 typval_T *argvars;
17299 typval_T *rettv;
17300{
17301 float_T f;
17302
17303 rettv->v_type = VAR_FLOAT;
17304 if (get_float_arg(argvars, &f) == OK)
17305 rettv->vval.v_float = sqrt(f);
17306 else
17307 rettv->vval.v_float = 0.0;
17308}
17309
17310/*
17311 * "str2float()" function
17312 */
17313 static void
17314f_str2float(argvars, rettv)
17315 typval_T *argvars;
17316 typval_T *rettv;
17317{
17318 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17319
17320 if (*p == '+')
17321 p = skipwhite(p + 1);
17322 (void)string2float(p, &rettv->vval.v_float);
17323 rettv->v_type = VAR_FLOAT;
17324}
17325#endif
17326
Bram Moolenaar2c932302006-03-18 21:42:09 +000017327/*
17328 * "str2nr()" function
17329 */
17330 static void
17331f_str2nr(argvars, rettv)
17332 typval_T *argvars;
17333 typval_T *rettv;
17334{
17335 int base = 10;
17336 char_u *p;
17337 long n;
17338
17339 if (argvars[1].v_type != VAR_UNKNOWN)
17340 {
17341 base = get_tv_number(&argvars[1]);
17342 if (base != 8 && base != 10 && base != 16)
17343 {
17344 EMSG(_(e_invarg));
17345 return;
17346 }
17347 }
17348
17349 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017350 if (*p == '+')
17351 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017352 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17353 rettv->vval.v_number = n;
17354}
17355
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356#ifdef HAVE_STRFTIME
17357/*
17358 * "strftime({format}[, {time}])" function
17359 */
17360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017361f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017362 typval_T *argvars;
17363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364{
17365 char_u result_buf[256];
17366 struct tm *curtime;
17367 time_t seconds;
17368 char_u *p;
17369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017370 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017372 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017373 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374 seconds = time(NULL);
17375 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017376 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377 curtime = localtime(&seconds);
17378 /* MSVC returns NULL for an invalid value of seconds. */
17379 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017380 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 else
17382 {
17383# ifdef FEAT_MBYTE
17384 vimconv_T conv;
17385 char_u *enc;
17386
17387 conv.vc_type = CONV_NONE;
17388 enc = enc_locale();
17389 convert_setup(&conv, p_enc, enc);
17390 if (conv.vc_type != CONV_NONE)
17391 p = string_convert(&conv, p, NULL);
17392# endif
17393 if (p != NULL)
17394 (void)strftime((char *)result_buf, sizeof(result_buf),
17395 (char *)p, curtime);
17396 else
17397 result_buf[0] = NUL;
17398
17399# ifdef FEAT_MBYTE
17400 if (conv.vc_type != CONV_NONE)
17401 vim_free(p);
17402 convert_setup(&conv, enc, p_enc);
17403 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017404 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 else
17406# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017407 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408
17409# ifdef FEAT_MBYTE
17410 /* Release conversion descriptors */
17411 convert_setup(&conv, NULL, NULL);
17412 vim_free(enc);
17413# endif
17414 }
17415}
17416#endif
17417
17418/*
17419 * "stridx()" function
17420 */
17421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017422f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017423 typval_T *argvars;
17424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425{
17426 char_u buf[NUMBUFLEN];
17427 char_u *needle;
17428 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017429 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017431 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017433 needle = get_tv_string_chk(&argvars[1]);
17434 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017435 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017436 if (needle == NULL || haystack == NULL)
17437 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438
Bram Moolenaar33570922005-01-25 22:26:29 +000017439 if (argvars[2].v_type != VAR_UNKNOWN)
17440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017441 int error = FALSE;
17442
17443 start_idx = get_tv_number_chk(&argvars[2], &error);
17444 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017445 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017446 if (start_idx >= 0)
17447 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017448 }
17449
17450 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17451 if (pos != NULL)
17452 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453}
17454
17455/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017456 * "string()" function
17457 */
17458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017459f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017460 typval_T *argvars;
17461 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017462{
17463 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017464 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017466 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017467 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017468 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017469 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017470 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471}
17472
17473/*
17474 * "strlen()" function
17475 */
17476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017477f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017478 typval_T *argvars;
17479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017481 rettv->vval.v_number = (varnumber_T)(STRLEN(
17482 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483}
17484
17485/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017486 * "strchars()" function
17487 */
17488 static void
17489f_strchars(argvars, rettv)
17490 typval_T *argvars;
17491 typval_T *rettv;
17492{
17493 char_u *s = get_tv_string(&argvars[0]);
17494#ifdef FEAT_MBYTE
17495 varnumber_T len = 0;
17496
17497 while (*s != NUL)
17498 {
17499 mb_cptr2char_adv(&s);
17500 ++len;
17501 }
17502 rettv->vval.v_number = len;
17503#else
17504 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17505#endif
17506}
17507
17508/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017509 * "strdisplaywidth()" function
17510 */
17511 static void
17512f_strdisplaywidth(argvars, rettv)
17513 typval_T *argvars;
17514 typval_T *rettv;
17515{
17516 char_u *s = get_tv_string(&argvars[0]);
17517 int col = 0;
17518
17519 if (argvars[1].v_type != VAR_UNKNOWN)
17520 col = get_tv_number(&argvars[1]);
17521
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017522 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017523}
17524
17525/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017526 * "strwidth()" function
17527 */
17528 static void
17529f_strwidth(argvars, rettv)
17530 typval_T *argvars;
17531 typval_T *rettv;
17532{
17533 char_u *s = get_tv_string(&argvars[0]);
17534
17535 rettv->vval.v_number = (varnumber_T)(
17536#ifdef FEAT_MBYTE
17537 mb_string2cells(s, -1)
17538#else
17539 STRLEN(s)
17540#endif
17541 );
17542}
17543
17544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545 * "strpart()" function
17546 */
17547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017548f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017549 typval_T *argvars;
17550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551{
17552 char_u *p;
17553 int n;
17554 int len;
17555 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017556 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017558 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 slen = (int)STRLEN(p);
17560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017561 n = get_tv_number_chk(&argvars[1], &error);
17562 if (error)
17563 len = 0;
17564 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017565 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 else
17567 len = slen - n; /* default len: all bytes that are available. */
17568
17569 /*
17570 * Only return the overlap between the specified part and the actual
17571 * string.
17572 */
17573 if (n < 0)
17574 {
17575 len += n;
17576 n = 0;
17577 }
17578 else if (n > slen)
17579 n = slen;
17580 if (len < 0)
17581 len = 0;
17582 else if (n + len > slen)
17583 len = slen - n;
17584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017585 rettv->v_type = VAR_STRING;
17586 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587}
17588
17589/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017590 * "strridx()" function
17591 */
17592 static void
17593f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017594 typval_T *argvars;
17595 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017596{
17597 char_u buf[NUMBUFLEN];
17598 char_u *needle;
17599 char_u *haystack;
17600 char_u *rest;
17601 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017602 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017604 needle = get_tv_string_chk(&argvars[1]);
17605 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017606
17607 rettv->vval.v_number = -1;
17608 if (needle == NULL || haystack == NULL)
17609 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017610
17611 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017612 if (argvars[2].v_type != VAR_UNKNOWN)
17613 {
17614 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017615 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017616 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017617 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017618 }
17619 else
17620 end_idx = haystack_len;
17621
Bram Moolenaar0d660222005-01-07 21:51:51 +000017622 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017623 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017624 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017625 lastmatch = haystack + end_idx;
17626 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017627 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017628 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017629 for (rest = haystack; *rest != '\0'; ++rest)
17630 {
17631 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017632 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017633 break;
17634 lastmatch = rest;
17635 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017636 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017637
17638 if (lastmatch == NULL)
17639 rettv->vval.v_number = -1;
17640 else
17641 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17642}
17643
17644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 * "strtrans()" function
17646 */
17647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017648f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017649 typval_T *argvars;
17650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017652 rettv->v_type = VAR_STRING;
17653 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654}
17655
17656/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017657 * "submatch()" function
17658 */
17659 static void
17660f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017661 typval_T *argvars;
17662 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017663{
17664 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017665 rettv->vval.v_string =
17666 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017667}
17668
17669/*
17670 * "substitute()" function
17671 */
17672 static void
17673f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017674 typval_T *argvars;
17675 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017676{
17677 char_u patbuf[NUMBUFLEN];
17678 char_u subbuf[NUMBUFLEN];
17679 char_u flagsbuf[NUMBUFLEN];
17680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017681 char_u *str = get_tv_string_chk(&argvars[0]);
17682 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17683 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17684 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17685
Bram Moolenaar0d660222005-01-07 21:51:51 +000017686 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017687 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17688 rettv->vval.v_string = NULL;
17689 else
17690 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017691}
17692
17693/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017694 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017697f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017698 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700{
17701 int id = 0;
17702#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017703 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 long col;
17705 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017706 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017708 lnum = get_tv_lnum(argvars); /* -1 on type error */
17709 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17710 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017712 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017713 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017714 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715#endif
17716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017717 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718}
17719
17720/*
17721 * "synIDattr(id, what [, mode])" function
17722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017724f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017725 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727{
17728 char_u *p = NULL;
17729#ifdef FEAT_SYN_HL
17730 int id;
17731 char_u *what;
17732 char_u *mode;
17733 char_u modebuf[NUMBUFLEN];
17734 int modec;
17735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017736 id = get_tv_number(&argvars[0]);
17737 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017738 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017740 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017742 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743 modec = 0; /* replace invalid with current */
17744 }
17745 else
17746 {
17747#ifdef FEAT_GUI
17748 if (gui.in_use)
17749 modec = 'g';
17750 else
17751#endif
17752 if (t_colors > 1)
17753 modec = 'c';
17754 else
17755 modec = 't';
17756 }
17757
17758
17759 switch (TOLOWER_ASC(what[0]))
17760 {
17761 case 'b':
17762 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17763 p = highlight_color(id, what, modec);
17764 else /* bold */
17765 p = highlight_has_attr(id, HL_BOLD, modec);
17766 break;
17767
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017768 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 p = highlight_color(id, what, modec);
17770 break;
17771
17772 case 'i':
17773 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17774 p = highlight_has_attr(id, HL_INVERSE, modec);
17775 else /* italic */
17776 p = highlight_has_attr(id, HL_ITALIC, modec);
17777 break;
17778
17779 case 'n': /* name */
17780 p = get_highlight_name(NULL, id - 1);
17781 break;
17782
17783 case 'r': /* reverse */
17784 p = highlight_has_attr(id, HL_INVERSE, modec);
17785 break;
17786
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017787 case 's':
17788 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17789 p = highlight_color(id, what, modec);
17790 else /* standout */
17791 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 break;
17793
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017794 case 'u':
17795 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17796 /* underline */
17797 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17798 else
17799 /* undercurl */
17800 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801 break;
17802 }
17803
17804 if (p != NULL)
17805 p = vim_strsave(p);
17806#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017807 rettv->v_type = VAR_STRING;
17808 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017809}
17810
17811/*
17812 * "synIDtrans(id)" function
17813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017815f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017816 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818{
17819 int id;
17820
17821#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017822 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823
17824 if (id > 0)
17825 id = syn_get_final_id(id);
17826 else
17827#endif
17828 id = 0;
17829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017830 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831}
17832
17833/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017834 * "synconcealed(lnum, col)" function
17835 */
17836 static void
17837f_synconcealed(argvars, rettv)
17838 typval_T *argvars UNUSED;
17839 typval_T *rettv;
17840{
17841#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17842 long lnum;
17843 long col;
17844 int syntax_flags = 0;
17845 int cchar;
17846 int matchid = 0;
17847 char_u str[NUMBUFLEN];
17848#endif
17849
17850 rettv->v_type = VAR_LIST;
17851 rettv->vval.v_list = NULL;
17852
17853#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17854 lnum = get_tv_lnum(argvars); /* -1 on type error */
17855 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17856
17857 vim_memset(str, NUL, sizeof(str));
17858
17859 if (rettv_list_alloc(rettv) != FAIL)
17860 {
17861 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17862 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17863 && curwin->w_p_cole > 0)
17864 {
17865 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17866 syntax_flags = get_syntax_info(&matchid);
17867
17868 /* get the conceal character */
17869 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17870 {
17871 cchar = syn_get_sub_char();
17872 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17873 cchar = lcs_conceal;
17874 if (cchar != NUL)
17875 {
17876# ifdef FEAT_MBYTE
17877 if (has_mbyte)
17878 (*mb_char2bytes)(cchar, str);
17879 else
17880# endif
17881 str[0] = cchar;
17882 }
17883 }
17884 }
17885
17886 list_append_number(rettv->vval.v_list,
17887 (syntax_flags & HL_CONCEAL) != 0);
17888 /* -1 to auto-determine strlen */
17889 list_append_string(rettv->vval.v_list, str, -1);
17890 list_append_number(rettv->vval.v_list, matchid);
17891 }
17892#endif
17893}
17894
17895/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017896 * "synstack(lnum, col)" function
17897 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017898 static void
17899f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017900 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017901 typval_T *rettv;
17902{
17903#ifdef FEAT_SYN_HL
17904 long lnum;
17905 long col;
17906 int i;
17907 int id;
17908#endif
17909
17910 rettv->v_type = VAR_LIST;
17911 rettv->vval.v_list = NULL;
17912
17913#ifdef FEAT_SYN_HL
17914 lnum = get_tv_lnum(argvars); /* -1 on type error */
17915 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17916
17917 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017918 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017919 && rettv_list_alloc(rettv) != FAIL)
17920 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017921 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017922 for (i = 0; ; ++i)
17923 {
17924 id = syn_get_stack_item(i);
17925 if (id < 0)
17926 break;
17927 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17928 break;
17929 }
17930 }
17931#endif
17932}
17933
17934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935 * "system()" function
17936 */
17937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017938f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017939 typval_T *argvars;
17940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017941{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017942 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017944 char_u *infile = NULL;
17945 char_u buf[NUMBUFLEN];
17946 int err = FALSE;
17947 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017949 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017950 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017951
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017952 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017953 {
17954 /*
17955 * Write the string to a temp file, to be used for input of the shell
17956 * command.
17957 */
17958 if ((infile = vim_tempname('i')) == NULL)
17959 {
17960 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017961 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017962 }
17963
17964 fd = mch_fopen((char *)infile, WRITEBIN);
17965 if (fd == NULL)
17966 {
17967 EMSG2(_(e_notopen), infile);
17968 goto done;
17969 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017970 p = get_tv_string_buf_chk(&argvars[1], buf);
17971 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017972 {
17973 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017974 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017975 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017976 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17977 err = TRUE;
17978 if (fclose(fd) != 0)
17979 err = TRUE;
17980 if (err)
17981 {
17982 EMSG(_("E677: Error writing temp file"));
17983 goto done;
17984 }
17985 }
17986
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017987 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17988 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017989
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990#ifdef USE_CR
17991 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017992 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993 {
17994 char_u *s;
17995
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017996 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 {
17998 if (*s == CAR)
17999 *s = NL;
18000 }
18001 }
18002#else
18003# ifdef USE_CRNL
18004 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018005 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006 {
18007 char_u *s, *d;
18008
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018009 d = res;
18010 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 {
18012 if (s[0] == CAR && s[1] == NL)
18013 ++s;
18014 *d++ = *s;
18015 }
18016 *d = NUL;
18017 }
18018# endif
18019#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018020
18021done:
18022 if (infile != NULL)
18023 {
18024 mch_remove(infile);
18025 vim_free(infile);
18026 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018027 rettv->v_type = VAR_STRING;
18028 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029}
18030
18031/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018032 * "tabpagebuflist()" function
18033 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018034 static void
18035f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018036 typval_T *argvars UNUSED;
18037 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018038{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018039#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018040 tabpage_T *tp;
18041 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018042
18043 if (argvars[0].v_type == VAR_UNKNOWN)
18044 wp = firstwin;
18045 else
18046 {
18047 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18048 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018049 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018050 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018051 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018052 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018053 for (; wp != NULL; wp = wp->w_next)
18054 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018055 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018056 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018057 }
18058#endif
18059}
18060
18061
18062/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018063 * "tabpagenr()" function
18064 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018065 static void
18066f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018067 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018068 typval_T *rettv;
18069{
18070 int nr = 1;
18071#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018072 char_u *arg;
18073
18074 if (argvars[0].v_type != VAR_UNKNOWN)
18075 {
18076 arg = get_tv_string_chk(&argvars[0]);
18077 nr = 0;
18078 if (arg != NULL)
18079 {
18080 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018081 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018082 else
18083 EMSG2(_(e_invexpr2), arg);
18084 }
18085 }
18086 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018087 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018088#endif
18089 rettv->vval.v_number = nr;
18090}
18091
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018092
18093#ifdef FEAT_WINDOWS
18094static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18095
18096/*
18097 * Common code for tabpagewinnr() and winnr().
18098 */
18099 static int
18100get_winnr(tp, argvar)
18101 tabpage_T *tp;
18102 typval_T *argvar;
18103{
18104 win_T *twin;
18105 int nr = 1;
18106 win_T *wp;
18107 char_u *arg;
18108
18109 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18110 if (argvar->v_type != VAR_UNKNOWN)
18111 {
18112 arg = get_tv_string_chk(argvar);
18113 if (arg == NULL)
18114 nr = 0; /* type error; errmsg already given */
18115 else if (STRCMP(arg, "$") == 0)
18116 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18117 else if (STRCMP(arg, "#") == 0)
18118 {
18119 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18120 if (twin == NULL)
18121 nr = 0;
18122 }
18123 else
18124 {
18125 EMSG2(_(e_invexpr2), arg);
18126 nr = 0;
18127 }
18128 }
18129
18130 if (nr > 0)
18131 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18132 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018133 {
18134 if (wp == NULL)
18135 {
18136 /* didn't find it in this tabpage */
18137 nr = 0;
18138 break;
18139 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018140 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018141 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018142 return nr;
18143}
18144#endif
18145
18146/*
18147 * "tabpagewinnr()" function
18148 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018149 static void
18150f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018151 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018152 typval_T *rettv;
18153{
18154 int nr = 1;
18155#ifdef FEAT_WINDOWS
18156 tabpage_T *tp;
18157
18158 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18159 if (tp == NULL)
18160 nr = 0;
18161 else
18162 nr = get_winnr(tp, &argvars[1]);
18163#endif
18164 rettv->vval.v_number = nr;
18165}
18166
18167
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018168/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018169 * "tagfiles()" function
18170 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018171 static void
18172f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018173 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018174 typval_T *rettv;
18175{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018176 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018177 tagname_T tn;
18178 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018179
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018180 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018181 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018182 fname = alloc(MAXPATHL);
18183 if (fname == NULL)
18184 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018185
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018186 for (first = TRUE; ; first = FALSE)
18187 if (get_tagfname(&tn, first, fname) == FAIL
18188 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018189 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018190 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018191 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018192}
18193
18194/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018195 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018196 */
18197 static void
18198f_taglist(argvars, rettv)
18199 typval_T *argvars;
18200 typval_T *rettv;
18201{
18202 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018203
18204 tag_pattern = get_tv_string(&argvars[0]);
18205
18206 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018207 if (*tag_pattern == NUL)
18208 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018209
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018210 if (rettv_list_alloc(rettv) == OK)
18211 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018212}
18213
18214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215 * "tempname()" function
18216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018218f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018219 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221{
18222 static int x = 'A';
18223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018224 rettv->v_type = VAR_STRING;
18225 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226
18227 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18228 * names. Skip 'I' and 'O', they are used for shell redirection. */
18229 do
18230 {
18231 if (x == 'Z')
18232 x = '0';
18233 else if (x == '9')
18234 x = 'A';
18235 else
18236 {
18237#ifdef EBCDIC
18238 if (x == 'I')
18239 x = 'J';
18240 else if (x == 'R')
18241 x = 'S';
18242 else
18243#endif
18244 ++x;
18245 }
18246 } while (x == 'I' || x == 'O');
18247}
18248
18249/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018250 * "test(list)" function: Just checking the walls...
18251 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018252 static void
18253f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018254 typval_T *argvars UNUSED;
18255 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018256{
18257 /* Used for unit testing. Change the code below to your liking. */
18258#if 0
18259 listitem_T *li;
18260 list_T *l;
18261 char_u *bad, *good;
18262
18263 if (argvars[0].v_type != VAR_LIST)
18264 return;
18265 l = argvars[0].vval.v_list;
18266 if (l == NULL)
18267 return;
18268 li = l->lv_first;
18269 if (li == NULL)
18270 return;
18271 bad = get_tv_string(&li->li_tv);
18272 li = li->li_next;
18273 if (li == NULL)
18274 return;
18275 good = get_tv_string(&li->li_tv);
18276 rettv->vval.v_number = test_edit_score(bad, good);
18277#endif
18278}
18279
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018280#ifdef FEAT_FLOAT
18281/*
18282 * "tan()" function
18283 */
18284 static void
18285f_tan(argvars, rettv)
18286 typval_T *argvars;
18287 typval_T *rettv;
18288{
18289 float_T f;
18290
18291 rettv->v_type = VAR_FLOAT;
18292 if (get_float_arg(argvars, &f) == OK)
18293 rettv->vval.v_float = tan(f);
18294 else
18295 rettv->vval.v_float = 0.0;
18296}
18297
18298/*
18299 * "tanh()" function
18300 */
18301 static void
18302f_tanh(argvars, rettv)
18303 typval_T *argvars;
18304 typval_T *rettv;
18305{
18306 float_T f;
18307
18308 rettv->v_type = VAR_FLOAT;
18309 if (get_float_arg(argvars, &f) == OK)
18310 rettv->vval.v_float = tanh(f);
18311 else
18312 rettv->vval.v_float = 0.0;
18313}
18314#endif
18315
Bram Moolenaard52d9742005-08-21 22:20:28 +000018316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317 * "tolower(string)" function
18318 */
18319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018320f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018321 typval_T *argvars;
18322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323{
18324 char_u *p;
18325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018326 p = vim_strsave(get_tv_string(&argvars[0]));
18327 rettv->v_type = VAR_STRING;
18328 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329
18330 if (p != NULL)
18331 while (*p != NUL)
18332 {
18333#ifdef FEAT_MBYTE
18334 int l;
18335
18336 if (enc_utf8)
18337 {
18338 int c, lc;
18339
18340 c = utf_ptr2char(p);
18341 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018342 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343 /* TODO: reallocate string when byte count changes. */
18344 if (utf_char2len(lc) == l)
18345 utf_char2bytes(lc, p);
18346 p += l;
18347 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018348 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349 p += l; /* skip multi-byte character */
18350 else
18351#endif
18352 {
18353 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18354 ++p;
18355 }
18356 }
18357}
18358
18359/*
18360 * "toupper(string)" function
18361 */
18362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018363f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018364 typval_T *argvars;
18365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018367 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018368 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369}
18370
18371/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018372 * "tr(string, fromstr, tostr)" function
18373 */
18374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018375f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018376 typval_T *argvars;
18377 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018378{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018379 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018380 char_u *fromstr;
18381 char_u *tostr;
18382 char_u *p;
18383#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018384 int inlen;
18385 int fromlen;
18386 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018387 int idx;
18388 char_u *cpstr;
18389 int cplen;
18390 int first = TRUE;
18391#endif
18392 char_u buf[NUMBUFLEN];
18393 char_u buf2[NUMBUFLEN];
18394 garray_T ga;
18395
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018396 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018397 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18398 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018399
18400 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018401 rettv->v_type = VAR_STRING;
18402 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018403 if (fromstr == NULL || tostr == NULL)
18404 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018405 ga_init2(&ga, (int)sizeof(char), 80);
18406
18407#ifdef FEAT_MBYTE
18408 if (!has_mbyte)
18409#endif
18410 /* not multi-byte: fromstr and tostr must be the same length */
18411 if (STRLEN(fromstr) != STRLEN(tostr))
18412 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018413#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018414error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018415#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018416 EMSG2(_(e_invarg2), fromstr);
18417 ga_clear(&ga);
18418 return;
18419 }
18420
18421 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018422 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018423 {
18424#ifdef FEAT_MBYTE
18425 if (has_mbyte)
18426 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018427 inlen = (*mb_ptr2len)(in_str);
18428 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018429 cplen = inlen;
18430 idx = 0;
18431 for (p = fromstr; *p != NUL; p += fromlen)
18432 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018433 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018434 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018435 {
18436 for (p = tostr; *p != NUL; p += tolen)
18437 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018438 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018439 if (idx-- == 0)
18440 {
18441 cplen = tolen;
18442 cpstr = p;
18443 break;
18444 }
18445 }
18446 if (*p == NUL) /* tostr is shorter than fromstr */
18447 goto error;
18448 break;
18449 }
18450 ++idx;
18451 }
18452
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018453 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018454 {
18455 /* Check that fromstr and tostr have the same number of
18456 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018457 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018458 first = FALSE;
18459 for (p = tostr; *p != NUL; p += tolen)
18460 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018461 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018462 --idx;
18463 }
18464 if (idx != 0)
18465 goto error;
18466 }
18467
18468 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018469 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018470 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018471
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018472 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018473 }
18474 else
18475#endif
18476 {
18477 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018478 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018479 if (p != NULL)
18480 ga_append(&ga, tostr[p - fromstr]);
18481 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018482 ga_append(&ga, *in_str);
18483 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018484 }
18485 }
18486
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018487 /* add a terminating NUL */
18488 ga_grow(&ga, 1);
18489 ga_append(&ga, NUL);
18490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018491 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018492}
18493
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018494#ifdef FEAT_FLOAT
18495/*
18496 * "trunc({float})" function
18497 */
18498 static void
18499f_trunc(argvars, rettv)
18500 typval_T *argvars;
18501 typval_T *rettv;
18502{
18503 float_T f;
18504
18505 rettv->v_type = VAR_FLOAT;
18506 if (get_float_arg(argvars, &f) == OK)
18507 /* trunc() is not in C90, use floor() or ceil() instead. */
18508 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18509 else
18510 rettv->vval.v_float = 0.0;
18511}
18512#endif
18513
Bram Moolenaar8299df92004-07-10 09:47:34 +000018514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515 * "type(expr)" function
18516 */
18517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018518f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018519 typval_T *argvars;
18520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018522 int n;
18523
18524 switch (argvars[0].v_type)
18525 {
18526 case VAR_NUMBER: n = 0; break;
18527 case VAR_STRING: n = 1; break;
18528 case VAR_FUNC: n = 2; break;
18529 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018530 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018531#ifdef FEAT_FLOAT
18532 case VAR_FLOAT: n = 5; break;
18533#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018534 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18535 }
18536 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537}
18538
18539/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018540 * "undofile(name)" function
18541 */
18542 static void
18543f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018544 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018545 typval_T *rettv;
18546{
18547 rettv->v_type = VAR_STRING;
18548#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018549 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018550 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018551
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018552 if (*fname == NUL)
18553 {
18554 /* If there is no file name there will be no undo file. */
18555 rettv->vval.v_string = NULL;
18556 }
18557 else
18558 {
18559 char_u *ffname = FullName_save(fname, FALSE);
18560
18561 if (ffname != NULL)
18562 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18563 vim_free(ffname);
18564 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018565 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018566#else
18567 rettv->vval.v_string = NULL;
18568#endif
18569}
18570
18571/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018572 * "undotree()" function
18573 */
18574 static void
18575f_undotree(argvars, rettv)
18576 typval_T *argvars UNUSED;
18577 typval_T *rettv;
18578{
18579 if (rettv_dict_alloc(rettv) == OK)
18580 {
18581 dict_T *dict = rettv->vval.v_dict;
18582 list_T *list;
18583
Bram Moolenaar730cde92010-06-27 05:18:54 +020018584 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018585 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018586 dict_add_nr_str(dict, "save_last",
18587 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018588 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18589 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018590 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018591
18592 list = list_alloc();
18593 if (list != NULL)
18594 {
18595 u_eval_tree(curbuf->b_u_oldhead, list);
18596 dict_add_list(dict, "entries", list);
18597 }
18598 }
18599}
18600
18601/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018602 * "values(dict)" function
18603 */
18604 static void
18605f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018606 typval_T *argvars;
18607 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018608{
18609 dict_list(argvars, rettv, 1);
18610}
18611
18612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613 * "virtcol(string)" function
18614 */
18615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018616f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018617 typval_T *argvars;
18618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619{
18620 colnr_T vcol = 0;
18621 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018622 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018624 fp = var2fpos(&argvars[0], FALSE, &fnum);
18625 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18626 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 {
18628 getvvcol(curwin, fp, NULL, NULL, &vcol);
18629 ++vcol;
18630 }
18631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018632 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633}
18634
18635/*
18636 * "visualmode()" function
18637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018639f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018640 typval_T *argvars UNUSED;
18641 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642{
18643#ifdef FEAT_VISUAL
18644 char_u str[2];
18645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018646 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 str[0] = curbuf->b_visual_mode_eval;
18648 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018649 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650
18651 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018652 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654#endif
18655}
18656
18657/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018658 * "wildmenumode()" function
18659 */
18660 static void
18661f_wildmenumode(argvars, rettv)
18662 typval_T *argvars UNUSED;
18663 typval_T *rettv UNUSED;
18664{
18665#ifdef FEAT_WILDMENU
18666 if (wild_menu_showing)
18667 rettv->vval.v_number = 1;
18668#endif
18669}
18670
18671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672 * "winbufnr(nr)" function
18673 */
18674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018675f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018676 typval_T *argvars;
18677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678{
18679 win_T *wp;
18680
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018681 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018683 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018685 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686}
18687
18688/*
18689 * "wincol()" function
18690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018692f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018693 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695{
18696 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018697 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698}
18699
18700/*
18701 * "winheight(nr)" function
18702 */
18703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018704f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018705 typval_T *argvars;
18706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707{
18708 win_T *wp;
18709
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018710 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018712 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018714 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715}
18716
18717/*
18718 * "winline()" function
18719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018721f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018722 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724{
18725 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018726 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727}
18728
18729/*
18730 * "winnr()" function
18731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018733f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018734 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736{
18737 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018738
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018740 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018742 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743}
18744
18745/*
18746 * "winrestcmd()" function
18747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018749f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752{
18753#ifdef FEAT_WINDOWS
18754 win_T *wp;
18755 int winnr = 1;
18756 garray_T ga;
18757 char_u buf[50];
18758
18759 ga_init2(&ga, (int)sizeof(char), 70);
18760 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18761 {
18762 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18763 ga_concat(&ga, buf);
18764# ifdef FEAT_VERTSPLIT
18765 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18766 ga_concat(&ga, buf);
18767# endif
18768 ++winnr;
18769 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018770 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018772 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018774 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018776 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777}
18778
18779/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018780 * "winrestview()" function
18781 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018782 static void
18783f_winrestview(argvars, rettv)
18784 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018785 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018786{
18787 dict_T *dict;
18788
18789 if (argvars[0].v_type != VAR_DICT
18790 || (dict = argvars[0].vval.v_dict) == NULL)
18791 EMSG(_(e_invarg));
18792 else
18793 {
18794 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18795 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18796#ifdef FEAT_VIRTUALEDIT
18797 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18798#endif
18799 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018800 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018801
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018802 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018803#ifdef FEAT_DIFF
18804 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18805#endif
18806 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18807 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18808
18809 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018810 win_new_height(curwin, curwin->w_height);
18811# ifdef FEAT_VERTSPLIT
18812 win_new_width(curwin, W_WIDTH(curwin));
18813# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018814 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018815
18816 if (curwin->w_topline == 0)
18817 curwin->w_topline = 1;
18818 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18819 curwin->w_topline = curbuf->b_ml.ml_line_count;
18820#ifdef FEAT_DIFF
18821 check_topfill(curwin, TRUE);
18822#endif
18823 }
18824}
18825
18826/*
18827 * "winsaveview()" function
18828 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018829 static void
18830f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018831 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018832 typval_T *rettv;
18833{
18834 dict_T *dict;
18835
Bram Moolenaara800b422010-06-27 01:15:55 +020018836 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018837 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018838 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018839
18840 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18841 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18842#ifdef FEAT_VIRTUALEDIT
18843 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18844#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018845 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018846 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18847
18848 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18849#ifdef FEAT_DIFF
18850 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18851#endif
18852 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18853 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18854}
18855
18856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 * "winwidth(nr)" function
18858 */
18859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018860f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018861 typval_T *argvars;
18862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863{
18864 win_T *wp;
18865
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018866 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018868 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018869 else
18870#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018871 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018873 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874#endif
18875}
18876
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018878 * "writefile()" function
18879 */
18880 static void
18881f_writefile(argvars, rettv)
18882 typval_T *argvars;
18883 typval_T *rettv;
18884{
18885 int binary = FALSE;
18886 char_u *fname;
18887 FILE *fd;
18888 listitem_T *li;
18889 char_u *s;
18890 int ret = 0;
18891 int c;
18892
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018893 if (check_restricted() || check_secure())
18894 return;
18895
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018896 if (argvars[0].v_type != VAR_LIST)
18897 {
18898 EMSG2(_(e_listarg), "writefile()");
18899 return;
18900 }
18901 if (argvars[0].vval.v_list == NULL)
18902 return;
18903
18904 if (argvars[2].v_type != VAR_UNKNOWN
18905 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18906 binary = TRUE;
18907
18908 /* Always open the file in binary mode, library functions have a mind of
18909 * their own about CR-LF conversion. */
18910 fname = get_tv_string(&argvars[1]);
18911 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18912 {
18913 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18914 ret = -1;
18915 }
18916 else
18917 {
18918 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18919 li = li->li_next)
18920 {
18921 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18922 {
18923 if (*s == '\n')
18924 c = putc(NUL, fd);
18925 else
18926 c = putc(*s, fd);
18927 if (c == EOF)
18928 {
18929 ret = -1;
18930 break;
18931 }
18932 }
18933 if (!binary || li->li_next != NULL)
18934 if (putc('\n', fd) == EOF)
18935 {
18936 ret = -1;
18937 break;
18938 }
18939 if (ret < 0)
18940 {
18941 EMSG(_(e_write));
18942 break;
18943 }
18944 }
18945 fclose(fd);
18946 }
18947
18948 rettv->vval.v_number = ret;
18949}
18950
18951/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018952 * "xor(expr, expr)" function
18953 */
18954 static void
18955f_xor(argvars, rettv)
18956 typval_T *argvars;
18957 typval_T *rettv;
18958{
18959 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18960 ^ get_tv_number_chk(&argvars[1], NULL);
18961}
18962
18963
18964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018966 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 */
18968 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018969var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018970 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018971 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018972 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018974 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018976 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977
Bram Moolenaara5525202006-03-02 22:52:09 +000018978 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018979 if (varp->v_type == VAR_LIST)
18980 {
18981 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018982 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018983 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018984 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018985
18986 l = varp->vval.v_list;
18987 if (l == NULL)
18988 return NULL;
18989
18990 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018991 pos.lnum = list_find_nr(l, 0L, &error);
18992 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018993 return NULL; /* invalid line number */
18994
18995 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018996 pos.col = list_find_nr(l, 1L, &error);
18997 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018998 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018999 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019000
19001 /* We accept "$" for the column number: last column. */
19002 li = list_find(l, 1L);
19003 if (li != NULL && li->li_tv.v_type == VAR_STRING
19004 && li->li_tv.vval.v_string != NULL
19005 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19006 pos.col = len + 1;
19007
Bram Moolenaara5525202006-03-02 22:52:09 +000019008 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019009 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019010 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019011 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019012
Bram Moolenaara5525202006-03-02 22:52:09 +000019013#ifdef FEAT_VIRTUALEDIT
19014 /* Get the virtual offset. Defaults to zero. */
19015 pos.coladd = list_find_nr(l, 2L, &error);
19016 if (error)
19017 pos.coladd = 0;
19018#endif
19019
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019020 return &pos;
19021 }
19022
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019023 name = get_tv_string_chk(varp);
19024 if (name == NULL)
19025 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019026 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019028#ifdef FEAT_VISUAL
19029 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19030 {
19031 if (VIsual_active)
19032 return &VIsual;
19033 return &curwin->w_cursor;
19034 }
19035#endif
19036 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019038 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19040 return NULL;
19041 return pp;
19042 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019043
19044#ifdef FEAT_VIRTUALEDIT
19045 pos.coladd = 0;
19046#endif
19047
Bram Moolenaar477933c2007-07-17 14:32:23 +000019048 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019049 {
19050 pos.col = 0;
19051 if (name[1] == '0') /* "w0": first visible line */
19052 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019053 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019054 pos.lnum = curwin->w_topline;
19055 return &pos;
19056 }
19057 else if (name[1] == '$') /* "w$": last visible line */
19058 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019059 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019060 pos.lnum = curwin->w_botline - 1;
19061 return &pos;
19062 }
19063 }
19064 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019066 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067 {
19068 pos.lnum = curbuf->b_ml.ml_line_count;
19069 pos.col = 0;
19070 }
19071 else
19072 {
19073 pos.lnum = curwin->w_cursor.lnum;
19074 pos.col = (colnr_T)STRLEN(ml_get_curline());
19075 }
19076 return &pos;
19077 }
19078 return NULL;
19079}
19080
19081/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019082 * Convert list in "arg" into a position and optional file number.
19083 * When "fnump" is NULL there is no file number, only 3 items.
19084 * Note that the column is passed on as-is, the caller may want to decrement
19085 * it to use 1 for the first column.
19086 * Return FAIL when conversion is not possible, doesn't check the position for
19087 * validity.
19088 */
19089 static int
19090list2fpos(arg, posp, fnump)
19091 typval_T *arg;
19092 pos_T *posp;
19093 int *fnump;
19094{
19095 list_T *l = arg->vval.v_list;
19096 long i = 0;
19097 long n;
19098
Bram Moolenaarbde35262006-07-23 20:12:24 +000019099 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19100 * when "fnump" isn't NULL and "coladd" is optional. */
19101 if (arg->v_type != VAR_LIST
19102 || l == NULL
19103 || l->lv_len < (fnump == NULL ? 2 : 3)
19104 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019105 return FAIL;
19106
19107 if (fnump != NULL)
19108 {
19109 n = list_find_nr(l, i++, NULL); /* fnum */
19110 if (n < 0)
19111 return FAIL;
19112 if (n == 0)
19113 n = curbuf->b_fnum; /* current buffer */
19114 *fnump = n;
19115 }
19116
19117 n = list_find_nr(l, i++, NULL); /* lnum */
19118 if (n < 0)
19119 return FAIL;
19120 posp->lnum = n;
19121
19122 n = list_find_nr(l, i++, NULL); /* col */
19123 if (n < 0)
19124 return FAIL;
19125 posp->col = n;
19126
19127#ifdef FEAT_VIRTUALEDIT
19128 n = list_find_nr(l, i, NULL);
19129 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019130 posp->coladd = 0;
19131 else
19132 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019133#endif
19134
19135 return OK;
19136}
19137
19138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139 * Get the length of an environment variable name.
19140 * Advance "arg" to the first character after the name.
19141 * Return 0 for error.
19142 */
19143 static int
19144get_env_len(arg)
19145 char_u **arg;
19146{
19147 char_u *p;
19148 int len;
19149
19150 for (p = *arg; vim_isIDc(*p); ++p)
19151 ;
19152 if (p == *arg) /* no name found */
19153 return 0;
19154
19155 len = (int)(p - *arg);
19156 *arg = p;
19157 return len;
19158}
19159
19160/*
19161 * Get the length of the name of a function or internal variable.
19162 * "arg" is advanced to the first non-white character after the name.
19163 * Return 0 if something is wrong.
19164 */
19165 static int
19166get_id_len(arg)
19167 char_u **arg;
19168{
19169 char_u *p;
19170 int len;
19171
19172 /* Find the end of the name. */
19173 for (p = *arg; eval_isnamec(*p); ++p)
19174 ;
19175 if (p == *arg) /* no name found */
19176 return 0;
19177
19178 len = (int)(p - *arg);
19179 *arg = skipwhite(p);
19180
19181 return len;
19182}
19183
19184/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019185 * Get the length of the name of a variable or function.
19186 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019188 * Return -1 if curly braces expansion failed.
19189 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190 * If the name contains 'magic' {}'s, expand them and return the
19191 * expanded name in an allocated string via 'alias' - caller must free.
19192 */
19193 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019194get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195 char_u **arg;
19196 char_u **alias;
19197 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019198 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199{
19200 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201 char_u *p;
19202 char_u *expr_start;
19203 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204
19205 *alias = NULL; /* default to no alias */
19206
19207 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19208 && (*arg)[2] == (int)KE_SNR)
19209 {
19210 /* hard coded <SNR>, already translated */
19211 *arg += 3;
19212 return get_id_len(arg) + 3;
19213 }
19214 len = eval_fname_script(*arg);
19215 if (len > 0)
19216 {
19217 /* literal "<SID>", "s:" or "<SNR>" */
19218 *arg += len;
19219 }
19220
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019222 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019224 p = find_name_end(*arg, &expr_start, &expr_end,
19225 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 if (expr_start != NULL)
19227 {
19228 char_u *temp_string;
19229
19230 if (!evaluate)
19231 {
19232 len += (int)(p - *arg);
19233 *arg = skipwhite(p);
19234 return len;
19235 }
19236
19237 /*
19238 * Include any <SID> etc in the expanded string:
19239 * Thus the -len here.
19240 */
19241 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19242 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019243 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 *alias = temp_string;
19245 *arg = skipwhite(p);
19246 return (int)STRLEN(temp_string);
19247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248
19249 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019250 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 EMSG2(_(e_invexpr2), *arg);
19252
19253 return len;
19254}
19255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019256/*
19257 * Find the end of a variable or function name, taking care of magic braces.
19258 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19259 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019260 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019261 * Return a pointer to just after the name. Equal to "arg" if there is no
19262 * valid name.
19263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019265find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266 char_u *arg;
19267 char_u **expr_start;
19268 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019269 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019271 int mb_nest = 0;
19272 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273 char_u *p;
19274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019275 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019277 *expr_start = NULL;
19278 *expr_end = NULL;
19279 }
19280
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019281 /* Quick check for valid starting character. */
19282 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19283 return arg;
19284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019285 for (p = arg; *p != NUL
19286 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019287 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019288 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019289 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019290 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019291 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019292 if (*p == '\'')
19293 {
19294 /* skip over 'string' to avoid counting [ and ] inside it. */
19295 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19296 ;
19297 if (*p == NUL)
19298 break;
19299 }
19300 else if (*p == '"')
19301 {
19302 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19303 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19304 if (*p == '\\' && p[1] != NUL)
19305 ++p;
19306 if (*p == NUL)
19307 break;
19308 }
19309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019310 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019312 if (*p == '[')
19313 ++br_nest;
19314 else if (*p == ']')
19315 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019318 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019320 if (*p == '{')
19321 {
19322 mb_nest++;
19323 if (expr_start != NULL && *expr_start == NULL)
19324 *expr_start = p;
19325 }
19326 else if (*p == '}')
19327 {
19328 mb_nest--;
19329 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19330 *expr_end = p;
19331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333 }
19334
19335 return p;
19336}
19337
19338/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019339 * Expands out the 'magic' {}'s in a variable/function name.
19340 * Note that this can call itself recursively, to deal with
19341 * constructs like foo{bar}{baz}{bam}
19342 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19343 * "in_start" ^
19344 * "expr_start" ^
19345 * "expr_end" ^
19346 * "in_end" ^
19347 *
19348 * Returns a new allocated string, which the caller must free.
19349 * Returns NULL for failure.
19350 */
19351 static char_u *
19352make_expanded_name(in_start, expr_start, expr_end, in_end)
19353 char_u *in_start;
19354 char_u *expr_start;
19355 char_u *expr_end;
19356 char_u *in_end;
19357{
19358 char_u c1;
19359 char_u *retval = NULL;
19360 char_u *temp_result;
19361 char_u *nextcmd = NULL;
19362
19363 if (expr_end == NULL || in_end == NULL)
19364 return NULL;
19365 *expr_start = NUL;
19366 *expr_end = NUL;
19367 c1 = *in_end;
19368 *in_end = NUL;
19369
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019370 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019371 if (temp_result != NULL && nextcmd == NULL)
19372 {
19373 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19374 + (in_end - expr_end) + 1));
19375 if (retval != NULL)
19376 {
19377 STRCPY(retval, in_start);
19378 STRCAT(retval, temp_result);
19379 STRCAT(retval, expr_end + 1);
19380 }
19381 }
19382 vim_free(temp_result);
19383
19384 *in_end = c1; /* put char back for error messages */
19385 *expr_start = '{';
19386 *expr_end = '}';
19387
19388 if (retval != NULL)
19389 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019390 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019391 if (expr_start != NULL)
19392 {
19393 /* Further expansion! */
19394 temp_result = make_expanded_name(retval, expr_start,
19395 expr_end, temp_result);
19396 vim_free(retval);
19397 retval = temp_result;
19398 }
19399 }
19400
19401 return retval;
19402}
19403
19404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019406 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407 */
19408 static int
19409eval_isnamec(c)
19410 int c;
19411{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019412 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19413}
19414
19415/*
19416 * Return TRUE if character "c" can be used as the first character in a
19417 * variable or function name (excluding '{' and '}').
19418 */
19419 static int
19420eval_isnamec1(c)
19421 int c;
19422{
19423 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424}
19425
19426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427 * Set number v: variable to "val".
19428 */
19429 void
19430set_vim_var_nr(idx, val)
19431 int idx;
19432 long val;
19433{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019434 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435}
19436
19437/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019438 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 */
19440 long
19441get_vim_var_nr(idx)
19442 int idx;
19443{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019444 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445}
19446
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019447/*
19448 * Get string v: variable value. Uses a static buffer, can only be used once.
19449 */
19450 char_u *
19451get_vim_var_str(idx)
19452 int idx;
19453{
19454 return get_tv_string(&vimvars[idx].vv_tv);
19455}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019456
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019458 * Get List v: variable value. Caller must take care of reference count when
19459 * needed.
19460 */
19461 list_T *
19462get_vim_var_list(idx)
19463 int idx;
19464{
19465 return vimvars[idx].vv_list;
19466}
19467
19468/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019469 * Set v:char to character "c".
19470 */
19471 void
19472set_vim_var_char(c)
19473 int c;
19474{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019475 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019476
19477#ifdef FEAT_MBYTE
19478 if (has_mbyte)
19479 buf[(*mb_char2bytes)(c, buf)] = NUL;
19480 else
19481#endif
19482 {
19483 buf[0] = c;
19484 buf[1] = NUL;
19485 }
19486 set_vim_var_string(VV_CHAR, buf, -1);
19487}
19488
19489/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019490 * Set v:count to "count" and v:count1 to "count1".
19491 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 */
19493 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019494set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495 long count;
19496 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019497 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019499 if (set_prevcount)
19500 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019501 vimvars[VV_COUNT].vv_nr = count;
19502 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503}
19504
19505/*
19506 * Set string v: variable to a copy of "val".
19507 */
19508 void
19509set_vim_var_string(idx, val, len)
19510 int idx;
19511 char_u *val;
19512 int len; /* length of "val" to use or -1 (whole string) */
19513{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019514 /* Need to do this (at least) once, since we can't initialize a union.
19515 * Will always be invoked when "v:progname" is set. */
19516 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19517
Bram Moolenaare9a41262005-01-15 22:18:47 +000019518 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019520 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019522 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019524 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525}
19526
19527/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019528 * Set List v: variable to "val".
19529 */
19530 void
19531set_vim_var_list(idx, val)
19532 int idx;
19533 list_T *val;
19534{
19535 list_unref(vimvars[idx].vv_list);
19536 vimvars[idx].vv_list = val;
19537 if (val != NULL)
19538 ++val->lv_refcount;
19539}
19540
19541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 * Set v:register if needed.
19543 */
19544 void
19545set_reg_var(c)
19546 int c;
19547{
19548 char_u regname;
19549
19550 if (c == 0 || c == ' ')
19551 regname = '"';
19552 else
19553 regname = c;
19554 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019555 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556 set_vim_var_string(VV_REG, &regname, 1);
19557}
19558
19559/*
19560 * Get or set v:exception. If "oldval" == NULL, return the current value.
19561 * Otherwise, restore the value to "oldval" and return NULL.
19562 * Must always be called in pairs to save and restore v:exception! Does not
19563 * take care of memory allocations.
19564 */
19565 char_u *
19566v_exception(oldval)
19567 char_u *oldval;
19568{
19569 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019570 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571
Bram Moolenaare9a41262005-01-15 22:18:47 +000019572 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573 return NULL;
19574}
19575
19576/*
19577 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19578 * Otherwise, restore the value to "oldval" and return NULL.
19579 * Must always be called in pairs to save and restore v:throwpoint! Does not
19580 * take care of memory allocations.
19581 */
19582 char_u *
19583v_throwpoint(oldval)
19584 char_u *oldval;
19585{
19586 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019587 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588
Bram Moolenaare9a41262005-01-15 22:18:47 +000019589 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590 return NULL;
19591}
19592
19593#if defined(FEAT_AUTOCMD) || defined(PROTO)
19594/*
19595 * Set v:cmdarg.
19596 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19597 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19598 * Must always be called in pairs!
19599 */
19600 char_u *
19601set_cmdarg(eap, oldarg)
19602 exarg_T *eap;
19603 char_u *oldarg;
19604{
19605 char_u *oldval;
19606 char_u *newval;
19607 unsigned len;
19608
Bram Moolenaare9a41262005-01-15 22:18:47 +000019609 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019610 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019612 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019613 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019614 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615 }
19616
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019617 if (eap->force_bin == FORCE_BIN)
19618 len = 6;
19619 else if (eap->force_bin == FORCE_NOBIN)
19620 len = 8;
19621 else
19622 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019623
19624 if (eap->read_edit)
19625 len += 7;
19626
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019627 if (eap->force_ff != 0)
19628 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19629# ifdef FEAT_MBYTE
19630 if (eap->force_enc != 0)
19631 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019632 if (eap->bad_char != 0)
19633 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019634# endif
19635
19636 newval = alloc(len + 1);
19637 if (newval == NULL)
19638 return NULL;
19639
19640 if (eap->force_bin == FORCE_BIN)
19641 sprintf((char *)newval, " ++bin");
19642 else if (eap->force_bin == FORCE_NOBIN)
19643 sprintf((char *)newval, " ++nobin");
19644 else
19645 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019646
19647 if (eap->read_edit)
19648 STRCAT(newval, " ++edit");
19649
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019650 if (eap->force_ff != 0)
19651 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19652 eap->cmd + eap->force_ff);
19653# ifdef FEAT_MBYTE
19654 if (eap->force_enc != 0)
19655 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19656 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019657 if (eap->bad_char == BAD_KEEP)
19658 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19659 else if (eap->bad_char == BAD_DROP)
19660 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19661 else if (eap->bad_char != 0)
19662 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019663# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019664 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019665 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666}
19667#endif
19668
19669/*
19670 * Get the value of internal variable "name".
19671 * Return OK or FAIL.
19672 */
19673 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019674get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675 char_u *name;
19676 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019677 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019678 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679{
19680 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019681 typval_T *tv = NULL;
19682 typval_T atv;
19683 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685
19686 /* truncate the name, so that we can use strcmp() */
19687 cc = name[len];
19688 name[len] = NUL;
19689
19690 /*
19691 * Check for "b:changedtick".
19692 */
19693 if (STRCMP(name, "b:changedtick") == 0)
19694 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019695 atv.v_type = VAR_NUMBER;
19696 atv.vval.v_number = curbuf->b_changedtick;
19697 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 }
19699
19700 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701 * Check for user-defined variables.
19702 */
19703 else
19704 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019705 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019707 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 }
19709
Bram Moolenaare9a41262005-01-15 22:18:47 +000019710 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019712 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019713 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 ret = FAIL;
19715 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019716 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019717 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718
19719 name[len] = cc;
19720
19721 return ret;
19722}
19723
19724/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019725 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19726 * Also handle function call with Funcref variable: func(expr)
19727 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19728 */
19729 static int
19730handle_subscript(arg, rettv, evaluate, verbose)
19731 char_u **arg;
19732 typval_T *rettv;
19733 int evaluate; /* do more than finding the end */
19734 int verbose; /* give error messages */
19735{
19736 int ret = OK;
19737 dict_T *selfdict = NULL;
19738 char_u *s;
19739 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019740 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019741
19742 while (ret == OK
19743 && (**arg == '['
19744 || (**arg == '.' && rettv->v_type == VAR_DICT)
19745 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19746 && !vim_iswhite(*(*arg - 1)))
19747 {
19748 if (**arg == '(')
19749 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019750 /* need to copy the funcref so that we can clear rettv */
19751 functv = *rettv;
19752 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019753
19754 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019755 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019756 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019757 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19758 &len, evaluate, selfdict);
19759
19760 /* Clear the funcref afterwards, so that deleting it while
19761 * evaluating the arguments is possible (see test55). */
19762 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019763
19764 /* Stop the expression evaluation when immediately aborting on
19765 * error, or when an interrupt occurred or an exception was thrown
19766 * but not caught. */
19767 if (aborting())
19768 {
19769 if (ret == OK)
19770 clear_tv(rettv);
19771 ret = FAIL;
19772 }
19773 dict_unref(selfdict);
19774 selfdict = NULL;
19775 }
19776 else /* **arg == '[' || **arg == '.' */
19777 {
19778 dict_unref(selfdict);
19779 if (rettv->v_type == VAR_DICT)
19780 {
19781 selfdict = rettv->vval.v_dict;
19782 if (selfdict != NULL)
19783 ++selfdict->dv_refcount;
19784 }
19785 else
19786 selfdict = NULL;
19787 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19788 {
19789 clear_tv(rettv);
19790 ret = FAIL;
19791 }
19792 }
19793 }
19794 dict_unref(selfdict);
19795 return ret;
19796}
19797
19798/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019799 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019800 * value).
19801 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019802 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019803alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019804{
Bram Moolenaar33570922005-01-25 22:26:29 +000019805 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019806}
19807
19808/*
19809 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 * The string "s" must have been allocated, it is consumed.
19811 * Return NULL for out of memory, the variable otherwise.
19812 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019813 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019814alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 char_u *s;
19816{
Bram Moolenaar33570922005-01-25 22:26:29 +000019817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019819 rettv = alloc_tv();
19820 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019822 rettv->v_type = VAR_STRING;
19823 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824 }
19825 else
19826 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019827 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828}
19829
19830/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019831 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019833 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019835 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836{
19837 if (varp != NULL)
19838 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019839 switch (varp->v_type)
19840 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019841 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019842 func_unref(varp->vval.v_string);
19843 /*FALLTHROUGH*/
19844 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019845 vim_free(varp->vval.v_string);
19846 break;
19847 case VAR_LIST:
19848 list_unref(varp->vval.v_list);
19849 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019850 case VAR_DICT:
19851 dict_unref(varp->vval.v_dict);
19852 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019853 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019854#ifdef FEAT_FLOAT
19855 case VAR_FLOAT:
19856#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019857 case VAR_UNKNOWN:
19858 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019859 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019860 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019861 break;
19862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863 vim_free(varp);
19864 }
19865}
19866
19867/*
19868 * Free the memory for a variable value and set the value to NULL or 0.
19869 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019870 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019871clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019872 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873{
19874 if (varp != NULL)
19875 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019876 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019878 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019879 func_unref(varp->vval.v_string);
19880 /*FALLTHROUGH*/
19881 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019882 vim_free(varp->vval.v_string);
19883 varp->vval.v_string = NULL;
19884 break;
19885 case VAR_LIST:
19886 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019887 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019888 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019889 case VAR_DICT:
19890 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019891 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019892 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019893 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019894 varp->vval.v_number = 0;
19895 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019896#ifdef FEAT_FLOAT
19897 case VAR_FLOAT:
19898 varp->vval.v_float = 0.0;
19899 break;
19900#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019901 case VAR_UNKNOWN:
19902 break;
19903 default:
19904 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019906 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 }
19908}
19909
19910/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019911 * Set the value of a variable to NULL without freeing items.
19912 */
19913 static void
19914init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019915 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019916{
19917 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019918 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019919}
19920
19921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922 * Get the number value of a variable.
19923 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019924 * For incompatible types, return 0.
19925 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19926 * caller of incompatible types: it sets *denote to TRUE if "denote"
19927 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928 */
19929 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019930get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019931 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019933 int error = FALSE;
19934
19935 return get_tv_number_chk(varp, &error); /* return 0L on error */
19936}
19937
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019938 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019939get_tv_number_chk(varp, denote)
19940 typval_T *varp;
19941 int *denote;
19942{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019943 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019945 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019947 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019948 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019949#ifdef FEAT_FLOAT
19950 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019951 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019952 break;
19953#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019954 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019955 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019956 break;
19957 case VAR_STRING:
19958 if (varp->vval.v_string != NULL)
19959 vim_str2nr(varp->vval.v_string, NULL, NULL,
19960 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019961 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019962 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019963 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019964 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019965 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019966 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019967 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019968 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019969 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019970 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019972 if (denote == NULL) /* useful for values that must be unsigned */
19973 n = -1;
19974 else
19975 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019976 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977}
19978
19979/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019980 * Get the lnum from the first argument.
19981 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019982 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 */
19984 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019985get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019986 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987{
Bram Moolenaar33570922005-01-25 22:26:29 +000019988 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 linenr_T lnum;
19990
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019991 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 if (lnum == 0) /* no valid number, try using line() */
19993 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019994 rettv.v_type = VAR_NUMBER;
19995 f_line(argvars, &rettv);
19996 lnum = rettv.vval.v_number;
19997 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998 }
19999 return lnum;
20000}
20001
20002/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020003 * Get the lnum from the first argument.
20004 * Also accepts "$", then "buf" is used.
20005 * Returns 0 on error.
20006 */
20007 static linenr_T
20008get_tv_lnum_buf(argvars, buf)
20009 typval_T *argvars;
20010 buf_T *buf;
20011{
20012 if (argvars[0].v_type == VAR_STRING
20013 && argvars[0].vval.v_string != NULL
20014 && argvars[0].vval.v_string[0] == '$'
20015 && buf != NULL)
20016 return buf->b_ml.ml_line_count;
20017 return get_tv_number_chk(&argvars[0], NULL);
20018}
20019
20020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021 * Get the string value of a variable.
20022 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020023 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20024 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025 * If the String variable has never been set, return an empty string.
20026 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020027 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20028 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029 */
20030 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020031get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020032 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033{
20034 static char_u mybuf[NUMBUFLEN];
20035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020036 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037}
20038
20039 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020040get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020041 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020042 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020044 char_u *res = get_tv_string_buf_chk(varp, buf);
20045
20046 return res != NULL ? res : (char_u *)"";
20047}
20048
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020049 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020050get_tv_string_chk(varp)
20051 typval_T *varp;
20052{
20053 static char_u mybuf[NUMBUFLEN];
20054
20055 return get_tv_string_buf_chk(varp, mybuf);
20056}
20057
20058 static char_u *
20059get_tv_string_buf_chk(varp, buf)
20060 typval_T *varp;
20061 char_u *buf;
20062{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020063 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020065 case VAR_NUMBER:
20066 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20067 return buf;
20068 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020069 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020070 break;
20071 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020072 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020073 break;
20074 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020075 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020076 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020077#ifdef FEAT_FLOAT
20078 case VAR_FLOAT:
20079 EMSG(_("E806: using Float as a String"));
20080 break;
20081#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020082 case VAR_STRING:
20083 if (varp->vval.v_string != NULL)
20084 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020085 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020086 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020087 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020088 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020090 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091}
20092
20093/*
20094 * Find variable "name" in the list of variables.
20095 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020096 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020097 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020098 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020100 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020101find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020103 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020106 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107
Bram Moolenaara7043832005-01-21 11:56:39 +000020108 ht = find_var_ht(name, &varname);
20109 if (htp != NULL)
20110 *htp = ht;
20111 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020113 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114}
20115
20116/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020117 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020118 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020120 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020121find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020122 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020123 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020124 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020125 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020126{
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 hashitem_T *hi;
20128
20129 if (*varname == NUL)
20130 {
20131 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020132 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020133 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020134 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020135 case 'g': return &globvars_var;
20136 case 'v': return &vimvars_var;
20137 case 'b': return &curbuf->b_bufvar;
20138 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020139#ifdef FEAT_WINDOWS
20140 case 't': return &curtab->tp_winvar;
20141#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020142 case 'l': return current_funccal == NULL
20143 ? NULL : &current_funccal->l_vars_var;
20144 case 'a': return current_funccal == NULL
20145 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020146 }
20147 return NULL;
20148 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020149
20150 hi = hash_find(ht, varname);
20151 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020152 {
20153 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020154 * worked find the variable again. Don't auto-load a script if it was
20155 * loaded already, otherwise it would be loaded every time when
20156 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020157 if (ht == &globvarht && !writing)
20158 {
20159 /* Note: script_autoload() may make "hi" invalid. It must either
20160 * be obtained again or not used. */
20161 if (!script_autoload(varname, FALSE) || aborting())
20162 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020163 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020164 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020165 if (HASHITEM_EMPTY(hi))
20166 return NULL;
20167 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020168 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020169}
20170
20171/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020172 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020173 * Set "varname" to the start of name without ':'.
20174 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020175 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020176find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 char_u *name;
20178 char_u **varname;
20179{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020180 hashitem_T *hi;
20181
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182 if (name[1] != ':')
20183 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020184 /* The name must not start with a colon or #. */
20185 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186 return NULL;
20187 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020188
20189 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020190 hi = hash_find(&compat_hashtab, name);
20191 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020192 return &compat_hashtab;
20193
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020195 return &globvarht; /* global variable */
20196 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197 }
20198 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020199 if (*name == 'g') /* global variable */
20200 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020201 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20202 */
20203 if (vim_strchr(name + 2, ':') != NULL
20204 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020205 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020207 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020209 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020210#ifdef FEAT_WINDOWS
20211 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020212 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020213#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020214 if (*name == 'v') /* v: variable */
20215 return &vimvarht;
20216 if (*name == 'a' && current_funccal != NULL) /* function argument */
20217 return &current_funccal->l_avars.dv_hashtab;
20218 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20219 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 if (*name == 's' /* script variable */
20221 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20222 return &SCRIPT_VARS(current_SID);
20223 return NULL;
20224}
20225
20226/*
20227 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020228 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 * Returns NULL when it doesn't exist.
20230 */
20231 char_u *
20232get_var_value(name)
20233 char_u *name;
20234{
Bram Moolenaar33570922005-01-25 22:26:29 +000020235 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236
Bram Moolenaara7043832005-01-21 11:56:39 +000020237 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 if (v == NULL)
20239 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020240 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241}
20242
20243/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020244 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245 * sourcing this script and when executing functions defined in the script.
20246 */
20247 void
20248new_script_vars(id)
20249 scid_T id;
20250{
Bram Moolenaara7043832005-01-21 11:56:39 +000020251 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020252 hashtab_T *ht;
20253 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020254
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20256 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020257 /* Re-allocating ga_data means that an ht_array pointing to
20258 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020259 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020260 for (i = 1; i <= ga_scripts.ga_len; ++i)
20261 {
20262 ht = &SCRIPT_VARS(i);
20263 if (ht->ht_mask == HT_INIT_SIZE - 1)
20264 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020265 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020266 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020267 }
20268
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269 while (ga_scripts.ga_len < id)
20270 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020271 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020272 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020273 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 }
20276 }
20277}
20278
20279/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020280 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20281 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 */
20283 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020284init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020285 dict_T *dict;
20286 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020287 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288{
Bram Moolenaar33570922005-01-25 22:26:29 +000020289 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020290 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020291 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020292 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020293 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020294 dict_var->di_tv.vval.v_dict = dict;
20295 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020296 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020297 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20298 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299}
20300
20301/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020302 * Unreference a dictionary initialized by init_var_dict().
20303 */
20304 void
20305unref_var_dict(dict)
20306 dict_T *dict;
20307{
20308 /* Now the dict needs to be freed if no one else is using it, go back to
20309 * normal reference counting. */
20310 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20311 dict_unref(dict);
20312}
20313
20314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020316 * Frees all allocated variables and the value they contain.
20317 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 */
20319 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020320vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020321 hashtab_T *ht;
20322{
20323 vars_clear_ext(ht, TRUE);
20324}
20325
20326/*
20327 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20328 */
20329 static void
20330vars_clear_ext(ht, free_val)
20331 hashtab_T *ht;
20332 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333{
Bram Moolenaara7043832005-01-21 11:56:39 +000020334 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020335 hashitem_T *hi;
20336 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337
Bram Moolenaar33570922005-01-25 22:26:29 +000020338 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020339 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020340 for (hi = ht->ht_array; todo > 0; ++hi)
20341 {
20342 if (!HASHITEM_EMPTY(hi))
20343 {
20344 --todo;
20345
Bram Moolenaar33570922005-01-25 22:26:29 +000020346 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020347 * ht_array might change then. hash_clear() takes care of it
20348 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020349 v = HI2DI(hi);
20350 if (free_val)
20351 clear_tv(&v->di_tv);
20352 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20353 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020354 }
20355 }
20356 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020357 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358}
20359
Bram Moolenaara7043832005-01-21 11:56:39 +000020360/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020361 * Delete a variable from hashtab "ht" at item "hi".
20362 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020365delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020366 hashtab_T *ht;
20367 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368{
Bram Moolenaar33570922005-01-25 22:26:29 +000020369 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020370
20371 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020372 clear_tv(&di->di_tv);
20373 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374}
20375
20376/*
20377 * List the value of one internal variable.
20378 */
20379 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020380list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020381 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020383 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020385 char_u *tofree;
20386 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020387 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020388
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020389 current_copyID += COPYID_INC;
20390 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020391 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020392 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020393 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394}
20395
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020397list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398 char_u *prefix;
20399 char_u *name;
20400 int type;
20401 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020402 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403{
Bram Moolenaar31859182007-08-14 20:41:13 +000020404 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20405 msg_start();
20406 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 if (name != NULL) /* "a:" vars don't have a name stored */
20408 msg_puts(name);
20409 msg_putchar(' ');
20410 msg_advance(22);
20411 if (type == VAR_NUMBER)
20412 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020413 else if (type == VAR_FUNC)
20414 msg_putchar('*');
20415 else if (type == VAR_LIST)
20416 {
20417 msg_putchar('[');
20418 if (*string == '[')
20419 ++string;
20420 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020421 else if (type == VAR_DICT)
20422 {
20423 msg_putchar('{');
20424 if (*string == '{')
20425 ++string;
20426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427 else
20428 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020429
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020431
20432 if (type == VAR_FUNC)
20433 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020434 if (*first)
20435 {
20436 msg_clr_eos();
20437 *first = FALSE;
20438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439}
20440
20441/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020442 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443 * If the variable already exists, the value is updated.
20444 * Otherwise the variable is created.
20445 */
20446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020447set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020449 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020450 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451{
Bram Moolenaar33570922005-01-25 22:26:29 +000020452 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020454 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020456 ht = find_var_ht(name, &varname);
20457 if (ht == NULL || *varname == NUL)
20458 {
20459 EMSG2(_(e_illvar), name);
20460 return;
20461 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020462 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020463
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020464 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20465 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020466
Bram Moolenaar33570922005-01-25 22:26:29 +000020467 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020469 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020470 if (var_check_ro(v->di_flags, name)
20471 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020472 return;
20473 if (v->di_tv.v_type != tv->v_type
20474 && !((v->di_tv.v_type == VAR_STRING
20475 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020476 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020477 || tv->v_type == VAR_NUMBER))
20478#ifdef FEAT_FLOAT
20479 && !((v->di_tv.v_type == VAR_NUMBER
20480 || v->di_tv.v_type == VAR_FLOAT)
20481 && (tv->v_type == VAR_NUMBER
20482 || tv->v_type == VAR_FLOAT))
20483#endif
20484 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020485 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020486 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020487 return;
20488 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020489
20490 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020491 * Handle setting internal v: variables separately: we don't change
20492 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020493 */
20494 if (ht == &vimvarht)
20495 {
20496 if (v->di_tv.v_type == VAR_STRING)
20497 {
20498 vim_free(v->di_tv.vval.v_string);
20499 if (copy || tv->v_type != VAR_STRING)
20500 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20501 else
20502 {
20503 /* Take over the string to avoid an extra alloc/free. */
20504 v->di_tv.vval.v_string = tv->vval.v_string;
20505 tv->vval.v_string = NULL;
20506 }
20507 }
20508 else if (v->di_tv.v_type != VAR_NUMBER)
20509 EMSG2(_(e_intern2), "set_var()");
20510 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020511 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020512 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020513 if (STRCMP(varname, "searchforward") == 0)
20514 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20515 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020516 return;
20517 }
20518
20519 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 }
20521 else /* add a new variable */
20522 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020523 /* Can't add "v:" variable. */
20524 if (ht == &vimvarht)
20525 {
20526 EMSG2(_(e_illvar), name);
20527 return;
20528 }
20529
Bram Moolenaar92124a32005-06-17 22:03:40 +000020530 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020531 if (!valid_varname(varname))
20532 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020533
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020534 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20535 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020536 if (v == NULL)
20537 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020538 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020539 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020541 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542 return;
20543 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020544 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020546
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020547 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020548 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020549 else
20550 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020551 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020552 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020553 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555}
20556
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020557/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020558 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020559 * Also give an error message.
20560 */
20561 static int
20562var_check_ro(flags, name)
20563 int flags;
20564 char_u *name;
20565{
20566 if (flags & DI_FLAGS_RO)
20567 {
20568 EMSG2(_(e_readonlyvar), name);
20569 return TRUE;
20570 }
20571 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20572 {
20573 EMSG2(_(e_readonlysbx), name);
20574 return TRUE;
20575 }
20576 return FALSE;
20577}
20578
20579/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020580 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20581 * Also give an error message.
20582 */
20583 static int
20584var_check_fixed(flags, name)
20585 int flags;
20586 char_u *name;
20587{
20588 if (flags & DI_FLAGS_FIX)
20589 {
20590 EMSG2(_("E795: Cannot delete variable %s"), name);
20591 return TRUE;
20592 }
20593 return FALSE;
20594}
20595
20596/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020597 * Check if a funcref is assigned to a valid variable name.
20598 * Return TRUE and give an error if not.
20599 */
20600 static int
20601var_check_func_name(name, new_var)
20602 char_u *name; /* points to start of variable name */
20603 int new_var; /* TRUE when creating the variable */
20604{
20605 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20606 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20607 ? name[2] : name[0]))
20608 {
20609 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20610 name);
20611 return TRUE;
20612 }
20613 /* Don't allow hiding a function. When "v" is not NULL we might be
20614 * assigning another function to the same var, the type is checked
20615 * below. */
20616 if (new_var && function_exists(name))
20617 {
20618 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20619 name);
20620 return TRUE;
20621 }
20622 return FALSE;
20623}
20624
20625/*
20626 * Check if a variable name is valid.
20627 * Return FALSE and give an error if not.
20628 */
20629 static int
20630valid_varname(varname)
20631 char_u *varname;
20632{
20633 char_u *p;
20634
20635 for (p = varname; *p != NUL; ++p)
20636 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20637 && *p != AUTOLOAD_CHAR)
20638 {
20639 EMSG2(_(e_illvar), varname);
20640 return FALSE;
20641 }
20642 return TRUE;
20643}
20644
20645/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020646 * Return TRUE if typeval "tv" is set to be locked (immutable).
20647 * Also give an error message, using "name".
20648 */
20649 static int
20650tv_check_lock(lock, name)
20651 int lock;
20652 char_u *name;
20653{
20654 if (lock & VAR_LOCKED)
20655 {
20656 EMSG2(_("E741: Value is locked: %s"),
20657 name == NULL ? (char_u *)_("Unknown") : name);
20658 return TRUE;
20659 }
20660 if (lock & VAR_FIXED)
20661 {
20662 EMSG2(_("E742: Cannot change value of %s"),
20663 name == NULL ? (char_u *)_("Unknown") : name);
20664 return TRUE;
20665 }
20666 return FALSE;
20667}
20668
20669/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020670 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020671 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020672 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020673 * It is OK for "from" and "to" to point to the same item. This is used to
20674 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020675 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020676 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020677copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020678 typval_T *from;
20679 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020681 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020682 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020683 switch (from->v_type)
20684 {
20685 case VAR_NUMBER:
20686 to->vval.v_number = from->vval.v_number;
20687 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020688#ifdef FEAT_FLOAT
20689 case VAR_FLOAT:
20690 to->vval.v_float = from->vval.v_float;
20691 break;
20692#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020693 case VAR_STRING:
20694 case VAR_FUNC:
20695 if (from->vval.v_string == NULL)
20696 to->vval.v_string = NULL;
20697 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020698 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020699 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020700 if (from->v_type == VAR_FUNC)
20701 func_ref(to->vval.v_string);
20702 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020703 break;
20704 case VAR_LIST:
20705 if (from->vval.v_list == NULL)
20706 to->vval.v_list = NULL;
20707 else
20708 {
20709 to->vval.v_list = from->vval.v_list;
20710 ++to->vval.v_list->lv_refcount;
20711 }
20712 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020713 case VAR_DICT:
20714 if (from->vval.v_dict == NULL)
20715 to->vval.v_dict = NULL;
20716 else
20717 {
20718 to->vval.v_dict = from->vval.v_dict;
20719 ++to->vval.v_dict->dv_refcount;
20720 }
20721 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020722 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020723 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020724 break;
20725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726}
20727
20728/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020729 * Make a copy of an item.
20730 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020731 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20732 * reference to an already copied list/dict can be used.
20733 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020734 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020735 static int
20736item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020737 typval_T *from;
20738 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020739 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020740 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020741{
20742 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020743 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020744
Bram Moolenaar33570922005-01-25 22:26:29 +000020745 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020746 {
20747 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020748 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020749 }
20750 ++recurse;
20751
20752 switch (from->v_type)
20753 {
20754 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020755#ifdef FEAT_FLOAT
20756 case VAR_FLOAT:
20757#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020758 case VAR_STRING:
20759 case VAR_FUNC:
20760 copy_tv(from, to);
20761 break;
20762 case VAR_LIST:
20763 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020764 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020765 if (from->vval.v_list == NULL)
20766 to->vval.v_list = NULL;
20767 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20768 {
20769 /* use the copy made earlier */
20770 to->vval.v_list = from->vval.v_list->lv_copylist;
20771 ++to->vval.v_list->lv_refcount;
20772 }
20773 else
20774 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20775 if (to->vval.v_list == NULL)
20776 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020777 break;
20778 case VAR_DICT:
20779 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020780 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020781 if (from->vval.v_dict == NULL)
20782 to->vval.v_dict = NULL;
20783 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20784 {
20785 /* use the copy made earlier */
20786 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20787 ++to->vval.v_dict->dv_refcount;
20788 }
20789 else
20790 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20791 if (to->vval.v_dict == NULL)
20792 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020793 break;
20794 default:
20795 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020796 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020797 }
20798 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020799 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020800}
20801
20802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803 * ":echo expr1 ..." print each argument separated with a space, add a
20804 * newline at the end.
20805 * ":echon expr1 ..." print each argument plain.
20806 */
20807 void
20808ex_echo(eap)
20809 exarg_T *eap;
20810{
20811 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020812 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020813 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814 char_u *p;
20815 int needclr = TRUE;
20816 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020817 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818
20819 if (eap->skip)
20820 ++emsg_skip;
20821 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20822 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020823 /* If eval1() causes an error message the text from the command may
20824 * still need to be cleared. E.g., "echo 22,44". */
20825 need_clr_eos = needclr;
20826
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020828 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 {
20830 /*
20831 * Report the invalid expression unless the expression evaluation
20832 * has been cancelled due to an aborting error, an interrupt, or an
20833 * exception.
20834 */
20835 if (!aborting())
20836 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020837 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838 break;
20839 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020840 need_clr_eos = FALSE;
20841
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 if (!eap->skip)
20843 {
20844 if (atstart)
20845 {
20846 atstart = FALSE;
20847 /* Call msg_start() after eval1(), evaluating the expression
20848 * may cause a message to appear. */
20849 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020850 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020851 /* Mark the saved text as finishing the line, so that what
20852 * follows is displayed on a new line when scrolling back
20853 * at the more prompt. */
20854 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 }
20858 else if (eap->cmdidx == CMD_echo)
20859 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020860 current_copyID += COPYID_INC;
20861 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020862 if (p != NULL)
20863 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020865 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020867 if (*p != TAB && needclr)
20868 {
20869 /* remove any text still there from the command */
20870 msg_clr_eos();
20871 needclr = FALSE;
20872 }
20873 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874 }
20875 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020876 {
20877#ifdef FEAT_MBYTE
20878 if (has_mbyte)
20879 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020880 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020881
20882 (void)msg_outtrans_len_attr(p, i, echo_attr);
20883 p += i - 1;
20884 }
20885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020887 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020890 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020892 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893 arg = skipwhite(arg);
20894 }
20895 eap->nextcmd = check_nextcmd(arg);
20896
20897 if (eap->skip)
20898 --emsg_skip;
20899 else
20900 {
20901 /* remove text that may still be there from the command */
20902 if (needclr)
20903 msg_clr_eos();
20904 if (eap->cmdidx == CMD_echo)
20905 msg_end();
20906 }
20907}
20908
20909/*
20910 * ":echohl {name}".
20911 */
20912 void
20913ex_echohl(eap)
20914 exarg_T *eap;
20915{
20916 int id;
20917
20918 id = syn_name2id(eap->arg);
20919 if (id == 0)
20920 echo_attr = 0;
20921 else
20922 echo_attr = syn_id2attr(id);
20923}
20924
20925/*
20926 * ":execute expr1 ..." execute the result of an expression.
20927 * ":echomsg expr1 ..." Print a message
20928 * ":echoerr expr1 ..." Print an error
20929 * Each gets spaces around each argument and a newline at the end for
20930 * echo commands
20931 */
20932 void
20933ex_execute(eap)
20934 exarg_T *eap;
20935{
20936 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020937 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938 int ret = OK;
20939 char_u *p;
20940 garray_T ga;
20941 int len;
20942 int save_did_emsg;
20943
20944 ga_init2(&ga, 1, 80);
20945
20946 if (eap->skip)
20947 ++emsg_skip;
20948 while (*arg != NUL && *arg != '|' && *arg != '\n')
20949 {
20950 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020951 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952 {
20953 /*
20954 * Report the invalid expression unless the expression evaluation
20955 * has been cancelled due to an aborting error, an interrupt, or an
20956 * exception.
20957 */
20958 if (!aborting())
20959 EMSG2(_(e_invexpr2), p);
20960 ret = FAIL;
20961 break;
20962 }
20963
20964 if (!eap->skip)
20965 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020966 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967 len = (int)STRLEN(p);
20968 if (ga_grow(&ga, len + 2) == FAIL)
20969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020970 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 ret = FAIL;
20972 break;
20973 }
20974 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 ga.ga_len += len;
20978 }
20979
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020980 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020981 arg = skipwhite(arg);
20982 }
20983
20984 if (ret != FAIL && ga.ga_data != NULL)
20985 {
20986 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020987 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020989 out_flush();
20990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991 else if (eap->cmdidx == CMD_echoerr)
20992 {
20993 /* We don't want to abort following commands, restore did_emsg. */
20994 save_did_emsg = did_emsg;
20995 EMSG((char_u *)ga.ga_data);
20996 if (!force_abort)
20997 did_emsg = save_did_emsg;
20998 }
20999 else if (eap->cmdidx == CMD_execute)
21000 do_cmdline((char_u *)ga.ga_data,
21001 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21002 }
21003
21004 ga_clear(&ga);
21005
21006 if (eap->skip)
21007 --emsg_skip;
21008
21009 eap->nextcmd = check_nextcmd(arg);
21010}
21011
21012/*
21013 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21014 * "arg" points to the "&" or '+' when called, to "option" when returning.
21015 * Returns NULL when no option name found. Otherwise pointer to the char
21016 * after the option name.
21017 */
21018 static char_u *
21019find_option_end(arg, opt_flags)
21020 char_u **arg;
21021 int *opt_flags;
21022{
21023 char_u *p = *arg;
21024
21025 ++p;
21026 if (*p == 'g' && p[1] == ':')
21027 {
21028 *opt_flags = OPT_GLOBAL;
21029 p += 2;
21030 }
21031 else if (*p == 'l' && p[1] == ':')
21032 {
21033 *opt_flags = OPT_LOCAL;
21034 p += 2;
21035 }
21036 else
21037 *opt_flags = 0;
21038
21039 if (!ASCII_ISALPHA(*p))
21040 return NULL;
21041 *arg = p;
21042
21043 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21044 p += 4; /* termcap option */
21045 else
21046 while (ASCII_ISALPHA(*p))
21047 ++p;
21048 return p;
21049}
21050
21051/*
21052 * ":function"
21053 */
21054 void
21055ex_function(eap)
21056 exarg_T *eap;
21057{
21058 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021059 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021060 int j;
21061 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 char_u *name = NULL;
21064 char_u *p;
21065 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021066 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067 garray_T newargs;
21068 garray_T newlines;
21069 int varargs = FALSE;
21070 int mustend = FALSE;
21071 int flags = 0;
21072 ufunc_T *fp;
21073 int indent;
21074 int nesting;
21075 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021076 dictitem_T *v;
21077 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021078 static int func_nr = 0; /* number for nameless function */
21079 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021080 hashtab_T *ht;
21081 int todo;
21082 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021083 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084
21085 /*
21086 * ":function" without argument: list functions.
21087 */
21088 if (ends_excmd(*eap->arg))
21089 {
21090 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021091 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021092 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021093 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021094 {
21095 if (!HASHITEM_EMPTY(hi))
21096 {
21097 --todo;
21098 fp = HI2UF(hi);
21099 if (!isdigit(*fp->uf_name))
21100 list_func_head(fp, FALSE);
21101 }
21102 }
21103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 eap->nextcmd = check_nextcmd(eap->arg);
21105 return;
21106 }
21107
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021108 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021109 * ":function /pat": list functions matching pattern.
21110 */
21111 if (*eap->arg == '/')
21112 {
21113 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21114 if (!eap->skip)
21115 {
21116 regmatch_T regmatch;
21117
21118 c = *p;
21119 *p = NUL;
21120 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21121 *p = c;
21122 if (regmatch.regprog != NULL)
21123 {
21124 regmatch.rm_ic = p_ic;
21125
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021126 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021127 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21128 {
21129 if (!HASHITEM_EMPTY(hi))
21130 {
21131 --todo;
21132 fp = HI2UF(hi);
21133 if (!isdigit(*fp->uf_name)
21134 && vim_regexec(&regmatch, fp->uf_name, 0))
21135 list_func_head(fp, FALSE);
21136 }
21137 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021138 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021139 }
21140 }
21141 if (*p == '/')
21142 ++p;
21143 eap->nextcmd = check_nextcmd(p);
21144 return;
21145 }
21146
21147 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021148 * Get the function name. There are these situations:
21149 * func normal function name
21150 * "name" == func, "fudi.fd_dict" == NULL
21151 * dict.func new dictionary entry
21152 * "name" == NULL, "fudi.fd_dict" set,
21153 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21154 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021155 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021156 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21157 * dict.func existing dict entry that's not a Funcref
21158 * "name" == NULL, "fudi.fd_dict" set,
21159 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021162 name = trans_function_name(&p, eap->skip, 0, &fudi);
21163 paren = (vim_strchr(p, '(') != NULL);
21164 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 {
21166 /*
21167 * Return on an invalid expression in braces, unless the expression
21168 * evaluation has been cancelled due to an aborting error, an
21169 * interrupt, or an exception.
21170 */
21171 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021172 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021173 if (!eap->skip && fudi.fd_newkey != NULL)
21174 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021175 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 else
21179 eap->skip = TRUE;
21180 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021181
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182 /* An error in a function call during evaluation of an expression in magic
21183 * braces should not cause the function not to be defined. */
21184 saved_did_emsg = did_emsg;
21185 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186
21187 /*
21188 * ":function func" with only function name: list function.
21189 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021190 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 {
21192 if (!ends_excmd(*skipwhite(p)))
21193 {
21194 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021195 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 }
21197 eap->nextcmd = check_nextcmd(p);
21198 if (eap->nextcmd != NULL)
21199 *p = NUL;
21200 if (!eap->skip && !got_int)
21201 {
21202 fp = find_func(name);
21203 if (fp != NULL)
21204 {
21205 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021206 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021208 if (FUNCLINE(fp, j) == NULL)
21209 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021210 msg_putchar('\n');
21211 msg_outnum((long)(j + 1));
21212 if (j < 9)
21213 msg_putchar(' ');
21214 if (j < 99)
21215 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021216 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021217 out_flush(); /* show a line at a time */
21218 ui_breakcheck();
21219 }
21220 if (!got_int)
21221 {
21222 msg_putchar('\n');
21223 msg_puts((char_u *)" endfunction");
21224 }
21225 }
21226 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021227 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021229 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 }
21231
21232 /*
21233 * ":function name(arg1, arg2)" Define function.
21234 */
21235 p = skipwhite(p);
21236 if (*p != '(')
21237 {
21238 if (!eap->skip)
21239 {
21240 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021241 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242 }
21243 /* attempt to continue by skipping some text */
21244 if (vim_strchr(p, '(') != NULL)
21245 p = vim_strchr(p, '(');
21246 }
21247 p = skipwhite(p + 1);
21248
21249 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21250 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21251
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021252 if (!eap->skip)
21253 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021254 /* Check the name of the function. Unless it's a dictionary function
21255 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021256 if (name != NULL)
21257 arg = name;
21258 else
21259 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021260 if (arg != NULL && (fudi.fd_di == NULL
21261 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021262 {
21263 if (*arg == K_SPECIAL)
21264 j = 3;
21265 else
21266 j = 0;
21267 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21268 : eval_isnamec(arg[j])))
21269 ++j;
21270 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021271 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021272 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021273 /* Disallow using the g: dict. */
21274 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21275 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021276 }
21277
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 /*
21279 * Isolate the arguments: "arg1, arg2, ...)"
21280 */
21281 while (*p != ')')
21282 {
21283 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21284 {
21285 varargs = TRUE;
21286 p += 3;
21287 mustend = TRUE;
21288 }
21289 else
21290 {
21291 arg = p;
21292 while (ASCII_ISALNUM(*p) || *p == '_')
21293 ++p;
21294 if (arg == p || isdigit(*arg)
21295 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21296 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21297 {
21298 if (!eap->skip)
21299 EMSG2(_("E125: Illegal argument: %s"), arg);
21300 break;
21301 }
21302 if (ga_grow(&newargs, 1) == FAIL)
21303 goto erret;
21304 c = *p;
21305 *p = NUL;
21306 arg = vim_strsave(arg);
21307 if (arg == NULL)
21308 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021309
21310 /* Check for duplicate argument name. */
21311 for (i = 0; i < newargs.ga_len; ++i)
21312 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21313 {
21314 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21315 goto erret;
21316 }
21317
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21319 *p = c;
21320 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321 if (*p == ',')
21322 ++p;
21323 else
21324 mustend = TRUE;
21325 }
21326 p = skipwhite(p);
21327 if (mustend && *p != ')')
21328 {
21329 if (!eap->skip)
21330 EMSG2(_(e_invarg2), eap->arg);
21331 break;
21332 }
21333 }
21334 ++p; /* skip the ')' */
21335
Bram Moolenaare9a41262005-01-15 22:18:47 +000021336 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 for (;;)
21338 {
21339 p = skipwhite(p);
21340 if (STRNCMP(p, "range", 5) == 0)
21341 {
21342 flags |= FC_RANGE;
21343 p += 5;
21344 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021345 else if (STRNCMP(p, "dict", 4) == 0)
21346 {
21347 flags |= FC_DICT;
21348 p += 4;
21349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 else if (STRNCMP(p, "abort", 5) == 0)
21351 {
21352 flags |= FC_ABORT;
21353 p += 5;
21354 }
21355 else
21356 break;
21357 }
21358
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021359 /* When there is a line break use what follows for the function body.
21360 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21361 if (*p == '\n')
21362 line_arg = p + 1;
21363 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364 EMSG(_(e_trailing));
21365
21366 /*
21367 * Read the body of the function, until ":endfunction" is found.
21368 */
21369 if (KeyTyped)
21370 {
21371 /* Check if the function already exists, don't let the user type the
21372 * whole function before telling him it doesn't work! For a script we
21373 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021374 if (!eap->skip && !eap->forceit)
21375 {
21376 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21377 EMSG(_(e_funcdict));
21378 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021379 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021382 if (!eap->skip && did_emsg)
21383 goto erret;
21384
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 msg_putchar('\n'); /* don't overwrite the function name */
21386 cmdline_row = msg_row;
21387 }
21388
21389 indent = 2;
21390 nesting = 0;
21391 for (;;)
21392 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021393 if (KeyTyped)
21394 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021396 sourcing_lnum_off = sourcing_lnum;
21397
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021398 if (line_arg != NULL)
21399 {
21400 /* Use eap->arg, split up in parts by line breaks. */
21401 theline = line_arg;
21402 p = vim_strchr(theline, '\n');
21403 if (p == NULL)
21404 line_arg += STRLEN(line_arg);
21405 else
21406 {
21407 *p = NUL;
21408 line_arg = p + 1;
21409 }
21410 }
21411 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 theline = getcmdline(':', 0L, indent);
21413 else
21414 theline = eap->getline(':', eap->cookie, indent);
21415 if (KeyTyped)
21416 lines_left = Rows - 1;
21417 if (theline == NULL)
21418 {
21419 EMSG(_("E126: Missing :endfunction"));
21420 goto erret;
21421 }
21422
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021423 /* Detect line continuation: sourcing_lnum increased more than one. */
21424 if (sourcing_lnum > sourcing_lnum_off + 1)
21425 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21426 else
21427 sourcing_lnum_off = 0;
21428
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 if (skip_until != NULL)
21430 {
21431 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21432 * don't check for ":endfunc". */
21433 if (STRCMP(theline, skip_until) == 0)
21434 {
21435 vim_free(skip_until);
21436 skip_until = NULL;
21437 }
21438 }
21439 else
21440 {
21441 /* skip ':' and blanks*/
21442 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21443 ;
21444
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021445 /* Check for "endfunction". */
21446 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021448 if (line_arg == NULL)
21449 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 break;
21451 }
21452
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021453 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454 * at "end". */
21455 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21456 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021457 else if (STRNCMP(p, "if", 2) == 0
21458 || STRNCMP(p, "wh", 2) == 0
21459 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460 || STRNCMP(p, "try", 3) == 0)
21461 indent += 2;
21462
21463 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021464 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021466 if (*p == '!')
21467 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468 p += eval_fname_script(p);
21469 if (ASCII_ISALPHA(*p))
21470 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021471 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 if (*skipwhite(p) == '(')
21473 {
21474 ++nesting;
21475 indent += 2;
21476 }
21477 }
21478 }
21479
21480 /* Check for ":append" or ":insert". */
21481 p = skip_range(p, NULL);
21482 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21483 || (p[0] == 'i'
21484 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21485 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21486 skip_until = vim_strsave((char_u *)".");
21487
21488 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21489 arg = skipwhite(skiptowhite(p));
21490 if (arg[0] == '<' && arg[1] =='<'
21491 && ((p[0] == 'p' && p[1] == 'y'
21492 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21493 || (p[0] == 'p' && p[1] == 'e'
21494 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21495 || (p[0] == 't' && p[1] == 'c'
21496 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021497 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21498 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21500 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021501 || (p[0] == 'm' && p[1] == 'z'
21502 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503 ))
21504 {
21505 /* ":python <<" continues until a dot, like ":append" */
21506 p = skipwhite(arg + 2);
21507 if (*p == NUL)
21508 skip_until = vim_strsave((char_u *)".");
21509 else
21510 skip_until = vim_strsave(p);
21511 }
21512 }
21513
21514 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021515 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021516 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021517 if (line_arg == NULL)
21518 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021520 }
21521
21522 /* Copy the line to newly allocated memory. get_one_sourceline()
21523 * allocates 250 bytes per line, this saves 80% on average. The cost
21524 * is an extra alloc/free. */
21525 p = vim_strsave(theline);
21526 if (p != NULL)
21527 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021528 if (line_arg == NULL)
21529 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021530 theline = p;
21531 }
21532
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021533 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21534
21535 /* Add NULL lines for continuation lines, so that the line count is
21536 * equal to the index in the growarray. */
21537 while (sourcing_lnum_off-- > 0)
21538 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021539
21540 /* Check for end of eap->arg. */
21541 if (line_arg != NULL && *line_arg == NUL)
21542 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543 }
21544
21545 /* Don't define the function when skipping commands or when an error was
21546 * detected. */
21547 if (eap->skip || did_emsg)
21548 goto erret;
21549
21550 /*
21551 * If there are no errors, add the function
21552 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021553 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021554 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021555 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021556 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021557 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021558 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021559 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021560 goto erret;
21561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021562
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021563 fp = find_func(name);
21564 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021566 if (!eap->forceit)
21567 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021568 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021569 goto erret;
21570 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021571 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021572 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021573 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021574 name);
21575 goto erret;
21576 }
21577 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021578 ga_clear_strings(&(fp->uf_args));
21579 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021580 vim_free(name);
21581 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 }
21584 else
21585 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021586 char numbuf[20];
21587
21588 fp = NULL;
21589 if (fudi.fd_newkey == NULL && !eap->forceit)
21590 {
21591 EMSG(_(e_funcdict));
21592 goto erret;
21593 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021594 if (fudi.fd_di == NULL)
21595 {
21596 /* Can't add a function to a locked dictionary */
21597 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21598 goto erret;
21599 }
21600 /* Can't change an existing function if it is locked */
21601 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21602 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021603
21604 /* Give the function a sequential number. Can only be used with a
21605 * Funcref! */
21606 vim_free(name);
21607 sprintf(numbuf, "%d", ++func_nr);
21608 name = vim_strsave((char_u *)numbuf);
21609 if (name == NULL)
21610 goto erret;
21611 }
21612
21613 if (fp == NULL)
21614 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021615 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021616 {
21617 int slen, plen;
21618 char_u *scriptname;
21619
21620 /* Check that the autoload name matches the script name. */
21621 j = FAIL;
21622 if (sourcing_name != NULL)
21623 {
21624 scriptname = autoload_name(name);
21625 if (scriptname != NULL)
21626 {
21627 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021628 plen = (int)STRLEN(p);
21629 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021630 if (slen > plen && fnamecmp(p,
21631 sourcing_name + slen - plen) == 0)
21632 j = OK;
21633 vim_free(scriptname);
21634 }
21635 }
21636 if (j == FAIL)
21637 {
21638 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21639 goto erret;
21640 }
21641 }
21642
21643 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 if (fp == NULL)
21645 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021646
21647 if (fudi.fd_dict != NULL)
21648 {
21649 if (fudi.fd_di == NULL)
21650 {
21651 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021652 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021653 if (fudi.fd_di == NULL)
21654 {
21655 vim_free(fp);
21656 goto erret;
21657 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021658 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21659 {
21660 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021661 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021662 goto erret;
21663 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021664 }
21665 else
21666 /* overwrite existing dict entry */
21667 clear_tv(&fudi.fd_di->di_tv);
21668 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021669 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021670 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021671 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021672
21673 /* behave like "dict" was used */
21674 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021675 }
21676
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021678 STRCPY(fp->uf_name, name);
21679 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021681 fp->uf_args = newargs;
21682 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021683#ifdef FEAT_PROFILE
21684 fp->uf_tml_count = NULL;
21685 fp->uf_tml_total = NULL;
21686 fp->uf_tml_self = NULL;
21687 fp->uf_profiling = FALSE;
21688 if (prof_def_func())
21689 func_do_profile(fp);
21690#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021691 fp->uf_varargs = varargs;
21692 fp->uf_flags = flags;
21693 fp->uf_calls = 0;
21694 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021695 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696
21697erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698 ga_clear_strings(&newargs);
21699 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021700ret_free:
21701 vim_free(skip_until);
21702 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705}
21706
21707/*
21708 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021709 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021711 * flags:
21712 * TFN_INT: internal function name OK
21713 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714 * Advances "pp" to just after the function name (if no error).
21715 */
21716 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021717trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 char_u **pp;
21719 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021720 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021721 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021723 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724 char_u *start;
21725 char_u *end;
21726 int lead;
21727 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021729 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021730
21731 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021732 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021734
21735 /* Check for hard coded <SNR>: already translated function ID (from a user
21736 * command). */
21737 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21738 && (*pp)[2] == (int)KE_SNR)
21739 {
21740 *pp += 3;
21741 len = get_id_len(pp) + 3;
21742 return vim_strnsave(start, len);
21743 }
21744
21745 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21746 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021748 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021750
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021751 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21752 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021753 if (end == start)
21754 {
21755 if (!skip)
21756 EMSG(_("E129: Function name required"));
21757 goto theend;
21758 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021759 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021760 {
21761 /*
21762 * Report an invalid expression in braces, unless the expression
21763 * evaluation has been cancelled due to an aborting error, an
21764 * interrupt, or an exception.
21765 */
21766 if (!aborting())
21767 {
21768 if (end != NULL)
21769 EMSG2(_(e_invarg2), start);
21770 }
21771 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021772 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021773 goto theend;
21774 }
21775
21776 if (lv.ll_tv != NULL)
21777 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021778 if (fdp != NULL)
21779 {
21780 fdp->fd_dict = lv.ll_dict;
21781 fdp->fd_newkey = lv.ll_newkey;
21782 lv.ll_newkey = NULL;
21783 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021784 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021785 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21786 {
21787 name = vim_strsave(lv.ll_tv->vval.v_string);
21788 *pp = end;
21789 }
21790 else
21791 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021792 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21793 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021794 EMSG(_(e_funcref));
21795 else
21796 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021797 name = NULL;
21798 }
21799 goto theend;
21800 }
21801
21802 if (lv.ll_name == NULL)
21803 {
21804 /* Error found, but continue after the function name. */
21805 *pp = end;
21806 goto theend;
21807 }
21808
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021809 /* Check if the name is a Funcref. If so, use the value. */
21810 if (lv.ll_exp_name != NULL)
21811 {
21812 len = (int)STRLEN(lv.ll_exp_name);
21813 name = deref_func_name(lv.ll_exp_name, &len);
21814 if (name == lv.ll_exp_name)
21815 name = NULL;
21816 }
21817 else
21818 {
21819 len = (int)(end - *pp);
21820 name = deref_func_name(*pp, &len);
21821 if (name == *pp)
21822 name = NULL;
21823 }
21824 if (name != NULL)
21825 {
21826 name = vim_strsave(name);
21827 *pp = end;
21828 goto theend;
21829 }
21830
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021831 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021832 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021833 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021834 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21835 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21836 {
21837 /* When there was "s:" already or the name expanded to get a
21838 * leading "s:" then remove it. */
21839 lv.ll_name += 2;
21840 len -= 2;
21841 lead = 2;
21842 }
21843 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021844 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021845 {
21846 if (lead == 2) /* skip over "s:" */
21847 lv.ll_name += 2;
21848 len = (int)(end - lv.ll_name);
21849 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021850
21851 /*
21852 * Copy the function name to allocated memory.
21853 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21854 * Accept <SNR>123_name() outside a script.
21855 */
21856 if (skip)
21857 lead = 0; /* do nothing */
21858 else if (lead > 0)
21859 {
21860 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021861 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21862 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021863 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021864 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021865 if (current_SID <= 0)
21866 {
21867 EMSG(_(e_usingsid));
21868 goto theend;
21869 }
21870 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21871 lead += (int)STRLEN(sid_buf);
21872 }
21873 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021874 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021875 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021876 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021877 goto theend;
21878 }
21879 name = alloc((unsigned)(len + lead + 1));
21880 if (name != NULL)
21881 {
21882 if (lead > 0)
21883 {
21884 name[0] = K_SPECIAL;
21885 name[1] = KS_EXTRA;
21886 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021887 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021888 STRCPY(name + 3, sid_buf);
21889 }
21890 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21891 name[len + lead] = NUL;
21892 }
21893 *pp = end;
21894
21895theend:
21896 clear_lval(&lv);
21897 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898}
21899
21900/*
21901 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21902 * Return 2 if "p" starts with "s:".
21903 * Return 0 otherwise.
21904 */
21905 static int
21906eval_fname_script(p)
21907 char_u *p;
21908{
21909 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21910 || STRNICMP(p + 1, "SNR>", 4) == 0))
21911 return 5;
21912 if (p[0] == 's' && p[1] == ':')
21913 return 2;
21914 return 0;
21915}
21916
21917/*
21918 * Return TRUE if "p" starts with "<SID>" or "s:".
21919 * Only works if eval_fname_script() returned non-zero for "p"!
21920 */
21921 static int
21922eval_fname_sid(p)
21923 char_u *p;
21924{
21925 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21926}
21927
21928/*
21929 * List the head of the function: "name(arg1, arg2)".
21930 */
21931 static void
21932list_func_head(fp, indent)
21933 ufunc_T *fp;
21934 int indent;
21935{
21936 int j;
21937
21938 msg_start();
21939 if (indent)
21940 MSG_PUTS(" ");
21941 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021942 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943 {
21944 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021945 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946 }
21947 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021948 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021950 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 {
21952 if (j)
21953 MSG_PUTS(", ");
21954 msg_puts(FUNCARG(fp, j));
21955 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021956 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021957 {
21958 if (j)
21959 MSG_PUTS(", ");
21960 MSG_PUTS("...");
21961 }
21962 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020021963 if (fp->uf_flags & FC_ABORT)
21964 MSG_PUTS(" abort");
21965 if (fp->uf_flags & FC_RANGE)
21966 MSG_PUTS(" range");
21967 if (fp->uf_flags & FC_DICT)
21968 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021969 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021970 if (p_verbose > 0)
21971 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972}
21973
21974/*
21975 * Find a function by name, return pointer to it in ufuncs.
21976 * Return NULL for unknown function.
21977 */
21978 static ufunc_T *
21979find_func(name)
21980 char_u *name;
21981{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021982 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021984 hi = hash_find(&func_hashtab, name);
21985 if (!HASHITEM_EMPTY(hi))
21986 return HI2UF(hi);
21987 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988}
21989
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021990#if defined(EXITFREE) || defined(PROTO)
21991 void
21992free_all_functions()
21993{
21994 hashitem_T *hi;
21995
21996 /* Need to start all over every time, because func_free() may change the
21997 * hash table. */
21998 while (func_hashtab.ht_used > 0)
21999 for (hi = func_hashtab.ht_array; ; ++hi)
22000 if (!HASHITEM_EMPTY(hi))
22001 {
22002 func_free(HI2UF(hi));
22003 break;
22004 }
22005}
22006#endif
22007
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022008 int
22009translated_function_exists(name)
22010 char_u *name;
22011{
22012 if (builtin_function(name))
22013 return find_internal_func(name) >= 0;
22014 return find_func(name) != NULL;
22015}
22016
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022017/*
22018 * Return TRUE if a function "name" exists.
22019 */
22020 static int
22021function_exists(name)
22022 char_u *name;
22023{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022024 char_u *nm = name;
22025 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022026 int n = FALSE;
22027
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022028 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022029 nm = skipwhite(nm);
22030
22031 /* Only accept "funcname", "funcname ", "funcname (..." and
22032 * "funcname(...", not "funcname!...". */
22033 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022034 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022035 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022036 return n;
22037}
22038
Bram Moolenaara1544c02013-05-30 12:35:52 +020022039 char_u *
22040get_expanded_name(name, check)
22041 char_u *name;
22042 int check;
22043{
22044 char_u *nm = name;
22045 char_u *p;
22046
22047 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22048
22049 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022050 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022051 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022052
Bram Moolenaara1544c02013-05-30 12:35:52 +020022053 vim_free(p);
22054 return NULL;
22055}
22056
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022057/*
22058 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022059 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022060 */
22061 static int
22062builtin_function(name)
22063 char_u *name;
22064{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022065 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22066 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022067}
22068
Bram Moolenaar05159a02005-02-26 23:04:13 +000022069#if defined(FEAT_PROFILE) || defined(PROTO)
22070/*
22071 * Start profiling function "fp".
22072 */
22073 static void
22074func_do_profile(fp)
22075 ufunc_T *fp;
22076{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022077 int len = fp->uf_lines.ga_len;
22078
22079 if (len == 0)
22080 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022081 fp->uf_tm_count = 0;
22082 profile_zero(&fp->uf_tm_self);
22083 profile_zero(&fp->uf_tm_total);
22084 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022085 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022086 if (fp->uf_tml_total == NULL)
22087 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022088 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022089 if (fp->uf_tml_self == NULL)
22090 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022091 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022092 fp->uf_tml_idx = -1;
22093 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22094 || fp->uf_tml_self == NULL)
22095 return; /* out of memory */
22096
22097 fp->uf_profiling = TRUE;
22098}
22099
22100/*
22101 * Dump the profiling results for all functions in file "fd".
22102 */
22103 void
22104func_dump_profile(fd)
22105 FILE *fd;
22106{
22107 hashitem_T *hi;
22108 int todo;
22109 ufunc_T *fp;
22110 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022111 ufunc_T **sorttab;
22112 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022113
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022114 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022115 if (todo == 0)
22116 return; /* nothing to dump */
22117
Bram Moolenaar73830342005-02-28 22:48:19 +000022118 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22119
Bram Moolenaar05159a02005-02-26 23:04:13 +000022120 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22121 {
22122 if (!HASHITEM_EMPTY(hi))
22123 {
22124 --todo;
22125 fp = HI2UF(hi);
22126 if (fp->uf_profiling)
22127 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022128 if (sorttab != NULL)
22129 sorttab[st_len++] = fp;
22130
Bram Moolenaar05159a02005-02-26 23:04:13 +000022131 if (fp->uf_name[0] == K_SPECIAL)
22132 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22133 else
22134 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22135 if (fp->uf_tm_count == 1)
22136 fprintf(fd, "Called 1 time\n");
22137 else
22138 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22139 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22140 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22141 fprintf(fd, "\n");
22142 fprintf(fd, "count total (s) self (s)\n");
22143
22144 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22145 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022146 if (FUNCLINE(fp, i) == NULL)
22147 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022148 prof_func_line(fd, fp->uf_tml_count[i],
22149 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022150 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22151 }
22152 fprintf(fd, "\n");
22153 }
22154 }
22155 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022156
22157 if (sorttab != NULL && st_len > 0)
22158 {
22159 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22160 prof_total_cmp);
22161 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22162 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22163 prof_self_cmp);
22164 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22165 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022166
22167 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022168}
Bram Moolenaar73830342005-02-28 22:48:19 +000022169
22170 static void
22171prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22172 FILE *fd;
22173 ufunc_T **sorttab;
22174 int st_len;
22175 char *title;
22176 int prefer_self; /* when equal print only self time */
22177{
22178 int i;
22179 ufunc_T *fp;
22180
22181 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22182 fprintf(fd, "count total (s) self (s) function\n");
22183 for (i = 0; i < 20 && i < st_len; ++i)
22184 {
22185 fp = sorttab[i];
22186 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22187 prefer_self);
22188 if (fp->uf_name[0] == K_SPECIAL)
22189 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22190 else
22191 fprintf(fd, " %s()\n", fp->uf_name);
22192 }
22193 fprintf(fd, "\n");
22194}
22195
22196/*
22197 * Print the count and times for one function or function line.
22198 */
22199 static void
22200prof_func_line(fd, count, total, self, prefer_self)
22201 FILE *fd;
22202 int count;
22203 proftime_T *total;
22204 proftime_T *self;
22205 int prefer_self; /* when equal print only self time */
22206{
22207 if (count > 0)
22208 {
22209 fprintf(fd, "%5d ", count);
22210 if (prefer_self && profile_equal(total, self))
22211 fprintf(fd, " ");
22212 else
22213 fprintf(fd, "%s ", profile_msg(total));
22214 if (!prefer_self && profile_equal(total, self))
22215 fprintf(fd, " ");
22216 else
22217 fprintf(fd, "%s ", profile_msg(self));
22218 }
22219 else
22220 fprintf(fd, " ");
22221}
22222
22223/*
22224 * Compare function for total time sorting.
22225 */
22226 static int
22227#ifdef __BORLANDC__
22228_RTLENTRYF
22229#endif
22230prof_total_cmp(s1, s2)
22231 const void *s1;
22232 const void *s2;
22233{
22234 ufunc_T *p1, *p2;
22235
22236 p1 = *(ufunc_T **)s1;
22237 p2 = *(ufunc_T **)s2;
22238 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22239}
22240
22241/*
22242 * Compare function for self time sorting.
22243 */
22244 static int
22245#ifdef __BORLANDC__
22246_RTLENTRYF
22247#endif
22248prof_self_cmp(s1, s2)
22249 const void *s1;
22250 const void *s2;
22251{
22252 ufunc_T *p1, *p2;
22253
22254 p1 = *(ufunc_T **)s1;
22255 p2 = *(ufunc_T **)s2;
22256 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22257}
22258
Bram Moolenaar05159a02005-02-26 23:04:13 +000022259#endif
22260
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022261/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022262 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022263 * Return TRUE if a package was loaded.
22264 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022265 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022266script_autoload(name, reload)
22267 char_u *name;
22268 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022269{
22270 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022271 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022272 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022273 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022274
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022275 /* Return quickly when autoload disabled. */
22276 if (no_autoload)
22277 return FALSE;
22278
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022279 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022280 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022281 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022282 return FALSE;
22283
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022284 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022285
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022286 /* Find the name in the list of previously loaded package names. Skip
22287 * "autoload/", it's always the same. */
22288 for (i = 0; i < ga_loaded.ga_len; ++i)
22289 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22290 break;
22291 if (!reload && i < ga_loaded.ga_len)
22292 ret = FALSE; /* was loaded already */
22293 else
22294 {
22295 /* Remember the name if it wasn't loaded already. */
22296 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22297 {
22298 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22299 tofree = NULL;
22300 }
22301
22302 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022303 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022304 ret = TRUE;
22305 }
22306
22307 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022308 return ret;
22309}
22310
22311/*
22312 * Return the autoload script name for a function or variable name.
22313 * Returns NULL when out of memory.
22314 */
22315 static char_u *
22316autoload_name(name)
22317 char_u *name;
22318{
22319 char_u *p;
22320 char_u *scriptname;
22321
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022322 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022323 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22324 if (scriptname == NULL)
22325 return FALSE;
22326 STRCPY(scriptname, "autoload/");
22327 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022328 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022329 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022330 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022331 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022332 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022333}
22334
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22336
22337/*
22338 * Function given to ExpandGeneric() to obtain the list of user defined
22339 * function names.
22340 */
22341 char_u *
22342get_user_func_name(xp, idx)
22343 expand_T *xp;
22344 int idx;
22345{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022346 static long_u done;
22347 static hashitem_T *hi;
22348 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349
22350 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022352 done = 0;
22353 hi = func_hashtab.ht_array;
22354 }
22355 if (done < func_hashtab.ht_used)
22356 {
22357 if (done++ > 0)
22358 ++hi;
22359 while (HASHITEM_EMPTY(hi))
22360 ++hi;
22361 fp = HI2UF(hi);
22362
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022363 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022364 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022365
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022366 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22367 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368
22369 cat_func_name(IObuff, fp);
22370 if (xp->xp_context != EXPAND_USER_FUNC)
22371 {
22372 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022373 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 STRCAT(IObuff, ")");
22375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 return IObuff;
22377 }
22378 return NULL;
22379}
22380
22381#endif /* FEAT_CMDL_COMPL */
22382
22383/*
22384 * Copy the function name of "fp" to buffer "buf".
22385 * "buf" must be able to hold the function name plus three bytes.
22386 * Takes care of script-local function names.
22387 */
22388 static void
22389cat_func_name(buf, fp)
22390 char_u *buf;
22391 ufunc_T *fp;
22392{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022393 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394 {
22395 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022396 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 }
22398 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022399 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400}
22401
22402/*
22403 * ":delfunction {name}"
22404 */
22405 void
22406ex_delfunction(eap)
22407 exarg_T *eap;
22408{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022409 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 char_u *p;
22411 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022412 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413
22414 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022415 name = trans_function_name(&p, eap->skip, 0, &fudi);
22416 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022418 {
22419 if (fudi.fd_dict != NULL && !eap->skip)
22420 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 if (!ends_excmd(*skipwhite(p)))
22424 {
22425 vim_free(name);
22426 EMSG(_(e_trailing));
22427 return;
22428 }
22429 eap->nextcmd = check_nextcmd(p);
22430 if (eap->nextcmd != NULL)
22431 *p = NUL;
22432
22433 if (!eap->skip)
22434 fp = find_func(name);
22435 vim_free(name);
22436
22437 if (!eap->skip)
22438 {
22439 if (fp == NULL)
22440 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022441 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 return;
22443 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022444 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 {
22446 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22447 return;
22448 }
22449
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022450 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022452 /* Delete the dict item that refers to the function, it will
22453 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022454 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022456 else
22457 func_free(fp);
22458 }
22459}
22460
22461/*
22462 * Free a function and remove it from the list of functions.
22463 */
22464 static void
22465func_free(fp)
22466 ufunc_T *fp;
22467{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022468 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022469
22470 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022471 ga_clear_strings(&(fp->uf_args));
22472 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022473#ifdef FEAT_PROFILE
22474 vim_free(fp->uf_tml_count);
22475 vim_free(fp->uf_tml_total);
22476 vim_free(fp->uf_tml_self);
22477#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022478
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022479 /* remove the function from the function hashtable */
22480 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22481 if (HASHITEM_EMPTY(hi))
22482 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022483 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022484 hash_remove(&func_hashtab, hi);
22485
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022486 vim_free(fp);
22487}
22488
22489/*
22490 * Unreference a Function: decrement the reference count and free it when it
22491 * becomes zero. Only for numbered functions.
22492 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022493 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022494func_unref(name)
22495 char_u *name;
22496{
22497 ufunc_T *fp;
22498
22499 if (name != NULL && isdigit(*name))
22500 {
22501 fp = find_func(name);
22502 if (fp == NULL)
22503 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022504 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022505 {
22506 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022507 * when "uf_calls" becomes zero. */
22508 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022509 func_free(fp);
22510 }
22511 }
22512}
22513
22514/*
22515 * Count a reference to a Function.
22516 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022517 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022518func_ref(name)
22519 char_u *name;
22520{
22521 ufunc_T *fp;
22522
22523 if (name != NULL && isdigit(*name))
22524 {
22525 fp = find_func(name);
22526 if (fp == NULL)
22527 EMSG2(_(e_intern2), "func_ref()");
22528 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022529 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530 }
22531}
22532
22533/*
22534 * Call a user function.
22535 */
22536 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022537call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538 ufunc_T *fp; /* pointer to function */
22539 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022540 typval_T *argvars; /* arguments */
22541 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 linenr_T firstline; /* first line of range */
22543 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022544 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545{
Bram Moolenaar33570922005-01-25 22:26:29 +000022546 char_u *save_sourcing_name;
22547 linenr_T save_sourcing_lnum;
22548 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022549 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022550 int save_did_emsg;
22551 static int depth = 0;
22552 dictitem_T *v;
22553 int fixvar_idx = 0; /* index in fixvar[] */
22554 int i;
22555 int ai;
22556 char_u numbuf[NUMBUFLEN];
22557 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022558#ifdef FEAT_PROFILE
22559 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022560 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562
22563 /* If depth of calling is getting too high, don't execute the function */
22564 if (depth >= p_mfd)
22565 {
22566 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022567 rettv->v_type = VAR_NUMBER;
22568 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569 return;
22570 }
22571 ++depth;
22572
22573 line_breakcheck(); /* check for CTRL-C hit */
22574
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022575 fc = (funccall_T *)alloc(sizeof(funccall_T));
22576 fc->caller = current_funccal;
22577 current_funccal = fc;
22578 fc->func = fp;
22579 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022580 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022581 fc->linenr = 0;
22582 fc->returned = FALSE;
22583 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022585 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22586 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022587
Bram Moolenaar33570922005-01-25 22:26:29 +000022588 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022589 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022590 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22591 * each argument variable and saves a lot of time.
22592 */
22593 /*
22594 * Init l: variables.
22595 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022596 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022597 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022598 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022599 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22600 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022601 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022602 name = v->di_key;
22603 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022604 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022605 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022606 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022607 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022608 v->di_tv.vval.v_dict = selfdict;
22609 ++selfdict->dv_refcount;
22610 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022611
Bram Moolenaar33570922005-01-25 22:26:29 +000022612 /*
22613 * Init a: variables.
22614 * Set a:0 to "argcount".
22615 * Set a:000 to a list with room for the "..." arguments.
22616 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022617 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022618 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022619 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022620 /* Use "name" to avoid a warning from some compiler that checks the
22621 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022622 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022623 name = v->di_key;
22624 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022625 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022626 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022627 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022628 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022629 v->di_tv.vval.v_list = &fc->l_varlist;
22630 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22631 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22632 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022633
22634 /*
22635 * Set a:firstline to "firstline" and a:lastline to "lastline".
22636 * Set a:name to named arguments.
22637 * Set a:N to the "..." arguments.
22638 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022639 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022640 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022641 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022642 (varnumber_T)lastline);
22643 for (i = 0; i < argcount; ++i)
22644 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022645 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022646 if (ai < 0)
22647 /* named argument a:name */
22648 name = FUNCARG(fp, i);
22649 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022650 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022651 /* "..." argument a:1, a:2, etc. */
22652 sprintf((char *)numbuf, "%d", ai + 1);
22653 name = numbuf;
22654 }
22655 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22656 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022657 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022658 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22659 }
22660 else
22661 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022662 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22663 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022664 if (v == NULL)
22665 break;
22666 v->di_flags = DI_FLAGS_RO;
22667 }
22668 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022669 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022670
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022671 /* Note: the values are copied directly to avoid alloc/free.
22672 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022673 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022674 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022675
22676 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22677 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022678 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22679 fc->l_listitems[ai].li_tv = argvars[i];
22680 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022681 }
22682 }
22683
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 /* Don't redraw while executing the function. */
22685 ++RedrawingDisabled;
22686 save_sourcing_name = sourcing_name;
22687 save_sourcing_lnum = sourcing_lnum;
22688 sourcing_lnum = 1;
22689 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022690 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 if (sourcing_name != NULL)
22692 {
22693 if (save_sourcing_name != NULL
22694 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22695 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22696 else
22697 STRCPY(sourcing_name, "function ");
22698 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22699
22700 if (p_verbose >= 12)
22701 {
22702 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022703 verbose_enter_scroll();
22704
Bram Moolenaar555b2802005-05-19 21:08:39 +000022705 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 if (p_verbose >= 14)
22707 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022709 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022710 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022711 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712
22713 msg_puts((char_u *)"(");
22714 for (i = 0; i < argcount; ++i)
22715 {
22716 if (i > 0)
22717 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022718 if (argvars[i].v_type == VAR_NUMBER)
22719 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720 else
22721 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022722 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22723 if (s != NULL)
22724 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022725 if (vim_strsize(s) > MSG_BUF_CLEN)
22726 {
22727 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22728 s = buf;
22729 }
22730 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022731 vim_free(tofree);
22732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 }
22734 }
22735 msg_puts((char_u *)")");
22736 }
22737 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022738
22739 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 --no_wait_return;
22741 }
22742 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022743#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022744 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022745 {
22746 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22747 func_do_profile(fp);
22748 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022749 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022750 {
22751 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022752 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022753 profile_zero(&fp->uf_tm_children);
22754 }
22755 script_prof_save(&wait_start);
22756 }
22757#endif
22758
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022760 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761 save_did_emsg = did_emsg;
22762 did_emsg = FALSE;
22763
22764 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022765 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22767
22768 --RedrawingDisabled;
22769
22770 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022771 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022773 clear_tv(rettv);
22774 rettv->v_type = VAR_NUMBER;
22775 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776 }
22777
Bram Moolenaar05159a02005-02-26 23:04:13 +000022778#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022779 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022780 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022781 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022782 profile_end(&call_start);
22783 profile_sub_wait(&wait_start, &call_start);
22784 profile_add(&fp->uf_tm_total, &call_start);
22785 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022786 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022787 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022788 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22789 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022790 }
22791 }
22792#endif
22793
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 /* when being verbose, mention the return value */
22795 if (p_verbose >= 12)
22796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022798 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022801 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022802 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022803 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022804 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022805 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022806 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022807 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022808 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022809 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022810 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022811
Bram Moolenaar555b2802005-05-19 21:08:39 +000022812 /* The value may be very long. Skip the middle part, so that we
22813 * have some idea how it starts and ends. smsg() would always
22814 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022815 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022816 if (s != NULL)
22817 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022818 if (vim_strsize(s) > MSG_BUF_CLEN)
22819 {
22820 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22821 s = buf;
22822 }
22823 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022824 vim_free(tofree);
22825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 }
22827 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022828
22829 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830 --no_wait_return;
22831 }
22832
22833 vim_free(sourcing_name);
22834 sourcing_name = save_sourcing_name;
22835 sourcing_lnum = save_sourcing_lnum;
22836 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022837#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022838 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022839 script_prof_restore(&wait_start);
22840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841
22842 if (p_verbose >= 12 && sourcing_name != NULL)
22843 {
22844 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022845 verbose_enter_scroll();
22846
Bram Moolenaar555b2802005-05-19 21:08:39 +000022847 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022849
22850 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 --no_wait_return;
22852 }
22853
22854 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022855 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022857
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022858 /* If the a:000 list and the l: and a: dicts are not referenced we can
22859 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022860 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22861 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22862 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22863 {
22864 free_funccal(fc, FALSE);
22865 }
22866 else
22867 {
22868 hashitem_T *hi;
22869 listitem_T *li;
22870 int todo;
22871
22872 /* "fc" is still in use. This can happen when returning "a:000" or
22873 * assigning "l:" to a global variable.
22874 * Link "fc" in the list for garbage collection later. */
22875 fc->caller = previous_funccal;
22876 previous_funccal = fc;
22877
22878 /* Make a copy of the a: variables, since we didn't do that above. */
22879 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22880 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22881 {
22882 if (!HASHITEM_EMPTY(hi))
22883 {
22884 --todo;
22885 v = HI2DI(hi);
22886 copy_tv(&v->di_tv, &v->di_tv);
22887 }
22888 }
22889
22890 /* Make a copy of the a:000 items, since we didn't do that above. */
22891 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22892 copy_tv(&li->li_tv, &li->li_tv);
22893 }
22894}
22895
22896/*
22897 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022898 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022899 */
22900 static int
22901can_free_funccal(fc, copyID)
22902 funccall_T *fc;
22903 int copyID;
22904{
22905 return (fc->l_varlist.lv_copyID != copyID
22906 && fc->l_vars.dv_copyID != copyID
22907 && fc->l_avars.dv_copyID != copyID);
22908}
22909
22910/*
22911 * Free "fc" and what it contains.
22912 */
22913 static void
22914free_funccal(fc, free_val)
22915 funccall_T *fc;
22916 int free_val; /* a: vars were allocated */
22917{
22918 listitem_T *li;
22919
22920 /* The a: variables typevals may not have been allocated, only free the
22921 * allocated variables. */
22922 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22923
22924 /* free all l: variables */
22925 vars_clear(&fc->l_vars.dv_hashtab);
22926
22927 /* Free the a:000 variables if they were allocated. */
22928 if (free_val)
22929 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22930 clear_tv(&li->li_tv);
22931
22932 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933}
22934
22935/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022936 * Add a number variable "name" to dict "dp" with value "nr".
22937 */
22938 static void
22939add_nr_var(dp, v, name, nr)
22940 dict_T *dp;
22941 dictitem_T *v;
22942 char *name;
22943 varnumber_T nr;
22944{
22945 STRCPY(v->di_key, name);
22946 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22947 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22948 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022949 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022950 v->di_tv.vval.v_number = nr;
22951}
22952
22953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 * ":return [expr]"
22955 */
22956 void
22957ex_return(eap)
22958 exarg_T *eap;
22959{
22960 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022961 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 int returning = FALSE;
22963
22964 if (current_funccal == NULL)
22965 {
22966 EMSG(_("E133: :return not inside a function"));
22967 return;
22968 }
22969
22970 if (eap->skip)
22971 ++emsg_skip;
22972
22973 eap->nextcmd = NULL;
22974 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022975 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 {
22977 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022978 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022980 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 }
22982 /* It's safer to return also on error. */
22983 else if (!eap->skip)
22984 {
22985 /*
22986 * Return unless the expression evaluation has been cancelled due to an
22987 * aborting error, an interrupt, or an exception.
22988 */
22989 if (!aborting())
22990 returning = do_return(eap, FALSE, TRUE, NULL);
22991 }
22992
22993 /* When skipping or the return gets pending, advance to the next command
22994 * in this line (!returning). Otherwise, ignore the rest of the line.
22995 * Following lines will be ignored by get_func_line(). */
22996 if (returning)
22997 eap->nextcmd = NULL;
22998 else if (eap->nextcmd == NULL) /* no argument */
22999 eap->nextcmd = check_nextcmd(arg);
23000
23001 if (eap->skip)
23002 --emsg_skip;
23003}
23004
23005/*
23006 * Return from a function. Possibly makes the return pending. Also called
23007 * for a pending return at the ":endtry" or after returning from an extra
23008 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023009 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023010 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023011 * FALSE when the return gets pending.
23012 */
23013 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023014do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015 exarg_T *eap;
23016 int reanimate;
23017 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023018 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019{
23020 int idx;
23021 struct condstack *cstack = eap->cstack;
23022
23023 if (reanimate)
23024 /* Undo the return. */
23025 current_funccal->returned = FALSE;
23026
23027 /*
23028 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23029 * not in its finally clause (which then is to be executed next) is found.
23030 * In this case, make the ":return" pending for execution at the ":endtry".
23031 * Otherwise, return normally.
23032 */
23033 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23034 if (idx >= 0)
23035 {
23036 cstack->cs_pending[idx] = CSTP_RETURN;
23037
23038 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023039 /* A pending return again gets pending. "rettv" points to an
23040 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023042 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 else
23044 {
23045 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023046 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023048 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023050 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023051 {
23052 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023053 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023054 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 else
23056 EMSG(_(e_outofmem));
23057 }
23058 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023059 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060
23061 if (reanimate)
23062 {
23063 /* The pending return value could be overwritten by a ":return"
23064 * without argument in a finally clause; reset the default
23065 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023066 current_funccal->rettv->v_type = VAR_NUMBER;
23067 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 }
23069 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023070 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 }
23072 else
23073 {
23074 current_funccal->returned = TRUE;
23075
23076 /* If the return is carried out now, store the return value. For
23077 * a return immediately after reanimation, the value is already
23078 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023079 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023081 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023082 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023083 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023084 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085 }
23086 }
23087
23088 return idx < 0;
23089}
23090
23091/*
23092 * Free the variable with a pending return value.
23093 */
23094 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023095discard_pending_return(rettv)
23096 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097{
Bram Moolenaar33570922005-01-25 22:26:29 +000023098 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099}
23100
23101/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023102 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 * is an allocated string. Used by report_pending() for verbose messages.
23104 */
23105 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023106get_return_cmd(rettv)
23107 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023109 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023110 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023111 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023113 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023114 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023115 if (s == NULL)
23116 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023117
23118 STRCPY(IObuff, ":return ");
23119 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23120 if (STRLEN(s) + 8 >= IOSIZE)
23121 STRCPY(IObuff + IOSIZE - 4, "...");
23122 vim_free(tofree);
23123 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124}
23125
23126/*
23127 * Get next function line.
23128 * Called by do_cmdline() to get the next line.
23129 * Returns allocated string, or NULL for end of function.
23130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131 char_u *
23132get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023133 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023135 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023136{
Bram Moolenaar33570922005-01-25 22:26:29 +000023137 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023138 ufunc_T *fp = fcp->func;
23139 char_u *retval;
23140 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141
23142 /* If breakpoints have been added/deleted need to check for it. */
23143 if (fcp->dbg_tick != debug_tick)
23144 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023145 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146 sourcing_lnum);
23147 fcp->dbg_tick = debug_tick;
23148 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023149#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023150 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023151 func_line_end(cookie);
23152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153
Bram Moolenaar05159a02005-02-26 23:04:13 +000023154 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023155 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23156 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157 retval = NULL;
23158 else
23159 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023160 /* Skip NULL lines (continuation lines). */
23161 while (fcp->linenr < gap->ga_len
23162 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23163 ++fcp->linenr;
23164 if (fcp->linenr >= gap->ga_len)
23165 retval = NULL;
23166 else
23167 {
23168 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23169 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023170#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023171 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023172 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023173#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023175 }
23176
23177 /* Did we encounter a breakpoint? */
23178 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23179 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023180 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023182 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183 sourcing_lnum);
23184 fcp->dbg_tick = debug_tick;
23185 }
23186
23187 return retval;
23188}
23189
Bram Moolenaar05159a02005-02-26 23:04:13 +000023190#if defined(FEAT_PROFILE) || defined(PROTO)
23191/*
23192 * Called when starting to read a function line.
23193 * "sourcing_lnum" must be correct!
23194 * When skipping lines it may not actually be executed, but we won't find out
23195 * until later and we need to store the time now.
23196 */
23197 void
23198func_line_start(cookie)
23199 void *cookie;
23200{
23201 funccall_T *fcp = (funccall_T *)cookie;
23202 ufunc_T *fp = fcp->func;
23203
23204 if (fp->uf_profiling && sourcing_lnum >= 1
23205 && sourcing_lnum <= fp->uf_lines.ga_len)
23206 {
23207 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023208 /* Skip continuation lines. */
23209 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23210 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023211 fp->uf_tml_execed = FALSE;
23212 profile_start(&fp->uf_tml_start);
23213 profile_zero(&fp->uf_tml_children);
23214 profile_get_wait(&fp->uf_tml_wait);
23215 }
23216}
23217
23218/*
23219 * Called when actually executing a function line.
23220 */
23221 void
23222func_line_exec(cookie)
23223 void *cookie;
23224{
23225 funccall_T *fcp = (funccall_T *)cookie;
23226 ufunc_T *fp = fcp->func;
23227
23228 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23229 fp->uf_tml_execed = TRUE;
23230}
23231
23232/*
23233 * Called when done with a function line.
23234 */
23235 void
23236func_line_end(cookie)
23237 void *cookie;
23238{
23239 funccall_T *fcp = (funccall_T *)cookie;
23240 ufunc_T *fp = fcp->func;
23241
23242 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23243 {
23244 if (fp->uf_tml_execed)
23245 {
23246 ++fp->uf_tml_count[fp->uf_tml_idx];
23247 profile_end(&fp->uf_tml_start);
23248 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023249 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023250 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23251 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023252 }
23253 fp->uf_tml_idx = -1;
23254 }
23255}
23256#endif
23257
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258/*
23259 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023260 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261 */
23262 int
23263func_has_ended(cookie)
23264 void *cookie;
23265{
Bram Moolenaar33570922005-01-25 22:26:29 +000023266 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023267
23268 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23269 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023270 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 || fcp->returned);
23272}
23273
23274/*
23275 * return TRUE if cookie indicates a function which "abort"s on errors.
23276 */
23277 int
23278func_has_abort(cookie)
23279 void *cookie;
23280{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023281 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282}
23283
23284#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23285typedef enum
23286{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023287 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23288 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23289 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290} var_flavour_T;
23291
23292static var_flavour_T var_flavour __ARGS((char_u *varname));
23293
23294 static var_flavour_T
23295var_flavour(varname)
23296 char_u *varname;
23297{
23298 char_u *p = varname;
23299
23300 if (ASCII_ISUPPER(*p))
23301 {
23302 while (*(++p))
23303 if (ASCII_ISLOWER(*p))
23304 return VAR_FLAVOUR_SESSION;
23305 return VAR_FLAVOUR_VIMINFO;
23306 }
23307 else
23308 return VAR_FLAVOUR_DEFAULT;
23309}
23310#endif
23311
23312#if defined(FEAT_VIMINFO) || defined(PROTO)
23313/*
23314 * Restore global vars that start with a capital from the viminfo file
23315 */
23316 int
23317read_viminfo_varlist(virp, writing)
23318 vir_T *virp;
23319 int writing;
23320{
23321 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023322 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023323 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324
23325 if (!writing && (find_viminfo_parameter('!') != NULL))
23326 {
23327 tab = vim_strchr(virp->vir_line + 1, '\t');
23328 if (tab != NULL)
23329 {
23330 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023331 switch (*tab)
23332 {
23333 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023334#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023335 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023336#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023337 case 'D': type = VAR_DICT; break;
23338 case 'L': type = VAR_LIST; break;
23339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340
23341 tab = vim_strchr(tab, '\t');
23342 if (tab != NULL)
23343 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023344 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023345 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023346 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023347 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023348#ifdef FEAT_FLOAT
23349 else if (type == VAR_FLOAT)
23350 (void)string2float(tab + 1, &tv.vval.v_float);
23351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023353 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023354 if (type == VAR_DICT || type == VAR_LIST)
23355 {
23356 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23357
23358 if (etv == NULL)
23359 /* Failed to parse back the dict or list, use it as a
23360 * string. */
23361 tv.v_type = VAR_STRING;
23362 else
23363 {
23364 vim_free(tv.vval.v_string);
23365 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023366 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023367 }
23368 }
23369
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023370 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023371
23372 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023373 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023374 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23375 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 }
23377 }
23378 }
23379
23380 return viminfo_readline(virp);
23381}
23382
23383/*
23384 * Write global vars that start with a capital to the viminfo file
23385 */
23386 void
23387write_viminfo_varlist(fp)
23388 FILE *fp;
23389{
Bram Moolenaar33570922005-01-25 22:26:29 +000023390 hashitem_T *hi;
23391 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023392 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023393 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023394 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023395 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023396 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023397
23398 if (find_viminfo_parameter('!') == NULL)
23399 return;
23400
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023401 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023402
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023403 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023404 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023405 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023406 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023408 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023409 this_var = HI2DI(hi);
23410 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023411 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023412 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023413 {
23414 case VAR_STRING: s = "STR"; break;
23415 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023416#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023417 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023418#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023419 case VAR_DICT: s = "DIC"; break;
23420 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023421 default: continue;
23422 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023423 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023424 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023425 if (p != NULL)
23426 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023427 vim_free(tofree);
23428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023429 }
23430 }
23431}
23432#endif
23433
23434#if defined(FEAT_SESSION) || defined(PROTO)
23435 int
23436store_session_globals(fd)
23437 FILE *fd;
23438{
Bram Moolenaar33570922005-01-25 22:26:29 +000023439 hashitem_T *hi;
23440 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023441 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023442 char_u *p, *t;
23443
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023444 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023445 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023447 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023448 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023449 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023450 this_var = HI2DI(hi);
23451 if ((this_var->di_tv.v_type == VAR_NUMBER
23452 || this_var->di_tv.v_type == VAR_STRING)
23453 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023454 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023455 /* Escape special characters with a backslash. Turn a LF and
23456 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023457 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023458 (char_u *)"\\\"\n\r");
23459 if (p == NULL) /* out of memory */
23460 break;
23461 for (t = p; *t != NUL; ++t)
23462 if (*t == '\n')
23463 *t = 'n';
23464 else if (*t == '\r')
23465 *t = 'r';
23466 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023467 this_var->di_key,
23468 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23469 : ' ',
23470 p,
23471 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23472 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023473 || put_eol(fd) == FAIL)
23474 {
23475 vim_free(p);
23476 return FAIL;
23477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478 vim_free(p);
23479 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023480#ifdef FEAT_FLOAT
23481 else if (this_var->di_tv.v_type == VAR_FLOAT
23482 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23483 {
23484 float_T f = this_var->di_tv.vval.v_float;
23485 int sign = ' ';
23486
23487 if (f < 0)
23488 {
23489 f = -f;
23490 sign = '-';
23491 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023492 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023493 this_var->di_key, sign, f) < 0)
23494 || put_eol(fd) == FAIL)
23495 return FAIL;
23496 }
23497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498 }
23499 }
23500 return OK;
23501}
23502#endif
23503
Bram Moolenaar661b1822005-07-28 22:36:45 +000023504/*
23505 * Display script name where an item was last set.
23506 * Should only be invoked when 'verbose' is non-zero.
23507 */
23508 void
23509last_set_msg(scriptID)
23510 scid_T scriptID;
23511{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023512 char_u *p;
23513
Bram Moolenaar661b1822005-07-28 22:36:45 +000023514 if (scriptID != 0)
23515 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023516 p = home_replace_save(NULL, get_scriptname(scriptID));
23517 if (p != NULL)
23518 {
23519 verbose_enter();
23520 MSG_PUTS(_("\n\tLast set from "));
23521 MSG_PUTS(p);
23522 vim_free(p);
23523 verbose_leave();
23524 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023525 }
23526}
23527
Bram Moolenaard812df62008-11-09 12:46:09 +000023528/*
23529 * List v:oldfiles in a nice way.
23530 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023531 void
23532ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023533 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023534{
23535 list_T *l = vimvars[VV_OLDFILES].vv_list;
23536 listitem_T *li;
23537 int nr = 0;
23538
23539 if (l == NULL)
23540 msg((char_u *)_("No old files"));
23541 else
23542 {
23543 msg_start();
23544 msg_scroll = TRUE;
23545 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23546 {
23547 msg_outnum((long)++nr);
23548 MSG_PUTS(": ");
23549 msg_outtrans(get_tv_string(&li->li_tv));
23550 msg_putchar('\n');
23551 out_flush(); /* output one line at a time */
23552 ui_breakcheck();
23553 }
23554 /* Assume "got_int" was set to truncate the listing. */
23555 got_int = FALSE;
23556
23557#ifdef FEAT_BROWSE_CMD
23558 if (cmdmod.browse)
23559 {
23560 quit_more = FALSE;
23561 nr = prompt_for_number(FALSE);
23562 msg_starthere();
23563 if (nr > 0)
23564 {
23565 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23566 (long)nr);
23567
23568 if (p != NULL)
23569 {
23570 p = expand_env_save(p);
23571 eap->arg = p;
23572 eap->cmdidx = CMD_edit;
23573 cmdmod.browse = FALSE;
23574 do_exedit(eap, NULL);
23575 vim_free(p);
23576 }
23577 }
23578 }
23579#endif
23580 }
23581}
23582
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583#endif /* FEAT_EVAL */
23584
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023586#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587
23588#ifdef WIN3264
23589/*
23590 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23591 */
23592static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23593static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23594static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23595
23596/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023597 * Get the short path (8.3) for the filename in "fnamep".
23598 * Only works for a valid file name.
23599 * When the path gets longer "fnamep" is changed and the allocated buffer
23600 * is put in "bufp".
23601 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23602 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 */
23604 static int
23605get_short_pathname(fnamep, bufp, fnamelen)
23606 char_u **fnamep;
23607 char_u **bufp;
23608 int *fnamelen;
23609{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023610 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611 char_u *newbuf;
23612
23613 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614 l = GetShortPathName(*fnamep, *fnamep, len);
23615 if (l > len - 1)
23616 {
23617 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023618 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023619 newbuf = vim_strnsave(*fnamep, l);
23620 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023621 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622
23623 vim_free(*bufp);
23624 *fnamep = *bufp = newbuf;
23625
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023626 /* Really should always succeed, as the buffer is big enough. */
23627 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 }
23629
23630 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023631 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632}
23633
23634/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023635 * Get the short path (8.3) for the filename in "fname". The converted
23636 * path is returned in "bufp".
23637 *
23638 * Some of the directories specified in "fname" may not exist. This function
23639 * will shorten the existing directories at the beginning of the path and then
23640 * append the remaining non-existing path.
23641 *
23642 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023643 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023644 * bufp - Pointer to an allocated buffer for the filename.
23645 * fnamelen - Length of the filename pointed to by fname
23646 *
23647 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 */
23649 static int
23650shortpath_for_invalid_fname(fname, bufp, fnamelen)
23651 char_u **fname;
23652 char_u **bufp;
23653 int *fnamelen;
23654{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023655 char_u *short_fname, *save_fname, *pbuf_unused;
23656 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023658 int old_len, len;
23659 int new_len, sfx_len;
23660 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023661
23662 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023663 old_len = *fnamelen;
23664 save_fname = vim_strnsave(*fname, old_len);
23665 pbuf_unused = NULL;
23666 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023668 endp = save_fname + old_len - 1; /* Find the end of the copy */
23669 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023671 /*
23672 * Try shortening the supplied path till it succeeds by removing one
23673 * directory at a time from the tail of the path.
23674 */
23675 len = 0;
23676 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023677 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023678 /* go back one path-separator */
23679 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23680 --endp;
23681 if (endp <= save_fname)
23682 break; /* processed the complete path */
23683
23684 /*
23685 * Replace the path separator with a NUL and try to shorten the
23686 * resulting path.
23687 */
23688 ch = *endp;
23689 *endp = 0;
23690 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023691 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023692 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23693 {
23694 retval = FAIL;
23695 goto theend;
23696 }
23697 *endp = ch; /* preserve the string */
23698
23699 if (len > 0)
23700 break; /* successfully shortened the path */
23701
23702 /* failed to shorten the path. Skip the path separator */
23703 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704 }
23705
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023706 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023708 /*
23709 * Succeeded in shortening the path. Now concatenate the shortened
23710 * path with the remaining path at the tail.
23711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023712
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023713 /* Compute the length of the new path. */
23714 sfx_len = (int)(save_endp - endp) + 1;
23715 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023717 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023719 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023721 /* There is not enough space in the currently allocated string,
23722 * copy it to a buffer big enough. */
23723 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023724 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023725 {
23726 retval = FAIL;
23727 goto theend;
23728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023729 }
23730 else
23731 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023732 /* Transfer short_fname to the main buffer (it's big enough),
23733 * unless get_short_pathname() did its work in-place. */
23734 *fname = *bufp = save_fname;
23735 if (short_fname != save_fname)
23736 vim_strncpy(save_fname, short_fname, len);
23737 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023739
23740 /* concat the not-shortened part of the path */
23741 vim_strncpy(*fname + len, endp, sfx_len);
23742 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023744
23745theend:
23746 vim_free(pbuf_unused);
23747 vim_free(save_fname);
23748
23749 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023750}
23751
23752/*
23753 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023754 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 */
23756 static int
23757shortpath_for_partial(fnamep, bufp, fnamelen)
23758 char_u **fnamep;
23759 char_u **bufp;
23760 int *fnamelen;
23761{
23762 int sepcount, len, tflen;
23763 char_u *p;
23764 char_u *pbuf, *tfname;
23765 int hasTilde;
23766
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023767 /* Count up the path separators from the RHS.. so we know which part
23768 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023769 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023770 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023771 if (vim_ispathsep(*p))
23772 ++sepcount;
23773
23774 /* Need full path first (use expand_env() to remove a "~/") */
23775 hasTilde = (**fnamep == '~');
23776 if (hasTilde)
23777 pbuf = tfname = expand_env_save(*fnamep);
23778 else
23779 pbuf = tfname = FullName_save(*fnamep, FALSE);
23780
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023781 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023782
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023783 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23784 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785
23786 if (len == 0)
23787 {
23788 /* Don't have a valid filename, so shorten the rest of the
23789 * path if we can. This CAN give us invalid 8.3 filenames, but
23790 * there's not a lot of point in guessing what it might be.
23791 */
23792 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023793 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23794 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 }
23796
23797 /* Count the paths backward to find the beginning of the desired string. */
23798 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023799 {
23800#ifdef FEAT_MBYTE
23801 if (has_mbyte)
23802 p -= mb_head_off(tfname, p);
23803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023804 if (vim_ispathsep(*p))
23805 {
23806 if (sepcount == 0 || (hasTilde && sepcount == 1))
23807 break;
23808 else
23809 sepcount --;
23810 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812 if (hasTilde)
23813 {
23814 --p;
23815 if (p >= tfname)
23816 *p = '~';
23817 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023818 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023819 }
23820 else
23821 ++p;
23822
23823 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23824 vim_free(*bufp);
23825 *fnamelen = (int)STRLEN(p);
23826 *bufp = pbuf;
23827 *fnamep = p;
23828
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023829 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830}
23831#endif /* WIN3264 */
23832
23833/*
23834 * Adjust a filename, according to a string of modifiers.
23835 * *fnamep must be NUL terminated when called. When returning, the length is
23836 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023837 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838 * When there is an error, *fnamep is set to NULL.
23839 */
23840 int
23841modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23842 char_u *src; /* string with modifiers */
23843 int *usedlen; /* characters after src that are used */
23844 char_u **fnamep; /* file name so far */
23845 char_u **bufp; /* buffer for allocated file name or NULL */
23846 int *fnamelen; /* length of fnamep */
23847{
23848 int valid = 0;
23849 char_u *tail;
23850 char_u *s, *p, *pbuf;
23851 char_u dirname[MAXPATHL];
23852 int c;
23853 int has_fullname = 0;
23854#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023855 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023856 int has_shortname = 0;
23857#endif
23858
23859repeat:
23860 /* ":p" - full path/file_name */
23861 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23862 {
23863 has_fullname = 1;
23864
23865 valid |= VALID_PATH;
23866 *usedlen += 2;
23867
23868 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23869 if ((*fnamep)[0] == '~'
23870#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23871 && ((*fnamep)[1] == '/'
23872# ifdef BACKSLASH_IN_FILENAME
23873 || (*fnamep)[1] == '\\'
23874# endif
23875 || (*fnamep)[1] == NUL)
23876
23877#endif
23878 )
23879 {
23880 *fnamep = expand_env_save(*fnamep);
23881 vim_free(*bufp); /* free any allocated file name */
23882 *bufp = *fnamep;
23883 if (*fnamep == NULL)
23884 return -1;
23885 }
23886
23887 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023888 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 {
23890 if (vim_ispathsep(*p)
23891 && p[1] == '.'
23892 && (p[2] == NUL
23893 || vim_ispathsep(p[2])
23894 || (p[2] == '.'
23895 && (p[3] == NUL || vim_ispathsep(p[3])))))
23896 break;
23897 }
23898
23899 /* FullName_save() is slow, don't use it when not needed. */
23900 if (*p != NUL || !vim_isAbsName(*fnamep))
23901 {
23902 *fnamep = FullName_save(*fnamep, *p != NUL);
23903 vim_free(*bufp); /* free any allocated file name */
23904 *bufp = *fnamep;
23905 if (*fnamep == NULL)
23906 return -1;
23907 }
23908
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023909#ifdef WIN3264
23910# if _WIN32_WINNT >= 0x0500
23911 if (vim_strchr(*fnamep, '~') != NULL)
23912 {
23913 /* Expand 8.3 filename to full path. Needed to make sure the same
23914 * file does not have two different names.
23915 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23916 p = alloc(_MAX_PATH + 1);
23917 if (p != NULL)
23918 {
23919 if (GetLongPathName(*fnamep, p, MAXPATHL))
23920 {
23921 vim_free(*bufp);
23922 *bufp = *fnamep = p;
23923 }
23924 else
23925 vim_free(p);
23926 }
23927 }
23928# endif
23929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023930 /* Append a path separator to a directory. */
23931 if (mch_isdir(*fnamep))
23932 {
23933 /* Make room for one or two extra characters. */
23934 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23935 vim_free(*bufp); /* free any allocated file name */
23936 *bufp = *fnamep;
23937 if (*fnamep == NULL)
23938 return -1;
23939 add_pathsep(*fnamep);
23940 }
23941 }
23942
23943 /* ":." - path relative to the current directory */
23944 /* ":~" - path relative to the home directory */
23945 /* ":8" - shortname path - postponed till after */
23946 while (src[*usedlen] == ':'
23947 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23948 {
23949 *usedlen += 2;
23950 if (c == '8')
23951 {
23952#ifdef WIN3264
23953 has_shortname = 1; /* Postpone this. */
23954#endif
23955 continue;
23956 }
23957 pbuf = NULL;
23958 /* Need full path first (use expand_env() to remove a "~/") */
23959 if (!has_fullname)
23960 {
23961 if (c == '.' && **fnamep == '~')
23962 p = pbuf = expand_env_save(*fnamep);
23963 else
23964 p = pbuf = FullName_save(*fnamep, FALSE);
23965 }
23966 else
23967 p = *fnamep;
23968
23969 has_fullname = 0;
23970
23971 if (p != NULL)
23972 {
23973 if (c == '.')
23974 {
23975 mch_dirname(dirname, MAXPATHL);
23976 s = shorten_fname(p, dirname);
23977 if (s != NULL)
23978 {
23979 *fnamep = s;
23980 if (pbuf != NULL)
23981 {
23982 vim_free(*bufp); /* free any allocated file name */
23983 *bufp = pbuf;
23984 pbuf = NULL;
23985 }
23986 }
23987 }
23988 else
23989 {
23990 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23991 /* Only replace it when it starts with '~' */
23992 if (*dirname == '~')
23993 {
23994 s = vim_strsave(dirname);
23995 if (s != NULL)
23996 {
23997 *fnamep = s;
23998 vim_free(*bufp);
23999 *bufp = s;
24000 }
24001 }
24002 }
24003 vim_free(pbuf);
24004 }
24005 }
24006
24007 tail = gettail(*fnamep);
24008 *fnamelen = (int)STRLEN(*fnamep);
24009
24010 /* ":h" - head, remove "/file_name", can be repeated */
24011 /* Don't remove the first "/" or "c:\" */
24012 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24013 {
24014 valid |= VALID_HEAD;
24015 *usedlen += 2;
24016 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024017 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024018 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024019 *fnamelen = (int)(tail - *fnamep);
24020#ifdef VMS
24021 if (*fnamelen > 0)
24022 *fnamelen += 1; /* the path separator is part of the path */
24023#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024024 if (*fnamelen == 0)
24025 {
24026 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24027 p = vim_strsave((char_u *)".");
24028 if (p == NULL)
24029 return -1;
24030 vim_free(*bufp);
24031 *bufp = *fnamep = tail = p;
24032 *fnamelen = 1;
24033 }
24034 else
24035 {
24036 while (tail > s && !after_pathsep(s, tail))
24037 mb_ptr_back(*fnamep, tail);
24038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039 }
24040
24041 /* ":8" - shortname */
24042 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24043 {
24044 *usedlen += 2;
24045#ifdef WIN3264
24046 has_shortname = 1;
24047#endif
24048 }
24049
24050#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024051 /*
24052 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024053 */
24054 if (has_shortname)
24055 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024056 /* Copy the string if it is shortened by :h and when it wasn't copied
24057 * yet, because we are going to change it in place. Avoids changing
24058 * the buffer name for "%:8". */
24059 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024060 {
24061 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024062 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063 return -1;
24064 vim_free(*bufp);
24065 *bufp = *fnamep = p;
24066 }
24067
24068 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024069 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024070 if (!has_fullname && !vim_isAbsName(*fnamep))
24071 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024072 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073 return -1;
24074 }
24075 else
24076 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024077 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078
Bram Moolenaardc935552011-08-17 15:23:23 +020024079 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024080 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024081 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082 return -1;
24083
24084 if (l == 0)
24085 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024086 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024088 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024089 return -1;
24090 }
24091 *fnamelen = l;
24092 }
24093 }
24094#endif /* WIN3264 */
24095
24096 /* ":t" - tail, just the basename */
24097 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24098 {
24099 *usedlen += 2;
24100 *fnamelen -= (int)(tail - *fnamep);
24101 *fnamep = tail;
24102 }
24103
24104 /* ":e" - extension, can be repeated */
24105 /* ":r" - root, without extension, can be repeated */
24106 while (src[*usedlen] == ':'
24107 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24108 {
24109 /* find a '.' in the tail:
24110 * - for second :e: before the current fname
24111 * - otherwise: The last '.'
24112 */
24113 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24114 s = *fnamep - 2;
24115 else
24116 s = *fnamep + *fnamelen - 1;
24117 for ( ; s > tail; --s)
24118 if (s[0] == '.')
24119 break;
24120 if (src[*usedlen + 1] == 'e') /* :e */
24121 {
24122 if (s > tail)
24123 {
24124 *fnamelen += (int)(*fnamep - (s + 1));
24125 *fnamep = s + 1;
24126#ifdef VMS
24127 /* cut version from the extension */
24128 s = *fnamep + *fnamelen - 1;
24129 for ( ; s > *fnamep; --s)
24130 if (s[0] == ';')
24131 break;
24132 if (s > *fnamep)
24133 *fnamelen = s - *fnamep;
24134#endif
24135 }
24136 else if (*fnamep <= tail)
24137 *fnamelen = 0;
24138 }
24139 else /* :r */
24140 {
24141 if (s > tail) /* remove one extension */
24142 *fnamelen = (int)(s - *fnamep);
24143 }
24144 *usedlen += 2;
24145 }
24146
24147 /* ":s?pat?foo?" - substitute */
24148 /* ":gs?pat?foo?" - global substitute */
24149 if (src[*usedlen] == ':'
24150 && (src[*usedlen + 1] == 's'
24151 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24152 {
24153 char_u *str;
24154 char_u *pat;
24155 char_u *sub;
24156 int sep;
24157 char_u *flags;
24158 int didit = FALSE;
24159
24160 flags = (char_u *)"";
24161 s = src + *usedlen + 2;
24162 if (src[*usedlen + 1] == 'g')
24163 {
24164 flags = (char_u *)"g";
24165 ++s;
24166 }
24167
24168 sep = *s++;
24169 if (sep)
24170 {
24171 /* find end of pattern */
24172 p = vim_strchr(s, sep);
24173 if (p != NULL)
24174 {
24175 pat = vim_strnsave(s, (int)(p - s));
24176 if (pat != NULL)
24177 {
24178 s = p + 1;
24179 /* find end of substitution */
24180 p = vim_strchr(s, sep);
24181 if (p != NULL)
24182 {
24183 sub = vim_strnsave(s, (int)(p - s));
24184 str = vim_strnsave(*fnamep, *fnamelen);
24185 if (sub != NULL && str != NULL)
24186 {
24187 *usedlen = (int)(p + 1 - src);
24188 s = do_string_sub(str, pat, sub, flags);
24189 if (s != NULL)
24190 {
24191 *fnamep = s;
24192 *fnamelen = (int)STRLEN(s);
24193 vim_free(*bufp);
24194 *bufp = s;
24195 didit = TRUE;
24196 }
24197 }
24198 vim_free(sub);
24199 vim_free(str);
24200 }
24201 vim_free(pat);
24202 }
24203 }
24204 /* after using ":s", repeat all the modifiers */
24205 if (didit)
24206 goto repeat;
24207 }
24208 }
24209
24210 return valid;
24211}
24212
24213/*
24214 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24215 * "flags" can be "g" to do a global substitute.
24216 * Returns an allocated string, NULL for error.
24217 */
24218 char_u *
24219do_string_sub(str, pat, sub, flags)
24220 char_u *str;
24221 char_u *pat;
24222 char_u *sub;
24223 char_u *flags;
24224{
24225 int sublen;
24226 regmatch_T regmatch;
24227 int i;
24228 int do_all;
24229 char_u *tail;
24230 garray_T ga;
24231 char_u *ret;
24232 char_u *save_cpo;
24233
24234 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24235 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024236 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237
24238 ga_init2(&ga, 1, 200);
24239
24240 do_all = (flags[0] == 'g');
24241
24242 regmatch.rm_ic = p_ic;
24243 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24244 if (regmatch.regprog != NULL)
24245 {
24246 tail = str;
24247 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24248 {
24249 /*
24250 * Get some space for a temporary buffer to do the substitution
24251 * into. It will contain:
24252 * - The text up to where the match is.
24253 * - The substituted text.
24254 * - The text after the match.
24255 */
24256 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24257 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24258 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24259 {
24260 ga_clear(&ga);
24261 break;
24262 }
24263
24264 /* copy the text up to where the match is */
24265 i = (int)(regmatch.startp[0] - tail);
24266 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24267 /* add the substituted text */
24268 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24269 + ga.ga_len + i, TRUE, TRUE, FALSE);
24270 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024271 /* avoid getting stuck on a match with an empty string */
24272 if (tail == regmatch.endp[0])
24273 {
24274 if (*tail == NUL)
24275 break;
24276 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24277 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278 }
24279 else
24280 {
24281 tail = regmatch.endp[0];
24282 if (*tail == NUL)
24283 break;
24284 }
24285 if (!do_all)
24286 break;
24287 }
24288
24289 if (ga.ga_data != NULL)
24290 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24291
Bram Moolenaar473de612013-06-08 18:19:48 +020024292 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293 }
24294
24295 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24296 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024297 if (p_cpo == empty_option)
24298 p_cpo = save_cpo;
24299 else
24300 /* Darn, evaluating {sub} expression changed the value. */
24301 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302
24303 return ret;
24304}
24305
24306#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */