blob: 5085b975b8b5114c6e1f99c234f77987e47a9d23 [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 Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
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 */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200364 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000365};
366
367/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000368#define vv_type vv_di.di_tv.v_type
369#define vv_nr vv_di.di_tv.vval.v_number
370#define vv_float vv_di.di_tv.vval.v_float
371#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000372#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000373#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000374
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200375static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000376#define vimvarht vimvardict.dv_hashtab
377
Bram Moolenaara40058a2005-07-11 22:42:07 +0000378static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
379static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
381static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
382static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
384static void list_glob_vars __ARGS((int *first));
385static void list_buf_vars __ARGS((int *first));
386static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000387#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000388static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000389#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_vim_vars __ARGS((int *first));
391static void list_script_vars __ARGS((int *first));
392static void list_func_vars __ARGS((int *first));
393static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000394static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
395static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100396static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static void clear_lval __ARGS((lval_T *lp));
398static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
399static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
401static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
402static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
403static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
404static void item_lock __ARGS((typval_T *tv, int deep, int lock));
405static int tv_islocked __ARGS((typval_T *tv));
406
Bram Moolenaar33570922005-01-25 22:26:29 +0000407static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
408static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000413static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
414static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000415
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000416static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000421static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100423static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
424static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
425static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000426static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000428static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
430static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100433static 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 +0000434static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000435static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200436static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
438static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000441static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
444static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000446#ifdef FEAT_FLOAT
447static int string2float __ARGS((char_u *text, float_T *value));
448#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
450static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100451static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static 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 +0200453static 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 +0000454static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000455static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000457#ifdef FEAT_FLOAT
458static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200459static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100462static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200468static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000469static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200470static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100481static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100483static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
486static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
487#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000488static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000491static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000493#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000494static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000495static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
501static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200502static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
507static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200517static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200519#ifdef FEAT_FLOAT
520static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
521#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000524static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#ifdef FEAT_FLOAT
531static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200533static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000535static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000544static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000546static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000552static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000560static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000561static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000562static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000563static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200566static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000567static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000575static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000589static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100594static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000596static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200609static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000610static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
611#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200612#ifdef FEAT_LUA
613static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
614#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000620static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000621static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000623static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000627#ifdef vim_mkdir
628static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100631#ifdef FEAT_MZSCHEME
632static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100636static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000638#ifdef FEAT_FLOAT
639static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000642static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000643static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200644#ifdef FEAT_PYTHON3
645static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
647#ifdef FEAT_PYTHON
648static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
649#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000651static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000652static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000664#ifdef FEAT_FLOAT
665static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200667static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100669static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100689#ifdef FEAT_CRYPT
690static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
691#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000692static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200693static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
696static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200697static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000698#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000700static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000701static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#ifdef FEAT_FLOAT
705static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
707#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000708static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200709static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710#ifdef HAVE_STRFTIME
711static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
712#endif
713static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200719static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200720static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000726static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200727static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200729static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000730static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000731static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000732static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000733static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000734static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000736static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200737#ifdef FEAT_FLOAT
738static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
740#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000744#ifdef FEAT_FLOAT
745static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
746#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200748static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200749static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100750static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100754static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000761static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100765static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000766
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000767static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000768static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static int get_env_len __ARGS((char_u **arg));
770static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000772static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
773#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
774#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
775 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000776static 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 +0000777static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000778static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100779static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000780static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static typval_T *alloc_tv __ARGS((void));
782static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static void init_tv __ARGS((typval_T *varp));
784static long get_tv_number __ARGS((typval_T *varp));
785static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000786static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static char_u *get_tv_string __ARGS((typval_T *varp));
788static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100790static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
791static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
793static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
794static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000795static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
796static 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 +0000797static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
798static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000799static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200800static int var_check_func_name __ARGS((char_u *name, int new_var));
801static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000802static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000803static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
805static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
806static int eval_fname_script __ARGS((char_u *p));
807static int eval_fname_sid __ARGS((char_u *p));
808static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static ufunc_T *find_func __ARGS((char_u *name));
810static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200811static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#ifdef FEAT_PROFILE
813static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000814static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
815static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
816static int
817# ifdef __BORLANDC__
818 _RTLENTRYF
819# endif
820 prof_total_cmp __ARGS((const void *s1, const void *s2));
821static int
822# ifdef __BORLANDC__
823 _RTLENTRYF
824# endif
825 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200827static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000828static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000829static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831static 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 +0000832static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
833static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
836static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000837static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000838static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000839static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200840static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200841static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000842
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200843
844#ifdef EBCDIC
845static int compare_func_name __ARGS((const void *s1, const void *s2));
846static void sortFunctions __ARGS(());
847#endif
848
Bram Moolenaar33570922005-01-25 22:26:29 +0000849/*
850 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000851 */
852 void
853eval_init()
854{
Bram Moolenaar33570922005-01-25 22:26:29 +0000855 int i;
856 struct vimvar *p;
857
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200858 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
859 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200860 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000861 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000862 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000863
864 for (i = 0; i < VV_LEN; ++i)
865 {
866 p = &vimvars[i];
867 STRCPY(p->vv_di.di_key, p->vv_name);
868 if (p->vv_flags & VV_RO)
869 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
870 else if (p->vv_flags & VV_RO_SBX)
871 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
872 else
873 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000874
875 /* add to v: scope dict, unless the value is not always available */
876 if (p->vv_type != VAR_UNKNOWN)
877 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000878 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000879 /* add to compat scope dict */
880 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000881 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000882 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100883 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200884 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200885
886#ifdef EBCDIC
887 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100888 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200889 */
890 sortFunctions();
891#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000892}
893
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000894#if defined(EXITFREE) || defined(PROTO)
895 void
896eval_clear()
897{
898 int i;
899 struct vimvar *p;
900
901 for (i = 0; i < VV_LEN; ++i)
902 {
903 p = &vimvars[i];
904 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000905 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000906 vim_free(p->vv_str);
907 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000908 }
909 else if (p->vv_di.di_tv.v_type == VAR_LIST)
910 {
911 list_unref(p->vv_list);
912 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914 }
915 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000916 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000917 hash_clear(&compat_hashtab);
918
Bram Moolenaard9fba312005-06-26 22:34:35 +0000919 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100920# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200921 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100922# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000923
924 /* global variables */
925 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000927 /* autoloaded script names */
928 ga_clear_strings(&ga_loaded);
929
Bram Moolenaarcca74132013-09-25 21:00:28 +0200930 /* Script-local variables. First clear all the variables and in a second
931 * loop free the scriptvar_T, because a variable in one script might hold
932 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200933 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200934 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200935 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200936 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200937 ga_clear(&ga_scripts);
938
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000939 /* unreferenced lists and dicts */
940 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000941
942 /* functions */
943 free_all_functions();
944 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000945}
946#endif
947
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 * Return the name of the executed function.
950 */
951 char_u *
952func_name(cookie)
953 void *cookie;
954{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000955 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the next breakpoint line for a funccall cookie.
960 */
961 linenr_T *
962func_breakpoint(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the address holding the debug tick for a funccall cookie.
970 */
971 int *
972func_dbg_tick(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/*
979 * Return the nesting level for a funccall cookie.
980 */
981 int
982func_level(cookie)
983 void *cookie;
984{
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000989funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000991/* pointer to list of previously used funccal, still around because some
992 * item in it is still being used. */
993funccall_T *previous_funccal = NULL;
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995/*
996 * Return TRUE when a function was ended by a ":return" command.
997 */
998 int
999current_func_returned()
1000{
1001 return current_funccal->returned;
1002}
1003
1004
1005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 * Set an internal variable to a string value. Creates the variable if it does
1007 * not already exist.
1008 */
1009 void
1010set_internal_string_var(name, value)
1011 char_u *name;
1012 char_u *value;
1013{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001015 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 val = vim_strsave(value);
1018 if (val != NULL)
1019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001023 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001024 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 }
1026 }
1027}
1028
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001030static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031static char_u *redir_endp = NULL;
1032static char_u *redir_varname = NULL;
1033
1034/*
1035 * Start recording command output to a variable
1036 * Returns OK if successfully completed the setup. FAIL otherwise.
1037 */
1038 int
1039var_redir_start(name, append)
1040 char_u *name;
1041 int append; /* append to an existing variable */
1042{
1043 int save_emsg;
1044 int err;
1045 typval_T tv;
1046
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001047 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001048 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 {
1050 EMSG(_(e_invarg));
1051 return FAIL;
1052 }
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055 redir_varname = vim_strsave(name);
1056 if (redir_varname == NULL)
1057 return FAIL;
1058
1059 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1060 if (redir_lval == NULL)
1061 {
1062 var_redir_stop();
1063 return FAIL;
1064 }
1065
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001066 /* The output is stored in growarray "redir_ga" until redirection ends. */
1067 ga_init2(&redir_ga, (int)sizeof(char), 500);
1068
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001070 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001071 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1073 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001074 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 if (redir_endp != NULL && *redir_endp != NUL)
1076 /* Trailing characters are present after the variable name */
1077 EMSG(_(e_trailing));
1078 else
1079 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001080 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 var_redir_stop();
1082 return FAIL;
1083 }
1084
1085 /* check if we can write to the variable: set it to or append an empty
1086 * string */
1087 save_emsg = did_emsg;
1088 did_emsg = FALSE;
1089 tv.v_type = VAR_STRING;
1090 tv.vval.v_string = (char_u *)"";
1091 if (append)
1092 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1093 else
1094 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001095 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001097 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 if (err)
1099 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001100 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 var_redir_stop();
1102 return FAIL;
1103 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104
1105 return OK;
1106}
1107
1108/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 * Append "value[value_len]" to the variable set by var_redir_start().
1110 * The actual appending is postponed until redirection ends, because the value
1111 * appended may in fact be the string we write to, changing it may cause freed
1112 * memory to be used:
1113 * :redir => foo
1114 * :let foo
1115 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 */
1117 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123
1124 if (redir_lval == NULL)
1125 return;
1126
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001128 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001130 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001132 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 {
1134 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136 }
1137 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139}
1140
1141/*
1142 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 */
1145 void
1146var_redir_stop()
1147{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 typval_T tv;
1149
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 if (redir_lval != NULL)
1151 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* If there was no error: assign the text to the variable. */
1153 if (redir_endp != NULL)
1154 {
1155 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1156 tv.v_type = VAR_STRING;
1157 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001158 /* Call get_lval() again, if it's inside a Dict or List it may
1159 * have changed. */
1160 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001161 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001162 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1163 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1164 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 /* free the collected output */
1168 vim_free(redir_ga.ga_data);
1169 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001170
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 vim_free(redir_lval);
1172 redir_lval = NULL;
1173 }
1174 vim_free(redir_varname);
1175 redir_varname = NULL;
1176}
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178# if defined(FEAT_MBYTE) || defined(PROTO)
1179 int
1180eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1181 char_u *enc_from;
1182 char_u *enc_to;
1183 char_u *fname_from;
1184 char_u *fname_to;
1185{
1186 int err = FALSE;
1187
1188 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1189 set_vim_var_string(VV_CC_TO, enc_to, -1);
1190 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1191 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1192 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1193 err = TRUE;
1194 set_vim_var_string(VV_CC_FROM, NULL, -1);
1195 set_vim_var_string(VV_CC_TO, NULL, -1);
1196 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1197 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1198
1199 if (err)
1200 return FAIL;
1201 return OK;
1202}
1203# endif
1204
1205# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1206 int
1207eval_printexpr(fname, args)
1208 char_u *fname;
1209 char_u *args;
1210{
1211 int err = FALSE;
1212
1213 set_vim_var_string(VV_FNAME_IN, fname, -1);
1214 set_vim_var_string(VV_CMDARG, args, -1);
1215 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1216 err = TRUE;
1217 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1218 set_vim_var_string(VV_CMDARG, NULL, -1);
1219
1220 if (err)
1221 {
1222 mch_remove(fname);
1223 return FAIL;
1224 }
1225 return OK;
1226}
1227# endif
1228
1229# if defined(FEAT_DIFF) || defined(PROTO)
1230 void
1231eval_diff(origfile, newfile, outfile)
1232 char_u *origfile;
1233 char_u *newfile;
1234 char_u *outfile;
1235{
1236 int err = FALSE;
1237
1238 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1239 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1240 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1241 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1242 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1243 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1244 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1245}
1246
1247 void
1248eval_patch(origfile, difffile, outfile)
1249 char_u *origfile;
1250 char_u *difffile;
1251 char_u *outfile;
1252{
1253 int err;
1254
1255 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1256 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1257 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1258 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1261 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1262}
1263# endif
1264
1265/*
1266 * Top level evaluation function, returning a boolean.
1267 * Sets "error" to TRUE if there was an error.
1268 * Return TRUE or FALSE.
1269 */
1270 int
1271eval_to_bool(arg, error, nextcmd, skip)
1272 char_u *arg;
1273 int *error;
1274 char_u **nextcmd;
1275 int skip; /* only parse, don't execute */
1276{
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int retval = FALSE;
1279
1280 if (skip)
1281 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 else
1285 {
1286 *error = FALSE;
1287 if (!skip)
1288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001289 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 }
1293 if (skip)
1294 --emsg_skip;
1295
1296 return retval;
1297}
1298
1299/*
1300 * Top level evaluation function, returning a string. If "skip" is TRUE,
1301 * only parsing to "nextcmd" is done, without reporting errors. Return
1302 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1303 */
1304 char_u *
1305eval_to_string_skip(arg, nextcmd, skip)
1306 char_u *arg;
1307 char_u **nextcmd;
1308 int skip; /* only parse, don't execute */
1309{
Bram Moolenaar33570922005-01-25 22:26:29 +00001310 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 char_u *retval;
1312
1313 if (skip)
1314 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001315 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 retval = NULL;
1317 else
1318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 retval = vim_strsave(get_tv_string(&tv));
1320 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 if (skip)
1323 --emsg_skip;
1324
1325 return retval;
1326}
1327
1328/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329 * Skip over an expression at "*pp".
1330 * Return FAIL for an error, OK otherwise.
1331 */
1332 int
1333skip_expr(pp)
1334 char_u **pp;
1335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001337
1338 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001340}
1341
1342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001344 * When "convert" is TRUE convert a List into a sequence of lines and convert
1345 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 * Return pointer to allocated memory, or NULL for failure.
1347 */
1348 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 char_u *arg;
1351 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
Bram Moolenaar33570922005-01-25 22:26:29 +00001354 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001357#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001358 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 retval = NULL;
1363 else
1364 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 {
1367 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001368 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001369 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001370 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001371 if (tv.vval.v_list->lv_len > 0)
1372 ga_append(&ga, NL);
1373 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001374 ga_append(&ga, NUL);
1375 retval = (char_u *)ga.ga_data;
1376 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377#ifdef FEAT_FLOAT
1378 else if (convert && tv.v_type == VAR_FLOAT)
1379 {
1380 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1381 retval = vim_strsave(numbuf);
1382 }
1383#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001384 else
1385 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388
1389 return retval;
1390}
1391
1392/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 * Call eval_to_string() without using current local variables and using
1394 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 */
1396 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 char_u *arg;
1399 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 char_u *retval;
1403 void *save_funccalp;
1404
1405 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 if (use_sandbox)
1407 ++sandbox;
1408 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001409 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001410 if (use_sandbox)
1411 --sandbox;
1412 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 restore_funccal(save_funccalp);
1414 return retval;
1415}
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417/*
1418 * Top level evaluation function, returning a number.
1419 * Evaluates "expr" silently.
1420 * Returns -1 for an error.
1421 */
1422 int
1423eval_to_number(expr)
1424 char_u *expr;
1425{
Bram Moolenaar33570922005-01-25 22:26:29 +00001426 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001428 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 ++emsg_off;
1431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 retval = -1;
1434 else
1435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001436 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439 --emsg_off;
1440
1441 return retval;
1442}
1443
Bram Moolenaara40058a2005-07-11 22:42:07 +00001444/*
1445 * Prepare v: variable "idx" to be used.
1446 * Save the current typeval in "save_tv".
1447 * When not used yet add the variable to the v: hashtable.
1448 */
1449 static void
1450prepare_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 *save_tv = vimvars[idx].vv_tv;
1455 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1456 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1457}
1458
1459/*
1460 * Restore v: variable "idx" to typeval "save_tv".
1461 * When no longer defined, remove the variable from the v: hashtable.
1462 */
1463 static void
1464restore_vimvar(idx, save_tv)
1465 int idx;
1466 typval_T *save_tv;
1467{
1468 hashitem_T *hi;
1469
Bram Moolenaara40058a2005-07-11 22:42:07 +00001470 vimvars[idx].vv_tv = *save_tv;
1471 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1472 {
1473 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1474 if (HASHITEM_EMPTY(hi))
1475 EMSG2(_(e_intern2), "restore_vimvar()");
1476 else
1477 hash_remove(&vimvarht, hi);
1478 }
1479}
1480
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001481#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001482/*
1483 * Evaluate an expression to a list with suggestions.
1484 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001485 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001486 */
1487 list_T *
1488eval_spell_expr(badword, expr)
1489 char_u *badword;
1490 char_u *expr;
1491{
1492 typval_T save_val;
1493 typval_T rettv;
1494 list_T *list = NULL;
1495 char_u *p = skipwhite(expr);
1496
1497 /* Set "v:val" to the bad word. */
1498 prepare_vimvar(VV_VAL, &save_val);
1499 vimvars[VV_VAL].vv_type = VAR_STRING;
1500 vimvars[VV_VAL].vv_str = badword;
1501 if (p_verbose == 0)
1502 ++emsg_off;
1503
1504 if (eval1(&p, &rettv, TRUE) == OK)
1505 {
1506 if (rettv.v_type != VAR_LIST)
1507 clear_tv(&rettv);
1508 else
1509 list = rettv.vval.v_list;
1510 }
1511
1512 if (p_verbose == 0)
1513 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001514 restore_vimvar(VV_VAL, &save_val);
1515
1516 return list;
1517}
1518
1519/*
1520 * "list" is supposed to contain two items: a word and a number. Return the
1521 * word in "pp" and the number as the return value.
1522 * Return -1 if anything isn't right.
1523 * Used to get the good word and score from the eval_spell_expr() result.
1524 */
1525 int
1526get_spellword(list, pp)
1527 list_T *list;
1528 char_u **pp;
1529{
1530 listitem_T *li;
1531
1532 li = list->lv_first;
1533 if (li == NULL)
1534 return -1;
1535 *pp = get_tv_string(&li->li_tv);
1536
1537 li = li->li_next;
1538 if (li == NULL)
1539 return -1;
1540 return get_tv_number(&li->li_tv);
1541}
1542#endif
1543
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 * Top level evaluation function.
1546 * Returns an allocated typval_T with the result.
1547 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 */
1549 typval_T *
1550eval_expr(arg, nextcmd)
1551 char_u *arg;
1552 char_u **nextcmd;
1553{
1554 typval_T *tv;
1555
1556 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001557 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001558 {
1559 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001560 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001561 }
1562
1563 return tv;
1564}
1565
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 * Uses argv[argc] for the function arguments. Only Number and String
1570 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001571 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001573 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001574call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 char_u *func;
1576 int argc;
1577 char_u **argv;
1578 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001579 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
Bram Moolenaar33570922005-01-25 22:26:29 +00001582 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 long n;
1584 int len;
1585 int i;
1586 int doesrange;
1587 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001590 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 for (i = 0; i < argc; i++)
1595 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001596 /* Pass a NULL or empty argument as an empty string */
1597 if (argv[i] == NULL || *argv[i] == NUL)
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_STRING;
1600 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001601 continue;
1602 }
1603
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001604 if (str_arg_only)
1605 len = 0;
1606 else
1607 /* Recognize a number argument, the others must be strings. */
1608 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (len != 0 && len == (int)STRLEN(argv[i]))
1610 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001611 argvars[i].v_type = VAR_NUMBER;
1612 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 else
1615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001616 argvars[i].v_type = VAR_STRING;
1617 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619 }
1620
1621 if (safe)
1622 {
1623 save_funccalp = save_funccal();
1624 ++sandbox;
1625 }
1626
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1628 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (safe)
1632 {
1633 --sandbox;
1634 restore_funccal(save_funccalp);
1635 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 vim_free(argvars);
1637
1638 if (ret == FAIL)
1639 clear_tv(rettv);
1640
1641 return ret;
1642}
1643
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001644/*
1645 * Call vimL function "func" and return the result as a number.
1646 * Returns -1 when calling the function fails.
1647 * Uses argv[argc] for the function arguments.
1648 */
1649 long
1650call_func_retnr(func, argc, argv, safe)
1651 char_u *func;
1652 int argc;
1653 char_u **argv;
1654 int safe; /* use the sandbox */
1655{
1656 typval_T rettv;
1657 long retval;
1658
1659 /* All arguments are passed as strings, no conversion to number. */
1660 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1661 return -1;
1662
1663 retval = get_tv_number_chk(&rettv, NULL);
1664 clear_tv(&rettv);
1665 return retval;
1666}
1667
1668#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1669 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1670
Bram Moolenaar4f688582007-07-24 12:34:30 +00001671# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001673 * Call vimL function "func" and return the result as a string.
1674 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 * Uses argv[argc] for the function arguments.
1676 */
1677 void *
1678call_func_retstr(func, argc, argv, safe)
1679 char_u *func;
1680 int argc;
1681 char_u **argv;
1682 int safe; /* use the sandbox */
1683{
1684 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001685 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001687 /* All arguments are passed as strings, no conversion to number. */
1688 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 return NULL;
1690
1691 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 return retval;
1694}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001695# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001697/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001698 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001700 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 */
1702 void *
1703call_func_retlist(func, argc, argv, safe)
1704 char_u *func;
1705 int argc;
1706 char_u **argv;
1707 int safe; /* use the sandbox */
1708{
1709 typval_T rettv;
1710
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001711 /* All arguments are passed as strings, no conversion to number. */
1712 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713 return NULL;
1714
1715 if (rettv.v_type != VAR_LIST)
1716 {
1717 clear_tv(&rettv);
1718 return NULL;
1719 }
1720
1721 return rettv.vval.v_list;
1722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
1724
1725/*
1726 * Save the current function call pointer, and set it to NULL.
1727 * Used when executing autocommands and for ":source".
1728 */
1729 void *
1730save_funccal()
1731{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 current_funccal = NULL;
1735 return (void *)fc;
1736}
1737
1738 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739restore_funccal(vfc)
1740 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001742 funccall_T *fc = (funccall_T *)vfc;
1743
1744 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745}
1746
Bram Moolenaar05159a02005-02-26 23:04:13 +00001747#if defined(FEAT_PROFILE) || defined(PROTO)
1748/*
1749 * Prepare profiling for entering a child or something else that is not
1750 * counted for the script/function itself.
1751 * Should always be called in pair with prof_child_exit().
1752 */
1753 void
1754prof_child_enter(tm)
1755 proftime_T *tm; /* place to store waittime */
1756{
1757 funccall_T *fc = current_funccal;
1758
1759 if (fc != NULL && fc->func->uf_profiling)
1760 profile_start(&fc->prof_child);
1761 script_prof_save(tm);
1762}
1763
1764/*
1765 * Take care of time spent in a child.
1766 * Should always be called after prof_child_enter().
1767 */
1768 void
1769prof_child_exit(tm)
1770 proftime_T *tm; /* where waittime was stored */
1771{
1772 funccall_T *fc = current_funccal;
1773
1774 if (fc != NULL && fc->func->uf_profiling)
1775 {
1776 profile_end(&fc->prof_child);
1777 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1778 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1779 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1780 }
1781 script_prof_restore(tm);
1782}
1783#endif
1784
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#ifdef FEAT_FOLDING
1787/*
1788 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1789 * it in "*cp". Doesn't give error messages.
1790 */
1791 int
1792eval_foldexpr(arg, cp)
1793 char_u *arg;
1794 int *cp;
1795{
Bram Moolenaar33570922005-01-25 22:26:29 +00001796 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 int retval;
1798 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001799 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1800 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001803 if (use_sandbox)
1804 ++sandbox;
1805 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 retval = 0;
1809 else
1810 {
1811 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 if (tv.v_type == VAR_NUMBER)
1813 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001814 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a string, check if there is a non-digit before
1819 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (!VIM_ISDIGIT(*s) && *s != '-')
1822 *cp = *s++;
1823 retval = atol((char *)s);
1824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
1827 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001828 if (use_sandbox)
1829 --sandbox;
1830 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832 return retval;
1833}
1834#endif
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let" list all variable values
1838 * ":let var1 var2" list variable values
1839 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001840 * ":let var += expr" assignment command.
1841 * ":let var -= expr" assignment command.
1842 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 */
1845 void
1846ex_let(eap)
1847 exarg_T *eap;
1848{
1849 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 int var_count = 0;
1854 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001856 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001857 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaardb552d602006-03-23 22:59:57 +00001859 argend = skip_var_list(arg, &var_count, &semicolon);
1860 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001862 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1863 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001864 expr = skipwhite(argend);
1865 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1866 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001868 /*
1869 * ":let" without "=": list variables
1870 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 if (*arg == '[')
1872 EMSG(_(e_invarg));
1873 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001875 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001879 list_glob_vars(&first);
1880 list_buf_vars(&first);
1881 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001882#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001883 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001884#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001885 list_script_vars(&first);
1886 list_func_vars(&first);
1887 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 eap->nextcmd = check_nextcmd(arg);
1890 }
1891 else
1892 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 op[0] = '=';
1894 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001895 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001896 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001897 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1898 op[0] = *expr; /* +=, -= or .= */
1899 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001901 else
1902 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (eap->skip)
1905 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (eap->skip)
1908 {
1909 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 --emsg_skip;
1912 }
1913 else if (i != FAIL)
1914 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 }
1919 }
1920}
1921
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922/*
1923 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1924 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001925 * When "nextchars" is not NULL it points to a string with characters that
1926 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1927 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 * Returns OK or FAIL;
1929 */
1930 static int
1931ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1932 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 int copy; /* copy values from "tv", don't move */
1935 int semicolon; /* from skip_var_list() */
1936 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938{
1939 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001942 listitem_T *item;
1943 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944
1945 if (*arg != '[')
1946 {
1947 /*
1948 * ":let var = expr" or ":for var in list"
1949 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001950 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 return OK;
1953 }
1954
1955 /*
1956 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1957 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001958 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 {
1960 EMSG(_(e_listreq));
1961 return FAIL;
1962 }
1963
1964 i = list_len(l);
1965 if (semicolon == 0 && var_count < i)
1966 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001967 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 return FAIL;
1969 }
1970 if (var_count - semicolon > i)
1971 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001972 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 return FAIL;
1974 }
1975
1976 item = l->lv_first;
1977 while (*arg != ']')
1978 {
1979 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 item = item->li_next;
1982 if (arg == NULL)
1983 return FAIL;
1984
1985 arg = skipwhite(arg);
1986 if (*arg == ';')
1987 {
1988 /* Put the rest of the list (may be empty) in the var after ';'.
1989 * Create a new list for this. */
1990 l = list_alloc();
1991 if (l == NULL)
1992 return FAIL;
1993 while (item != NULL)
1994 {
1995 list_append_tv(l, &item->li_tv);
1996 item = item->li_next;
1997 }
1998
1999 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002000 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 ltv.vval.v_list = l;
2002 l->lv_refcount = 1;
2003
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002004 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2005 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 clear_tv(&ltv);
2007 if (arg == NULL)
2008 return FAIL;
2009 break;
2010 }
2011 else if (*arg != ',' && *arg != ']')
2012 {
2013 EMSG2(_(e_intern2), "ex_let_vars()");
2014 return FAIL;
2015 }
2016 }
2017
2018 return OK;
2019}
2020
2021/*
2022 * Skip over assignable variable "var" or list of variables "[var, var]".
2023 * Used for ":let varvar = expr" and ":for varvar in expr".
2024 * For "[var, var]" increment "*var_count" for each variable.
2025 * for "[var, var; var]" set "semicolon".
2026 * Return NULL for an error.
2027 */
2028 static char_u *
2029skip_var_list(arg, var_count, semicolon)
2030 char_u *arg;
2031 int *var_count;
2032 int *semicolon;
2033{
2034 char_u *p, *s;
2035
2036 if (*arg == '[')
2037 {
2038 /* "[var, var]": find the matching ']'. */
2039 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002040 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 {
2042 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2043 s = skip_var_one(p);
2044 if (s == p)
2045 {
2046 EMSG2(_(e_invarg2), p);
2047 return NULL;
2048 }
2049 ++*var_count;
2050
2051 p = skipwhite(s);
2052 if (*p == ']')
2053 break;
2054 else if (*p == ';')
2055 {
2056 if (*semicolon == 1)
2057 {
2058 EMSG(_("Double ; in list of variables"));
2059 return NULL;
2060 }
2061 *semicolon = 1;
2062 }
2063 else if (*p != ',')
2064 {
2065 EMSG2(_(e_invarg2), p);
2066 return NULL;
2067 }
2068 }
2069 return p + 1;
2070 }
2071 else
2072 return skip_var_one(arg);
2073}
2074
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002076 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002077 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002078 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002079 static char_u *
2080skip_var_one(arg)
2081 char_u *arg;
2082{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002083 if (*arg == '@' && arg[1] != NUL)
2084 return arg + 2;
2085 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2086 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087}
2088
Bram Moolenaara7043832005-01-21 11:56:39 +00002089/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002090 * List variables for hashtab "ht" with prefix "prefix".
2091 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002093 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002094list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002095 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002098 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099{
Bram Moolenaar33570922005-01-25 22:26:29 +00002100 hashitem_T *hi;
2101 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 int todo;
2103
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002104 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2106 {
2107 if (!HASHITEM_EMPTY(hi))
2108 {
2109 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 di = HI2DI(hi);
2111 if (empty || di->di_tv.v_type != VAR_STRING
2112 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002114 }
2115 }
2116}
2117
2118/*
2119 * List global variables.
2120 */
2121 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122list_glob_vars(first)
2123 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002124{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002126}
2127
2128/*
2129 * List buffer variables.
2130 */
2131 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132list_buf_vars(first)
2133 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002134{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002135 char_u numbuf[NUMBUFLEN];
2136
Bram Moolenaar429fa852013-04-15 12:27:36 +02002137 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002139
2140 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2142 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002143}
2144
2145/*
2146 * List window variables.
2147 */
2148 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149list_win_vars(first)
2150 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002151{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002152 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002154}
2155
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002156#ifdef FEAT_WINDOWS
2157/*
2158 * List tab page variables.
2159 */
2160 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161list_tab_vars(first)
2162 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002164 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002166}
2167#endif
2168
Bram Moolenaara7043832005-01-21 11:56:39 +00002169/*
2170 * List Vim variables.
2171 */
2172 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173list_vim_vars(first)
2174 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177}
2178
2179/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180 * List script-local variables, if there is a script.
2181 */
2182 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183list_script_vars(first)
2184 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185{
2186 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2188 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002189}
2190
2191/*
2192 * List function variables, if there is a function.
2193 */
2194 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195list_func_vars(first)
2196 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002197{
2198 if (current_funccal != NULL)
2199 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002201}
2202
2203/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 * List variables in "arg".
2205 */
2206 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 exarg_T *eap;
2209 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211{
2212 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 char_u *name_start;
2216 char_u *arg_subsc;
2217 char_u *tofree;
2218 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219
2220 while (!ends_excmd(*arg) && !got_int)
2221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002224 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2226 {
2227 emsg_severe = TRUE;
2228 EMSG(_(e_trailing));
2229 break;
2230 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 /* get_name_len() takes care of expanding curly braces */
2235 name_start = name = arg;
2236 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2237 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 /* This is mainly to keep test 49 working: when expanding
2240 * curly braces fails overrule the exception error message. */
2241 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 emsg_severe = TRUE;
2244 EMSG2(_(e_invarg2), arg);
2245 break;
2246 }
2247 error = TRUE;
2248 }
2249 else
2250 {
2251 if (tofree != NULL)
2252 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002253 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 else
2256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 /* handle d.key, l[idx], f(expr) */
2258 arg_subsc = arg;
2259 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002260 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002266 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002267 case 'g': list_glob_vars(first); break;
2268 case 'b': list_buf_vars(first); break;
2269 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002270#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002271 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002272#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002273 case 'v': list_vim_vars(first); break;
2274 case 's': list_script_vars(first); break;
2275 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 default:
2277 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002278 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 }
2280 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 {
2282 char_u numbuf[NUMBUFLEN];
2283 char_u *tf;
2284 int c;
2285 char_u *s;
2286
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002287 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 c = *arg;
2289 *arg = NUL;
2290 list_one_var_a((char_u *)"",
2291 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002292 tv.v_type,
2293 s == NULL ? (char_u *)"" : s,
2294 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 *arg = c;
2296 vim_free(tf);
2297 }
2298 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 }
2301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302
2303 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305
2306 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308
2309 return arg;
2310}
2311
2312/*
2313 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2314 * Returns a pointer to the char just after the var name.
2315 * Returns NULL if there is an error.
2316 */
2317 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002318ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002320 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002322 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324{
2325 int c1;
2326 char_u *name;
2327 char_u *p;
2328 char_u *arg_end = NULL;
2329 int len;
2330 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332
2333 /*
2334 * ":let $VAR = expr": Set environment variable.
2335 */
2336 if (*arg == '$')
2337 {
2338 /* Find the end of the name. */
2339 ++arg;
2340 name = arg;
2341 len = get_env_len(&arg);
2342 if (len == 0)
2343 EMSG2(_(e_invarg2), name - 1);
2344 else
2345 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 if (op != NULL && (*op == '+' || *op == '-'))
2347 EMSG2(_(e_letwrong), op);
2348 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002349 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002351 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 {
2353 c1 = name[len];
2354 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 p = get_tv_string_chk(tv);
2356 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002357 {
2358 int mustfree = FALSE;
2359 char_u *s = vim_getenv(name, &mustfree);
2360
2361 if (s != NULL)
2362 {
2363 p = tofree = concat_str(s, p);
2364 if (mustfree)
2365 vim_free(s);
2366 }
2367 }
2368 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002369 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002371 if (STRICMP(name, "HOME") == 0)
2372 init_homedir();
2373 else if (didset_vim && STRICMP(name, "VIM") == 0)
2374 didset_vim = FALSE;
2375 else if (didset_vimruntime
2376 && STRICMP(name, "VIMRUNTIME") == 0)
2377 didset_vimruntime = FALSE;
2378 arg_end = arg;
2379 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 }
2383 }
2384 }
2385
2386 /*
2387 * ":let &option = expr": Set option value.
2388 * ":let &l:option = expr": Set local option value.
2389 * ":let &g:option = expr": Set global option value.
2390 */
2391 else if (*arg == '&')
2392 {
2393 /* Find the end of the name. */
2394 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002395 if (p == NULL || (endchars != NULL
2396 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 EMSG(_(e_letunexp));
2398 else
2399 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 long n;
2401 int opt_type;
2402 long numval;
2403 char_u *stringval = NULL;
2404 char_u *s;
2405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 c1 = *p;
2407 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408
2409 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002410 s = get_tv_string_chk(tv); /* != NULL if number or string */
2411 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 {
2413 opt_type = get_option_value(arg, &numval,
2414 &stringval, opt_flags);
2415 if ((opt_type == 1 && *op == '.')
2416 || (opt_type == 0 && *op != '.'))
2417 EMSG2(_(e_letwrong), op);
2418 else
2419 {
2420 if (opt_type == 1) /* number */
2421 {
2422 if (*op == '+')
2423 n = numval + n;
2424 else
2425 n = numval - n;
2426 }
2427 else if (opt_type == 0 && stringval != NULL) /* string */
2428 {
2429 s = concat_str(stringval, s);
2430 vim_free(stringval);
2431 stringval = s;
2432 }
2433 }
2434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002435 if (s != NULL)
2436 {
2437 set_option_value(arg, n, s, opt_flags);
2438 arg_end = p;
2439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 }
2443 }
2444
2445 /*
2446 * ":let @r = expr": Set register contents.
2447 */
2448 else if (*arg == '@')
2449 {
2450 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 if (op != NULL && (*op == '+' || *op == '-'))
2452 EMSG2(_(e_letwrong), op);
2453 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002454 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 EMSG(_(e_letunexp));
2456 else
2457 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002458 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 char_u *s;
2460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461 p = get_tv_string_chk(tv);
2462 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002464 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 if (s != NULL)
2466 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002467 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 vim_free(s);
2469 }
2470 }
2471 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002472 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002474 arg_end = arg + 1;
2475 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002476 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
2478 }
2479
2480 /*
2481 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002484 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002486 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002488 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2492 EMSG(_(e_letunexp));
2493 else
2494 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 arg_end = p;
2497 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 }
2501
2502 else
2503 EMSG2(_(e_invarg2), arg);
2504
2505 return arg_end;
2506}
2507
2508/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2510 */
2511 static int
2512check_changedtick(arg)
2513 char_u *arg;
2514{
2515 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2516 {
2517 EMSG2(_(e_readonlyvar), arg);
2518 return TRUE;
2519 }
2520 return FALSE;
2521}
2522
2523/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 * Get an lval: variable, Dict item or List item that can be assigned a value
2525 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2526 * "name.key", "name.key[expr]" etc.
2527 * Indexing only works if "name" is an existing List or Dictionary.
2528 * "name" points to the start of the name.
2529 * If "rettv" is not NULL it points to the value to be assigned.
2530 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2531 * wrong; must end in space or cmd separator.
2532 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002533 * flags:
2534 * GLV_QUIET: do not give error messages
2535 * GLV_NO_AUTOLOAD: do not use script autoloading
2536 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002538 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 */
2541 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002542get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 typval_T *rettv;
2545 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 int unlet;
2547 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002548 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002551 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 char_u *expr_start, *expr_end;
2553 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 dictitem_T *v;
2555 typval_T var1;
2556 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002558 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002562 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566
2567 if (skip)
2568 {
2569 /* When skipping just find the end of the name. */
2570 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002571 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 }
2573
2574 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002575 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 if (expr_start != NULL)
2577 {
2578 /* Don't expand the name when we already know there is an error. */
2579 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2580 && *p != '[' && *p != '.')
2581 {
2582 EMSG(_(e_trailing));
2583 return NULL;
2584 }
2585
2586 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2587 if (lp->ll_exp_name == NULL)
2588 {
2589 /* Report an invalid expression in braces, unless the
2590 * expression evaluation has been cancelled due to an
2591 * aborting error, an interrupt, or an exception. */
2592 if (!aborting() && !quiet)
2593 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002594 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 EMSG2(_(e_invarg2), name);
2596 return NULL;
2597 }
2598 }
2599 lp->ll_name = lp->ll_exp_name;
2600 }
2601 else
2602 lp->ll_name = name;
2603
2604 /* Without [idx] or .key we are done. */
2605 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2606 return p;
2607
2608 cc = *p;
2609 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002610 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (v == NULL && !quiet)
2612 EMSG2(_(e_undefvar), lp->ll_name);
2613 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 if (v == NULL)
2615 return NULL;
2616
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 /*
2618 * Loop until no more [idx] or .key is following.
2619 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002620 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2624 && !(lp->ll_tv->v_type == VAR_DICT
2625 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 if (!quiet)
2628 EMSG(_("E689: Can only index a List or Dictionary"));
2629 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002630 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!quiet)
2634 EMSG(_("E708: [:] must come last"));
2635 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 len = -1;
2639 if (*p == '.')
2640 {
2641 key = p + 1;
2642 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2643 ;
2644 if (len == 0)
2645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!quiet)
2647 EMSG(_(e_emptykey));
2648 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 }
2650 p = key + len;
2651 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 else
2653 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 if (*p == ':')
2657 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 else
2659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 empty1 = FALSE;
2661 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002663 if (get_tv_string_chk(&var1) == NULL)
2664 {
2665 /* not a number or string */
2666 clear_tv(&var1);
2667 return NULL;
2668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
2670
2671 /* Optionally get the second index [ :expr]. */
2672 if (*p == ':')
2673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002677 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (rettv != NULL && (rettv->v_type != VAR_LIST
2683 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (!quiet)
2686 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (!empty1)
2688 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
2691 p = skipwhite(p + 1);
2692 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 else
2695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (!empty1)
2700 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002703 if (get_tv_string_chk(&var2) == NULL)
2704 {
2705 /* not a number or string */
2706 if (!empty1)
2707 clear_tv(&var1);
2708 clear_tv(&var2);
2709 return NULL;
2710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002716
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 if (!empty1)
2722 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
2727
2728 /* Skip to past ']'. */
2729 ++p;
2730 }
2731
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 {
2734 if (len == -1)
2735 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002737 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 if (*key == NUL)
2739 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 if (!quiet)
2741 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 }
2745 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002746 lp->ll_list = NULL;
2747 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002748 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002749
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002750 /* When assigning to a scope dictionary check that a function and
2751 * variable name is valid (only variable name unless it is l: or
2752 * g: dictionary). Disallow overwriting a builtin function. */
2753 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002754 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002755 int prevval;
2756 int wrong;
2757
2758 if (len != -1)
2759 {
2760 prevval = key[len];
2761 key[len] = NUL;
2762 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002763 else
2764 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002765 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2766 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002767 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002768 || !valid_varname(key);
2769 if (len != -1)
2770 key[len] = prevval;
2771 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002772 return NULL;
2773 }
2774
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 /* Can't add "v:" variable. */
2778 if (lp->ll_dict == &vimvardict)
2779 {
2780 EMSG2(_(e_illvar), name);
2781 return NULL;
2782 }
2783
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002784 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002788 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 if (len == -1)
2790 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 }
2793 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 if (len == -1)
2798 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 p = NULL;
2801 break;
2802 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 /* existing variable, need to check if it can be changed */
2804 else if (var_check_ro(lp->ll_di->di_flags, name))
2805 return NULL;
2806
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 if (len == -1)
2808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 }
2811 else
2812 {
2813 /*
2814 * Get the number and item for the only or first index of the List.
2815 */
2816 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 else
2819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002820 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 clear_tv(&var1);
2822 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002823 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_list = lp->ll_tv->vval.v_list;
2825 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2826 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002828 if (lp->ll_n1 < 0)
2829 {
2830 lp->ll_n1 = 0;
2831 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2832 }
2833 }
2834 if (lp->ll_li == NULL)
2835 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002838 if (!quiet)
2839 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 }
2842
2843 /*
2844 * May need to find the item or absolute index for the second
2845 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 * When no index given: "lp->ll_empty2" is TRUE.
2847 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002851 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002857 {
2858 if (!quiet)
2859 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002861 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 }
2864
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2866 if (lp->ll_n1 < 0)
2867 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2868 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002869 {
2870 if (!quiet)
2871 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002873 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002874 }
2875
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002877 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002878 }
2879
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 return p;
2881}
2882
2883/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 */
2886 static void
2887clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002888 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889{
2890 vim_free(lp->ll_exp_name);
2891 vim_free(lp->ll_newkey);
2892}
2893
2894/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002897 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 */
2899 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002900set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002901 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002905 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906{
2907 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 listitem_T *ri;
2909 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910
2911 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002914 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 cc = *endp;
2916 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002917 if (op != NULL && *op != '=')
2918 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002919 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002921 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002922 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002923 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 {
2925 if (tv_op(&tv, rettv, op) == OK)
2926 set_var(lp->ll_name, &tv, FALSE);
2927 clear_tv(&tv);
2928 }
2929 }
2930 else
2931 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002935 else if (tv_check_lock(lp->ll_newkey == NULL
2936 ? lp->ll_tv->v_lock
2937 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2938 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 else if (lp->ll_range)
2940 {
2941 /*
2942 * Assign the List values to the list items.
2943 */
2944 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002945 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 if (op != NULL && *op != '=')
2947 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2948 else
2949 {
2950 clear_tv(&lp->ll_li->li_tv);
2951 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2952 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 ri = ri->li_next;
2954 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2955 break;
2956 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002959 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002960 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 ri = NULL;
2962 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002963 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002964 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 lp->ll_li = lp->ll_li->li_next;
2966 ++lp->ll_n1;
2967 }
2968 if (ri != NULL)
2969 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 else if (lp->ll_empty2
2971 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 : lp->ll_n1 != lp->ll_n2)
2973 EMSG(_("E711: List value has not enough items"));
2974 }
2975 else
2976 {
2977 /*
2978 * Assign to a List or Dictionary item.
2979 */
2980 if (lp->ll_newkey != NULL)
2981 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 if (op != NULL && *op != '=')
2983 {
2984 EMSG2(_(e_letwrong), op);
2985 return;
2986 }
2987
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002989 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 if (di == NULL)
2991 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002992 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2993 {
2994 vim_free(di);
2995 return;
2996 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 else if (op != NULL && *op != '=')
3000 {
3001 tv_op(lp->ll_tv, rettv, op);
3002 return;
3003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003004 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003006
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 /*
3008 * Assign the value to the variable or list item.
3009 */
3010 if (copy)
3011 copy_tv(rettv, lp->ll_tv);
3012 else
3013 {
3014 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003015 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003017 }
3018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003019}
3020
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003021/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003022 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3023 * Returns OK or FAIL.
3024 */
3025 static int
3026tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003027 typval_T *tv1;
3028 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 char_u *op;
3030{
3031 long n;
3032 char_u numbuf[NUMBUFLEN];
3033 char_u *s;
3034
3035 /* Can't do anything with a Funcref or a Dict on the right. */
3036 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3037 {
3038 switch (tv1->v_type)
3039 {
3040 case VAR_DICT:
3041 case VAR_FUNC:
3042 break;
3043
3044 case VAR_LIST:
3045 if (*op != '+' || tv2->v_type != VAR_LIST)
3046 break;
3047 /* List += List */
3048 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3049 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3050 return OK;
3051
3052 case VAR_NUMBER:
3053 case VAR_STRING:
3054 if (tv2->v_type == VAR_LIST)
3055 break;
3056 if (*op == '+' || *op == '-')
3057 {
3058 /* nr += nr or nr -= nr*/
3059 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003060#ifdef FEAT_FLOAT
3061 if (tv2->v_type == VAR_FLOAT)
3062 {
3063 float_T f = n;
3064
3065 if (*op == '+')
3066 f += tv2->vval.v_float;
3067 else
3068 f -= tv2->vval.v_float;
3069 clear_tv(tv1);
3070 tv1->v_type = VAR_FLOAT;
3071 tv1->vval.v_float = f;
3072 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003073 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003074#endif
3075 {
3076 if (*op == '+')
3077 n += get_tv_number(tv2);
3078 else
3079 n -= get_tv_number(tv2);
3080 clear_tv(tv1);
3081 tv1->v_type = VAR_NUMBER;
3082 tv1->vval.v_number = n;
3083 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 }
3085 else
3086 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003087 if (tv2->v_type == VAR_FLOAT)
3088 break;
3089
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 /* str .= str */
3091 s = get_tv_string(tv1);
3092 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3093 clear_tv(tv1);
3094 tv1->v_type = VAR_STRING;
3095 tv1->vval.v_string = s;
3096 }
3097 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003098
3099#ifdef FEAT_FLOAT
3100 case VAR_FLOAT:
3101 {
3102 float_T f;
3103
3104 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3105 && tv2->v_type != VAR_NUMBER
3106 && tv2->v_type != VAR_STRING))
3107 break;
3108 if (tv2->v_type == VAR_FLOAT)
3109 f = tv2->vval.v_float;
3110 else
3111 f = get_tv_number(tv2);
3112 if (*op == '+')
3113 tv1->vval.v_float += f;
3114 else
3115 tv1->vval.v_float -= f;
3116 }
3117 return OK;
3118#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 }
3120 }
3121
3122 EMSG2(_(e_letwrong), op);
3123 return FAIL;
3124}
3125
3126/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127 * Add a watcher to a list.
3128 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003129 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003131 list_T *l;
3132 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003133{
3134 lw->lw_next = l->lv_watch;
3135 l->lv_watch = lw;
3136}
3137
3138/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003139 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140 * No warning when it isn't found...
3141 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003142 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003144 list_T *l;
3145 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146{
Bram Moolenaar33570922005-01-25 22:26:29 +00003147 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148
3149 lwp = &l->lv_watch;
3150 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3151 {
3152 if (lw == lwrem)
3153 {
3154 *lwp = lw->lw_next;
3155 break;
3156 }
3157 lwp = &lw->lw_next;
3158 }
3159}
3160
3161/*
3162 * Just before removing an item from a list: advance watchers to the next
3163 * item.
3164 */
3165 static void
3166list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003167 list_T *l;
3168 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169{
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171
3172 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3173 if (lw->lw_item == item)
3174 lw->lw_item = item->li_next;
3175}
3176
3177/*
3178 * Evaluate the expression used in a ":for var in expr" command.
3179 * "arg" points to "var".
3180 * Set "*errp" to TRUE for an error, FALSE otherwise;
3181 * Return a pointer that holds the info. Null when there is an error.
3182 */
3183 void *
3184eval_for_line(arg, errp, nextcmdp, skip)
3185 char_u *arg;
3186 int *errp;
3187 char_u **nextcmdp;
3188 int skip;
3189{
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 typval_T tv;
3193 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194
3195 *errp = TRUE; /* default: there is an error */
3196
Bram Moolenaar33570922005-01-25 22:26:29 +00003197 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 if (fi == NULL)
3199 return NULL;
3200
3201 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3202 if (expr == NULL)
3203 return fi;
3204
3205 expr = skipwhite(expr);
3206 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3207 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003208 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209 return fi;
3210 }
3211
3212 if (skip)
3213 ++emsg_skip;
3214 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3215 {
3216 *errp = FALSE;
3217 if (!skip)
3218 {
3219 l = tv.vval.v_list;
3220 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003221 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003223 clear_tv(&tv);
3224 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225 else
3226 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003227 /* No need to increment the refcount, it's already set for the
3228 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 fi->fi_list = l;
3230 list_add_watch(l, &fi->fi_lw);
3231 fi->fi_lw.lw_item = l->lv_first;
3232 }
3233 }
3234 }
3235 if (skip)
3236 --emsg_skip;
3237
3238 return fi;
3239}
3240
3241/*
3242 * Use the first item in a ":for" list. Advance to the next.
3243 * Assign the values to the variable (list). "arg" points to the first one.
3244 * Return TRUE when a valid item was found, FALSE when at end of list or
3245 * something wrong.
3246 */
3247 int
3248next_for_item(fi_void, arg)
3249 void *fi_void;
3250 char_u *arg;
3251{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003252 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255
3256 item = fi->fi_lw.lw_item;
3257 if (item == NULL)
3258 result = FALSE;
3259 else
3260 {
3261 fi->fi_lw.lw_item = item->li_next;
3262 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3263 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3264 }
3265 return result;
3266}
3267
3268/*
3269 * Free the structure used to store info used by ":for".
3270 */
3271 void
3272free_for_info(fi_void)
3273 void *fi_void;
3274{
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003277 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003280 list_unref(fi->fi_list);
3281 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 vim_free(fi);
3283}
3284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3286
3287 void
3288set_context_for_expression(xp, arg, cmdidx)
3289 expand_T *xp;
3290 char_u *arg;
3291 cmdidx_T cmdidx;
3292{
3293 int got_eq = FALSE;
3294 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 if (cmdidx == CMD_let)
3298 {
3299 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003300 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 {
3302 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003303 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 {
3305 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003306 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 if (vim_iswhite(*p))
3308 break;
3309 }
3310 return;
3311 }
3312 }
3313 else
3314 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3315 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 while ((xp->xp_pattern = vim_strpbrk(arg,
3317 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3318 {
3319 c = *xp->xp_pattern;
3320 if (c == '&')
3321 {
3322 c = xp->xp_pattern[1];
3323 if (c == '&')
3324 {
3325 ++xp->xp_pattern;
3326 xp->xp_context = cmdidx != CMD_let || got_eq
3327 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3328 }
3329 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003332 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3333 xp->xp_pattern += 2;
3334
3335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337 else if (c == '$')
3338 {
3339 /* environment variable */
3340 xp->xp_context = EXPAND_ENV_VARS;
3341 }
3342 else if (c == '=')
3343 {
3344 got_eq = TRUE;
3345 xp->xp_context = EXPAND_EXPRESSION;
3346 }
3347 else if (c == '<'
3348 && xp->xp_context == EXPAND_FUNCTIONS
3349 && vim_strchr(xp->xp_pattern, '(') == NULL)
3350 {
3351 /* Function name can start with "<SNR>" */
3352 break;
3353 }
3354 else if (cmdidx != CMD_let || got_eq)
3355 {
3356 if (c == '"') /* string */
3357 {
3358 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3359 if (c == '\\' && xp->xp_pattern[1] != NUL)
3360 ++xp->xp_pattern;
3361 xp->xp_context = EXPAND_NOTHING;
3362 }
3363 else if (c == '\'') /* literal string */
3364 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003365 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3367 /* skip */ ;
3368 xp->xp_context = EXPAND_NOTHING;
3369 }
3370 else if (c == '|')
3371 {
3372 if (xp->xp_pattern[1] == '|')
3373 {
3374 ++xp->xp_pattern;
3375 xp->xp_context = EXPAND_EXPRESSION;
3376 }
3377 else
3378 xp->xp_context = EXPAND_COMMANDS;
3379 }
3380 else
3381 xp->xp_context = EXPAND_EXPRESSION;
3382 }
3383 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003384 /* Doesn't look like something valid, expand as an expression
3385 * anyway. */
3386 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 arg = xp->xp_pattern;
3388 if (*arg != NUL)
3389 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3390 /* skip */ ;
3391 }
3392 xp->xp_pattern = arg;
3393}
3394
3395#endif /* FEAT_CMDL_COMPL */
3396
3397/*
3398 * ":1,25call func(arg1, arg2)" function call.
3399 */
3400 void
3401ex_call(eap)
3402 exarg_T *eap;
3403{
3404 char_u *arg = eap->arg;
3405 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003407 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003409 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 linenr_T lnum;
3411 int doesrange;
3412 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003413 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003415 if (eap->skip)
3416 {
3417 /* trans_function_name() doesn't work well when skipping, use eval0()
3418 * instead to skip to any following command, e.g. for:
3419 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003420 ++emsg_skip;
3421 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3422 clear_tv(&rettv);
3423 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003424 return;
3425 }
3426
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003427 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003428 if (fudi.fd_newkey != NULL)
3429 {
3430 /* Still need to give an error message for missing key. */
3431 EMSG2(_(e_dictkey), fudi.fd_newkey);
3432 vim_free(fudi.fd_newkey);
3433 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003434 if (tofree == NULL)
3435 return;
3436
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003437 /* Increase refcount on dictionary, it could get deleted when evaluating
3438 * the arguments. */
3439 if (fudi.fd_dict != NULL)
3440 ++fudi.fd_dict->dv_refcount;
3441
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003442 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003443 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003444 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaar532c7802005-01-27 14:44:31 +00003446 /* Skip white space to allow ":call func ()". Not good, but required for
3447 * backward compatibility. */
3448 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
3451 if (*startarg != '(')
3452 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003453 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 goto end;
3455 }
3456
3457 /*
3458 * When skipping, evaluate the function once, to find the end of the
3459 * arguments.
3460 * When the function takes a range, this is discovered after the first
3461 * call, and the loop is broken.
3462 */
3463 if (eap->skip)
3464 {
3465 ++emsg_skip;
3466 lnum = eap->line2; /* do it once, also with an invalid range */
3467 }
3468 else
3469 lnum = eap->line1;
3470 for ( ; lnum <= eap->line2; ++lnum)
3471 {
3472 if (!eap->skip && eap->addr_count > 0)
3473 {
3474 curwin->w_cursor.lnum = lnum;
3475 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003476#ifdef FEAT_VIRTUALEDIT
3477 curwin->w_cursor.coladd = 0;
3478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003481 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003482 eap->line1, eap->line2, &doesrange,
3483 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
3485 failed = TRUE;
3486 break;
3487 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003488
3489 /* Handle a function returning a Funcref, Dictionary or List. */
3490 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3491 {
3492 failed = TRUE;
3493 break;
3494 }
3495
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (doesrange || eap->skip)
3498 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003501 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003502 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003503 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (aborting())
3505 break;
3506 }
3507 if (eap->skip)
3508 --emsg_skip;
3509
3510 if (!failed)
3511 {
3512 /* Check for trailing illegal characters and a following command. */
3513 if (!ends_excmd(*arg))
3514 {
3515 emsg_severe = TRUE;
3516 EMSG(_(e_trailing));
3517 }
3518 else
3519 eap->nextcmd = check_nextcmd(arg);
3520 }
3521
3522end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003523 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003524 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525}
3526
3527/*
3528 * ":unlet[!] var1 ... " command.
3529 */
3530 void
3531ex_unlet(eap)
3532 exarg_T *eap;
3533{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003534 ex_unletlock(eap, eap->arg, 0);
3535}
3536
3537/*
3538 * ":lockvar" and ":unlockvar" commands
3539 */
3540 void
3541ex_lockvar(eap)
3542 exarg_T *eap;
3543{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003545 int deep = 2;
3546
3547 if (eap->forceit)
3548 deep = -1;
3549 else if (vim_isdigit(*arg))
3550 {
3551 deep = getdigits(&arg);
3552 arg = skipwhite(arg);
3553 }
3554
3555 ex_unletlock(eap, arg, deep);
3556}
3557
3558/*
3559 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3560 */
3561 static void
3562ex_unletlock(eap, argstart, deep)
3563 exarg_T *eap;
3564 char_u *argstart;
3565 int deep;
3566{
3567 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003570 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
3572 do
3573 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003574 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003575 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003576 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003577 if (lv.ll_name == NULL)
3578 error = TRUE; /* error but continue parsing */
3579 if (name_end == NULL || (!vim_iswhite(*name_end)
3580 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003582 if (name_end != NULL)
3583 {
3584 emsg_severe = TRUE;
3585 EMSG(_(e_trailing));
3586 }
3587 if (!(eap->skip || error))
3588 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 break;
3590 }
3591
3592 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593 {
3594 if (eap->cmdidx == CMD_unlet)
3595 {
3596 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3597 error = TRUE;
3598 }
3599 else
3600 {
3601 if (do_lock_var(&lv, name_end, deep,
3602 eap->cmdidx == CMD_lockvar) == FAIL)
3603 error = TRUE;
3604 }
3605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607 if (!eap->skip)
3608 clear_lval(&lv);
3609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 arg = skipwhite(name_end);
3611 } while (!ends_excmd(*arg));
3612
3613 eap->nextcmd = check_nextcmd(arg);
3614}
3615
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003618 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620 int forceit;
3621{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 int ret = OK;
3623 int cc;
3624
3625 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 cc = *name_end;
3628 *name_end = NUL;
3629
3630 /* Normal name or expanded name. */
3631 if (check_changedtick(lp->ll_name))
3632 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003633 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003636 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3638 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 else if (lp->ll_range)
3640 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003641 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642
3643 /* Delete a range of List items. */
3644 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3645 {
3646 li = lp->ll_li->li_next;
3647 listitem_remove(lp->ll_list, lp->ll_li);
3648 lp->ll_li = li;
3649 ++lp->ll_n1;
3650 }
3651 }
3652 else
3653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003655 /* unlet a List item. */
3656 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003659 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 }
3661
3662 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663}
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665/*
3666 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 */
3669 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
Bram Moolenaar33570922005-01-25 22:26:29 +00003674 hashtab_T *ht;
3675 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003676 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003677 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 ht = find_var_ht(name, &varname);
3680 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 hi = hash_find(ht, varname);
3683 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003684 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003685 di = HI2DI(hi);
3686 if (var_check_fixed(di->di_flags, name)
3687 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003688 return FAIL;
3689 delete_var(ht, hi);
3690 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003693 if (forceit)
3694 return OK;
3695 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 return FAIL;
3697}
3698
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003699/*
3700 * Lock or unlock variable indicated by "lp".
3701 * "deep" is the levels to go (-1 for unlimited);
3702 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3703 */
3704 static int
3705do_lock_var(lp, name_end, deep, lock)
3706 lval_T *lp;
3707 char_u *name_end;
3708 int deep;
3709 int lock;
3710{
3711 int ret = OK;
3712 int cc;
3713 dictitem_T *di;
3714
3715 if (deep == 0) /* nothing to do */
3716 return OK;
3717
3718 if (lp->ll_tv == NULL)
3719 {
3720 cc = *name_end;
3721 *name_end = NUL;
3722
3723 /* Normal name or expanded name. */
3724 if (check_changedtick(lp->ll_name))
3725 ret = FAIL;
3726 else
3727 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003728 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003729 if (di == NULL)
3730 ret = FAIL;
3731 else
3732 {
3733 if (lock)
3734 di->di_flags |= DI_FLAGS_LOCK;
3735 else
3736 di->di_flags &= ~DI_FLAGS_LOCK;
3737 item_lock(&di->di_tv, deep, lock);
3738 }
3739 }
3740 *name_end = cc;
3741 }
3742 else if (lp->ll_range)
3743 {
3744 listitem_T *li = lp->ll_li;
3745
3746 /* (un)lock a range of List items. */
3747 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3748 {
3749 item_lock(&li->li_tv, deep, lock);
3750 li = li->li_next;
3751 ++lp->ll_n1;
3752 }
3753 }
3754 else if (lp->ll_list != NULL)
3755 /* (un)lock a List item. */
3756 item_lock(&lp->ll_li->li_tv, deep, lock);
3757 else
3758 /* un(lock) a Dictionary item. */
3759 item_lock(&lp->ll_di->di_tv, deep, lock);
3760
3761 return ret;
3762}
3763
3764/*
3765 * Lock or unlock an item. "deep" is nr of levels to go.
3766 */
3767 static void
3768item_lock(tv, deep, lock)
3769 typval_T *tv;
3770 int deep;
3771 int lock;
3772{
3773 static int recurse = 0;
3774 list_T *l;
3775 listitem_T *li;
3776 dict_T *d;
3777 hashitem_T *hi;
3778 int todo;
3779
3780 if (recurse >= DICT_MAXNEST)
3781 {
3782 EMSG(_("E743: variable nested too deep for (un)lock"));
3783 return;
3784 }
3785 if (deep == 0)
3786 return;
3787 ++recurse;
3788
3789 /* lock/unlock the item itself */
3790 if (lock)
3791 tv->v_lock |= VAR_LOCKED;
3792 else
3793 tv->v_lock &= ~VAR_LOCKED;
3794
3795 switch (tv->v_type)
3796 {
3797 case VAR_LIST:
3798 if ((l = tv->vval.v_list) != NULL)
3799 {
3800 if (lock)
3801 l->lv_lock |= VAR_LOCKED;
3802 else
3803 l->lv_lock &= ~VAR_LOCKED;
3804 if (deep < 0 || deep > 1)
3805 /* recursive: lock/unlock the items the List contains */
3806 for (li = l->lv_first; li != NULL; li = li->li_next)
3807 item_lock(&li->li_tv, deep - 1, lock);
3808 }
3809 break;
3810 case VAR_DICT:
3811 if ((d = tv->vval.v_dict) != NULL)
3812 {
3813 if (lock)
3814 d->dv_lock |= VAR_LOCKED;
3815 else
3816 d->dv_lock &= ~VAR_LOCKED;
3817 if (deep < 0 || deep > 1)
3818 {
3819 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003820 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003821 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3822 {
3823 if (!HASHITEM_EMPTY(hi))
3824 {
3825 --todo;
3826 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3827 }
3828 }
3829 }
3830 }
3831 }
3832 --recurse;
3833}
3834
Bram Moolenaara40058a2005-07-11 22:42:07 +00003835/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003836 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3837 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003838 */
3839 static int
3840tv_islocked(tv)
3841 typval_T *tv;
3842{
3843 return (tv->v_lock & VAR_LOCKED)
3844 || (tv->v_type == VAR_LIST
3845 && tv->vval.v_list != NULL
3846 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3847 || (tv->v_type == VAR_DICT
3848 && tv->vval.v_dict != NULL
3849 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3850}
3851
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3853/*
3854 * Delete all "menutrans_" variables.
3855 */
3856 void
3857del_menutrans_vars()
3858{
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003860 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
Bram Moolenaar33570922005-01-25 22:26:29 +00003862 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003863 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003864 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003865 {
3866 if (!HASHITEM_EMPTY(hi))
3867 {
3868 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3870 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003871 }
3872 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003873 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874}
3875#endif
3876
3877#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3878
3879/*
3880 * Local string buffer for the next two functions to store a variable name
3881 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3882 * get_user_var_name().
3883 */
3884
3885static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3886
3887static char_u *varnamebuf = NULL;
3888static int varnamebuflen = 0;
3889
3890/*
3891 * Function to concatenate a prefix and a variable name.
3892 */
3893 static char_u *
3894cat_prefix_varname(prefix, name)
3895 int prefix;
3896 char_u *name;
3897{
3898 int len;
3899
3900 len = (int)STRLEN(name) + 3;
3901 if (len > varnamebuflen)
3902 {
3903 vim_free(varnamebuf);
3904 len += 10; /* some additional space */
3905 varnamebuf = alloc(len);
3906 if (varnamebuf == NULL)
3907 {
3908 varnamebuflen = 0;
3909 return NULL;
3910 }
3911 varnamebuflen = len;
3912 }
3913 *varnamebuf = prefix;
3914 varnamebuf[1] = ':';
3915 STRCPY(varnamebuf + 2, name);
3916 return varnamebuf;
3917}
3918
3919/*
3920 * Function given to ExpandGeneric() to obtain the list of user defined
3921 * (global/buffer/window/built-in) variable names.
3922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 char_u *
3924get_user_var_name(xp, idx)
3925 expand_T *xp;
3926 int idx;
3927{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003928 static long_u gdone;
3929 static long_u bdone;
3930 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931#ifdef FEAT_WINDOWS
3932 static long_u tdone;
3933#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003934 static int vidx;
3935 static hashitem_T *hi;
3936 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003939 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003941#ifdef FEAT_WINDOWS
3942 tdone = 0;
3943#endif
3944 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003945
3946 /* Global variables */
3947 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003949 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003951 else
3952 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 while (HASHITEM_EMPTY(hi))
3954 ++hi;
3955 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3956 return cat_prefix_varname('g', hi->hi_key);
3957 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003959
3960 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003961 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003964 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003965 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003966 else
3967 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003968 while (HASHITEM_EMPTY(hi))
3969 ++hi;
3970 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003972 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003974 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 return (char_u *)"b:changedtick";
3976 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003977
3978 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003979 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003982 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003983 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003984 else
3985 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003986 while (HASHITEM_EMPTY(hi))
3987 ++hi;
3988 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003990
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003991#ifdef FEAT_WINDOWS
3992 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003993 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003994 if (tdone < ht->ht_used)
3995 {
3996 if (tdone++ == 0)
3997 hi = ht->ht_array;
3998 else
3999 ++hi;
4000 while (HASHITEM_EMPTY(hi))
4001 ++hi;
4002 return cat_prefix_varname('t', hi->hi_key);
4003 }
4004#endif
4005
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 /* v: variables */
4007 if (vidx < VV_LEN)
4008 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 vim_free(varnamebuf);
4011 varnamebuf = NULL;
4012 varnamebuflen = 0;
4013 return NULL;
4014}
4015
4016#endif /* FEAT_CMDL_COMPL */
4017
4018/*
4019 * types for expressions.
4020 */
4021typedef enum
4022{
4023 TYPE_UNKNOWN = 0
4024 , TYPE_EQUAL /* == */
4025 , TYPE_NEQUAL /* != */
4026 , TYPE_GREATER /* > */
4027 , TYPE_GEQUAL /* >= */
4028 , TYPE_SMALLER /* < */
4029 , TYPE_SEQUAL /* <= */
4030 , TYPE_MATCH /* =~ */
4031 , TYPE_NOMATCH /* !~ */
4032} exptype_T;
4033
4034/*
4035 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004036 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4038 */
4039
4040/*
4041 * Handle zero level expression.
4042 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004044 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 * Return OK or FAIL.
4046 */
4047 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 char_u **nextcmd;
4052 int evaluate;
4053{
4054 int ret;
4055 char_u *p;
4056
4057 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (ret == FAIL || !ends_excmd(*p))
4060 {
4061 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004062 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 /*
4064 * Report the invalid expression unless the expression evaluation has
4065 * been cancelled due to an aborting error, an interrupt, or an
4066 * exception.
4067 */
4068 if (!aborting())
4069 EMSG2(_(e_invexpr2), arg);
4070 ret = FAIL;
4071 }
4072 if (nextcmd != NULL)
4073 *nextcmd = check_nextcmd(p);
4074
4075 return ret;
4076}
4077
4078/*
4079 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004080 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 *
4082 * "arg" must point to the first non-white of the expression.
4083 * "arg" is advanced to the next non-white after the recognized expression.
4084 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004085 * Note: "rettv.v_lock" is not set.
4086 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 * Return OK or FAIL.
4088 */
4089 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 int evaluate;
4094{
4095 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004096 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098 /*
4099 * Get the first variable.
4100 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 return FAIL;
4103
4104 if ((*arg)[0] == '?')
4105 {
4106 result = FALSE;
4107 if (evaluate)
4108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004109 int error = FALSE;
4110
4111 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114 if (error)
4115 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117
4118 /*
4119 * Get the second variable.
4120 */
4121 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124
4125 /*
4126 * Check for the ":".
4127 */
4128 if ((*arg)[0] != ':')
4129 {
4130 EMSG(_("E109: Missing ':' after '?'"));
4131 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 return FAIL;
4134 }
4135
4136 /*
4137 * Get the third variable.
4138 */
4139 *arg = skipwhite(*arg + 1);
4140 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4141 {
4142 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 return FAIL;
4145 }
4146 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
4149
4150 return OK;
4151}
4152
4153/*
4154 * Handle first level expression:
4155 * expr2 || expr2 || expr2 logical OR
4156 *
4157 * "arg" must point to the first non-white of the expression.
4158 * "arg" is advanced to the next non-white after the recognized expression.
4159 *
4160 * Return OK or FAIL.
4161 */
4162 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004163eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 int evaluate;
4167{
Bram Moolenaar33570922005-01-25 22:26:29 +00004168 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 long result;
4170 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 /*
4174 * Get the first variable.
4175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178
4179 /*
4180 * Repeat until there is no following "||".
4181 */
4182 first = TRUE;
4183 result = FALSE;
4184 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4185 {
4186 if (evaluate && first)
4187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191 if (error)
4192 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 first = FALSE;
4194 }
4195
4196 /*
4197 * Get the second variable.
4198 */
4199 *arg = skipwhite(*arg + 2);
4200 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4201 return FAIL;
4202
4203 /*
4204 * Compute the result.
4205 */
4206 if (evaluate && !result)
4207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004208 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004211 if (error)
4212 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214 if (evaluate)
4215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 rettv->v_type = VAR_NUMBER;
4217 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219 }
4220
4221 return OK;
4222}
4223
4224/*
4225 * Handle second level expression:
4226 * expr3 && expr3 && expr3 logical AND
4227 *
4228 * "arg" must point to the first non-white of the expression.
4229 * "arg" is advanced to the next non-white after the recognized expression.
4230 *
4231 * Return OK or FAIL.
4232 */
4233 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 int evaluate;
4238{
Bram Moolenaar33570922005-01-25 22:26:29 +00004239 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 long result;
4241 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 /*
4245 * Get the first variable.
4246 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 return FAIL;
4249
4250 /*
4251 * Repeat until there is no following "&&".
4252 */
4253 first = TRUE;
4254 result = TRUE;
4255 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4256 {
4257 if (evaluate && first)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 if (error)
4263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 first = FALSE;
4265 }
4266
4267 /*
4268 * Get the second variable.
4269 */
4270 *arg = skipwhite(*arg + 2);
4271 if (eval4(arg, &var2, evaluate && result) == FAIL)
4272 return FAIL;
4273
4274 /*
4275 * Compute the result.
4276 */
4277 if (evaluate && result)
4278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 if (error)
4283 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285 if (evaluate)
4286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 rettv->v_type = VAR_NUMBER;
4288 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 }
4291
4292 return OK;
4293}
4294
4295/*
4296 * Handle third level expression:
4297 * var1 == var2
4298 * var1 =~ var2
4299 * var1 != var2
4300 * var1 !~ var2
4301 * var1 > var2
4302 * var1 >= var2
4303 * var1 < var2
4304 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004305 * var1 is var2
4306 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 *
4308 * "arg" must point to the first non-white of the expression.
4309 * "arg" is advanced to the next non-white after the recognized expression.
4310 *
4311 * Return OK or FAIL.
4312 */
4313 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 int evaluate;
4318{
Bram Moolenaar33570922005-01-25 22:26:29 +00004319 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 char_u *p;
4321 int i;
4322 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004323 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 int len = 2;
4325 long n1, n2;
4326 char_u *s1, *s2;
4327 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4328 regmatch_T regmatch;
4329 int ic;
4330 char_u *save_cpo;
4331
4332 /*
4333 * Get the first variable.
4334 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 return FAIL;
4337
4338 p = *arg;
4339 switch (p[0])
4340 {
4341 case '=': if (p[1] == '=')
4342 type = TYPE_EQUAL;
4343 else if (p[1] == '~')
4344 type = TYPE_MATCH;
4345 break;
4346 case '!': if (p[1] == '=')
4347 type = TYPE_NEQUAL;
4348 else if (p[1] == '~')
4349 type = TYPE_NOMATCH;
4350 break;
4351 case '>': if (p[1] != '=')
4352 {
4353 type = TYPE_GREATER;
4354 len = 1;
4355 }
4356 else
4357 type = TYPE_GEQUAL;
4358 break;
4359 case '<': if (p[1] != '=')
4360 {
4361 type = TYPE_SMALLER;
4362 len = 1;
4363 }
4364 else
4365 type = TYPE_SEQUAL;
4366 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004367 case 'i': if (p[1] == 's')
4368 {
4369 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4370 len = 5;
4371 if (!vim_isIDc(p[len]))
4372 {
4373 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4374 type_is = TRUE;
4375 }
4376 }
4377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379
4380 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004381 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 */
4383 if (type != TYPE_UNKNOWN)
4384 {
4385 /* extra question mark appended: ignore case */
4386 if (p[len] == '?')
4387 {
4388 ic = TRUE;
4389 ++len;
4390 }
4391 /* extra '#' appended: match case */
4392 else if (p[len] == '#')
4393 {
4394 ic = FALSE;
4395 ++len;
4396 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004397 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 else
4399 ic = p_ic;
4400
4401 /*
4402 * Get the second variable.
4403 */
4404 *arg = skipwhite(p + len);
4405 if (eval5(arg, &var2, evaluate) == FAIL)
4406 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004407 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 return FAIL;
4409 }
4410
4411 if (evaluate)
4412 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004413 if (type_is && rettv->v_type != var2.v_type)
4414 {
4415 /* For "is" a different type always means FALSE, for "notis"
4416 * it means TRUE. */
4417 n1 = (type == TYPE_NEQUAL);
4418 }
4419 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4420 {
4421 if (type_is)
4422 {
4423 n1 = (rettv->v_type == var2.v_type
4424 && rettv->vval.v_list == var2.vval.v_list);
4425 if (type == TYPE_NEQUAL)
4426 n1 = !n1;
4427 }
4428 else if (rettv->v_type != var2.v_type
4429 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4430 {
4431 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004432 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004434 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 clear_tv(rettv);
4436 clear_tv(&var2);
4437 return FAIL;
4438 }
4439 else
4440 {
4441 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004442 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4443 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004444 if (type == TYPE_NEQUAL)
4445 n1 = !n1;
4446 }
4447 }
4448
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004449 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4450 {
4451 if (type_is)
4452 {
4453 n1 = (rettv->v_type == var2.v_type
4454 && rettv->vval.v_dict == var2.vval.v_dict);
4455 if (type == TYPE_NEQUAL)
4456 n1 = !n1;
4457 }
4458 else if (rettv->v_type != var2.v_type
4459 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4460 {
4461 if (rettv->v_type != var2.v_type)
4462 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4463 else
4464 EMSG(_("E736: Invalid operation for Dictionary"));
4465 clear_tv(rettv);
4466 clear_tv(&var2);
4467 return FAIL;
4468 }
4469 else
4470 {
4471 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004472 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4473 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004474 if (type == TYPE_NEQUAL)
4475 n1 = !n1;
4476 }
4477 }
4478
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4480 {
4481 if (rettv->v_type != var2.v_type
4482 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4483 {
4484 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004485 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004487 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004488 clear_tv(rettv);
4489 clear_tv(&var2);
4490 return FAIL;
4491 }
4492 else
4493 {
4494 /* Compare two Funcrefs for being equal or unequal. */
4495 if (rettv->vval.v_string == NULL
4496 || var2.vval.v_string == NULL)
4497 n1 = FALSE;
4498 else
4499 n1 = STRCMP(rettv->vval.v_string,
4500 var2.vval.v_string) == 0;
4501 if (type == TYPE_NEQUAL)
4502 n1 = !n1;
4503 }
4504 }
4505
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004506#ifdef FEAT_FLOAT
4507 /*
4508 * If one of the two variables is a float, compare as a float.
4509 * When using "=~" or "!~", always compare as string.
4510 */
4511 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4512 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4513 {
4514 float_T f1, f2;
4515
4516 if (rettv->v_type == VAR_FLOAT)
4517 f1 = rettv->vval.v_float;
4518 else
4519 f1 = get_tv_number(rettv);
4520 if (var2.v_type == VAR_FLOAT)
4521 f2 = var2.vval.v_float;
4522 else
4523 f2 = get_tv_number(&var2);
4524 n1 = FALSE;
4525 switch (type)
4526 {
4527 case TYPE_EQUAL: n1 = (f1 == f2); break;
4528 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4529 case TYPE_GREATER: n1 = (f1 > f2); break;
4530 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4531 case TYPE_SMALLER: n1 = (f1 < f2); break;
4532 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4533 case TYPE_UNKNOWN:
4534 case TYPE_MATCH:
4535 case TYPE_NOMATCH: break; /* avoid gcc warning */
4536 }
4537 }
4538#endif
4539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 /*
4541 * If one of the two variables is a number, compare as a number.
4542 * When using "=~" or "!~", always compare as string.
4543 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004544 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004547 n1 = get_tv_number(rettv);
4548 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 switch (type)
4550 {
4551 case TYPE_EQUAL: n1 = (n1 == n2); break;
4552 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4553 case TYPE_GREATER: n1 = (n1 > n2); break;
4554 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4555 case TYPE_SMALLER: n1 = (n1 < n2); break;
4556 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4557 case TYPE_UNKNOWN:
4558 case TYPE_MATCH:
4559 case TYPE_NOMATCH: break; /* avoid gcc warning */
4560 }
4561 }
4562 else
4563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004564 s1 = get_tv_string_buf(rettv, buf1);
4565 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4567 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4568 else
4569 i = 0;
4570 n1 = FALSE;
4571 switch (type)
4572 {
4573 case TYPE_EQUAL: n1 = (i == 0); break;
4574 case TYPE_NEQUAL: n1 = (i != 0); break;
4575 case TYPE_GREATER: n1 = (i > 0); break;
4576 case TYPE_GEQUAL: n1 = (i >= 0); break;
4577 case TYPE_SMALLER: n1 = (i < 0); break;
4578 case TYPE_SEQUAL: n1 = (i <= 0); break;
4579
4580 case TYPE_MATCH:
4581 case TYPE_NOMATCH:
4582 /* avoid 'l' flag in 'cpoptions' */
4583 save_cpo = p_cpo;
4584 p_cpo = (char_u *)"";
4585 regmatch.regprog = vim_regcomp(s2,
4586 RE_MAGIC + RE_STRING);
4587 regmatch.rm_ic = ic;
4588 if (regmatch.regprog != NULL)
4589 {
4590 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004591 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 if (type == TYPE_NOMATCH)
4593 n1 = !n1;
4594 }
4595 p_cpo = save_cpo;
4596 break;
4597
4598 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4599 }
4600 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004601 clear_tv(rettv);
4602 clear_tv(&var2);
4603 rettv->v_type = VAR_NUMBER;
4604 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
4606 }
4607
4608 return OK;
4609}
4610
4611/*
4612 * Handle fourth level expression:
4613 * + number addition
4614 * - number subtraction
4615 * . string concatenation
4616 *
4617 * "arg" must point to the first non-white of the expression.
4618 * "arg" is advanced to the next non-white after the recognized expression.
4619 *
4620 * Return OK or FAIL.
4621 */
4622 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004623eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 int evaluate;
4627{
Bram Moolenaar33570922005-01-25 22:26:29 +00004628 typval_T var2;
4629 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 int op;
4631 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004632#ifdef FEAT_FLOAT
4633 float_T f1 = 0, f2 = 0;
4634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 char_u *s1, *s2;
4636 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4637 char_u *p;
4638
4639 /*
4640 * Get the first variable.
4641 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004642 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 return FAIL;
4644
4645 /*
4646 * Repeat computing, until no '+', '-' or '.' is following.
4647 */
4648 for (;;)
4649 {
4650 op = **arg;
4651 if (op != '+' && op != '-' && op != '.')
4652 break;
4653
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004654 if ((op != '+' || rettv->v_type != VAR_LIST)
4655#ifdef FEAT_FLOAT
4656 && (op == '.' || rettv->v_type != VAR_FLOAT)
4657#endif
4658 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004659 {
4660 /* For "list + ...", an illegal use of the first operand as
4661 * a number cannot be determined before evaluating the 2nd
4662 * operand: if this is also a list, all is ok.
4663 * For "something . ...", "something - ..." or "non-list + ...",
4664 * we know that the first operand needs to be a string or number
4665 * without evaluating the 2nd operand. So check before to avoid
4666 * side effects after an error. */
4667 if (evaluate && get_tv_string_chk(rettv) == NULL)
4668 {
4669 clear_tv(rettv);
4670 return FAIL;
4671 }
4672 }
4673
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 /*
4675 * Get the second variable.
4676 */
4677 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004678 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004680 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 return FAIL;
4682 }
4683
4684 if (evaluate)
4685 {
4686 /*
4687 * Compute the result.
4688 */
4689 if (op == '.')
4690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004691 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4692 s2 = get_tv_string_buf_chk(&var2, buf2);
4693 if (s2 == NULL) /* type error ? */
4694 {
4695 clear_tv(rettv);
4696 clear_tv(&var2);
4697 return FAIL;
4698 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004699 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004700 clear_tv(rettv);
4701 rettv->v_type = VAR_STRING;
4702 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004704 else if (op == '+' && rettv->v_type == VAR_LIST
4705 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004706 {
4707 /* concatenate Lists */
4708 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4709 &var3) == FAIL)
4710 {
4711 clear_tv(rettv);
4712 clear_tv(&var2);
4713 return FAIL;
4714 }
4715 clear_tv(rettv);
4716 *rettv = var3;
4717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 else
4719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 int error = FALSE;
4721
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722#ifdef FEAT_FLOAT
4723 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004725 f1 = rettv->vval.v_float;
4726 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004727 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728 else
4729#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004730 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004731 n1 = get_tv_number_chk(rettv, &error);
4732 if (error)
4733 {
4734 /* This can only happen for "list + non-list". For
4735 * "non-list + ..." or "something - ...", we returned
4736 * before evaluating the 2nd operand. */
4737 clear_tv(rettv);
4738 return FAIL;
4739 }
4740#ifdef FEAT_FLOAT
4741 if (var2.v_type == VAR_FLOAT)
4742 f1 = n1;
4743#endif
4744 }
4745#ifdef FEAT_FLOAT
4746 if (var2.v_type == VAR_FLOAT)
4747 {
4748 f2 = var2.vval.v_float;
4749 n2 = 0;
4750 }
4751 else
4752#endif
4753 {
4754 n2 = get_tv_number_chk(&var2, &error);
4755 if (error)
4756 {
4757 clear_tv(rettv);
4758 clear_tv(&var2);
4759 return FAIL;
4760 }
4761#ifdef FEAT_FLOAT
4762 if (rettv->v_type == VAR_FLOAT)
4763 f2 = n2;
4764#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004767
4768#ifdef FEAT_FLOAT
4769 /* If there is a float on either side the result is a float. */
4770 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4771 {
4772 if (op == '+')
4773 f1 = f1 + f2;
4774 else
4775 f1 = f1 - f2;
4776 rettv->v_type = VAR_FLOAT;
4777 rettv->vval.v_float = f1;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780#endif
4781 {
4782 if (op == '+')
4783 n1 = n1 + n2;
4784 else
4785 n1 = n1 - n2;
4786 rettv->v_type = VAR_NUMBER;
4787 rettv->vval.v_number = n1;
4788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004790 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 }
4793 return OK;
4794}
4795
4796/*
4797 * Handle fifth level expression:
4798 * * number multiplication
4799 * / number division
4800 * % number modulo
4801 *
4802 * "arg" must point to the first non-white of the expression.
4803 * "arg" is advanced to the next non-white after the recognized expression.
4804 *
4805 * Return OK or FAIL.
4806 */
4807 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004808eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004812 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
Bram Moolenaar33570922005-01-25 22:26:29 +00004814 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 int op;
4816 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817#ifdef FEAT_FLOAT
4818 int use_float = FALSE;
4819 float_T f1 = 0, f2;
4820#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004821 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822
4823 /*
4824 * Get the first variable.
4825 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004826 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 return FAIL;
4828
4829 /*
4830 * Repeat computing, until no '*', '/' or '%' is following.
4831 */
4832 for (;;)
4833 {
4834 op = **arg;
4835 if (op != '*' && op != '/' && op != '%')
4836 break;
4837
4838 if (evaluate)
4839 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840#ifdef FEAT_FLOAT
4841 if (rettv->v_type == VAR_FLOAT)
4842 {
4843 f1 = rettv->vval.v_float;
4844 use_float = TRUE;
4845 n1 = 0;
4846 }
4847 else
4848#endif
4849 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004850 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004851 if (error)
4852 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 else
4855 n1 = 0;
4856
4857 /*
4858 * Get the second variable.
4859 */
4860 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004861 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 return FAIL;
4863
4864 if (evaluate)
4865 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866#ifdef FEAT_FLOAT
4867 if (var2.v_type == VAR_FLOAT)
4868 {
4869 if (!use_float)
4870 {
4871 f1 = n1;
4872 use_float = TRUE;
4873 }
4874 f2 = var2.vval.v_float;
4875 n2 = 0;
4876 }
4877 else
4878#endif
4879 {
4880 n2 = get_tv_number_chk(&var2, &error);
4881 clear_tv(&var2);
4882 if (error)
4883 return FAIL;
4884#ifdef FEAT_FLOAT
4885 if (use_float)
4886 f2 = n2;
4887#endif
4888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 /*
4891 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004892 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894#ifdef FEAT_FLOAT
4895 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 if (op == '*')
4898 f1 = f1 * f2;
4899 else if (op == '/')
4900 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004901# ifdef VMS
4902 /* VMS crashes on divide by zero, work around it */
4903 if (f2 == 0.0)
4904 {
4905 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004906 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004907 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004908 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004909 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004910 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004911 }
4912 else
4913 f1 = f1 / f2;
4914# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915 /* We rely on the floating point library to handle divide
4916 * by zero to result in "inf" and not a crash. */
4917 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004918# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004922 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004923 return FAIL;
4924 }
4925 rettv->v_type = VAR_FLOAT;
4926 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931 if (op == '*')
4932 n1 = n1 * n2;
4933 else if (op == '/')
4934 {
4935 if (n2 == 0) /* give an error message? */
4936 {
4937 if (n1 == 0)
4938 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4939 else if (n1 < 0)
4940 n1 = -0x7fffffffL;
4941 else
4942 n1 = 0x7fffffffL;
4943 }
4944 else
4945 n1 = n1 / n2;
4946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004948 {
4949 if (n2 == 0) /* give an error message? */
4950 n1 = 0;
4951 else
4952 n1 = n1 % n2;
4953 }
4954 rettv->v_type = VAR_NUMBER;
4955 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
4958 }
4959
4960 return OK;
4961}
4962
4963/*
4964 * Handle sixth level expression:
4965 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004966 * "string" string constant
4967 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 * &option-name option value
4969 * @r register contents
4970 * identifier variable value
4971 * function() function call
4972 * $VAR environment variable
4973 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004974 * [expr, expr] List
4975 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 *
4977 * Also handle:
4978 * ! in front logical NOT
4979 * - in front unary minus
4980 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981 * trailing [] subscript in String or List
4982 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 *
4984 * "arg" must point to the first non-white of the expression.
4985 * "arg" is advanced to the next non-white after the recognized expression.
4986 *
4987 * Return OK or FAIL.
4988 */
4989 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004990eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004994 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 long n;
4997 int len;
4998 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 char_u *start_leader, *end_leader;
5000 int ret = OK;
5001 char_u *alias;
5002
5003 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005007 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
5009 /*
5010 * Skip '!' and '-' characters. They are handled later.
5011 */
5012 start_leader = *arg;
5013 while (**arg == '!' || **arg == '-' || **arg == '+')
5014 *arg = skipwhite(*arg + 1);
5015 end_leader = *arg;
5016
5017 switch (**arg)
5018 {
5019 /*
5020 * Number constant.
5021 */
5022 case '0':
5023 case '1':
5024 case '2':
5025 case '3':
5026 case '4':
5027 case '5':
5028 case '6':
5029 case '7':
5030 case '8':
5031 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005032 {
5033#ifdef FEAT_FLOAT
5034 char_u *p = skipdigits(*arg + 1);
5035 int get_float = FALSE;
5036
5037 /* We accept a float when the format matches
5038 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005039 * strict to avoid backwards compatibility problems.
5040 * Don't look for a float after the "." operator, so that
5041 * ":let vers = 1.2.3" doesn't fail. */
5042 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005044 get_float = TRUE;
5045 p = skipdigits(p + 2);
5046 if (*p == 'e' || *p == 'E')
5047 {
5048 ++p;
5049 if (*p == '-' || *p == '+')
5050 ++p;
5051 if (!vim_isdigit(*p))
5052 get_float = FALSE;
5053 else
5054 p = skipdigits(p + 1);
5055 }
5056 if (ASCII_ISALPHA(*p) || *p == '.')
5057 get_float = FALSE;
5058 }
5059 if (get_float)
5060 {
5061 float_T f;
5062
5063 *arg += string2float(*arg, &f);
5064 if (evaluate)
5065 {
5066 rettv->v_type = VAR_FLOAT;
5067 rettv->vval.v_float = f;
5068 }
5069 }
5070 else
5071#endif
5072 {
5073 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5074 *arg += len;
5075 if (evaluate)
5076 {
5077 rettv->v_type = VAR_NUMBER;
5078 rettv->vval.v_number = n;
5079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
5084 /*
5085 * String constant: "string".
5086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 break;
5089
5090 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005091 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005094 break;
5095
5096 /*
5097 * List: [expr, expr]
5098 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005099 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 break;
5101
5102 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 * Dictionary: {key: val, key: val}
5104 */
5105 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5106 break;
5107
5108 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005109 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005111 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 break;
5113
5114 /*
5115 * Environment variable: $VAR.
5116 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 break;
5119
5120 /*
5121 * Register contents: @r.
5122 */
5123 case '@': ++*arg;
5124 if (evaluate)
5125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005126 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005127 rettv->vval.v_string = get_reg_contents(**arg,
5128 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130 if (**arg != NUL)
5131 ++*arg;
5132 break;
5133
5134 /*
5135 * nested expression: (expression).
5136 */
5137 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005138 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 if (**arg == ')')
5140 ++*arg;
5141 else if (ret == OK)
5142 {
5143 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 ret = FAIL;
5146 }
5147 break;
5148
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 break;
5151 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152
5153 if (ret == NOTDONE)
5154 {
5155 /*
5156 * Must be a variable or function name.
5157 * Can also be a curly-braces kind of name: {expr}.
5158 */
5159 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005160 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 if (alias != NULL)
5162 s = alias;
5163
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005164 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 ret = FAIL;
5166 else
5167 {
5168 if (**arg == '(') /* recursive! */
5169 {
5170 /* If "s" is the name of a variable of type VAR_FUNC
5171 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005172 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173
5174 /* Invoke the function. */
5175 ret = get_func_tv(s, len, rettv, arg,
5176 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005177 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005178
5179 /* If evaluate is FALSE rettv->v_type was not set in
5180 * get_func_tv, but it's needed in handle_subscript() to parse
5181 * what follows. So set it here. */
5182 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5183 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005184 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005185 rettv->v_type = VAR_FUNC;
5186 }
5187
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 /* Stop the expression evaluation when immediately
5189 * aborting on error, or when an interrupt occurred or
5190 * an exception was thrown but not caught. */
5191 if (aborting())
5192 {
5193 if (ret == OK)
5194 clear_tv(rettv);
5195 ret = FAIL;
5196 }
5197 }
5198 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005199 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005200 else
5201 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005203 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 }
5205
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 *arg = skipwhite(*arg);
5207
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005208 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5209 * expr(expr). */
5210 if (ret == OK)
5211 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212
5213 /*
5214 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5215 */
5216 if (ret == OK && evaluate && end_leader > start_leader)
5217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005219 int val = 0;
5220#ifdef FEAT_FLOAT
5221 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005223 if (rettv->v_type == VAR_FLOAT)
5224 f = rettv->vval.v_float;
5225 else
5226#endif
5227 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005230 clear_tv(rettv);
5231 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005233 else
5234 {
5235 while (end_leader > start_leader)
5236 {
5237 --end_leader;
5238 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005239 {
5240#ifdef FEAT_FLOAT
5241 if (rettv->v_type == VAR_FLOAT)
5242 f = !f;
5243 else
5244#endif
5245 val = !val;
5246 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005247 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005248 {
5249#ifdef FEAT_FLOAT
5250 if (rettv->v_type == VAR_FLOAT)
5251 f = -f;
5252 else
5253#endif
5254 val = -val;
5255 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005256 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005257#ifdef FEAT_FLOAT
5258 if (rettv->v_type == VAR_FLOAT)
5259 {
5260 clear_tv(rettv);
5261 rettv->vval.v_float = f;
5262 }
5263 else
5264#endif
5265 {
5266 clear_tv(rettv);
5267 rettv->v_type = VAR_NUMBER;
5268 rettv->vval.v_number = val;
5269 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
5272
5273 return ret;
5274}
5275
5276/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005277 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5278 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5280 */
5281 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005282eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005284 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005286 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287{
5288 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005289 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 long len = -1;
5292 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005296 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005298 if (verbose)
5299 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 return FAIL;
5301 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005302#ifdef FEAT_FLOAT
5303 else if (rettv->v_type == VAR_FLOAT)
5304 {
5305 if (verbose)
5306 EMSG(_(e_float_as_string));
5307 return FAIL;
5308 }
5309#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313 /*
5314 * dict.name
5315 */
5316 key = *arg + 1;
5317 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5318 ;
5319 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 }
5323 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005325 /*
5326 * something[idx]
5327 *
5328 * Get the (first) variable from inside the [].
5329 */
5330 *arg = skipwhite(*arg + 1);
5331 if (**arg == ':')
5332 empty1 = TRUE;
5333 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5334 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5336 {
5337 /* not a number or string */
5338 clear_tv(&var1);
5339 return FAIL;
5340 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341
5342 /*
5343 * Get the second variable from inside the [:].
5344 */
5345 if (**arg == ':')
5346 {
5347 range = TRUE;
5348 *arg = skipwhite(*arg + 1);
5349 if (**arg == ']')
5350 empty2 = TRUE;
5351 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005353 if (!empty1)
5354 clear_tv(&var1);
5355 return FAIL;
5356 }
5357 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5358 {
5359 /* not a number or string */
5360 if (!empty1)
5361 clear_tv(&var1);
5362 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 return FAIL;
5364 }
5365 }
5366
5367 /* Check for the ']'. */
5368 if (**arg != ']')
5369 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005370 if (verbose)
5371 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 clear_tv(&var1);
5373 if (range)
5374 clear_tv(&var2);
5375 return FAIL;
5376 }
5377 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 }
5379
5380 if (evaluate)
5381 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 n1 = 0;
5383 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005385 n1 = get_tv_number(&var1);
5386 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 }
5388 if (range)
5389 {
5390 if (empty2)
5391 n2 = -1;
5392 else
5393 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005394 n2 = get_tv_number(&var2);
5395 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 }
5397 }
5398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005399 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 {
5401 case VAR_NUMBER:
5402 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005403 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 len = (long)STRLEN(s);
5405 if (range)
5406 {
5407 /* The resulting variable is a substring. If the indexes
5408 * are out of range the result is empty. */
5409 if (n1 < 0)
5410 {
5411 n1 = len + n1;
5412 if (n1 < 0)
5413 n1 = 0;
5414 }
5415 if (n2 < 0)
5416 n2 = len + n2;
5417 else if (n2 >= len)
5418 n2 = len;
5419 if (n1 >= len || n2 < 0 || n1 > n2)
5420 s = NULL;
5421 else
5422 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5423 }
5424 else
5425 {
5426 /* The resulting variable is a string of a single
5427 * character. If the index is too big or negative the
5428 * result is empty. */
5429 if (n1 >= len || n1 < 0)
5430 s = NULL;
5431 else
5432 s = vim_strnsave(s + n1, 1);
5433 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005434 clear_tv(rettv);
5435 rettv->v_type = VAR_STRING;
5436 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 break;
5438
5439 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 if (n1 < 0)
5442 n1 = len + n1;
5443 if (!empty1 && (n1 < 0 || n1 >= len))
5444 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005445 /* For a range we allow invalid values and return an empty
5446 * list. A list index out of range is an error. */
5447 if (!range)
5448 {
5449 if (verbose)
5450 EMSGN(_(e_listidx), n1);
5451 return FAIL;
5452 }
5453 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455 if (range)
5456 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005457 list_T *l;
5458 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459
5460 if (n2 < 0)
5461 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005462 else if (n2 >= len)
5463 n2 = len - 1;
5464 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005465 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 l = list_alloc();
5467 if (l == NULL)
5468 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 n1 <= n2; ++n1)
5471 {
5472 if (list_append_tv(l, &item->li_tv) == FAIL)
5473 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005474 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 return FAIL;
5476 }
5477 item = item->li_next;
5478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005479 clear_tv(rettv);
5480 rettv->v_type = VAR_LIST;
5481 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005482 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 }
5484 else
5485 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005486 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005487 clear_tv(rettv);
5488 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 }
5490 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491
5492 case VAR_DICT:
5493 if (range)
5494 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005495 if (verbose)
5496 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 if (len == -1)
5498 clear_tv(&var1);
5499 return FAIL;
5500 }
5501 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005502 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005503
5504 if (len == -1)
5505 {
5506 key = get_tv_string(&var1);
5507 if (*key == NUL)
5508 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005509 if (verbose)
5510 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005511 clear_tv(&var1);
5512 return FAIL;
5513 }
5514 }
5515
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005516 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005517
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005518 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005519 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005520 if (len == -1)
5521 clear_tv(&var1);
5522 if (item == NULL)
5523 return FAIL;
5524
5525 copy_tv(&item->di_tv, &var1);
5526 clear_tv(rettv);
5527 *rettv = var1;
5528 }
5529 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 }
5531 }
5532
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 return OK;
5534}
5535
5536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 * Get an option value.
5538 * "arg" points to the '&' or '+' before the option name.
5539 * "arg" is advanced to character after the option name.
5540 * Return OK or FAIL.
5541 */
5542 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005543get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005545 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 int evaluate;
5547{
5548 char_u *option_end;
5549 long numval;
5550 char_u *stringval;
5551 int opt_type;
5552 int c;
5553 int working = (**arg == '+'); /* has("+option") */
5554 int ret = OK;
5555 int opt_flags;
5556
5557 /*
5558 * Isolate the option name and find its value.
5559 */
5560 option_end = find_option_end(arg, &opt_flags);
5561 if (option_end == NULL)
5562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 EMSG2(_("E112: Option name missing: %s"), *arg);
5565 return FAIL;
5566 }
5567
5568 if (!evaluate)
5569 {
5570 *arg = option_end;
5571 return OK;
5572 }
5573
5574 c = *option_end;
5575 *option_end = NUL;
5576 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578
5579 if (opt_type == -3) /* invalid name */
5580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 EMSG2(_("E113: Unknown option: %s"), *arg);
5583 ret = FAIL;
5584 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005585 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
5587 if (opt_type == -2) /* hidden string option */
5588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 rettv->v_type = VAR_STRING;
5590 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 else if (opt_type == -1) /* hidden number option */
5593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 rettv->v_type = VAR_NUMBER;
5595 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 else if (opt_type == 1) /* number option */
5598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005599 rettv->v_type = VAR_NUMBER;
5600 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
5602 else /* string option */
5603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005604 rettv->v_type = VAR_STRING;
5605 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 }
5607 }
5608 else if (working && (opt_type == -2 || opt_type == -1))
5609 ret = FAIL;
5610
5611 *option_end = c; /* put back for error messages */
5612 *arg = option_end;
5613
5614 return ret;
5615}
5616
5617/*
5618 * Allocate a variable for a string constant.
5619 * Return OK or FAIL.
5620 */
5621 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005622get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 int evaluate;
5626{
5627 char_u *p;
5628 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 int extra = 0;
5630
5631 /*
5632 * Find the end of the string, skipping backslashed characters.
5633 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005634 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 {
5636 if (*p == '\\' && p[1] != NUL)
5637 {
5638 ++p;
5639 /* A "\<x>" form occupies at least 4 characters, and produces up
5640 * to 6 characters: reserve space for 2 extra */
5641 if (*p == '<')
5642 extra += 2;
5643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 }
5645
5646 if (*p != '"')
5647 {
5648 EMSG2(_("E114: Missing quote: %s"), *arg);
5649 return FAIL;
5650 }
5651
5652 /* If only parsing, set *arg and return here */
5653 if (!evaluate)
5654 {
5655 *arg = p + 1;
5656 return OK;
5657 }
5658
5659 /*
5660 * Copy the string into allocated memory, handling backslashed
5661 * characters.
5662 */
5663 name = alloc((unsigned)(p - *arg + extra));
5664 if (name == NULL)
5665 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 rettv->v_type = VAR_STRING;
5667 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
Bram Moolenaar8c711452005-01-14 21:53:12 +00005669 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 if (*p == '\\')
5672 {
5673 switch (*++p)
5674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 case 'b': *name++ = BS; ++p; break;
5676 case 'e': *name++ = ESC; ++p; break;
5677 case 'f': *name++ = FF; ++p; break;
5678 case 'n': *name++ = NL; ++p; break;
5679 case 'r': *name++ = CAR; ++p; break;
5680 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681
5682 case 'X': /* hex: "\x1", "\x12" */
5683 case 'x':
5684 case 'u': /* Unicode: "\u0023" */
5685 case 'U':
5686 if (vim_isxdigit(p[1]))
5687 {
5688 int n, nr;
5689 int c = toupper(*p);
5690
5691 if (c == 'X')
5692 n = 2;
5693 else
5694 n = 4;
5695 nr = 0;
5696 while (--n >= 0 && vim_isxdigit(p[1]))
5697 {
5698 ++p;
5699 nr = (nr << 4) + hex2nr(*p);
5700 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702#ifdef FEAT_MBYTE
5703 /* For "\u" store the number according to
5704 * 'encoding'. */
5705 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 else
5708#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 break;
5712
5713 /* octal: "\1", "\12", "\123" */
5714 case '0':
5715 case '1':
5716 case '2':
5717 case '3':
5718 case '4':
5719 case '5':
5720 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 case '7': *name = *p++ - '0';
5722 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 *name = (*name << 3) + *p++ - '0';
5725 if (*p >= '0' && *p <= '7')
5726 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 break;
5730
5731 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 if (extra != 0)
5734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 break;
5737 }
5738 /* FALLTHROUGH */
5739
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 break;
5742 }
5743 }
5744 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 *arg = p + 1;
5750
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 return OK;
5752}
5753
5754/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 * Return OK or FAIL.
5757 */
5758 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005759get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 int evaluate;
5763{
5764 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005765 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 int reduce = 0;
5767
5768 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 */
5771 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5772 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 break;
5777 ++reduce;
5778 ++p;
5779 }
5780 }
5781
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785 return FAIL;
5786 }
5787
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005789 if (!evaluate)
5790 {
5791 *arg = p + 1;
5792 return OK;
5793 }
5794
5795 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005797 */
5798 str = alloc((unsigned)((p - *arg) - reduce));
5799 if (str == NULL)
5800 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 rettv->v_type = VAR_STRING;
5802 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809 break;
5810 ++p;
5811 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005813 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 *arg = p + 1;
5816
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005817 return OK;
5818}
5819
5820/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 * Allocate a variable for a List and fill it from "*arg".
5822 * Return OK or FAIL.
5823 */
5824 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005825get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005827 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 int evaluate;
5829{
Bram Moolenaar33570922005-01-25 22:26:29 +00005830 list_T *l = NULL;
5831 typval_T tv;
5832 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833
5834 if (evaluate)
5835 {
5836 l = list_alloc();
5837 if (l == NULL)
5838 return FAIL;
5839 }
5840
5841 *arg = skipwhite(*arg + 1);
5842 while (**arg != ']' && **arg != NUL)
5843 {
5844 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5845 goto failret;
5846 if (evaluate)
5847 {
5848 item = listitem_alloc();
5849 if (item != NULL)
5850 {
5851 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005852 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 list_append(l, item);
5854 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005855 else
5856 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857 }
5858
5859 if (**arg == ']')
5860 break;
5861 if (**arg != ',')
5862 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 goto failret;
5865 }
5866 *arg = skipwhite(*arg + 1);
5867 }
5868
5869 if (**arg != ']')
5870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872failret:
5873 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005874 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875 return FAIL;
5876 }
5877
5878 *arg = skipwhite(*arg + 1);
5879 if (evaluate)
5880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005881 rettv->v_type = VAR_LIST;
5882 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 ++l->lv_refcount;
5884 }
5885
5886 return OK;
5887}
5888
5889/*
5890 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005891 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005893 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894list_alloc()
5895{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005896 list_T *l;
5897
5898 l = (list_T *)alloc_clear(sizeof(list_T));
5899 if (l != NULL)
5900 {
5901 /* Prepend the list to the list of lists for garbage collection. */
5902 if (first_list != NULL)
5903 first_list->lv_used_prev = l;
5904 l->lv_used_prev = NULL;
5905 l->lv_used_next = first_list;
5906 first_list = l;
5907 }
5908 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909}
5910
5911/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005912 * Allocate an empty list for a return value.
5913 * Returns OK or FAIL.
5914 */
5915 static int
5916rettv_list_alloc(rettv)
5917 typval_T *rettv;
5918{
5919 list_T *l = list_alloc();
5920
5921 if (l == NULL)
5922 return FAIL;
5923
5924 rettv->vval.v_list = l;
5925 rettv->v_type = VAR_LIST;
5926 ++l->lv_refcount;
5927 return OK;
5928}
5929
5930/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931 * Unreference a list: decrement the reference count and free it when it
5932 * becomes zero.
5933 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005934 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005938 if (l != NULL && --l->lv_refcount <= 0)
5939 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940}
5941
5942/*
5943 * Free a list, including all items it points to.
5944 * Ignores the reference count.
5945 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005946 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005947list_free(l, recurse)
5948 list_T *l;
5949 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950{
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005953 /* Remove the list from the list of lists for garbage collection. */
5954 if (l->lv_used_prev == NULL)
5955 first_list = l->lv_used_next;
5956 else
5957 l->lv_used_prev->lv_used_next = l->lv_used_next;
5958 if (l->lv_used_next != NULL)
5959 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5960
Bram Moolenaard9fba312005-06-26 22:34:35 +00005961 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005963 /* Remove the item before deleting it. */
5964 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005965 if (recurse || (item->li_tv.v_type != VAR_LIST
5966 && item->li_tv.v_type != VAR_DICT))
5967 clear_tv(&item->li_tv);
5968 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 }
5970 vim_free(l);
5971}
5972
5973/*
5974 * Allocate a list item.
5975 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005976 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977listitem_alloc()
5978{
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980}
5981
5982/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005983 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005985 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005987 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005989 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 vim_free(item);
5991}
5992
5993/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005994 * Remove a list item from a List and free it. Also clears the value.
5995 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005996 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005997listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005998 list_T *l;
5999 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006000{
6001 list_remove(l, item, item);
6002 listitem_free(item);
6003}
6004
6005/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 * Get the number of items in a list.
6007 */
6008 static long
6009list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 if (l == NULL)
6013 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006014 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015}
6016
6017/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018 * Return TRUE when two lists have exactly the same values.
6019 */
6020 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006021list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 list_T *l1;
6023 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006024 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006025 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006026{
Bram Moolenaar33570922005-01-25 22:26:29 +00006027 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006028
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006029 if (l1 == NULL || l2 == NULL)
6030 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006031 if (l1 == l2)
6032 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006033 if (list_len(l1) != list_len(l2))
6034 return FALSE;
6035
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006036 for (item1 = l1->lv_first, item2 = l2->lv_first;
6037 item1 != NULL && item2 != NULL;
6038 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006039 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006040 return FALSE;
6041 return item1 == NULL && item2 == NULL;
6042}
6043
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006044#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6045 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006046/*
6047 * Return the dictitem that an entry in a hashtable points to.
6048 */
6049 dictitem_T *
6050dict_lookup(hi)
6051 hashitem_T *hi;
6052{
6053 return HI2DI(hi);
6054}
6055#endif
6056
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006057/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006058 * Return TRUE when two dictionaries have exactly the same key/values.
6059 */
6060 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 dict_T *d1;
6063 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006064 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006065 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006066{
Bram Moolenaar33570922005-01-25 22:26:29 +00006067 hashitem_T *hi;
6068 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006069 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006070
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006071 if (d1 == NULL || d2 == NULL)
6072 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006073 if (d1 == d2)
6074 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075 if (dict_len(d1) != dict_len(d2))
6076 return FALSE;
6077
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006078 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006079 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006080 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006081 if (!HASHITEM_EMPTY(hi))
6082 {
6083 item2 = dict_find(d2, hi->hi_key, -1);
6084 if (item2 == NULL)
6085 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006087 return FALSE;
6088 --todo;
6089 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006090 }
6091 return TRUE;
6092}
6093
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006094static int tv_equal_recurse_limit;
6095
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006096/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006098 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006099 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100 */
6101 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006103 typval_T *tv1;
6104 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105 int ic; /* ignore case */
6106 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107{
6108 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006109 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006111 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006113 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006114 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006115
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006116 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 * recursiveness to a limit. We guess they are equal then.
6118 * A fixed limit has the problem of still taking an awful long time.
6119 * Reduce the limit every time running into it. That should work fine for
6120 * deeply linked structures that are not recursively linked and catch
6121 * recursiveness quickly. */
6122 if (!recursive)
6123 tv_equal_recurse_limit = 1000;
6124 if (recursive_cnt >= tv_equal_recurse_limit)
6125 {
6126 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006127 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006128 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006129
6130 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006132 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006133 ++recursive_cnt;
6134 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6135 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006136 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006137
6138 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006139 ++recursive_cnt;
6140 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6141 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006142 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006143
6144 case VAR_FUNC:
6145 return (tv1->vval.v_string != NULL
6146 && tv2->vval.v_string != NULL
6147 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6148
6149 case VAR_NUMBER:
6150 return tv1->vval.v_number == tv2->vval.v_number;
6151
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006152#ifdef FEAT_FLOAT
6153 case VAR_FLOAT:
6154 return tv1->vval.v_float == tv2->vval.v_float;
6155#endif
6156
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006157 case VAR_STRING:
6158 s1 = get_tv_string_buf(tv1, buf1);
6159 s2 = get_tv_string_buf(tv2, buf2);
6160 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006161 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006162
6163 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006164 return TRUE;
6165}
6166
6167/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 * Locate item with index "n" in list "l" and return it.
6169 * A negative index is counted from the end; -1 is the last item.
6170 * Returns NULL when "n" is out of range.
6171 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006172 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006174 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006175 long n;
6176{
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 long idx;
6179
6180 if (l == NULL)
6181 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006182
6183 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006184 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006185 n = l->lv_len + n;
6186
6187 /* Check for index out of range. */
6188 if (n < 0 || n >= l->lv_len)
6189 return NULL;
6190
6191 /* When there is a cached index may start search from there. */
6192 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006194 if (n < l->lv_idx / 2)
6195 {
6196 /* closest to the start of the list */
6197 item = l->lv_first;
6198 idx = 0;
6199 }
6200 else if (n > (l->lv_idx + l->lv_len) / 2)
6201 {
6202 /* closest to the end of the list */
6203 item = l->lv_last;
6204 idx = l->lv_len - 1;
6205 }
6206 else
6207 {
6208 /* closest to the cached index */
6209 item = l->lv_idx_item;
6210 idx = l->lv_idx;
6211 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006212 }
6213 else
6214 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006215 if (n < l->lv_len / 2)
6216 {
6217 /* closest to the start of the list */
6218 item = l->lv_first;
6219 idx = 0;
6220 }
6221 else
6222 {
6223 /* closest to the end of the list */
6224 item = l->lv_last;
6225 idx = l->lv_len - 1;
6226 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006227 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006228
6229 while (n > idx)
6230 {
6231 /* search forward */
6232 item = item->li_next;
6233 ++idx;
6234 }
6235 while (n < idx)
6236 {
6237 /* search backward */
6238 item = item->li_prev;
6239 --idx;
6240 }
6241
6242 /* cache the used index */
6243 l->lv_idx = idx;
6244 l->lv_idx_item = item;
6245
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006246 return item;
6247}
6248
6249/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006250 * Get list item "l[idx]" as a number.
6251 */
6252 static long
6253list_find_nr(l, idx, errorp)
6254 list_T *l;
6255 long idx;
6256 int *errorp; /* set to TRUE when something wrong */
6257{
6258 listitem_T *li;
6259
6260 li = list_find(l, idx);
6261 if (li == NULL)
6262 {
6263 if (errorp != NULL)
6264 *errorp = TRUE;
6265 return -1L;
6266 }
6267 return get_tv_number_chk(&li->li_tv, errorp);
6268}
6269
6270/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006271 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6272 */
6273 char_u *
6274list_find_str(l, idx)
6275 list_T *l;
6276 long idx;
6277{
6278 listitem_T *li;
6279
6280 li = list_find(l, idx - 1);
6281 if (li == NULL)
6282 {
6283 EMSGN(_(e_listidx), idx);
6284 return NULL;
6285 }
6286 return get_tv_string(&li->li_tv);
6287}
6288
6289/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006290 * Locate "item" list "l" and return its index.
6291 * Returns -1 when "item" is not in the list.
6292 */
6293 static long
6294list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 list_T *l;
6296 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006297{
6298 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006299 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006300
6301 if (l == NULL)
6302 return -1;
6303 idx = 0;
6304 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6305 ++idx;
6306 if (li == NULL)
6307 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006308 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006309}
6310
6311/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 * Append item "item" to the end of list "l".
6313 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006314 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006316 list_T *l;
6317 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318{
6319 if (l->lv_last == NULL)
6320 {
6321 /* empty list */
6322 l->lv_first = item;
6323 l->lv_last = item;
6324 item->li_prev = NULL;
6325 }
6326 else
6327 {
6328 l->lv_last->li_next = item;
6329 item->li_prev = l->lv_last;
6330 l->lv_last = item;
6331 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006332 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006333 item->li_next = NULL;
6334}
6335
6336/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006338 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006339 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006340 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006341list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 list_T *l;
6343 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006344{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006345 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346
Bram Moolenaar05159a02005-02-26 23:04:13 +00006347 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006349 copy_tv(tv, &li->li_tv);
6350 list_append(l, li);
6351 return OK;
6352}
6353
6354/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006355 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006356 * Return FAIL when out of memory.
6357 */
6358 int
6359list_append_dict(list, dict)
6360 list_T *list;
6361 dict_T *dict;
6362{
6363 listitem_T *li = listitem_alloc();
6364
6365 if (li == NULL)
6366 return FAIL;
6367 li->li_tv.v_type = VAR_DICT;
6368 li->li_tv.v_lock = 0;
6369 li->li_tv.vval.v_dict = dict;
6370 list_append(list, li);
6371 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372 return OK;
6373}
6374
6375/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006376 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006377 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006378 * Returns FAIL when out of memory.
6379 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006380 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006381list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006382 list_T *l;
6383 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006384 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006385{
6386 listitem_T *li = listitem_alloc();
6387
6388 if (li == NULL)
6389 return FAIL;
6390 list_append(l, li);
6391 li->li_tv.v_type = VAR_STRING;
6392 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006393 if (str == NULL)
6394 li->li_tv.vval.v_string = NULL;
6395 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006396 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006397 return FAIL;
6398 return OK;
6399}
6400
6401/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006402 * Append "n" to list "l".
6403 * Returns FAIL when out of memory.
6404 */
6405 static int
6406list_append_number(l, n)
6407 list_T *l;
6408 varnumber_T n;
6409{
6410 listitem_T *li;
6411
6412 li = listitem_alloc();
6413 if (li == NULL)
6414 return FAIL;
6415 li->li_tv.v_type = VAR_NUMBER;
6416 li->li_tv.v_lock = 0;
6417 li->li_tv.vval.v_number = n;
6418 list_append(l, li);
6419 return OK;
6420}
6421
6422/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 * If "item" is NULL append at the end.
6425 * Return FAIL when out of memory.
6426 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006427 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006429 list_T *l;
6430 typval_T *tv;
6431 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432{
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434
6435 if (ni == NULL)
6436 return FAIL;
6437 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006438 list_insert(l, ni, item);
6439 return OK;
6440}
6441
6442 void
6443list_insert(l, ni, item)
6444 list_T *l;
6445 listitem_T *ni;
6446 listitem_T *item;
6447{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 if (item == NULL)
6449 /* Append new item at end of list. */
6450 list_append(l, ni);
6451 else
6452 {
6453 /* Insert new item before existing item. */
6454 ni->li_prev = item->li_prev;
6455 ni->li_next = item;
6456 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006457 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006459 ++l->lv_idx;
6460 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006461 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006462 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006464 l->lv_idx_item = NULL;
6465 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006467 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469}
6470
6471/*
6472 * Extend "l1" with "l2".
6473 * If "bef" is NULL append at the end, otherwise insert before this item.
6474 * Returns FAIL when out of memory.
6475 */
6476 static int
6477list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 list_T *l1;
6479 list_T *l2;
6480 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006481{
Bram Moolenaar33570922005-01-25 22:26:29 +00006482 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006483 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006485 /* We also quit the loop when we have inserted the original item count of
6486 * the list, avoid a hang when we extend a list with itself. */
6487 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6489 return FAIL;
6490 return OK;
6491}
6492
6493/*
6494 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6495 * Return FAIL when out of memory.
6496 */
6497 static int
6498list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 list_T *l1;
6500 list_T *l2;
6501 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502{
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006505 if (l1 == NULL || l2 == NULL)
6506 return FAIL;
6507
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006509 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510 if (l == NULL)
6511 return FAIL;
6512 tv->v_type = VAR_LIST;
6513 tv->vval.v_list = l;
6514
6515 /* append all items from the second list */
6516 return list_extend(l, l2, NULL);
6517}
6518
6519/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006521 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006522 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006523 * Returns NULL when out of memory.
6524 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006525 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006526list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006527 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530{
Bram Moolenaar33570922005-01-25 22:26:29 +00006531 list_T *copy;
6532 listitem_T *item;
6533 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534
6535 if (orig == NULL)
6536 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537
6538 copy = list_alloc();
6539 if (copy != NULL)
6540 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006541 if (copyID != 0)
6542 {
6543 /* Do this before adding the items, because one of the items may
6544 * refer back to this list. */
6545 orig->lv_copyID = copyID;
6546 orig->lv_copylist = copy;
6547 }
6548 for (item = orig->lv_first; item != NULL && !got_int;
6549 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 {
6551 ni = listitem_alloc();
6552 if (ni == NULL)
6553 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006555 {
6556 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6557 {
6558 vim_free(ni);
6559 break;
6560 }
6561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006563 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564 list_append(copy, ni);
6565 }
6566 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006567 if (item != NULL)
6568 {
6569 list_unref(copy);
6570 copy = NULL;
6571 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 }
6573
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 return copy;
6575}
6576
6577/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006578 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006579 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006581 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006582list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 list_T *l;
6584 listitem_T *item;
6585 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586{
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589 /* notify watchers */
6590 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006592 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593 list_fix_watch(l, ip);
6594 if (ip == item2)
6595 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006597
6598 if (item2->li_next == NULL)
6599 l->lv_last = item->li_prev;
6600 else
6601 item2->li_next->li_prev = item->li_prev;
6602 if (item->li_prev == NULL)
6603 l->lv_first = item2->li_next;
6604 else
6605 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006606 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607}
6608
6609/*
6610 * Return an allocated string with the string representation of a list.
6611 * May return NULL.
6612 */
6613 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006614list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006615 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006616 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617{
6618 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619
6620 if (tv->vval.v_list == NULL)
6621 return NULL;
6622 ga_init2(&ga, (int)sizeof(char), 80);
6623 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006624 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006625 {
6626 vim_free(ga.ga_data);
6627 return NULL;
6628 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629 ga_append(&ga, ']');
6630 ga_append(&ga, NUL);
6631 return (char_u *)ga.ga_data;
6632}
6633
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006634typedef struct join_S {
6635 char_u *s;
6636 char_u *tofree;
6637} join_T;
6638
6639 static int
6640list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6641 garray_T *gap; /* to store the result in */
6642 list_T *l;
6643 char_u *sep;
6644 int echo_style;
6645 int copyID;
6646 garray_T *join_gap; /* to keep each list item string */
6647{
6648 int i;
6649 join_T *p;
6650 int len;
6651 int sumlen = 0;
6652 int first = TRUE;
6653 char_u *tofree;
6654 char_u numbuf[NUMBUFLEN];
6655 listitem_T *item;
6656 char_u *s;
6657
6658 /* Stringify each item in the list. */
6659 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6660 {
6661 if (echo_style)
6662 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6663 else
6664 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6665 if (s == NULL)
6666 return FAIL;
6667
6668 len = (int)STRLEN(s);
6669 sumlen += len;
6670
6671 ga_grow(join_gap, 1);
6672 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6673 if (tofree != NULL || s != numbuf)
6674 {
6675 p->s = s;
6676 p->tofree = tofree;
6677 }
6678 else
6679 {
6680 p->s = vim_strnsave(s, len);
6681 p->tofree = p->s;
6682 }
6683
6684 line_breakcheck();
6685 }
6686
6687 /* Allocate result buffer with its total size, avoid re-allocation and
6688 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6689 if (join_gap->ga_len >= 2)
6690 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6691 if (ga_grow(gap, sumlen + 2) == FAIL)
6692 return FAIL;
6693
6694 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6695 {
6696 if (first)
6697 first = FALSE;
6698 else
6699 ga_concat(gap, sep);
6700 p = ((join_T *)join_gap->ga_data) + i;
6701
6702 if (p->s != NULL)
6703 ga_concat(gap, p->s);
6704 line_breakcheck();
6705 }
6706
6707 return OK;
6708}
6709
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006710/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006711 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006712 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006713 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006715 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006716list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006717 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006718 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006719 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006720 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006721 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006722{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006723 garray_T join_ga;
6724 int retval;
6725 join_T *p;
6726 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006727
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006728 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6729 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6730
6731 /* Dispose each item in join_ga. */
6732 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006733 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006734 p = (join_T *)join_ga.ga_data;
6735 for (i = 0; i < join_ga.ga_len; ++i)
6736 {
6737 vim_free(p->tofree);
6738 ++p;
6739 }
6740 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006741 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006742
6743 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006744}
6745
6746/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006747 * Garbage collection for lists and dictionaries.
6748 *
6749 * We use reference counts to be able to free most items right away when they
6750 * are no longer used. But for composite items it's possible that it becomes
6751 * unused while the reference count is > 0: When there is a recursive
6752 * reference. Example:
6753 * :let l = [1, 2, 3]
6754 * :let d = {9: l}
6755 * :let l[1] = d
6756 *
6757 * Since this is quite unusual we handle this with garbage collection: every
6758 * once in a while find out which lists and dicts are not referenced from any
6759 * variable.
6760 *
6761 * Here is a good reference text about garbage collection (refers to Python
6762 * but it applies to all reference-counting mechanisms):
6763 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006764 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006765
6766/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 * Do garbage collection for lists and dicts.
6768 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006769 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 int
6771garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006772{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006773 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006774 buf_T *buf;
6775 win_T *wp;
6776 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006777 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006778 int did_free;
6779 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006780#ifdef FEAT_WINDOWS
6781 tabpage_T *tp;
6782#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006783
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006784 /* Only do this once. */
6785 want_garbage_collect = FALSE;
6786 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006787 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006788
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006789 /* We advance by two because we add one for items referenced through
6790 * previous_funccal. */
6791 current_copyID += COPYID_INC;
6792 copyID = current_copyID;
6793
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006794 /*
6795 * 1. Go through all accessible variables and mark all lists and dicts
6796 * with copyID.
6797 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006798
6799 /* Don't free variables in the previous_funccal list unless they are only
6800 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006801 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006802 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6803 {
6804 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6805 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6806 }
6807
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006808 /* script-local variables */
6809 for (i = 1; i <= ga_scripts.ga_len; ++i)
6810 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6811
6812 /* buffer-local variables */
6813 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006814 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006815
6816 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006817 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006818 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006819#ifdef FEAT_AUTOCMD
6820 if (aucmd_win != NULL)
6821 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6822#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006823
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006824#ifdef FEAT_WINDOWS
6825 /* tabpage-local variables */
6826 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006827 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006828#endif
6829
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006830 /* global variables */
6831 set_ref_in_ht(&globvarht, copyID);
6832
6833 /* function-local variables */
6834 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6835 {
6836 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6837 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6838 }
6839
Bram Moolenaard812df62008-11-09 12:46:09 +00006840 /* v: vars */
6841 set_ref_in_ht(&vimvarht, copyID);
6842
Bram Moolenaar1dced572012-04-05 16:54:08 +02006843#ifdef FEAT_LUA
6844 set_ref_in_lua(copyID);
6845#endif
6846
Bram Moolenaardb913952012-06-29 12:54:53 +02006847#ifdef FEAT_PYTHON
6848 set_ref_in_python(copyID);
6849#endif
6850
6851#ifdef FEAT_PYTHON3
6852 set_ref_in_python3(copyID);
6853#endif
6854
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006855 /*
6856 * 2. Free lists and dictionaries that are not referenced.
6857 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858 did_free = free_unref_items(copyID);
6859
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006860 /*
6861 * 3. Check if any funccal can be freed now.
6862 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863 for (pfc = &previous_funccal; *pfc != NULL; )
6864 {
6865 if (can_free_funccal(*pfc, copyID))
6866 {
6867 fc = *pfc;
6868 *pfc = fc->caller;
6869 free_funccal(fc, TRUE);
6870 did_free = TRUE;
6871 did_free_funccal = TRUE;
6872 }
6873 else
6874 pfc = &(*pfc)->caller;
6875 }
6876 if (did_free_funccal)
6877 /* When a funccal was freed some more items might be garbage
6878 * collected, so run again. */
6879 (void)garbage_collect();
6880
6881 return did_free;
6882}
6883
6884/*
6885 * Free lists and dictionaries that are no longer referenced.
6886 */
6887 static int
6888free_unref_items(copyID)
6889 int copyID;
6890{
6891 dict_T *dd;
6892 list_T *ll;
6893 int did_free = FALSE;
6894
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006896 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006897 */
6898 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006899 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006901 /* Free the Dictionary and ordinary items it contains, but don't
6902 * recurse into Lists and Dictionaries, they will be in the list
6903 * of dicts or list of lists. */
6904 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905 did_free = TRUE;
6906
6907 /* restart, next dict may also have been freed */
6908 dd = first_dict;
6909 }
6910 else
6911 dd = dd->dv_used_next;
6912
6913 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006914 * Go through the list of lists and free items without the copyID.
6915 * But don't free a list that has a watcher (used in a for loop), these
6916 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 */
6918 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006919 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6920 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006922 /* Free the List and ordinary items it contains, but don't recurse
6923 * into Lists and Dictionaries, they will be in the list of dicts
6924 * or list of lists. */
6925 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006926 did_free = TRUE;
6927
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006928 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 ll = first_list;
6930 }
6931 else
6932 ll = ll->lv_used_next;
6933
6934 return did_free;
6935}
6936
6937/*
6938 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6939 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006940 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006941set_ref_in_ht(ht, copyID)
6942 hashtab_T *ht;
6943 int copyID;
6944{
6945 int todo;
6946 hashitem_T *hi;
6947
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006948 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006949 for (hi = ht->ht_array; todo > 0; ++hi)
6950 if (!HASHITEM_EMPTY(hi))
6951 {
6952 --todo;
6953 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6954 }
6955}
6956
6957/*
6958 * Mark all lists and dicts referenced through list "l" with "copyID".
6959 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006960 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006961set_ref_in_list(l, copyID)
6962 list_T *l;
6963 int copyID;
6964{
6965 listitem_T *li;
6966
6967 for (li = l->lv_first; li != NULL; li = li->li_next)
6968 set_ref_in_item(&li->li_tv, copyID);
6969}
6970
6971/*
6972 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6973 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006974 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975set_ref_in_item(tv, copyID)
6976 typval_T *tv;
6977 int copyID;
6978{
6979 dict_T *dd;
6980 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006981
6982 switch (tv->v_type)
6983 {
6984 case VAR_DICT:
6985 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006986 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006987 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 /* Didn't see this dict yet. */
6989 dd->dv_copyID = copyID;
6990 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006991 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006993
6994 case VAR_LIST:
6995 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006996 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006997 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 /* Didn't see this list yet. */
6999 ll->lv_copyID = copyID;
7000 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007001 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007003 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007005}
7006
7007/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007008 * Allocate an empty header for a dictionary.
7009 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007010 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007011dict_alloc()
7012{
Bram Moolenaar33570922005-01-25 22:26:29 +00007013 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007014
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007016 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007017 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007018 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 if (first_dict != NULL)
7020 first_dict->dv_used_prev = d;
7021 d->dv_used_next = first_dict;
7022 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007023 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024
Bram Moolenaar33570922005-01-25 22:26:29 +00007025 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007026 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007027 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007028 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007029 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007030 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007031 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007032}
7033
7034/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007035 * Allocate an empty dict for a return value.
7036 * Returns OK or FAIL.
7037 */
7038 static int
7039rettv_dict_alloc(rettv)
7040 typval_T *rettv;
7041{
7042 dict_T *d = dict_alloc();
7043
7044 if (d == NULL)
7045 return FAIL;
7046
7047 rettv->vval.v_dict = d;
7048 rettv->v_type = VAR_DICT;
7049 ++d->dv_refcount;
7050 return OK;
7051}
7052
7053
7054/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007055 * Unreference a Dictionary: decrement the reference count and free it when it
7056 * becomes zero.
7057 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007058 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007059dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007060 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007061{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007062 if (d != NULL && --d->dv_refcount <= 0)
7063 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064}
7065
7066/*
7067 * Free a Dictionary, including all items it contains.
7068 * Ignores the reference count.
7069 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007070 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007071dict_free(d, recurse)
7072 dict_T *d;
7073 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007074{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007075 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007076 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007077 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079 /* Remove the dict from the list of dicts for garbage collection. */
7080 if (d->dv_used_prev == NULL)
7081 first_dict = d->dv_used_next;
7082 else
7083 d->dv_used_prev->dv_used_next = d->dv_used_next;
7084 if (d->dv_used_next != NULL)
7085 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7086
7087 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007088 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007089 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007090 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007091 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092 if (!HASHITEM_EMPTY(hi))
7093 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007094 /* Remove the item before deleting it, just in case there is
7095 * something recursive causing trouble. */
7096 di = HI2DI(hi);
7097 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007098 if (recurse || (di->di_tv.v_type != VAR_LIST
7099 && di->di_tv.v_type != VAR_DICT))
7100 clear_tv(&di->di_tv);
7101 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007102 --todo;
7103 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007105 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007106 vim_free(d);
7107}
7108
7109/*
7110 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007111 * The "key" is copied to the new item.
7112 * Note that the value of the item "di_tv" still needs to be initialized!
7113 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007114 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007115 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007116dictitem_alloc(key)
7117 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007118{
Bram Moolenaar33570922005-01-25 22:26:29 +00007119 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007121 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007123 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007125 di->di_flags = 0;
7126 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128}
7129
7130/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007131 * Make a copy of a Dictionary item.
7132 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007134dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007136{
Bram Moolenaar33570922005-01-25 22:26:29 +00007137 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007139 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7140 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007141 if (di != NULL)
7142 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007143 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007144 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007145 copy_tv(&org->di_tv, &di->di_tv);
7146 }
7147 return di;
7148}
7149
7150/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151 * Remove item "item" from Dictionary "dict" and free it.
7152 */
7153 static void
7154dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007155 dict_T *dict;
7156 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007157{
Bram Moolenaar33570922005-01-25 22:26:29 +00007158 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007159
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161 if (HASHITEM_EMPTY(hi))
7162 EMSG2(_(e_intern2), "dictitem_remove()");
7163 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007164 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007165 dictitem_free(item);
7166}
7167
7168/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007169 * Free a dict item. Also clears the value.
7170 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007171 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175 clear_tv(&item->di_tv);
7176 vim_free(item);
7177}
7178
7179/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7181 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007182 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007183 * Returns NULL when out of memory.
7184 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007185 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007186dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007188 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007189 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190{
Bram Moolenaar33570922005-01-25 22:26:29 +00007191 dict_T *copy;
7192 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007195
7196 if (orig == NULL)
7197 return NULL;
7198
7199 copy = dict_alloc();
7200 if (copy != NULL)
7201 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007202 if (copyID != 0)
7203 {
7204 orig->dv_copyID = copyID;
7205 orig->dv_copydict = copy;
7206 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007207 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007208 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007209 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007210 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212 --todo;
7213
7214 di = dictitem_alloc(hi->hi_key);
7215 if (di == NULL)
7216 break;
7217 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007218 {
7219 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7220 copyID) == FAIL)
7221 {
7222 vim_free(di);
7223 break;
7224 }
7225 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007226 else
7227 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7228 if (dict_add(copy, di) == FAIL)
7229 {
7230 dictitem_free(di);
7231 break;
7232 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007233 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007234 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007235
Bram Moolenaare9a41262005-01-15 22:18:47 +00007236 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007237 if (todo > 0)
7238 {
7239 dict_unref(copy);
7240 copy = NULL;
7241 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007242 }
7243
7244 return copy;
7245}
7246
7247/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007249 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007251 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007253 dict_T *d;
7254 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255{
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257}
7258
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007260 * Add a number or string entry to dictionary "d".
7261 * When "str" is NULL use number "nr", otherwise use "str".
7262 * Returns FAIL when out of memory and when key already exists.
7263 */
7264 int
7265dict_add_nr_str(d, key, nr, str)
7266 dict_T *d;
7267 char *key;
7268 long nr;
7269 char_u *str;
7270{
7271 dictitem_T *item;
7272
7273 item = dictitem_alloc((char_u *)key);
7274 if (item == NULL)
7275 return FAIL;
7276 item->di_tv.v_lock = 0;
7277 if (str == NULL)
7278 {
7279 item->di_tv.v_type = VAR_NUMBER;
7280 item->di_tv.vval.v_number = nr;
7281 }
7282 else
7283 {
7284 item->di_tv.v_type = VAR_STRING;
7285 item->di_tv.vval.v_string = vim_strsave(str);
7286 }
7287 if (dict_add(d, item) == FAIL)
7288 {
7289 dictitem_free(item);
7290 return FAIL;
7291 }
7292 return OK;
7293}
7294
7295/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007296 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007297 * Returns FAIL when out of memory and when key already exists.
7298 */
7299 int
7300dict_add_list(d, key, list)
7301 dict_T *d;
7302 char *key;
7303 list_T *list;
7304{
7305 dictitem_T *item;
7306
7307 item = dictitem_alloc((char_u *)key);
7308 if (item == NULL)
7309 return FAIL;
7310 item->di_tv.v_lock = 0;
7311 item->di_tv.v_type = VAR_LIST;
7312 item->di_tv.vval.v_list = list;
7313 if (dict_add(d, item) == FAIL)
7314 {
7315 dictitem_free(item);
7316 return FAIL;
7317 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007318 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007319 return OK;
7320}
7321
7322/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 * Get the number of items in a Dictionary.
7324 */
7325 static long
7326dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007328{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329 if (d == NULL)
7330 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007331 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332}
7333
7334/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335 * Find item "key[len]" in Dictionary "d".
7336 * If "len" is negative use strlen(key).
7337 * Returns NULL when not found.
7338 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007339 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007340dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007341 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 char_u *key;
7343 int len;
7344{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345#define AKEYLEN 200
7346 char_u buf[AKEYLEN];
7347 char_u *akey;
7348 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007350
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351 if (len < 0)
7352 akey = key;
7353 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355 tofree = akey = vim_strnsave(key, len);
7356 if (akey == NULL)
7357 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007358 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 else
7360 {
7361 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007362 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 akey = buf;
7364 }
7365
Bram Moolenaar33570922005-01-25 22:26:29 +00007366 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007367 vim_free(tofree);
7368 if (HASHITEM_EMPTY(hi))
7369 return NULL;
7370 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007371}
7372
7373/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007374 * Get a string item from a dictionary.
7375 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007376 * Returns NULL if the entry doesn't exist or out of memory.
7377 */
7378 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007379get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007380 dict_T *d;
7381 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007382 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007383{
7384 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007385 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007386
7387 di = dict_find(d, key, -1);
7388 if (di == NULL)
7389 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007390 s = get_tv_string(&di->di_tv);
7391 if (save && s != NULL)
7392 s = vim_strsave(s);
7393 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007394}
7395
7396/*
7397 * Get a number item from a dictionary.
7398 * Returns 0 if the entry doesn't exist or out of memory.
7399 */
7400 long
7401get_dict_number(d, key)
7402 dict_T *d;
7403 char_u *key;
7404{
7405 dictitem_T *di;
7406
7407 di = dict_find(d, key, -1);
7408 if (di == NULL)
7409 return 0;
7410 return get_tv_number(&di->di_tv);
7411}
7412
7413/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414 * Return an allocated string with the string representation of a Dictionary.
7415 * May return NULL.
7416 */
7417 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007418dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007420 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421{
7422 garray_T ga;
7423 int first = TRUE;
7424 char_u *tofree;
7425 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007429 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007431 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432 return NULL;
7433 ga_init2(&ga, (int)sizeof(char), 80);
7434 ga_append(&ga, '{');
7435
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007436 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007437 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007439 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007441 --todo;
7442
7443 if (first)
7444 first = FALSE;
7445 else
7446 ga_concat(&ga, (char_u *)", ");
7447
7448 tofree = string_quote(hi->hi_key, FALSE);
7449 if (tofree != NULL)
7450 {
7451 ga_concat(&ga, tofree);
7452 vim_free(tofree);
7453 }
7454 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007455 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007456 if (s != NULL)
7457 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007459 if (s == NULL)
7460 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007462 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007463 if (todo > 0)
7464 {
7465 vim_free(ga.ga_data);
7466 return NULL;
7467 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468
7469 ga_append(&ga, '}');
7470 ga_append(&ga, NUL);
7471 return (char_u *)ga.ga_data;
7472}
7473
7474/*
7475 * Allocate a variable for a Dictionary and fill it from "*arg".
7476 * Return OK or FAIL. Returns NOTDONE for {expr}.
7477 */
7478 static int
7479get_dict_tv(arg, rettv, evaluate)
7480 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007481 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482 int evaluate;
7483{
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 dict_T *d = NULL;
7485 typval_T tvkey;
7486 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007487 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007488 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007490 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491
7492 /*
7493 * First check if it's not a curly-braces thing: {expr}.
7494 * Must do this without evaluating, otherwise a function may be called
7495 * twice. Unfortunately this means we need to call eval1() twice for the
7496 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499 if (*start != '}')
7500 {
7501 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7502 return FAIL;
7503 if (*start == '}')
7504 return NOTDONE;
7505 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506
7507 if (evaluate)
7508 {
7509 d = dict_alloc();
7510 if (d == NULL)
7511 return FAIL;
7512 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 tvkey.v_type = VAR_UNKNOWN;
7514 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515
7516 *arg = skipwhite(*arg + 1);
7517 while (**arg != '}' && **arg != NUL)
7518 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520 goto failret;
7521 if (**arg != ':')
7522 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007523 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007524 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525 goto failret;
7526 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007527 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007529 key = get_tv_string_buf_chk(&tvkey, buf);
7530 if (key == NULL || *key == NUL)
7531 {
7532 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7533 if (key != NULL)
7534 EMSG(_(e_emptykey));
7535 clear_tv(&tvkey);
7536 goto failret;
7537 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539
7540 *arg = skipwhite(*arg + 1);
7541 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7542 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007543 if (evaluate)
7544 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545 goto failret;
7546 }
7547 if (evaluate)
7548 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007549 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 if (item != NULL)
7551 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007552 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007553 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554 clear_tv(&tv);
7555 goto failret;
7556 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007557 item = dictitem_alloc(key);
7558 clear_tv(&tvkey);
7559 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007562 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563 if (dict_add(d, item) == FAIL)
7564 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 }
7566 }
7567
7568 if (**arg == '}')
7569 break;
7570 if (**arg != ',')
7571 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007572 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573 goto failret;
7574 }
7575 *arg = skipwhite(*arg + 1);
7576 }
7577
7578 if (**arg != '}')
7579 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007580 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581failret:
7582 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007583 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 return FAIL;
7585 }
7586
7587 *arg = skipwhite(*arg + 1);
7588 if (evaluate)
7589 {
7590 rettv->v_type = VAR_DICT;
7591 rettv->vval.v_dict = d;
7592 ++d->dv_refcount;
7593 }
7594
7595 return OK;
7596}
7597
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007599 * Return a string with the string representation of a variable.
7600 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007601 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007602 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007603 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007604 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007605 */
7606 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007607echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007609 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007610 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007611 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007613 static int recurse = 0;
7614 char_u *r = NULL;
7615
Bram Moolenaar33570922005-01-25 22:26:29 +00007616 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007618 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619 *tofree = NULL;
7620 return NULL;
7621 }
7622 ++recurse;
7623
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007624 switch (tv->v_type)
7625 {
7626 case VAR_FUNC:
7627 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007628 r = tv->vval.v_string;
7629 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007630
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007631 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007632 if (tv->vval.v_list == NULL)
7633 {
7634 *tofree = NULL;
7635 r = NULL;
7636 }
7637 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7638 {
7639 *tofree = NULL;
7640 r = (char_u *)"[...]";
7641 }
7642 else
7643 {
7644 tv->vval.v_list->lv_copyID = copyID;
7645 *tofree = list2string(tv, copyID);
7646 r = *tofree;
7647 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007649
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007651 if (tv->vval.v_dict == NULL)
7652 {
7653 *tofree = NULL;
7654 r = NULL;
7655 }
7656 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7657 {
7658 *tofree = NULL;
7659 r = (char_u *)"{...}";
7660 }
7661 else
7662 {
7663 tv->vval.v_dict->dv_copyID = copyID;
7664 *tofree = dict2string(tv, copyID);
7665 r = *tofree;
7666 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007667 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007668
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007669 case VAR_STRING:
7670 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671 *tofree = NULL;
7672 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007673 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007674
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007675#ifdef FEAT_FLOAT
7676 case VAR_FLOAT:
7677 *tofree = NULL;
7678 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7679 r = numbuf;
7680 break;
7681#endif
7682
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007683 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007684 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007685 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007687
7688 --recurse;
7689 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690}
7691
7692/*
7693 * Return a string with the string representation of a variable.
7694 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7695 * "numbuf" is used for a number.
7696 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007697 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007698 */
7699 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007700tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007702 char_u **tofree;
7703 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007704 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007705{
7706 switch (tv->v_type)
7707 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007708 case VAR_FUNC:
7709 *tofree = string_quote(tv->vval.v_string, TRUE);
7710 return *tofree;
7711 case VAR_STRING:
7712 *tofree = string_quote(tv->vval.v_string, FALSE);
7713 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007714#ifdef FEAT_FLOAT
7715 case VAR_FLOAT:
7716 *tofree = NULL;
7717 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7718 return numbuf;
7719#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007720 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007721 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007723 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007724 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007725 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007726 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007727 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007728}
7729
7730/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007731 * Return string "str" in ' quotes, doubling ' characters.
7732 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007734 */
7735 static char_u *
7736string_quote(str, function)
7737 char_u *str;
7738 int function;
7739{
Bram Moolenaar33570922005-01-25 22:26:29 +00007740 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007741 char_u *p, *r, *s;
7742
Bram Moolenaar33570922005-01-25 22:26:29 +00007743 len = (function ? 13 : 3);
7744 if (str != NULL)
7745 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007746 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007747 for (p = str; *p != NUL; mb_ptr_adv(p))
7748 if (*p == '\'')
7749 ++len;
7750 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007751 s = r = alloc(len);
7752 if (r != NULL)
7753 {
7754 if (function)
7755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007757 r += 10;
7758 }
7759 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007760 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007761 if (str != NULL)
7762 for (p = str; *p != NUL; )
7763 {
7764 if (*p == '\'')
7765 *r++ = '\'';
7766 MB_COPY_CHAR(p, r);
7767 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007768 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007769 if (function)
7770 *r++ = ')';
7771 *r++ = NUL;
7772 }
7773 return s;
7774}
7775
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007776#ifdef FEAT_FLOAT
7777/*
7778 * Convert the string "text" to a floating point number.
7779 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7780 * this always uses a decimal point.
7781 * Returns the length of the text that was consumed.
7782 */
7783 static int
7784string2float(text, value)
7785 char_u *text;
7786 float_T *value; /* result stored here */
7787{
7788 char *s = (char *)text;
7789 float_T f;
7790
7791 f = strtod(s, &s);
7792 *value = f;
7793 return (int)((char_u *)s - text);
7794}
7795#endif
7796
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 * Get the value of an environment variable.
7799 * "arg" is pointing to the '$'. It is advanced to after the name.
7800 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007801 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 */
7803 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 int evaluate;
7808{
7809 char_u *string = NULL;
7810 int len;
7811 int cc;
7812 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007813 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814
7815 ++*arg;
7816 name = *arg;
7817 len = get_env_len(arg);
7818 if (evaluate)
7819 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007820 if (len == 0)
7821 return FAIL; /* can't be an environment variable */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007822
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007823 cc = name[len];
7824 name[len] = NUL;
7825 /* first try vim_getenv(), fast for normal environment vars */
7826 string = vim_getenv(name, &mustfree);
7827 if (string != NULL && *string != NUL)
7828 {
7829 if (!mustfree)
7830 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007832 else
7833 {
7834 if (mustfree)
7835 vim_free(string);
7836
7837 /* next try expanding things like $VIM and ${HOME} */
7838 string = expand_env_save(name - 1);
7839 if (string != NULL && *string == '$')
7840 {
7841 vim_free(string);
7842 string = NULL;
7843 }
7844 }
7845 name[len] = cc;
7846
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007847 rettv->v_type = VAR_STRING;
7848 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 }
7850
7851 return OK;
7852}
7853
7854/*
7855 * Array with names and number of arguments of all internal functions
7856 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7857 */
7858static struct fst
7859{
7860 char *f_name; /* function name */
7861 char f_min_argc; /* minimal number of arguments */
7862 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007863 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007864 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865} functions[] =
7866{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007867#ifdef FEAT_FLOAT
7868 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007869 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007870#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007871 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007872 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {"append", 2, 2, f_append},
7874 {"argc", 0, 0, f_argc},
7875 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007876 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007877#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007878 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007879 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007880 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007883 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"bufexists", 1, 1, f_bufexists},
7885 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7886 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7887 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7888 {"buflisted", 1, 1, f_buflisted},
7889 {"bufloaded", 1, 1, f_bufloaded},
7890 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007891 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"bufwinnr", 1, 1, f_bufwinnr},
7893 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007894 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007895 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007896 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007897#ifdef FEAT_FLOAT
7898 {"ceil", 1, 1, f_ceil},
7899#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007900 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007901 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007903 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007905#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007906 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007907 {"complete_add", 1, 1, f_complete_add},
7908 {"complete_check", 0, 0, f_complete_check},
7909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007911 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007912#ifdef FEAT_FLOAT
7913 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007914 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007915#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007916 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007918 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007919 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {"delete", 1, 1, f_delete},
7921 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007922 {"diff_filler", 1, 1, f_diff_filler},
7923 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007924 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007926 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"eventhandler", 0, 0, f_eventhandler},
7928 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007929 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007931#ifdef FEAT_FLOAT
7932 {"exp", 1, 1, f_exp},
7933#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007934 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007935 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007936 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7938 {"filereadable", 1, 1, f_filereadable},
7939 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007940 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007941 {"finddir", 1, 3, f_finddir},
7942 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007943#ifdef FEAT_FLOAT
7944 {"float2nr", 1, 1, f_float2nr},
7945 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007946 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007947#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007948 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"fnamemodify", 2, 2, f_fnamemodify},
7950 {"foldclosed", 1, 1, f_foldclosed},
7951 {"foldclosedend", 1, 1, f_foldclosedend},
7952 {"foldlevel", 1, 1, f_foldlevel},
7953 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007954 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007956 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007957 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007958 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007959 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007960 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {"getchar", 0, 1, f_getchar},
7962 {"getcharmod", 0, 0, f_getcharmod},
7963 {"getcmdline", 0, 0, f_getcmdline},
7964 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007965 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007967 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007968 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 {"getfsize", 1, 1, f_getfsize},
7970 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007971 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007972 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007973 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007974 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007975 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007976 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007977 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02007978 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007980 {"gettabvar", 2, 3, f_gettabvar},
7981 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {"getwinposx", 0, 0, f_getwinposx},
7983 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007984 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007985 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007986 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007988 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007989 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007990 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7992 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7993 {"histadd", 2, 2, f_histadd},
7994 {"histdel", 1, 2, f_histdel},
7995 {"histget", 1, 2, f_histget},
7996 {"histnr", 1, 1, f_histnr},
7997 {"hlID", 1, 1, f_hlID},
7998 {"hlexists", 1, 1, f_hlexists},
7999 {"hostname", 0, 0, f_hostname},
8000 {"iconv", 3, 3, f_iconv},
8001 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008002 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008003 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008005 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {"inputrestore", 0, 0, f_inputrestore},
8007 {"inputsave", 0, 0, f_inputsave},
8008 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008009 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008010 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008012 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008013 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008014 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008015 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008017 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 {"libcall", 3, 3, f_libcall},
8019 {"libcallnr", 3, 3, f_libcallnr},
8020 {"line", 1, 1, f_line},
8021 {"line2byte", 1, 1, f_line2byte},
8022 {"lispindent", 1, 1, f_lispindent},
8023 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008024#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008025 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008026 {"log10", 1, 1, f_log10},
8027#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008028#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008029 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008030#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008031 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008032 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008033 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008034 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008035 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008036 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008037 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008038 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008039 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008040 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008041 {"max", 1, 1, f_max},
8042 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008043#ifdef vim_mkdir
8044 {"mkdir", 1, 3, f_mkdir},
8045#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008046 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008047#ifdef FEAT_MZSCHEME
8048 {"mzeval", 1, 1, f_mzeval},
8049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008051 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008052 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008053 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008054#ifdef FEAT_FLOAT
8055 {"pow", 2, 2, f_pow},
8056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008058 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008059 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008060#ifdef FEAT_PYTHON3
8061 {"py3eval", 1, 1, f_py3eval},
8062#endif
8063#ifdef FEAT_PYTHON
8064 {"pyeval", 1, 1, f_pyeval},
8065#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008066 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008067 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008068 {"reltime", 0, 2, f_reltime},
8069 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"remote_expr", 2, 3, f_remote_expr},
8071 {"remote_foreground", 1, 1, f_remote_foreground},
8072 {"remote_peek", 1, 2, f_remote_peek},
8073 {"remote_read", 1, 1, f_remote_read},
8074 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008075 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008077 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008079 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008080#ifdef FEAT_FLOAT
8081 {"round", 1, 1, f_round},
8082#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008083 {"screenattr", 2, 2, f_screenattr},
8084 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008085 {"screencol", 0, 0, f_screencol},
8086 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008087 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008088 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008089 {"searchpair", 3, 7, f_searchpair},
8090 {"searchpairpos", 3, 7, f_searchpairpos},
8091 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"server2client", 2, 2, f_server2client},
8093 {"serverlist", 0, 0, f_serverlist},
8094 {"setbufvar", 3, 3, f_setbufvar},
8095 {"setcmdpos", 1, 1, f_setcmdpos},
8096 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008097 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008098 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008099 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008100 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008102 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008103 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008105#ifdef FEAT_CRYPT
8106 {"sha256", 1, 1, f_sha256},
8107#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008108 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008109 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008111#ifdef FEAT_FLOAT
8112 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008113 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008114#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008115 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008116 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008117 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008118 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008119 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008120#ifdef FEAT_FLOAT
8121 {"sqrt", 1, 1, f_sqrt},
8122 {"str2float", 1, 1, f_str2float},
8123#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008124 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008125 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008126 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127#ifdef HAVE_STRFTIME
8128 {"strftime", 1, 2, f_strftime},
8129#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008130 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008131 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"strlen", 1, 1, f_strlen},
8133 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008134 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008136 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008137 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"substitute", 4, 4, f_substitute},
8139 {"synID", 3, 3, f_synID},
8140 {"synIDattr", 2, 3, f_synIDattr},
8141 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008142 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008143 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008144 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008145 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008146 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008147 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008148 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008149 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008150 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008151#ifdef FEAT_FLOAT
8152 {"tan", 1, 1, f_tan},
8153 {"tanh", 1, 1, f_tanh},
8154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008156 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"tolower", 1, 1, f_tolower},
8158 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008159 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008160#ifdef FEAT_FLOAT
8161 {"trunc", 1, 1, f_trunc},
8162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008164 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008165 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008166 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008167 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {"virtcol", 1, 1, f_virtcol},
8169 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008170 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {"winbufnr", 1, 1, f_winbufnr},
8172 {"wincol", 0, 0, f_wincol},
8173 {"winheight", 1, 1, f_winheight},
8174 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008175 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008177 {"winrestview", 1, 1, f_winrestview},
8178 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008180 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008181 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182};
8183
8184#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8185
8186/*
8187 * Function given to ExpandGeneric() to obtain the list of internal
8188 * or user defined function names.
8189 */
8190 char_u *
8191get_function_name(xp, idx)
8192 expand_T *xp;
8193 int idx;
8194{
8195 static int intidx = -1;
8196 char_u *name;
8197
8198 if (idx == 0)
8199 intidx = -1;
8200 if (intidx < 0)
8201 {
8202 name = get_user_func_name(xp, idx);
8203 if (name != NULL)
8204 return name;
8205 }
8206 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8207 {
8208 STRCPY(IObuff, functions[intidx].f_name);
8209 STRCAT(IObuff, "(");
8210 if (functions[intidx].f_max_argc == 0)
8211 STRCAT(IObuff, ")");
8212 return IObuff;
8213 }
8214
8215 return NULL;
8216}
8217
8218/*
8219 * Function given to ExpandGeneric() to obtain the list of internal or
8220 * user defined variable or function names.
8221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 char_u *
8223get_expr_name(xp, idx)
8224 expand_T *xp;
8225 int idx;
8226{
8227 static int intidx = -1;
8228 char_u *name;
8229
8230 if (idx == 0)
8231 intidx = -1;
8232 if (intidx < 0)
8233 {
8234 name = get_function_name(xp, idx);
8235 if (name != NULL)
8236 return name;
8237 }
8238 return get_user_var_name(xp, ++intidx);
8239}
8240
8241#endif /* FEAT_CMDL_COMPL */
8242
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008243#if defined(EBCDIC) || defined(PROTO)
8244/*
8245 * Compare struct fst by function name.
8246 */
8247 static int
8248compare_func_name(s1, s2)
8249 const void *s1;
8250 const void *s2;
8251{
8252 struct fst *p1 = (struct fst *)s1;
8253 struct fst *p2 = (struct fst *)s2;
8254
8255 return STRCMP(p1->f_name, p2->f_name);
8256}
8257
8258/*
8259 * Sort the function table by function name.
8260 * The sorting of the table above is ASCII dependant.
8261 * On machines using EBCDIC we have to sort it.
8262 */
8263 static void
8264sortFunctions()
8265{
8266 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8267
8268 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8269}
8270#endif
8271
8272
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273/*
8274 * Find internal function in table above.
8275 * Return index, or -1 if not found
8276 */
8277 static int
8278find_internal_func(name)
8279 char_u *name; /* name of the function */
8280{
8281 int first = 0;
8282 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8283 int cmp;
8284 int x;
8285
8286 /*
8287 * Find the function name in the table. Binary search.
8288 */
8289 while (first <= last)
8290 {
8291 x = first + ((unsigned)(last - first) >> 1);
8292 cmp = STRCMP(name, functions[x].f_name);
8293 if (cmp < 0)
8294 last = x - 1;
8295 else if (cmp > 0)
8296 first = x + 1;
8297 else
8298 return x;
8299 }
8300 return -1;
8301}
8302
8303/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008304 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8305 * name it contains, otherwise return "name".
8306 */
8307 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008308deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008309 char_u *name;
8310 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008311 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008312{
Bram Moolenaar33570922005-01-25 22:26:29 +00008313 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008314 int cc;
8315
8316 cc = name[*lenp];
8317 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008318 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008319 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008320 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008321 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008322 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008323 {
8324 *lenp = 0;
8325 return (char_u *)""; /* just in case */
8326 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008327 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008328 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008329 }
8330
8331 return name;
8332}
8333
8334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 * Allocate a variable for the result of a function.
8336 * Return OK or FAIL.
8337 */
8338 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008339get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8340 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 char_u *name; /* name of the function */
8342 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 char_u **arg; /* argument, pointing to the '(' */
8345 linenr_T firstline; /* first line of range */
8346 linenr_T lastline; /* last line of range */
8347 int *doesrange; /* return: function handled range */
8348 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350{
8351 char_u *argp;
8352 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008353 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 int argcount = 0; /* number of arguments found */
8355
8356 /*
8357 * Get the arguments.
8358 */
8359 argp = *arg;
8360 while (argcount < MAX_FUNC_ARGS)
8361 {
8362 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8363 if (*argp == ')' || *argp == ',' || *argp == NUL)
8364 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8366 {
8367 ret = FAIL;
8368 break;
8369 }
8370 ++argcount;
8371 if (*argp != ',')
8372 break;
8373 }
8374 if (*argp == ')')
8375 ++argp;
8376 else
8377 ret = FAIL;
8378
8379 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008380 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008381 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008383 {
8384 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008385 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008386 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008387 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389
8390 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008391 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392
8393 *arg = skipwhite(argp);
8394 return ret;
8395}
8396
8397
8398/*
8399 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008400 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008401 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 */
8403 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008404call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008405 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008406 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008408 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008410 typval_T *argvars; /* vars for arguments, must have "argcount"
8411 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 linenr_T firstline; /* first line of range */
8413 linenr_T lastline; /* last line of range */
8414 int *doesrange; /* return: function handled range */
8415 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008416 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417{
8418 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419#define ERROR_UNKNOWN 0
8420#define ERROR_TOOMANY 1
8421#define ERROR_TOOFEW 2
8422#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008423#define ERROR_DICT 4
8424#define ERROR_NONE 5
8425#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 int error = ERROR_NONE;
8427 int i;
8428 int llen;
8429 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430#define FLEN_FIXED 40
8431 char_u fname_buf[FLEN_FIXED + 1];
8432 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008433 char_u *name;
8434
8435 /* Make a copy of the name, if it comes from a funcref variable it could
8436 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008437 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008438 if (name == NULL)
8439 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440
8441 /*
8442 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8443 * Change <SNR>123_name() to K_SNR 123_name().
8444 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 llen = eval_fname_script(name);
8447 if (llen > 0)
8448 {
8449 fname_buf[0] = K_SPECIAL;
8450 fname_buf[1] = KS_EXTRA;
8451 fname_buf[2] = (int)KE_SNR;
8452 i = 3;
8453 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8454 {
8455 if (current_SID <= 0)
8456 error = ERROR_SCRIPT;
8457 else
8458 {
8459 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8460 i = (int)STRLEN(fname_buf);
8461 }
8462 }
8463 if (i + STRLEN(name + llen) < FLEN_FIXED)
8464 {
8465 STRCPY(fname_buf + i, name + llen);
8466 fname = fname_buf;
8467 }
8468 else
8469 {
8470 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8471 if (fname == NULL)
8472 error = ERROR_OTHER;
8473 else
8474 {
8475 mch_memmove(fname, fname_buf, (size_t)i);
8476 STRCPY(fname + i, name + llen);
8477 }
8478 }
8479 }
8480 else
8481 fname = name;
8482
8483 *doesrange = FALSE;
8484
8485
8486 /* execute the function if no errors detected and executing */
8487 if (evaluate && error == ERROR_NONE)
8488 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008489 char_u *rfname = fname;
8490
8491 /* Ignore "g:" before a function name. */
8492 if (fname[0] == 'g' && fname[1] == ':')
8493 rfname = fname + 2;
8494
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008495 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8496 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 error = ERROR_UNKNOWN;
8498
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008499 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 {
8501 /*
8502 * User defined function.
8503 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008504 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008505
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008507 /* Trigger FuncUndefined event, may load the function. */
8508 if (fp == NULL
8509 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008510 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008511 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008513 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008514 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 }
8516#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008517 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008518 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008519 {
8520 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008521 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008522 }
8523
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 if (fp != NULL)
8525 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008526 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008528 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008530 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008532 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008533 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 else
8535 {
8536 /*
8537 * Call the user function.
8538 * Save and restore search patterns, script variables and
8539 * redo buffer.
8540 */
8541 save_search_patterns();
8542 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008543 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008544 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008545 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008546 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8547 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8548 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008549 /* Function was unreferenced while being used, free it
8550 * now. */
8551 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 restoreRedobuff();
8553 restore_search_patterns();
8554 error = ERROR_NONE;
8555 }
8556 }
8557 }
8558 else
8559 {
8560 /*
8561 * Find the function name in the table, call its implementation.
8562 */
8563 i = find_internal_func(fname);
8564 if (i >= 0)
8565 {
8566 if (argcount < functions[i].f_min_argc)
8567 error = ERROR_TOOFEW;
8568 else if (argcount > functions[i].f_max_argc)
8569 error = ERROR_TOOMANY;
8570 else
8571 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008572 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008573 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 error = ERROR_NONE;
8575 }
8576 }
8577 }
8578 /*
8579 * The function call (or "FuncUndefined" autocommand sequence) might
8580 * have been aborted by an error, an interrupt, or an explicitly thrown
8581 * exception that has not been caught so far. This situation can be
8582 * tested for by calling aborting(). For an error in an internal
8583 * function or for the "E132" error in call_user_func(), however, the
8584 * throw point at which the "force_abort" flag (temporarily reset by
8585 * emsg()) is normally updated has not been reached yet. We need to
8586 * update that flag first to make aborting() reliable.
8587 */
8588 update_force_abort();
8589 }
8590 if (error == ERROR_NONE)
8591 ret = OK;
8592
8593 /*
8594 * Report an error unless the argument evaluation or function call has been
8595 * cancelled due to an aborting error, an interrupt, or an exception.
8596 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008597 if (!aborting())
8598 {
8599 switch (error)
8600 {
8601 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008602 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008603 break;
8604 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008605 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008606 break;
8607 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008608 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008609 name);
8610 break;
8611 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008612 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008613 name);
8614 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008615 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008616 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008617 name);
8618 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008619 }
8620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 if (fname != name && fname != fname_buf)
8623 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008624 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625
8626 return ret;
8627}
8628
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008629/*
8630 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008631 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008632 */
8633 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008634emsg_funcname(ermsg, name)
8635 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008636 char_u *name;
8637{
8638 char_u *p;
8639
8640 if (*name == K_SPECIAL)
8641 p = concat_str((char_u *)"<SNR>", name + 3);
8642 else
8643 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008644 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008645 if (p != name)
8646 vim_free(p);
8647}
8648
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008649/*
8650 * Return TRUE for a non-zero Number and a non-empty String.
8651 */
8652 static int
8653non_zero_arg(argvars)
8654 typval_T *argvars;
8655{
8656 return ((argvars[0].v_type == VAR_NUMBER
8657 && argvars[0].vval.v_number != 0)
8658 || (argvars[0].v_type == VAR_STRING
8659 && argvars[0].vval.v_string != NULL
8660 && *argvars[0].vval.v_string != NUL));
8661}
8662
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663/*********************************************
8664 * Implementation of the built-in functions
8665 */
8666
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008667#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008668static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8669
8670/*
8671 * Get the float value of "argvars[0]" into "f".
8672 * Returns FAIL when the argument is not a Number or Float.
8673 */
8674 static int
8675get_float_arg(argvars, f)
8676 typval_T *argvars;
8677 float_T *f;
8678{
8679 if (argvars[0].v_type == VAR_FLOAT)
8680 {
8681 *f = argvars[0].vval.v_float;
8682 return OK;
8683 }
8684 if (argvars[0].v_type == VAR_NUMBER)
8685 {
8686 *f = (float_T)argvars[0].vval.v_number;
8687 return OK;
8688 }
8689 EMSG(_("E808: Number or Float required"));
8690 return FAIL;
8691}
8692
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008693/*
8694 * "abs(expr)" function
8695 */
8696 static void
8697f_abs(argvars, rettv)
8698 typval_T *argvars;
8699 typval_T *rettv;
8700{
8701 if (argvars[0].v_type == VAR_FLOAT)
8702 {
8703 rettv->v_type = VAR_FLOAT;
8704 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8705 }
8706 else
8707 {
8708 varnumber_T n;
8709 int error = FALSE;
8710
8711 n = get_tv_number_chk(&argvars[0], &error);
8712 if (error)
8713 rettv->vval.v_number = -1;
8714 else if (n > 0)
8715 rettv->vval.v_number = n;
8716 else
8717 rettv->vval.v_number = -n;
8718 }
8719}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008720
8721/*
8722 * "acos()" function
8723 */
8724 static void
8725f_acos(argvars, rettv)
8726 typval_T *argvars;
8727 typval_T *rettv;
8728{
8729 float_T f;
8730
8731 rettv->v_type = VAR_FLOAT;
8732 if (get_float_arg(argvars, &f) == OK)
8733 rettv->vval.v_float = acos(f);
8734 else
8735 rettv->vval.v_float = 0.0;
8736}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008737#endif
8738
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008740 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 */
8742 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008743f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008744 typval_T *argvars;
8745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746{
Bram Moolenaar33570922005-01-25 22:26:29 +00008747 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008749 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008750 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008752 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008753 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008754 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008755 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008756 }
8757 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008758 EMSG(_(e_listreq));
8759}
8760
8761/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008762 * "and(expr, expr)" function
8763 */
8764 static void
8765f_and(argvars, rettv)
8766 typval_T *argvars;
8767 typval_T *rettv;
8768{
8769 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8770 & get_tv_number_chk(&argvars[1], NULL);
8771}
8772
8773/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008774 * "append(lnum, string/list)" function
8775 */
8776 static void
8777f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008778 typval_T *argvars;
8779 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008780{
8781 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008782 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008783 list_T *l = NULL;
8784 listitem_T *li = NULL;
8785 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008786 long added = 0;
8787
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008788 /* When coming here from Insert mode, sync undo, so that this can be
8789 * undone separately from what was previously inserted. */
8790 if (u_sync_once == 2)
8791 {
8792 u_sync_once = 1; /* notify that u_sync() was called */
8793 u_sync(TRUE);
8794 }
8795
Bram Moolenaar0d660222005-01-07 21:51:51 +00008796 lnum = get_tv_lnum(argvars);
8797 if (lnum >= 0
8798 && lnum <= curbuf->b_ml.ml_line_count
8799 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008800 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008801 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008802 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008803 l = argvars[1].vval.v_list;
8804 if (l == NULL)
8805 return;
8806 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008807 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008808 for (;;)
8809 {
8810 if (l == NULL)
8811 tv = &argvars[1]; /* append a string */
8812 else if (li == NULL)
8813 break; /* end of list */
8814 else
8815 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008816 line = get_tv_string_chk(tv);
8817 if (line == NULL) /* type error */
8818 {
8819 rettv->vval.v_number = 1; /* Failed */
8820 break;
8821 }
8822 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008823 ++added;
8824 if (l == NULL)
8825 break;
8826 li = li->li_next;
8827 }
8828
8829 appended_lines_mark(lnum, added);
8830 if (curwin->w_cursor.lnum > lnum)
8831 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008833 else
8834 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835}
8836
8837/*
8838 * "argc()" function
8839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008842 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846}
8847
8848/*
8849 * "argidx()" function
8850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008852f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008853 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857}
8858
8859/*
8860 * "argv(nr)" function
8861 */
8862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008863f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008864 typval_T *argvars;
8865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
8867 int idx;
8868
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008869 if (argvars[0].v_type != VAR_UNKNOWN)
8870 {
8871 idx = get_tv_number_chk(&argvars[0], NULL);
8872 if (idx >= 0 && idx < ARGCOUNT)
8873 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8874 else
8875 rettv->vval.v_string = NULL;
8876 rettv->v_type = VAR_STRING;
8877 }
8878 else if (rettv_list_alloc(rettv) == OK)
8879 for (idx = 0; idx < ARGCOUNT; ++idx)
8880 list_append_string(rettv->vval.v_list,
8881 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882}
8883
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008884#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008885/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008886 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008887 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008888 static void
8889f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008890 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008891 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008892{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008893 float_T f;
8894
8895 rettv->v_type = VAR_FLOAT;
8896 if (get_float_arg(argvars, &f) == OK)
8897 rettv->vval.v_float = asin(f);
8898 else
8899 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008900}
8901
8902/*
8903 * "atan()" function
8904 */
8905 static void
8906f_atan(argvars, rettv)
8907 typval_T *argvars;
8908 typval_T *rettv;
8909{
8910 float_T f;
8911
8912 rettv->v_type = VAR_FLOAT;
8913 if (get_float_arg(argvars, &f) == OK)
8914 rettv->vval.v_float = atan(f);
8915 else
8916 rettv->vval.v_float = 0.0;
8917}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008918
8919/*
8920 * "atan2()" function
8921 */
8922 static void
8923f_atan2(argvars, rettv)
8924 typval_T *argvars;
8925 typval_T *rettv;
8926{
8927 float_T fx, fy;
8928
8929 rettv->v_type = VAR_FLOAT;
8930 if (get_float_arg(argvars, &fx) == OK
8931 && get_float_arg(&argvars[1], &fy) == OK)
8932 rettv->vval.v_float = atan2(fx, fy);
8933 else
8934 rettv->vval.v_float = 0.0;
8935}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008936#endif
8937
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938/*
8939 * "browse(save, title, initdir, default)" function
8940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008942f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008943 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945{
8946#ifdef FEAT_BROWSE
8947 int save;
8948 char_u *title;
8949 char_u *initdir;
8950 char_u *defname;
8951 char_u buf[NUMBUFLEN];
8952 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008953 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008955 save = get_tv_number_chk(&argvars[0], &error);
8956 title = get_tv_string_chk(&argvars[1]);
8957 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8958 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008960 if (error || title == NULL || initdir == NULL || defname == NULL)
8961 rettv->vval.v_string = NULL;
8962 else
8963 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008964 do_browse(save ? BROWSE_SAVE : 0,
8965 title, defname, NULL, initdir, NULL, curbuf);
8966#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008968#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008970}
8971
8972/*
8973 * "browsedir(title, initdir)" function
8974 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008977 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008979{
8980#ifdef FEAT_BROWSE
8981 char_u *title;
8982 char_u *initdir;
8983 char_u buf[NUMBUFLEN];
8984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 title = get_tv_string_chk(&argvars[0]);
8986 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008987
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008988 if (title == NULL || initdir == NULL)
8989 rettv->vval.v_string = NULL;
8990 else
8991 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008992 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997}
8998
Bram Moolenaar33570922005-01-25 22:26:29 +00008999static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009000
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001/*
9002 * Find a buffer by number or exact name.
9003 */
9004 static buf_T *
9005find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009006 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
9008 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009010 if (avar->v_type == VAR_NUMBER)
9011 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009012 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009014 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009015 if (buf == NULL)
9016 {
9017 /* No full path name match, try a match with a URL or a "nofile"
9018 * buffer, these don't use the full path. */
9019 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9020 if (buf->b_fname != NULL
9021 && (path_with_url(buf->b_fname)
9022#ifdef FEAT_QUICKFIX
9023 || bt_nofile(buf)
9024#endif
9025 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009026 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009027 break;
9028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 }
9030 return buf;
9031}
9032
9033/*
9034 * "bufexists(expr)" function
9035 */
9036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009037f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009038 typval_T *argvars;
9039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042}
9043
9044/*
9045 * "buflisted(expr)" function
9046 */
9047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009049 typval_T *argvars;
9050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051{
9052 buf_T *buf;
9053
9054 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009055 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056}
9057
9058/*
9059 * "bufloaded(expr)" function
9060 */
9061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009063 typval_T *argvars;
9064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065{
9066 buf_T *buf;
9067
9068 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070}
9071
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009072static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009073
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074/*
9075 * Get buffer by number or pattern.
9076 */
9077 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009078get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009079 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009080 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 int save_magic;
9084 char_u *save_cpo;
9085 buf_T *buf;
9086
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 if (tv->v_type == VAR_NUMBER)
9088 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009089 if (tv->v_type != VAR_STRING)
9090 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 if (name == NULL || *name == NUL)
9092 return curbuf;
9093 if (name[0] == '$' && name[1] == NUL)
9094 return lastbuf;
9095
9096 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9097 save_magic = p_magic;
9098 p_magic = TRUE;
9099 save_cpo = p_cpo;
9100 p_cpo = (char_u *)"";
9101
9102 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009103 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104
9105 p_magic = save_magic;
9106 p_cpo = save_cpo;
9107
9108 /* If not found, try expanding the name, like done for bufexists(). */
9109 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111
9112 return buf;
9113}
9114
9115/*
9116 * "bufname(expr)" function
9117 */
9118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009119f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009120 typval_T *argvars;
9121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122{
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], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009128 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009130 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009132 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 --emsg_off;
9134}
9135
9136/*
9137 * "bufnr(expr)" function
9138 */
9139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009141 typval_T *argvars;
9142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143{
9144 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009145 int error = FALSE;
9146 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009148 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009150 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009151 --emsg_off;
9152
9153 /* If the buffer isn't found and the second argument is not zero create a
9154 * new buffer. */
9155 if (buf == NULL
9156 && argvars[1].v_type != VAR_UNKNOWN
9157 && get_tv_number_chk(&argvars[1], &error) != 0
9158 && !error
9159 && (name = get_tv_string_chk(&argvars[0])) != NULL
9160 && !error)
9161 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9162
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167}
9168
9169/*
9170 * "bufwinnr(nr)" function
9171 */
9172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009174 typval_T *argvars;
9175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176{
9177#ifdef FEAT_WINDOWS
9178 win_T *wp;
9179 int winnr = 0;
9180#endif
9181 buf_T *buf;
9182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009185 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186#ifdef FEAT_WINDOWS
9187 for (wp = firstwin; wp; wp = wp->w_next)
9188 {
9189 ++winnr;
9190 if (wp->w_buffer == buf)
9191 break;
9192 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009193 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196#endif
9197 --emsg_off;
9198}
9199
9200/*
9201 * "byte2line(byte)" function
9202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009205 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207{
9208#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009209 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210#else
9211 long boff = 0;
9212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009213 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009215 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009217 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 (linenr_T)0, &boff);
9219#endif
9220}
9221
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009222 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009223byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009224 typval_T *argvars;
9225 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009226 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009227{
9228#ifdef FEAT_MBYTE
9229 char_u *t;
9230#endif
9231 char_u *str;
9232 long idx;
9233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009234 str = get_tv_string_chk(&argvars[0]);
9235 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009236 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009238 return;
9239
9240#ifdef FEAT_MBYTE
9241 t = str;
9242 for ( ; idx > 0; idx--)
9243 {
9244 if (*t == NUL) /* EOL reached */
9245 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009246 if (enc_utf8 && comp)
9247 t += utf_ptr2len(t);
9248 else
9249 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009250 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009251 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009252#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009253 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009254 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009255#endif
9256}
9257
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009258/*
9259 * "byteidx()" function
9260 */
9261 static void
9262f_byteidx(argvars, rettv)
9263 typval_T *argvars;
9264 typval_T *rettv;
9265{
9266 byteidx(argvars, rettv, FALSE);
9267}
9268
9269/*
9270 * "byteidxcomp()" function
9271 */
9272 static void
9273f_byteidxcomp(argvars, rettv)
9274 typval_T *argvars;
9275 typval_T *rettv;
9276{
9277 byteidx(argvars, rettv, TRUE);
9278}
9279
Bram Moolenaardb913952012-06-29 12:54:53 +02009280 int
9281func_call(name, args, selfdict, rettv)
9282 char_u *name;
9283 typval_T *args;
9284 dict_T *selfdict;
9285 typval_T *rettv;
9286{
9287 listitem_T *item;
9288 typval_T argv[MAX_FUNC_ARGS + 1];
9289 int argc = 0;
9290 int dummy;
9291 int r = 0;
9292
9293 for (item = args->vval.v_list->lv_first; item != NULL;
9294 item = item->li_next)
9295 {
9296 if (argc == MAX_FUNC_ARGS)
9297 {
9298 EMSG(_("E699: Too many arguments"));
9299 break;
9300 }
9301 /* Make a copy of each argument. This is needed to be able to set
9302 * v_lock to VAR_FIXED in the copy without changing the original list.
9303 */
9304 copy_tv(&item->li_tv, &argv[argc++]);
9305 }
9306
9307 if (item == NULL)
9308 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9309 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9310 &dummy, TRUE, selfdict);
9311
9312 /* Free the arguments. */
9313 while (argc > 0)
9314 clear_tv(&argv[--argc]);
9315
9316 return r;
9317}
9318
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009319/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009320 * "call(func, arglist)" function
9321 */
9322 static void
9323f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009324 typval_T *argvars;
9325 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009326{
9327 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009328 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009329
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009330 if (argvars[1].v_type != VAR_LIST)
9331 {
9332 EMSG(_(e_listreq));
9333 return;
9334 }
9335 if (argvars[1].vval.v_list == NULL)
9336 return;
9337
9338 if (argvars[0].v_type == VAR_FUNC)
9339 func = argvars[0].vval.v_string;
9340 else
9341 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009342 if (*func == NUL)
9343 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009344
Bram Moolenaare9a41262005-01-15 22:18:47 +00009345 if (argvars[2].v_type != VAR_UNKNOWN)
9346 {
9347 if (argvars[2].v_type != VAR_DICT)
9348 {
9349 EMSG(_(e_dictreq));
9350 return;
9351 }
9352 selfdict = argvars[2].vval.v_dict;
9353 }
9354
Bram Moolenaardb913952012-06-29 12:54:53 +02009355 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009356}
9357
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009358#ifdef FEAT_FLOAT
9359/*
9360 * "ceil({float})" function
9361 */
9362 static void
9363f_ceil(argvars, rettv)
9364 typval_T *argvars;
9365 typval_T *rettv;
9366{
9367 float_T f;
9368
9369 rettv->v_type = VAR_FLOAT;
9370 if (get_float_arg(argvars, &f) == OK)
9371 rettv->vval.v_float = ceil(f);
9372 else
9373 rettv->vval.v_float = 0.0;
9374}
9375#endif
9376
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009377/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009378 * "changenr()" function
9379 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009380 static void
9381f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009382 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009383 typval_T *rettv;
9384{
9385 rettv->vval.v_number = curbuf->b_u_seq_cur;
9386}
9387
9388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 * "char2nr(string)" function
9390 */
9391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009392f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009393 typval_T *argvars;
9394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395{
9396#ifdef FEAT_MBYTE
9397 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009398 {
9399 int utf8 = 0;
9400
9401 if (argvars[1].v_type != VAR_UNKNOWN)
9402 utf8 = get_tv_number_chk(&argvars[1], NULL);
9403
9404 if (utf8)
9405 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9406 else
9407 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 else
9410#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009411 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412}
9413
9414/*
9415 * "cindent(lnum)" function
9416 */
9417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009419 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421{
9422#ifdef FEAT_CINDENT
9423 pos_T pos;
9424 linenr_T lnum;
9425
9426 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9429 {
9430 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009431 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 curwin->w_cursor = pos;
9433 }
9434 else
9435#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437}
9438
9439/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009440 * "clearmatches()" function
9441 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009442 static void
9443f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009444 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009445 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009446{
9447#ifdef FEAT_SEARCH_EXTRA
9448 clear_matches(curwin);
9449#endif
9450}
9451
9452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 * "col(string)" function
9454 */
9455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009457 typval_T *argvars;
9458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459{
9460 colnr_T col = 0;
9461 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009462 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009464 fp = var2fpos(&argvars[0], FALSE, &fnum);
9465 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 {
9467 if (fp->col == MAXCOL)
9468 {
9469 /* '> can be MAXCOL, get the length of the line then */
9470 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009471 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 else
9473 col = MAXCOL;
9474 }
9475 else
9476 {
9477 col = fp->col + 1;
9478#ifdef FEAT_VIRTUALEDIT
9479 /* col(".") when the cursor is on the NUL at the end of the line
9480 * because of "coladd" can be seen as an extra column. */
9481 if (virtual_active() && fp == &curwin->w_cursor)
9482 {
9483 char_u *p = ml_get_cursor();
9484
9485 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9486 curwin->w_virtcol - curwin->w_cursor.coladd))
9487 {
9488# ifdef FEAT_MBYTE
9489 int l;
9490
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009491 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 col += l;
9493# else
9494 if (*p != NUL && p[1] == NUL)
9495 ++col;
9496# endif
9497 }
9498 }
9499#endif
9500 }
9501 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503}
9504
Bram Moolenaar572cb562005-08-05 21:35:02 +00009505#if defined(FEAT_INS_EXPAND)
9506/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009507 * "complete()" function
9508 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009509 static void
9510f_complete(argvars, rettv)
9511 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009512 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009513{
9514 int startcol;
9515
9516 if ((State & INSERT) == 0)
9517 {
9518 EMSG(_("E785: complete() can only be used in Insert mode"));
9519 return;
9520 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009521
9522 /* Check for undo allowed here, because if something was already inserted
9523 * the line was already saved for undo and this check isn't done. */
9524 if (!undo_allowed())
9525 return;
9526
Bram Moolenaarade00832006-03-10 21:46:58 +00009527 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9528 {
9529 EMSG(_(e_invarg));
9530 return;
9531 }
9532
9533 startcol = get_tv_number_chk(&argvars[0], NULL);
9534 if (startcol <= 0)
9535 return;
9536
9537 set_completion(startcol - 1, argvars[1].vval.v_list);
9538}
9539
9540/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009541 * "complete_add()" function
9542 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009543 static void
9544f_complete_add(argvars, rettv)
9545 typval_T *argvars;
9546 typval_T *rettv;
9547{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009548 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009549}
9550
9551/*
9552 * "complete_check()" function
9553 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009554 static void
9555f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009556 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009557 typval_T *rettv;
9558{
9559 int saved = RedrawingDisabled;
9560
9561 RedrawingDisabled = 0;
9562 ins_compl_check_keys(0);
9563 rettv->vval.v_number = compl_interrupted;
9564 RedrawingDisabled = saved;
9565}
9566#endif
9567
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568/*
9569 * "confirm(message, buttons[, default [, type]])" function
9570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009573 typval_T *argvars UNUSED;
9574 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575{
9576#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9577 char_u *message;
9578 char_u *buttons = NULL;
9579 char_u buf[NUMBUFLEN];
9580 char_u buf2[NUMBUFLEN];
9581 int def = 1;
9582 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 char_u *typestr;
9584 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009586 message = get_tv_string_chk(&argvars[0]);
9587 if (message == NULL)
9588 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009589 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009591 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9592 if (buttons == NULL)
9593 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009594 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009596 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009597 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009599 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9600 if (typestr == NULL)
9601 error = TRUE;
9602 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009604 switch (TOUPPER_ASC(*typestr))
9605 {
9606 case 'E': type = VIM_ERROR; break;
9607 case 'Q': type = VIM_QUESTION; break;
9608 case 'I': type = VIM_INFO; break;
9609 case 'W': type = VIM_WARNING; break;
9610 case 'G': type = VIM_GENERIC; break;
9611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 }
9613 }
9614 }
9615 }
9616
9617 if (buttons == NULL || *buttons == NUL)
9618 buttons = (char_u *)_("&Ok");
9619
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009620 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009621 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009622 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623#endif
9624}
9625
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009626/*
9627 * "copy()" function
9628 */
9629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009631 typval_T *argvars;
9632 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009633{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009634 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009635}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009637#ifdef FEAT_FLOAT
9638/*
9639 * "cos()" function
9640 */
9641 static void
9642f_cos(argvars, rettv)
9643 typval_T *argvars;
9644 typval_T *rettv;
9645{
9646 float_T f;
9647
9648 rettv->v_type = VAR_FLOAT;
9649 if (get_float_arg(argvars, &f) == OK)
9650 rettv->vval.v_float = cos(f);
9651 else
9652 rettv->vval.v_float = 0.0;
9653}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009654
9655/*
9656 * "cosh()" function
9657 */
9658 static void
9659f_cosh(argvars, rettv)
9660 typval_T *argvars;
9661 typval_T *rettv;
9662{
9663 float_T f;
9664
9665 rettv->v_type = VAR_FLOAT;
9666 if (get_float_arg(argvars, &f) == OK)
9667 rettv->vval.v_float = cosh(f);
9668 else
9669 rettv->vval.v_float = 0.0;
9670}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009671#endif
9672
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009674 * "count()" function
9675 */
9676 static void
9677f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009678 typval_T *argvars;
9679 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009680{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009681 long n = 0;
9682 int ic = FALSE;
9683
Bram Moolenaare9a41262005-01-15 22:18:47 +00009684 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009685 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009686 listitem_T *li;
9687 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009688 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009689
Bram Moolenaare9a41262005-01-15 22:18:47 +00009690 if ((l = argvars[0].vval.v_list) != NULL)
9691 {
9692 li = l->lv_first;
9693 if (argvars[2].v_type != VAR_UNKNOWN)
9694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009695 int error = FALSE;
9696
9697 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009698 if (argvars[3].v_type != VAR_UNKNOWN)
9699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009700 idx = get_tv_number_chk(&argvars[3], &error);
9701 if (!error)
9702 {
9703 li = list_find(l, idx);
9704 if (li == NULL)
9705 EMSGN(_(e_listidx), idx);
9706 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009707 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009708 if (error)
9709 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009710 }
9711
9712 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009713 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009714 ++n;
9715 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009716 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009717 else if (argvars[0].v_type == VAR_DICT)
9718 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009719 int todo;
9720 dict_T *d;
9721 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009722
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009723 if ((d = argvars[0].vval.v_dict) != NULL)
9724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009725 int error = FALSE;
9726
Bram Moolenaare9a41262005-01-15 22:18:47 +00009727 if (argvars[2].v_type != VAR_UNKNOWN)
9728 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009729 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009730 if (argvars[3].v_type != VAR_UNKNOWN)
9731 EMSG(_(e_invarg));
9732 }
9733
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009734 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009735 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009736 {
9737 if (!HASHITEM_EMPTY(hi))
9738 {
9739 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009740 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009741 ++n;
9742 }
9743 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009744 }
9745 }
9746 else
9747 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009748 rettv->vval.v_number = n;
9749}
9750
9751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9753 *
9754 * Checks the existence of a cscope connection.
9755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009757f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009758 typval_T *argvars UNUSED;
9759 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760{
9761#ifdef FEAT_CSCOPE
9762 int num = 0;
9763 char_u *dbpath = NULL;
9764 char_u *prepend = NULL;
9765 char_u buf[NUMBUFLEN];
9766
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009767 if (argvars[0].v_type != VAR_UNKNOWN
9768 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009770 num = (int)get_tv_number(&argvars[0]);
9771 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009772 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009773 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 }
9775
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777#endif
9778}
9779
9780/*
9781 * "cursor(lnum, col)" function
9782 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009783 * Moves the cursor to the specified line and column.
9784 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009788 typval_T *argvars;
9789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790{
9791 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009792#ifdef FEAT_VIRTUALEDIT
9793 long coladd = 0;
9794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009796 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009797 if (argvars[1].v_type == VAR_UNKNOWN)
9798 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009799 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009800
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009801 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009802 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009803 line = pos.lnum;
9804 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009805#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009806 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009807#endif
9808 }
9809 else
9810 {
9811 line = get_tv_lnum(argvars);
9812 col = get_tv_number_chk(&argvars[1], NULL);
9813#ifdef FEAT_VIRTUALEDIT
9814 if (argvars[2].v_type != VAR_UNKNOWN)
9815 coladd = get_tv_number_chk(&argvars[2], NULL);
9816#endif
9817 }
9818 if (line < 0 || col < 0
9819#ifdef FEAT_VIRTUALEDIT
9820 || coladd < 0
9821#endif
9822 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009823 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 if (line > 0)
9825 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 if (col > 0)
9827 curwin->w_cursor.col = col - 1;
9828#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009829 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830#endif
9831
9832 /* Make sure the cursor is in a valid position. */
9833 check_cursor();
9834#ifdef FEAT_MBYTE
9835 /* Correct cursor for multi-byte character. */
9836 if (has_mbyte)
9837 mb_adjust_cursor();
9838#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009839
9840 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009841 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842}
9843
9844/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009845 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 */
9847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009849 typval_T *argvars;
9850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009852 int noref = 0;
9853
9854 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009855 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009856 if (noref < 0 || noref > 1)
9857 EMSG(_(e_invarg));
9858 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009859 {
9860 current_copyID += COPYID_INC;
9861 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863}
9864
9865/*
9866 * "delete()" function
9867 */
9868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009869f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009870 typval_T *argvars;
9871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872{
9873 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877}
9878
9879/*
9880 * "did_filetype()" function
9881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009883f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009884 typval_T *argvars UNUSED;
9885 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886{
9887#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009888 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889#endif
9890}
9891
9892/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009893 * "diff_filler()" function
9894 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009896f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009897 typval_T *argvars UNUSED;
9898 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009899{
9900#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009902#endif
9903}
9904
9905/*
9906 * "diff_hlID()" function
9907 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009909f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009910 typval_T *argvars UNUSED;
9911 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009912{
9913#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009915 static linenr_T prev_lnum = 0;
9916 static int changedtick = 0;
9917 static int fnum = 0;
9918 static int change_start = 0;
9919 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009920 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009921 int filler_lines;
9922 int col;
9923
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009924 if (lnum < 0) /* ignore type error in {lnum} arg */
9925 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009926 if (lnum != prev_lnum
9927 || changedtick != curbuf->b_changedtick
9928 || fnum != curbuf->b_fnum)
9929 {
9930 /* New line, buffer, change: need to get the values. */
9931 filler_lines = diff_check(curwin, lnum);
9932 if (filler_lines < 0)
9933 {
9934 if (filler_lines == -1)
9935 {
9936 change_start = MAXCOL;
9937 change_end = -1;
9938 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9939 hlID = HLF_ADD; /* added line */
9940 else
9941 hlID = HLF_CHD; /* changed line */
9942 }
9943 else
9944 hlID = HLF_ADD; /* added line */
9945 }
9946 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009947 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009948 prev_lnum = lnum;
9949 changedtick = curbuf->b_changedtick;
9950 fnum = curbuf->b_fnum;
9951 }
9952
9953 if (hlID == HLF_CHD || hlID == HLF_TXD)
9954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009955 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009956 if (col >= change_start && col <= change_end)
9957 hlID = HLF_TXD; /* changed text */
9958 else
9959 hlID = HLF_CHD; /* changed line */
9960 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009961 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009962#endif
9963}
9964
9965/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009966 * "empty({expr})" function
9967 */
9968 static void
9969f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009970 typval_T *argvars;
9971 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009972{
9973 int n;
9974
9975 switch (argvars[0].v_type)
9976 {
9977 case VAR_STRING:
9978 case VAR_FUNC:
9979 n = argvars[0].vval.v_string == NULL
9980 || *argvars[0].vval.v_string == NUL;
9981 break;
9982 case VAR_NUMBER:
9983 n = argvars[0].vval.v_number == 0;
9984 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009985#ifdef FEAT_FLOAT
9986 case VAR_FLOAT:
9987 n = argvars[0].vval.v_float == 0.0;
9988 break;
9989#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009990 case VAR_LIST:
9991 n = argvars[0].vval.v_list == NULL
9992 || argvars[0].vval.v_list->lv_first == NULL;
9993 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009994 case VAR_DICT:
9995 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009996 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009997 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009998 default:
9999 EMSG2(_(e_intern2), "f_empty()");
10000 n = 0;
10001 }
10002
10003 rettv->vval.v_number = n;
10004}
10005
10006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 * "escape({string}, {chars})" function
10008 */
10009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010010f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010011 typval_T *argvars;
10012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013{
10014 char_u buf[NUMBUFLEN];
10015
Bram Moolenaar758711c2005-02-02 23:11:38 +000010016 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10017 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010018 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019}
10020
10021/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010022 * "eval()" function
10023 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010024 static void
10025f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010026 typval_T *argvars;
10027 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010028{
10029 char_u *s;
10030
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010031 s = get_tv_string_chk(&argvars[0]);
10032 if (s != NULL)
10033 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010034
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010035 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10036 {
10037 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010038 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010039 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010040 else if (*s != NUL)
10041 EMSG(_(e_trailing));
10042}
10043
10044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 * "eventhandler()" function
10046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010049 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053}
10054
10055/*
10056 * "executable()" function
10057 */
10058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010060 typval_T *argvars;
10061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010063 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10064}
10065
10066/*
10067 * "exepath()" function
10068 */
10069 static void
10070f_exepath(argvars, rettv)
10071 typval_T *argvars;
10072 typval_T *rettv;
10073{
10074 char_u *p = NULL;
10075
10076 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10077 rettv->v_type = VAR_STRING;
10078 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079}
10080
10081/*
10082 * "exists()" function
10083 */
10084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010085f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010086 typval_T *argvars;
10087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088{
10089 char_u *p;
10090 char_u *name;
10091 int n = FALSE;
10092 int len = 0;
10093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 if (*p == '$') /* environment variable */
10096 {
10097 /* first try "normal" environment variables (fast) */
10098 if (mch_getenv(p + 1) != NULL)
10099 n = TRUE;
10100 else
10101 {
10102 /* try expanding things like $VIM and ${HOME} */
10103 p = expand_env_save(p);
10104 if (p != NULL && *p != '$')
10105 n = TRUE;
10106 vim_free(p);
10107 }
10108 }
10109 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010110 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010112 if (*skipwhite(p) != NUL)
10113 n = FALSE; /* trailing garbage */
10114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 else if (*p == '*') /* internal or user defined function */
10116 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010117 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 }
10119 else if (*p == ':')
10120 {
10121 n = cmd_exists(p + 1);
10122 }
10123 else if (*p == '#')
10124 {
10125#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010126 if (p[1] == '#')
10127 n = autocmd_supported(p + 2);
10128 else
10129 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130#endif
10131 }
10132 else /* internal variable */
10133 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010134 char_u *tofree;
10135 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010137 /* get_name_len() takes care of expanding curly braces */
10138 name = p;
10139 len = get_name_len(&p, &tofree, TRUE, FALSE);
10140 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010142 if (tofree != NULL)
10143 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010144 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010145 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010147 /* handle d.key, l[idx], f(expr) */
10148 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10149 if (n)
10150 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 }
10152 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010153 if (*p != NUL)
10154 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010156 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 }
10158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160}
10161
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010162#ifdef FEAT_FLOAT
10163/*
10164 * "exp()" function
10165 */
10166 static void
10167f_exp(argvars, rettv)
10168 typval_T *argvars;
10169 typval_T *rettv;
10170{
10171 float_T f;
10172
10173 rettv->v_type = VAR_FLOAT;
10174 if (get_float_arg(argvars, &f) == OK)
10175 rettv->vval.v_float = exp(f);
10176 else
10177 rettv->vval.v_float = 0.0;
10178}
10179#endif
10180
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181/*
10182 * "expand()" function
10183 */
10184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010185f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010186 typval_T *argvars;
10187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188{
10189 char_u *s;
10190 int len;
10191 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010192 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010195 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010197 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010198 if (argvars[1].v_type != VAR_UNKNOWN
10199 && argvars[2].v_type != VAR_UNKNOWN
10200 && get_tv_number_chk(&argvars[2], &error)
10201 && !error)
10202 {
10203 rettv->v_type = VAR_LIST;
10204 rettv->vval.v_list = NULL;
10205 }
10206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010207 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 if (*s == '%' || *s == '#' || *s == '<')
10209 {
10210 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010211 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010213 if (rettv->v_type == VAR_LIST)
10214 {
10215 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10216 list_append_string(rettv->vval.v_list, result, -1);
10217 else
10218 vim_free(result);
10219 }
10220 else
10221 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 }
10223 else
10224 {
10225 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010226 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010227 if (argvars[1].v_type != VAR_UNKNOWN
10228 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010229 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010230 if (!error)
10231 {
10232 ExpandInit(&xpc);
10233 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010234 if (p_wic)
10235 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010236 if (rettv->v_type == VAR_STRING)
10237 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10238 options, WILD_ALL);
10239 else if (rettv_list_alloc(rettv) != FAIL)
10240 {
10241 int i;
10242
10243 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10244 for (i = 0; i < xpc.xp_numfiles; i++)
10245 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10246 ExpandCleanup(&xpc);
10247 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010248 }
10249 else
10250 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 }
10252}
10253
10254/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010255 * Go over all entries in "d2" and add them to "d1".
10256 * When "action" is "error" then a duplicate key is an error.
10257 * When "action" is "force" then a duplicate key is overwritten.
10258 * Otherwise duplicate keys are ignored ("action" is "keep").
10259 */
10260 void
10261dict_extend(d1, d2, action)
10262 dict_T *d1;
10263 dict_T *d2;
10264 char_u *action;
10265{
10266 dictitem_T *di1;
10267 hashitem_T *hi2;
10268 int todo;
10269
10270 todo = (int)d2->dv_hashtab.ht_used;
10271 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10272 {
10273 if (!HASHITEM_EMPTY(hi2))
10274 {
10275 --todo;
10276 di1 = dict_find(d1, hi2->hi_key, -1);
10277 if (d1->dv_scope != 0)
10278 {
10279 /* Disallow replacing a builtin function in l: and g:.
10280 * Check the key to be valid when adding to any
10281 * scope. */
10282 if (d1->dv_scope == VAR_DEF_SCOPE
10283 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10284 && var_check_func_name(hi2->hi_key,
10285 di1 == NULL))
10286 break;
10287 if (!valid_varname(hi2->hi_key))
10288 break;
10289 }
10290 if (di1 == NULL)
10291 {
10292 di1 = dictitem_copy(HI2DI(hi2));
10293 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10294 dictitem_free(di1);
10295 }
10296 else if (*action == 'e')
10297 {
10298 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10299 break;
10300 }
10301 else if (*action == 'f' && HI2DI(hi2) != di1)
10302 {
10303 clear_tv(&di1->di_tv);
10304 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10305 }
10306 }
10307 }
10308}
10309
10310/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010311 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010312 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010313 */
10314 static void
10315f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010316 typval_T *argvars;
10317 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010318{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010319 char *arg_errmsg = N_("extend() argument");
10320
Bram Moolenaare9a41262005-01-15 22:18:47 +000010321 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010322 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010323 list_T *l1, *l2;
10324 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010325 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010326 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010327
Bram Moolenaare9a41262005-01-15 22:18:47 +000010328 l1 = argvars[0].vval.v_list;
10329 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010330 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010331 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010332 {
10333 if (argvars[2].v_type != VAR_UNKNOWN)
10334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010335 before = get_tv_number_chk(&argvars[2], &error);
10336 if (error)
10337 return; /* type error; errmsg already given */
10338
Bram Moolenaar758711c2005-02-02 23:11:38 +000010339 if (before == l1->lv_len)
10340 item = NULL;
10341 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010342 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010343 item = list_find(l1, before);
10344 if (item == NULL)
10345 {
10346 EMSGN(_(e_listidx), before);
10347 return;
10348 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010349 }
10350 }
10351 else
10352 item = NULL;
10353 list_extend(l1, l2, item);
10354
Bram Moolenaare9a41262005-01-15 22:18:47 +000010355 copy_tv(&argvars[0], rettv);
10356 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010357 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010358 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10359 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010360 dict_T *d1, *d2;
10361 char_u *action;
10362 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010363
10364 d1 = argvars[0].vval.v_dict;
10365 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010366 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010367 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010368 {
10369 /* Check the third argument. */
10370 if (argvars[2].v_type != VAR_UNKNOWN)
10371 {
10372 static char *(av[]) = {"keep", "force", "error"};
10373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010374 action = get_tv_string_chk(&argvars[2]);
10375 if (action == NULL)
10376 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010377 for (i = 0; i < 3; ++i)
10378 if (STRCMP(action, av[i]) == 0)
10379 break;
10380 if (i == 3)
10381 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010382 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010383 return;
10384 }
10385 }
10386 else
10387 action = (char_u *)"force";
10388
Bram Moolenaara9922d62013-05-30 13:01:18 +020010389 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010390
Bram Moolenaare9a41262005-01-15 22:18:47 +000010391 copy_tv(&argvars[0], rettv);
10392 }
10393 }
10394 else
10395 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010396}
10397
10398/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010399 * "feedkeys()" function
10400 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010401 static void
10402f_feedkeys(argvars, rettv)
10403 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010404 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010405{
10406 int remap = TRUE;
10407 char_u *keys, *flags;
10408 char_u nbuf[NUMBUFLEN];
10409 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010410 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010411
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010412 /* This is not allowed in the sandbox. If the commands would still be
10413 * executed in the sandbox it would be OK, but it probably happens later,
10414 * when "sandbox" is no longer set. */
10415 if (check_secure())
10416 return;
10417
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010418 keys = get_tv_string(&argvars[0]);
10419 if (*keys != NUL)
10420 {
10421 if (argvars[1].v_type != VAR_UNKNOWN)
10422 {
10423 flags = get_tv_string_buf(&argvars[1], nbuf);
10424 for ( ; *flags != NUL; ++flags)
10425 {
10426 switch (*flags)
10427 {
10428 case 'n': remap = FALSE; break;
10429 case 'm': remap = TRUE; break;
10430 case 't': typed = TRUE; break;
10431 }
10432 }
10433 }
10434
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010435 /* Need to escape K_SPECIAL and CSI before putting the string in the
10436 * typeahead buffer. */
10437 keys_esc = vim_strsave_escape_csi(keys);
10438 if (keys_esc != NULL)
10439 {
10440 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010441 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010442 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010443 if (vgetc_busy)
10444 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010445 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010446 }
10447}
10448
10449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 * "filereadable()" function
10451 */
10452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010453f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010454 typval_T *argvars;
10455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010457 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 char_u *p;
10459 int n;
10460
Bram Moolenaarc236c162008-07-13 17:41:49 +000010461#ifndef O_NONBLOCK
10462# define O_NONBLOCK 0
10463#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010465 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10466 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 {
10468 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010469 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 }
10471 else
10472 n = FALSE;
10473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010474 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475}
10476
10477/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010478 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 * rights to write into.
10480 */
10481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010482f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010483 typval_T *argvars;
10484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010486 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487}
10488
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010489static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010490
10491 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010492findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010493 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010494 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010495 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010496{
10497#ifdef FEAT_SEARCHPATH
10498 char_u *fname;
10499 char_u *fresult = NULL;
10500 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10501 char_u *p;
10502 char_u pathbuf[NUMBUFLEN];
10503 int count = 1;
10504 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010505 int error = FALSE;
10506#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010507
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010508 rettv->vval.v_string = NULL;
10509 rettv->v_type = VAR_STRING;
10510
10511#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010513
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010514 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010515 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10517 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010518 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 else
10520 {
10521 if (*p != NUL)
10522 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010524 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010525 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010526 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010527 }
10528
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010529 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10530 error = TRUE;
10531
10532 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010534 do
10535 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010536 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010537 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010538 fresult = find_file_in_path_option(first ? fname : NULL,
10539 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010540 0, first, path,
10541 find_what,
10542 curbuf->b_ffname,
10543 find_what == FINDFILE_DIR
10544 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010546
10547 if (fresult != NULL && rettv->v_type == VAR_LIST)
10548 list_append_string(rettv->vval.v_list, fresult, -1);
10549
10550 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010551 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010552
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010553 if (rettv->v_type == VAR_STRING)
10554 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010555#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010556}
10557
Bram Moolenaar33570922005-01-25 22:26:29 +000010558static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10559static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010560
10561/*
10562 * Implementation of map() and filter().
10563 */
10564 static void
10565filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 typval_T *argvars;
10567 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010568 int map;
10569{
10570 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010571 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010572 listitem_T *li, *nli;
10573 list_T *l = NULL;
10574 dictitem_T *di;
10575 hashtab_T *ht;
10576 hashitem_T *hi;
10577 dict_T *d = NULL;
10578 typval_T save_val;
10579 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010581 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010582 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10583 char *arg_errmsg = (map ? N_("map() argument")
10584 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010585 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010586 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010587
Bram Moolenaare9a41262005-01-15 22:18:47 +000010588 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010589 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010590 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010591 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 return;
10593 }
10594 else if (argvars[0].v_type == VAR_DICT)
10595 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010596 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010597 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010598 return;
10599 }
10600 else
10601 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010602 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 return;
10604 }
10605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010606 expr = get_tv_string_buf_chk(&argvars[1], buf);
10607 /* On type errors, the preceding call has already displayed an error
10608 * message. Avoid a misleading error message for an empty string that
10609 * was not passed as argument. */
10610 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 prepare_vimvar(VV_VAL, &save_val);
10613 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010614
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010615 /* We reset "did_emsg" to be able to detect whether an error
10616 * occurred during evaluation of the expression. */
10617 save_did_emsg = did_emsg;
10618 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010619
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010620 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010621 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 vimvars[VV_KEY].vv_type = VAR_STRING;
10624
10625 ht = &d->dv_hashtab;
10626 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010627 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010628 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010629 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010630 if (!HASHITEM_EMPTY(hi))
10631 {
10632 --todo;
10633 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010634 if (tv_check_lock(di->di_tv.v_lock,
10635 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010636 break;
10637 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010638 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010639 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010640 break;
10641 if (!map && rem)
10642 dictitem_remove(d, di);
10643 clear_tv(&vimvars[VV_KEY].vv_tv);
10644 }
10645 }
10646 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010647 }
10648 else
10649 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010650 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10651
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010652 for (li = l->lv_first; li != NULL; li = nli)
10653 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010654 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010655 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010656 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010657 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010658 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010659 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010660 break;
10661 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010662 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010663 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010664 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010665 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010666
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010667 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010668 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010669
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010670 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010671 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010672
10673 copy_tv(&argvars[0], rettv);
10674}
10675
10676 static int
10677filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010678 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010679 char_u *expr;
10680 int map;
10681 int *remp;
10682{
Bram Moolenaar33570922005-01-25 22:26:29 +000010683 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010684 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010685 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010686
Bram Moolenaar33570922005-01-25 22:26:29 +000010687 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010688 s = expr;
10689 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010690 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010691 if (*s != NUL) /* check for trailing chars after expr */
10692 {
10693 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010694 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010695 }
10696 if (map)
10697 {
10698 /* map(): replace the list item value */
10699 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010700 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010701 *tv = rettv;
10702 }
10703 else
10704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010705 int error = FALSE;
10706
Bram Moolenaare9a41262005-01-15 22:18:47 +000010707 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010708 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010709 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 /* On type error, nothing has been removed; return FAIL to stop the
10711 * loop. The error message was given by get_tv_number_chk(). */
10712 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010713 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010714 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010715 retval = OK;
10716theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010717 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010718 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010719}
10720
10721/*
10722 * "filter()" function
10723 */
10724 static void
10725f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010726 typval_T *argvars;
10727 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010728{
10729 filter_map(argvars, rettv, FALSE);
10730}
10731
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010732/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010733 * "finddir({fname}[, {path}[, {count}]])" function
10734 */
10735 static void
10736f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010737 typval_T *argvars;
10738 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010739{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010740 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010741}
10742
10743/*
10744 * "findfile({fname}[, {path}[, {count}]])" function
10745 */
10746 static void
10747f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010748 typval_T *argvars;
10749 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010750{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010751 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010752}
10753
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010754#ifdef FEAT_FLOAT
10755/*
10756 * "float2nr({float})" function
10757 */
10758 static void
10759f_float2nr(argvars, rettv)
10760 typval_T *argvars;
10761 typval_T *rettv;
10762{
10763 float_T f;
10764
10765 if (get_float_arg(argvars, &f) == OK)
10766 {
10767 if (f < -0x7fffffff)
10768 rettv->vval.v_number = -0x7fffffff;
10769 else if (f > 0x7fffffff)
10770 rettv->vval.v_number = 0x7fffffff;
10771 else
10772 rettv->vval.v_number = (varnumber_T)f;
10773 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010774}
10775
10776/*
10777 * "floor({float})" function
10778 */
10779 static void
10780f_floor(argvars, rettv)
10781 typval_T *argvars;
10782 typval_T *rettv;
10783{
10784 float_T f;
10785
10786 rettv->v_type = VAR_FLOAT;
10787 if (get_float_arg(argvars, &f) == OK)
10788 rettv->vval.v_float = floor(f);
10789 else
10790 rettv->vval.v_float = 0.0;
10791}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010792
10793/*
10794 * "fmod()" function
10795 */
10796 static void
10797f_fmod(argvars, rettv)
10798 typval_T *argvars;
10799 typval_T *rettv;
10800{
10801 float_T fx, fy;
10802
10803 rettv->v_type = VAR_FLOAT;
10804 if (get_float_arg(argvars, &fx) == OK
10805 && get_float_arg(&argvars[1], &fy) == OK)
10806 rettv->vval.v_float = fmod(fx, fy);
10807 else
10808 rettv->vval.v_float = 0.0;
10809}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010810#endif
10811
Bram Moolenaar0d660222005-01-07 21:51:51 +000010812/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010813 * "fnameescape({string})" function
10814 */
10815 static void
10816f_fnameescape(argvars, rettv)
10817 typval_T *argvars;
10818 typval_T *rettv;
10819{
10820 rettv->vval.v_string = vim_strsave_fnameescape(
10821 get_tv_string(&argvars[0]), FALSE);
10822 rettv->v_type = VAR_STRING;
10823}
10824
10825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 * "fnamemodify({fname}, {mods})" function
10827 */
10828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010829f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010830 typval_T *argvars;
10831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832{
10833 char_u *fname;
10834 char_u *mods;
10835 int usedlen = 0;
10836 int len;
10837 char_u *fbuf = NULL;
10838 char_u buf[NUMBUFLEN];
10839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010840 fname = get_tv_string_chk(&argvars[0]);
10841 mods = get_tv_string_buf_chk(&argvars[1], buf);
10842 if (fname == NULL || mods == NULL)
10843 fname = NULL;
10844 else
10845 {
10846 len = (int)STRLEN(fname);
10847 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010850 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010854 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 vim_free(fbuf);
10856}
10857
Bram Moolenaar33570922005-01-25 22:26:29 +000010858static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859
10860/*
10861 * "foldclosed()" function
10862 */
10863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010864foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010865 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010866 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010867 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868{
10869#ifdef FEAT_FOLDING
10870 linenr_T lnum;
10871 linenr_T first, last;
10872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010873 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10875 {
10876 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10877 {
10878 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 return;
10883 }
10884 }
10885#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010886 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887}
10888
10889/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010890 * "foldclosed()" function
10891 */
10892 static void
10893f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010894 typval_T *argvars;
10895 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010896{
10897 foldclosed_both(argvars, rettv, FALSE);
10898}
10899
10900/*
10901 * "foldclosedend()" function
10902 */
10903 static void
10904f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010905 typval_T *argvars;
10906 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010907{
10908 foldclosed_both(argvars, rettv, TRUE);
10909}
10910
10911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 * "foldlevel()" function
10913 */
10914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010916 typval_T *argvars UNUSED;
10917 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918{
10919#ifdef FEAT_FOLDING
10920 linenr_T lnum;
10921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010924 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926}
10927
10928/*
10929 * "foldtext()" function
10930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010932f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010933 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935{
10936#ifdef FEAT_FOLDING
10937 linenr_T lnum;
10938 char_u *s;
10939 char_u *r;
10940 int len;
10941 char *txt;
10942#endif
10943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010944 rettv->v_type = VAR_STRING;
10945 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010947 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10948 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10949 <= curbuf->b_ml.ml_line_count
10950 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 {
10952 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010953 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10954 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 {
10956 if (!linewhite(lnum))
10957 break;
10958 ++lnum;
10959 }
10960
10961 /* Find interesting text in this line. */
10962 s = skipwhite(ml_get(lnum));
10963 /* skip C comment-start */
10964 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010965 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010967 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010968 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010969 {
10970 s = skipwhite(ml_get(lnum + 1));
10971 if (*s == '*')
10972 s = skipwhite(s + 1);
10973 }
10974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 txt = _("+-%s%3ld lines: ");
10976 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010977 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 + 20 /* for %3ld */
10979 + STRLEN(s))); /* concatenated */
10980 if (r != NULL)
10981 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010982 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10983 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10984 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 len = (int)STRLEN(r);
10986 STRCAT(r, s);
10987 /* remove 'foldmarker' and 'commentstring' */
10988 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010989 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 }
10991 }
10992#endif
10993}
10994
10995/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010996 * "foldtextresult(lnum)" function
10997 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011000 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011001 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011002{
11003#ifdef FEAT_FOLDING
11004 linenr_T lnum;
11005 char_u *text;
11006 char_u buf[51];
11007 foldinfo_T foldinfo;
11008 int fold_count;
11009#endif
11010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011011 rettv->v_type = VAR_STRING;
11012 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011013#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011015 /* treat illegal types and illegal string values for {lnum} the same */
11016 if (lnum < 0)
11017 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011018 fold_count = foldedCount(curwin, lnum, &foldinfo);
11019 if (fold_count > 0)
11020 {
11021 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11022 &foldinfo, buf);
11023 if (text == buf)
11024 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011026 }
11027#endif
11028}
11029
11030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 * "foreground()" function
11032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011034f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011035 typval_T *argvars UNUSED;
11036 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038#ifdef FEAT_GUI
11039 if (gui.in_use)
11040 gui_mch_set_foreground();
11041#else
11042# ifdef WIN32
11043 win32_set_foreground();
11044# endif
11045#endif
11046}
11047
11048/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011049 * "function()" function
11050 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011053 typval_T *argvars;
11054 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011055{
11056 char_u *s;
11057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011059 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011060 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011061 /* Don't check an autoload name for existence here. */
11062 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011063 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011064 else
11065 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011066 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011067 {
11068 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011069 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011070
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011071 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11072 * also be called from another script. Using trans_function_name()
11073 * would also work, but some plugins depend on the name being
11074 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011075 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011076 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011077 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011078 if (rettv->vval.v_string != NULL)
11079 {
11080 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011081 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011082 }
11083 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011084 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011085 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011087 }
11088}
11089
11090/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011091 * "garbagecollect()" function
11092 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011093 static void
11094f_garbagecollect(argvars, rettv)
11095 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011096 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011097{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011098 /* This is postponed until we are back at the toplevel, because we may be
11099 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11100 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011101
11102 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11103 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011104}
11105
11106/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011107 * "get()" function
11108 */
11109 static void
11110f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011111 typval_T *argvars;
11112 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011113{
Bram Moolenaar33570922005-01-25 22:26:29 +000011114 listitem_T *li;
11115 list_T *l;
11116 dictitem_T *di;
11117 dict_T *d;
11118 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011119
Bram Moolenaare9a41262005-01-15 22:18:47 +000011120 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011121 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011122 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124 int error = FALSE;
11125
11126 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11127 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011128 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011129 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011130 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011131 else if (argvars[0].v_type == VAR_DICT)
11132 {
11133 if ((d = argvars[0].vval.v_dict) != NULL)
11134 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011135 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011136 if (di != NULL)
11137 tv = &di->di_tv;
11138 }
11139 }
11140 else
11141 EMSG2(_(e_listdictarg), "get()");
11142
11143 if (tv == NULL)
11144 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011145 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011146 copy_tv(&argvars[2], rettv);
11147 }
11148 else
11149 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011150}
11151
Bram Moolenaar342337a2005-07-21 21:11:17 +000011152static 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 +000011153
11154/*
11155 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011156 * Return a range (from start to end) of lines in rettv from the specified
11157 * buffer.
11158 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011159 */
11160 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011161get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011162 buf_T *buf;
11163 linenr_T start;
11164 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011165 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011166 typval_T *rettv;
11167{
11168 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011169
Bram Moolenaar959a1432013-12-14 12:17:38 +010011170 rettv->v_type = VAR_STRING;
11171 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011172 if (retlist && rettv_list_alloc(rettv) == FAIL)
11173 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011174
11175 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11176 return;
11177
11178 if (!retlist)
11179 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011180 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11181 p = ml_get_buf(buf, start, FALSE);
11182 else
11183 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011184 rettv->vval.v_string = vim_strsave(p);
11185 }
11186 else
11187 {
11188 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011189 return;
11190
11191 if (start < 1)
11192 start = 1;
11193 if (end > buf->b_ml.ml_line_count)
11194 end = buf->b_ml.ml_line_count;
11195 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011196 if (list_append_string(rettv->vval.v_list,
11197 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011198 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011199 }
11200}
11201
11202/*
11203 * "getbufline()" function
11204 */
11205 static void
11206f_getbufline(argvars, rettv)
11207 typval_T *argvars;
11208 typval_T *rettv;
11209{
11210 linenr_T lnum;
11211 linenr_T end;
11212 buf_T *buf;
11213
11214 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11215 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011216 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011217 --emsg_off;
11218
Bram Moolenaar661b1822005-07-28 22:36:45 +000011219 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011220 if (argvars[2].v_type == VAR_UNKNOWN)
11221 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011222 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011223 end = get_tv_lnum_buf(&argvars[2], buf);
11224
Bram Moolenaar342337a2005-07-21 21:11:17 +000011225 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011226}
11227
Bram Moolenaar0d660222005-01-07 21:51:51 +000011228/*
11229 * "getbufvar()" function
11230 */
11231 static void
11232f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011233 typval_T *argvars;
11234 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011235{
11236 buf_T *buf;
11237 buf_T *save_curbuf;
11238 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011239 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011240 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011241
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011242 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11243 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011244 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011245 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011246
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011247 rettv->v_type = VAR_STRING;
11248 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011249
11250 if (buf != NULL && varname != NULL)
11251 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011252 /* set curbuf to be our buf, temporarily */
11253 save_curbuf = curbuf;
11254 curbuf = buf;
11255
Bram Moolenaar0d660222005-01-07 21:51:51 +000011256 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011257 {
11258 if (get_option_tv(&varname, rettv, TRUE) == OK)
11259 done = TRUE;
11260 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011261 else if (STRCMP(varname, "changedtick") == 0)
11262 {
11263 rettv->v_type = VAR_NUMBER;
11264 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011265 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011266 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011267 else
11268 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011269 /* Look up the variable. */
11270 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11271 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11272 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011273 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011274 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011275 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011276 done = TRUE;
11277 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011278 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011279
11280 /* restore previous notion of curbuf */
11281 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011282 }
11283
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011284 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11285 /* use the default value */
11286 copy_tv(&argvars[2], rettv);
11287
Bram Moolenaar0d660222005-01-07 21:51:51 +000011288 --emsg_off;
11289}
11290
11291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 * "getchar()" function
11293 */
11294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011295f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 typval_T *argvars;
11297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298{
11299 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011300 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011302 /* Position the cursor. Needed after a message that ends in a space. */
11303 windgoto(msg_row, msg_col);
11304
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 ++no_mapping;
11306 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011307 for (;;)
11308 {
11309 if (argvars[0].v_type == VAR_UNKNOWN)
11310 /* getchar(): blocking wait. */
11311 n = safe_vgetc();
11312 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11313 /* getchar(1): only check if char avail */
11314 n = vpeekc();
11315 else if (error || vpeekc() == NUL)
11316 /* illegal argument or getchar(0) and no char avail: return zero */
11317 n = 0;
11318 else
11319 /* getchar(0) and char avail: return char */
11320 n = safe_vgetc();
11321 if (n == K_IGNORE)
11322 continue;
11323 break;
11324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 --no_mapping;
11326 --allow_keys;
11327
Bram Moolenaar219b8702006-11-01 14:32:36 +000011328 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11329 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11330 vimvars[VV_MOUSE_COL].vv_nr = 0;
11331
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011332 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 if (IS_SPECIAL(n) || mod_mask != 0)
11334 {
11335 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11336 int i = 0;
11337
11338 /* Turn a special key into three bytes, plus modifier. */
11339 if (mod_mask != 0)
11340 {
11341 temp[i++] = K_SPECIAL;
11342 temp[i++] = KS_MODIFIER;
11343 temp[i++] = mod_mask;
11344 }
11345 if (IS_SPECIAL(n))
11346 {
11347 temp[i++] = K_SPECIAL;
11348 temp[i++] = K_SECOND(n);
11349 temp[i++] = K_THIRD(n);
11350 }
11351#ifdef FEAT_MBYTE
11352 else if (has_mbyte)
11353 i += (*mb_char2bytes)(n, temp + i);
11354#endif
11355 else
11356 temp[i++] = n;
11357 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358 rettv->v_type = VAR_STRING;
11359 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011360
11361#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011362 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011363 {
11364 int row = mouse_row;
11365 int col = mouse_col;
11366 win_T *win;
11367 linenr_T lnum;
11368# ifdef FEAT_WINDOWS
11369 win_T *wp;
11370# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011371 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011372
11373 if (row >= 0 && col >= 0)
11374 {
11375 /* Find the window at the mouse coordinates and compute the
11376 * text position. */
11377 win = mouse_find_win(&row, &col);
11378 (void)mouse_comp_pos(win, &row, &col, &lnum);
11379# ifdef FEAT_WINDOWS
11380 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011381 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011382# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011383 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011384 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11385 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11386 }
11387 }
11388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 }
11390}
11391
11392/*
11393 * "getcharmod()" function
11394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011397 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401}
11402
11403/*
11404 * "getcmdline()" function
11405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011408 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411 rettv->v_type = VAR_STRING;
11412 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413}
11414
11415/*
11416 * "getcmdpos()" function
11417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011419f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011420 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011423 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424}
11425
11426/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011427 * "getcmdtype()" function
11428 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011429 static void
11430f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011431 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011432 typval_T *rettv;
11433{
11434 rettv->v_type = VAR_STRING;
11435 rettv->vval.v_string = alloc(2);
11436 if (rettv->vval.v_string != NULL)
11437 {
11438 rettv->vval.v_string[0] = get_cmdline_type();
11439 rettv->vval.v_string[1] = NUL;
11440 }
11441}
11442
11443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 * "getcwd()" function
11445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011448 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011451 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011454 rettv->vval.v_string = NULL;
11455 cwd = alloc(MAXPATHL);
11456 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011458 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11459 {
11460 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011462 if (rettv->vval.v_string != NULL)
11463 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011465 }
11466 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 }
11468}
11469
11470/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011471 * "getfontname()" function
11472 */
11473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011475 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011476 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011477{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 rettv->v_type = VAR_STRING;
11479 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011480#ifdef FEAT_GUI
11481 if (gui.in_use)
11482 {
11483 GuiFont font;
11484 char_u *name = NULL;
11485
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011486 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011487 {
11488 /* Get the "Normal" font. Either the name saved by
11489 * hl_set_font_name() or from the font ID. */
11490 font = gui.norm_font;
11491 name = hl_get_font_name();
11492 }
11493 else
11494 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011496 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11497 return;
11498 font = gui_mch_get_font(name, FALSE);
11499 if (font == NOFONT)
11500 return; /* Invalid font name, return empty string. */
11501 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011503 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011504 gui_mch_free_font(font);
11505 }
11506#endif
11507}
11508
11509/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011510 * "getfperm({fname})" function
11511 */
11512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011513f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011514 typval_T *argvars;
11515 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011516{
11517 char_u *fname;
11518 struct stat st;
11519 char_u *perm = NULL;
11520 char_u flags[] = "rwx";
11521 int i;
11522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011523 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011525 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011526 if (mch_stat((char *)fname, &st) >= 0)
11527 {
11528 perm = vim_strsave((char_u *)"---------");
11529 if (perm != NULL)
11530 {
11531 for (i = 0; i < 9; i++)
11532 {
11533 if (st.st_mode & (1 << (8 - i)))
11534 perm[i] = flags[i % 3];
11535 }
11536 }
11537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011538 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011539}
11540
11541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 * "getfsize({fname})" function
11543 */
11544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011545f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011546 typval_T *argvars;
11547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548{
11549 char_u *fname;
11550 struct stat st;
11551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555
11556 if (mch_stat((char *)fname, &st) >= 0)
11557 {
11558 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011559 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011563
11564 /* non-perfect check for overflow */
11565 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11566 rettv->vval.v_number = -2;
11567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568 }
11569 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571}
11572
11573/*
11574 * "getftime({fname})" function
11575 */
11576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011578 typval_T *argvars;
11579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580{
11581 char_u *fname;
11582 struct stat st;
11583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585
11586 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590}
11591
11592/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011593 * "getftype({fname})" function
11594 */
11595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011597 typval_T *argvars;
11598 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011599{
11600 char_u *fname;
11601 struct stat st;
11602 char_u *type = NULL;
11603 char *t;
11604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011607 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011608 if (mch_lstat((char *)fname, &st) >= 0)
11609 {
11610#ifdef S_ISREG
11611 if (S_ISREG(st.st_mode))
11612 t = "file";
11613 else if (S_ISDIR(st.st_mode))
11614 t = "dir";
11615# ifdef S_ISLNK
11616 else if (S_ISLNK(st.st_mode))
11617 t = "link";
11618# endif
11619# ifdef S_ISBLK
11620 else if (S_ISBLK(st.st_mode))
11621 t = "bdev";
11622# endif
11623# ifdef S_ISCHR
11624 else if (S_ISCHR(st.st_mode))
11625 t = "cdev";
11626# endif
11627# ifdef S_ISFIFO
11628 else if (S_ISFIFO(st.st_mode))
11629 t = "fifo";
11630# endif
11631# ifdef S_ISSOCK
11632 else if (S_ISSOCK(st.st_mode))
11633 t = "fifo";
11634# endif
11635 else
11636 t = "other";
11637#else
11638# ifdef S_IFMT
11639 switch (st.st_mode & S_IFMT)
11640 {
11641 case S_IFREG: t = "file"; break;
11642 case S_IFDIR: t = "dir"; break;
11643# ifdef S_IFLNK
11644 case S_IFLNK: t = "link"; break;
11645# endif
11646# ifdef S_IFBLK
11647 case S_IFBLK: t = "bdev"; break;
11648# endif
11649# ifdef S_IFCHR
11650 case S_IFCHR: t = "cdev"; break;
11651# endif
11652# ifdef S_IFIFO
11653 case S_IFIFO: t = "fifo"; break;
11654# endif
11655# ifdef S_IFSOCK
11656 case S_IFSOCK: t = "socket"; break;
11657# endif
11658 default: t = "other";
11659 }
11660# else
11661 if (mch_isdir(fname))
11662 t = "dir";
11663 else
11664 t = "file";
11665# endif
11666#endif
11667 type = vim_strsave((char_u *)t);
11668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011669 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011670}
11671
11672/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011673 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011674 */
11675 static void
11676f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011677 typval_T *argvars;
11678 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011679{
11680 linenr_T lnum;
11681 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011682 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011683
11684 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011685 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011686 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011687 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011688 retlist = FALSE;
11689 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011690 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011691 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011692 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011693 retlist = TRUE;
11694 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011695
Bram Moolenaar342337a2005-07-21 21:11:17 +000011696 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011697}
11698
11699/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011700 * "getmatches()" function
11701 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011702 static void
11703f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011704 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011705 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011706{
11707#ifdef FEAT_SEARCH_EXTRA
11708 dict_T *dict;
11709 matchitem_T *cur = curwin->w_match_head;
11710
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011711 if (rettv_list_alloc(rettv) == OK)
11712 {
11713 while (cur != NULL)
11714 {
11715 dict = dict_alloc();
11716 if (dict == NULL)
11717 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011718 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11719 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11720 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11721 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11722 list_append_dict(rettv->vval.v_list, dict);
11723 cur = cur->next;
11724 }
11725 }
11726#endif
11727}
11728
11729/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011730 * "getpid()" function
11731 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011732 static void
11733f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011734 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011735 typval_T *rettv;
11736{
11737 rettv->vval.v_number = mch_get_pid();
11738}
11739
11740/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011741 * "getpos(string)" function
11742 */
11743 static void
11744f_getpos(argvars, rettv)
11745 typval_T *argvars;
11746 typval_T *rettv;
11747{
11748 pos_T *fp;
11749 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011750 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011751
11752 if (rettv_list_alloc(rettv) == OK)
11753 {
11754 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011755 fp = var2fpos(&argvars[0], TRUE, &fnum);
11756 if (fnum != -1)
11757 list_append_number(l, (varnumber_T)fnum);
11758 else
11759 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011760 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11761 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011762 list_append_number(l, (fp != NULL)
11763 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011764 : (varnumber_T)0);
11765 list_append_number(l,
11766#ifdef FEAT_VIRTUALEDIT
11767 (fp != NULL) ? (varnumber_T)fp->coladd :
11768#endif
11769 (varnumber_T)0);
11770 }
11771 else
11772 rettv->vval.v_number = FALSE;
11773}
11774
11775/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011776 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011777 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011778 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011779f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011780 typval_T *argvars UNUSED;
11781 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011782{
11783#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011784 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011785#endif
11786
Bram Moolenaar2641f772005-03-25 21:58:17 +000011787#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011788 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011789 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011790 wp = NULL;
11791 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11792 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011793 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011794 if (wp == NULL)
11795 return;
11796 }
11797
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011798 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011799 }
11800#endif
11801}
11802
11803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804 * "getreg()" function
11805 */
11806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011807f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011808 typval_T *argvars;
11809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810{
11811 char_u *strregname;
11812 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011813 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011814 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011817 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011819 strregname = get_tv_string_chk(&argvars[0]);
11820 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011821 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011823 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011824 if (!error && argvars[2].v_type != VAR_UNKNOWN)
11825 return_list = get_tv_number_chk(&argvars[2], &error);
11826 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011829 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011830
11831 if (error)
11832 return;
11833
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834 regname = (strregname == NULL ? '"' : *strregname);
11835 if (regname == 0)
11836 regname = '"';
11837
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011838 if (return_list)
11839 {
11840 rettv->v_type = VAR_LIST;
11841 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
11842 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
11843 }
11844 else
11845 {
11846 rettv->v_type = VAR_STRING;
11847 rettv->vval.v_string = get_reg_contents(regname,
11848 arg2 ? GREG_EXPR_SRC : 0);
11849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850}
11851
11852/*
11853 * "getregtype()" function
11854 */
11855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011857 typval_T *argvars;
11858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859{
11860 char_u *strregname;
11861 int regname;
11862 char_u buf[NUMBUFLEN + 2];
11863 long reglen = 0;
11864
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011865 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011866 {
11867 strregname = get_tv_string_chk(&argvars[0]);
11868 if (strregname == NULL) /* type error; errmsg already given */
11869 {
11870 rettv->v_type = VAR_STRING;
11871 rettv->vval.v_string = NULL;
11872 return;
11873 }
11874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875 else
11876 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011877 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878
11879 regname = (strregname == NULL ? '"' : *strregname);
11880 if (regname == 0)
11881 regname = '"';
11882
11883 buf[0] = NUL;
11884 buf[1] = NUL;
11885 switch (get_reg_type(regname, &reglen))
11886 {
11887 case MLINE: buf[0] = 'V'; break;
11888 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889 case MBLOCK:
11890 buf[0] = Ctrl_V;
11891 sprintf((char *)buf + 1, "%ld", reglen + 1);
11892 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011894 rettv->v_type = VAR_STRING;
11895 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896}
11897
11898/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011899 * "gettabvar()" function
11900 */
11901 static void
11902f_gettabvar(argvars, rettv)
11903 typval_T *argvars;
11904 typval_T *rettv;
11905{
11906 tabpage_T *tp;
11907 dictitem_T *v;
11908 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011909 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011910
11911 rettv->v_type = VAR_STRING;
11912 rettv->vval.v_string = NULL;
11913
11914 varname = get_tv_string_chk(&argvars[1]);
11915 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11916 if (tp != NULL && varname != NULL)
11917 {
11918 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011919 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011920 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011921 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011922 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011923 done = TRUE;
11924 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011925 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011926
11927 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011928 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011929}
11930
11931/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011932 * "gettabwinvar()" function
11933 */
11934 static void
11935f_gettabwinvar(argvars, rettv)
11936 typval_T *argvars;
11937 typval_T *rettv;
11938{
11939 getwinvar(argvars, rettv, 1);
11940}
11941
11942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943 * "getwinposx()" function
11944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011946f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011947 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011950 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951#ifdef FEAT_GUI
11952 if (gui.in_use)
11953 {
11954 int x, y;
11955
11956 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011957 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958 }
11959#endif
11960}
11961
11962/*
11963 * "getwinposy()" function
11964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011966f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011967 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971#ifdef FEAT_GUI
11972 if (gui.in_use)
11973 {
11974 int x, y;
11975
11976 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011977 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978 }
11979#endif
11980}
11981
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011982/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011983 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011984 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011985 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011986find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011987 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011988 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011989{
11990#ifdef FEAT_WINDOWS
11991 win_T *wp;
11992#endif
11993 int nr;
11994
11995 nr = get_tv_number_chk(vp, NULL);
11996
11997#ifdef FEAT_WINDOWS
11998 if (nr < 0)
11999 return NULL;
12000 if (nr == 0)
12001 return curwin;
12002
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012003 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12004 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012005 if (--nr <= 0)
12006 break;
12007 return wp;
12008#else
12009 if (nr == 0 || nr == 1)
12010 return curwin;
12011 return NULL;
12012#endif
12013}
12014
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015/*
12016 * "getwinvar()" function
12017 */
12018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012020 typval_T *argvars;
12021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012023 getwinvar(argvars, rettv, 0);
12024}
12025
12026/*
12027 * getwinvar() and gettabwinvar()
12028 */
12029 static void
12030getwinvar(argvars, rettv, off)
12031 typval_T *argvars;
12032 typval_T *rettv;
12033 int off; /* 1 for gettabwinvar() */
12034{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035 win_T *win, *oldcurwin;
12036 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012037 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012038 tabpage_T *tp = NULL;
12039 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012040 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012042#ifdef FEAT_WINDOWS
12043 if (off == 1)
12044 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12045 else
12046 tp = curtab;
12047#endif
12048 win = find_win_by_nr(&argvars[off], tp);
12049 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012050 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012052 rettv->v_type = VAR_STRING;
12053 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054
12055 if (win != NULL && varname != NULL)
12056 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012057 /* Set curwin to be our win, temporarily. Also set the tabpage,
12058 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012059 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012060
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012062 {
12063 if (get_option_tv(&varname, rettv, 1) == OK)
12064 done = TRUE;
12065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066 else
12067 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012068 /* Look up the variable. */
12069 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12070 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012072 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012073 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012074 done = TRUE;
12075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012077
12078 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012079 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 }
12081
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012082 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12083 /* use the default return value */
12084 copy_tv(&argvars[off + 2], rettv);
12085
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 --emsg_off;
12087}
12088
12089/*
12090 * "glob()" function
12091 */
12092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012094 typval_T *argvars;
12095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012097 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012099 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012101 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012102 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012104 if (argvars[1].v_type != VAR_UNKNOWN)
12105 {
12106 if (get_tv_number_chk(&argvars[1], &error))
12107 options |= WILD_KEEP_ALL;
12108 if (argvars[2].v_type != VAR_UNKNOWN
12109 && get_tv_number_chk(&argvars[2], &error))
12110 {
12111 rettv->v_type = VAR_LIST;
12112 rettv->vval.v_list = NULL;
12113 }
12114 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012115 if (!error)
12116 {
12117 ExpandInit(&xpc);
12118 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012119 if (p_wic)
12120 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012121 if (rettv->v_type == VAR_STRING)
12122 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012123 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012124 else if (rettv_list_alloc(rettv) != FAIL)
12125 {
12126 int i;
12127
12128 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12129 NULL, options, WILD_ALL_KEEP);
12130 for (i = 0; i < xpc.xp_numfiles; i++)
12131 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12132
12133 ExpandCleanup(&xpc);
12134 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012135 }
12136 else
12137 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012138}
12139
12140/*
12141 * "globpath()" function
12142 */
12143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012144f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012145 typval_T *argvars;
12146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012148 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012150 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012151 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012153 /* When the optional second argument is non-zero, don't remove matches
12154 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12155 if (argvars[2].v_type != VAR_UNKNOWN
12156 && get_tv_number_chk(&argvars[2], &error))
12157 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012159 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012160 rettv->vval.v_string = NULL;
12161 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012162 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12163 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164}
12165
12166/*
12167 * "has()" function
12168 */
12169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012170f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012171 typval_T *argvars;
12172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173{
12174 int i;
12175 char_u *name;
12176 int n = FALSE;
12177 static char *(has_list[]) =
12178 {
12179#ifdef AMIGA
12180 "amiga",
12181# ifdef FEAT_ARP
12182 "arp",
12183# endif
12184#endif
12185#ifdef __BEOS__
12186 "beos",
12187#endif
12188#ifdef MSDOS
12189# ifdef DJGPP
12190 "dos32",
12191# else
12192 "dos16",
12193# endif
12194#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012195#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196 "mac",
12197#endif
12198#if defined(MACOS_X_UNIX)
12199 "macunix",
12200#endif
12201#ifdef OS2
12202 "os2",
12203#endif
12204#ifdef __QNX__
12205 "qnx",
12206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207#ifdef UNIX
12208 "unix",
12209#endif
12210#ifdef VMS
12211 "vms",
12212#endif
12213#ifdef WIN16
12214 "win16",
12215#endif
12216#ifdef WIN32
12217 "win32",
12218#endif
12219#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12220 "win32unix",
12221#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012222#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223 "win64",
12224#endif
12225#ifdef EBCDIC
12226 "ebcdic",
12227#endif
12228#ifndef CASE_INSENSITIVE_FILENAME
12229 "fname_case",
12230#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012231#ifdef HAVE_ACL
12232 "acl",
12233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234#ifdef FEAT_ARABIC
12235 "arabic",
12236#endif
12237#ifdef FEAT_AUTOCMD
12238 "autocmd",
12239#endif
12240#ifdef FEAT_BEVAL
12241 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012242# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12243 "balloon_multiline",
12244# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245#endif
12246#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12247 "builtin_terms",
12248# ifdef ALL_BUILTIN_TCAPS
12249 "all_builtin_terms",
12250# endif
12251#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012252#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12253 || defined(FEAT_GUI_W32) \
12254 || defined(FEAT_GUI_MOTIF))
12255 "browsefilter",
12256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257#ifdef FEAT_BYTEOFF
12258 "byte_offset",
12259#endif
12260#ifdef FEAT_CINDENT
12261 "cindent",
12262#endif
12263#ifdef FEAT_CLIENTSERVER
12264 "clientserver",
12265#endif
12266#ifdef FEAT_CLIPBOARD
12267 "clipboard",
12268#endif
12269#ifdef FEAT_CMDL_COMPL
12270 "cmdline_compl",
12271#endif
12272#ifdef FEAT_CMDHIST
12273 "cmdline_hist",
12274#endif
12275#ifdef FEAT_COMMENTS
12276 "comments",
12277#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012278#ifdef FEAT_CONCEAL
12279 "conceal",
12280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281#ifdef FEAT_CRYPT
12282 "cryptv",
12283#endif
12284#ifdef FEAT_CSCOPE
12285 "cscope",
12286#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012287#ifdef FEAT_CURSORBIND
12288 "cursorbind",
12289#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012290#ifdef CURSOR_SHAPE
12291 "cursorshape",
12292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293#ifdef DEBUG
12294 "debug",
12295#endif
12296#ifdef FEAT_CON_DIALOG
12297 "dialog_con",
12298#endif
12299#ifdef FEAT_GUI_DIALOG
12300 "dialog_gui",
12301#endif
12302#ifdef FEAT_DIFF
12303 "diff",
12304#endif
12305#ifdef FEAT_DIGRAPHS
12306 "digraphs",
12307#endif
12308#ifdef FEAT_DND
12309 "dnd",
12310#endif
12311#ifdef FEAT_EMACS_TAGS
12312 "emacs_tags",
12313#endif
12314 "eval", /* always present, of course! */
12315#ifdef FEAT_EX_EXTRA
12316 "ex_extra",
12317#endif
12318#ifdef FEAT_SEARCH_EXTRA
12319 "extra_search",
12320#endif
12321#ifdef FEAT_FKMAP
12322 "farsi",
12323#endif
12324#ifdef FEAT_SEARCHPATH
12325 "file_in_path",
12326#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012327#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012328 "filterpipe",
12329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330#ifdef FEAT_FIND_ID
12331 "find_in_path",
12332#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012333#ifdef FEAT_FLOAT
12334 "float",
12335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336#ifdef FEAT_FOLDING
12337 "folding",
12338#endif
12339#ifdef FEAT_FOOTER
12340 "footer",
12341#endif
12342#if !defined(USE_SYSTEM) && defined(UNIX)
12343 "fork",
12344#endif
12345#ifdef FEAT_GETTEXT
12346 "gettext",
12347#endif
12348#ifdef FEAT_GUI
12349 "gui",
12350#endif
12351#ifdef FEAT_GUI_ATHENA
12352# ifdef FEAT_GUI_NEXTAW
12353 "gui_neXtaw",
12354# else
12355 "gui_athena",
12356# endif
12357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358#ifdef FEAT_GUI_GTK
12359 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012362#ifdef FEAT_GUI_GNOME
12363 "gui_gnome",
12364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365#ifdef FEAT_GUI_MAC
12366 "gui_mac",
12367#endif
12368#ifdef FEAT_GUI_MOTIF
12369 "gui_motif",
12370#endif
12371#ifdef FEAT_GUI_PHOTON
12372 "gui_photon",
12373#endif
12374#ifdef FEAT_GUI_W16
12375 "gui_win16",
12376#endif
12377#ifdef FEAT_GUI_W32
12378 "gui_win32",
12379#endif
12380#ifdef FEAT_HANGULIN
12381 "hangul_input",
12382#endif
12383#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12384 "iconv",
12385#endif
12386#ifdef FEAT_INS_EXPAND
12387 "insert_expand",
12388#endif
12389#ifdef FEAT_JUMPLIST
12390 "jumplist",
12391#endif
12392#ifdef FEAT_KEYMAP
12393 "keymap",
12394#endif
12395#ifdef FEAT_LANGMAP
12396 "langmap",
12397#endif
12398#ifdef FEAT_LIBCALL
12399 "libcall",
12400#endif
12401#ifdef FEAT_LINEBREAK
12402 "linebreak",
12403#endif
12404#ifdef FEAT_LISP
12405 "lispindent",
12406#endif
12407#ifdef FEAT_LISTCMDS
12408 "listcmds",
12409#endif
12410#ifdef FEAT_LOCALMAP
12411 "localmap",
12412#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012413#ifdef FEAT_LUA
12414# ifndef DYNAMIC_LUA
12415 "lua",
12416# endif
12417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418#ifdef FEAT_MENU
12419 "menu",
12420#endif
12421#ifdef FEAT_SESSION
12422 "mksession",
12423#endif
12424#ifdef FEAT_MODIFY_FNAME
12425 "modify_fname",
12426#endif
12427#ifdef FEAT_MOUSE
12428 "mouse",
12429#endif
12430#ifdef FEAT_MOUSESHAPE
12431 "mouseshape",
12432#endif
12433#if defined(UNIX) || defined(VMS)
12434# ifdef FEAT_MOUSE_DEC
12435 "mouse_dec",
12436# endif
12437# ifdef FEAT_MOUSE_GPM
12438 "mouse_gpm",
12439# endif
12440# ifdef FEAT_MOUSE_JSB
12441 "mouse_jsbterm",
12442# endif
12443# ifdef FEAT_MOUSE_NET
12444 "mouse_netterm",
12445# endif
12446# ifdef FEAT_MOUSE_PTERM
12447 "mouse_pterm",
12448# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012449# ifdef FEAT_MOUSE_SGR
12450 "mouse_sgr",
12451# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012452# ifdef FEAT_SYSMOUSE
12453 "mouse_sysmouse",
12454# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012455# ifdef FEAT_MOUSE_URXVT
12456 "mouse_urxvt",
12457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458# ifdef FEAT_MOUSE_XTERM
12459 "mouse_xterm",
12460# endif
12461#endif
12462#ifdef FEAT_MBYTE
12463 "multi_byte",
12464#endif
12465#ifdef FEAT_MBYTE_IME
12466 "multi_byte_ime",
12467#endif
12468#ifdef FEAT_MULTI_LANG
12469 "multi_lang",
12470#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012471#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012472#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012473 "mzscheme",
12474#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476#ifdef FEAT_OLE
12477 "ole",
12478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479#ifdef FEAT_PATH_EXTRA
12480 "path_extra",
12481#endif
12482#ifdef FEAT_PERL
12483#ifndef DYNAMIC_PERL
12484 "perl",
12485#endif
12486#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012487#ifdef FEAT_PERSISTENT_UNDO
12488 "persistent_undo",
12489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490#ifdef FEAT_PYTHON
12491#ifndef DYNAMIC_PYTHON
12492 "python",
12493#endif
12494#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012495#ifdef FEAT_PYTHON3
12496#ifndef DYNAMIC_PYTHON3
12497 "python3",
12498#endif
12499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500#ifdef FEAT_POSTSCRIPT
12501 "postscript",
12502#endif
12503#ifdef FEAT_PRINTER
12504 "printer",
12505#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012506#ifdef FEAT_PROFILE
12507 "profile",
12508#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012509#ifdef FEAT_RELTIME
12510 "reltime",
12511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512#ifdef FEAT_QUICKFIX
12513 "quickfix",
12514#endif
12515#ifdef FEAT_RIGHTLEFT
12516 "rightleft",
12517#endif
12518#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12519 "ruby",
12520#endif
12521#ifdef FEAT_SCROLLBIND
12522 "scrollbind",
12523#endif
12524#ifdef FEAT_CMDL_INFO
12525 "showcmd",
12526 "cmdline_info",
12527#endif
12528#ifdef FEAT_SIGNS
12529 "signs",
12530#endif
12531#ifdef FEAT_SMARTINDENT
12532 "smartindent",
12533#endif
12534#ifdef FEAT_SNIFF
12535 "sniff",
12536#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012537#ifdef STARTUPTIME
12538 "startuptime",
12539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540#ifdef FEAT_STL_OPT
12541 "statusline",
12542#endif
12543#ifdef FEAT_SUN_WORKSHOP
12544 "sun_workshop",
12545#endif
12546#ifdef FEAT_NETBEANS_INTG
12547 "netbeans_intg",
12548#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012549#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012550 "spell",
12551#endif
12552#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553 "syntax",
12554#endif
12555#if defined(USE_SYSTEM) || !defined(UNIX)
12556 "system",
12557#endif
12558#ifdef FEAT_TAG_BINS
12559 "tag_binary",
12560#endif
12561#ifdef FEAT_TAG_OLDSTATIC
12562 "tag_old_static",
12563#endif
12564#ifdef FEAT_TAG_ANYWHITE
12565 "tag_any_white",
12566#endif
12567#ifdef FEAT_TCL
12568# ifndef DYNAMIC_TCL
12569 "tcl",
12570# endif
12571#endif
12572#ifdef TERMINFO
12573 "terminfo",
12574#endif
12575#ifdef FEAT_TERMRESPONSE
12576 "termresponse",
12577#endif
12578#ifdef FEAT_TEXTOBJ
12579 "textobjects",
12580#endif
12581#ifdef HAVE_TGETENT
12582 "tgetent",
12583#endif
12584#ifdef FEAT_TITLE
12585 "title",
12586#endif
12587#ifdef FEAT_TOOLBAR
12588 "toolbar",
12589#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012590#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12591 "unnamedplus",
12592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593#ifdef FEAT_USR_CMDS
12594 "user-commands", /* was accidentally included in 5.4 */
12595 "user_commands",
12596#endif
12597#ifdef FEAT_VIMINFO
12598 "viminfo",
12599#endif
12600#ifdef FEAT_VERTSPLIT
12601 "vertsplit",
12602#endif
12603#ifdef FEAT_VIRTUALEDIT
12604 "virtualedit",
12605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607#ifdef FEAT_VISUALEXTRA
12608 "visualextra",
12609#endif
12610#ifdef FEAT_VREPLACE
12611 "vreplace",
12612#endif
12613#ifdef FEAT_WILDIGN
12614 "wildignore",
12615#endif
12616#ifdef FEAT_WILDMENU
12617 "wildmenu",
12618#endif
12619#ifdef FEAT_WINDOWS
12620 "windows",
12621#endif
12622#ifdef FEAT_WAK
12623 "winaltkeys",
12624#endif
12625#ifdef FEAT_WRITEBACKUP
12626 "writebackup",
12627#endif
12628#ifdef FEAT_XIM
12629 "xim",
12630#endif
12631#ifdef FEAT_XFONTSET
12632 "xfontset",
12633#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012634#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012635 "xpm",
12636 "xpm_w32", /* for backward compatibility */
12637#else
12638# if defined(HAVE_XPM)
12639 "xpm",
12640# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642#ifdef USE_XSMP
12643 "xsmp",
12644#endif
12645#ifdef USE_XSMP_INTERACT
12646 "xsmp_interact",
12647#endif
12648#ifdef FEAT_XCLIPBOARD
12649 "xterm_clipboard",
12650#endif
12651#ifdef FEAT_XTERM_SAVE
12652 "xterm_save",
12653#endif
12654#if defined(UNIX) && defined(FEAT_X11)
12655 "X11",
12656#endif
12657 NULL
12658 };
12659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012660 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661 for (i = 0; has_list[i] != NULL; ++i)
12662 if (STRICMP(name, has_list[i]) == 0)
12663 {
12664 n = TRUE;
12665 break;
12666 }
12667
12668 if (n == FALSE)
12669 {
12670 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012671 {
12672 if (name[5] == '-'
12673 && STRLEN(name) > 11
12674 && vim_isdigit(name[6])
12675 && vim_isdigit(name[8])
12676 && vim_isdigit(name[10]))
12677 {
12678 int major = atoi((char *)name + 6);
12679 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012680
12681 /* Expect "patch-9.9.01234". */
12682 n = (major < VIM_VERSION_MAJOR
12683 || (major == VIM_VERSION_MAJOR
12684 && (minor < VIM_VERSION_MINOR
12685 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012686 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012687 }
12688 else
12689 n = has_patch(atoi((char *)name + 5));
12690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691 else if (STRICMP(name, "vim_starting") == 0)
12692 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012693#ifdef FEAT_MBYTE
12694 else if (STRICMP(name, "multi_byte_encoding") == 0)
12695 n = has_mbyte;
12696#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012697#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12698 else if (STRICMP(name, "balloon_multiline") == 0)
12699 n = multiline_balloon_available();
12700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701#ifdef DYNAMIC_TCL
12702 else if (STRICMP(name, "tcl") == 0)
12703 n = tcl_enabled(FALSE);
12704#endif
12705#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12706 else if (STRICMP(name, "iconv") == 0)
12707 n = iconv_enabled(FALSE);
12708#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012709#ifdef DYNAMIC_LUA
12710 else if (STRICMP(name, "lua") == 0)
12711 n = lua_enabled(FALSE);
12712#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012713#ifdef DYNAMIC_MZSCHEME
12714 else if (STRICMP(name, "mzscheme") == 0)
12715 n = mzscheme_enabled(FALSE);
12716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717#ifdef DYNAMIC_RUBY
12718 else if (STRICMP(name, "ruby") == 0)
12719 n = ruby_enabled(FALSE);
12720#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012721#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722#ifdef DYNAMIC_PYTHON
12723 else if (STRICMP(name, "python") == 0)
12724 n = python_enabled(FALSE);
12725#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012726#endif
12727#ifdef FEAT_PYTHON3
12728#ifdef DYNAMIC_PYTHON3
12729 else if (STRICMP(name, "python3") == 0)
12730 n = python3_enabled(FALSE);
12731#endif
12732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733#ifdef DYNAMIC_PERL
12734 else if (STRICMP(name, "perl") == 0)
12735 n = perl_enabled(FALSE);
12736#endif
12737#ifdef FEAT_GUI
12738 else if (STRICMP(name, "gui_running") == 0)
12739 n = (gui.in_use || gui.starting);
12740# ifdef FEAT_GUI_W32
12741 else if (STRICMP(name, "gui_win32s") == 0)
12742 n = gui_is_win32s();
12743# endif
12744# ifdef FEAT_BROWSE
12745 else if (STRICMP(name, "browse") == 0)
12746 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12747# endif
12748#endif
12749#ifdef FEAT_SYN_HL
12750 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012751 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752#endif
12753#if defined(WIN3264)
12754 else if (STRICMP(name, "win95") == 0)
12755 n = mch_windows95();
12756#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012757#ifdef FEAT_NETBEANS_INTG
12758 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012759 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 }
12762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764}
12765
12766/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012767 * "has_key()" function
12768 */
12769 static void
12770f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012771 typval_T *argvars;
12772 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012773{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012774 if (argvars[0].v_type != VAR_DICT)
12775 {
12776 EMSG(_(e_dictreq));
12777 return;
12778 }
12779 if (argvars[0].vval.v_dict == NULL)
12780 return;
12781
12782 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012783 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012784}
12785
12786/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012787 * "haslocaldir()" function
12788 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012789 static void
12790f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012791 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012792 typval_T *rettv;
12793{
12794 rettv->vval.v_number = (curwin->w_localdir != NULL);
12795}
12796
12797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 * "hasmapto()" function
12799 */
12800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012802 typval_T *argvars;
12803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804{
12805 char_u *name;
12806 char_u *mode;
12807 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012808 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012811 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812 mode = (char_u *)"nvo";
12813 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012814 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012815 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012816 if (argvars[2].v_type != VAR_UNKNOWN)
12817 abbr = get_tv_number(&argvars[2]);
12818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819
Bram Moolenaar2c932302006-03-18 21:42:09 +000012820 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012821 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824}
12825
12826/*
12827 * "histadd()" function
12828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012831 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833{
12834#ifdef FEAT_CMDHIST
12835 int histype;
12836 char_u *str;
12837 char_u buf[NUMBUFLEN];
12838#endif
12839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012840 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 if (check_restricted() || check_secure())
12842 return;
12843#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012844 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12845 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846 if (histype >= 0)
12847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849 if (*str != NUL)
12850 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012851 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 return;
12855 }
12856 }
12857#endif
12858}
12859
12860/*
12861 * "histdel()" function
12862 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012865 typval_T *argvars UNUSED;
12866 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867{
12868#ifdef FEAT_CMDHIST
12869 int n;
12870 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012871 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012873 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12874 if (str == NULL)
12875 n = 0;
12876 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012878 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012879 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012881 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012882 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 else
12884 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012885 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886 get_tv_string_buf(&argvars[1], buf));
12887 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888#endif
12889}
12890
12891/*
12892 * "histget()" function
12893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012895f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012896 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898{
12899#ifdef FEAT_CMDHIST
12900 int type;
12901 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012902 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012904 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12905 if (str == NULL)
12906 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012908 {
12909 type = get_histtype(str);
12910 if (argvars[1].v_type == VAR_UNKNOWN)
12911 idx = get_history_idx(type);
12912 else
12913 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12914 /* -1 on type error */
12915 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012918 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012920 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921}
12922
12923/*
12924 * "histnr()" function
12925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012927f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012928 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930{
12931 int i;
12932
12933#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012934 char_u *history = get_tv_string_chk(&argvars[0]);
12935
12936 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 if (i >= HIST_CMD && i < HIST_COUNT)
12938 i = get_history_idx(i);
12939 else
12940#endif
12941 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943}
12944
12945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946 * "highlightID(name)" function
12947 */
12948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012949f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012950 typval_T *argvars;
12951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012953 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954}
12955
12956/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012957 * "highlight_exists()" function
12958 */
12959 static void
12960f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012961 typval_T *argvars;
12962 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012963{
12964 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12965}
12966
12967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968 * "hostname()" function
12969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012971f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012972 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974{
12975 char_u hostname[256];
12976
12977 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012978 rettv->v_type = VAR_STRING;
12979 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980}
12981
12982/*
12983 * iconv() function
12984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012986f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012987 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989{
12990#ifdef FEAT_MBYTE
12991 char_u buf1[NUMBUFLEN];
12992 char_u buf2[NUMBUFLEN];
12993 char_u *from, *to, *str;
12994 vimconv_T vimconv;
12995#endif
12996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012997 rettv->v_type = VAR_STRING;
12998 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999
13000#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013001 str = get_tv_string(&argvars[0]);
13002 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13003 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004 vimconv.vc_type = CONV_NONE;
13005 convert_setup(&vimconv, from, to);
13006
13007 /* If the encodings are equal, no conversion needed. */
13008 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013009 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013011 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012
13013 convert_setup(&vimconv, NULL, NULL);
13014 vim_free(from);
13015 vim_free(to);
13016#endif
13017}
13018
13019/*
13020 * "indent()" function
13021 */
13022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013023f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013024 typval_T *argvars;
13025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026{
13027 linenr_T lnum;
13028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013029 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013031 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013033 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034}
13035
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013036/*
13037 * "index()" function
13038 */
13039 static void
13040f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013041 typval_T *argvars;
13042 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013043{
Bram Moolenaar33570922005-01-25 22:26:29 +000013044 list_T *l;
13045 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013046 long idx = 0;
13047 int ic = FALSE;
13048
13049 rettv->vval.v_number = -1;
13050 if (argvars[0].v_type != VAR_LIST)
13051 {
13052 EMSG(_(e_listreq));
13053 return;
13054 }
13055 l = argvars[0].vval.v_list;
13056 if (l != NULL)
13057 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013058 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013059 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013061 int error = FALSE;
13062
Bram Moolenaar758711c2005-02-02 23:11:38 +000013063 /* Start at specified item. Use the cached index that list_find()
13064 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013065 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013066 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013067 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013068 ic = get_tv_number_chk(&argvars[3], &error);
13069 if (error)
13070 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013071 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013072
Bram Moolenaar758711c2005-02-02 23:11:38 +000013073 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013074 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013075 {
13076 rettv->vval.v_number = idx;
13077 break;
13078 }
13079 }
13080}
13081
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082static int inputsecret_flag = 0;
13083
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013084static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13085
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013087 * This function is used by f_input() and f_inputdialog() functions. The third
13088 * argument to f_input() specifies the type of completion to use at the
13089 * prompt. The third argument to f_inputdialog() specifies the value to return
13090 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091 */
13092 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013093get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013094 typval_T *argvars;
13095 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013096 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013098 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099 char_u *p = NULL;
13100 int c;
13101 char_u buf[NUMBUFLEN];
13102 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013103 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013104 int xp_type = EXPAND_NOTHING;
13105 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013107 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013108 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109
13110#ifdef NO_CONSOLE_INPUT
13111 /* While starting up, there is no place to enter text. */
13112 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114#endif
13115
13116 cmd_silent = FALSE; /* Want to see the prompt. */
13117 if (prompt != NULL)
13118 {
13119 /* Only the part of the message after the last NL is considered as
13120 * prompt for the command line */
13121 p = vim_strrchr(prompt, '\n');
13122 if (p == NULL)
13123 p = prompt;
13124 else
13125 {
13126 ++p;
13127 c = *p;
13128 *p = NUL;
13129 msg_start();
13130 msg_clr_eos();
13131 msg_puts_attr(prompt, echo_attr);
13132 msg_didout = FALSE;
13133 msg_starthere();
13134 *p = c;
13135 }
13136 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013138 if (argvars[1].v_type != VAR_UNKNOWN)
13139 {
13140 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13141 if (defstr != NULL)
13142 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013144 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013145 {
13146 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013147 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013148 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013149
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013150 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013151 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013152
Bram Moolenaar4463f292005-09-25 22:20:24 +000013153 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13154 if (xp_name == NULL)
13155 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013156
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013157 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013158
Bram Moolenaar4463f292005-09-25 22:20:24 +000013159 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13160 &xp_arg) == FAIL)
13161 return;
13162 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013163 }
13164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013165 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013166 {
13167# ifdef FEAT_EX_EXTRA
13168 int save_ex_normal_busy = ex_normal_busy;
13169 ex_normal_busy = 0;
13170# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013171 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013172 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13173 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013174# ifdef FEAT_EX_EXTRA
13175 ex_normal_busy = save_ex_normal_busy;
13176# endif
13177 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013178 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013179 && argvars[1].v_type != VAR_UNKNOWN
13180 && argvars[2].v_type != VAR_UNKNOWN)
13181 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13182 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013183
13184 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013186 /* since the user typed this, no need to wait for return */
13187 need_wait_return = FALSE;
13188 msg_didout = FALSE;
13189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 cmd_silent = cmd_silent_save;
13191}
13192
13193/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013194 * "input()" function
13195 * Also handles inputsecret() when inputsecret is set.
13196 */
13197 static void
13198f_input(argvars, rettv)
13199 typval_T *argvars;
13200 typval_T *rettv;
13201{
13202 get_user_input(argvars, rettv, FALSE);
13203}
13204
13205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206 * "inputdialog()" function
13207 */
13208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013209f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013210 typval_T *argvars;
13211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212{
13213#if defined(FEAT_GUI_TEXTDIALOG)
13214 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13215 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13216 {
13217 char_u *message;
13218 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013219 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013221 message = get_tv_string_chk(&argvars[0]);
13222 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013223 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013224 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 else
13226 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013227 if (message != NULL && defstr != NULL
13228 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013229 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013230 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 else
13232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013233 if (message != NULL && defstr != NULL
13234 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013235 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013236 rettv->vval.v_string = vim_strsave(
13237 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013239 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 }
13243 else
13244#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013245 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246}
13247
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013248/*
13249 * "inputlist()" function
13250 */
13251 static void
13252f_inputlist(argvars, rettv)
13253 typval_T *argvars;
13254 typval_T *rettv;
13255{
13256 listitem_T *li;
13257 int selected;
13258 int mouse_used;
13259
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013260#ifdef NO_CONSOLE_INPUT
13261 /* While starting up, there is no place to enter text. */
13262 if (no_console_input())
13263 return;
13264#endif
13265 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13266 {
13267 EMSG2(_(e_listarg), "inputlist()");
13268 return;
13269 }
13270
13271 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013272 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013273 lines_left = Rows; /* avoid more prompt */
13274 msg_scroll = TRUE;
13275 msg_clr_eos();
13276
13277 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13278 {
13279 msg_puts(get_tv_string(&li->li_tv));
13280 msg_putchar('\n');
13281 }
13282
13283 /* Ask for choice. */
13284 selected = prompt_for_number(&mouse_used);
13285 if (mouse_used)
13286 selected -= lines_left;
13287
13288 rettv->vval.v_number = selected;
13289}
13290
13291
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13293
13294/*
13295 * "inputrestore()" function
13296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013298f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013299 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301{
13302 if (ga_userinput.ga_len > 0)
13303 {
13304 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13306 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013307 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308 }
13309 else if (p_verbose > 1)
13310 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013311 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013312 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313 }
13314}
13315
13316/*
13317 * "inputsave()" function
13318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013320f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013321 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013324 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325 if (ga_grow(&ga_userinput, 1) == OK)
13326 {
13327 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13328 + ga_userinput.ga_len);
13329 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013330 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 }
13332 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013333 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334}
13335
13336/*
13337 * "inputsecret()" function
13338 */
13339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013340f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013341 typval_T *argvars;
13342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343{
13344 ++cmdline_star;
13345 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013346 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 --cmdline_star;
13348 --inputsecret_flag;
13349}
13350
13351/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013352 * "insert()" function
13353 */
13354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013355f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013356 typval_T *argvars;
13357 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013358{
13359 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013360 listitem_T *item;
13361 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013362 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013363
13364 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013365 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013366 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013367 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013368 {
13369 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013370 before = get_tv_number_chk(&argvars[2], &error);
13371 if (error)
13372 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013373
Bram Moolenaar758711c2005-02-02 23:11:38 +000013374 if (before == l->lv_len)
13375 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013376 else
13377 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013378 item = list_find(l, before);
13379 if (item == NULL)
13380 {
13381 EMSGN(_(e_listidx), before);
13382 l = NULL;
13383 }
13384 }
13385 if (l != NULL)
13386 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013387 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013388 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013389 }
13390 }
13391}
13392
13393/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013394 * "invert(expr)" function
13395 */
13396 static void
13397f_invert(argvars, rettv)
13398 typval_T *argvars;
13399 typval_T *rettv;
13400{
13401 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13402}
13403
13404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405 * "isdirectory()" function
13406 */
13407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013408f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013409 typval_T *argvars;
13410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013412 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413}
13414
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013415/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013416 * "islocked()" function
13417 */
13418 static void
13419f_islocked(argvars, rettv)
13420 typval_T *argvars;
13421 typval_T *rettv;
13422{
13423 lval_T lv;
13424 char_u *end;
13425 dictitem_T *di;
13426
13427 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013428 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13429 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013430 if (end != NULL && lv.ll_name != NULL)
13431 {
13432 if (*end != NUL)
13433 EMSG(_(e_trailing));
13434 else
13435 {
13436 if (lv.ll_tv == NULL)
13437 {
13438 if (check_changedtick(lv.ll_name))
13439 rettv->vval.v_number = 1; /* always locked */
13440 else
13441 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013442 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013443 if (di != NULL)
13444 {
13445 /* Consider a variable locked when:
13446 * 1. the variable itself is locked
13447 * 2. the value of the variable is locked.
13448 * 3. the List or Dict value is locked.
13449 */
13450 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13451 || tv_islocked(&di->di_tv));
13452 }
13453 }
13454 }
13455 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013456 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013457 else if (lv.ll_newkey != NULL)
13458 EMSG2(_(e_dictkey), lv.ll_newkey);
13459 else if (lv.ll_list != NULL)
13460 /* List item. */
13461 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13462 else
13463 /* Dictionary item. */
13464 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13465 }
13466 }
13467
13468 clear_lval(&lv);
13469}
13470
Bram Moolenaar33570922005-01-25 22:26:29 +000013471static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013472
13473/*
13474 * Turn a dict into a list:
13475 * "what" == 0: list of keys
13476 * "what" == 1: list of values
13477 * "what" == 2: list of items
13478 */
13479 static void
13480dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013481 typval_T *argvars;
13482 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013483 int what;
13484{
Bram Moolenaar33570922005-01-25 22:26:29 +000013485 list_T *l2;
13486 dictitem_T *di;
13487 hashitem_T *hi;
13488 listitem_T *li;
13489 listitem_T *li2;
13490 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013491 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013492
Bram Moolenaar8c711452005-01-14 21:53:12 +000013493 if (argvars[0].v_type != VAR_DICT)
13494 {
13495 EMSG(_(e_dictreq));
13496 return;
13497 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013498 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013499 return;
13500
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013501 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013502 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013503
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013504 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013505 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013506 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013507 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013508 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013509 --todo;
13510 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013511
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013512 li = listitem_alloc();
13513 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013514 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013515 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013516
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013517 if (what == 0)
13518 {
13519 /* keys() */
13520 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013521 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013522 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13523 }
13524 else if (what == 1)
13525 {
13526 /* values() */
13527 copy_tv(&di->di_tv, &li->li_tv);
13528 }
13529 else
13530 {
13531 /* items() */
13532 l2 = list_alloc();
13533 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013534 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013535 li->li_tv.vval.v_list = l2;
13536 if (l2 == NULL)
13537 break;
13538 ++l2->lv_refcount;
13539
13540 li2 = listitem_alloc();
13541 if (li2 == NULL)
13542 break;
13543 list_append(l2, li2);
13544 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013545 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013546 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13547
13548 li2 = listitem_alloc();
13549 if (li2 == NULL)
13550 break;
13551 list_append(l2, li2);
13552 copy_tv(&di->di_tv, &li2->li_tv);
13553 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013554 }
13555 }
13556}
13557
13558/*
13559 * "items(dict)" function
13560 */
13561 static void
13562f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013563 typval_T *argvars;
13564 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013565{
13566 dict_list(argvars, rettv, 2);
13567}
13568
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013570 * "join()" function
13571 */
13572 static void
13573f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013574 typval_T *argvars;
13575 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013576{
13577 garray_T ga;
13578 char_u *sep;
13579
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013580 if (argvars[0].v_type != VAR_LIST)
13581 {
13582 EMSG(_(e_listreq));
13583 return;
13584 }
13585 if (argvars[0].vval.v_list == NULL)
13586 return;
13587 if (argvars[1].v_type == VAR_UNKNOWN)
13588 sep = (char_u *)" ";
13589 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013590 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013591
13592 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013593
13594 if (sep != NULL)
13595 {
13596 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013597 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013598 ga_append(&ga, NUL);
13599 rettv->vval.v_string = (char_u *)ga.ga_data;
13600 }
13601 else
13602 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013603}
13604
13605/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013606 * "keys()" function
13607 */
13608 static void
13609f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013610 typval_T *argvars;
13611 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013612{
13613 dict_list(argvars, rettv, 0);
13614}
13615
13616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617 * "last_buffer_nr()" function.
13618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013621 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623{
13624 int n = 0;
13625 buf_T *buf;
13626
13627 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13628 if (n < buf->b_fnum)
13629 n = buf->b_fnum;
13630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013632}
13633
13634/*
13635 * "len()" function
13636 */
13637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013638f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013639 typval_T *argvars;
13640 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013641{
13642 switch (argvars[0].v_type)
13643 {
13644 case VAR_STRING:
13645 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646 rettv->vval.v_number = (varnumber_T)STRLEN(
13647 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013648 break;
13649 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013650 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013651 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013652 case VAR_DICT:
13653 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13654 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013655 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013656 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013657 break;
13658 }
13659}
13660
Bram Moolenaar33570922005-01-25 22:26:29 +000013661static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013662
13663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013664libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013665 typval_T *argvars;
13666 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013667 int type;
13668{
13669#ifdef FEAT_LIBCALL
13670 char_u *string_in;
13671 char_u **string_result;
13672 int nr_result;
13673#endif
13674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013675 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013676 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013678
13679 if (check_restricted() || check_secure())
13680 return;
13681
13682#ifdef FEAT_LIBCALL
13683 /* The first two args must be strings, otherwise its meaningless */
13684 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13685 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013686 string_in = NULL;
13687 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013688 string_in = argvars[2].vval.v_string;
13689 if (type == VAR_NUMBER)
13690 string_result = NULL;
13691 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013692 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013693 if (mch_libcall(argvars[0].vval.v_string,
13694 argvars[1].vval.v_string,
13695 string_in,
13696 argvars[2].vval.v_number,
13697 string_result,
13698 &nr_result) == OK
13699 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013700 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013701 }
13702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703}
13704
13705/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013706 * "libcall()" function
13707 */
13708 static void
13709f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013710 typval_T *argvars;
13711 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013712{
13713 libcall_common(argvars, rettv, VAR_STRING);
13714}
13715
13716/*
13717 * "libcallnr()" function
13718 */
13719 static void
13720f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013721 typval_T *argvars;
13722 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013723{
13724 libcall_common(argvars, rettv, VAR_NUMBER);
13725}
13726
13727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728 * "line(string)" function
13729 */
13730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013731f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013732 typval_T *argvars;
13733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734{
13735 linenr_T lnum = 0;
13736 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013737 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013739 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740 if (fp != NULL)
13741 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013742 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743}
13744
13745/*
13746 * "line2byte(lnum)" function
13747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013749f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752{
13753#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013754 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755#else
13756 linenr_T lnum;
13757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013758 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013760 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013762 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13763 if (rettv->vval.v_number >= 0)
13764 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765#endif
13766}
13767
13768/*
13769 * "lispindent(lnum)" function
13770 */
13771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013772f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013773 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775{
13776#ifdef FEAT_LISP
13777 pos_T pos;
13778 linenr_T lnum;
13779
13780 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013781 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13783 {
13784 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013785 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786 curwin->w_cursor = pos;
13787 }
13788 else
13789#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013790 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791}
13792
13793/*
13794 * "localtime()" function
13795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013797f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013798 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013801 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802}
13803
Bram Moolenaar33570922005-01-25 22:26:29 +000013804static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805
13806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013807get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013808 typval_T *argvars;
13809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810 int exact;
13811{
13812 char_u *keys;
13813 char_u *which;
13814 char_u buf[NUMBUFLEN];
13815 char_u *keys_buf = NULL;
13816 char_u *rhs;
13817 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013818 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013819 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013820 mapblock_T *mp;
13821 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822
13823 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013824 rettv->v_type = VAR_STRING;
13825 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013827 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 if (*keys == NUL)
13829 return;
13830
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013831 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013833 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013834 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013835 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013836 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013837 if (argvars[3].v_type != VAR_UNKNOWN)
13838 get_dict = get_tv_number(&argvars[3]);
13839 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 else
13842 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013843 if (which == NULL)
13844 return;
13845
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 mode = get_map_mode(&which, 0);
13847
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013848 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013849 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013851
13852 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013854 /* Return a string. */
13855 if (rhs != NULL)
13856 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857
Bram Moolenaarbd743252010-10-20 21:23:33 +020013858 }
13859 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13860 {
13861 /* Return a dictionary. */
13862 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13863 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13864 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865
Bram Moolenaarbd743252010-10-20 21:23:33 +020013866 dict_add_nr_str(dict, "lhs", 0L, lhs);
13867 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13868 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13869 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13870 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13871 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13872 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013873 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013874 dict_add_nr_str(dict, "mode", 0L, mapmode);
13875
13876 vim_free(lhs);
13877 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878 }
13879}
13880
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013881#ifdef FEAT_FLOAT
13882/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013883 * "log()" function
13884 */
13885 static void
13886f_log(argvars, rettv)
13887 typval_T *argvars;
13888 typval_T *rettv;
13889{
13890 float_T f;
13891
13892 rettv->v_type = VAR_FLOAT;
13893 if (get_float_arg(argvars, &f) == OK)
13894 rettv->vval.v_float = log(f);
13895 else
13896 rettv->vval.v_float = 0.0;
13897}
13898
13899/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013900 * "log10()" function
13901 */
13902 static void
13903f_log10(argvars, rettv)
13904 typval_T *argvars;
13905 typval_T *rettv;
13906{
13907 float_T f;
13908
13909 rettv->v_type = VAR_FLOAT;
13910 if (get_float_arg(argvars, &f) == OK)
13911 rettv->vval.v_float = log10(f);
13912 else
13913 rettv->vval.v_float = 0.0;
13914}
13915#endif
13916
Bram Moolenaar1dced572012-04-05 16:54:08 +020013917#ifdef FEAT_LUA
13918/*
13919 * "luaeval()" function
13920 */
13921 static void
13922f_luaeval(argvars, rettv)
13923 typval_T *argvars;
13924 typval_T *rettv;
13925{
13926 char_u *str;
13927 char_u buf[NUMBUFLEN];
13928
13929 str = get_tv_string_buf(&argvars[0], buf);
13930 do_luaeval(str, argvars + 1, rettv);
13931}
13932#endif
13933
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013935 * "map()" function
13936 */
13937 static void
13938f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013939 typval_T *argvars;
13940 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013941{
13942 filter_map(argvars, rettv, TRUE);
13943}
13944
13945/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013946 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 */
13948 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013949f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013950 typval_T *argvars;
13951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013953 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954}
13955
13956/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013957 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 */
13959 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013960f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013961 typval_T *argvars;
13962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013964 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965}
13966
Bram Moolenaar33570922005-01-25 22:26:29 +000013967static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968
13969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013970find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013971 typval_T *argvars;
13972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 int type;
13974{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013975 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013976 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013977 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 char_u *pat;
13979 regmatch_T regmatch;
13980 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013981 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 char_u *save_cpo;
13983 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013984 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013985 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013986 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013987 list_T *l = NULL;
13988 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013989 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013990 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991
13992 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13993 save_cpo = p_cpo;
13994 p_cpo = (char_u *)"";
13995
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013996 rettv->vval.v_number = -1;
13997 if (type == 3)
13998 {
13999 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014000 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014001 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014002 }
14003 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014005 rettv->v_type = VAR_STRING;
14006 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014009 if (argvars[0].v_type == VAR_LIST)
14010 {
14011 if ((l = argvars[0].vval.v_list) == NULL)
14012 goto theend;
14013 li = l->lv_first;
14014 }
14015 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014016 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014017 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014018 len = (long)STRLEN(str);
14019 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014021 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14022 if (pat == NULL)
14023 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014024
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014025 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014027 int error = FALSE;
14028
14029 start = get_tv_number_chk(&argvars[2], &error);
14030 if (error)
14031 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014032 if (l != NULL)
14033 {
14034 li = list_find(l, start);
14035 if (li == NULL)
14036 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014037 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014038 }
14039 else
14040 {
14041 if (start < 0)
14042 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014043 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014044 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014045 /* When "count" argument is there ignore matches before "start",
14046 * otherwise skip part of the string. Differs when pattern is "^"
14047 * or "\<". */
14048 if (argvars[3].v_type != VAR_UNKNOWN)
14049 startcol = start;
14050 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014051 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014052 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014053 len -= start;
14054 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014055 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014056
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014057 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014058 nth = get_tv_number_chk(&argvars[3], &error);
14059 if (error)
14060 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 }
14062
14063 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14064 if (regmatch.regprog != NULL)
14065 {
14066 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014067
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014068 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014069 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014070 if (l != NULL)
14071 {
14072 if (li == NULL)
14073 {
14074 match = FALSE;
14075 break;
14076 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014077 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014078 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014079 if (str == NULL)
14080 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014081 }
14082
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014083 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014084
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014085 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014086 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014087 if (l == NULL && !match)
14088 break;
14089
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014090 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014091 if (l != NULL)
14092 {
14093 li = li->li_next;
14094 ++idx;
14095 }
14096 else
14097 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014098#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014099 startcol = (colnr_T)(regmatch.startp[0]
14100 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014101#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014102 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014103#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014104 if (startcol > (colnr_T)len
14105 || str + startcol <= regmatch.startp[0])
14106 {
14107 match = FALSE;
14108 break;
14109 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014110 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014111 }
14112
14113 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014115 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014116 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014117 int i;
14118
14119 /* return list with matched string and submatches */
14120 for (i = 0; i < NSUBEXP; ++i)
14121 {
14122 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014123 {
14124 if (list_append_string(rettv->vval.v_list,
14125 (char_u *)"", 0) == FAIL)
14126 break;
14127 }
14128 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014129 regmatch.startp[i],
14130 (int)(regmatch.endp[i] - regmatch.startp[i]))
14131 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014132 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014133 }
14134 }
14135 else if (type == 2)
14136 {
14137 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014138 if (l != NULL)
14139 copy_tv(&li->li_tv, rettv);
14140 else
14141 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014143 }
14144 else if (l != NULL)
14145 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 else
14147 {
14148 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150 (varnumber_T)(regmatch.startp[0] - str);
14151 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014152 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014154 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 }
14156 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014157 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 }
14159
14160theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014161 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162 p_cpo = save_cpo;
14163}
14164
14165/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014166 * "match()" function
14167 */
14168 static void
14169f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014170 typval_T *argvars;
14171 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014172{
14173 find_some_match(argvars, rettv, 1);
14174}
14175
14176/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014177 * "matchadd()" function
14178 */
14179 static void
14180f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014181 typval_T *argvars UNUSED;
14182 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014183{
14184#ifdef FEAT_SEARCH_EXTRA
14185 char_u buf[NUMBUFLEN];
14186 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14187 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14188 int prio = 10; /* default priority */
14189 int id = -1;
14190 int error = FALSE;
14191
14192 rettv->vval.v_number = -1;
14193
14194 if (grp == NULL || pat == NULL)
14195 return;
14196 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014197 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014198 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014199 if (argvars[3].v_type != VAR_UNKNOWN)
14200 id = get_tv_number_chk(&argvars[3], &error);
14201 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014202 if (error == TRUE)
14203 return;
14204 if (id >= 1 && id <= 3)
14205 {
14206 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14207 return;
14208 }
14209
14210 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14211#endif
14212}
14213
14214/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014215 * "matcharg()" function
14216 */
14217 static void
14218f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014219 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014220 typval_T *rettv;
14221{
14222 if (rettv_list_alloc(rettv) == OK)
14223 {
14224#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014225 int id = get_tv_number(&argvars[0]);
14226 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014227
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014228 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014229 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014230 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14231 {
14232 list_append_string(rettv->vval.v_list,
14233 syn_id2name(m->hlg_id), -1);
14234 list_append_string(rettv->vval.v_list, m->pattern, -1);
14235 }
14236 else
14237 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014238 list_append_string(rettv->vval.v_list, NULL, -1);
14239 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014240 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014241 }
14242#endif
14243 }
14244}
14245
14246/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014247 * "matchdelete()" function
14248 */
14249 static void
14250f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014251 typval_T *argvars UNUSED;
14252 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014253{
14254#ifdef FEAT_SEARCH_EXTRA
14255 rettv->vval.v_number = match_delete(curwin,
14256 (int)get_tv_number(&argvars[0]), TRUE);
14257#endif
14258}
14259
14260/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014261 * "matchend()" function
14262 */
14263 static void
14264f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014265 typval_T *argvars;
14266 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014267{
14268 find_some_match(argvars, rettv, 0);
14269}
14270
14271/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014272 * "matchlist()" function
14273 */
14274 static void
14275f_matchlist(argvars, rettv)
14276 typval_T *argvars;
14277 typval_T *rettv;
14278{
14279 find_some_match(argvars, rettv, 3);
14280}
14281
14282/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014283 * "matchstr()" function
14284 */
14285 static void
14286f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014287 typval_T *argvars;
14288 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014289{
14290 find_some_match(argvars, rettv, 2);
14291}
14292
Bram Moolenaar33570922005-01-25 22:26:29 +000014293static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014294
14295 static void
14296max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014297 typval_T *argvars;
14298 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014299 int domax;
14300{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014301 long n = 0;
14302 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014303 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014304
14305 if (argvars[0].v_type == VAR_LIST)
14306 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014307 list_T *l;
14308 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014309
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014310 l = argvars[0].vval.v_list;
14311 if (l != NULL)
14312 {
14313 li = l->lv_first;
14314 if (li != NULL)
14315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014316 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014317 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014318 {
14319 li = li->li_next;
14320 if (li == NULL)
14321 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014322 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014323 if (domax ? i > n : i < n)
14324 n = i;
14325 }
14326 }
14327 }
14328 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014329 else if (argvars[0].v_type == VAR_DICT)
14330 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014331 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014332 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014333 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014334 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014335
14336 d = argvars[0].vval.v_dict;
14337 if (d != NULL)
14338 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014339 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014340 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014341 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014342 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014343 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014344 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014345 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014346 if (first)
14347 {
14348 n = i;
14349 first = FALSE;
14350 }
14351 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014352 n = i;
14353 }
14354 }
14355 }
14356 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014357 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014358 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014359 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014360}
14361
14362/*
14363 * "max()" function
14364 */
14365 static void
14366f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014367 typval_T *argvars;
14368 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014369{
14370 max_min(argvars, rettv, TRUE);
14371}
14372
14373/*
14374 * "min()" function
14375 */
14376 static void
14377f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014378 typval_T *argvars;
14379 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014380{
14381 max_min(argvars, rettv, FALSE);
14382}
14383
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014384static int mkdir_recurse __ARGS((char_u *dir, int prot));
14385
14386/*
14387 * Create the directory in which "dir" is located, and higher levels when
14388 * needed.
14389 */
14390 static int
14391mkdir_recurse(dir, prot)
14392 char_u *dir;
14393 int prot;
14394{
14395 char_u *p;
14396 char_u *updir;
14397 int r = FAIL;
14398
14399 /* Get end of directory name in "dir".
14400 * We're done when it's "/" or "c:/". */
14401 p = gettail_sep(dir);
14402 if (p <= get_past_head(dir))
14403 return OK;
14404
14405 /* If the directory exists we're done. Otherwise: create it.*/
14406 updir = vim_strnsave(dir, (int)(p - dir));
14407 if (updir == NULL)
14408 return FAIL;
14409 if (mch_isdir(updir))
14410 r = OK;
14411 else if (mkdir_recurse(updir, prot) == OK)
14412 r = vim_mkdir_emsg(updir, prot);
14413 vim_free(updir);
14414 return r;
14415}
14416
14417#ifdef vim_mkdir
14418/*
14419 * "mkdir()" function
14420 */
14421 static void
14422f_mkdir(argvars, rettv)
14423 typval_T *argvars;
14424 typval_T *rettv;
14425{
14426 char_u *dir;
14427 char_u buf[NUMBUFLEN];
14428 int prot = 0755;
14429
14430 rettv->vval.v_number = FAIL;
14431 if (check_restricted() || check_secure())
14432 return;
14433
14434 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014435 if (*dir == NUL)
14436 rettv->vval.v_number = FAIL;
14437 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014438 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014439 if (*gettail(dir) == NUL)
14440 /* remove trailing slashes */
14441 *gettail_sep(dir) = NUL;
14442
14443 if (argvars[1].v_type != VAR_UNKNOWN)
14444 {
14445 if (argvars[2].v_type != VAR_UNKNOWN)
14446 prot = get_tv_number_chk(&argvars[2], NULL);
14447 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14448 mkdir_recurse(dir, prot);
14449 }
14450 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014451 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014452}
14453#endif
14454
Bram Moolenaar0d660222005-01-07 21:51:51 +000014455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 * "mode()" function
14457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014459f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014460 typval_T *argvars;
14461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014463 char_u buf[3];
14464
14465 buf[1] = NUL;
14466 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468 if (VIsual_active)
14469 {
14470 if (VIsual_select)
14471 buf[0] = VIsual_mode + 's' - 'v';
14472 else
14473 buf[0] = VIsual_mode;
14474 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014475 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014476 || State == CONFIRM)
14477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014479 if (State == ASKMORE)
14480 buf[1] = 'm';
14481 else if (State == CONFIRM)
14482 buf[1] = '?';
14483 }
14484 else if (State == EXTERNCMD)
14485 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 else if (State & INSERT)
14487 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014488#ifdef FEAT_VREPLACE
14489 if (State & VREPLACE_FLAG)
14490 {
14491 buf[0] = 'R';
14492 buf[1] = 'v';
14493 }
14494 else
14495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496 if (State & REPLACE_FLAG)
14497 buf[0] = 'R';
14498 else
14499 buf[0] = 'i';
14500 }
14501 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014504 if (exmode_active)
14505 buf[1] = 'v';
14506 }
14507 else if (exmode_active)
14508 {
14509 buf[0] = 'c';
14510 buf[1] = 'e';
14511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014515 if (finish_op)
14516 buf[1] = 'o';
14517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014519 /* Clear out the minor mode when the argument is not a non-zero number or
14520 * non-empty string. */
14521 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014522 buf[1] = NUL;
14523
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014524 rettv->vval.v_string = vim_strsave(buf);
14525 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526}
14527
Bram Moolenaar429fa852013-04-15 12:27:36 +020014528#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014529/*
14530 * "mzeval()" function
14531 */
14532 static void
14533f_mzeval(argvars, rettv)
14534 typval_T *argvars;
14535 typval_T *rettv;
14536{
14537 char_u *str;
14538 char_u buf[NUMBUFLEN];
14539
14540 str = get_tv_string_buf(&argvars[0], buf);
14541 do_mzeval(str, rettv);
14542}
Bram Moolenaar75676462013-01-30 14:55:42 +010014543
14544 void
14545mzscheme_call_vim(name, args, rettv)
14546 char_u *name;
14547 typval_T *args;
14548 typval_T *rettv;
14549{
14550 typval_T argvars[3];
14551
14552 argvars[0].v_type = VAR_STRING;
14553 argvars[0].vval.v_string = name;
14554 copy_tv(args, &argvars[1]);
14555 argvars[2].v_type = VAR_UNKNOWN;
14556 f_call(argvars, rettv);
14557 clear_tv(&argvars[1]);
14558}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014559#endif
14560
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014562 * "nextnonblank()" function
14563 */
14564 static void
14565f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014566 typval_T *argvars;
14567 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014568{
14569 linenr_T lnum;
14570
14571 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014573 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014574 {
14575 lnum = 0;
14576 break;
14577 }
14578 if (*skipwhite(ml_get(lnum)) != NUL)
14579 break;
14580 }
14581 rettv->vval.v_number = lnum;
14582}
14583
14584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585 * "nr2char()" function
14586 */
14587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014589 typval_T *argvars;
14590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591{
14592 char_u buf[NUMBUFLEN];
14593
14594#ifdef FEAT_MBYTE
14595 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014596 {
14597 int utf8 = 0;
14598
14599 if (argvars[1].v_type != VAR_UNKNOWN)
14600 utf8 = get_tv_number_chk(&argvars[1], NULL);
14601 if (utf8)
14602 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14603 else
14604 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606 else
14607#endif
14608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014609 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610 buf[1] = NUL;
14611 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014612 rettv->v_type = VAR_STRING;
14613 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014614}
14615
14616/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014617 * "or(expr, expr)" function
14618 */
14619 static void
14620f_or(argvars, rettv)
14621 typval_T *argvars;
14622 typval_T *rettv;
14623{
14624 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14625 | get_tv_number_chk(&argvars[1], NULL);
14626}
14627
14628/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014629 * "pathshorten()" function
14630 */
14631 static void
14632f_pathshorten(argvars, rettv)
14633 typval_T *argvars;
14634 typval_T *rettv;
14635{
14636 char_u *p;
14637
14638 rettv->v_type = VAR_STRING;
14639 p = get_tv_string_chk(&argvars[0]);
14640 if (p == NULL)
14641 rettv->vval.v_string = NULL;
14642 else
14643 {
14644 p = vim_strsave(p);
14645 rettv->vval.v_string = p;
14646 if (p != NULL)
14647 shorten_dir(p);
14648 }
14649}
14650
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014651#ifdef FEAT_FLOAT
14652/*
14653 * "pow()" function
14654 */
14655 static void
14656f_pow(argvars, rettv)
14657 typval_T *argvars;
14658 typval_T *rettv;
14659{
14660 float_T fx, fy;
14661
14662 rettv->v_type = VAR_FLOAT;
14663 if (get_float_arg(argvars, &fx) == OK
14664 && get_float_arg(&argvars[1], &fy) == OK)
14665 rettv->vval.v_float = pow(fx, fy);
14666 else
14667 rettv->vval.v_float = 0.0;
14668}
14669#endif
14670
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014671/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014672 * "prevnonblank()" function
14673 */
14674 static void
14675f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014676 typval_T *argvars;
14677 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014678{
14679 linenr_T lnum;
14680
14681 lnum = get_tv_lnum(argvars);
14682 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14683 lnum = 0;
14684 else
14685 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14686 --lnum;
14687 rettv->vval.v_number = lnum;
14688}
14689
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014690#ifdef HAVE_STDARG_H
14691/* This dummy va_list is here because:
14692 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14693 * - locally in the function results in a "used before set" warning
14694 * - using va_start() to initialize it gives "function with fixed args" error */
14695static va_list ap;
14696#endif
14697
Bram Moolenaar8c711452005-01-14 21:53:12 +000014698/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014699 * "printf()" function
14700 */
14701 static void
14702f_printf(argvars, rettv)
14703 typval_T *argvars;
14704 typval_T *rettv;
14705{
14706 rettv->v_type = VAR_STRING;
14707 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014708#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014709 {
14710 char_u buf[NUMBUFLEN];
14711 int len;
14712 char_u *s;
14713 int saved_did_emsg = did_emsg;
14714 char *fmt;
14715
14716 /* Get the required length, allocate the buffer and do it for real. */
14717 did_emsg = FALSE;
14718 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014719 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014720 if (!did_emsg)
14721 {
14722 s = alloc(len + 1);
14723 if (s != NULL)
14724 {
14725 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014726 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014727 }
14728 }
14729 did_emsg |= saved_did_emsg;
14730 }
14731#endif
14732}
14733
14734/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014735 * "pumvisible()" function
14736 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014737 static void
14738f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014739 typval_T *argvars UNUSED;
14740 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014741{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014742#ifdef FEAT_INS_EXPAND
14743 if (pum_visible())
14744 rettv->vval.v_number = 1;
14745#endif
14746}
14747
Bram Moolenaardb913952012-06-29 12:54:53 +020014748#ifdef FEAT_PYTHON3
14749/*
14750 * "py3eval()" function
14751 */
14752 static void
14753f_py3eval(argvars, rettv)
14754 typval_T *argvars;
14755 typval_T *rettv;
14756{
14757 char_u *str;
14758 char_u buf[NUMBUFLEN];
14759
14760 str = get_tv_string_buf(&argvars[0], buf);
14761 do_py3eval(str, rettv);
14762}
14763#endif
14764
14765#ifdef FEAT_PYTHON
14766/*
14767 * "pyeval()" function
14768 */
14769 static void
14770f_pyeval(argvars, rettv)
14771 typval_T *argvars;
14772 typval_T *rettv;
14773{
14774 char_u *str;
14775 char_u buf[NUMBUFLEN];
14776
14777 str = get_tv_string_buf(&argvars[0], buf);
14778 do_pyeval(str, rettv);
14779}
14780#endif
14781
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014782/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014783 * "range()" function
14784 */
14785 static void
14786f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014787 typval_T *argvars;
14788 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014789{
14790 long start;
14791 long end;
14792 long stride = 1;
14793 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014794 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014797 if (argvars[1].v_type == VAR_UNKNOWN)
14798 {
14799 end = start - 1;
14800 start = 0;
14801 }
14802 else
14803 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014804 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014805 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014806 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014807 }
14808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014809 if (error)
14810 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014811 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014812 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014813 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014814 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014815 else
14816 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014817 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014818 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014819 if (list_append_number(rettv->vval.v_list,
14820 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014821 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014822 }
14823}
14824
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014825/*
14826 * "readfile()" function
14827 */
14828 static void
14829f_readfile(argvars, rettv)
14830 typval_T *argvars;
14831 typval_T *rettv;
14832{
14833 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014834 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014835 char_u *fname;
14836 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014837 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14838 int io_size = sizeof(buf);
14839 int readlen; /* size of last fread() */
14840 char_u *prev = NULL; /* previously read bytes, if any */
14841 long prevlen = 0; /* length of data in prev */
14842 long prevsize = 0; /* size of prev buffer */
14843 long maxline = MAXLNUM;
14844 long cnt = 0;
14845 char_u *p; /* position in buf */
14846 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014847
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014848 if (argvars[1].v_type != VAR_UNKNOWN)
14849 {
14850 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14851 binary = TRUE;
14852 if (argvars[2].v_type != VAR_UNKNOWN)
14853 maxline = get_tv_number(&argvars[2]);
14854 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014855
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014856 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014857 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014858
14859 /* Always open the file in binary mode, library functions have a mind of
14860 * their own about CR-LF conversion. */
14861 fname = get_tv_string(&argvars[0]);
14862 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14863 {
14864 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14865 return;
14866 }
14867
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014868 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014869 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014870 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014871
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014872 /* This for loop processes what was read, but is also entered at end
14873 * of file so that either:
14874 * - an incomplete line gets written
14875 * - a "binary" file gets an empty line at the end if it ends in a
14876 * newline. */
14877 for (p = buf, start = buf;
14878 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14879 ++p)
14880 {
14881 if (*p == '\n' || readlen <= 0)
14882 {
14883 listitem_T *li;
14884 char_u *s = NULL;
14885 long_u len = p - start;
14886
14887 /* Finished a line. Remove CRs before NL. */
14888 if (readlen > 0 && !binary)
14889 {
14890 while (len > 0 && start[len - 1] == '\r')
14891 --len;
14892 /* removal may cross back to the "prev" string */
14893 if (len == 0)
14894 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14895 --prevlen;
14896 }
14897 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014898 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014899 else
14900 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014901 /* Change "prev" buffer to be the right size. This way
14902 * the bytes are only copied once, and very long lines are
14903 * allocated only once. */
14904 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014905 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014906 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014907 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014908 prev = NULL; /* the list will own the string */
14909 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014910 }
14911 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014912 if (s == NULL)
14913 {
14914 do_outofmem_msg((long_u) prevlen + len + 1);
14915 failed = TRUE;
14916 break;
14917 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014918
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014919 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014920 {
14921 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014922 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014923 break;
14924 }
14925 li->li_tv.v_type = VAR_STRING;
14926 li->li_tv.v_lock = 0;
14927 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014928 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014929
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014930 start = p + 1; /* step over newline */
14931 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014932 break;
14933 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014934 else if (*p == NUL)
14935 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014936#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014937 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14938 * when finding the BF and check the previous two bytes. */
14939 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014940 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014941 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14942 * + 1, these may be in the "prev" string. */
14943 char_u back1 = p >= buf + 1 ? p[-1]
14944 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14945 char_u back2 = p >= buf + 2 ? p[-2]
14946 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14947 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014948
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014949 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014950 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014951 char_u *dest = p - 2;
14952
14953 /* Usually a BOM is at the beginning of a file, and so at
14954 * the beginning of a line; then we can just step over it.
14955 */
14956 if (start == dest)
14957 start = p + 1;
14958 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014959 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014960 /* have to shuffle buf to close gap */
14961 int adjust_prevlen = 0;
14962
14963 if (dest < buf)
14964 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014965 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014966 dest = buf;
14967 }
14968 if (readlen > p - buf + 1)
14969 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14970 readlen -= 3 - adjust_prevlen;
14971 prevlen -= adjust_prevlen;
14972 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014973 }
14974 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014975 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014976#endif
14977 } /* for */
14978
14979 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14980 break;
14981 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014982 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014983 /* There's part of a line in buf, store it in "prev". */
14984 if (p - start + prevlen >= prevsize)
14985 {
14986 /* need bigger "prev" buffer */
14987 char_u *newprev;
14988
14989 /* A common use case is ordinary text files and "prev" gets a
14990 * fragment of a line, so the first allocation is made
14991 * small, to avoid repeatedly 'allocing' large and
14992 * 'reallocing' small. */
14993 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014994 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014995 else
14996 {
14997 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014998 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014999 prevsize = grow50pc > growmin ? grow50pc : growmin;
15000 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015001 newprev = prev == NULL ? alloc(prevsize)
15002 : vim_realloc(prev, prevsize);
15003 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015004 {
15005 do_outofmem_msg((long_u)prevsize);
15006 failed = TRUE;
15007 break;
15008 }
15009 prev = newprev;
15010 }
15011 /* Add the line part to end of "prev". */
15012 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015013 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015014 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015015 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015016
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015017 /*
15018 * For a negative line count use only the lines at the end of the file,
15019 * free the rest.
15020 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015021 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015022 while (cnt > -maxline)
15023 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015024 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015025 --cnt;
15026 }
15027
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015028 if (failed)
15029 {
15030 list_free(rettv->vval.v_list, TRUE);
15031 /* readfile doc says an empty list is returned on error */
15032 rettv->vval.v_list = list_alloc();
15033 }
15034
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015035 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015036 fclose(fd);
15037}
15038
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015039#if defined(FEAT_RELTIME)
15040static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15041
15042/*
15043 * Convert a List to proftime_T.
15044 * Return FAIL when there is something wrong.
15045 */
15046 static int
15047list2proftime(arg, tm)
15048 typval_T *arg;
15049 proftime_T *tm;
15050{
15051 long n1, n2;
15052 int error = FALSE;
15053
15054 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15055 || arg->vval.v_list->lv_len != 2)
15056 return FAIL;
15057 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15058 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15059# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015060 tm->HighPart = n1;
15061 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015062# else
15063 tm->tv_sec = n1;
15064 tm->tv_usec = n2;
15065# endif
15066 return error ? FAIL : OK;
15067}
15068#endif /* FEAT_RELTIME */
15069
15070/*
15071 * "reltime()" function
15072 */
15073 static void
15074f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015075 typval_T *argvars UNUSED;
15076 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015077{
15078#ifdef FEAT_RELTIME
15079 proftime_T res;
15080 proftime_T start;
15081
15082 if (argvars[0].v_type == VAR_UNKNOWN)
15083 {
15084 /* No arguments: get current time. */
15085 profile_start(&res);
15086 }
15087 else if (argvars[1].v_type == VAR_UNKNOWN)
15088 {
15089 if (list2proftime(&argvars[0], &res) == FAIL)
15090 return;
15091 profile_end(&res);
15092 }
15093 else
15094 {
15095 /* Two arguments: compute the difference. */
15096 if (list2proftime(&argvars[0], &start) == FAIL
15097 || list2proftime(&argvars[1], &res) == FAIL)
15098 return;
15099 profile_sub(&res, &start);
15100 }
15101
15102 if (rettv_list_alloc(rettv) == OK)
15103 {
15104 long n1, n2;
15105
15106# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015107 n1 = res.HighPart;
15108 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015109# else
15110 n1 = res.tv_sec;
15111 n2 = res.tv_usec;
15112# endif
15113 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15114 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15115 }
15116#endif
15117}
15118
15119/*
15120 * "reltimestr()" function
15121 */
15122 static void
15123f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015124 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015125 typval_T *rettv;
15126{
15127#ifdef FEAT_RELTIME
15128 proftime_T tm;
15129#endif
15130
15131 rettv->v_type = VAR_STRING;
15132 rettv->vval.v_string = NULL;
15133#ifdef FEAT_RELTIME
15134 if (list2proftime(&argvars[0], &tm) == OK)
15135 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15136#endif
15137}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015138
Bram Moolenaar0d660222005-01-07 21:51:51 +000015139#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15140static void make_connection __ARGS((void));
15141static int check_connection __ARGS((void));
15142
15143 static void
15144make_connection()
15145{
15146 if (X_DISPLAY == NULL
15147# ifdef FEAT_GUI
15148 && !gui.in_use
15149# endif
15150 )
15151 {
15152 x_force_connect = TRUE;
15153 setup_term_clip();
15154 x_force_connect = FALSE;
15155 }
15156}
15157
15158 static int
15159check_connection()
15160{
15161 make_connection();
15162 if (X_DISPLAY == NULL)
15163 {
15164 EMSG(_("E240: No connection to Vim server"));
15165 return FAIL;
15166 }
15167 return OK;
15168}
15169#endif
15170
15171#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015172static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015173
15174 static void
15175remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015176 typval_T *argvars;
15177 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015178 int expr;
15179{
15180 char_u *server_name;
15181 char_u *keys;
15182 char_u *r = NULL;
15183 char_u buf[NUMBUFLEN];
15184# ifdef WIN32
15185 HWND w;
15186# else
15187 Window w;
15188# endif
15189
15190 if (check_restricted() || check_secure())
15191 return;
15192
15193# ifdef FEAT_X11
15194 if (check_connection() == FAIL)
15195 return;
15196# endif
15197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015198 server_name = get_tv_string_chk(&argvars[0]);
15199 if (server_name == NULL)
15200 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015201 keys = get_tv_string_buf(&argvars[1], buf);
15202# ifdef WIN32
15203 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15204# else
15205 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15206 < 0)
15207# endif
15208 {
15209 if (r != NULL)
15210 EMSG(r); /* sending worked but evaluation failed */
15211 else
15212 EMSG2(_("E241: Unable to send to %s"), server_name);
15213 return;
15214 }
15215
15216 rettv->vval.v_string = r;
15217
15218 if (argvars[2].v_type != VAR_UNKNOWN)
15219 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015220 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015221 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015222 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015223
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015224 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015225 v.di_tv.v_type = VAR_STRING;
15226 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015227 idvar = get_tv_string_chk(&argvars[2]);
15228 if (idvar != NULL)
15229 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015230 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015231 }
15232}
15233#endif
15234
15235/*
15236 * "remote_expr()" function
15237 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015238 static void
15239f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015240 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015241 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015242{
15243 rettv->v_type = VAR_STRING;
15244 rettv->vval.v_string = NULL;
15245#ifdef FEAT_CLIENTSERVER
15246 remote_common(argvars, rettv, TRUE);
15247#endif
15248}
15249
15250/*
15251 * "remote_foreground()" function
15252 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015253 static void
15254f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015255 typval_T *argvars UNUSED;
15256 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015257{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015258#ifdef FEAT_CLIENTSERVER
15259# ifdef WIN32
15260 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015261 {
15262 char_u *server_name = get_tv_string_chk(&argvars[0]);
15263
15264 if (server_name != NULL)
15265 serverForeground(server_name);
15266 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015267# else
15268 /* Send a foreground() expression to the server. */
15269 argvars[1].v_type = VAR_STRING;
15270 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15271 argvars[2].v_type = VAR_UNKNOWN;
15272 remote_common(argvars, rettv, TRUE);
15273 vim_free(argvars[1].vval.v_string);
15274# endif
15275#endif
15276}
15277
Bram Moolenaar0d660222005-01-07 21:51:51 +000015278 static void
15279f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015280 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015281 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015282{
15283#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015284 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015285 char_u *s = NULL;
15286# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015287 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015288# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015289 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015290
15291 if (check_restricted() || check_secure())
15292 {
15293 rettv->vval.v_number = -1;
15294 return;
15295 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015296 serverid = get_tv_string_chk(&argvars[0]);
15297 if (serverid == NULL)
15298 {
15299 rettv->vval.v_number = -1;
15300 return; /* type error; errmsg already given */
15301 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015302# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015303 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015304 if (n == 0)
15305 rettv->vval.v_number = -1;
15306 else
15307 {
15308 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15309 rettv->vval.v_number = (s != NULL);
15310 }
15311# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015312 if (check_connection() == FAIL)
15313 return;
15314
15315 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015316 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015317# endif
15318
15319 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015321 char_u *retvar;
15322
Bram Moolenaar33570922005-01-25 22:26:29 +000015323 v.di_tv.v_type = VAR_STRING;
15324 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015325 retvar = get_tv_string_chk(&argvars[1]);
15326 if (retvar != NULL)
15327 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015328 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015329 }
15330#else
15331 rettv->vval.v_number = -1;
15332#endif
15333}
15334
Bram Moolenaar0d660222005-01-07 21:51:51 +000015335 static void
15336f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015337 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015338 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339{
15340 char_u *r = NULL;
15341
15342#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015343 char_u *serverid = get_tv_string_chk(&argvars[0]);
15344
15345 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015346 {
15347# ifdef WIN32
15348 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015349 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015350
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015351 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015352 if (n != 0)
15353 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15354 if (r == NULL)
15355# else
15356 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015357 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015358# endif
15359 EMSG(_("E277: Unable to read a server reply"));
15360 }
15361#endif
15362 rettv->v_type = VAR_STRING;
15363 rettv->vval.v_string = r;
15364}
15365
15366/*
15367 * "remote_send()" function
15368 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015369 static void
15370f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015371 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015372 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015373{
15374 rettv->v_type = VAR_STRING;
15375 rettv->vval.v_string = NULL;
15376#ifdef FEAT_CLIENTSERVER
15377 remote_common(argvars, rettv, FALSE);
15378#endif
15379}
15380
15381/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015382 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015383 */
15384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015385f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015386 typval_T *argvars;
15387 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015388{
Bram Moolenaar33570922005-01-25 22:26:29 +000015389 list_T *l;
15390 listitem_T *item, *item2;
15391 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015392 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015393 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015394 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015395 dict_T *d;
15396 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015397 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015398
Bram Moolenaar8c711452005-01-14 21:53:12 +000015399 if (argvars[0].v_type == VAR_DICT)
15400 {
15401 if (argvars[2].v_type != VAR_UNKNOWN)
15402 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015403 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015404 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015406 key = get_tv_string_chk(&argvars[1]);
15407 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015409 di = dict_find(d, key, -1);
15410 if (di == NULL)
15411 EMSG2(_(e_dictkey), key);
15412 else
15413 {
15414 *rettv = di->di_tv;
15415 init_tv(&di->di_tv);
15416 dictitem_remove(d, di);
15417 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015419 }
15420 }
15421 else if (argvars[0].v_type != VAR_LIST)
15422 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015423 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015424 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015425 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015426 int error = FALSE;
15427
15428 idx = get_tv_number_chk(&argvars[1], &error);
15429 if (error)
15430 ; /* type error: do nothing, errmsg already given */
15431 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015432 EMSGN(_(e_listidx), idx);
15433 else
15434 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015435 if (argvars[2].v_type == VAR_UNKNOWN)
15436 {
15437 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015438 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015439 *rettv = item->li_tv;
15440 vim_free(item);
15441 }
15442 else
15443 {
15444 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015445 end = get_tv_number_chk(&argvars[2], &error);
15446 if (error)
15447 ; /* type error: do nothing */
15448 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015449 EMSGN(_(e_listidx), end);
15450 else
15451 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015452 int cnt = 0;
15453
15454 for (li = item; li != NULL; li = li->li_next)
15455 {
15456 ++cnt;
15457 if (li == item2)
15458 break;
15459 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015460 if (li == NULL) /* didn't find "item2" after "item" */
15461 EMSG(_(e_invrange));
15462 else
15463 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015464 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015465 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015466 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015467 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015468 l->lv_first = item;
15469 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015470 item->li_prev = NULL;
15471 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015472 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015473 }
15474 }
15475 }
15476 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015477 }
15478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479}
15480
15481/*
15482 * "rename({from}, {to})" function
15483 */
15484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015485f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015486 typval_T *argvars;
15487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015488{
15489 char_u buf[NUMBUFLEN];
15490
15491 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015492 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015494 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15495 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496}
15497
15498/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015499 * "repeat()" function
15500 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015501 static void
15502f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015503 typval_T *argvars;
15504 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015505{
15506 char_u *p;
15507 int n;
15508 int slen;
15509 int len;
15510 char_u *r;
15511 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015512
15513 n = get_tv_number(&argvars[1]);
15514 if (argvars[0].v_type == VAR_LIST)
15515 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015516 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015517 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015518 if (list_extend(rettv->vval.v_list,
15519 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015520 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015521 }
15522 else
15523 {
15524 p = get_tv_string(&argvars[0]);
15525 rettv->v_type = VAR_STRING;
15526 rettv->vval.v_string = NULL;
15527
15528 slen = (int)STRLEN(p);
15529 len = slen * n;
15530 if (len <= 0)
15531 return;
15532
15533 r = alloc(len + 1);
15534 if (r != NULL)
15535 {
15536 for (i = 0; i < n; i++)
15537 mch_memmove(r + i * slen, p, (size_t)slen);
15538 r[len] = NUL;
15539 }
15540
15541 rettv->vval.v_string = r;
15542 }
15543}
15544
15545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546 * "resolve()" function
15547 */
15548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015549f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015550 typval_T *argvars;
15551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552{
15553 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015554#ifdef HAVE_READLINK
15555 char_u *buf = NULL;
15556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015558 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559#ifdef FEAT_SHORTCUT
15560 {
15561 char_u *v = NULL;
15562
15563 v = mch_resolve_shortcut(p);
15564 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015565 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015567 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 }
15569#else
15570# ifdef HAVE_READLINK
15571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572 char_u *cpy;
15573 int len;
15574 char_u *remain = NULL;
15575 char_u *q;
15576 int is_relative_to_current = FALSE;
15577 int has_trailing_pathsep = FALSE;
15578 int limit = 100;
15579
15580 p = vim_strsave(p);
15581
15582 if (p[0] == '.' && (vim_ispathsep(p[1])
15583 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15584 is_relative_to_current = TRUE;
15585
15586 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015587 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015588 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015590 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592
15593 q = getnextcomp(p);
15594 if (*q != NUL)
15595 {
15596 /* Separate the first path component in "p", and keep the
15597 * remainder (beginning with the path separator). */
15598 remain = vim_strsave(q - 1);
15599 q[-1] = NUL;
15600 }
15601
Bram Moolenaard9462e32011-04-11 21:35:11 +020015602 buf = alloc(MAXPATHL + 1);
15603 if (buf == NULL)
15604 goto fail;
15605
Bram Moolenaar071d4272004-06-13 20:20:40 +000015606 for (;;)
15607 {
15608 for (;;)
15609 {
15610 len = readlink((char *)p, (char *)buf, MAXPATHL);
15611 if (len <= 0)
15612 break;
15613 buf[len] = NUL;
15614
15615 if (limit-- == 0)
15616 {
15617 vim_free(p);
15618 vim_free(remain);
15619 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015620 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 goto fail;
15622 }
15623
15624 /* Ensure that the result will have a trailing path separator
15625 * if the argument has one. */
15626 if (remain == NULL && has_trailing_pathsep)
15627 add_pathsep(buf);
15628
15629 /* Separate the first path component in the link value and
15630 * concatenate the remainders. */
15631 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15632 if (*q != NUL)
15633 {
15634 if (remain == NULL)
15635 remain = vim_strsave(q - 1);
15636 else
15637 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015638 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639 if (cpy != NULL)
15640 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 vim_free(remain);
15642 remain = cpy;
15643 }
15644 }
15645 q[-1] = NUL;
15646 }
15647
15648 q = gettail(p);
15649 if (q > p && *q == NUL)
15650 {
15651 /* Ignore trailing path separator. */
15652 q[-1] = NUL;
15653 q = gettail(p);
15654 }
15655 if (q > p && !mch_isFullName(buf))
15656 {
15657 /* symlink is relative to directory of argument */
15658 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15659 if (cpy != NULL)
15660 {
15661 STRCPY(cpy, p);
15662 STRCPY(gettail(cpy), buf);
15663 vim_free(p);
15664 p = cpy;
15665 }
15666 }
15667 else
15668 {
15669 vim_free(p);
15670 p = vim_strsave(buf);
15671 }
15672 }
15673
15674 if (remain == NULL)
15675 break;
15676
15677 /* Append the first path component of "remain" to "p". */
15678 q = getnextcomp(remain + 1);
15679 len = q - remain - (*q != NUL);
15680 cpy = vim_strnsave(p, STRLEN(p) + len);
15681 if (cpy != NULL)
15682 {
15683 STRNCAT(cpy, remain, len);
15684 vim_free(p);
15685 p = cpy;
15686 }
15687 /* Shorten "remain". */
15688 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015689 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 else
15691 {
15692 vim_free(remain);
15693 remain = NULL;
15694 }
15695 }
15696
15697 /* If the result is a relative path name, make it explicitly relative to
15698 * the current directory if and only if the argument had this form. */
15699 if (!vim_ispathsep(*p))
15700 {
15701 if (is_relative_to_current
15702 && *p != NUL
15703 && !(p[0] == '.'
15704 && (p[1] == NUL
15705 || vim_ispathsep(p[1])
15706 || (p[1] == '.'
15707 && (p[2] == NUL
15708 || vim_ispathsep(p[2]))))))
15709 {
15710 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015711 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712 if (cpy != NULL)
15713 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714 vim_free(p);
15715 p = cpy;
15716 }
15717 }
15718 else if (!is_relative_to_current)
15719 {
15720 /* Strip leading "./". */
15721 q = p;
15722 while (q[0] == '.' && vim_ispathsep(q[1]))
15723 q += 2;
15724 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015725 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726 }
15727 }
15728
15729 /* Ensure that the result will have no trailing path separator
15730 * if the argument had none. But keep "/" or "//". */
15731 if (!has_trailing_pathsep)
15732 {
15733 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015734 if (after_pathsep(p, q))
15735 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 }
15737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015738 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 }
15740# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015741 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742# endif
15743#endif
15744
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015745 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746
15747#ifdef HAVE_READLINK
15748fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015749 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015751 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752}
15753
15754/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015755 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756 */
15757 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015758f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015759 typval_T *argvars;
15760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761{
Bram Moolenaar33570922005-01-25 22:26:29 +000015762 list_T *l;
15763 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764
Bram Moolenaar0d660222005-01-07 21:51:51 +000015765 if (argvars[0].v_type != VAR_LIST)
15766 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015767 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015768 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015769 {
15770 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015771 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015772 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015773 while (li != NULL)
15774 {
15775 ni = li->li_prev;
15776 list_append(l, li);
15777 li = ni;
15778 }
15779 rettv->vval.v_list = l;
15780 rettv->v_type = VAR_LIST;
15781 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015782 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784}
15785
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015786#define SP_NOMOVE 0x01 /* don't move cursor */
15787#define SP_REPEAT 0x02 /* repeat to find outer pair */
15788#define SP_RETCOUNT 0x04 /* return matchcount */
15789#define SP_SETPCMARK 0x08 /* set previous context mark */
15790#define SP_START 0x10 /* accept match at start position */
15791#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15792#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015793
Bram Moolenaar33570922005-01-25 22:26:29 +000015794static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015795
15796/*
15797 * Get flags for a search function.
15798 * Possibly sets "p_ws".
15799 * Returns BACKWARD, FORWARD or zero (for an error).
15800 */
15801 static int
15802get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015803 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015804 int *flagsp;
15805{
15806 int dir = FORWARD;
15807 char_u *flags;
15808 char_u nbuf[NUMBUFLEN];
15809 int mask;
15810
15811 if (varp->v_type != VAR_UNKNOWN)
15812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015813 flags = get_tv_string_buf_chk(varp, nbuf);
15814 if (flags == NULL)
15815 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015816 while (*flags != NUL)
15817 {
15818 switch (*flags)
15819 {
15820 case 'b': dir = BACKWARD; break;
15821 case 'w': p_ws = TRUE; break;
15822 case 'W': p_ws = FALSE; break;
15823 default: mask = 0;
15824 if (flagsp != NULL)
15825 switch (*flags)
15826 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015827 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015828 case 'e': mask = SP_END; break;
15829 case 'm': mask = SP_RETCOUNT; break;
15830 case 'n': mask = SP_NOMOVE; break;
15831 case 'p': mask = SP_SUBPAT; break;
15832 case 'r': mask = SP_REPEAT; break;
15833 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015834 }
15835 if (mask == 0)
15836 {
15837 EMSG2(_(e_invarg2), flags);
15838 dir = 0;
15839 }
15840 else
15841 *flagsp |= mask;
15842 }
15843 if (dir == 0)
15844 break;
15845 ++flags;
15846 }
15847 }
15848 return dir;
15849}
15850
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015852 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015854 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015855search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015856 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015857 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015858 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015860 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861 char_u *pat;
15862 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015863 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864 int save_p_ws = p_ws;
15865 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015866 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015867 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015868 proftime_T tm;
15869#ifdef FEAT_RELTIME
15870 long time_limit = 0;
15871#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015872 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015873 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015875 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015876 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015877 if (dir == 0)
15878 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015879 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015880 if (flags & SP_START)
15881 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015882 if (flags & SP_END)
15883 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015884
Bram Moolenaar76929292008-01-06 19:07:36 +000015885 /* Optional arguments: line number to stop searching and timeout. */
15886 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015887 {
15888 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15889 if (lnum_stop < 0)
15890 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015891#ifdef FEAT_RELTIME
15892 if (argvars[3].v_type != VAR_UNKNOWN)
15893 {
15894 time_limit = get_tv_number_chk(&argvars[3], NULL);
15895 if (time_limit < 0)
15896 goto theend;
15897 }
15898#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015899 }
15900
Bram Moolenaar76929292008-01-06 19:07:36 +000015901#ifdef FEAT_RELTIME
15902 /* Set the time limit, if there is one. */
15903 profile_setlimit(time_limit, &tm);
15904#endif
15905
Bram Moolenaar231334e2005-07-25 20:46:57 +000015906 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015907 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015908 * Check to make sure only those flags are set.
15909 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15910 * flags cannot be set. Check for that condition also.
15911 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015912 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015913 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015915 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015916 goto theend;
15917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015919 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015920 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015921 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015922 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015923 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015924 if (flags & SP_SUBPAT)
15925 retval = subpatnum;
15926 else
15927 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015928 if (flags & SP_SETPCMARK)
15929 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015931 if (match_pos != NULL)
15932 {
15933 /* Store the match cursor position */
15934 match_pos->lnum = pos.lnum;
15935 match_pos->col = pos.col + 1;
15936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015937 /* "/$" will put the cursor after the end of the line, may need to
15938 * correct that here */
15939 check_cursor();
15940 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015941
15942 /* If 'n' flag is used: restore cursor position. */
15943 if (flags & SP_NOMOVE)
15944 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015945 else
15946 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015947theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015948 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015949
15950 return retval;
15951}
15952
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015953#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015954
15955/*
15956 * round() is not in C90, use ceil() or floor() instead.
15957 */
15958 float_T
15959vim_round(f)
15960 float_T f;
15961{
15962 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15963}
15964
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015965/*
15966 * "round({float})" function
15967 */
15968 static void
15969f_round(argvars, rettv)
15970 typval_T *argvars;
15971 typval_T *rettv;
15972{
15973 float_T f;
15974
15975 rettv->v_type = VAR_FLOAT;
15976 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015977 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015978 else
15979 rettv->vval.v_float = 0.0;
15980}
15981#endif
15982
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015983/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015984 * "screenattr()" function
15985 */
15986 static void
15987f_screenattr(argvars, rettv)
15988 typval_T *argvars UNUSED;
15989 typval_T *rettv;
15990{
15991 int row;
15992 int col;
15993 int c;
15994
15995 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15996 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15997 if (row < 0 || row >= screen_Rows
15998 || col < 0 || col >= screen_Columns)
15999 c = -1;
16000 else
16001 c = ScreenAttrs[LineOffset[row] + col];
16002 rettv->vval.v_number = c;
16003}
16004
16005/*
16006 * "screenchar()" function
16007 */
16008 static void
16009f_screenchar(argvars, rettv)
16010 typval_T *argvars UNUSED;
16011 typval_T *rettv;
16012{
16013 int row;
16014 int col;
16015 int off;
16016 int c;
16017
16018 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16019 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16020 if (row < 0 || row >= screen_Rows
16021 || col < 0 || col >= screen_Columns)
16022 c = -1;
16023 else
16024 {
16025 off = LineOffset[row] + col;
16026#ifdef FEAT_MBYTE
16027 if (enc_utf8 && ScreenLinesUC[off] != 0)
16028 c = ScreenLinesUC[off];
16029 else
16030#endif
16031 c = ScreenLines[off];
16032 }
16033 rettv->vval.v_number = c;
16034}
16035
16036/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016037 * "screencol()" function
16038 *
16039 * First column is 1 to be consistent with virtcol().
16040 */
16041 static void
16042f_screencol(argvars, rettv)
16043 typval_T *argvars UNUSED;
16044 typval_T *rettv;
16045{
16046 rettv->vval.v_number = screen_screencol() + 1;
16047}
16048
16049/*
16050 * "screenrow()" function
16051 */
16052 static void
16053f_screenrow(argvars, rettv)
16054 typval_T *argvars UNUSED;
16055 typval_T *rettv;
16056{
16057 rettv->vval.v_number = screen_screenrow() + 1;
16058}
16059
16060/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016061 * "search()" function
16062 */
16063 static void
16064f_search(argvars, rettv)
16065 typval_T *argvars;
16066 typval_T *rettv;
16067{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016068 int flags = 0;
16069
16070 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071}
16072
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016074 * "searchdecl()" function
16075 */
16076 static void
16077f_searchdecl(argvars, rettv)
16078 typval_T *argvars;
16079 typval_T *rettv;
16080{
16081 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016082 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016083 int error = FALSE;
16084 char_u *name;
16085
16086 rettv->vval.v_number = 1; /* default: FAIL */
16087
16088 name = get_tv_string_chk(&argvars[0]);
16089 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016090 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016091 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016092 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16093 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16094 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016095 if (!error && name != NULL)
16096 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016097 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016098}
16099
16100/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016101 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016103 static int
16104searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016105 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016106 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107{
16108 char_u *spat, *mpat, *epat;
16109 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111 int dir;
16112 int flags = 0;
16113 char_u nbuf1[NUMBUFLEN];
16114 char_u nbuf2[NUMBUFLEN];
16115 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016116 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016117 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016118 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016121 spat = get_tv_string_chk(&argvars[0]);
16122 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16123 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16124 if (spat == NULL || mpat == NULL || epat == NULL)
16125 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126
Bram Moolenaar071d4272004-06-13 20:20:40 +000016127 /* Handle the optional fourth argument: flags */
16128 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016129 if (dir == 0)
16130 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016131
16132 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016133 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16134 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016135 if ((flags & (SP_END | SP_SUBPAT)) != 0
16136 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016137 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016138 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016139 goto theend;
16140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016142 /* Using 'r' implies 'W', otherwise it doesn't work. */
16143 if (flags & SP_REPEAT)
16144 p_ws = FALSE;
16145
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016146 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016147 if (argvars[3].v_type == VAR_UNKNOWN
16148 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149 skip = (char_u *)"";
16150 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016152 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016153 if (argvars[5].v_type != VAR_UNKNOWN)
16154 {
16155 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16156 if (lnum_stop < 0)
16157 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016158#ifdef FEAT_RELTIME
16159 if (argvars[6].v_type != VAR_UNKNOWN)
16160 {
16161 time_limit = get_tv_number_chk(&argvars[6], NULL);
16162 if (time_limit < 0)
16163 goto theend;
16164 }
16165#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016166 }
16167 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016168 if (skip == NULL)
16169 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016171 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016172 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016173
16174theend:
16175 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016176
16177 return retval;
16178}
16179
16180/*
16181 * "searchpair()" function
16182 */
16183 static void
16184f_searchpair(argvars, rettv)
16185 typval_T *argvars;
16186 typval_T *rettv;
16187{
16188 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16189}
16190
16191/*
16192 * "searchpairpos()" function
16193 */
16194 static void
16195f_searchpairpos(argvars, rettv)
16196 typval_T *argvars;
16197 typval_T *rettv;
16198{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016199 pos_T match_pos;
16200 int lnum = 0;
16201 int col = 0;
16202
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016203 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016204 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016205
16206 if (searchpair_cmn(argvars, &match_pos) > 0)
16207 {
16208 lnum = match_pos.lnum;
16209 col = match_pos.col;
16210 }
16211
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016212 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16213 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016214}
16215
16216/*
16217 * Search for a start/middle/end thing.
16218 * Used by searchpair(), see its documentation for the details.
16219 * Returns 0 or -1 for no match,
16220 */
16221 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016222do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16223 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016224 char_u *spat; /* start pattern */
16225 char_u *mpat; /* middle pattern */
16226 char_u *epat; /* end pattern */
16227 int dir; /* BACKWARD or FORWARD */
16228 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016229 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016230 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016231 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016232 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016233{
16234 char_u *save_cpo;
16235 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16236 long retval = 0;
16237 pos_T pos;
16238 pos_T firstpos;
16239 pos_T foundpos;
16240 pos_T save_cursor;
16241 pos_T save_pos;
16242 int n;
16243 int r;
16244 int nest = 1;
16245 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016246 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016247 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016248
16249 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16250 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016251 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016252
Bram Moolenaar76929292008-01-06 19:07:36 +000016253#ifdef FEAT_RELTIME
16254 /* Set the time limit, if there is one. */
16255 profile_setlimit(time_limit, &tm);
16256#endif
16257
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016258 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16259 * start/middle/end (pat3, for the top pair). */
16260 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16261 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16262 if (pat2 == NULL || pat3 == NULL)
16263 goto theend;
16264 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16265 if (*mpat == NUL)
16266 STRCPY(pat3, pat2);
16267 else
16268 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16269 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016270 if (flags & SP_START)
16271 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016272
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 save_cursor = curwin->w_cursor;
16274 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016275 clearpos(&firstpos);
16276 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277 pat = pat3;
16278 for (;;)
16279 {
16280 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016281 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16283 /* didn't find it or found the first match again: FAIL */
16284 break;
16285
16286 if (firstpos.lnum == 0)
16287 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016288 if (equalpos(pos, foundpos))
16289 {
16290 /* Found the same position again. Can happen with a pattern that
16291 * has "\zs" at the end and searching backwards. Advance one
16292 * character and try again. */
16293 if (dir == BACKWARD)
16294 decl(&pos);
16295 else
16296 incl(&pos);
16297 }
16298 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016300 /* clear the start flag to avoid getting stuck here */
16301 options &= ~SEARCH_START;
16302
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303 /* If the skip pattern matches, ignore this match. */
16304 if (*skip != NUL)
16305 {
16306 save_pos = curwin->w_cursor;
16307 curwin->w_cursor = pos;
16308 r = eval_to_bool(skip, &err, NULL, FALSE);
16309 curwin->w_cursor = save_pos;
16310 if (err)
16311 {
16312 /* Evaluating {skip} caused an error, break here. */
16313 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016314 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315 break;
16316 }
16317 if (r)
16318 continue;
16319 }
16320
16321 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16322 {
16323 /* Found end when searching backwards or start when searching
16324 * forward: nested pair. */
16325 ++nest;
16326 pat = pat2; /* nested, don't search for middle */
16327 }
16328 else
16329 {
16330 /* Found end when searching forward or start when searching
16331 * backward: end of (nested) pair; or found middle in outer pair. */
16332 if (--nest == 1)
16333 pat = pat3; /* outer level, search for middle */
16334 }
16335
16336 if (nest == 0)
16337 {
16338 /* Found the match: return matchcount or line number. */
16339 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016340 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016342 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016343 if (flags & SP_SETPCMARK)
16344 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016345 curwin->w_cursor = pos;
16346 if (!(flags & SP_REPEAT))
16347 break;
16348 nest = 1; /* search for next unmatched */
16349 }
16350 }
16351
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016352 if (match_pos != NULL)
16353 {
16354 /* Store the match cursor position */
16355 match_pos->lnum = curwin->w_cursor.lnum;
16356 match_pos->col = curwin->w_cursor.col + 1;
16357 }
16358
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016360 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361 curwin->w_cursor = save_cursor;
16362
16363theend:
16364 vim_free(pat2);
16365 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016366 if (p_cpo == empty_option)
16367 p_cpo = save_cpo;
16368 else
16369 /* Darn, evaluating the {skip} expression changed the value. */
16370 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016371
16372 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373}
16374
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016375/*
16376 * "searchpos()" function
16377 */
16378 static void
16379f_searchpos(argvars, rettv)
16380 typval_T *argvars;
16381 typval_T *rettv;
16382{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016383 pos_T match_pos;
16384 int lnum = 0;
16385 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016386 int n;
16387 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016388
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016389 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016390 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016391
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016392 n = search_cmn(argvars, &match_pos, &flags);
16393 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016394 {
16395 lnum = match_pos.lnum;
16396 col = match_pos.col;
16397 }
16398
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016399 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16400 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016401 if (flags & SP_SUBPAT)
16402 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016403}
16404
16405
Bram Moolenaar0d660222005-01-07 21:51:51 +000016406 static void
16407f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016408 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016411#ifdef FEAT_CLIENTSERVER
16412 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016413 char_u *server = get_tv_string_chk(&argvars[0]);
16414 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415
Bram Moolenaar0d660222005-01-07 21:51:51 +000016416 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016417 if (server == NULL || reply == NULL)
16418 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016419 if (check_restricted() || check_secure())
16420 return;
16421# ifdef FEAT_X11
16422 if (check_connection() == FAIL)
16423 return;
16424# endif
16425
16426 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016428 EMSG(_("E258: Unable to send to client"));
16429 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016431 rettv->vval.v_number = 0;
16432#else
16433 rettv->vval.v_number = -1;
16434#endif
16435}
16436
Bram Moolenaar0d660222005-01-07 21:51:51 +000016437 static void
16438f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016439 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016440 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016441{
16442 char_u *r = NULL;
16443
16444#ifdef FEAT_CLIENTSERVER
16445# ifdef WIN32
16446 r = serverGetVimNames();
16447# else
16448 make_connection();
16449 if (X_DISPLAY != NULL)
16450 r = serverGetVimNames(X_DISPLAY);
16451# endif
16452#endif
16453 rettv->v_type = VAR_STRING;
16454 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455}
16456
16457/*
16458 * "setbufvar()" function
16459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016461f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016462 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016463 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464{
16465 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016468 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469 char_u nbuf[NUMBUFLEN];
16470
16471 if (check_restricted() || check_secure())
16472 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016473 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16474 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016475 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476 varp = &argvars[2];
16477
16478 if (buf != NULL && varname != NULL && varp != NULL)
16479 {
16480 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482
16483 if (*varname == '&')
16484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016485 long numval;
16486 char_u *strval;
16487 int error = FALSE;
16488
Bram Moolenaar071d4272004-06-13 20:20:40 +000016489 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016490 numval = get_tv_number_chk(varp, &error);
16491 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016492 if (!error && strval != NULL)
16493 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 }
16495 else
16496 {
16497 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16498 if (bufvarname != NULL)
16499 {
16500 STRCPY(bufvarname, "b:");
16501 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016502 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 vim_free(bufvarname);
16504 }
16505 }
16506
16507 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016508 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510}
16511
16512/*
16513 * "setcmdpos()" function
16514 */
16515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016516f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016517 typval_T *argvars;
16518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016520 int pos = (int)get_tv_number(&argvars[0]) - 1;
16521
16522 if (pos >= 0)
16523 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524}
16525
16526/*
16527 * "setline()" function
16528 */
16529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016530f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016531 typval_T *argvars;
16532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533{
16534 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016535 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016536 list_T *l = NULL;
16537 listitem_T *li = NULL;
16538 long added = 0;
16539 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016541 lnum = get_tv_lnum(&argvars[0]);
16542 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016544 l = argvars[1].vval.v_list;
16545 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016547 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016548 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016549
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016550 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016551 for (;;)
16552 {
16553 if (l != NULL)
16554 {
16555 /* list argument, get next string */
16556 if (li == NULL)
16557 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016558 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016559 li = li->li_next;
16560 }
16561
16562 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016563 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016564 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016565
16566 /* When coming here from Insert mode, sync undo, so that this can be
16567 * undone separately from what was previously inserted. */
16568 if (u_sync_once == 2)
16569 {
16570 u_sync_once = 1; /* notify that u_sync() was called */
16571 u_sync(TRUE);
16572 }
16573
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016574 if (lnum <= curbuf->b_ml.ml_line_count)
16575 {
16576 /* existing line, replace it */
16577 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16578 {
16579 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016580 if (lnum == curwin->w_cursor.lnum)
16581 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016582 rettv->vval.v_number = 0; /* OK */
16583 }
16584 }
16585 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16586 {
16587 /* lnum is one past the last line, append the line */
16588 ++added;
16589 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16590 rettv->vval.v_number = 0; /* OK */
16591 }
16592
16593 if (l == NULL) /* only one string argument */
16594 break;
16595 ++lnum;
16596 }
16597
16598 if (added > 0)
16599 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600}
16601
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016602static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16603
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016605 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016606 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016607 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016608set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016609 win_T *wp UNUSED;
16610 typval_T *list_arg UNUSED;
16611 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016612 typval_T *rettv;
16613{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016614#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016615 char_u *act;
16616 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016617#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016618
Bram Moolenaar2641f772005-03-25 21:58:17 +000016619 rettv->vval.v_number = -1;
16620
16621#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016622 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016623 EMSG(_(e_listreq));
16624 else
16625 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016626 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016627
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016628 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016629 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016630 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016631 if (act == NULL)
16632 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016633 if (*act == 'a' || *act == 'r')
16634 action = *act;
16635 }
16636
Bram Moolenaar81484f42012-12-05 15:16:47 +010016637 if (l != NULL && set_errorlist(wp, l, action,
16638 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016639 rettv->vval.v_number = 0;
16640 }
16641#endif
16642}
16643
16644/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016645 * "setloclist()" function
16646 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016647 static void
16648f_setloclist(argvars, rettv)
16649 typval_T *argvars;
16650 typval_T *rettv;
16651{
16652 win_T *win;
16653
16654 rettv->vval.v_number = -1;
16655
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016656 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016657 if (win != NULL)
16658 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16659}
16660
16661/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016662 * "setmatches()" function
16663 */
16664 static void
16665f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016666 typval_T *argvars UNUSED;
16667 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016668{
16669#ifdef FEAT_SEARCH_EXTRA
16670 list_T *l;
16671 listitem_T *li;
16672 dict_T *d;
16673
16674 rettv->vval.v_number = -1;
16675 if (argvars[0].v_type != VAR_LIST)
16676 {
16677 EMSG(_(e_listreq));
16678 return;
16679 }
16680 if ((l = argvars[0].vval.v_list) != NULL)
16681 {
16682
16683 /* To some extent make sure that we are dealing with a list from
16684 * "getmatches()". */
16685 li = l->lv_first;
16686 while (li != NULL)
16687 {
16688 if (li->li_tv.v_type != VAR_DICT
16689 || (d = li->li_tv.vval.v_dict) == NULL)
16690 {
16691 EMSG(_(e_invarg));
16692 return;
16693 }
16694 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16695 && dict_find(d, (char_u *)"pattern", -1) != NULL
16696 && dict_find(d, (char_u *)"priority", -1) != NULL
16697 && dict_find(d, (char_u *)"id", -1) != NULL))
16698 {
16699 EMSG(_(e_invarg));
16700 return;
16701 }
16702 li = li->li_next;
16703 }
16704
16705 clear_matches(curwin);
16706 li = l->lv_first;
16707 while (li != NULL)
16708 {
16709 d = li->li_tv.vval.v_dict;
16710 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16711 get_dict_string(d, (char_u *)"pattern", FALSE),
16712 (int)get_dict_number(d, (char_u *)"priority"),
16713 (int)get_dict_number(d, (char_u *)"id"));
16714 li = li->li_next;
16715 }
16716 rettv->vval.v_number = 0;
16717 }
16718#endif
16719}
16720
16721/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016722 * "setpos()" function
16723 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016724 static void
16725f_setpos(argvars, rettv)
16726 typval_T *argvars;
16727 typval_T *rettv;
16728{
16729 pos_T pos;
16730 int fnum;
16731 char_u *name;
16732
Bram Moolenaar08250432008-02-13 11:42:46 +000016733 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016734 name = get_tv_string_chk(argvars);
16735 if (name != NULL)
16736 {
16737 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16738 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016739 if (--pos.col < 0)
16740 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016741 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016742 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016743 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016744 if (fnum == curbuf->b_fnum)
16745 {
16746 curwin->w_cursor = pos;
16747 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016748 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016749 }
16750 else
16751 EMSG(_(e_invarg));
16752 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016753 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16754 {
16755 /* set mark */
16756 if (setmark_pos(name[1], &pos, fnum) == OK)
16757 rettv->vval.v_number = 0;
16758 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016759 else
16760 EMSG(_(e_invarg));
16761 }
16762 }
16763}
16764
16765/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016766 * "setqflist()" function
16767 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016768 static void
16769f_setqflist(argvars, rettv)
16770 typval_T *argvars;
16771 typval_T *rettv;
16772{
16773 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16774}
16775
16776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777 * "setreg()" function
16778 */
16779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016780f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016781 typval_T *argvars;
16782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783{
16784 int regname;
16785 char_u *strregname;
16786 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016787 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788 int append;
16789 char_u yank_type;
16790 long block_len;
16791
16792 block_len = -1;
16793 yank_type = MAUTO;
16794 append = FALSE;
16795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016796 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016797 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016799 if (strregname == NULL)
16800 return; /* type error; errmsg already given */
16801 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 if (regname == 0 || regname == '@')
16803 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016805 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016807 stropt = get_tv_string_chk(&argvars[2]);
16808 if (stropt == NULL)
16809 return; /* type error */
16810 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811 switch (*stropt)
16812 {
16813 case 'a': case 'A': /* append */
16814 append = TRUE;
16815 break;
16816 case 'v': case 'c': /* character-wise selection */
16817 yank_type = MCHAR;
16818 break;
16819 case 'V': case 'l': /* line-wise selection */
16820 yank_type = MLINE;
16821 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822 case 'b': case Ctrl_V: /* block-wise selection */
16823 yank_type = MBLOCK;
16824 if (VIM_ISDIGIT(stropt[1]))
16825 {
16826 ++stropt;
16827 block_len = getdigits(&stropt) - 1;
16828 --stropt;
16829 }
16830 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831 }
16832 }
16833
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016834 if (argvars[1].v_type == VAR_LIST)
16835 {
16836 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016837 char_u **allocval;
16838 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016839 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016840 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016841 int len = argvars[1].vval.v_list->lv_len;
16842 listitem_T *li;
16843
Bram Moolenaar7d647822014-04-05 21:28:56 +020016844 /* First half: use for pointers to result lines; second half: use for
16845 * pointers to allocated copies. */
16846 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016847 if (lstval == NULL)
16848 return;
16849 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016850 allocval = lstval + len + 2;
16851 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016852
16853 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
16854 li = li->li_next)
16855 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016856 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016857 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020016858 goto free_lstval;
16859 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016860 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016861 /* Need to make a copy, next get_tv_string_buf_chk() will
16862 * overwrite the string. */
16863 strval = vim_strsave(buf);
16864 if (strval == NULL)
16865 goto free_lstval;
16866 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016867 }
16868 *curval++ = strval;
16869 }
16870 *curval++ = NULL;
16871
16872 write_reg_contents_lst(regname, lstval, -1,
16873 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020016874free_lstval:
16875 while (curallocval > allocval)
16876 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016877 vim_free(lstval);
16878 }
16879 else
16880 {
16881 strval = get_tv_string_chk(&argvars[1]);
16882 if (strval == NULL)
16883 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016884 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016887 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888}
16889
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016890/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016891 * "settabvar()" function
16892 */
16893 static void
16894f_settabvar(argvars, rettv)
16895 typval_T *argvars;
16896 typval_T *rettv;
16897{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016898#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016899 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016900 tabpage_T *tp;
16901#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016902 char_u *varname, *tabvarname;
16903 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016904
16905 rettv->vval.v_number = 0;
16906
16907 if (check_restricted() || check_secure())
16908 return;
16909
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016910#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016911 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016912#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016913 varname = get_tv_string_chk(&argvars[1]);
16914 varp = &argvars[2];
16915
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016916 if (varname != NULL && varp != NULL
16917#ifdef FEAT_WINDOWS
16918 && tp != NULL
16919#endif
16920 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016921 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016922#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016923 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016924 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016925#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016926
16927 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16928 if (tabvarname != NULL)
16929 {
16930 STRCPY(tabvarname, "t:");
16931 STRCPY(tabvarname + 2, varname);
16932 set_var(tabvarname, varp, TRUE);
16933 vim_free(tabvarname);
16934 }
16935
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016936#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016937 /* Restore current tabpage */
16938 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016939 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016940#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016941 }
16942}
16943
16944/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016945 * "settabwinvar()" function
16946 */
16947 static void
16948f_settabwinvar(argvars, rettv)
16949 typval_T *argvars;
16950 typval_T *rettv;
16951{
16952 setwinvar(argvars, rettv, 1);
16953}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954
16955/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016956 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016959f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016960 typval_T *argvars;
16961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016963 setwinvar(argvars, rettv, 0);
16964}
16965
16966/*
16967 * "setwinvar()" and "settabwinvar()" functions
16968 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016969
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016970 static void
16971setwinvar(argvars, rettv, off)
16972 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016973 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016974 int off;
16975{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 win_T *win;
16977#ifdef FEAT_WINDOWS
16978 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016979 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980#endif
16981 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016982 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016984 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985
16986 if (check_restricted() || check_secure())
16987 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016988
16989#ifdef FEAT_WINDOWS
16990 if (off == 1)
16991 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16992 else
16993 tp = curtab;
16994#endif
16995 win = find_win_by_nr(&argvars[off], tp);
16996 varname = get_tv_string_chk(&argvars[off + 1]);
16997 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998
16999 if (win != NULL && varname != NULL && varp != NULL)
17000 {
17001#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017002 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017003 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004#endif
17005
17006 if (*varname == '&')
17007 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017008 long numval;
17009 char_u *strval;
17010 int error = FALSE;
17011
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017013 numval = get_tv_number_chk(varp, &error);
17014 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017015 if (!error && strval != NULL)
17016 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 }
17018 else
17019 {
17020 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17021 if (winvarname != NULL)
17022 {
17023 STRCPY(winvarname, "w:");
17024 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017025 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 vim_free(winvarname);
17027 }
17028 }
17029
17030#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017031 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032#endif
17033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034}
17035
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017036#ifdef FEAT_CRYPT
17037/*
17038 * "sha256({string})" function
17039 */
17040 static void
17041f_sha256(argvars, rettv)
17042 typval_T *argvars;
17043 typval_T *rettv;
17044{
17045 char_u *p;
17046
17047 p = get_tv_string(&argvars[0]);
17048 rettv->vval.v_string = vim_strsave(
17049 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17050 rettv->v_type = VAR_STRING;
17051}
17052#endif /* FEAT_CRYPT */
17053
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017055 * "shellescape({string})" function
17056 */
17057 static void
17058f_shellescape(argvars, rettv)
17059 typval_T *argvars;
17060 typval_T *rettv;
17061{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017062 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017063 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017064 rettv->v_type = VAR_STRING;
17065}
17066
17067/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017068 * shiftwidth() function
17069 */
17070 static void
17071f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017072 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017073 typval_T *rettv;
17074{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017075 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017076}
17077
17078/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017079 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080 */
17081 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017083 typval_T *argvars;
17084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017086 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087
Bram Moolenaar0d660222005-01-07 21:51:51 +000017088 p = get_tv_string(&argvars[0]);
17089 rettv->vval.v_string = vim_strsave(p);
17090 simplify_filename(rettv->vval.v_string); /* simplify in place */
17091 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092}
17093
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017094#ifdef FEAT_FLOAT
17095/*
17096 * "sin()" function
17097 */
17098 static void
17099f_sin(argvars, rettv)
17100 typval_T *argvars;
17101 typval_T *rettv;
17102{
17103 float_T f;
17104
17105 rettv->v_type = VAR_FLOAT;
17106 if (get_float_arg(argvars, &f) == OK)
17107 rettv->vval.v_float = sin(f);
17108 else
17109 rettv->vval.v_float = 0.0;
17110}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017111
17112/*
17113 * "sinh()" function
17114 */
17115 static void
17116f_sinh(argvars, rettv)
17117 typval_T *argvars;
17118 typval_T *rettv;
17119{
17120 float_T f;
17121
17122 rettv->v_type = VAR_FLOAT;
17123 if (get_float_arg(argvars, &f) == OK)
17124 rettv->vval.v_float = sinh(f);
17125 else
17126 rettv->vval.v_float = 0.0;
17127}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017128#endif
17129
Bram Moolenaar0d660222005-01-07 21:51:51 +000017130static int
17131#ifdef __BORLANDC__
17132 _RTLENTRYF
17133#endif
17134 item_compare __ARGS((const void *s1, const void *s2));
17135static int
17136#ifdef __BORLANDC__
17137 _RTLENTRYF
17138#endif
17139 item_compare2 __ARGS((const void *s1, const void *s2));
17140
17141static int item_compare_ic;
17142static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017143static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017144static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017145static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017146#define ITEM_COMPARE_FAIL 999
17147
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017149 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017151 static int
17152#ifdef __BORLANDC__
17153_RTLENTRYF
17154#endif
17155item_compare(s1, s2)
17156 const void *s1;
17157 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017159 char_u *p1, *p2;
17160 char_u *tofree1, *tofree2;
17161 int res;
17162 char_u numbuf1[NUMBUFLEN];
17163 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017165 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17166 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017167 if (p1 == NULL)
17168 p1 = (char_u *)"";
17169 if (p2 == NULL)
17170 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017171 if (item_compare_ic)
17172 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017174 res = STRCMP(p1, p2);
17175 vim_free(tofree1);
17176 vim_free(tofree2);
17177 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178}
17179
17180 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017181#ifdef __BORLANDC__
17182_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017184item_compare2(s1, s2)
17185 const void *s1;
17186 const void *s2;
17187{
17188 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017189 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017190 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017191 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017193 /* shortcut after failure in previous call; compare all items equal */
17194 if (item_compare_func_err)
17195 return 0;
17196
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017197 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17198 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017199 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17200 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017201
17202 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017203 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017204 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17205 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017206 clear_tv(&argv[0]);
17207 clear_tv(&argv[1]);
17208
17209 if (res == FAIL)
17210 res = ITEM_COMPARE_FAIL;
17211 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017212 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17213 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017214 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017215 clear_tv(&rettv);
17216 return res;
17217}
17218
17219/*
17220 * "sort({list})" function
17221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017223do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017224 typval_T *argvars;
17225 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017226 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227{
Bram Moolenaar33570922005-01-25 22:26:29 +000017228 list_T *l;
17229 listitem_T *li;
17230 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017231 long len;
17232 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233
Bram Moolenaar0d660222005-01-07 21:51:51 +000017234 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017235 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 else
17237 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017238 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017239 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017240 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017241 return;
17242 rettv->vval.v_list = l;
17243 rettv->v_type = VAR_LIST;
17244 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245
Bram Moolenaar0d660222005-01-07 21:51:51 +000017246 len = list_len(l);
17247 if (len <= 1)
17248 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249
Bram Moolenaar0d660222005-01-07 21:51:51 +000017250 item_compare_ic = FALSE;
17251 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017252 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017253 if (argvars[1].v_type != VAR_UNKNOWN)
17254 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017255 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017256 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017257 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017258 else
17259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017260 int error = FALSE;
17261
17262 i = get_tv_number_chk(&argvars[1], &error);
17263 if (error)
17264 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017265 if (i == 1)
17266 item_compare_ic = TRUE;
17267 else
17268 item_compare_func = get_tv_string(&argvars[1]);
17269 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017270
17271 if (argvars[2].v_type != VAR_UNKNOWN)
17272 {
17273 /* optional third argument: {dict} */
17274 if (argvars[2].v_type != VAR_DICT)
17275 {
17276 EMSG(_(e_dictreq));
17277 return;
17278 }
17279 item_compare_selfdict = argvars[2].vval.v_dict;
17280 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282
Bram Moolenaar0d660222005-01-07 21:51:51 +000017283 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017284 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017285 if (ptrs == NULL)
17286 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287
Bram Moolenaar327aa022014-03-25 18:24:23 +010017288 i = 0;
17289 if (sort)
17290 {
17291 /* sort(): ptrs will be the list to sort */
17292 for (li = l->lv_first; li != NULL; li = li->li_next)
17293 ptrs[i++] = li;
17294
17295 item_compare_func_err = FALSE;
17296 /* test the compare function */
17297 if (item_compare_func != NULL
17298 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017299 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017300 EMSG(_("E702: Sort compare function failed"));
17301 else
17302 {
17303 /* Sort the array with item pointers. */
17304 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17305 item_compare_func == NULL ? item_compare : item_compare2);
17306
17307 if (!item_compare_func_err)
17308 {
17309 /* Clear the List and append the items in sorted order. */
17310 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17311 l->lv_len = 0;
17312 for (i = 0; i < len; ++i)
17313 list_append(l, ptrs[i]);
17314 }
17315 }
17316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017318 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017319 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17320
17321 /* f_uniq(): ptrs will be a stack of items to remove */
17322 item_compare_func_err = FALSE;
17323 item_compare_func_ptr = item_compare_func
17324 ? item_compare2 : item_compare;
17325
17326 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17327 li = li->li_next)
17328 {
17329 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17330 == 0)
17331 ptrs[i++] = li;
17332 if (item_compare_func_err)
17333 {
17334 EMSG(_("E882: Uniq compare function failed"));
17335 break;
17336 }
17337 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017338
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017339 if (!item_compare_func_err)
17340 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017341 while (--i >= 0)
17342 {
17343 li = ptrs[i]->li_next;
17344 ptrs[i]->li_next = li->li_next;
17345 if (li->li_next != NULL)
17346 li->li_next->li_prev = ptrs[i];
17347 else
17348 l->lv_last = ptrs[i];
17349 list_fix_watch(l, li);
17350 listitem_free(li);
17351 l->lv_len--;
17352 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017353 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017354 }
17355
17356 vim_free(ptrs);
17357 }
17358}
17359
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017360/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017361 * "sort({list})" function
17362 */
17363 static void
17364f_sort(argvars, rettv)
17365 typval_T *argvars;
17366 typval_T *rettv;
17367{
17368 do_sort_uniq(argvars, rettv, TRUE);
17369}
17370
17371/*
17372 * "uniq({list})" function
17373 */
17374 static void
17375f_uniq(argvars, rettv)
17376 typval_T *argvars;
17377 typval_T *rettv;
17378{
17379 do_sort_uniq(argvars, rettv, FALSE);
17380}
17381
17382/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017383 * "soundfold({word})" function
17384 */
17385 static void
17386f_soundfold(argvars, rettv)
17387 typval_T *argvars;
17388 typval_T *rettv;
17389{
17390 char_u *s;
17391
17392 rettv->v_type = VAR_STRING;
17393 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017394#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017395 rettv->vval.v_string = eval_soundfold(s);
17396#else
17397 rettv->vval.v_string = vim_strsave(s);
17398#endif
17399}
17400
17401/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017402 * "spellbadword()" function
17403 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017404 static void
17405f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017406 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017407 typval_T *rettv;
17408{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017409 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017410 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017411 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017412
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017413 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017414 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017415
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017416#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017417 if (argvars[0].v_type == VAR_UNKNOWN)
17418 {
17419 /* Find the start and length of the badly spelled word. */
17420 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17421 if (len != 0)
17422 word = ml_get_cursor();
17423 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017424 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017425 {
17426 char_u *str = get_tv_string_chk(&argvars[0]);
17427 int capcol = -1;
17428
17429 if (str != NULL)
17430 {
17431 /* Check the argument for spelling. */
17432 while (*str != NUL)
17433 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017434 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017435 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017436 {
17437 word = str;
17438 break;
17439 }
17440 str += len;
17441 }
17442 }
17443 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017444#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017445
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017446 list_append_string(rettv->vval.v_list, word, len);
17447 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017448 attr == HLF_SPB ? "bad" :
17449 attr == HLF_SPR ? "rare" :
17450 attr == HLF_SPL ? "local" :
17451 attr == HLF_SPC ? "caps" :
17452 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017453}
17454
17455/*
17456 * "spellsuggest()" function
17457 */
17458 static void
17459f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017460 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017461 typval_T *rettv;
17462{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017463#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017464 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017465 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017466 int maxcount;
17467 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017468 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017469 listitem_T *li;
17470 int need_capital = FALSE;
17471#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017472
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017473 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017474 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017475
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017476#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017477 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017478 {
17479 str = get_tv_string(&argvars[0]);
17480 if (argvars[1].v_type != VAR_UNKNOWN)
17481 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017482 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017483 if (maxcount <= 0)
17484 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017485 if (argvars[2].v_type != VAR_UNKNOWN)
17486 {
17487 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17488 if (typeerr)
17489 return;
17490 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017491 }
17492 else
17493 maxcount = 25;
17494
Bram Moolenaar4770d092006-01-12 23:22:24 +000017495 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017496
17497 for (i = 0; i < ga.ga_len; ++i)
17498 {
17499 str = ((char_u **)ga.ga_data)[i];
17500
17501 li = listitem_alloc();
17502 if (li == NULL)
17503 vim_free(str);
17504 else
17505 {
17506 li->li_tv.v_type = VAR_STRING;
17507 li->li_tv.v_lock = 0;
17508 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017509 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017510 }
17511 }
17512 ga_clear(&ga);
17513 }
17514#endif
17515}
17516
Bram Moolenaar0d660222005-01-07 21:51:51 +000017517 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017518f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017519 typval_T *argvars;
17520 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017521{
17522 char_u *str;
17523 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017524 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017525 regmatch_T regmatch;
17526 char_u patbuf[NUMBUFLEN];
17527 char_u *save_cpo;
17528 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017529 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017530 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017531 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017532
17533 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17534 save_cpo = p_cpo;
17535 p_cpo = (char_u *)"";
17536
17537 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017538 if (argvars[1].v_type != VAR_UNKNOWN)
17539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017540 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17541 if (pat == NULL)
17542 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017543 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017544 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017545 }
17546 if (pat == NULL || *pat == NUL)
17547 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017548
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017549 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017551 if (typeerr)
17552 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553
Bram Moolenaar0d660222005-01-07 21:51:51 +000017554 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17555 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017557 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017558 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017559 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017560 if (*str == NUL)
17561 match = FALSE; /* empty item at the end */
17562 else
17563 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017564 if (match)
17565 end = regmatch.startp[0];
17566 else
17567 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017568 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17569 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017570 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017571 if (list_append_string(rettv->vval.v_list, str,
17572 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017573 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017574 }
17575 if (!match)
17576 break;
17577 /* Advance to just after the match. */
17578 if (regmatch.endp[0] > str)
17579 col = 0;
17580 else
17581 {
17582 /* Don't get stuck at the same match. */
17583#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017584 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017585#else
17586 col = 1;
17587#endif
17588 }
17589 str = regmatch.endp[0];
17590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017591
Bram Moolenaar473de612013-06-08 18:19:48 +020017592 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594
Bram Moolenaar0d660222005-01-07 21:51:51 +000017595 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596}
17597
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017598#ifdef FEAT_FLOAT
17599/*
17600 * "sqrt()" function
17601 */
17602 static void
17603f_sqrt(argvars, rettv)
17604 typval_T *argvars;
17605 typval_T *rettv;
17606{
17607 float_T f;
17608
17609 rettv->v_type = VAR_FLOAT;
17610 if (get_float_arg(argvars, &f) == OK)
17611 rettv->vval.v_float = sqrt(f);
17612 else
17613 rettv->vval.v_float = 0.0;
17614}
17615
17616/*
17617 * "str2float()" function
17618 */
17619 static void
17620f_str2float(argvars, rettv)
17621 typval_T *argvars;
17622 typval_T *rettv;
17623{
17624 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17625
17626 if (*p == '+')
17627 p = skipwhite(p + 1);
17628 (void)string2float(p, &rettv->vval.v_float);
17629 rettv->v_type = VAR_FLOAT;
17630}
17631#endif
17632
Bram Moolenaar2c932302006-03-18 21:42:09 +000017633/*
17634 * "str2nr()" function
17635 */
17636 static void
17637f_str2nr(argvars, rettv)
17638 typval_T *argvars;
17639 typval_T *rettv;
17640{
17641 int base = 10;
17642 char_u *p;
17643 long n;
17644
17645 if (argvars[1].v_type != VAR_UNKNOWN)
17646 {
17647 base = get_tv_number(&argvars[1]);
17648 if (base != 8 && base != 10 && base != 16)
17649 {
17650 EMSG(_(e_invarg));
17651 return;
17652 }
17653 }
17654
17655 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017656 if (*p == '+')
17657 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017658 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17659 rettv->vval.v_number = n;
17660}
17661
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662#ifdef HAVE_STRFTIME
17663/*
17664 * "strftime({format}[, {time}])" function
17665 */
17666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017667f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017668 typval_T *argvars;
17669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670{
17671 char_u result_buf[256];
17672 struct tm *curtime;
17673 time_t seconds;
17674 char_u *p;
17675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017676 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017678 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017679 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680 seconds = time(NULL);
17681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017682 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 curtime = localtime(&seconds);
17684 /* MSVC returns NULL for an invalid value of seconds. */
17685 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017686 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687 else
17688 {
17689# ifdef FEAT_MBYTE
17690 vimconv_T conv;
17691 char_u *enc;
17692
17693 conv.vc_type = CONV_NONE;
17694 enc = enc_locale();
17695 convert_setup(&conv, p_enc, enc);
17696 if (conv.vc_type != CONV_NONE)
17697 p = string_convert(&conv, p, NULL);
17698# endif
17699 if (p != NULL)
17700 (void)strftime((char *)result_buf, sizeof(result_buf),
17701 (char *)p, curtime);
17702 else
17703 result_buf[0] = NUL;
17704
17705# ifdef FEAT_MBYTE
17706 if (conv.vc_type != CONV_NONE)
17707 vim_free(p);
17708 convert_setup(&conv, enc, p_enc);
17709 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017710 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 else
17712# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714
17715# ifdef FEAT_MBYTE
17716 /* Release conversion descriptors */
17717 convert_setup(&conv, NULL, NULL);
17718 vim_free(enc);
17719# endif
17720 }
17721}
17722#endif
17723
17724/*
17725 * "stridx()" function
17726 */
17727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017728f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017729 typval_T *argvars;
17730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731{
17732 char_u buf[NUMBUFLEN];
17733 char_u *needle;
17734 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017735 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017737 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017739 needle = get_tv_string_chk(&argvars[1]);
17740 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017741 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017742 if (needle == NULL || haystack == NULL)
17743 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744
Bram Moolenaar33570922005-01-25 22:26:29 +000017745 if (argvars[2].v_type != VAR_UNKNOWN)
17746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017747 int error = FALSE;
17748
17749 start_idx = get_tv_number_chk(&argvars[2], &error);
17750 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017751 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017752 if (start_idx >= 0)
17753 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017754 }
17755
17756 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17757 if (pos != NULL)
17758 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759}
17760
17761/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017762 * "string()" function
17763 */
17764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017765f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017766 typval_T *argvars;
17767 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017768{
17769 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017770 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017772 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017773 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017774 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017775 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017776 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777}
17778
17779/*
17780 * "strlen()" function
17781 */
17782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017783f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017784 typval_T *argvars;
17785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017787 rettv->vval.v_number = (varnumber_T)(STRLEN(
17788 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789}
17790
17791/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017792 * "strchars()" function
17793 */
17794 static void
17795f_strchars(argvars, rettv)
17796 typval_T *argvars;
17797 typval_T *rettv;
17798{
17799 char_u *s = get_tv_string(&argvars[0]);
17800#ifdef FEAT_MBYTE
17801 varnumber_T len = 0;
17802
17803 while (*s != NUL)
17804 {
17805 mb_cptr2char_adv(&s);
17806 ++len;
17807 }
17808 rettv->vval.v_number = len;
17809#else
17810 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17811#endif
17812}
17813
17814/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017815 * "strdisplaywidth()" function
17816 */
17817 static void
17818f_strdisplaywidth(argvars, rettv)
17819 typval_T *argvars;
17820 typval_T *rettv;
17821{
17822 char_u *s = get_tv_string(&argvars[0]);
17823 int col = 0;
17824
17825 if (argvars[1].v_type != VAR_UNKNOWN)
17826 col = get_tv_number(&argvars[1]);
17827
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017828 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017829}
17830
17831/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017832 * "strwidth()" function
17833 */
17834 static void
17835f_strwidth(argvars, rettv)
17836 typval_T *argvars;
17837 typval_T *rettv;
17838{
17839 char_u *s = get_tv_string(&argvars[0]);
17840
17841 rettv->vval.v_number = (varnumber_T)(
17842#ifdef FEAT_MBYTE
17843 mb_string2cells(s, -1)
17844#else
17845 STRLEN(s)
17846#endif
17847 );
17848}
17849
17850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851 * "strpart()" function
17852 */
17853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017854f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017855 typval_T *argvars;
17856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857{
17858 char_u *p;
17859 int n;
17860 int len;
17861 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017862 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017864 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 slen = (int)STRLEN(p);
17866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017867 n = get_tv_number_chk(&argvars[1], &error);
17868 if (error)
17869 len = 0;
17870 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017871 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 else
17873 len = slen - n; /* default len: all bytes that are available. */
17874
17875 /*
17876 * Only return the overlap between the specified part and the actual
17877 * string.
17878 */
17879 if (n < 0)
17880 {
17881 len += n;
17882 n = 0;
17883 }
17884 else if (n > slen)
17885 n = slen;
17886 if (len < 0)
17887 len = 0;
17888 else if (n + len > slen)
17889 len = slen - n;
17890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017891 rettv->v_type = VAR_STRING;
17892 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893}
17894
17895/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017896 * "strridx()" function
17897 */
17898 static void
17899f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017900 typval_T *argvars;
17901 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017902{
17903 char_u buf[NUMBUFLEN];
17904 char_u *needle;
17905 char_u *haystack;
17906 char_u *rest;
17907 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017908 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017910 needle = get_tv_string_chk(&argvars[1]);
17911 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017912
17913 rettv->vval.v_number = -1;
17914 if (needle == NULL || haystack == NULL)
17915 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017916
17917 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017918 if (argvars[2].v_type != VAR_UNKNOWN)
17919 {
17920 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017921 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017922 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017923 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017924 }
17925 else
17926 end_idx = haystack_len;
17927
Bram Moolenaar0d660222005-01-07 21:51:51 +000017928 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017929 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017930 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017931 lastmatch = haystack + end_idx;
17932 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017933 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017934 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017935 for (rest = haystack; *rest != '\0'; ++rest)
17936 {
17937 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017938 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017939 break;
17940 lastmatch = rest;
17941 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017942 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017943
17944 if (lastmatch == NULL)
17945 rettv->vval.v_number = -1;
17946 else
17947 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17948}
17949
17950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 * "strtrans()" function
17952 */
17953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017954f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017955 typval_T *argvars;
17956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017958 rettv->v_type = VAR_STRING;
17959 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960}
17961
17962/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017963 * "submatch()" function
17964 */
17965 static void
17966f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017967 typval_T *argvars;
17968 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017969{
Bram Moolenaar41571762014-04-02 19:00:58 +020017970 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020017971 int no;
17972 int retList = 0;
17973
17974 no = (int)get_tv_number_chk(&argvars[0], &error);
17975 if (error)
17976 return;
17977 error = FALSE;
17978 if (argvars[1].v_type != VAR_UNKNOWN)
17979 retList = get_tv_number_chk(&argvars[1], &error);
17980 if (error)
17981 return;
17982
17983 if (retList == 0)
17984 {
17985 rettv->v_type = VAR_STRING;
17986 rettv->vval.v_string = reg_submatch(no);
17987 }
17988 else
17989 {
17990 rettv->v_type = VAR_LIST;
17991 rettv->vval.v_list = reg_submatch_list(no);
17992 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017993}
17994
17995/*
17996 * "substitute()" function
17997 */
17998 static void
17999f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018000 typval_T *argvars;
18001 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018002{
18003 char_u patbuf[NUMBUFLEN];
18004 char_u subbuf[NUMBUFLEN];
18005 char_u flagsbuf[NUMBUFLEN];
18006
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018007 char_u *str = get_tv_string_chk(&argvars[0]);
18008 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18009 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18010 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18011
Bram Moolenaar0d660222005-01-07 21:51:51 +000018012 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018013 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18014 rettv->vval.v_string = NULL;
18015 else
18016 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018017}
18018
18019/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018020 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018023f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018024 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026{
18027 int id = 0;
18028#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018029 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030 long col;
18031 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018032 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018034 lnum = get_tv_lnum(argvars); /* -1 on type error */
18035 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18036 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018038 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018039 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018040 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041#endif
18042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018043 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044}
18045
18046/*
18047 * "synIDattr(id, what [, mode])" function
18048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018050f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018051 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053{
18054 char_u *p = NULL;
18055#ifdef FEAT_SYN_HL
18056 int id;
18057 char_u *what;
18058 char_u *mode;
18059 char_u modebuf[NUMBUFLEN];
18060 int modec;
18061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018062 id = get_tv_number(&argvars[0]);
18063 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018064 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018066 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018068 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069 modec = 0; /* replace invalid with current */
18070 }
18071 else
18072 {
18073#ifdef FEAT_GUI
18074 if (gui.in_use)
18075 modec = 'g';
18076 else
18077#endif
18078 if (t_colors > 1)
18079 modec = 'c';
18080 else
18081 modec = 't';
18082 }
18083
18084
18085 switch (TOLOWER_ASC(what[0]))
18086 {
18087 case 'b':
18088 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18089 p = highlight_color(id, what, modec);
18090 else /* bold */
18091 p = highlight_has_attr(id, HL_BOLD, modec);
18092 break;
18093
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018094 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095 p = highlight_color(id, what, modec);
18096 break;
18097
18098 case 'i':
18099 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18100 p = highlight_has_attr(id, HL_INVERSE, modec);
18101 else /* italic */
18102 p = highlight_has_attr(id, HL_ITALIC, modec);
18103 break;
18104
18105 case 'n': /* name */
18106 p = get_highlight_name(NULL, id - 1);
18107 break;
18108
18109 case 'r': /* reverse */
18110 p = highlight_has_attr(id, HL_INVERSE, modec);
18111 break;
18112
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018113 case 's':
18114 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18115 p = highlight_color(id, what, modec);
18116 else /* standout */
18117 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 break;
18119
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018120 case 'u':
18121 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18122 /* underline */
18123 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18124 else
18125 /* undercurl */
18126 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127 break;
18128 }
18129
18130 if (p != NULL)
18131 p = vim_strsave(p);
18132#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018133 rettv->v_type = VAR_STRING;
18134 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135}
18136
18137/*
18138 * "synIDtrans(id)" function
18139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018141f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018142 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144{
18145 int id;
18146
18147#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018148 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149
18150 if (id > 0)
18151 id = syn_get_final_id(id);
18152 else
18153#endif
18154 id = 0;
18155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018156 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157}
18158
18159/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018160 * "synconcealed(lnum, col)" function
18161 */
18162 static void
18163f_synconcealed(argvars, rettv)
18164 typval_T *argvars UNUSED;
18165 typval_T *rettv;
18166{
18167#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18168 long lnum;
18169 long col;
18170 int syntax_flags = 0;
18171 int cchar;
18172 int matchid = 0;
18173 char_u str[NUMBUFLEN];
18174#endif
18175
18176 rettv->v_type = VAR_LIST;
18177 rettv->vval.v_list = NULL;
18178
18179#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18180 lnum = get_tv_lnum(argvars); /* -1 on type error */
18181 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18182
18183 vim_memset(str, NUL, sizeof(str));
18184
18185 if (rettv_list_alloc(rettv) != FAIL)
18186 {
18187 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18188 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18189 && curwin->w_p_cole > 0)
18190 {
18191 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18192 syntax_flags = get_syntax_info(&matchid);
18193
18194 /* get the conceal character */
18195 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18196 {
18197 cchar = syn_get_sub_char();
18198 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18199 cchar = lcs_conceal;
18200 if (cchar != NUL)
18201 {
18202# ifdef FEAT_MBYTE
18203 if (has_mbyte)
18204 (*mb_char2bytes)(cchar, str);
18205 else
18206# endif
18207 str[0] = cchar;
18208 }
18209 }
18210 }
18211
18212 list_append_number(rettv->vval.v_list,
18213 (syntax_flags & HL_CONCEAL) != 0);
18214 /* -1 to auto-determine strlen */
18215 list_append_string(rettv->vval.v_list, str, -1);
18216 list_append_number(rettv->vval.v_list, matchid);
18217 }
18218#endif
18219}
18220
18221/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018222 * "synstack(lnum, col)" function
18223 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018224 static void
18225f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018226 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018227 typval_T *rettv;
18228{
18229#ifdef FEAT_SYN_HL
18230 long lnum;
18231 long col;
18232 int i;
18233 int id;
18234#endif
18235
18236 rettv->v_type = VAR_LIST;
18237 rettv->vval.v_list = NULL;
18238
18239#ifdef FEAT_SYN_HL
18240 lnum = get_tv_lnum(argvars); /* -1 on type error */
18241 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18242
18243 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018244 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018245 && rettv_list_alloc(rettv) != FAIL)
18246 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018247 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018248 for (i = 0; ; ++i)
18249 {
18250 id = syn_get_stack_item(i);
18251 if (id < 0)
18252 break;
18253 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18254 break;
18255 }
18256 }
18257#endif
18258}
18259
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018261get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018262 typval_T *argvars;
18263 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018264 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018266 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018268 char_u *infile = NULL;
18269 char_u buf[NUMBUFLEN];
18270 int err = FALSE;
18271 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018272 list_T *list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018274 rettv->v_type = VAR_STRING;
18275 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018276 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018277 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018278
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018279 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018280 {
18281 /*
18282 * Write the string to a temp file, to be used for input of the shell
18283 * command.
18284 */
18285 if ((infile = vim_tempname('i')) == NULL)
18286 {
18287 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018288 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018289 }
18290
18291 fd = mch_fopen((char *)infile, WRITEBIN);
18292 if (fd == NULL)
18293 {
18294 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018295 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018296 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018297 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018298 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018299 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18300 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018301 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018302 else
18303 {
18304 p = get_tv_string_buf_chk(&argvars[1], buf);
18305 if (p == NULL)
18306 {
18307 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018308 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018309 }
18310 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18311 err = TRUE;
18312 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018313 if (fclose(fd) != 0)
18314 err = TRUE;
18315 if (err)
18316 {
18317 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018318 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018319 }
18320 }
18321
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018322 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018324 int len;
18325 listitem_T *li;
18326 char_u *s = NULL;
18327 char_u *start;
18328 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018329 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018331 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18332 SHELL_SILENT | SHELL_COOKED, &len);
18333 if (res == NULL)
18334 goto errret;
18335
18336 list = list_alloc();
18337 if (list == NULL)
18338 goto errret;
18339
18340 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018342 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018343 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018344 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018345 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018346
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018347 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018348 if (s == NULL)
18349 goto errret;
18350
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018351 for (p = s; start < end; ++p, ++start)
18352 *p = *start == NUL ? NL : *start;
18353 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018354
18355 li = listitem_alloc();
18356 if (li == NULL)
18357 {
18358 vim_free(s);
18359 goto errret;
18360 }
18361 li->li_tv.v_type = VAR_STRING;
18362 li->li_tv.vval.v_string = s;
18363 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018365
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018366 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018367 rettv->v_type = VAR_LIST;
18368 rettv->vval.v_list = list;
18369 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018371 else
18372 {
18373 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18374 SHELL_SILENT | SHELL_COOKED, NULL);
18375#ifdef USE_CR
18376 /* translate <CR> into <NL> */
18377 if (res != NULL)
18378 {
18379 char_u *s;
18380
18381 for (s = res; *s; ++s)
18382 {
18383 if (*s == CAR)
18384 *s = NL;
18385 }
18386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387#else
18388# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018389 /* translate <CR><NL> into <NL> */
18390 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018392 char_u *s, *d;
18393
18394 d = res;
18395 for (s = res; *s; ++s)
18396 {
18397 if (s[0] == CAR && s[1] == NL)
18398 ++s;
18399 *d++ = *s;
18400 }
18401 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403# endif
18404#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018405 rettv->vval.v_string = res;
18406 res = NULL;
18407 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018408
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018409errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018410 if (infile != NULL)
18411 {
18412 mch_remove(infile);
18413 vim_free(infile);
18414 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018415 if (res != NULL)
18416 vim_free(res);
18417 if (list != NULL)
18418 list_free(list, TRUE);
18419}
18420
18421/*
18422 * "system()" function
18423 */
18424 static void
18425f_system(argvars, rettv)
18426 typval_T *argvars;
18427 typval_T *rettv;
18428{
18429 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18430}
18431
18432/*
18433 * "systemlist()" function
18434 */
18435 static void
18436f_systemlist(argvars, rettv)
18437 typval_T *argvars;
18438 typval_T *rettv;
18439{
18440 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441}
18442
18443/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018444 * "tabpagebuflist()" function
18445 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018446 static void
18447f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018448 typval_T *argvars UNUSED;
18449 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018450{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018451#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018452 tabpage_T *tp;
18453 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018454
18455 if (argvars[0].v_type == VAR_UNKNOWN)
18456 wp = firstwin;
18457 else
18458 {
18459 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18460 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018461 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018462 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018463 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018464 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018465 for (; wp != NULL; wp = wp->w_next)
18466 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018467 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018468 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018469 }
18470#endif
18471}
18472
18473
18474/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018475 * "tabpagenr()" function
18476 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018477 static void
18478f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018479 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018480 typval_T *rettv;
18481{
18482 int nr = 1;
18483#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018484 char_u *arg;
18485
18486 if (argvars[0].v_type != VAR_UNKNOWN)
18487 {
18488 arg = get_tv_string_chk(&argvars[0]);
18489 nr = 0;
18490 if (arg != NULL)
18491 {
18492 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018493 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018494 else
18495 EMSG2(_(e_invexpr2), arg);
18496 }
18497 }
18498 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018499 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018500#endif
18501 rettv->vval.v_number = nr;
18502}
18503
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018504
18505#ifdef FEAT_WINDOWS
18506static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18507
18508/*
18509 * Common code for tabpagewinnr() and winnr().
18510 */
18511 static int
18512get_winnr(tp, argvar)
18513 tabpage_T *tp;
18514 typval_T *argvar;
18515{
18516 win_T *twin;
18517 int nr = 1;
18518 win_T *wp;
18519 char_u *arg;
18520
18521 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18522 if (argvar->v_type != VAR_UNKNOWN)
18523 {
18524 arg = get_tv_string_chk(argvar);
18525 if (arg == NULL)
18526 nr = 0; /* type error; errmsg already given */
18527 else if (STRCMP(arg, "$") == 0)
18528 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18529 else if (STRCMP(arg, "#") == 0)
18530 {
18531 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18532 if (twin == NULL)
18533 nr = 0;
18534 }
18535 else
18536 {
18537 EMSG2(_(e_invexpr2), arg);
18538 nr = 0;
18539 }
18540 }
18541
18542 if (nr > 0)
18543 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18544 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018545 {
18546 if (wp == NULL)
18547 {
18548 /* didn't find it in this tabpage */
18549 nr = 0;
18550 break;
18551 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018552 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018553 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018554 return nr;
18555}
18556#endif
18557
18558/*
18559 * "tabpagewinnr()" function
18560 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018561 static void
18562f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018563 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018564 typval_T *rettv;
18565{
18566 int nr = 1;
18567#ifdef FEAT_WINDOWS
18568 tabpage_T *tp;
18569
18570 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18571 if (tp == NULL)
18572 nr = 0;
18573 else
18574 nr = get_winnr(tp, &argvars[1]);
18575#endif
18576 rettv->vval.v_number = nr;
18577}
18578
18579
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018580/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018581 * "tagfiles()" function
18582 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018583 static void
18584f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018585 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018586 typval_T *rettv;
18587{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018588 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018589 tagname_T tn;
18590 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018591
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018592 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018593 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018594 fname = alloc(MAXPATHL);
18595 if (fname == NULL)
18596 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018597
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018598 for (first = TRUE; ; first = FALSE)
18599 if (get_tagfname(&tn, first, fname) == FAIL
18600 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018601 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018602 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018603 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018604}
18605
18606/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018607 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018608 */
18609 static void
18610f_taglist(argvars, rettv)
18611 typval_T *argvars;
18612 typval_T *rettv;
18613{
18614 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018615
18616 tag_pattern = get_tv_string(&argvars[0]);
18617
18618 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018619 if (*tag_pattern == NUL)
18620 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018621
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018622 if (rettv_list_alloc(rettv) == OK)
18623 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018624}
18625
18626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 * "tempname()" function
18628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018630f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018631 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633{
18634 static int x = 'A';
18635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018636 rettv->v_type = VAR_STRING;
18637 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638
18639 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18640 * names. Skip 'I' and 'O', they are used for shell redirection. */
18641 do
18642 {
18643 if (x == 'Z')
18644 x = '0';
18645 else if (x == '9')
18646 x = 'A';
18647 else
18648 {
18649#ifdef EBCDIC
18650 if (x == 'I')
18651 x = 'J';
18652 else if (x == 'R')
18653 x = 'S';
18654 else
18655#endif
18656 ++x;
18657 }
18658 } while (x == 'I' || x == 'O');
18659}
18660
18661/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018662 * "test(list)" function: Just checking the walls...
18663 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018664 static void
18665f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018666 typval_T *argvars UNUSED;
18667 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018668{
18669 /* Used for unit testing. Change the code below to your liking. */
18670#if 0
18671 listitem_T *li;
18672 list_T *l;
18673 char_u *bad, *good;
18674
18675 if (argvars[0].v_type != VAR_LIST)
18676 return;
18677 l = argvars[0].vval.v_list;
18678 if (l == NULL)
18679 return;
18680 li = l->lv_first;
18681 if (li == NULL)
18682 return;
18683 bad = get_tv_string(&li->li_tv);
18684 li = li->li_next;
18685 if (li == NULL)
18686 return;
18687 good = get_tv_string(&li->li_tv);
18688 rettv->vval.v_number = test_edit_score(bad, good);
18689#endif
18690}
18691
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018692#ifdef FEAT_FLOAT
18693/*
18694 * "tan()" function
18695 */
18696 static void
18697f_tan(argvars, rettv)
18698 typval_T *argvars;
18699 typval_T *rettv;
18700{
18701 float_T f;
18702
18703 rettv->v_type = VAR_FLOAT;
18704 if (get_float_arg(argvars, &f) == OK)
18705 rettv->vval.v_float = tan(f);
18706 else
18707 rettv->vval.v_float = 0.0;
18708}
18709
18710/*
18711 * "tanh()" function
18712 */
18713 static void
18714f_tanh(argvars, rettv)
18715 typval_T *argvars;
18716 typval_T *rettv;
18717{
18718 float_T f;
18719
18720 rettv->v_type = VAR_FLOAT;
18721 if (get_float_arg(argvars, &f) == OK)
18722 rettv->vval.v_float = tanh(f);
18723 else
18724 rettv->vval.v_float = 0.0;
18725}
18726#endif
18727
Bram Moolenaard52d9742005-08-21 22:20:28 +000018728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729 * "tolower(string)" function
18730 */
18731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018732f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018733 typval_T *argvars;
18734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735{
18736 char_u *p;
18737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018738 p = vim_strsave(get_tv_string(&argvars[0]));
18739 rettv->v_type = VAR_STRING;
18740 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741
18742 if (p != NULL)
18743 while (*p != NUL)
18744 {
18745#ifdef FEAT_MBYTE
18746 int l;
18747
18748 if (enc_utf8)
18749 {
18750 int c, lc;
18751
18752 c = utf_ptr2char(p);
18753 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018754 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755 /* TODO: reallocate string when byte count changes. */
18756 if (utf_char2len(lc) == l)
18757 utf_char2bytes(lc, p);
18758 p += l;
18759 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018760 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 p += l; /* skip multi-byte character */
18762 else
18763#endif
18764 {
18765 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18766 ++p;
18767 }
18768 }
18769}
18770
18771/*
18772 * "toupper(string)" function
18773 */
18774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018775f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018776 typval_T *argvars;
18777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018779 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018780 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781}
18782
18783/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018784 * "tr(string, fromstr, tostr)" function
18785 */
18786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018787f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018788 typval_T *argvars;
18789 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018790{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018791 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018792 char_u *fromstr;
18793 char_u *tostr;
18794 char_u *p;
18795#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018796 int inlen;
18797 int fromlen;
18798 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018799 int idx;
18800 char_u *cpstr;
18801 int cplen;
18802 int first = TRUE;
18803#endif
18804 char_u buf[NUMBUFLEN];
18805 char_u buf2[NUMBUFLEN];
18806 garray_T ga;
18807
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018808 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018809 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18810 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018811
18812 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018813 rettv->v_type = VAR_STRING;
18814 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018815 if (fromstr == NULL || tostr == NULL)
18816 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018817 ga_init2(&ga, (int)sizeof(char), 80);
18818
18819#ifdef FEAT_MBYTE
18820 if (!has_mbyte)
18821#endif
18822 /* not multi-byte: fromstr and tostr must be the same length */
18823 if (STRLEN(fromstr) != STRLEN(tostr))
18824 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018825#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018826error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018827#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018828 EMSG2(_(e_invarg2), fromstr);
18829 ga_clear(&ga);
18830 return;
18831 }
18832
18833 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018834 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018835 {
18836#ifdef FEAT_MBYTE
18837 if (has_mbyte)
18838 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018839 inlen = (*mb_ptr2len)(in_str);
18840 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018841 cplen = inlen;
18842 idx = 0;
18843 for (p = fromstr; *p != NUL; p += fromlen)
18844 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018845 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018846 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018847 {
18848 for (p = tostr; *p != NUL; p += tolen)
18849 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018850 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018851 if (idx-- == 0)
18852 {
18853 cplen = tolen;
18854 cpstr = p;
18855 break;
18856 }
18857 }
18858 if (*p == NUL) /* tostr is shorter than fromstr */
18859 goto error;
18860 break;
18861 }
18862 ++idx;
18863 }
18864
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018865 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018866 {
18867 /* Check that fromstr and tostr have the same number of
18868 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018869 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018870 first = FALSE;
18871 for (p = tostr; *p != NUL; p += tolen)
18872 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018873 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018874 --idx;
18875 }
18876 if (idx != 0)
18877 goto error;
18878 }
18879
18880 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018881 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018882 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018883
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018884 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018885 }
18886 else
18887#endif
18888 {
18889 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018890 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018891 if (p != NULL)
18892 ga_append(&ga, tostr[p - fromstr]);
18893 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018894 ga_append(&ga, *in_str);
18895 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018896 }
18897 }
18898
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018899 /* add a terminating NUL */
18900 ga_grow(&ga, 1);
18901 ga_append(&ga, NUL);
18902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018903 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018904}
18905
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018906#ifdef FEAT_FLOAT
18907/*
18908 * "trunc({float})" function
18909 */
18910 static void
18911f_trunc(argvars, rettv)
18912 typval_T *argvars;
18913 typval_T *rettv;
18914{
18915 float_T f;
18916
18917 rettv->v_type = VAR_FLOAT;
18918 if (get_float_arg(argvars, &f) == OK)
18919 /* trunc() is not in C90, use floor() or ceil() instead. */
18920 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18921 else
18922 rettv->vval.v_float = 0.0;
18923}
18924#endif
18925
Bram Moolenaar8299df92004-07-10 09:47:34 +000018926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927 * "type(expr)" function
18928 */
18929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018930f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018931 typval_T *argvars;
18932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018934 int n;
18935
18936 switch (argvars[0].v_type)
18937 {
18938 case VAR_NUMBER: n = 0; break;
18939 case VAR_STRING: n = 1; break;
18940 case VAR_FUNC: n = 2; break;
18941 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018942 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018943#ifdef FEAT_FLOAT
18944 case VAR_FLOAT: n = 5; break;
18945#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018946 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18947 }
18948 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949}
18950
18951/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018952 * "undofile(name)" function
18953 */
18954 static void
18955f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018956 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018957 typval_T *rettv;
18958{
18959 rettv->v_type = VAR_STRING;
18960#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018961 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018962 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018963
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018964 if (*fname == NUL)
18965 {
18966 /* If there is no file name there will be no undo file. */
18967 rettv->vval.v_string = NULL;
18968 }
18969 else
18970 {
18971 char_u *ffname = FullName_save(fname, FALSE);
18972
18973 if (ffname != NULL)
18974 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18975 vim_free(ffname);
18976 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018977 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018978#else
18979 rettv->vval.v_string = NULL;
18980#endif
18981}
18982
18983/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018984 * "undotree()" function
18985 */
18986 static void
18987f_undotree(argvars, rettv)
18988 typval_T *argvars UNUSED;
18989 typval_T *rettv;
18990{
18991 if (rettv_dict_alloc(rettv) == OK)
18992 {
18993 dict_T *dict = rettv->vval.v_dict;
18994 list_T *list;
18995
Bram Moolenaar730cde92010-06-27 05:18:54 +020018996 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018997 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018998 dict_add_nr_str(dict, "save_last",
18999 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019000 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19001 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019002 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019003
19004 list = list_alloc();
19005 if (list != NULL)
19006 {
19007 u_eval_tree(curbuf->b_u_oldhead, list);
19008 dict_add_list(dict, "entries", list);
19009 }
19010 }
19011}
19012
19013/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019014 * "values(dict)" function
19015 */
19016 static void
19017f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019018 typval_T *argvars;
19019 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019020{
19021 dict_list(argvars, rettv, 1);
19022}
19023
19024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025 * "virtcol(string)" function
19026 */
19027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019028f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019029 typval_T *argvars;
19030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031{
19032 colnr_T vcol = 0;
19033 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019034 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019036 fp = var2fpos(&argvars[0], FALSE, &fnum);
19037 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19038 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039 {
19040 getvvcol(curwin, fp, NULL, NULL, &vcol);
19041 ++vcol;
19042 }
19043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019044 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045}
19046
19047/*
19048 * "visualmode()" function
19049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019051f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019052 typval_T *argvars UNUSED;
19053 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 char_u str[2];
19056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019057 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 str[0] = curbuf->b_visual_mode_eval;
19059 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019060 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061
19062 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019063 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065}
19066
19067/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019068 * "wildmenumode()" function
19069 */
19070 static void
19071f_wildmenumode(argvars, rettv)
19072 typval_T *argvars UNUSED;
19073 typval_T *rettv UNUSED;
19074{
19075#ifdef FEAT_WILDMENU
19076 if (wild_menu_showing)
19077 rettv->vval.v_number = 1;
19078#endif
19079}
19080
19081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 * "winbufnr(nr)" function
19083 */
19084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019085f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019086 typval_T *argvars;
19087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088{
19089 win_T *wp;
19090
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019091 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019093 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019095 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096}
19097
19098/*
19099 * "wincol()" function
19100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019102f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019103 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105{
19106 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019107 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108}
19109
19110/*
19111 * "winheight(nr)" function
19112 */
19113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019114f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019115 typval_T *argvars;
19116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117{
19118 win_T *wp;
19119
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019120 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019122 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019124 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125}
19126
19127/*
19128 * "winline()" function
19129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019131f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019132 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134{
19135 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019136 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137}
19138
19139/*
19140 * "winnr()" function
19141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019143f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019144 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019146{
19147 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019148
Bram Moolenaar071d4272004-06-13 20:20:40 +000019149#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019150 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019152 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153}
19154
19155/*
19156 * "winrestcmd()" function
19157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019159f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019160 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162{
19163#ifdef FEAT_WINDOWS
19164 win_T *wp;
19165 int winnr = 1;
19166 garray_T ga;
19167 char_u buf[50];
19168
19169 ga_init2(&ga, (int)sizeof(char), 70);
19170 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19171 {
19172 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19173 ga_concat(&ga, buf);
19174# ifdef FEAT_VERTSPLIT
19175 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19176 ga_concat(&ga, buf);
19177# endif
19178 ++winnr;
19179 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019180 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019182 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019184 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019186 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187}
19188
19189/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019190 * "winrestview()" function
19191 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019192 static void
19193f_winrestview(argvars, rettv)
19194 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019195 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019196{
19197 dict_T *dict;
19198
19199 if (argvars[0].v_type != VAR_DICT
19200 || (dict = argvars[0].vval.v_dict) == NULL)
19201 EMSG(_(e_invarg));
19202 else
19203 {
19204 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19205 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
19206#ifdef FEAT_VIRTUALEDIT
19207 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
19208#endif
19209 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019210 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019211
Bram Moolenaar6f11a412006-09-06 20:16:42 +000019212 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019213#ifdef FEAT_DIFF
19214 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
19215#endif
19216 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19217 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
19218
19219 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019220 win_new_height(curwin, curwin->w_height);
19221# ifdef FEAT_VERTSPLIT
19222 win_new_width(curwin, W_WIDTH(curwin));
19223# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019224 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019225
19226 if (curwin->w_topline == 0)
19227 curwin->w_topline = 1;
19228 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19229 curwin->w_topline = curbuf->b_ml.ml_line_count;
19230#ifdef FEAT_DIFF
19231 check_topfill(curwin, TRUE);
19232#endif
19233 }
19234}
19235
19236/*
19237 * "winsaveview()" function
19238 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019239 static void
19240f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019241 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019242 typval_T *rettv;
19243{
19244 dict_T *dict;
19245
Bram Moolenaara800b422010-06-27 01:15:55 +020019246 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019247 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019248 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019249
19250 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19251 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19252#ifdef FEAT_VIRTUALEDIT
19253 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19254#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019255 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019256 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19257
19258 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19259#ifdef FEAT_DIFF
19260 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19261#endif
19262 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19263 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19264}
19265
19266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019267 * "winwidth(nr)" function
19268 */
19269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019270f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019271 typval_T *argvars;
19272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273{
19274 win_T *wp;
19275
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019276 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019278 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279 else
19280#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019281 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019283 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019284#endif
19285}
19286
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019288 * Write list of strings to file
19289 */
19290 static int
19291write_list(fd, list, binary)
19292 FILE *fd;
19293 list_T *list;
19294 int binary;
19295{
19296 listitem_T *li;
19297 int c;
19298 int ret = OK;
19299 char_u *s;
19300
19301 for (li = list->lv_first; li != NULL; li = li->li_next)
19302 {
19303 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19304 {
19305 if (*s == '\n')
19306 c = putc(NUL, fd);
19307 else
19308 c = putc(*s, fd);
19309 if (c == EOF)
19310 {
19311 ret = FAIL;
19312 break;
19313 }
19314 }
19315 if (!binary || li->li_next != NULL)
19316 if (putc('\n', fd) == EOF)
19317 {
19318 ret = FAIL;
19319 break;
19320 }
19321 if (ret == FAIL)
19322 {
19323 EMSG(_(e_write));
19324 break;
19325 }
19326 }
19327 return ret;
19328}
19329
19330/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019331 * "writefile()" function
19332 */
19333 static void
19334f_writefile(argvars, rettv)
19335 typval_T *argvars;
19336 typval_T *rettv;
19337{
19338 int binary = FALSE;
19339 char_u *fname;
19340 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019341 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019342
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019343 if (check_restricted() || check_secure())
19344 return;
19345
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019346 if (argvars[0].v_type != VAR_LIST)
19347 {
19348 EMSG2(_(e_listarg), "writefile()");
19349 return;
19350 }
19351 if (argvars[0].vval.v_list == NULL)
19352 return;
19353
19354 if (argvars[2].v_type != VAR_UNKNOWN
19355 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19356 binary = TRUE;
19357
19358 /* Always open the file in binary mode, library functions have a mind of
19359 * their own about CR-LF conversion. */
19360 fname = get_tv_string(&argvars[1]);
19361 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19362 {
19363 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19364 ret = -1;
19365 }
19366 else
19367 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019368 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19369 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019370 fclose(fd);
19371 }
19372
19373 rettv->vval.v_number = ret;
19374}
19375
19376/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019377 * "xor(expr, expr)" function
19378 */
19379 static void
19380f_xor(argvars, rettv)
19381 typval_T *argvars;
19382 typval_T *rettv;
19383{
19384 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19385 ^ get_tv_number_chk(&argvars[1], NULL);
19386}
19387
19388
19389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019391 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392 */
19393 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019394var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019395 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019396 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019397 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019399 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019401 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402
Bram Moolenaara5525202006-03-02 22:52:09 +000019403 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019404 if (varp->v_type == VAR_LIST)
19405 {
19406 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019407 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019408 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019409 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019410
19411 l = varp->vval.v_list;
19412 if (l == NULL)
19413 return NULL;
19414
19415 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019416 pos.lnum = list_find_nr(l, 0L, &error);
19417 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019418 return NULL; /* invalid line number */
19419
19420 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019421 pos.col = list_find_nr(l, 1L, &error);
19422 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019423 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019424 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019425
19426 /* We accept "$" for the column number: last column. */
19427 li = list_find(l, 1L);
19428 if (li != NULL && li->li_tv.v_type == VAR_STRING
19429 && li->li_tv.vval.v_string != NULL
19430 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19431 pos.col = len + 1;
19432
Bram Moolenaara5525202006-03-02 22:52:09 +000019433 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019434 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019435 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019436 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019437
Bram Moolenaara5525202006-03-02 22:52:09 +000019438#ifdef FEAT_VIRTUALEDIT
19439 /* Get the virtual offset. Defaults to zero. */
19440 pos.coladd = list_find_nr(l, 2L, &error);
19441 if (error)
19442 pos.coladd = 0;
19443#endif
19444
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019445 return &pos;
19446 }
19447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019448 name = get_tv_string_chk(varp);
19449 if (name == NULL)
19450 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019451 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019453 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19454 {
19455 if (VIsual_active)
19456 return &VIsual;
19457 return &curwin->w_cursor;
19458 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019459 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019461 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19463 return NULL;
19464 return pp;
19465 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019466
19467#ifdef FEAT_VIRTUALEDIT
19468 pos.coladd = 0;
19469#endif
19470
Bram Moolenaar477933c2007-07-17 14:32:23 +000019471 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019472 {
19473 pos.col = 0;
19474 if (name[1] == '0') /* "w0": first visible line */
19475 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019476 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019477 pos.lnum = curwin->w_topline;
19478 return &pos;
19479 }
19480 else if (name[1] == '$') /* "w$": last visible line */
19481 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019482 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019483 pos.lnum = curwin->w_botline - 1;
19484 return &pos;
19485 }
19486 }
19487 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019489 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 {
19491 pos.lnum = curbuf->b_ml.ml_line_count;
19492 pos.col = 0;
19493 }
19494 else
19495 {
19496 pos.lnum = curwin->w_cursor.lnum;
19497 pos.col = (colnr_T)STRLEN(ml_get_curline());
19498 }
19499 return &pos;
19500 }
19501 return NULL;
19502}
19503
19504/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019505 * Convert list in "arg" into a position and optional file number.
19506 * When "fnump" is NULL there is no file number, only 3 items.
19507 * Note that the column is passed on as-is, the caller may want to decrement
19508 * it to use 1 for the first column.
19509 * Return FAIL when conversion is not possible, doesn't check the position for
19510 * validity.
19511 */
19512 static int
19513list2fpos(arg, posp, fnump)
19514 typval_T *arg;
19515 pos_T *posp;
19516 int *fnump;
19517{
19518 list_T *l = arg->vval.v_list;
19519 long i = 0;
19520 long n;
19521
Bram Moolenaarbde35262006-07-23 20:12:24 +000019522 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19523 * when "fnump" isn't NULL and "coladd" is optional. */
19524 if (arg->v_type != VAR_LIST
19525 || l == NULL
19526 || l->lv_len < (fnump == NULL ? 2 : 3)
19527 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019528 return FAIL;
19529
19530 if (fnump != NULL)
19531 {
19532 n = list_find_nr(l, i++, NULL); /* fnum */
19533 if (n < 0)
19534 return FAIL;
19535 if (n == 0)
19536 n = curbuf->b_fnum; /* current buffer */
19537 *fnump = n;
19538 }
19539
19540 n = list_find_nr(l, i++, NULL); /* lnum */
19541 if (n < 0)
19542 return FAIL;
19543 posp->lnum = n;
19544
19545 n = list_find_nr(l, i++, NULL); /* col */
19546 if (n < 0)
19547 return FAIL;
19548 posp->col = n;
19549
19550#ifdef FEAT_VIRTUALEDIT
19551 n = list_find_nr(l, i, NULL);
19552 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019553 posp->coladd = 0;
19554 else
19555 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019556#endif
19557
19558 return OK;
19559}
19560
19561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562 * Get the length of an environment variable name.
19563 * Advance "arg" to the first character after the name.
19564 * Return 0 for error.
19565 */
19566 static int
19567get_env_len(arg)
19568 char_u **arg;
19569{
19570 char_u *p;
19571 int len;
19572
19573 for (p = *arg; vim_isIDc(*p); ++p)
19574 ;
19575 if (p == *arg) /* no name found */
19576 return 0;
19577
19578 len = (int)(p - *arg);
19579 *arg = p;
19580 return len;
19581}
19582
19583/*
19584 * Get the length of the name of a function or internal variable.
19585 * "arg" is advanced to the first non-white character after the name.
19586 * Return 0 if something is wrong.
19587 */
19588 static int
19589get_id_len(arg)
19590 char_u **arg;
19591{
19592 char_u *p;
19593 int len;
19594
19595 /* Find the end of the name. */
19596 for (p = *arg; eval_isnamec(*p); ++p)
19597 ;
19598 if (p == *arg) /* no name found */
19599 return 0;
19600
19601 len = (int)(p - *arg);
19602 *arg = skipwhite(p);
19603
19604 return len;
19605}
19606
19607/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019608 * Get the length of the name of a variable or function.
19609 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019611 * Return -1 if curly braces expansion failed.
19612 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613 * If the name contains 'magic' {}'s, expand them and return the
19614 * expanded name in an allocated string via 'alias' - caller must free.
19615 */
19616 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019617get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618 char_u **arg;
19619 char_u **alias;
19620 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019621 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622{
19623 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 char_u *p;
19625 char_u *expr_start;
19626 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627
19628 *alias = NULL; /* default to no alias */
19629
19630 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19631 && (*arg)[2] == (int)KE_SNR)
19632 {
19633 /* hard coded <SNR>, already translated */
19634 *arg += 3;
19635 return get_id_len(arg) + 3;
19636 }
19637 len = eval_fname_script(*arg);
19638 if (len > 0)
19639 {
19640 /* literal "<SID>", "s:" or "<SNR>" */
19641 *arg += len;
19642 }
19643
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019645 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019647 p = find_name_end(*arg, &expr_start, &expr_end,
19648 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 if (expr_start != NULL)
19650 {
19651 char_u *temp_string;
19652
19653 if (!evaluate)
19654 {
19655 len += (int)(p - *arg);
19656 *arg = skipwhite(p);
19657 return len;
19658 }
19659
19660 /*
19661 * Include any <SID> etc in the expanded string:
19662 * Thus the -len here.
19663 */
19664 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19665 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019666 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667 *alias = temp_string;
19668 *arg = skipwhite(p);
19669 return (int)STRLEN(temp_string);
19670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671
19672 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019673 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674 EMSG2(_(e_invexpr2), *arg);
19675
19676 return len;
19677}
19678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019679/*
19680 * Find the end of a variable or function name, taking care of magic braces.
19681 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19682 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019683 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019684 * Return a pointer to just after the name. Equal to "arg" if there is no
19685 * valid name.
19686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019688find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689 char_u *arg;
19690 char_u **expr_start;
19691 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019692 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019694 int mb_nest = 0;
19695 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696 char_u *p;
19697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019698 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019700 *expr_start = NULL;
19701 *expr_end = NULL;
19702 }
19703
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019704 /* Quick check for valid starting character. */
19705 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19706 return arg;
19707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019708 for (p = arg; *p != NUL
19709 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019710 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019711 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019712 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019713 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019714 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019715 if (*p == '\'')
19716 {
19717 /* skip over 'string' to avoid counting [ and ] inside it. */
19718 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19719 ;
19720 if (*p == NUL)
19721 break;
19722 }
19723 else if (*p == '"')
19724 {
19725 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19726 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19727 if (*p == '\\' && p[1] != NUL)
19728 ++p;
19729 if (*p == NUL)
19730 break;
19731 }
19732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019733 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019735 if (*p == '[')
19736 ++br_nest;
19737 else if (*p == ']')
19738 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019739 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019741 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019743 if (*p == '{')
19744 {
19745 mb_nest++;
19746 if (expr_start != NULL && *expr_start == NULL)
19747 *expr_start = p;
19748 }
19749 else if (*p == '}')
19750 {
19751 mb_nest--;
19752 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19753 *expr_end = p;
19754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756 }
19757
19758 return p;
19759}
19760
19761/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019762 * Expands out the 'magic' {}'s in a variable/function name.
19763 * Note that this can call itself recursively, to deal with
19764 * constructs like foo{bar}{baz}{bam}
19765 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19766 * "in_start" ^
19767 * "expr_start" ^
19768 * "expr_end" ^
19769 * "in_end" ^
19770 *
19771 * Returns a new allocated string, which the caller must free.
19772 * Returns NULL for failure.
19773 */
19774 static char_u *
19775make_expanded_name(in_start, expr_start, expr_end, in_end)
19776 char_u *in_start;
19777 char_u *expr_start;
19778 char_u *expr_end;
19779 char_u *in_end;
19780{
19781 char_u c1;
19782 char_u *retval = NULL;
19783 char_u *temp_result;
19784 char_u *nextcmd = NULL;
19785
19786 if (expr_end == NULL || in_end == NULL)
19787 return NULL;
19788 *expr_start = NUL;
19789 *expr_end = NUL;
19790 c1 = *in_end;
19791 *in_end = NUL;
19792
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019793 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019794 if (temp_result != NULL && nextcmd == NULL)
19795 {
19796 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19797 + (in_end - expr_end) + 1));
19798 if (retval != NULL)
19799 {
19800 STRCPY(retval, in_start);
19801 STRCAT(retval, temp_result);
19802 STRCAT(retval, expr_end + 1);
19803 }
19804 }
19805 vim_free(temp_result);
19806
19807 *in_end = c1; /* put char back for error messages */
19808 *expr_start = '{';
19809 *expr_end = '}';
19810
19811 if (retval != NULL)
19812 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019813 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019814 if (expr_start != NULL)
19815 {
19816 /* Further expansion! */
19817 temp_result = make_expanded_name(retval, expr_start,
19818 expr_end, temp_result);
19819 vim_free(retval);
19820 retval = temp_result;
19821 }
19822 }
19823
19824 return retval;
19825}
19826
19827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019829 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830 */
19831 static int
19832eval_isnamec(c)
19833 int c;
19834{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019835 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19836}
19837
19838/*
19839 * Return TRUE if character "c" can be used as the first character in a
19840 * variable or function name (excluding '{' and '}').
19841 */
19842 static int
19843eval_isnamec1(c)
19844 int c;
19845{
19846 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847}
19848
19849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 * Set number v: variable to "val".
19851 */
19852 void
19853set_vim_var_nr(idx, val)
19854 int idx;
19855 long val;
19856{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019857 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858}
19859
19860/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019861 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 */
19863 long
19864get_vim_var_nr(idx)
19865 int idx;
19866{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019867 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868}
19869
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019870/*
19871 * Get string v: variable value. Uses a static buffer, can only be used once.
19872 */
19873 char_u *
19874get_vim_var_str(idx)
19875 int idx;
19876{
19877 return get_tv_string(&vimvars[idx].vv_tv);
19878}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019879
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019881 * Get List v: variable value. Caller must take care of reference count when
19882 * needed.
19883 */
19884 list_T *
19885get_vim_var_list(idx)
19886 int idx;
19887{
19888 return vimvars[idx].vv_list;
19889}
19890
19891/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019892 * Set v:char to character "c".
19893 */
19894 void
19895set_vim_var_char(c)
19896 int c;
19897{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019898 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019899
19900#ifdef FEAT_MBYTE
19901 if (has_mbyte)
19902 buf[(*mb_char2bytes)(c, buf)] = NUL;
19903 else
19904#endif
19905 {
19906 buf[0] = c;
19907 buf[1] = NUL;
19908 }
19909 set_vim_var_string(VV_CHAR, buf, -1);
19910}
19911
19912/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019913 * Set v:count to "count" and v:count1 to "count1".
19914 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915 */
19916 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019917set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 long count;
19919 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019920 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019922 if (set_prevcount)
19923 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019924 vimvars[VV_COUNT].vv_nr = count;
19925 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926}
19927
19928/*
19929 * Set string v: variable to a copy of "val".
19930 */
19931 void
19932set_vim_var_string(idx, val, len)
19933 int idx;
19934 char_u *val;
19935 int len; /* length of "val" to use or -1 (whole string) */
19936{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019937 /* Need to do this (at least) once, since we can't initialize a union.
19938 * Will always be invoked when "v:progname" is set. */
19939 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19940
Bram Moolenaare9a41262005-01-15 22:18:47 +000019941 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019943 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019945 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019947 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948}
19949
19950/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019951 * Set List v: variable to "val".
19952 */
19953 void
19954set_vim_var_list(idx, val)
19955 int idx;
19956 list_T *val;
19957{
19958 list_unref(vimvars[idx].vv_list);
19959 vimvars[idx].vv_list = val;
19960 if (val != NULL)
19961 ++val->lv_refcount;
19962}
19963
19964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965 * Set v:register if needed.
19966 */
19967 void
19968set_reg_var(c)
19969 int c;
19970{
19971 char_u regname;
19972
19973 if (c == 0 || c == ' ')
19974 regname = '"';
19975 else
19976 regname = c;
19977 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019978 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 set_vim_var_string(VV_REG, &regname, 1);
19980}
19981
19982/*
19983 * Get or set v:exception. If "oldval" == NULL, return the current value.
19984 * Otherwise, restore the value to "oldval" and return NULL.
19985 * Must always be called in pairs to save and restore v:exception! Does not
19986 * take care of memory allocations.
19987 */
19988 char_u *
19989v_exception(oldval)
19990 char_u *oldval;
19991{
19992 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019993 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994
Bram Moolenaare9a41262005-01-15 22:18:47 +000019995 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996 return NULL;
19997}
19998
19999/*
20000 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20001 * Otherwise, restore the value to "oldval" and return NULL.
20002 * Must always be called in pairs to save and restore v:throwpoint! Does not
20003 * take care of memory allocations.
20004 */
20005 char_u *
20006v_throwpoint(oldval)
20007 char_u *oldval;
20008{
20009 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020010 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011
Bram Moolenaare9a41262005-01-15 22:18:47 +000020012 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 return NULL;
20014}
20015
20016#if defined(FEAT_AUTOCMD) || defined(PROTO)
20017/*
20018 * Set v:cmdarg.
20019 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20020 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20021 * Must always be called in pairs!
20022 */
20023 char_u *
20024set_cmdarg(eap, oldarg)
20025 exarg_T *eap;
20026 char_u *oldarg;
20027{
20028 char_u *oldval;
20029 char_u *newval;
20030 unsigned len;
20031
Bram Moolenaare9a41262005-01-15 22:18:47 +000020032 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020033 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020035 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020036 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020037 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038 }
20039
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020040 if (eap->force_bin == FORCE_BIN)
20041 len = 6;
20042 else if (eap->force_bin == FORCE_NOBIN)
20043 len = 8;
20044 else
20045 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020046
20047 if (eap->read_edit)
20048 len += 7;
20049
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020050 if (eap->force_ff != 0)
20051 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20052# ifdef FEAT_MBYTE
20053 if (eap->force_enc != 0)
20054 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020055 if (eap->bad_char != 0)
20056 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020057# endif
20058
20059 newval = alloc(len + 1);
20060 if (newval == NULL)
20061 return NULL;
20062
20063 if (eap->force_bin == FORCE_BIN)
20064 sprintf((char *)newval, " ++bin");
20065 else if (eap->force_bin == FORCE_NOBIN)
20066 sprintf((char *)newval, " ++nobin");
20067 else
20068 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020069
20070 if (eap->read_edit)
20071 STRCAT(newval, " ++edit");
20072
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020073 if (eap->force_ff != 0)
20074 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20075 eap->cmd + eap->force_ff);
20076# ifdef FEAT_MBYTE
20077 if (eap->force_enc != 0)
20078 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20079 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020080 if (eap->bad_char == BAD_KEEP)
20081 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20082 else if (eap->bad_char == BAD_DROP)
20083 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20084 else if (eap->bad_char != 0)
20085 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020086# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020087 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020088 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089}
20090#endif
20091
20092/*
20093 * Get the value of internal variable "name".
20094 * Return OK or FAIL.
20095 */
20096 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020097get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 char_u *name;
20099 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020100 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020101 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020102 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103{
20104 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020105 typval_T *tv = NULL;
20106 typval_T atv;
20107 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109
20110 /* truncate the name, so that we can use strcmp() */
20111 cc = name[len];
20112 name[len] = NUL;
20113
20114 /*
20115 * Check for "b:changedtick".
20116 */
20117 if (STRCMP(name, "b:changedtick") == 0)
20118 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020119 atv.v_type = VAR_NUMBER;
20120 atv.vval.v_number = curbuf->b_changedtick;
20121 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 }
20123
20124 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 * Check for user-defined variables.
20126 */
20127 else
20128 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020129 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020131 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 }
20133
Bram Moolenaare9a41262005-01-15 22:18:47 +000020134 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020136 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020137 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 ret = FAIL;
20139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020140 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020141 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142
20143 name[len] = cc;
20144
20145 return ret;
20146}
20147
20148/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020149 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20150 * Also handle function call with Funcref variable: func(expr)
20151 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20152 */
20153 static int
20154handle_subscript(arg, rettv, evaluate, verbose)
20155 char_u **arg;
20156 typval_T *rettv;
20157 int evaluate; /* do more than finding the end */
20158 int verbose; /* give error messages */
20159{
20160 int ret = OK;
20161 dict_T *selfdict = NULL;
20162 char_u *s;
20163 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020164 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020165
20166 while (ret == OK
20167 && (**arg == '['
20168 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020169 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020170 && !vim_iswhite(*(*arg - 1)))
20171 {
20172 if (**arg == '(')
20173 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020174 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020175 if (evaluate)
20176 {
20177 functv = *rettv;
20178 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020179
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020180 /* Invoke the function. Recursive! */
20181 s = functv.vval.v_string;
20182 }
20183 else
20184 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020185 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020186 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20187 &len, evaluate, selfdict);
20188
20189 /* Clear the funcref afterwards, so that deleting it while
20190 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020191 if (evaluate)
20192 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020193
20194 /* Stop the expression evaluation when immediately aborting on
20195 * error, or when an interrupt occurred or an exception was thrown
20196 * but not caught. */
20197 if (aborting())
20198 {
20199 if (ret == OK)
20200 clear_tv(rettv);
20201 ret = FAIL;
20202 }
20203 dict_unref(selfdict);
20204 selfdict = NULL;
20205 }
20206 else /* **arg == '[' || **arg == '.' */
20207 {
20208 dict_unref(selfdict);
20209 if (rettv->v_type == VAR_DICT)
20210 {
20211 selfdict = rettv->vval.v_dict;
20212 if (selfdict != NULL)
20213 ++selfdict->dv_refcount;
20214 }
20215 else
20216 selfdict = NULL;
20217 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20218 {
20219 clear_tv(rettv);
20220 ret = FAIL;
20221 }
20222 }
20223 }
20224 dict_unref(selfdict);
20225 return ret;
20226}
20227
20228/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020229 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020230 * value).
20231 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020232 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020233alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020234{
Bram Moolenaar33570922005-01-25 22:26:29 +000020235 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020236}
20237
20238/*
20239 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 * The string "s" must have been allocated, it is consumed.
20241 * Return NULL for out of memory, the variable otherwise.
20242 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020243 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020244alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245 char_u *s;
20246{
Bram Moolenaar33570922005-01-25 22:26:29 +000020247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020249 rettv = alloc_tv();
20250 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020252 rettv->v_type = VAR_STRING;
20253 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254 }
20255 else
20256 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020257 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258}
20259
20260/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020261 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020263 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020264free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020265 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266{
20267 if (varp != NULL)
20268 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020269 switch (varp->v_type)
20270 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020271 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020272 func_unref(varp->vval.v_string);
20273 /*FALLTHROUGH*/
20274 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020275 vim_free(varp->vval.v_string);
20276 break;
20277 case VAR_LIST:
20278 list_unref(varp->vval.v_list);
20279 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020280 case VAR_DICT:
20281 dict_unref(varp->vval.v_dict);
20282 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020283 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020284#ifdef FEAT_FLOAT
20285 case VAR_FLOAT:
20286#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020287 case VAR_UNKNOWN:
20288 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020289 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020290 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020291 break;
20292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293 vim_free(varp);
20294 }
20295}
20296
20297/*
20298 * Free the memory for a variable value and set the value to NULL or 0.
20299 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020300 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020301clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020302 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303{
20304 if (varp != NULL)
20305 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020306 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020308 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020309 func_unref(varp->vval.v_string);
20310 /*FALLTHROUGH*/
20311 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020312 vim_free(varp->vval.v_string);
20313 varp->vval.v_string = NULL;
20314 break;
20315 case VAR_LIST:
20316 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020317 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020318 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020319 case VAR_DICT:
20320 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020321 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020322 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020323 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020324 varp->vval.v_number = 0;
20325 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020326#ifdef FEAT_FLOAT
20327 case VAR_FLOAT:
20328 varp->vval.v_float = 0.0;
20329 break;
20330#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020331 case VAR_UNKNOWN:
20332 break;
20333 default:
20334 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020336 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337 }
20338}
20339
20340/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020341 * Set the value of a variable to NULL without freeing items.
20342 */
20343 static void
20344init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020345 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020346{
20347 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020348 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020349}
20350
20351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 * Get the number value of a variable.
20353 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020354 * For incompatible types, return 0.
20355 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20356 * caller of incompatible types: it sets *denote to TRUE if "denote"
20357 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 */
20359 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020360get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020361 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020363 int error = FALSE;
20364
20365 return get_tv_number_chk(varp, &error); /* return 0L on error */
20366}
20367
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020368 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020369get_tv_number_chk(varp, denote)
20370 typval_T *varp;
20371 int *denote;
20372{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020373 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020375 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020377 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020378 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020379#ifdef FEAT_FLOAT
20380 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020381 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020382 break;
20383#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020384 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020385 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020386 break;
20387 case VAR_STRING:
20388 if (varp->vval.v_string != NULL)
20389 vim_str2nr(varp->vval.v_string, NULL, NULL,
20390 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020391 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020392 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020393 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020394 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020395 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020396 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020397 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020398 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020399 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020400 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020401 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020402 if (denote == NULL) /* useful for values that must be unsigned */
20403 n = -1;
20404 else
20405 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020406 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407}
20408
20409/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020410 * Get the lnum from the first argument.
20411 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020412 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413 */
20414 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020415get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020416 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417{
Bram Moolenaar33570922005-01-25 22:26:29 +000020418 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419 linenr_T lnum;
20420
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020421 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422 if (lnum == 0) /* no valid number, try using line() */
20423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020424 rettv.v_type = VAR_NUMBER;
20425 f_line(argvars, &rettv);
20426 lnum = rettv.vval.v_number;
20427 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 }
20429 return lnum;
20430}
20431
20432/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020433 * Get the lnum from the first argument.
20434 * Also accepts "$", then "buf" is used.
20435 * Returns 0 on error.
20436 */
20437 static linenr_T
20438get_tv_lnum_buf(argvars, buf)
20439 typval_T *argvars;
20440 buf_T *buf;
20441{
20442 if (argvars[0].v_type == VAR_STRING
20443 && argvars[0].vval.v_string != NULL
20444 && argvars[0].vval.v_string[0] == '$'
20445 && buf != NULL)
20446 return buf->b_ml.ml_line_count;
20447 return get_tv_number_chk(&argvars[0], NULL);
20448}
20449
20450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 * Get the string value of a variable.
20452 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020453 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20454 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 * If the String variable has never been set, return an empty string.
20456 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020457 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20458 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459 */
20460 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020461get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020462 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463{
20464 static char_u mybuf[NUMBUFLEN];
20465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020466 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467}
20468
20469 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020470get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020471 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020472 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020474 char_u *res = get_tv_string_buf_chk(varp, buf);
20475
20476 return res != NULL ? res : (char_u *)"";
20477}
20478
Bram Moolenaar7d647822014-04-05 21:28:56 +020020479/*
20480 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20481 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020482 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020483get_tv_string_chk(varp)
20484 typval_T *varp;
20485{
20486 static char_u mybuf[NUMBUFLEN];
20487
20488 return get_tv_string_buf_chk(varp, mybuf);
20489}
20490
20491 static char_u *
20492get_tv_string_buf_chk(varp, buf)
20493 typval_T *varp;
20494 char_u *buf;
20495{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020496 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020498 case VAR_NUMBER:
20499 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20500 return buf;
20501 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020502 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020503 break;
20504 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020505 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020506 break;
20507 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020508 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020509 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020510#ifdef FEAT_FLOAT
20511 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020512 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020513 break;
20514#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020515 case VAR_STRING:
20516 if (varp->vval.v_string != NULL)
20517 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020518 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020519 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020520 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020521 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020523 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524}
20525
20526/*
20527 * Find variable "name" in the list of variables.
20528 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020529 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020530 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020531 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020533 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020534find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020536 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020537 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020540 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541
Bram Moolenaara7043832005-01-21 11:56:39 +000020542 ht = find_var_ht(name, &varname);
20543 if (htp != NULL)
20544 *htp = ht;
20545 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020547 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548}
20549
20550/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020551 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020552 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020554 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020555find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020556 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020557 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020558 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020559 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020560{
Bram Moolenaar33570922005-01-25 22:26:29 +000020561 hashitem_T *hi;
20562
20563 if (*varname == NUL)
20564 {
20565 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020566 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020567 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020568 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020569 case 'g': return &globvars_var;
20570 case 'v': return &vimvars_var;
20571 case 'b': return &curbuf->b_bufvar;
20572 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020573#ifdef FEAT_WINDOWS
20574 case 't': return &curtab->tp_winvar;
20575#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020576 case 'l': return current_funccal == NULL
20577 ? NULL : &current_funccal->l_vars_var;
20578 case 'a': return current_funccal == NULL
20579 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020580 }
20581 return NULL;
20582 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020583
20584 hi = hash_find(ht, varname);
20585 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020586 {
20587 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020588 * worked find the variable again. Don't auto-load a script if it was
20589 * loaded already, otherwise it would be loaded every time when
20590 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020591 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020592 {
20593 /* Note: script_autoload() may make "hi" invalid. It must either
20594 * be obtained again or not used. */
20595 if (!script_autoload(varname, FALSE) || aborting())
20596 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020597 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020598 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020599 if (HASHITEM_EMPTY(hi))
20600 return NULL;
20601 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020602 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020603}
20604
20605/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020606 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020607 * Set "varname" to the start of name without ':'.
20608 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020609 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020610find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 char_u *name;
20612 char_u **varname;
20613{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020614 hashitem_T *hi;
20615
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 if (name[1] != ':')
20617 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020618 /* The name must not start with a colon or #. */
20619 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620 return NULL;
20621 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020622
20623 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020624 hi = hash_find(&compat_hashtab, name);
20625 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020626 return &compat_hashtab;
20627
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020629 return &globvarht; /* global variable */
20630 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631 }
20632 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020633 if (*name == 'g') /* global variable */
20634 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020635 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20636 */
20637 if (vim_strchr(name + 2, ':') != NULL
20638 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020639 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020641 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020643 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020644#ifdef FEAT_WINDOWS
20645 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020646 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020648 if (*name == 'v') /* v: variable */
20649 return &vimvarht;
20650 if (*name == 'a' && current_funccal != NULL) /* function argument */
20651 return &current_funccal->l_avars.dv_hashtab;
20652 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20653 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654 if (*name == 's' /* script variable */
20655 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20656 return &SCRIPT_VARS(current_SID);
20657 return NULL;
20658}
20659
20660/*
20661 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020662 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 * Returns NULL when it doesn't exist.
20664 */
20665 char_u *
20666get_var_value(name)
20667 char_u *name;
20668{
Bram Moolenaar33570922005-01-25 22:26:29 +000020669 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020670
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020671 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672 if (v == NULL)
20673 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020674 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675}
20676
20677/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020678 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 * sourcing this script and when executing functions defined in the script.
20680 */
20681 void
20682new_script_vars(id)
20683 scid_T id;
20684{
Bram Moolenaara7043832005-01-21 11:56:39 +000020685 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020686 hashtab_T *ht;
20687 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020688
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20690 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020691 /* Re-allocating ga_data means that an ht_array pointing to
20692 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020693 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020694 for (i = 1; i <= ga_scripts.ga_len; ++i)
20695 {
20696 ht = &SCRIPT_VARS(i);
20697 if (ht->ht_mask == HT_INIT_SIZE - 1)
20698 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020699 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020700 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020701 }
20702
Bram Moolenaar071d4272004-06-13 20:20:40 +000020703 while (ga_scripts.ga_len < id)
20704 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020705 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020706 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020707 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020709 }
20710 }
20711}
20712
20713/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020714 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20715 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 */
20717 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020718init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020719 dict_T *dict;
20720 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020721 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020722{
Bram Moolenaar33570922005-01-25 22:26:29 +000020723 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020724 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020725 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020726 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020727 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020728 dict_var->di_tv.vval.v_dict = dict;
20729 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020730 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020731 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20732 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020733}
20734
20735/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020736 * Unreference a dictionary initialized by init_var_dict().
20737 */
20738 void
20739unref_var_dict(dict)
20740 dict_T *dict;
20741{
20742 /* Now the dict needs to be freed if no one else is using it, go back to
20743 * normal reference counting. */
20744 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20745 dict_unref(dict);
20746}
20747
20748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020750 * Frees all allocated variables and the value they contain.
20751 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 */
20753 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020754vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020755 hashtab_T *ht;
20756{
20757 vars_clear_ext(ht, TRUE);
20758}
20759
20760/*
20761 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20762 */
20763 static void
20764vars_clear_ext(ht, free_val)
20765 hashtab_T *ht;
20766 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767{
Bram Moolenaara7043832005-01-21 11:56:39 +000020768 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020769 hashitem_T *hi;
20770 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771
Bram Moolenaar33570922005-01-25 22:26:29 +000020772 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020773 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020774 for (hi = ht->ht_array; todo > 0; ++hi)
20775 {
20776 if (!HASHITEM_EMPTY(hi))
20777 {
20778 --todo;
20779
Bram Moolenaar33570922005-01-25 22:26:29 +000020780 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020781 * ht_array might change then. hash_clear() takes care of it
20782 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020783 v = HI2DI(hi);
20784 if (free_val)
20785 clear_tv(&v->di_tv);
20786 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20787 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020788 }
20789 }
20790 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020791 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792}
20793
Bram Moolenaara7043832005-01-21 11:56:39 +000020794/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020795 * Delete a variable from hashtab "ht" at item "hi".
20796 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020799delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020800 hashtab_T *ht;
20801 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802{
Bram Moolenaar33570922005-01-25 22:26:29 +000020803 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020804
20805 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020806 clear_tv(&di->di_tv);
20807 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808}
20809
20810/*
20811 * List the value of one internal variable.
20812 */
20813 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020814list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020815 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020817 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020819 char_u *tofree;
20820 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020821 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020822
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020823 current_copyID += COPYID_INC;
20824 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020825 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020826 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020827 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828}
20829
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020831list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 char_u *prefix;
20833 char_u *name;
20834 int type;
20835 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020836 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837{
Bram Moolenaar31859182007-08-14 20:41:13 +000020838 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20839 msg_start();
20840 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 if (name != NULL) /* "a:" vars don't have a name stored */
20842 msg_puts(name);
20843 msg_putchar(' ');
20844 msg_advance(22);
20845 if (type == VAR_NUMBER)
20846 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020847 else if (type == VAR_FUNC)
20848 msg_putchar('*');
20849 else if (type == VAR_LIST)
20850 {
20851 msg_putchar('[');
20852 if (*string == '[')
20853 ++string;
20854 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020855 else if (type == VAR_DICT)
20856 {
20857 msg_putchar('{');
20858 if (*string == '{')
20859 ++string;
20860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861 else
20862 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020863
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020865
20866 if (type == VAR_FUNC)
20867 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020868 if (*first)
20869 {
20870 msg_clr_eos();
20871 *first = FALSE;
20872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873}
20874
20875/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020876 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877 * If the variable already exists, the value is updated.
20878 * Otherwise the variable is created.
20879 */
20880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020881set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020883 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020884 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885{
Bram Moolenaar33570922005-01-25 22:26:29 +000020886 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020888 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020890 ht = find_var_ht(name, &varname);
20891 if (ht == NULL || *varname == NUL)
20892 {
20893 EMSG2(_(e_illvar), name);
20894 return;
20895 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020896 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020897
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020898 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20899 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020900
Bram Moolenaar33570922005-01-25 22:26:29 +000020901 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020903 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020904 if (var_check_ro(v->di_flags, name)
20905 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020906 return;
20907 if (v->di_tv.v_type != tv->v_type
20908 && !((v->di_tv.v_type == VAR_STRING
20909 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020910 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020911 || tv->v_type == VAR_NUMBER))
20912#ifdef FEAT_FLOAT
20913 && !((v->di_tv.v_type == VAR_NUMBER
20914 || v->di_tv.v_type == VAR_FLOAT)
20915 && (tv->v_type == VAR_NUMBER
20916 || tv->v_type == VAR_FLOAT))
20917#endif
20918 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020919 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020920 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020921 return;
20922 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020923
20924 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020925 * Handle setting internal v: variables separately: we don't change
20926 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020927 */
20928 if (ht == &vimvarht)
20929 {
20930 if (v->di_tv.v_type == VAR_STRING)
20931 {
20932 vim_free(v->di_tv.vval.v_string);
20933 if (copy || tv->v_type != VAR_STRING)
20934 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20935 else
20936 {
20937 /* Take over the string to avoid an extra alloc/free. */
20938 v->di_tv.vval.v_string = tv->vval.v_string;
20939 tv->vval.v_string = NULL;
20940 }
20941 }
20942 else if (v->di_tv.v_type != VAR_NUMBER)
20943 EMSG2(_(e_intern2), "set_var()");
20944 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020945 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020946 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020947 if (STRCMP(varname, "searchforward") == 0)
20948 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020949#ifdef FEAT_SEARCH_EXTRA
20950 else if (STRCMP(varname, "hlsearch") == 0)
20951 {
20952 no_hlsearch = !v->di_tv.vval.v_number;
20953 redraw_all_later(SOME_VALID);
20954 }
20955#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020956 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020957 return;
20958 }
20959
20960 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961 }
20962 else /* add a new variable */
20963 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020964 /* Can't add "v:" variable. */
20965 if (ht == &vimvarht)
20966 {
20967 EMSG2(_(e_illvar), name);
20968 return;
20969 }
20970
Bram Moolenaar92124a32005-06-17 22:03:40 +000020971 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020972 if (!valid_varname(varname))
20973 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020974
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020975 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20976 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020977 if (v == NULL)
20978 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020979 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020980 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020981 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020982 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 return;
20984 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020985 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020987
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020988 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020989 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020990 else
20991 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020992 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020993 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020994 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996}
20997
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020998/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020999 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021000 * Also give an error message.
21001 */
21002 static int
21003var_check_ro(flags, name)
21004 int flags;
21005 char_u *name;
21006{
21007 if (flags & DI_FLAGS_RO)
21008 {
21009 EMSG2(_(e_readonlyvar), name);
21010 return TRUE;
21011 }
21012 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21013 {
21014 EMSG2(_(e_readonlysbx), name);
21015 return TRUE;
21016 }
21017 return FALSE;
21018}
21019
21020/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021021 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21022 * Also give an error message.
21023 */
21024 static int
21025var_check_fixed(flags, name)
21026 int flags;
21027 char_u *name;
21028{
21029 if (flags & DI_FLAGS_FIX)
21030 {
21031 EMSG2(_("E795: Cannot delete variable %s"), name);
21032 return TRUE;
21033 }
21034 return FALSE;
21035}
21036
21037/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021038 * Check if a funcref is assigned to a valid variable name.
21039 * Return TRUE and give an error if not.
21040 */
21041 static int
21042var_check_func_name(name, new_var)
21043 char_u *name; /* points to start of variable name */
21044 int new_var; /* TRUE when creating the variable */
21045{
21046 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
21047 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21048 ? name[2] : name[0]))
21049 {
21050 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21051 name);
21052 return TRUE;
21053 }
21054 /* Don't allow hiding a function. When "v" is not NULL we might be
21055 * assigning another function to the same var, the type is checked
21056 * below. */
21057 if (new_var && function_exists(name))
21058 {
21059 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21060 name);
21061 return TRUE;
21062 }
21063 return FALSE;
21064}
21065
21066/*
21067 * Check if a variable name is valid.
21068 * Return FALSE and give an error if not.
21069 */
21070 static int
21071valid_varname(varname)
21072 char_u *varname;
21073{
21074 char_u *p;
21075
21076 for (p = varname; *p != NUL; ++p)
21077 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21078 && *p != AUTOLOAD_CHAR)
21079 {
21080 EMSG2(_(e_illvar), varname);
21081 return FALSE;
21082 }
21083 return TRUE;
21084}
21085
21086/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021087 * Return TRUE if typeval "tv" is set to be locked (immutable).
21088 * Also give an error message, using "name".
21089 */
21090 static int
21091tv_check_lock(lock, name)
21092 int lock;
21093 char_u *name;
21094{
21095 if (lock & VAR_LOCKED)
21096 {
21097 EMSG2(_("E741: Value is locked: %s"),
21098 name == NULL ? (char_u *)_("Unknown") : name);
21099 return TRUE;
21100 }
21101 if (lock & VAR_FIXED)
21102 {
21103 EMSG2(_("E742: Cannot change value of %s"),
21104 name == NULL ? (char_u *)_("Unknown") : name);
21105 return TRUE;
21106 }
21107 return FALSE;
21108}
21109
21110/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021111 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021112 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021113 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021114 * It is OK for "from" and "to" to point to the same item. This is used to
21115 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021116 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021117 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021118copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021119 typval_T *from;
21120 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021122 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021123 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021124 switch (from->v_type)
21125 {
21126 case VAR_NUMBER:
21127 to->vval.v_number = from->vval.v_number;
21128 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021129#ifdef FEAT_FLOAT
21130 case VAR_FLOAT:
21131 to->vval.v_float = from->vval.v_float;
21132 break;
21133#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021134 case VAR_STRING:
21135 case VAR_FUNC:
21136 if (from->vval.v_string == NULL)
21137 to->vval.v_string = NULL;
21138 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021139 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021140 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021141 if (from->v_type == VAR_FUNC)
21142 func_ref(to->vval.v_string);
21143 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021144 break;
21145 case VAR_LIST:
21146 if (from->vval.v_list == NULL)
21147 to->vval.v_list = NULL;
21148 else
21149 {
21150 to->vval.v_list = from->vval.v_list;
21151 ++to->vval.v_list->lv_refcount;
21152 }
21153 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021154 case VAR_DICT:
21155 if (from->vval.v_dict == NULL)
21156 to->vval.v_dict = NULL;
21157 else
21158 {
21159 to->vval.v_dict = from->vval.v_dict;
21160 ++to->vval.v_dict->dv_refcount;
21161 }
21162 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021163 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021164 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021165 break;
21166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167}
21168
21169/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021170 * Make a copy of an item.
21171 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021172 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21173 * reference to an already copied list/dict can be used.
21174 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021175 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021176 static int
21177item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021178 typval_T *from;
21179 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021180 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021181 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021182{
21183 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021184 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021185
Bram Moolenaar33570922005-01-25 22:26:29 +000021186 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021187 {
21188 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021189 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021190 }
21191 ++recurse;
21192
21193 switch (from->v_type)
21194 {
21195 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021196#ifdef FEAT_FLOAT
21197 case VAR_FLOAT:
21198#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021199 case VAR_STRING:
21200 case VAR_FUNC:
21201 copy_tv(from, to);
21202 break;
21203 case VAR_LIST:
21204 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021205 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021206 if (from->vval.v_list == NULL)
21207 to->vval.v_list = NULL;
21208 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21209 {
21210 /* use the copy made earlier */
21211 to->vval.v_list = from->vval.v_list->lv_copylist;
21212 ++to->vval.v_list->lv_refcount;
21213 }
21214 else
21215 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21216 if (to->vval.v_list == NULL)
21217 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021218 break;
21219 case VAR_DICT:
21220 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021221 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021222 if (from->vval.v_dict == NULL)
21223 to->vval.v_dict = NULL;
21224 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21225 {
21226 /* use the copy made earlier */
21227 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21228 ++to->vval.v_dict->dv_refcount;
21229 }
21230 else
21231 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21232 if (to->vval.v_dict == NULL)
21233 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021234 break;
21235 default:
21236 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021237 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021238 }
21239 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021240 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021241}
21242
21243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 * ":echo expr1 ..." print each argument separated with a space, add a
21245 * newline at the end.
21246 * ":echon expr1 ..." print each argument plain.
21247 */
21248 void
21249ex_echo(eap)
21250 exarg_T *eap;
21251{
21252 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021253 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021254 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255 char_u *p;
21256 int needclr = TRUE;
21257 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021258 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259
21260 if (eap->skip)
21261 ++emsg_skip;
21262 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21263 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021264 /* If eval1() causes an error message the text from the command may
21265 * still need to be cleared. E.g., "echo 22,44". */
21266 need_clr_eos = needclr;
21267
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021269 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 {
21271 /*
21272 * Report the invalid expression unless the expression evaluation
21273 * has been cancelled due to an aborting error, an interrupt, or an
21274 * exception.
21275 */
21276 if (!aborting())
21277 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021278 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 break;
21280 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021281 need_clr_eos = FALSE;
21282
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 if (!eap->skip)
21284 {
21285 if (atstart)
21286 {
21287 atstart = FALSE;
21288 /* Call msg_start() after eval1(), evaluating the expression
21289 * may cause a message to appear. */
21290 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021291 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021292 /* Mark the saved text as finishing the line, so that what
21293 * follows is displayed on a new line when scrolling back
21294 * at the more prompt. */
21295 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 }
21299 else if (eap->cmdidx == CMD_echo)
21300 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021301 current_copyID += COPYID_INC;
21302 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021303 if (p != NULL)
21304 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021306 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021308 if (*p != TAB && needclr)
21309 {
21310 /* remove any text still there from the command */
21311 msg_clr_eos();
21312 needclr = FALSE;
21313 }
21314 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315 }
21316 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021317 {
21318#ifdef FEAT_MBYTE
21319 if (has_mbyte)
21320 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021321 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021322
21323 (void)msg_outtrans_len_attr(p, i, echo_attr);
21324 p += i - 1;
21325 }
21326 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021328 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021330 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021331 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021333 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 arg = skipwhite(arg);
21335 }
21336 eap->nextcmd = check_nextcmd(arg);
21337
21338 if (eap->skip)
21339 --emsg_skip;
21340 else
21341 {
21342 /* remove text that may still be there from the command */
21343 if (needclr)
21344 msg_clr_eos();
21345 if (eap->cmdidx == CMD_echo)
21346 msg_end();
21347 }
21348}
21349
21350/*
21351 * ":echohl {name}".
21352 */
21353 void
21354ex_echohl(eap)
21355 exarg_T *eap;
21356{
21357 int id;
21358
21359 id = syn_name2id(eap->arg);
21360 if (id == 0)
21361 echo_attr = 0;
21362 else
21363 echo_attr = syn_id2attr(id);
21364}
21365
21366/*
21367 * ":execute expr1 ..." execute the result of an expression.
21368 * ":echomsg expr1 ..." Print a message
21369 * ":echoerr expr1 ..." Print an error
21370 * Each gets spaces around each argument and a newline at the end for
21371 * echo commands
21372 */
21373 void
21374ex_execute(eap)
21375 exarg_T *eap;
21376{
21377 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021378 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 int ret = OK;
21380 char_u *p;
21381 garray_T ga;
21382 int len;
21383 int save_did_emsg;
21384
21385 ga_init2(&ga, 1, 80);
21386
21387 if (eap->skip)
21388 ++emsg_skip;
21389 while (*arg != NUL && *arg != '|' && *arg != '\n')
21390 {
21391 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021392 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 {
21394 /*
21395 * Report the invalid expression unless the expression evaluation
21396 * has been cancelled due to an aborting error, an interrupt, or an
21397 * exception.
21398 */
21399 if (!aborting())
21400 EMSG2(_(e_invexpr2), p);
21401 ret = FAIL;
21402 break;
21403 }
21404
21405 if (!eap->skip)
21406 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021407 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408 len = (int)STRLEN(p);
21409 if (ga_grow(&ga, len + 2) == FAIL)
21410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021411 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 ret = FAIL;
21413 break;
21414 }
21415 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 ga.ga_len += len;
21419 }
21420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021421 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422 arg = skipwhite(arg);
21423 }
21424
21425 if (ret != FAIL && ga.ga_data != NULL)
21426 {
21427 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021430 out_flush();
21431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432 else if (eap->cmdidx == CMD_echoerr)
21433 {
21434 /* We don't want to abort following commands, restore did_emsg. */
21435 save_did_emsg = did_emsg;
21436 EMSG((char_u *)ga.ga_data);
21437 if (!force_abort)
21438 did_emsg = save_did_emsg;
21439 }
21440 else if (eap->cmdidx == CMD_execute)
21441 do_cmdline((char_u *)ga.ga_data,
21442 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21443 }
21444
21445 ga_clear(&ga);
21446
21447 if (eap->skip)
21448 --emsg_skip;
21449
21450 eap->nextcmd = check_nextcmd(arg);
21451}
21452
21453/*
21454 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21455 * "arg" points to the "&" or '+' when called, to "option" when returning.
21456 * Returns NULL when no option name found. Otherwise pointer to the char
21457 * after the option name.
21458 */
21459 static char_u *
21460find_option_end(arg, opt_flags)
21461 char_u **arg;
21462 int *opt_flags;
21463{
21464 char_u *p = *arg;
21465
21466 ++p;
21467 if (*p == 'g' && p[1] == ':')
21468 {
21469 *opt_flags = OPT_GLOBAL;
21470 p += 2;
21471 }
21472 else if (*p == 'l' && p[1] == ':')
21473 {
21474 *opt_flags = OPT_LOCAL;
21475 p += 2;
21476 }
21477 else
21478 *opt_flags = 0;
21479
21480 if (!ASCII_ISALPHA(*p))
21481 return NULL;
21482 *arg = p;
21483
21484 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21485 p += 4; /* termcap option */
21486 else
21487 while (ASCII_ISALPHA(*p))
21488 ++p;
21489 return p;
21490}
21491
21492/*
21493 * ":function"
21494 */
21495 void
21496ex_function(eap)
21497 exarg_T *eap;
21498{
21499 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021500 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 int j;
21502 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021504 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021505 char_u *name = NULL;
21506 char_u *p;
21507 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021508 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509 garray_T newargs;
21510 garray_T newlines;
21511 int varargs = FALSE;
21512 int mustend = FALSE;
21513 int flags = 0;
21514 ufunc_T *fp;
21515 int indent;
21516 int nesting;
21517 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021518 dictitem_T *v;
21519 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021520 static int func_nr = 0; /* number for nameless function */
21521 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021522 hashtab_T *ht;
21523 int todo;
21524 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021525 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526
21527 /*
21528 * ":function" without argument: list functions.
21529 */
21530 if (ends_excmd(*eap->arg))
21531 {
21532 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021533 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021534 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021535 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021536 {
21537 if (!HASHITEM_EMPTY(hi))
21538 {
21539 --todo;
21540 fp = HI2UF(hi);
21541 if (!isdigit(*fp->uf_name))
21542 list_func_head(fp, FALSE);
21543 }
21544 }
21545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546 eap->nextcmd = check_nextcmd(eap->arg);
21547 return;
21548 }
21549
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021550 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021551 * ":function /pat": list functions matching pattern.
21552 */
21553 if (*eap->arg == '/')
21554 {
21555 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21556 if (!eap->skip)
21557 {
21558 regmatch_T regmatch;
21559
21560 c = *p;
21561 *p = NUL;
21562 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21563 *p = c;
21564 if (regmatch.regprog != NULL)
21565 {
21566 regmatch.rm_ic = p_ic;
21567
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021568 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021569 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21570 {
21571 if (!HASHITEM_EMPTY(hi))
21572 {
21573 --todo;
21574 fp = HI2UF(hi);
21575 if (!isdigit(*fp->uf_name)
21576 && vim_regexec(&regmatch, fp->uf_name, 0))
21577 list_func_head(fp, FALSE);
21578 }
21579 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021580 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021581 }
21582 }
21583 if (*p == '/')
21584 ++p;
21585 eap->nextcmd = check_nextcmd(p);
21586 return;
21587 }
21588
21589 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021590 * Get the function name. There are these situations:
21591 * func normal function name
21592 * "name" == func, "fudi.fd_dict" == NULL
21593 * dict.func new dictionary entry
21594 * "name" == NULL, "fudi.fd_dict" set,
21595 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21596 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021597 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021598 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21599 * dict.func existing dict entry that's not a Funcref
21600 * "name" == NULL, "fudi.fd_dict" set,
21601 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020021602 * s:func script-local function name
21603 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021606 name = trans_function_name(&p, eap->skip, 0, &fudi);
21607 paren = (vim_strchr(p, '(') != NULL);
21608 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 {
21610 /*
21611 * Return on an invalid expression in braces, unless the expression
21612 * evaluation has been cancelled due to an aborting error, an
21613 * interrupt, or an exception.
21614 */
21615 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021616 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021617 if (!eap->skip && fudi.fd_newkey != NULL)
21618 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021619 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 else
21623 eap->skip = TRUE;
21624 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021625
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 /* An error in a function call during evaluation of an expression in magic
21627 * braces should not cause the function not to be defined. */
21628 saved_did_emsg = did_emsg;
21629 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630
21631 /*
21632 * ":function func" with only function name: list function.
21633 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021634 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 {
21636 if (!ends_excmd(*skipwhite(p)))
21637 {
21638 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021639 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 }
21641 eap->nextcmd = check_nextcmd(p);
21642 if (eap->nextcmd != NULL)
21643 *p = NUL;
21644 if (!eap->skip && !got_int)
21645 {
21646 fp = find_func(name);
21647 if (fp != NULL)
21648 {
21649 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021650 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021652 if (FUNCLINE(fp, j) == NULL)
21653 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 msg_putchar('\n');
21655 msg_outnum((long)(j + 1));
21656 if (j < 9)
21657 msg_putchar(' ');
21658 if (j < 99)
21659 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021660 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 out_flush(); /* show a line at a time */
21662 ui_breakcheck();
21663 }
21664 if (!got_int)
21665 {
21666 msg_putchar('\n');
21667 msg_puts((char_u *)" endfunction");
21668 }
21669 }
21670 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021671 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021673 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 }
21675
21676 /*
21677 * ":function name(arg1, arg2)" Define function.
21678 */
21679 p = skipwhite(p);
21680 if (*p != '(')
21681 {
21682 if (!eap->skip)
21683 {
21684 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021685 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021686 }
21687 /* attempt to continue by skipping some text */
21688 if (vim_strchr(p, '(') != NULL)
21689 p = vim_strchr(p, '(');
21690 }
21691 p = skipwhite(p + 1);
21692
21693 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21694 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21695
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021696 if (!eap->skip)
21697 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021698 /* Check the name of the function. Unless it's a dictionary function
21699 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021700 if (name != NULL)
21701 arg = name;
21702 else
21703 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021704 if (arg != NULL && (fudi.fd_di == NULL
21705 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021706 {
21707 if (*arg == K_SPECIAL)
21708 j = 3;
21709 else
21710 j = 0;
21711 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21712 : eval_isnamec(arg[j])))
21713 ++j;
21714 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021715 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021716 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021717 /* Disallow using the g: dict. */
21718 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21719 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021720 }
21721
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722 /*
21723 * Isolate the arguments: "arg1, arg2, ...)"
21724 */
21725 while (*p != ')')
21726 {
21727 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21728 {
21729 varargs = TRUE;
21730 p += 3;
21731 mustend = TRUE;
21732 }
21733 else
21734 {
21735 arg = p;
21736 while (ASCII_ISALNUM(*p) || *p == '_')
21737 ++p;
21738 if (arg == p || isdigit(*arg)
21739 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21740 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21741 {
21742 if (!eap->skip)
21743 EMSG2(_("E125: Illegal argument: %s"), arg);
21744 break;
21745 }
21746 if (ga_grow(&newargs, 1) == FAIL)
21747 goto erret;
21748 c = *p;
21749 *p = NUL;
21750 arg = vim_strsave(arg);
21751 if (arg == NULL)
21752 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021753
21754 /* Check for duplicate argument name. */
21755 for (i = 0; i < newargs.ga_len; ++i)
21756 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21757 {
21758 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021759 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021760 goto erret;
21761 }
21762
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21764 *p = c;
21765 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766 if (*p == ',')
21767 ++p;
21768 else
21769 mustend = TRUE;
21770 }
21771 p = skipwhite(p);
21772 if (mustend && *p != ')')
21773 {
21774 if (!eap->skip)
21775 EMSG2(_(e_invarg2), eap->arg);
21776 break;
21777 }
21778 }
21779 ++p; /* skip the ')' */
21780
Bram Moolenaare9a41262005-01-15 22:18:47 +000021781 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 for (;;)
21783 {
21784 p = skipwhite(p);
21785 if (STRNCMP(p, "range", 5) == 0)
21786 {
21787 flags |= FC_RANGE;
21788 p += 5;
21789 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021790 else if (STRNCMP(p, "dict", 4) == 0)
21791 {
21792 flags |= FC_DICT;
21793 p += 4;
21794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 else if (STRNCMP(p, "abort", 5) == 0)
21796 {
21797 flags |= FC_ABORT;
21798 p += 5;
21799 }
21800 else
21801 break;
21802 }
21803
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021804 /* When there is a line break use what follows for the function body.
21805 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21806 if (*p == '\n')
21807 line_arg = p + 1;
21808 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809 EMSG(_(e_trailing));
21810
21811 /*
21812 * Read the body of the function, until ":endfunction" is found.
21813 */
21814 if (KeyTyped)
21815 {
21816 /* Check if the function already exists, don't let the user type the
21817 * whole function before telling him it doesn't work! For a script we
21818 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021819 if (!eap->skip && !eap->forceit)
21820 {
21821 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21822 EMSG(_(e_funcdict));
21823 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021824 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021827 if (!eap->skip && did_emsg)
21828 goto erret;
21829
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830 msg_putchar('\n'); /* don't overwrite the function name */
21831 cmdline_row = msg_row;
21832 }
21833
21834 indent = 2;
21835 nesting = 0;
21836 for (;;)
21837 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021838 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021839 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021840 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021841 saved_wait_return = FALSE;
21842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021844 sourcing_lnum_off = sourcing_lnum;
21845
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021846 if (line_arg != NULL)
21847 {
21848 /* Use eap->arg, split up in parts by line breaks. */
21849 theline = line_arg;
21850 p = vim_strchr(theline, '\n');
21851 if (p == NULL)
21852 line_arg += STRLEN(line_arg);
21853 else
21854 {
21855 *p = NUL;
21856 line_arg = p + 1;
21857 }
21858 }
21859 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 theline = getcmdline(':', 0L, indent);
21861 else
21862 theline = eap->getline(':', eap->cookie, indent);
21863 if (KeyTyped)
21864 lines_left = Rows - 1;
21865 if (theline == NULL)
21866 {
21867 EMSG(_("E126: Missing :endfunction"));
21868 goto erret;
21869 }
21870
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021871 /* Detect line continuation: sourcing_lnum increased more than one. */
21872 if (sourcing_lnum > sourcing_lnum_off + 1)
21873 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21874 else
21875 sourcing_lnum_off = 0;
21876
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877 if (skip_until != NULL)
21878 {
21879 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21880 * don't check for ":endfunc". */
21881 if (STRCMP(theline, skip_until) == 0)
21882 {
21883 vim_free(skip_until);
21884 skip_until = NULL;
21885 }
21886 }
21887 else
21888 {
21889 /* skip ':' and blanks*/
21890 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21891 ;
21892
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021893 /* Check for "endfunction". */
21894 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021896 if (line_arg == NULL)
21897 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 break;
21899 }
21900
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021901 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 * at "end". */
21903 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21904 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021905 else if (STRNCMP(p, "if", 2) == 0
21906 || STRNCMP(p, "wh", 2) == 0
21907 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908 || STRNCMP(p, "try", 3) == 0)
21909 indent += 2;
21910
21911 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021912 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021914 if (*p == '!')
21915 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916 p += eval_fname_script(p);
21917 if (ASCII_ISALPHA(*p))
21918 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021919 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 if (*skipwhite(p) == '(')
21921 {
21922 ++nesting;
21923 indent += 2;
21924 }
21925 }
21926 }
21927
21928 /* Check for ":append" or ":insert". */
21929 p = skip_range(p, NULL);
21930 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21931 || (p[0] == 'i'
21932 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21933 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21934 skip_until = vim_strsave((char_u *)".");
21935
21936 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21937 arg = skipwhite(skiptowhite(p));
21938 if (arg[0] == '<' && arg[1] =='<'
21939 && ((p[0] == 'p' && p[1] == 'y'
21940 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21941 || (p[0] == 'p' && p[1] == 'e'
21942 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21943 || (p[0] == 't' && p[1] == 'c'
21944 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021945 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21946 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21948 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021949 || (p[0] == 'm' && p[1] == 'z'
21950 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 ))
21952 {
21953 /* ":python <<" continues until a dot, like ":append" */
21954 p = skipwhite(arg + 2);
21955 if (*p == NUL)
21956 skip_until = vim_strsave((char_u *)".");
21957 else
21958 skip_until = vim_strsave(p);
21959 }
21960 }
21961
21962 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021963 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021964 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021965 if (line_arg == NULL)
21966 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021968 }
21969
21970 /* Copy the line to newly allocated memory. get_one_sourceline()
21971 * allocates 250 bytes per line, this saves 80% on average. The cost
21972 * is an extra alloc/free. */
21973 p = vim_strsave(theline);
21974 if (p != NULL)
21975 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021976 if (line_arg == NULL)
21977 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021978 theline = p;
21979 }
21980
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021981 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21982
21983 /* Add NULL lines for continuation lines, so that the line count is
21984 * equal to the index in the growarray. */
21985 while (sourcing_lnum_off-- > 0)
21986 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021987
21988 /* Check for end of eap->arg. */
21989 if (line_arg != NULL && *line_arg == NUL)
21990 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 }
21992
21993 /* Don't define the function when skipping commands or when an error was
21994 * detected. */
21995 if (eap->skip || did_emsg)
21996 goto erret;
21997
21998 /*
21999 * If there are no errors, add the function
22000 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022001 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022002 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022003 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022004 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022005 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022006 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022007 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022008 goto erret;
22009 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022011 fp = find_func(name);
22012 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022014 if (!eap->forceit)
22015 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022016 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022017 goto erret;
22018 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022019 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022020 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022021 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022022 name);
22023 goto erret;
22024 }
22025 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022026 ga_clear_strings(&(fp->uf_args));
22027 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022028 vim_free(name);
22029 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031 }
22032 else
22033 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022034 char numbuf[20];
22035
22036 fp = NULL;
22037 if (fudi.fd_newkey == NULL && !eap->forceit)
22038 {
22039 EMSG(_(e_funcdict));
22040 goto erret;
22041 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022042 if (fudi.fd_di == NULL)
22043 {
22044 /* Can't add a function to a locked dictionary */
22045 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22046 goto erret;
22047 }
22048 /* Can't change an existing function if it is locked */
22049 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22050 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022051
22052 /* Give the function a sequential number. Can only be used with a
22053 * Funcref! */
22054 vim_free(name);
22055 sprintf(numbuf, "%d", ++func_nr);
22056 name = vim_strsave((char_u *)numbuf);
22057 if (name == NULL)
22058 goto erret;
22059 }
22060
22061 if (fp == NULL)
22062 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022063 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022064 {
22065 int slen, plen;
22066 char_u *scriptname;
22067
22068 /* Check that the autoload name matches the script name. */
22069 j = FAIL;
22070 if (sourcing_name != NULL)
22071 {
22072 scriptname = autoload_name(name);
22073 if (scriptname != NULL)
22074 {
22075 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022076 plen = (int)STRLEN(p);
22077 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022078 if (slen > plen && fnamecmp(p,
22079 sourcing_name + slen - plen) == 0)
22080 j = OK;
22081 vim_free(scriptname);
22082 }
22083 }
22084 if (j == FAIL)
22085 {
22086 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22087 goto erret;
22088 }
22089 }
22090
22091 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 if (fp == NULL)
22093 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022094
22095 if (fudi.fd_dict != NULL)
22096 {
22097 if (fudi.fd_di == NULL)
22098 {
22099 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022100 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022101 if (fudi.fd_di == NULL)
22102 {
22103 vim_free(fp);
22104 goto erret;
22105 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022106 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22107 {
22108 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022109 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022110 goto erret;
22111 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022112 }
22113 else
22114 /* overwrite existing dict entry */
22115 clear_tv(&fudi.fd_di->di_tv);
22116 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022117 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022118 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022119 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022120
22121 /* behave like "dict" was used */
22122 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022123 }
22124
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022126 STRCPY(fp->uf_name, name);
22127 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022129 fp->uf_args = newargs;
22130 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022131#ifdef FEAT_PROFILE
22132 fp->uf_tml_count = NULL;
22133 fp->uf_tml_total = NULL;
22134 fp->uf_tml_self = NULL;
22135 fp->uf_profiling = FALSE;
22136 if (prof_def_func())
22137 func_do_profile(fp);
22138#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022139 fp->uf_varargs = varargs;
22140 fp->uf_flags = flags;
22141 fp->uf_calls = 0;
22142 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022143 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144
22145erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 ga_clear_strings(&newargs);
22147 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022148ret_free:
22149 vim_free(skip_until);
22150 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022153 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154}
22155
22156/*
22157 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022158 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022160 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022161 * TFN_INT: internal function name OK
22162 * TFN_QUIET: be quiet
22163 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164 * Advances "pp" to just after the function name (if no error).
22165 */
22166 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022167trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 char_u **pp;
22169 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022170 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022171 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022173 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 char_u *start;
22175 char_u *end;
22176 int lead;
22177 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022179 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022180
22181 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022182 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022183 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022184
22185 /* Check for hard coded <SNR>: already translated function ID (from a user
22186 * command). */
22187 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22188 && (*pp)[2] == (int)KE_SNR)
22189 {
22190 *pp += 3;
22191 len = get_id_len(pp) + 3;
22192 return vim_strnsave(start, len);
22193 }
22194
22195 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22196 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022198 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022200
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022201 /* Note that TFN_ flags use the same values as GLV_ flags. */
22202 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022203 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022204 if (end == start)
22205 {
22206 if (!skip)
22207 EMSG(_("E129: Function name required"));
22208 goto theend;
22209 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022210 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022211 {
22212 /*
22213 * Report an invalid expression in braces, unless the expression
22214 * evaluation has been cancelled due to an aborting error, an
22215 * interrupt, or an exception.
22216 */
22217 if (!aborting())
22218 {
22219 if (end != NULL)
22220 EMSG2(_(e_invarg2), start);
22221 }
22222 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022223 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022224 goto theend;
22225 }
22226
22227 if (lv.ll_tv != NULL)
22228 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022229 if (fdp != NULL)
22230 {
22231 fdp->fd_dict = lv.ll_dict;
22232 fdp->fd_newkey = lv.ll_newkey;
22233 lv.ll_newkey = NULL;
22234 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022235 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022236 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22237 {
22238 name = vim_strsave(lv.ll_tv->vval.v_string);
22239 *pp = end;
22240 }
22241 else
22242 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022243 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22244 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022245 EMSG(_(e_funcref));
22246 else
22247 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022248 name = NULL;
22249 }
22250 goto theend;
22251 }
22252
22253 if (lv.ll_name == NULL)
22254 {
22255 /* Error found, but continue after the function name. */
22256 *pp = end;
22257 goto theend;
22258 }
22259
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022260 /* Check if the name is a Funcref. If so, use the value. */
22261 if (lv.ll_exp_name != NULL)
22262 {
22263 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022264 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022265 if (name == lv.ll_exp_name)
22266 name = NULL;
22267 }
22268 else
22269 {
22270 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022271 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022272 if (name == *pp)
22273 name = NULL;
22274 }
22275 if (name != NULL)
22276 {
22277 name = vim_strsave(name);
22278 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022279 if (STRNCMP(name, "<SNR>", 5) == 0)
22280 {
22281 /* Change "<SNR>" to the byte sequence. */
22282 name[0] = K_SPECIAL;
22283 name[1] = KS_EXTRA;
22284 name[2] = (int)KE_SNR;
22285 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22286 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022287 goto theend;
22288 }
22289
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022290 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022291 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022292 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022293 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22294 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22295 {
22296 /* When there was "s:" already or the name expanded to get a
22297 * leading "s:" then remove it. */
22298 lv.ll_name += 2;
22299 len -= 2;
22300 lead = 2;
22301 }
22302 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022303 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022304 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022305 /* skip over "s:" and "g:" */
22306 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022307 lv.ll_name += 2;
22308 len = (int)(end - lv.ll_name);
22309 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022310
22311 /*
22312 * Copy the function name to allocated memory.
22313 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22314 * Accept <SNR>123_name() outside a script.
22315 */
22316 if (skip)
22317 lead = 0; /* do nothing */
22318 else if (lead > 0)
22319 {
22320 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022321 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22322 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022323 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022324 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022325 if (current_SID <= 0)
22326 {
22327 EMSG(_(e_usingsid));
22328 goto theend;
22329 }
22330 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22331 lead += (int)STRLEN(sid_buf);
22332 }
22333 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022334 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022335 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022336 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022337 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022338 goto theend;
22339 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022340 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022341 {
22342 char_u *cp = vim_strchr(lv.ll_name, ':');
22343
22344 if (cp != NULL && cp < end)
22345 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022346 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022347 goto theend;
22348 }
22349 }
22350
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022351 name = alloc((unsigned)(len + lead + 1));
22352 if (name != NULL)
22353 {
22354 if (lead > 0)
22355 {
22356 name[0] = K_SPECIAL;
22357 name[1] = KS_EXTRA;
22358 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022359 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022360 STRCPY(name + 3, sid_buf);
22361 }
22362 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022363 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022364 }
22365 *pp = end;
22366
22367theend:
22368 clear_lval(&lv);
22369 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370}
22371
22372/*
22373 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22374 * Return 2 if "p" starts with "s:".
22375 * Return 0 otherwise.
22376 */
22377 static int
22378eval_fname_script(p)
22379 char_u *p;
22380{
22381 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22382 || STRNICMP(p + 1, "SNR>", 4) == 0))
22383 return 5;
22384 if (p[0] == 's' && p[1] == ':')
22385 return 2;
22386 return 0;
22387}
22388
22389/*
22390 * Return TRUE if "p" starts with "<SID>" or "s:".
22391 * Only works if eval_fname_script() returned non-zero for "p"!
22392 */
22393 static int
22394eval_fname_sid(p)
22395 char_u *p;
22396{
22397 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22398}
22399
22400/*
22401 * List the head of the function: "name(arg1, arg2)".
22402 */
22403 static void
22404list_func_head(fp, indent)
22405 ufunc_T *fp;
22406 int indent;
22407{
22408 int j;
22409
22410 msg_start();
22411 if (indent)
22412 MSG_PUTS(" ");
22413 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022414 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415 {
22416 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022417 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418 }
22419 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022420 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022422 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 {
22424 if (j)
22425 MSG_PUTS(", ");
22426 msg_puts(FUNCARG(fp, j));
22427 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022428 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 {
22430 if (j)
22431 MSG_PUTS(", ");
22432 MSG_PUTS("...");
22433 }
22434 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022435 if (fp->uf_flags & FC_ABORT)
22436 MSG_PUTS(" abort");
22437 if (fp->uf_flags & FC_RANGE)
22438 MSG_PUTS(" range");
22439 if (fp->uf_flags & FC_DICT)
22440 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022441 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022442 if (p_verbose > 0)
22443 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444}
22445
22446/*
22447 * Find a function by name, return pointer to it in ufuncs.
22448 * Return NULL for unknown function.
22449 */
22450 static ufunc_T *
22451find_func(name)
22452 char_u *name;
22453{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022454 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022456 hi = hash_find(&func_hashtab, name);
22457 if (!HASHITEM_EMPTY(hi))
22458 return HI2UF(hi);
22459 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460}
22461
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022462#if defined(EXITFREE) || defined(PROTO)
22463 void
22464free_all_functions()
22465{
22466 hashitem_T *hi;
22467
22468 /* Need to start all over every time, because func_free() may change the
22469 * hash table. */
22470 while (func_hashtab.ht_used > 0)
22471 for (hi = func_hashtab.ht_array; ; ++hi)
22472 if (!HASHITEM_EMPTY(hi))
22473 {
22474 func_free(HI2UF(hi));
22475 break;
22476 }
22477}
22478#endif
22479
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022480 int
22481translated_function_exists(name)
22482 char_u *name;
22483{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022484 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022485 return find_internal_func(name) >= 0;
22486 return find_func(name) != NULL;
22487}
22488
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022489/*
22490 * Return TRUE if a function "name" exists.
22491 */
22492 static int
22493function_exists(name)
22494 char_u *name;
22495{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022496 char_u *nm = name;
22497 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022498 int n = FALSE;
22499
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022500 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22501 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022502 nm = skipwhite(nm);
22503
22504 /* Only accept "funcname", "funcname ", "funcname (..." and
22505 * "funcname(...", not "funcname!...". */
22506 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022507 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022508 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022509 return n;
22510}
22511
Bram Moolenaara1544c02013-05-30 12:35:52 +020022512 char_u *
22513get_expanded_name(name, check)
22514 char_u *name;
22515 int check;
22516{
22517 char_u *nm = name;
22518 char_u *p;
22519
22520 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22521
22522 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022523 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022524 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022525
Bram Moolenaara1544c02013-05-30 12:35:52 +020022526 vim_free(p);
22527 return NULL;
22528}
22529
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022530/*
22531 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022532 * lower case letter and doesn't contain AUTOLOAD_CHAR.
22533 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022534 */
22535 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022536builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022537 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022538 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022539{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022540 char_u *p;
22541
22542 if (!ASCII_ISLOWER(name[0]))
22543 return FALSE;
22544 p = vim_strchr(name, AUTOLOAD_CHAR);
22545 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022546}
22547
Bram Moolenaar05159a02005-02-26 23:04:13 +000022548#if defined(FEAT_PROFILE) || defined(PROTO)
22549/*
22550 * Start profiling function "fp".
22551 */
22552 static void
22553func_do_profile(fp)
22554 ufunc_T *fp;
22555{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022556 int len = fp->uf_lines.ga_len;
22557
22558 if (len == 0)
22559 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022560 fp->uf_tm_count = 0;
22561 profile_zero(&fp->uf_tm_self);
22562 profile_zero(&fp->uf_tm_total);
22563 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022564 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022565 if (fp->uf_tml_total == NULL)
22566 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022567 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022568 if (fp->uf_tml_self == NULL)
22569 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022570 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022571 fp->uf_tml_idx = -1;
22572 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22573 || fp->uf_tml_self == NULL)
22574 return; /* out of memory */
22575
22576 fp->uf_profiling = TRUE;
22577}
22578
22579/*
22580 * Dump the profiling results for all functions in file "fd".
22581 */
22582 void
22583func_dump_profile(fd)
22584 FILE *fd;
22585{
22586 hashitem_T *hi;
22587 int todo;
22588 ufunc_T *fp;
22589 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022590 ufunc_T **sorttab;
22591 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022592
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022593 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022594 if (todo == 0)
22595 return; /* nothing to dump */
22596
Bram Moolenaar73830342005-02-28 22:48:19 +000022597 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22598
Bram Moolenaar05159a02005-02-26 23:04:13 +000022599 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22600 {
22601 if (!HASHITEM_EMPTY(hi))
22602 {
22603 --todo;
22604 fp = HI2UF(hi);
22605 if (fp->uf_profiling)
22606 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022607 if (sorttab != NULL)
22608 sorttab[st_len++] = fp;
22609
Bram Moolenaar05159a02005-02-26 23:04:13 +000022610 if (fp->uf_name[0] == K_SPECIAL)
22611 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22612 else
22613 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22614 if (fp->uf_tm_count == 1)
22615 fprintf(fd, "Called 1 time\n");
22616 else
22617 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22618 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22619 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22620 fprintf(fd, "\n");
22621 fprintf(fd, "count total (s) self (s)\n");
22622
22623 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22624 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022625 if (FUNCLINE(fp, i) == NULL)
22626 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022627 prof_func_line(fd, fp->uf_tml_count[i],
22628 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022629 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22630 }
22631 fprintf(fd, "\n");
22632 }
22633 }
22634 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022635
22636 if (sorttab != NULL && st_len > 0)
22637 {
22638 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22639 prof_total_cmp);
22640 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22641 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22642 prof_self_cmp);
22643 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22644 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022645
22646 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022647}
Bram Moolenaar73830342005-02-28 22:48:19 +000022648
22649 static void
22650prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22651 FILE *fd;
22652 ufunc_T **sorttab;
22653 int st_len;
22654 char *title;
22655 int prefer_self; /* when equal print only self time */
22656{
22657 int i;
22658 ufunc_T *fp;
22659
22660 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22661 fprintf(fd, "count total (s) self (s) function\n");
22662 for (i = 0; i < 20 && i < st_len; ++i)
22663 {
22664 fp = sorttab[i];
22665 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22666 prefer_self);
22667 if (fp->uf_name[0] == K_SPECIAL)
22668 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22669 else
22670 fprintf(fd, " %s()\n", fp->uf_name);
22671 }
22672 fprintf(fd, "\n");
22673}
22674
22675/*
22676 * Print the count and times for one function or function line.
22677 */
22678 static void
22679prof_func_line(fd, count, total, self, prefer_self)
22680 FILE *fd;
22681 int count;
22682 proftime_T *total;
22683 proftime_T *self;
22684 int prefer_self; /* when equal print only self time */
22685{
22686 if (count > 0)
22687 {
22688 fprintf(fd, "%5d ", count);
22689 if (prefer_self && profile_equal(total, self))
22690 fprintf(fd, " ");
22691 else
22692 fprintf(fd, "%s ", profile_msg(total));
22693 if (!prefer_self && profile_equal(total, self))
22694 fprintf(fd, " ");
22695 else
22696 fprintf(fd, "%s ", profile_msg(self));
22697 }
22698 else
22699 fprintf(fd, " ");
22700}
22701
22702/*
22703 * Compare function for total time sorting.
22704 */
22705 static int
22706#ifdef __BORLANDC__
22707_RTLENTRYF
22708#endif
22709prof_total_cmp(s1, s2)
22710 const void *s1;
22711 const void *s2;
22712{
22713 ufunc_T *p1, *p2;
22714
22715 p1 = *(ufunc_T **)s1;
22716 p2 = *(ufunc_T **)s2;
22717 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22718}
22719
22720/*
22721 * Compare function for self time sorting.
22722 */
22723 static int
22724#ifdef __BORLANDC__
22725_RTLENTRYF
22726#endif
22727prof_self_cmp(s1, s2)
22728 const void *s1;
22729 const void *s2;
22730{
22731 ufunc_T *p1, *p2;
22732
22733 p1 = *(ufunc_T **)s1;
22734 p2 = *(ufunc_T **)s2;
22735 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22736}
22737
Bram Moolenaar05159a02005-02-26 23:04:13 +000022738#endif
22739
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022740/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022741 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022742 * Return TRUE if a package was loaded.
22743 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022744 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022745script_autoload(name, reload)
22746 char_u *name;
22747 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022748{
22749 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022750 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022751 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022752 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022753
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022754 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022755 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022756 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022757 return FALSE;
22758
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022759 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022760
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022761 /* Find the name in the list of previously loaded package names. Skip
22762 * "autoload/", it's always the same. */
22763 for (i = 0; i < ga_loaded.ga_len; ++i)
22764 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22765 break;
22766 if (!reload && i < ga_loaded.ga_len)
22767 ret = FALSE; /* was loaded already */
22768 else
22769 {
22770 /* Remember the name if it wasn't loaded already. */
22771 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22772 {
22773 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22774 tofree = NULL;
22775 }
22776
22777 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022778 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022779 ret = TRUE;
22780 }
22781
22782 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022783 return ret;
22784}
22785
22786/*
22787 * Return the autoload script name for a function or variable name.
22788 * Returns NULL when out of memory.
22789 */
22790 static char_u *
22791autoload_name(name)
22792 char_u *name;
22793{
22794 char_u *p;
22795 char_u *scriptname;
22796
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022797 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022798 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22799 if (scriptname == NULL)
22800 return FALSE;
22801 STRCPY(scriptname, "autoload/");
22802 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022803 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022804 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022805 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022806 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022807 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022808}
22809
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22811
22812/*
22813 * Function given to ExpandGeneric() to obtain the list of user defined
22814 * function names.
22815 */
22816 char_u *
22817get_user_func_name(xp, idx)
22818 expand_T *xp;
22819 int idx;
22820{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022821 static long_u done;
22822 static hashitem_T *hi;
22823 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824
22825 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022827 done = 0;
22828 hi = func_hashtab.ht_array;
22829 }
22830 if (done < func_hashtab.ht_used)
22831 {
22832 if (done++ > 0)
22833 ++hi;
22834 while (HASHITEM_EMPTY(hi))
22835 ++hi;
22836 fp = HI2UF(hi);
22837
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022838 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022839 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022840
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022841 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22842 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843
22844 cat_func_name(IObuff, fp);
22845 if (xp->xp_context != EXPAND_USER_FUNC)
22846 {
22847 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022848 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 STRCAT(IObuff, ")");
22850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 return IObuff;
22852 }
22853 return NULL;
22854}
22855
22856#endif /* FEAT_CMDL_COMPL */
22857
22858/*
22859 * Copy the function name of "fp" to buffer "buf".
22860 * "buf" must be able to hold the function name plus three bytes.
22861 * Takes care of script-local function names.
22862 */
22863 static void
22864cat_func_name(buf, fp)
22865 char_u *buf;
22866 ufunc_T *fp;
22867{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022868 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 {
22870 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022871 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 }
22873 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022874 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875}
22876
22877/*
22878 * ":delfunction {name}"
22879 */
22880 void
22881ex_delfunction(eap)
22882 exarg_T *eap;
22883{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022884 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885 char_u *p;
22886 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022887 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888
22889 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022890 name = trans_function_name(&p, eap->skip, 0, &fudi);
22891 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022893 {
22894 if (fudi.fd_dict != NULL && !eap->skip)
22895 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898 if (!ends_excmd(*skipwhite(p)))
22899 {
22900 vim_free(name);
22901 EMSG(_(e_trailing));
22902 return;
22903 }
22904 eap->nextcmd = check_nextcmd(p);
22905 if (eap->nextcmd != NULL)
22906 *p = NUL;
22907
22908 if (!eap->skip)
22909 fp = find_func(name);
22910 vim_free(name);
22911
22912 if (!eap->skip)
22913 {
22914 if (fp == NULL)
22915 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022916 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917 return;
22918 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022919 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 {
22921 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22922 return;
22923 }
22924
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022925 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022926 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022927 /* Delete the dict item that refers to the function, it will
22928 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022929 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022930 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022931 else
22932 func_free(fp);
22933 }
22934}
22935
22936/*
22937 * Free a function and remove it from the list of functions.
22938 */
22939 static void
22940func_free(fp)
22941 ufunc_T *fp;
22942{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022943 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022944
22945 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022946 ga_clear_strings(&(fp->uf_args));
22947 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022948#ifdef FEAT_PROFILE
22949 vim_free(fp->uf_tml_count);
22950 vim_free(fp->uf_tml_total);
22951 vim_free(fp->uf_tml_self);
22952#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022953
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022954 /* remove the function from the function hashtable */
22955 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22956 if (HASHITEM_EMPTY(hi))
22957 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022958 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022959 hash_remove(&func_hashtab, hi);
22960
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022961 vim_free(fp);
22962}
22963
22964/*
22965 * Unreference a Function: decrement the reference count and free it when it
22966 * becomes zero. Only for numbered functions.
22967 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022968 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022969func_unref(name)
22970 char_u *name;
22971{
22972 ufunc_T *fp;
22973
22974 if (name != NULL && isdigit(*name))
22975 {
22976 fp = find_func(name);
22977 if (fp == NULL)
22978 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022979 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022980 {
22981 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022982 * when "uf_calls" becomes zero. */
22983 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022984 func_free(fp);
22985 }
22986 }
22987}
22988
22989/*
22990 * Count a reference to a Function.
22991 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022992 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022993func_ref(name)
22994 char_u *name;
22995{
22996 ufunc_T *fp;
22997
22998 if (name != NULL && isdigit(*name))
22999 {
23000 fp = find_func(name);
23001 if (fp == NULL)
23002 EMSG2(_(e_intern2), "func_ref()");
23003 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023004 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 }
23006}
23007
23008/*
23009 * Call a user function.
23010 */
23011 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023012call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013 ufunc_T *fp; /* pointer to function */
23014 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023015 typval_T *argvars; /* arguments */
23016 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017 linenr_T firstline; /* first line of range */
23018 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023019 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020{
Bram Moolenaar33570922005-01-25 22:26:29 +000023021 char_u *save_sourcing_name;
23022 linenr_T save_sourcing_lnum;
23023 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023024 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023025 int save_did_emsg;
23026 static int depth = 0;
23027 dictitem_T *v;
23028 int fixvar_idx = 0; /* index in fixvar[] */
23029 int i;
23030 int ai;
23031 char_u numbuf[NUMBUFLEN];
23032 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023033#ifdef FEAT_PROFILE
23034 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023035 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037
23038 /* If depth of calling is getting too high, don't execute the function */
23039 if (depth >= p_mfd)
23040 {
23041 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023042 rettv->v_type = VAR_NUMBER;
23043 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023044 return;
23045 }
23046 ++depth;
23047
23048 line_breakcheck(); /* check for CTRL-C hit */
23049
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023050 fc = (funccall_T *)alloc(sizeof(funccall_T));
23051 fc->caller = current_funccal;
23052 current_funccal = fc;
23053 fc->func = fp;
23054 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023055 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023056 fc->linenr = 0;
23057 fc->returned = FALSE;
23058 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023059 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023060 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23061 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062
Bram Moolenaar33570922005-01-25 22:26:29 +000023063 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023064 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023065 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23066 * each argument variable and saves a lot of time.
23067 */
23068 /*
23069 * Init l: variables.
23070 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023071 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023072 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023073 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023074 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23075 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023076 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023077 name = v->di_key;
23078 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023079 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023080 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023081 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023082 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023083 v->di_tv.vval.v_dict = selfdict;
23084 ++selfdict->dv_refcount;
23085 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023086
Bram Moolenaar33570922005-01-25 22:26:29 +000023087 /*
23088 * Init a: variables.
23089 * Set a:0 to "argcount".
23090 * Set a:000 to a list with room for the "..." arguments.
23091 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023092 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023093 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023094 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023095 /* Use "name" to avoid a warning from some compiler that checks the
23096 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023097 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023098 name = v->di_key;
23099 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023100 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023101 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023102 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023103 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023104 v->di_tv.vval.v_list = &fc->l_varlist;
23105 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23106 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23107 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023108
23109 /*
23110 * Set a:firstline to "firstline" and a:lastline to "lastline".
23111 * Set a:name to named arguments.
23112 * Set a:N to the "..." arguments.
23113 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023114 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023115 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023116 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023117 (varnumber_T)lastline);
23118 for (i = 0; i < argcount; ++i)
23119 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023120 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023121 if (ai < 0)
23122 /* named argument a:name */
23123 name = FUNCARG(fp, i);
23124 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023125 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023126 /* "..." argument a:1, a:2, etc. */
23127 sprintf((char *)numbuf, "%d", ai + 1);
23128 name = numbuf;
23129 }
23130 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23131 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023132 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023133 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23134 }
23135 else
23136 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023137 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23138 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023139 if (v == NULL)
23140 break;
23141 v->di_flags = DI_FLAGS_RO;
23142 }
23143 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023144 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023145
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023146 /* Note: the values are copied directly to avoid alloc/free.
23147 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023148 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023149 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023150
23151 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23152 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023153 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23154 fc->l_listitems[ai].li_tv = argvars[i];
23155 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023156 }
23157 }
23158
Bram Moolenaar071d4272004-06-13 20:20:40 +000023159 /* Don't redraw while executing the function. */
23160 ++RedrawingDisabled;
23161 save_sourcing_name = sourcing_name;
23162 save_sourcing_lnum = sourcing_lnum;
23163 sourcing_lnum = 1;
23164 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023165 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 if (sourcing_name != NULL)
23167 {
23168 if (save_sourcing_name != NULL
23169 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23170 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23171 else
23172 STRCPY(sourcing_name, "function ");
23173 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23174
23175 if (p_verbose >= 12)
23176 {
23177 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023178 verbose_enter_scroll();
23179
Bram Moolenaar555b2802005-05-19 21:08:39 +000023180 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181 if (p_verbose >= 14)
23182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023184 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023185 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023186 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187
23188 msg_puts((char_u *)"(");
23189 for (i = 0; i < argcount; ++i)
23190 {
23191 if (i > 0)
23192 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023193 if (argvars[i].v_type == VAR_NUMBER)
23194 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 else
23196 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023197 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
23198 if (s != NULL)
23199 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023200 if (vim_strsize(s) > MSG_BUF_CLEN)
23201 {
23202 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23203 s = buf;
23204 }
23205 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023206 vim_free(tofree);
23207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208 }
23209 }
23210 msg_puts((char_u *)")");
23211 }
23212 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023213
23214 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215 --no_wait_return;
23216 }
23217 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023218#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023219 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023220 {
23221 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23222 func_do_profile(fp);
23223 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023224 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023225 {
23226 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023227 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023228 profile_zero(&fp->uf_tm_children);
23229 }
23230 script_prof_save(&wait_start);
23231 }
23232#endif
23233
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023235 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236 save_did_emsg = did_emsg;
23237 did_emsg = FALSE;
23238
23239 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023240 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023241 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23242
23243 --RedrawingDisabled;
23244
23245 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023246 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023248 clear_tv(rettv);
23249 rettv->v_type = VAR_NUMBER;
23250 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023251 }
23252
Bram Moolenaar05159a02005-02-26 23:04:13 +000023253#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023254 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023255 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023256 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023257 profile_end(&call_start);
23258 profile_sub_wait(&wait_start, &call_start);
23259 profile_add(&fp->uf_tm_total, &call_start);
23260 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023261 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023262 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023263 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23264 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023265 }
23266 }
23267#endif
23268
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269 /* when being verbose, mention the return value */
23270 if (p_verbose >= 12)
23271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023272 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023273 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023276 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023277 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023278 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023279 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023280 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023282 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023283 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023284 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023285 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023286
Bram Moolenaar555b2802005-05-19 21:08:39 +000023287 /* The value may be very long. Skip the middle part, so that we
23288 * have some idea how it starts and ends. smsg() would always
23289 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023290 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023291 if (s != NULL)
23292 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023293 if (vim_strsize(s) > MSG_BUF_CLEN)
23294 {
23295 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23296 s = buf;
23297 }
23298 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023299 vim_free(tofree);
23300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301 }
23302 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023303
23304 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 --no_wait_return;
23306 }
23307
23308 vim_free(sourcing_name);
23309 sourcing_name = save_sourcing_name;
23310 sourcing_lnum = save_sourcing_lnum;
23311 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023312#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023313 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023314 script_prof_restore(&wait_start);
23315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316
23317 if (p_verbose >= 12 && sourcing_name != NULL)
23318 {
23319 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023320 verbose_enter_scroll();
23321
Bram Moolenaar555b2802005-05-19 21:08:39 +000023322 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023324
23325 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326 --no_wait_return;
23327 }
23328
23329 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023330 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023331 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023332
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023333 /* If the a:000 list and the l: and a: dicts are not referenced we can
23334 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023335 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23336 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23337 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23338 {
23339 free_funccal(fc, FALSE);
23340 }
23341 else
23342 {
23343 hashitem_T *hi;
23344 listitem_T *li;
23345 int todo;
23346
23347 /* "fc" is still in use. This can happen when returning "a:000" or
23348 * assigning "l:" to a global variable.
23349 * Link "fc" in the list for garbage collection later. */
23350 fc->caller = previous_funccal;
23351 previous_funccal = fc;
23352
23353 /* Make a copy of the a: variables, since we didn't do that above. */
23354 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23355 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23356 {
23357 if (!HASHITEM_EMPTY(hi))
23358 {
23359 --todo;
23360 v = HI2DI(hi);
23361 copy_tv(&v->di_tv, &v->di_tv);
23362 }
23363 }
23364
23365 /* Make a copy of the a:000 items, since we didn't do that above. */
23366 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23367 copy_tv(&li->li_tv, &li->li_tv);
23368 }
23369}
23370
23371/*
23372 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023373 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023374 */
23375 static int
23376can_free_funccal(fc, copyID)
23377 funccall_T *fc;
23378 int copyID;
23379{
23380 return (fc->l_varlist.lv_copyID != copyID
23381 && fc->l_vars.dv_copyID != copyID
23382 && fc->l_avars.dv_copyID != copyID);
23383}
23384
23385/*
23386 * Free "fc" and what it contains.
23387 */
23388 static void
23389free_funccal(fc, free_val)
23390 funccall_T *fc;
23391 int free_val; /* a: vars were allocated */
23392{
23393 listitem_T *li;
23394
23395 /* The a: variables typevals may not have been allocated, only free the
23396 * allocated variables. */
23397 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23398
23399 /* free all l: variables */
23400 vars_clear(&fc->l_vars.dv_hashtab);
23401
23402 /* Free the a:000 variables if they were allocated. */
23403 if (free_val)
23404 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23405 clear_tv(&li->li_tv);
23406
23407 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408}
23409
23410/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023411 * Add a number variable "name" to dict "dp" with value "nr".
23412 */
23413 static void
23414add_nr_var(dp, v, name, nr)
23415 dict_T *dp;
23416 dictitem_T *v;
23417 char *name;
23418 varnumber_T nr;
23419{
23420 STRCPY(v->di_key, name);
23421 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23422 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23423 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023424 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023425 v->di_tv.vval.v_number = nr;
23426}
23427
23428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023429 * ":return [expr]"
23430 */
23431 void
23432ex_return(eap)
23433 exarg_T *eap;
23434{
23435 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023436 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437 int returning = FALSE;
23438
23439 if (current_funccal == NULL)
23440 {
23441 EMSG(_("E133: :return not inside a function"));
23442 return;
23443 }
23444
23445 if (eap->skip)
23446 ++emsg_skip;
23447
23448 eap->nextcmd = NULL;
23449 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023450 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023451 {
23452 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023453 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023454 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023455 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023456 }
23457 /* It's safer to return also on error. */
23458 else if (!eap->skip)
23459 {
23460 /*
23461 * Return unless the expression evaluation has been cancelled due to an
23462 * aborting error, an interrupt, or an exception.
23463 */
23464 if (!aborting())
23465 returning = do_return(eap, FALSE, TRUE, NULL);
23466 }
23467
23468 /* When skipping or the return gets pending, advance to the next command
23469 * in this line (!returning). Otherwise, ignore the rest of the line.
23470 * Following lines will be ignored by get_func_line(). */
23471 if (returning)
23472 eap->nextcmd = NULL;
23473 else if (eap->nextcmd == NULL) /* no argument */
23474 eap->nextcmd = check_nextcmd(arg);
23475
23476 if (eap->skip)
23477 --emsg_skip;
23478}
23479
23480/*
23481 * Return from a function. Possibly makes the return pending. Also called
23482 * for a pending return at the ":endtry" or after returning from an extra
23483 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023484 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023485 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486 * FALSE when the return gets pending.
23487 */
23488 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023489do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490 exarg_T *eap;
23491 int reanimate;
23492 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023493 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494{
23495 int idx;
23496 struct condstack *cstack = eap->cstack;
23497
23498 if (reanimate)
23499 /* Undo the return. */
23500 current_funccal->returned = FALSE;
23501
23502 /*
23503 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23504 * not in its finally clause (which then is to be executed next) is found.
23505 * In this case, make the ":return" pending for execution at the ":endtry".
23506 * Otherwise, return normally.
23507 */
23508 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23509 if (idx >= 0)
23510 {
23511 cstack->cs_pending[idx] = CSTP_RETURN;
23512
23513 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023514 /* A pending return again gets pending. "rettv" points to an
23515 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023517 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518 else
23519 {
23520 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023521 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023523 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023525 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526 {
23527 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023528 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023529 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 else
23531 EMSG(_(e_outofmem));
23532 }
23533 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023534 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535
23536 if (reanimate)
23537 {
23538 /* The pending return value could be overwritten by a ":return"
23539 * without argument in a finally clause; reset the default
23540 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023541 current_funccal->rettv->v_type = VAR_NUMBER;
23542 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543 }
23544 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023545 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 }
23547 else
23548 {
23549 current_funccal->returned = TRUE;
23550
23551 /* If the return is carried out now, store the return value. For
23552 * a return immediately after reanimation, the value is already
23553 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023554 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023556 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023557 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023559 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023560 }
23561 }
23562
23563 return idx < 0;
23564}
23565
23566/*
23567 * Free the variable with a pending return value.
23568 */
23569 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023570discard_pending_return(rettv)
23571 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572{
Bram Moolenaar33570922005-01-25 22:26:29 +000023573 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023574}
23575
23576/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023577 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 * is an allocated string. Used by report_pending() for verbose messages.
23579 */
23580 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023581get_return_cmd(rettv)
23582 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023584 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023585 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023586 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023588 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023589 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023590 if (s == NULL)
23591 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023592
23593 STRCPY(IObuff, ":return ");
23594 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23595 if (STRLEN(s) + 8 >= IOSIZE)
23596 STRCPY(IObuff + IOSIZE - 4, "...");
23597 vim_free(tofree);
23598 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599}
23600
23601/*
23602 * Get next function line.
23603 * Called by do_cmdline() to get the next line.
23604 * Returns allocated string, or NULL for end of function.
23605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606 char_u *
23607get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023608 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023609 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023610 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611{
Bram Moolenaar33570922005-01-25 22:26:29 +000023612 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023613 ufunc_T *fp = fcp->func;
23614 char_u *retval;
23615 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616
23617 /* If breakpoints have been added/deleted need to check for it. */
23618 if (fcp->dbg_tick != debug_tick)
23619 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023620 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621 sourcing_lnum);
23622 fcp->dbg_tick = debug_tick;
23623 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023624#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023625 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023626 func_line_end(cookie);
23627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628
Bram Moolenaar05159a02005-02-26 23:04:13 +000023629 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023630 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23631 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632 retval = NULL;
23633 else
23634 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023635 /* Skip NULL lines (continuation lines). */
23636 while (fcp->linenr < gap->ga_len
23637 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23638 ++fcp->linenr;
23639 if (fcp->linenr >= gap->ga_len)
23640 retval = NULL;
23641 else
23642 {
23643 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23644 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023645#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023646 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023647 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023648#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650 }
23651
23652 /* Did we encounter a breakpoint? */
23653 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23654 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023655 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023657 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 sourcing_lnum);
23659 fcp->dbg_tick = debug_tick;
23660 }
23661
23662 return retval;
23663}
23664
Bram Moolenaar05159a02005-02-26 23:04:13 +000023665#if defined(FEAT_PROFILE) || defined(PROTO)
23666/*
23667 * Called when starting to read a function line.
23668 * "sourcing_lnum" must be correct!
23669 * When skipping lines it may not actually be executed, but we won't find out
23670 * until later and we need to store the time now.
23671 */
23672 void
23673func_line_start(cookie)
23674 void *cookie;
23675{
23676 funccall_T *fcp = (funccall_T *)cookie;
23677 ufunc_T *fp = fcp->func;
23678
23679 if (fp->uf_profiling && sourcing_lnum >= 1
23680 && sourcing_lnum <= fp->uf_lines.ga_len)
23681 {
23682 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023683 /* Skip continuation lines. */
23684 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23685 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023686 fp->uf_tml_execed = FALSE;
23687 profile_start(&fp->uf_tml_start);
23688 profile_zero(&fp->uf_tml_children);
23689 profile_get_wait(&fp->uf_tml_wait);
23690 }
23691}
23692
23693/*
23694 * Called when actually executing a function line.
23695 */
23696 void
23697func_line_exec(cookie)
23698 void *cookie;
23699{
23700 funccall_T *fcp = (funccall_T *)cookie;
23701 ufunc_T *fp = fcp->func;
23702
23703 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23704 fp->uf_tml_execed = TRUE;
23705}
23706
23707/*
23708 * Called when done with a function line.
23709 */
23710 void
23711func_line_end(cookie)
23712 void *cookie;
23713{
23714 funccall_T *fcp = (funccall_T *)cookie;
23715 ufunc_T *fp = fcp->func;
23716
23717 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23718 {
23719 if (fp->uf_tml_execed)
23720 {
23721 ++fp->uf_tml_count[fp->uf_tml_idx];
23722 profile_end(&fp->uf_tml_start);
23723 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023724 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023725 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23726 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023727 }
23728 fp->uf_tml_idx = -1;
23729 }
23730}
23731#endif
23732
Bram Moolenaar071d4272004-06-13 20:20:40 +000023733/*
23734 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023735 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023736 */
23737 int
23738func_has_ended(cookie)
23739 void *cookie;
23740{
Bram Moolenaar33570922005-01-25 22:26:29 +000023741 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023742
23743 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23744 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023745 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023746 || fcp->returned);
23747}
23748
23749/*
23750 * return TRUE if cookie indicates a function which "abort"s on errors.
23751 */
23752 int
23753func_has_abort(cookie)
23754 void *cookie;
23755{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023756 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757}
23758
23759#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23760typedef enum
23761{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023762 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23763 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23764 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765} var_flavour_T;
23766
23767static var_flavour_T var_flavour __ARGS((char_u *varname));
23768
23769 static var_flavour_T
23770var_flavour(varname)
23771 char_u *varname;
23772{
23773 char_u *p = varname;
23774
23775 if (ASCII_ISUPPER(*p))
23776 {
23777 while (*(++p))
23778 if (ASCII_ISLOWER(*p))
23779 return VAR_FLAVOUR_SESSION;
23780 return VAR_FLAVOUR_VIMINFO;
23781 }
23782 else
23783 return VAR_FLAVOUR_DEFAULT;
23784}
23785#endif
23786
23787#if defined(FEAT_VIMINFO) || defined(PROTO)
23788/*
23789 * Restore global vars that start with a capital from the viminfo file
23790 */
23791 int
23792read_viminfo_varlist(virp, writing)
23793 vir_T *virp;
23794 int writing;
23795{
23796 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023797 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023798 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023799
23800 if (!writing && (find_viminfo_parameter('!') != NULL))
23801 {
23802 tab = vim_strchr(virp->vir_line + 1, '\t');
23803 if (tab != NULL)
23804 {
23805 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023806 switch (*tab)
23807 {
23808 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023809#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023810 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023811#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023812 case 'D': type = VAR_DICT; break;
23813 case 'L': type = VAR_LIST; break;
23814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023815
23816 tab = vim_strchr(tab, '\t');
23817 if (tab != NULL)
23818 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023819 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023820 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023821 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023823#ifdef FEAT_FLOAT
23824 else if (type == VAR_FLOAT)
23825 (void)string2float(tab + 1, &tv.vval.v_float);
23826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023828 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023829 if (type == VAR_DICT || type == VAR_LIST)
23830 {
23831 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23832
23833 if (etv == NULL)
23834 /* Failed to parse back the dict or list, use it as a
23835 * string. */
23836 tv.v_type = VAR_STRING;
23837 else
23838 {
23839 vim_free(tv.vval.v_string);
23840 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023841 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023842 }
23843 }
23844
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023845 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023846
23847 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023848 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023849 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23850 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851 }
23852 }
23853 }
23854
23855 return viminfo_readline(virp);
23856}
23857
23858/*
23859 * Write global vars that start with a capital to the viminfo file
23860 */
23861 void
23862write_viminfo_varlist(fp)
23863 FILE *fp;
23864{
Bram Moolenaar33570922005-01-25 22:26:29 +000023865 hashitem_T *hi;
23866 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023867 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023868 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023869 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023870 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023871 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872
23873 if (find_viminfo_parameter('!') == NULL)
23874 return;
23875
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023876 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023877
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023878 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023879 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023880 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023881 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023883 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023884 this_var = HI2DI(hi);
23885 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023886 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023887 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023888 {
23889 case VAR_STRING: s = "STR"; break;
23890 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023891#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023892 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023893#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023894 case VAR_DICT: s = "DIC"; break;
23895 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023896 default: continue;
23897 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023898 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023899 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023900 if (p != NULL)
23901 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023902 vim_free(tofree);
23903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904 }
23905 }
23906}
23907#endif
23908
23909#if defined(FEAT_SESSION) || defined(PROTO)
23910 int
23911store_session_globals(fd)
23912 FILE *fd;
23913{
Bram Moolenaar33570922005-01-25 22:26:29 +000023914 hashitem_T *hi;
23915 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023916 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917 char_u *p, *t;
23918
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023919 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023920 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023921 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023922 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023924 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023925 this_var = HI2DI(hi);
23926 if ((this_var->di_tv.v_type == VAR_NUMBER
23927 || this_var->di_tv.v_type == VAR_STRING)
23928 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023929 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023930 /* Escape special characters with a backslash. Turn a LF and
23931 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023932 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023933 (char_u *)"\\\"\n\r");
23934 if (p == NULL) /* out of memory */
23935 break;
23936 for (t = p; *t != NUL; ++t)
23937 if (*t == '\n')
23938 *t = 'n';
23939 else if (*t == '\r')
23940 *t = 'r';
23941 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023942 this_var->di_key,
23943 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23944 : ' ',
23945 p,
23946 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23947 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023948 || put_eol(fd) == FAIL)
23949 {
23950 vim_free(p);
23951 return FAIL;
23952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023953 vim_free(p);
23954 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023955#ifdef FEAT_FLOAT
23956 else if (this_var->di_tv.v_type == VAR_FLOAT
23957 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23958 {
23959 float_T f = this_var->di_tv.vval.v_float;
23960 int sign = ' ';
23961
23962 if (f < 0)
23963 {
23964 f = -f;
23965 sign = '-';
23966 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023967 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023968 this_var->di_key, sign, f) < 0)
23969 || put_eol(fd) == FAIL)
23970 return FAIL;
23971 }
23972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 }
23974 }
23975 return OK;
23976}
23977#endif
23978
Bram Moolenaar661b1822005-07-28 22:36:45 +000023979/*
23980 * Display script name where an item was last set.
23981 * Should only be invoked when 'verbose' is non-zero.
23982 */
23983 void
23984last_set_msg(scriptID)
23985 scid_T scriptID;
23986{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023987 char_u *p;
23988
Bram Moolenaar661b1822005-07-28 22:36:45 +000023989 if (scriptID != 0)
23990 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023991 p = home_replace_save(NULL, get_scriptname(scriptID));
23992 if (p != NULL)
23993 {
23994 verbose_enter();
23995 MSG_PUTS(_("\n\tLast set from "));
23996 MSG_PUTS(p);
23997 vim_free(p);
23998 verbose_leave();
23999 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024000 }
24001}
24002
Bram Moolenaard812df62008-11-09 12:46:09 +000024003/*
24004 * List v:oldfiles in a nice way.
24005 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024006 void
24007ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024008 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024009{
24010 list_T *l = vimvars[VV_OLDFILES].vv_list;
24011 listitem_T *li;
24012 int nr = 0;
24013
24014 if (l == NULL)
24015 msg((char_u *)_("No old files"));
24016 else
24017 {
24018 msg_start();
24019 msg_scroll = TRUE;
24020 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24021 {
24022 msg_outnum((long)++nr);
24023 MSG_PUTS(": ");
24024 msg_outtrans(get_tv_string(&li->li_tv));
24025 msg_putchar('\n');
24026 out_flush(); /* output one line at a time */
24027 ui_breakcheck();
24028 }
24029 /* Assume "got_int" was set to truncate the listing. */
24030 got_int = FALSE;
24031
24032#ifdef FEAT_BROWSE_CMD
24033 if (cmdmod.browse)
24034 {
24035 quit_more = FALSE;
24036 nr = prompt_for_number(FALSE);
24037 msg_starthere();
24038 if (nr > 0)
24039 {
24040 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24041 (long)nr);
24042
24043 if (p != NULL)
24044 {
24045 p = expand_env_save(p);
24046 eap->arg = p;
24047 eap->cmdidx = CMD_edit;
24048 cmdmod.browse = FALSE;
24049 do_exedit(eap, NULL);
24050 vim_free(p);
24051 }
24052 }
24053 }
24054#endif
24055 }
24056}
24057
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058#endif /* FEAT_EVAL */
24059
Bram Moolenaar071d4272004-06-13 20:20:40 +000024060
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024061#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024062
24063#ifdef WIN3264
24064/*
24065 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24066 */
24067static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24068static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24069static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24070
24071/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024072 * Get the short path (8.3) for the filename in "fnamep".
24073 * Only works for a valid file name.
24074 * When the path gets longer "fnamep" is changed and the allocated buffer
24075 * is put in "bufp".
24076 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24077 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 */
24079 static int
24080get_short_pathname(fnamep, bufp, fnamelen)
24081 char_u **fnamep;
24082 char_u **bufp;
24083 int *fnamelen;
24084{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024085 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024086 char_u *newbuf;
24087
24088 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024089 l = GetShortPathName(*fnamep, *fnamep, len);
24090 if (l > len - 1)
24091 {
24092 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024093 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 newbuf = vim_strnsave(*fnamep, l);
24095 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024096 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097
24098 vim_free(*bufp);
24099 *fnamep = *bufp = newbuf;
24100
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024101 /* Really should always succeed, as the buffer is big enough. */
24102 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103 }
24104
24105 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024106 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107}
24108
24109/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024110 * Get the short path (8.3) for the filename in "fname". The converted
24111 * path is returned in "bufp".
24112 *
24113 * Some of the directories specified in "fname" may not exist. This function
24114 * will shorten the existing directories at the beginning of the path and then
24115 * append the remaining non-existing path.
24116 *
24117 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024118 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024119 * bufp - Pointer to an allocated buffer for the filename.
24120 * fnamelen - Length of the filename pointed to by fname
24121 *
24122 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024123 */
24124 static int
24125shortpath_for_invalid_fname(fname, bufp, fnamelen)
24126 char_u **fname;
24127 char_u **bufp;
24128 int *fnamelen;
24129{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024130 char_u *short_fname, *save_fname, *pbuf_unused;
24131 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024132 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024133 int old_len, len;
24134 int new_len, sfx_len;
24135 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136
24137 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024138 old_len = *fnamelen;
24139 save_fname = vim_strnsave(*fname, old_len);
24140 pbuf_unused = NULL;
24141 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024143 endp = save_fname + old_len - 1; /* Find the end of the copy */
24144 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024146 /*
24147 * Try shortening the supplied path till it succeeds by removing one
24148 * directory at a time from the tail of the path.
24149 */
24150 len = 0;
24151 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024152 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024153 /* go back one path-separator */
24154 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24155 --endp;
24156 if (endp <= save_fname)
24157 break; /* processed the complete path */
24158
24159 /*
24160 * Replace the path separator with a NUL and try to shorten the
24161 * resulting path.
24162 */
24163 ch = *endp;
24164 *endp = 0;
24165 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024166 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024167 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24168 {
24169 retval = FAIL;
24170 goto theend;
24171 }
24172 *endp = ch; /* preserve the string */
24173
24174 if (len > 0)
24175 break; /* successfully shortened the path */
24176
24177 /* failed to shorten the path. Skip the path separator */
24178 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024179 }
24180
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024181 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024182 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024183 /*
24184 * Succeeded in shortening the path. Now concatenate the shortened
24185 * path with the remaining path at the tail.
24186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024188 /* Compute the length of the new path. */
24189 sfx_len = (int)(save_endp - endp) + 1;
24190 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024192 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024193 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024194 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024196 /* There is not enough space in the currently allocated string,
24197 * copy it to a buffer big enough. */
24198 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024199 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024200 {
24201 retval = FAIL;
24202 goto theend;
24203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024204 }
24205 else
24206 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024207 /* Transfer short_fname to the main buffer (it's big enough),
24208 * unless get_short_pathname() did its work in-place. */
24209 *fname = *bufp = save_fname;
24210 if (short_fname != save_fname)
24211 vim_strncpy(save_fname, short_fname, len);
24212 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024213 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024214
24215 /* concat the not-shortened part of the path */
24216 vim_strncpy(*fname + len, endp, sfx_len);
24217 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024219
24220theend:
24221 vim_free(pbuf_unused);
24222 vim_free(save_fname);
24223
24224 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225}
24226
24227/*
24228 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024229 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024230 */
24231 static int
24232shortpath_for_partial(fnamep, bufp, fnamelen)
24233 char_u **fnamep;
24234 char_u **bufp;
24235 int *fnamelen;
24236{
24237 int sepcount, len, tflen;
24238 char_u *p;
24239 char_u *pbuf, *tfname;
24240 int hasTilde;
24241
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024242 /* Count up the path separators from the RHS.. so we know which part
24243 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024244 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024245 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246 if (vim_ispathsep(*p))
24247 ++sepcount;
24248
24249 /* Need full path first (use expand_env() to remove a "~/") */
24250 hasTilde = (**fnamep == '~');
24251 if (hasTilde)
24252 pbuf = tfname = expand_env_save(*fnamep);
24253 else
24254 pbuf = tfname = FullName_save(*fnamep, FALSE);
24255
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024256 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024257
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024258 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024260
24261 if (len == 0)
24262 {
24263 /* Don't have a valid filename, so shorten the rest of the
24264 * path if we can. This CAN give us invalid 8.3 filenames, but
24265 * there's not a lot of point in guessing what it might be.
24266 */
24267 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024268 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024270 }
24271
24272 /* Count the paths backward to find the beginning of the desired string. */
24273 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024274 {
24275#ifdef FEAT_MBYTE
24276 if (has_mbyte)
24277 p -= mb_head_off(tfname, p);
24278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 if (vim_ispathsep(*p))
24280 {
24281 if (sepcount == 0 || (hasTilde && sepcount == 1))
24282 break;
24283 else
24284 sepcount --;
24285 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024287 if (hasTilde)
24288 {
24289 --p;
24290 if (p >= tfname)
24291 *p = '~';
24292 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024293 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024294 }
24295 else
24296 ++p;
24297
24298 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24299 vim_free(*bufp);
24300 *fnamelen = (int)STRLEN(p);
24301 *bufp = pbuf;
24302 *fnamep = p;
24303
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024304 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024305}
24306#endif /* WIN3264 */
24307
24308/*
24309 * Adjust a filename, according to a string of modifiers.
24310 * *fnamep must be NUL terminated when called. When returning, the length is
24311 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024312 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024313 * When there is an error, *fnamep is set to NULL.
24314 */
24315 int
24316modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24317 char_u *src; /* string with modifiers */
24318 int *usedlen; /* characters after src that are used */
24319 char_u **fnamep; /* file name so far */
24320 char_u **bufp; /* buffer for allocated file name or NULL */
24321 int *fnamelen; /* length of fnamep */
24322{
24323 int valid = 0;
24324 char_u *tail;
24325 char_u *s, *p, *pbuf;
24326 char_u dirname[MAXPATHL];
24327 int c;
24328 int has_fullname = 0;
24329#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024330 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331 int has_shortname = 0;
24332#endif
24333
24334repeat:
24335 /* ":p" - full path/file_name */
24336 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24337 {
24338 has_fullname = 1;
24339
24340 valid |= VALID_PATH;
24341 *usedlen += 2;
24342
24343 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24344 if ((*fnamep)[0] == '~'
24345#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24346 && ((*fnamep)[1] == '/'
24347# ifdef BACKSLASH_IN_FILENAME
24348 || (*fnamep)[1] == '\\'
24349# endif
24350 || (*fnamep)[1] == NUL)
24351
24352#endif
24353 )
24354 {
24355 *fnamep = expand_env_save(*fnamep);
24356 vim_free(*bufp); /* free any allocated file name */
24357 *bufp = *fnamep;
24358 if (*fnamep == NULL)
24359 return -1;
24360 }
24361
24362 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024363 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024364 {
24365 if (vim_ispathsep(*p)
24366 && p[1] == '.'
24367 && (p[2] == NUL
24368 || vim_ispathsep(p[2])
24369 || (p[2] == '.'
24370 && (p[3] == NUL || vim_ispathsep(p[3])))))
24371 break;
24372 }
24373
24374 /* FullName_save() is slow, don't use it when not needed. */
24375 if (*p != NUL || !vim_isAbsName(*fnamep))
24376 {
24377 *fnamep = FullName_save(*fnamep, *p != NUL);
24378 vim_free(*bufp); /* free any allocated file name */
24379 *bufp = *fnamep;
24380 if (*fnamep == NULL)
24381 return -1;
24382 }
24383
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024384#ifdef WIN3264
24385# if _WIN32_WINNT >= 0x0500
24386 if (vim_strchr(*fnamep, '~') != NULL)
24387 {
24388 /* Expand 8.3 filename to full path. Needed to make sure the same
24389 * file does not have two different names.
24390 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24391 p = alloc(_MAX_PATH + 1);
24392 if (p != NULL)
24393 {
24394 if (GetLongPathName(*fnamep, p, MAXPATHL))
24395 {
24396 vim_free(*bufp);
24397 *bufp = *fnamep = p;
24398 }
24399 else
24400 vim_free(p);
24401 }
24402 }
24403# endif
24404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024405 /* Append a path separator to a directory. */
24406 if (mch_isdir(*fnamep))
24407 {
24408 /* Make room for one or two extra characters. */
24409 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24410 vim_free(*bufp); /* free any allocated file name */
24411 *bufp = *fnamep;
24412 if (*fnamep == NULL)
24413 return -1;
24414 add_pathsep(*fnamep);
24415 }
24416 }
24417
24418 /* ":." - path relative to the current directory */
24419 /* ":~" - path relative to the home directory */
24420 /* ":8" - shortname path - postponed till after */
24421 while (src[*usedlen] == ':'
24422 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24423 {
24424 *usedlen += 2;
24425 if (c == '8')
24426 {
24427#ifdef WIN3264
24428 has_shortname = 1; /* Postpone this. */
24429#endif
24430 continue;
24431 }
24432 pbuf = NULL;
24433 /* Need full path first (use expand_env() to remove a "~/") */
24434 if (!has_fullname)
24435 {
24436 if (c == '.' && **fnamep == '~')
24437 p = pbuf = expand_env_save(*fnamep);
24438 else
24439 p = pbuf = FullName_save(*fnamep, FALSE);
24440 }
24441 else
24442 p = *fnamep;
24443
24444 has_fullname = 0;
24445
24446 if (p != NULL)
24447 {
24448 if (c == '.')
24449 {
24450 mch_dirname(dirname, MAXPATHL);
24451 s = shorten_fname(p, dirname);
24452 if (s != NULL)
24453 {
24454 *fnamep = s;
24455 if (pbuf != NULL)
24456 {
24457 vim_free(*bufp); /* free any allocated file name */
24458 *bufp = pbuf;
24459 pbuf = NULL;
24460 }
24461 }
24462 }
24463 else
24464 {
24465 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24466 /* Only replace it when it starts with '~' */
24467 if (*dirname == '~')
24468 {
24469 s = vim_strsave(dirname);
24470 if (s != NULL)
24471 {
24472 *fnamep = s;
24473 vim_free(*bufp);
24474 *bufp = s;
24475 }
24476 }
24477 }
24478 vim_free(pbuf);
24479 }
24480 }
24481
24482 tail = gettail(*fnamep);
24483 *fnamelen = (int)STRLEN(*fnamep);
24484
24485 /* ":h" - head, remove "/file_name", can be repeated */
24486 /* Don't remove the first "/" or "c:\" */
24487 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24488 {
24489 valid |= VALID_HEAD;
24490 *usedlen += 2;
24491 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024492 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024493 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024494 *fnamelen = (int)(tail - *fnamep);
24495#ifdef VMS
24496 if (*fnamelen > 0)
24497 *fnamelen += 1; /* the path separator is part of the path */
24498#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024499 if (*fnamelen == 0)
24500 {
24501 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24502 p = vim_strsave((char_u *)".");
24503 if (p == NULL)
24504 return -1;
24505 vim_free(*bufp);
24506 *bufp = *fnamep = tail = p;
24507 *fnamelen = 1;
24508 }
24509 else
24510 {
24511 while (tail > s && !after_pathsep(s, tail))
24512 mb_ptr_back(*fnamep, tail);
24513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024514 }
24515
24516 /* ":8" - shortname */
24517 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24518 {
24519 *usedlen += 2;
24520#ifdef WIN3264
24521 has_shortname = 1;
24522#endif
24523 }
24524
24525#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024526 /*
24527 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024528 */
24529 if (has_shortname)
24530 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024531 /* Copy the string if it is shortened by :h and when it wasn't copied
24532 * yet, because we are going to change it in place. Avoids changing
24533 * the buffer name for "%:8". */
24534 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024535 {
24536 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024537 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 return -1;
24539 vim_free(*bufp);
24540 *bufp = *fnamep = p;
24541 }
24542
24543 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024544 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024545 if (!has_fullname && !vim_isAbsName(*fnamep))
24546 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024547 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024548 return -1;
24549 }
24550 else
24551 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024552 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024553
Bram Moolenaardc935552011-08-17 15:23:23 +020024554 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024555 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024556 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024557 return -1;
24558
24559 if (l == 0)
24560 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024561 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024562 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024563 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024564 return -1;
24565 }
24566 *fnamelen = l;
24567 }
24568 }
24569#endif /* WIN3264 */
24570
24571 /* ":t" - tail, just the basename */
24572 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24573 {
24574 *usedlen += 2;
24575 *fnamelen -= (int)(tail - *fnamep);
24576 *fnamep = tail;
24577 }
24578
24579 /* ":e" - extension, can be repeated */
24580 /* ":r" - root, without extension, can be repeated */
24581 while (src[*usedlen] == ':'
24582 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24583 {
24584 /* find a '.' in the tail:
24585 * - for second :e: before the current fname
24586 * - otherwise: The last '.'
24587 */
24588 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24589 s = *fnamep - 2;
24590 else
24591 s = *fnamep + *fnamelen - 1;
24592 for ( ; s > tail; --s)
24593 if (s[0] == '.')
24594 break;
24595 if (src[*usedlen + 1] == 'e') /* :e */
24596 {
24597 if (s > tail)
24598 {
24599 *fnamelen += (int)(*fnamep - (s + 1));
24600 *fnamep = s + 1;
24601#ifdef VMS
24602 /* cut version from the extension */
24603 s = *fnamep + *fnamelen - 1;
24604 for ( ; s > *fnamep; --s)
24605 if (s[0] == ';')
24606 break;
24607 if (s > *fnamep)
24608 *fnamelen = s - *fnamep;
24609#endif
24610 }
24611 else if (*fnamep <= tail)
24612 *fnamelen = 0;
24613 }
24614 else /* :r */
24615 {
24616 if (s > tail) /* remove one extension */
24617 *fnamelen = (int)(s - *fnamep);
24618 }
24619 *usedlen += 2;
24620 }
24621
24622 /* ":s?pat?foo?" - substitute */
24623 /* ":gs?pat?foo?" - global substitute */
24624 if (src[*usedlen] == ':'
24625 && (src[*usedlen + 1] == 's'
24626 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24627 {
24628 char_u *str;
24629 char_u *pat;
24630 char_u *sub;
24631 int sep;
24632 char_u *flags;
24633 int didit = FALSE;
24634
24635 flags = (char_u *)"";
24636 s = src + *usedlen + 2;
24637 if (src[*usedlen + 1] == 'g')
24638 {
24639 flags = (char_u *)"g";
24640 ++s;
24641 }
24642
24643 sep = *s++;
24644 if (sep)
24645 {
24646 /* find end of pattern */
24647 p = vim_strchr(s, sep);
24648 if (p != NULL)
24649 {
24650 pat = vim_strnsave(s, (int)(p - s));
24651 if (pat != NULL)
24652 {
24653 s = p + 1;
24654 /* find end of substitution */
24655 p = vim_strchr(s, sep);
24656 if (p != NULL)
24657 {
24658 sub = vim_strnsave(s, (int)(p - s));
24659 str = vim_strnsave(*fnamep, *fnamelen);
24660 if (sub != NULL && str != NULL)
24661 {
24662 *usedlen = (int)(p + 1 - src);
24663 s = do_string_sub(str, pat, sub, flags);
24664 if (s != NULL)
24665 {
24666 *fnamep = s;
24667 *fnamelen = (int)STRLEN(s);
24668 vim_free(*bufp);
24669 *bufp = s;
24670 didit = TRUE;
24671 }
24672 }
24673 vim_free(sub);
24674 vim_free(str);
24675 }
24676 vim_free(pat);
24677 }
24678 }
24679 /* after using ":s", repeat all the modifiers */
24680 if (didit)
24681 goto repeat;
24682 }
24683 }
24684
Bram Moolenaar26df0922014-02-23 23:39:13 +010024685 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24686 {
24687 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24688 if (p == NULL)
24689 return -1;
24690 vim_free(*bufp);
24691 *bufp = *fnamep = p;
24692 *fnamelen = (int)STRLEN(p);
24693 *usedlen += 2;
24694 }
24695
Bram Moolenaar071d4272004-06-13 20:20:40 +000024696 return valid;
24697}
24698
24699/*
24700 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24701 * "flags" can be "g" to do a global substitute.
24702 * Returns an allocated string, NULL for error.
24703 */
24704 char_u *
24705do_string_sub(str, pat, sub, flags)
24706 char_u *str;
24707 char_u *pat;
24708 char_u *sub;
24709 char_u *flags;
24710{
24711 int sublen;
24712 regmatch_T regmatch;
24713 int i;
24714 int do_all;
24715 char_u *tail;
24716 garray_T ga;
24717 char_u *ret;
24718 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024719 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024720
24721 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24722 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024723 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024724
24725 ga_init2(&ga, 1, 200);
24726
24727 do_all = (flags[0] == 'g');
24728
24729 regmatch.rm_ic = p_ic;
24730 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24731 if (regmatch.regprog != NULL)
24732 {
24733 tail = str;
24734 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24735 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024736 /* Skip empty match except for first match. */
24737 if (regmatch.startp[0] == regmatch.endp[0])
24738 {
24739 if (zero_width == regmatch.startp[0])
24740 {
24741 /* avoid getting stuck on a match with an empty string */
24742 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24743 ++ga.ga_len;
24744 continue;
24745 }
24746 zero_width = regmatch.startp[0];
24747 }
24748
Bram Moolenaar071d4272004-06-13 20:20:40 +000024749 /*
24750 * Get some space for a temporary buffer to do the substitution
24751 * into. It will contain:
24752 * - The text up to where the match is.
24753 * - The substituted text.
24754 * - The text after the match.
24755 */
24756 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24757 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24758 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24759 {
24760 ga_clear(&ga);
24761 break;
24762 }
24763
24764 /* copy the text up to where the match is */
24765 i = (int)(regmatch.startp[0] - tail);
24766 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24767 /* add the substituted text */
24768 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24769 + ga.ga_len + i, TRUE, TRUE, FALSE);
24770 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024771 tail = regmatch.endp[0];
24772 if (*tail == NUL)
24773 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024774 if (!do_all)
24775 break;
24776 }
24777
24778 if (ga.ga_data != NULL)
24779 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24780
Bram Moolenaar473de612013-06-08 18:19:48 +020024781 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024782 }
24783
24784 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24785 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024786 if (p_cpo == empty_option)
24787 p_cpo = save_cpo;
24788 else
24789 /* Darn, evaluating {sub} expression changed the value. */
24790 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024791
24792 return ret;
24793}
24794
24795#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */