blob: c96a4e4e50d07acaed27baa5a42ce4b79bad07d5 [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.
7801 * Always return OK.
7802 */
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 {
7820 if (len != 0)
7821 {
7822 cc = name[len];
7823 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007824 /* first try vim_getenv(), fast for normal environment vars */
7825 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007827 {
7828 if (!mustfree)
7829 string = vim_strsave(string);
7830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 else
7832 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007833 if (mustfree)
7834 vim_free(string);
7835
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 /* next try expanding things like $VIM and ${HOME} */
7837 string = expand_env_save(name - 1);
7838 if (string != NULL && *string == '$')
7839 {
7840 vim_free(string);
7841 string = NULL;
7842 }
7843 }
7844 name[len] = cc;
7845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007846 rettv->v_type = VAR_STRING;
7847 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 }
7849
7850 return OK;
7851}
7852
7853/*
7854 * Array with names and number of arguments of all internal functions
7855 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7856 */
7857static struct fst
7858{
7859 char *f_name; /* function name */
7860 char f_min_argc; /* minimal number of arguments */
7861 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007863 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864} functions[] =
7865{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007866#ifdef FEAT_FLOAT
7867 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007868 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007869#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007870 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007871 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {"append", 2, 2, f_append},
7873 {"argc", 0, 0, f_argc},
7874 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007875 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007876#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007877 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007878 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007879 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007882 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {"bufexists", 1, 1, f_bufexists},
7884 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7885 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7886 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7887 {"buflisted", 1, 1, f_buflisted},
7888 {"bufloaded", 1, 1, f_bufloaded},
7889 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007890 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"bufwinnr", 1, 1, f_bufwinnr},
7892 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007893 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007894 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007895 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007896#ifdef FEAT_FLOAT
7897 {"ceil", 1, 1, f_ceil},
7898#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007899 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007900 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007902 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007904#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007905 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007906 {"complete_add", 1, 1, f_complete_add},
7907 {"complete_check", 0, 0, f_complete_check},
7908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007910 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007911#ifdef FEAT_FLOAT
7912 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007913 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007914#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007915 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007917 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007918 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {"delete", 1, 1, f_delete},
7920 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007921 {"diff_filler", 1, 1, f_diff_filler},
7922 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007923 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007925 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 {"eventhandler", 0, 0, f_eventhandler},
7927 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007928 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007930#ifdef FEAT_FLOAT
7931 {"exp", 1, 1, f_exp},
7932#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007933 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007934 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007935 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7937 {"filereadable", 1, 1, f_filereadable},
7938 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007939 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007940 {"finddir", 1, 3, f_finddir},
7941 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007942#ifdef FEAT_FLOAT
7943 {"float2nr", 1, 1, f_float2nr},
7944 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007945 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007946#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007947 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"fnamemodify", 2, 2, f_fnamemodify},
7949 {"foldclosed", 1, 1, f_foldclosed},
7950 {"foldclosedend", 1, 1, f_foldclosedend},
7951 {"foldlevel", 1, 1, f_foldlevel},
7952 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007953 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007955 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007956 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007957 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007958 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007959 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 {"getchar", 0, 1, f_getchar},
7961 {"getcharmod", 0, 0, f_getcharmod},
7962 {"getcmdline", 0, 0, f_getcmdline},
7963 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007964 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007966 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007967 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"getfsize", 1, 1, f_getfsize},
7969 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007970 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007971 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007972 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007973 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007974 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007975 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007976 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02007977 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007979 {"gettabvar", 2, 3, f_gettabvar},
7980 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {"getwinposx", 0, 0, f_getwinposx},
7982 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007983 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007984 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007985 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007987 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007988 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007989 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7991 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7992 {"histadd", 2, 2, f_histadd},
7993 {"histdel", 1, 2, f_histdel},
7994 {"histget", 1, 2, f_histget},
7995 {"histnr", 1, 1, f_histnr},
7996 {"hlID", 1, 1, f_hlID},
7997 {"hlexists", 1, 1, f_hlexists},
7998 {"hostname", 0, 0, f_hostname},
7999 {"iconv", 3, 3, f_iconv},
8000 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008001 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008002 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008004 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 {"inputrestore", 0, 0, f_inputrestore},
8006 {"inputsave", 0, 0, f_inputsave},
8007 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008008 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008009 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008011 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008012 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008013 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008014 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008016 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 {"libcall", 3, 3, f_libcall},
8018 {"libcallnr", 3, 3, f_libcallnr},
8019 {"line", 1, 1, f_line},
8020 {"line2byte", 1, 1, f_line2byte},
8021 {"lispindent", 1, 1, f_lispindent},
8022 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008023#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008024 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008025 {"log10", 1, 1, f_log10},
8026#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008027#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008028 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008029#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008030 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008031 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008032 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008033 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008034 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008035 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008036 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008037 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008038 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008039 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008040 {"max", 1, 1, f_max},
8041 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008042#ifdef vim_mkdir
8043 {"mkdir", 1, 3, f_mkdir},
8044#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008045 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008046#ifdef FEAT_MZSCHEME
8047 {"mzeval", 1, 1, f_mzeval},
8048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008050 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008051 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008052 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008053#ifdef FEAT_FLOAT
8054 {"pow", 2, 2, f_pow},
8055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008057 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008058 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008059#ifdef FEAT_PYTHON3
8060 {"py3eval", 1, 1, f_py3eval},
8061#endif
8062#ifdef FEAT_PYTHON
8063 {"pyeval", 1, 1, f_pyeval},
8064#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008065 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008066 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008067 {"reltime", 0, 2, f_reltime},
8068 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {"remote_expr", 2, 3, f_remote_expr},
8070 {"remote_foreground", 1, 1, f_remote_foreground},
8071 {"remote_peek", 1, 2, f_remote_peek},
8072 {"remote_read", 1, 1, f_remote_read},
8073 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008074 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008076 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008078 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079#ifdef FEAT_FLOAT
8080 {"round", 1, 1, f_round},
8081#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008082 {"screenattr", 2, 2, f_screenattr},
8083 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008084 {"screencol", 0, 0, f_screencol},
8085 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008086 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008087 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008088 {"searchpair", 3, 7, f_searchpair},
8089 {"searchpairpos", 3, 7, f_searchpairpos},
8090 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"server2client", 2, 2, f_server2client},
8092 {"serverlist", 0, 0, f_serverlist},
8093 {"setbufvar", 3, 3, f_setbufvar},
8094 {"setcmdpos", 1, 1, f_setcmdpos},
8095 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008096 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008097 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008098 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008099 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008101 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008102 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008104#ifdef FEAT_CRYPT
8105 {"sha256", 1, 1, f_sha256},
8106#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008107 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008108 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008110#ifdef FEAT_FLOAT
8111 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008112 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008113#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008114 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008115 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008116 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008117 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008118 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008119#ifdef FEAT_FLOAT
8120 {"sqrt", 1, 1, f_sqrt},
8121 {"str2float", 1, 1, f_str2float},
8122#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008123 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008124 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008125 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126#ifdef HAVE_STRFTIME
8127 {"strftime", 1, 2, f_strftime},
8128#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008129 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008130 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"strlen", 1, 1, f_strlen},
8132 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008133 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008135 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008136 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"substitute", 4, 4, f_substitute},
8138 {"synID", 3, 3, f_synID},
8139 {"synIDattr", 2, 3, f_synIDattr},
8140 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008141 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008142 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008143 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008144 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008145 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008146 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008147 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008148 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008149 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008150#ifdef FEAT_FLOAT
8151 {"tan", 1, 1, f_tan},
8152 {"tanh", 1, 1, f_tanh},
8153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008155 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {"tolower", 1, 1, f_tolower},
8157 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008158 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008159#ifdef FEAT_FLOAT
8160 {"trunc", 1, 1, f_trunc},
8161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008163 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008164 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008165 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008166 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {"virtcol", 1, 1, f_virtcol},
8168 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008169 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"winbufnr", 1, 1, f_winbufnr},
8171 {"wincol", 0, 0, f_wincol},
8172 {"winheight", 1, 1, f_winheight},
8173 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008174 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008176 {"winrestview", 1, 1, f_winrestview},
8177 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008179 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008180 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181};
8182
8183#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8184
8185/*
8186 * Function given to ExpandGeneric() to obtain the list of internal
8187 * or user defined function names.
8188 */
8189 char_u *
8190get_function_name(xp, idx)
8191 expand_T *xp;
8192 int idx;
8193{
8194 static int intidx = -1;
8195 char_u *name;
8196
8197 if (idx == 0)
8198 intidx = -1;
8199 if (intidx < 0)
8200 {
8201 name = get_user_func_name(xp, idx);
8202 if (name != NULL)
8203 return name;
8204 }
8205 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8206 {
8207 STRCPY(IObuff, functions[intidx].f_name);
8208 STRCAT(IObuff, "(");
8209 if (functions[intidx].f_max_argc == 0)
8210 STRCAT(IObuff, ")");
8211 return IObuff;
8212 }
8213
8214 return NULL;
8215}
8216
8217/*
8218 * Function given to ExpandGeneric() to obtain the list of internal or
8219 * user defined variable or function names.
8220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 char_u *
8222get_expr_name(xp, idx)
8223 expand_T *xp;
8224 int idx;
8225{
8226 static int intidx = -1;
8227 char_u *name;
8228
8229 if (idx == 0)
8230 intidx = -1;
8231 if (intidx < 0)
8232 {
8233 name = get_function_name(xp, idx);
8234 if (name != NULL)
8235 return name;
8236 }
8237 return get_user_var_name(xp, ++intidx);
8238}
8239
8240#endif /* FEAT_CMDL_COMPL */
8241
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008242#if defined(EBCDIC) || defined(PROTO)
8243/*
8244 * Compare struct fst by function name.
8245 */
8246 static int
8247compare_func_name(s1, s2)
8248 const void *s1;
8249 const void *s2;
8250{
8251 struct fst *p1 = (struct fst *)s1;
8252 struct fst *p2 = (struct fst *)s2;
8253
8254 return STRCMP(p1->f_name, p2->f_name);
8255}
8256
8257/*
8258 * Sort the function table by function name.
8259 * The sorting of the table above is ASCII dependant.
8260 * On machines using EBCDIC we have to sort it.
8261 */
8262 static void
8263sortFunctions()
8264{
8265 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8266
8267 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8268}
8269#endif
8270
8271
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272/*
8273 * Find internal function in table above.
8274 * Return index, or -1 if not found
8275 */
8276 static int
8277find_internal_func(name)
8278 char_u *name; /* name of the function */
8279{
8280 int first = 0;
8281 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8282 int cmp;
8283 int x;
8284
8285 /*
8286 * Find the function name in the table. Binary search.
8287 */
8288 while (first <= last)
8289 {
8290 x = first + ((unsigned)(last - first) >> 1);
8291 cmp = STRCMP(name, functions[x].f_name);
8292 if (cmp < 0)
8293 last = x - 1;
8294 else if (cmp > 0)
8295 first = x + 1;
8296 else
8297 return x;
8298 }
8299 return -1;
8300}
8301
8302/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008303 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8304 * name it contains, otherwise return "name".
8305 */
8306 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008307deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008308 char_u *name;
8309 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008310 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008311{
Bram Moolenaar33570922005-01-25 22:26:29 +00008312 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008313 int cc;
8314
8315 cc = name[*lenp];
8316 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008317 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008318 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008319 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008320 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008321 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008322 {
8323 *lenp = 0;
8324 return (char_u *)""; /* just in case */
8325 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008326 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008327 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008328 }
8329
8330 return name;
8331}
8332
8333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 * Allocate a variable for the result of a function.
8335 * Return OK or FAIL.
8336 */
8337 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008338get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8339 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 char_u *name; /* name of the function */
8341 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 char_u **arg; /* argument, pointing to the '(' */
8344 linenr_T firstline; /* first line of range */
8345 linenr_T lastline; /* last line of range */
8346 int *doesrange; /* return: function handled range */
8347 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008348 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
8350 char_u *argp;
8351 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008352 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 int argcount = 0; /* number of arguments found */
8354
8355 /*
8356 * Get the arguments.
8357 */
8358 argp = *arg;
8359 while (argcount < MAX_FUNC_ARGS)
8360 {
8361 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8362 if (*argp == ')' || *argp == ',' || *argp == NUL)
8363 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8365 {
8366 ret = FAIL;
8367 break;
8368 }
8369 ++argcount;
8370 if (*argp != ',')
8371 break;
8372 }
8373 if (*argp == ')')
8374 ++argp;
8375 else
8376 ret = FAIL;
8377
8378 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008379 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008380 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008382 {
8383 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008384 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008385 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008386 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388
8389 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008390 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
8392 *arg = skipwhite(argp);
8393 return ret;
8394}
8395
8396
8397/*
8398 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008399 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008400 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 */
8402 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008403call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008404 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008405 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008407 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008409 typval_T *argvars; /* vars for arguments, must have "argcount"
8410 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 linenr_T firstline; /* first line of range */
8412 linenr_T lastline; /* last line of range */
8413 int *doesrange; /* return: function handled range */
8414 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008415 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416{
8417 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418#define ERROR_UNKNOWN 0
8419#define ERROR_TOOMANY 1
8420#define ERROR_TOOFEW 2
8421#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008422#define ERROR_DICT 4
8423#define ERROR_NONE 5
8424#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 int error = ERROR_NONE;
8426 int i;
8427 int llen;
8428 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429#define FLEN_FIXED 40
8430 char_u fname_buf[FLEN_FIXED + 1];
8431 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008432 char_u *name;
8433
8434 /* Make a copy of the name, if it comes from a funcref variable it could
8435 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008436 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008437 if (name == NULL)
8438 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439
8440 /*
8441 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8442 * Change <SNR>123_name() to K_SNR 123_name().
8443 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 llen = eval_fname_script(name);
8446 if (llen > 0)
8447 {
8448 fname_buf[0] = K_SPECIAL;
8449 fname_buf[1] = KS_EXTRA;
8450 fname_buf[2] = (int)KE_SNR;
8451 i = 3;
8452 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8453 {
8454 if (current_SID <= 0)
8455 error = ERROR_SCRIPT;
8456 else
8457 {
8458 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8459 i = (int)STRLEN(fname_buf);
8460 }
8461 }
8462 if (i + STRLEN(name + llen) < FLEN_FIXED)
8463 {
8464 STRCPY(fname_buf + i, name + llen);
8465 fname = fname_buf;
8466 }
8467 else
8468 {
8469 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8470 if (fname == NULL)
8471 error = ERROR_OTHER;
8472 else
8473 {
8474 mch_memmove(fname, fname_buf, (size_t)i);
8475 STRCPY(fname + i, name + llen);
8476 }
8477 }
8478 }
8479 else
8480 fname = name;
8481
8482 *doesrange = FALSE;
8483
8484
8485 /* execute the function if no errors detected and executing */
8486 if (evaluate && error == ERROR_NONE)
8487 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008488 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8489 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 error = ERROR_UNKNOWN;
8491
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008492 if (!builtin_function(fname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {
8494 /*
8495 * User defined function.
8496 */
8497 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008498
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008500 /* Trigger FuncUndefined event, may load the function. */
8501 if (fp == NULL
8502 && apply_autocmds(EVENT_FUNCUNDEFINED,
8503 fname, fname, TRUE, NULL)
8504 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008506 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 fp = find_func(fname);
8508 }
8509#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008510 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008511 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008512 {
8513 /* loaded a package, search for the function again */
8514 fp = find_func(fname);
8515 }
8516
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 if (fp != NULL)
8518 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008519 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008521 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008523 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008525 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008526 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 else
8528 {
8529 /*
8530 * Call the user function.
8531 * Save and restore search patterns, script variables and
8532 * redo buffer.
8533 */
8534 save_search_patterns();
8535 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008536 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008537 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008538 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008539 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8540 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8541 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008542 /* Function was unreferenced while being used, free it
8543 * now. */
8544 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 restoreRedobuff();
8546 restore_search_patterns();
8547 error = ERROR_NONE;
8548 }
8549 }
8550 }
8551 else
8552 {
8553 /*
8554 * Find the function name in the table, call its implementation.
8555 */
8556 i = find_internal_func(fname);
8557 if (i >= 0)
8558 {
8559 if (argcount < functions[i].f_min_argc)
8560 error = ERROR_TOOFEW;
8561 else if (argcount > functions[i].f_max_argc)
8562 error = ERROR_TOOMANY;
8563 else
8564 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008565 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008566 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 error = ERROR_NONE;
8568 }
8569 }
8570 }
8571 /*
8572 * The function call (or "FuncUndefined" autocommand sequence) might
8573 * have been aborted by an error, an interrupt, or an explicitly thrown
8574 * exception that has not been caught so far. This situation can be
8575 * tested for by calling aborting(). For an error in an internal
8576 * function or for the "E132" error in call_user_func(), however, the
8577 * throw point at which the "force_abort" flag (temporarily reset by
8578 * emsg()) is normally updated has not been reached yet. We need to
8579 * update that flag first to make aborting() reliable.
8580 */
8581 update_force_abort();
8582 }
8583 if (error == ERROR_NONE)
8584 ret = OK;
8585
8586 /*
8587 * Report an error unless the argument evaluation or function call has been
8588 * cancelled due to an aborting error, an interrupt, or an exception.
8589 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008590 if (!aborting())
8591 {
8592 switch (error)
8593 {
8594 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008595 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008596 break;
8597 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008598 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008599 break;
8600 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008601 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008602 name);
8603 break;
8604 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008605 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008606 name);
8607 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008608 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008609 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008610 name);
8611 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008612 }
8613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 if (fname != name && fname != fname_buf)
8616 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008617 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618
8619 return ret;
8620}
8621
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008622/*
8623 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008624 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008625 */
8626 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008627emsg_funcname(ermsg, name)
8628 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008629 char_u *name;
8630{
8631 char_u *p;
8632
8633 if (*name == K_SPECIAL)
8634 p = concat_str((char_u *)"<SNR>", name + 3);
8635 else
8636 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008637 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008638 if (p != name)
8639 vim_free(p);
8640}
8641
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008642/*
8643 * Return TRUE for a non-zero Number and a non-empty String.
8644 */
8645 static int
8646non_zero_arg(argvars)
8647 typval_T *argvars;
8648{
8649 return ((argvars[0].v_type == VAR_NUMBER
8650 && argvars[0].vval.v_number != 0)
8651 || (argvars[0].v_type == VAR_STRING
8652 && argvars[0].vval.v_string != NULL
8653 && *argvars[0].vval.v_string != NUL));
8654}
8655
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656/*********************************************
8657 * Implementation of the built-in functions
8658 */
8659
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008660#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008661static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8662
8663/*
8664 * Get the float value of "argvars[0]" into "f".
8665 * Returns FAIL when the argument is not a Number or Float.
8666 */
8667 static int
8668get_float_arg(argvars, f)
8669 typval_T *argvars;
8670 float_T *f;
8671{
8672 if (argvars[0].v_type == VAR_FLOAT)
8673 {
8674 *f = argvars[0].vval.v_float;
8675 return OK;
8676 }
8677 if (argvars[0].v_type == VAR_NUMBER)
8678 {
8679 *f = (float_T)argvars[0].vval.v_number;
8680 return OK;
8681 }
8682 EMSG(_("E808: Number or Float required"));
8683 return FAIL;
8684}
8685
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008686/*
8687 * "abs(expr)" function
8688 */
8689 static void
8690f_abs(argvars, rettv)
8691 typval_T *argvars;
8692 typval_T *rettv;
8693{
8694 if (argvars[0].v_type == VAR_FLOAT)
8695 {
8696 rettv->v_type = VAR_FLOAT;
8697 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8698 }
8699 else
8700 {
8701 varnumber_T n;
8702 int error = FALSE;
8703
8704 n = get_tv_number_chk(&argvars[0], &error);
8705 if (error)
8706 rettv->vval.v_number = -1;
8707 else if (n > 0)
8708 rettv->vval.v_number = n;
8709 else
8710 rettv->vval.v_number = -n;
8711 }
8712}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008713
8714/*
8715 * "acos()" function
8716 */
8717 static void
8718f_acos(argvars, rettv)
8719 typval_T *argvars;
8720 typval_T *rettv;
8721{
8722 float_T f;
8723
8724 rettv->v_type = VAR_FLOAT;
8725 if (get_float_arg(argvars, &f) == OK)
8726 rettv->vval.v_float = acos(f);
8727 else
8728 rettv->vval.v_float = 0.0;
8729}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008730#endif
8731
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008733 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 */
8735 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008736f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008737 typval_T *argvars;
8738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739{
Bram Moolenaar33570922005-01-25 22:26:29 +00008740 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008743 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008745 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008746 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008747 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008748 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008749 }
8750 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008751 EMSG(_(e_listreq));
8752}
8753
8754/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008755 * "and(expr, expr)" function
8756 */
8757 static void
8758f_and(argvars, rettv)
8759 typval_T *argvars;
8760 typval_T *rettv;
8761{
8762 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8763 & get_tv_number_chk(&argvars[1], NULL);
8764}
8765
8766/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008767 * "append(lnum, string/list)" function
8768 */
8769 static void
8770f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *argvars;
8772 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008773{
8774 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008775 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008776 list_T *l = NULL;
8777 listitem_T *li = NULL;
8778 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008779 long added = 0;
8780
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008781 /* When coming here from Insert mode, sync undo, so that this can be
8782 * undone separately from what was previously inserted. */
8783 if (u_sync_once == 2)
8784 {
8785 u_sync_once = 1; /* notify that u_sync() was called */
8786 u_sync(TRUE);
8787 }
8788
Bram Moolenaar0d660222005-01-07 21:51:51 +00008789 lnum = get_tv_lnum(argvars);
8790 if (lnum >= 0
8791 && lnum <= curbuf->b_ml.ml_line_count
8792 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008793 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008794 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008795 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008796 l = argvars[1].vval.v_list;
8797 if (l == NULL)
8798 return;
8799 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008800 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008801 for (;;)
8802 {
8803 if (l == NULL)
8804 tv = &argvars[1]; /* append a string */
8805 else if (li == NULL)
8806 break; /* end of list */
8807 else
8808 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008809 line = get_tv_string_chk(tv);
8810 if (line == NULL) /* type error */
8811 {
8812 rettv->vval.v_number = 1; /* Failed */
8813 break;
8814 }
8815 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008816 ++added;
8817 if (l == NULL)
8818 break;
8819 li = li->li_next;
8820 }
8821
8822 appended_lines_mark(lnum, added);
8823 if (curwin->w_cursor.lnum > lnum)
8824 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008826 else
8827 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828}
8829
8830/*
8831 * "argc()" function
8832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008834f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008835 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839}
8840
8841/*
8842 * "argidx()" function
8843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008846 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008849 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850}
8851
8852/*
8853 * "argv(nr)" function
8854 */
8855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008857 typval_T *argvars;
8858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
8860 int idx;
8861
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008862 if (argvars[0].v_type != VAR_UNKNOWN)
8863 {
8864 idx = get_tv_number_chk(&argvars[0], NULL);
8865 if (idx >= 0 && idx < ARGCOUNT)
8866 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8867 else
8868 rettv->vval.v_string = NULL;
8869 rettv->v_type = VAR_STRING;
8870 }
8871 else if (rettv_list_alloc(rettv) == OK)
8872 for (idx = 0; idx < ARGCOUNT; ++idx)
8873 list_append_string(rettv->vval.v_list,
8874 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875}
8876
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008877#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008878/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008879 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008880 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008881 static void
8882f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008883 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008884 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008885{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008886 float_T f;
8887
8888 rettv->v_type = VAR_FLOAT;
8889 if (get_float_arg(argvars, &f) == OK)
8890 rettv->vval.v_float = asin(f);
8891 else
8892 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008893}
8894
8895/*
8896 * "atan()" function
8897 */
8898 static void
8899f_atan(argvars, rettv)
8900 typval_T *argvars;
8901 typval_T *rettv;
8902{
8903 float_T f;
8904
8905 rettv->v_type = VAR_FLOAT;
8906 if (get_float_arg(argvars, &f) == OK)
8907 rettv->vval.v_float = atan(f);
8908 else
8909 rettv->vval.v_float = 0.0;
8910}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008911
8912/*
8913 * "atan2()" function
8914 */
8915 static void
8916f_atan2(argvars, rettv)
8917 typval_T *argvars;
8918 typval_T *rettv;
8919{
8920 float_T fx, fy;
8921
8922 rettv->v_type = VAR_FLOAT;
8923 if (get_float_arg(argvars, &fx) == OK
8924 && get_float_arg(&argvars[1], &fy) == OK)
8925 rettv->vval.v_float = atan2(fx, fy);
8926 else
8927 rettv->vval.v_float = 0.0;
8928}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008929#endif
8930
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931/*
8932 * "browse(save, title, initdir, default)" function
8933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008935f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008936 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938{
8939#ifdef FEAT_BROWSE
8940 int save;
8941 char_u *title;
8942 char_u *initdir;
8943 char_u *defname;
8944 char_u buf[NUMBUFLEN];
8945 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008946 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008948 save = get_tv_number_chk(&argvars[0], &error);
8949 title = get_tv_string_chk(&argvars[1]);
8950 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8951 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008953 if (error || title == NULL || initdir == NULL || defname == NULL)
8954 rettv->vval.v_string = NULL;
8955 else
8956 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008957 do_browse(save ? BROWSE_SAVE : 0,
8958 title, defname, NULL, initdir, NULL, curbuf);
8959#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008961#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008962 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008963}
8964
8965/*
8966 * "browsedir(title, initdir)" function
8967 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008970 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008971 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008972{
8973#ifdef FEAT_BROWSE
8974 char_u *title;
8975 char_u *initdir;
8976 char_u buf[NUMBUFLEN];
8977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008978 title = get_tv_string_chk(&argvars[0]);
8979 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008981 if (title == NULL || initdir == NULL)
8982 rettv->vval.v_string = NULL;
8983 else
8984 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008985 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990}
8991
Bram Moolenaar33570922005-01-25 22:26:29 +00008992static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008993
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994/*
8995 * Find a buffer by number or exact name.
8996 */
8997 static buf_T *
8998find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008999 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000{
9001 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009003 if (avar->v_type == VAR_NUMBER)
9004 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009005 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009007 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009008 if (buf == NULL)
9009 {
9010 /* No full path name match, try a match with a URL or a "nofile"
9011 * buffer, these don't use the full path. */
9012 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9013 if (buf->b_fname != NULL
9014 && (path_with_url(buf->b_fname)
9015#ifdef FEAT_QUICKFIX
9016 || bt_nofile(buf)
9017#endif
9018 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009019 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009020 break;
9021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 }
9023 return buf;
9024}
9025
9026/*
9027 * "bufexists(expr)" function
9028 */
9029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009030f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009031 typval_T *argvars;
9032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009034 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035}
9036
9037/*
9038 * "buflisted(expr)" function
9039 */
9040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009042 typval_T *argvars;
9043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044{
9045 buf_T *buf;
9046
9047 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049}
9050
9051/*
9052 * "bufloaded(expr)" function
9053 */
9054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009055f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009056 typval_T *argvars;
9057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058{
9059 buf_T *buf;
9060
9061 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063}
9064
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009065static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009066
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067/*
9068 * Get buffer by number or pattern.
9069 */
9070 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009071get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009072 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009073 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 int save_magic;
9077 char_u *save_cpo;
9078 buf_T *buf;
9079
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080 if (tv->v_type == VAR_NUMBER)
9081 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009082 if (tv->v_type != VAR_STRING)
9083 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 if (name == NULL || *name == NUL)
9085 return curbuf;
9086 if (name[0] == '$' && name[1] == NUL)
9087 return lastbuf;
9088
9089 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9090 save_magic = p_magic;
9091 p_magic = TRUE;
9092 save_cpo = p_cpo;
9093 p_cpo = (char_u *)"";
9094
9095 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009096 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097
9098 p_magic = save_magic;
9099 p_cpo = save_cpo;
9100
9101 /* If not found, try expanding the name, like done for bufexists(). */
9102 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104
9105 return buf;
9106}
9107
9108/*
9109 * "bufname(expr)" function
9110 */
9111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009113 typval_T *argvars;
9114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115{
9116 buf_T *buf;
9117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009118 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009120 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009125 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 --emsg_off;
9127}
9128
9129/*
9130 * "bufnr(expr)" function
9131 */
9132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009134 typval_T *argvars;
9135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
9137 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009138 int error = FALSE;
9139 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009141 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009143 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009144 --emsg_off;
9145
9146 /* If the buffer isn't found and the second argument is not zero create a
9147 * new buffer. */
9148 if (buf == NULL
9149 && argvars[1].v_type != VAR_UNKNOWN
9150 && get_tv_number_chk(&argvars[1], &error) != 0
9151 && !error
9152 && (name = get_tv_string_chk(&argvars[0])) != NULL
9153 && !error)
9154 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9155
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009157 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009159 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160}
9161
9162/*
9163 * "bufwinnr(nr)" function
9164 */
9165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009167 typval_T *argvars;
9168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169{
9170#ifdef FEAT_WINDOWS
9171 win_T *wp;
9172 int winnr = 0;
9173#endif
9174 buf_T *buf;
9175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009176 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009178 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179#ifdef FEAT_WINDOWS
9180 for (wp = firstwin; wp; wp = wp->w_next)
9181 {
9182 ++winnr;
9183 if (wp->w_buffer == buf)
9184 break;
9185 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189#endif
9190 --emsg_off;
9191}
9192
9193/*
9194 * "byte2line(byte)" function
9195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009198 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
9201#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009202 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203#else
9204 long boff = 0;
9205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009206 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009208 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009210 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 (linenr_T)0, &boff);
9212#endif
9213}
9214
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009215 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009216byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009217 typval_T *argvars;
9218 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009219 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009220{
9221#ifdef FEAT_MBYTE
9222 char_u *t;
9223#endif
9224 char_u *str;
9225 long idx;
9226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009227 str = get_tv_string_chk(&argvars[0]);
9228 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009230 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009231 return;
9232
9233#ifdef FEAT_MBYTE
9234 t = str;
9235 for ( ; idx > 0; idx--)
9236 {
9237 if (*t == NUL) /* EOL reached */
9238 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009239 if (enc_utf8 && comp)
9240 t += utf_ptr2len(t);
9241 else
9242 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009243 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009244 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009245#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009246 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009247 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009248#endif
9249}
9250
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009251/*
9252 * "byteidx()" function
9253 */
9254 static void
9255f_byteidx(argvars, rettv)
9256 typval_T *argvars;
9257 typval_T *rettv;
9258{
9259 byteidx(argvars, rettv, FALSE);
9260}
9261
9262/*
9263 * "byteidxcomp()" function
9264 */
9265 static void
9266f_byteidxcomp(argvars, rettv)
9267 typval_T *argvars;
9268 typval_T *rettv;
9269{
9270 byteidx(argvars, rettv, TRUE);
9271}
9272
Bram Moolenaardb913952012-06-29 12:54:53 +02009273 int
9274func_call(name, args, selfdict, rettv)
9275 char_u *name;
9276 typval_T *args;
9277 dict_T *selfdict;
9278 typval_T *rettv;
9279{
9280 listitem_T *item;
9281 typval_T argv[MAX_FUNC_ARGS + 1];
9282 int argc = 0;
9283 int dummy;
9284 int r = 0;
9285
9286 for (item = args->vval.v_list->lv_first; item != NULL;
9287 item = item->li_next)
9288 {
9289 if (argc == MAX_FUNC_ARGS)
9290 {
9291 EMSG(_("E699: Too many arguments"));
9292 break;
9293 }
9294 /* Make a copy of each argument. This is needed to be able to set
9295 * v_lock to VAR_FIXED in the copy without changing the original list.
9296 */
9297 copy_tv(&item->li_tv, &argv[argc++]);
9298 }
9299
9300 if (item == NULL)
9301 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9302 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9303 &dummy, TRUE, selfdict);
9304
9305 /* Free the arguments. */
9306 while (argc > 0)
9307 clear_tv(&argv[--argc]);
9308
9309 return r;
9310}
9311
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009312/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009313 * "call(func, arglist)" function
9314 */
9315 static void
9316f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009317 typval_T *argvars;
9318 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009319{
9320 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009321 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009322
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009323 if (argvars[1].v_type != VAR_LIST)
9324 {
9325 EMSG(_(e_listreq));
9326 return;
9327 }
9328 if (argvars[1].vval.v_list == NULL)
9329 return;
9330
9331 if (argvars[0].v_type == VAR_FUNC)
9332 func = argvars[0].vval.v_string;
9333 else
9334 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009335 if (*func == NUL)
9336 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009337
Bram Moolenaare9a41262005-01-15 22:18:47 +00009338 if (argvars[2].v_type != VAR_UNKNOWN)
9339 {
9340 if (argvars[2].v_type != VAR_DICT)
9341 {
9342 EMSG(_(e_dictreq));
9343 return;
9344 }
9345 selfdict = argvars[2].vval.v_dict;
9346 }
9347
Bram Moolenaardb913952012-06-29 12:54:53 +02009348 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009349}
9350
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009351#ifdef FEAT_FLOAT
9352/*
9353 * "ceil({float})" function
9354 */
9355 static void
9356f_ceil(argvars, rettv)
9357 typval_T *argvars;
9358 typval_T *rettv;
9359{
9360 float_T f;
9361
9362 rettv->v_type = VAR_FLOAT;
9363 if (get_float_arg(argvars, &f) == OK)
9364 rettv->vval.v_float = ceil(f);
9365 else
9366 rettv->vval.v_float = 0.0;
9367}
9368#endif
9369
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009370/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009371 * "changenr()" function
9372 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009373 static void
9374f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009375 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009376 typval_T *rettv;
9377{
9378 rettv->vval.v_number = curbuf->b_u_seq_cur;
9379}
9380
9381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 * "char2nr(string)" function
9383 */
9384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009385f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009386 typval_T *argvars;
9387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388{
9389#ifdef FEAT_MBYTE
9390 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009391 {
9392 int utf8 = 0;
9393
9394 if (argvars[1].v_type != VAR_UNKNOWN)
9395 utf8 = get_tv_number_chk(&argvars[1], NULL);
9396
9397 if (utf8)
9398 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9399 else
9400 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 else
9403#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405}
9406
9407/*
9408 * "cindent(lnum)" function
9409 */
9410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009411f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009412 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414{
9415#ifdef FEAT_CINDENT
9416 pos_T pos;
9417 linenr_T lnum;
9418
9419 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9422 {
9423 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009424 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 curwin->w_cursor = pos;
9426 }
9427 else
9428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430}
9431
9432/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009433 * "clearmatches()" function
9434 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009435 static void
9436f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009437 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009438 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009439{
9440#ifdef FEAT_SEARCH_EXTRA
9441 clear_matches(curwin);
9442#endif
9443}
9444
9445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 * "col(string)" function
9447 */
9448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009450 typval_T *argvars;
9451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452{
9453 colnr_T col = 0;
9454 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009455 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009457 fp = var2fpos(&argvars[0], FALSE, &fnum);
9458 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 {
9460 if (fp->col == MAXCOL)
9461 {
9462 /* '> can be MAXCOL, get the length of the line then */
9463 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009464 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 else
9466 col = MAXCOL;
9467 }
9468 else
9469 {
9470 col = fp->col + 1;
9471#ifdef FEAT_VIRTUALEDIT
9472 /* col(".") when the cursor is on the NUL at the end of the line
9473 * because of "coladd" can be seen as an extra column. */
9474 if (virtual_active() && fp == &curwin->w_cursor)
9475 {
9476 char_u *p = ml_get_cursor();
9477
9478 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9479 curwin->w_virtcol - curwin->w_cursor.coladd))
9480 {
9481# ifdef FEAT_MBYTE
9482 int l;
9483
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009484 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 col += l;
9486# else
9487 if (*p != NUL && p[1] == NUL)
9488 ++col;
9489# endif
9490 }
9491 }
9492#endif
9493 }
9494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496}
9497
Bram Moolenaar572cb562005-08-05 21:35:02 +00009498#if defined(FEAT_INS_EXPAND)
9499/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009500 * "complete()" function
9501 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009502 static void
9503f_complete(argvars, rettv)
9504 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009505 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009506{
9507 int startcol;
9508
9509 if ((State & INSERT) == 0)
9510 {
9511 EMSG(_("E785: complete() can only be used in Insert mode"));
9512 return;
9513 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009514
9515 /* Check for undo allowed here, because if something was already inserted
9516 * the line was already saved for undo and this check isn't done. */
9517 if (!undo_allowed())
9518 return;
9519
Bram Moolenaarade00832006-03-10 21:46:58 +00009520 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9521 {
9522 EMSG(_(e_invarg));
9523 return;
9524 }
9525
9526 startcol = get_tv_number_chk(&argvars[0], NULL);
9527 if (startcol <= 0)
9528 return;
9529
9530 set_completion(startcol - 1, argvars[1].vval.v_list);
9531}
9532
9533/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009534 * "complete_add()" function
9535 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009536 static void
9537f_complete_add(argvars, rettv)
9538 typval_T *argvars;
9539 typval_T *rettv;
9540{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009541 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009542}
9543
9544/*
9545 * "complete_check()" function
9546 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009547 static void
9548f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009549 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009550 typval_T *rettv;
9551{
9552 int saved = RedrawingDisabled;
9553
9554 RedrawingDisabled = 0;
9555 ins_compl_check_keys(0);
9556 rettv->vval.v_number = compl_interrupted;
9557 RedrawingDisabled = saved;
9558}
9559#endif
9560
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561/*
9562 * "confirm(message, buttons[, default [, type]])" function
9563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009566 typval_T *argvars UNUSED;
9567 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
9569#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9570 char_u *message;
9571 char_u *buttons = NULL;
9572 char_u buf[NUMBUFLEN];
9573 char_u buf2[NUMBUFLEN];
9574 int def = 1;
9575 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009576 char_u *typestr;
9577 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009579 message = get_tv_string_chk(&argvars[0]);
9580 if (message == NULL)
9581 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009582 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9585 if (buttons == NULL)
9586 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009587 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009589 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009590 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009592 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9593 if (typestr == NULL)
9594 error = TRUE;
9595 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009597 switch (TOUPPER_ASC(*typestr))
9598 {
9599 case 'E': type = VIM_ERROR; break;
9600 case 'Q': type = VIM_QUESTION; break;
9601 case 'I': type = VIM_INFO; break;
9602 case 'W': type = VIM_WARNING; break;
9603 case 'G': type = VIM_GENERIC; break;
9604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 }
9606 }
9607 }
9608 }
9609
9610 if (buttons == NULL || *buttons == NUL)
9611 buttons = (char_u *)_("&Ok");
9612
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009613 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009614 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009615 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616#endif
9617}
9618
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009619/*
9620 * "copy()" function
9621 */
9622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009624 typval_T *argvars;
9625 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009626{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009627 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009628}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009630#ifdef FEAT_FLOAT
9631/*
9632 * "cos()" function
9633 */
9634 static void
9635f_cos(argvars, rettv)
9636 typval_T *argvars;
9637 typval_T *rettv;
9638{
9639 float_T f;
9640
9641 rettv->v_type = VAR_FLOAT;
9642 if (get_float_arg(argvars, &f) == OK)
9643 rettv->vval.v_float = cos(f);
9644 else
9645 rettv->vval.v_float = 0.0;
9646}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009647
9648/*
9649 * "cosh()" function
9650 */
9651 static void
9652f_cosh(argvars, rettv)
9653 typval_T *argvars;
9654 typval_T *rettv;
9655{
9656 float_T f;
9657
9658 rettv->v_type = VAR_FLOAT;
9659 if (get_float_arg(argvars, &f) == OK)
9660 rettv->vval.v_float = cosh(f);
9661 else
9662 rettv->vval.v_float = 0.0;
9663}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009664#endif
9665
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009667 * "count()" function
9668 */
9669 static void
9670f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009671 typval_T *argvars;
9672 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009673{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009674 long n = 0;
9675 int ic = FALSE;
9676
Bram Moolenaare9a41262005-01-15 22:18:47 +00009677 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009678 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009679 listitem_T *li;
9680 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009681 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009682
Bram Moolenaare9a41262005-01-15 22:18:47 +00009683 if ((l = argvars[0].vval.v_list) != NULL)
9684 {
9685 li = l->lv_first;
9686 if (argvars[2].v_type != VAR_UNKNOWN)
9687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009688 int error = FALSE;
9689
9690 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009691 if (argvars[3].v_type != VAR_UNKNOWN)
9692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009693 idx = get_tv_number_chk(&argvars[3], &error);
9694 if (!error)
9695 {
9696 li = list_find(l, idx);
9697 if (li == NULL)
9698 EMSGN(_(e_listidx), idx);
9699 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009700 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009701 if (error)
9702 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009703 }
9704
9705 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009706 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009707 ++n;
9708 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009709 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009710 else if (argvars[0].v_type == VAR_DICT)
9711 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009712 int todo;
9713 dict_T *d;
9714 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009715
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009716 if ((d = argvars[0].vval.v_dict) != NULL)
9717 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009718 int error = FALSE;
9719
Bram Moolenaare9a41262005-01-15 22:18:47 +00009720 if (argvars[2].v_type != VAR_UNKNOWN)
9721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009722 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009723 if (argvars[3].v_type != VAR_UNKNOWN)
9724 EMSG(_(e_invarg));
9725 }
9726
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009727 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009728 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009729 {
9730 if (!HASHITEM_EMPTY(hi))
9731 {
9732 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009733 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009734 ++n;
9735 }
9736 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009737 }
9738 }
9739 else
9740 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009741 rettv->vval.v_number = n;
9742}
9743
9744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9746 *
9747 * Checks the existence of a cscope connection.
9748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009751 typval_T *argvars UNUSED;
9752 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753{
9754#ifdef FEAT_CSCOPE
9755 int num = 0;
9756 char_u *dbpath = NULL;
9757 char_u *prepend = NULL;
9758 char_u buf[NUMBUFLEN];
9759
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009760 if (argvars[0].v_type != VAR_UNKNOWN
9761 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763 num = (int)get_tv_number(&argvars[0]);
9764 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009765 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 }
9768
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009769 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770#endif
9771}
9772
9773/*
9774 * "cursor(lnum, col)" function
9775 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009776 * Moves the cursor to the specified line and column.
9777 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009781 typval_T *argvars;
9782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783{
9784 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009785#ifdef FEAT_VIRTUALEDIT
9786 long coladd = 0;
9787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009789 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009790 if (argvars[1].v_type == VAR_UNKNOWN)
9791 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009792 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009793
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009794 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009795 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009796 line = pos.lnum;
9797 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009798#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009799 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009800#endif
9801 }
9802 else
9803 {
9804 line = get_tv_lnum(argvars);
9805 col = get_tv_number_chk(&argvars[1], NULL);
9806#ifdef FEAT_VIRTUALEDIT
9807 if (argvars[2].v_type != VAR_UNKNOWN)
9808 coladd = get_tv_number_chk(&argvars[2], NULL);
9809#endif
9810 }
9811 if (line < 0 || col < 0
9812#ifdef FEAT_VIRTUALEDIT
9813 || coladd < 0
9814#endif
9815 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009816 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 if (line > 0)
9818 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 if (col > 0)
9820 curwin->w_cursor.col = col - 1;
9821#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009822 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823#endif
9824
9825 /* Make sure the cursor is in a valid position. */
9826 check_cursor();
9827#ifdef FEAT_MBYTE
9828 /* Correct cursor for multi-byte character. */
9829 if (has_mbyte)
9830 mb_adjust_cursor();
9831#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009832
9833 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009834 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835}
9836
9837/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009838 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 */
9840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009842 typval_T *argvars;
9843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009845 int noref = 0;
9846
9847 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009848 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009849 if (noref < 0 || noref > 1)
9850 EMSG(_(e_invarg));
9851 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009852 {
9853 current_copyID += COPYID_INC;
9854 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856}
9857
9858/*
9859 * "delete()" function
9860 */
9861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009862f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009863 typval_T *argvars;
9864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865{
9866 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009867 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009869 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870}
9871
9872/*
9873 * "did_filetype()" function
9874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009877 typval_T *argvars UNUSED;
9878 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879{
9880#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009881 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882#endif
9883}
9884
9885/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009886 * "diff_filler()" function
9887 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009890 typval_T *argvars UNUSED;
9891 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009892{
9893#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009895#endif
9896}
9897
9898/*
9899 * "diff_hlID()" function
9900 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009902f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009903 typval_T *argvars UNUSED;
9904 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009905{
9906#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009908 static linenr_T prev_lnum = 0;
9909 static int changedtick = 0;
9910 static int fnum = 0;
9911 static int change_start = 0;
9912 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009913 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009914 int filler_lines;
9915 int col;
9916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009917 if (lnum < 0) /* ignore type error in {lnum} arg */
9918 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009919 if (lnum != prev_lnum
9920 || changedtick != curbuf->b_changedtick
9921 || fnum != curbuf->b_fnum)
9922 {
9923 /* New line, buffer, change: need to get the values. */
9924 filler_lines = diff_check(curwin, lnum);
9925 if (filler_lines < 0)
9926 {
9927 if (filler_lines == -1)
9928 {
9929 change_start = MAXCOL;
9930 change_end = -1;
9931 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9932 hlID = HLF_ADD; /* added line */
9933 else
9934 hlID = HLF_CHD; /* changed line */
9935 }
9936 else
9937 hlID = HLF_ADD; /* added line */
9938 }
9939 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009940 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009941 prev_lnum = lnum;
9942 changedtick = curbuf->b_changedtick;
9943 fnum = curbuf->b_fnum;
9944 }
9945
9946 if (hlID == HLF_CHD || hlID == HLF_TXD)
9947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009948 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009949 if (col >= change_start && col <= change_end)
9950 hlID = HLF_TXD; /* changed text */
9951 else
9952 hlID = HLF_CHD; /* changed line */
9953 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009954 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009955#endif
9956}
9957
9958/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009959 * "empty({expr})" function
9960 */
9961 static void
9962f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009963 typval_T *argvars;
9964 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009965{
9966 int n;
9967
9968 switch (argvars[0].v_type)
9969 {
9970 case VAR_STRING:
9971 case VAR_FUNC:
9972 n = argvars[0].vval.v_string == NULL
9973 || *argvars[0].vval.v_string == NUL;
9974 break;
9975 case VAR_NUMBER:
9976 n = argvars[0].vval.v_number == 0;
9977 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009978#ifdef FEAT_FLOAT
9979 case VAR_FLOAT:
9980 n = argvars[0].vval.v_float == 0.0;
9981 break;
9982#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009983 case VAR_LIST:
9984 n = argvars[0].vval.v_list == NULL
9985 || argvars[0].vval.v_list->lv_first == NULL;
9986 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009987 case VAR_DICT:
9988 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009989 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009990 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009991 default:
9992 EMSG2(_(e_intern2), "f_empty()");
9993 n = 0;
9994 }
9995
9996 rettv->vval.v_number = n;
9997}
9998
9999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 * "escape({string}, {chars})" function
10001 */
10002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010003f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010004 typval_T *argvars;
10005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006{
10007 char_u buf[NUMBUFLEN];
10008
Bram Moolenaar758711c2005-02-02 23:11:38 +000010009 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10010 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010011 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012}
10013
10014/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010015 * "eval()" function
10016 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010017 static void
10018f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010019 typval_T *argvars;
10020 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010021{
10022 char_u *s;
10023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010024 s = get_tv_string_chk(&argvars[0]);
10025 if (s != NULL)
10026 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010028 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10029 {
10030 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010031 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010032 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010033 else if (*s != NUL)
10034 EMSG(_(e_trailing));
10035}
10036
10037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 * "eventhandler()" function
10039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010041f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010042 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046}
10047
10048/*
10049 * "executable()" function
10050 */
10051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010053 typval_T *argvars;
10054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010056 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10057}
10058
10059/*
10060 * "exepath()" function
10061 */
10062 static void
10063f_exepath(argvars, rettv)
10064 typval_T *argvars;
10065 typval_T *rettv;
10066{
10067 char_u *p = NULL;
10068
10069 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10070 rettv->v_type = VAR_STRING;
10071 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072}
10073
10074/*
10075 * "exists()" function
10076 */
10077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010078f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010079 typval_T *argvars;
10080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081{
10082 char_u *p;
10083 char_u *name;
10084 int n = FALSE;
10085 int len = 0;
10086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 if (*p == '$') /* environment variable */
10089 {
10090 /* first try "normal" environment variables (fast) */
10091 if (mch_getenv(p + 1) != NULL)
10092 n = TRUE;
10093 else
10094 {
10095 /* try expanding things like $VIM and ${HOME} */
10096 p = expand_env_save(p);
10097 if (p != NULL && *p != '$')
10098 n = TRUE;
10099 vim_free(p);
10100 }
10101 }
10102 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010105 if (*skipwhite(p) != NUL)
10106 n = FALSE; /* trailing garbage */
10107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 else if (*p == '*') /* internal or user defined function */
10109 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010110 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 }
10112 else if (*p == ':')
10113 {
10114 n = cmd_exists(p + 1);
10115 }
10116 else if (*p == '#')
10117 {
10118#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010119 if (p[1] == '#')
10120 n = autocmd_supported(p + 2);
10121 else
10122 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123#endif
10124 }
10125 else /* internal variable */
10126 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010127 char_u *tofree;
10128 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010130 /* get_name_len() takes care of expanding curly braces */
10131 name = p;
10132 len = get_name_len(&p, &tofree, TRUE, FALSE);
10133 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010135 if (tofree != NULL)
10136 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010137 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010138 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010140 /* handle d.key, l[idx], f(expr) */
10141 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10142 if (n)
10143 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 }
10145 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010146 if (*p != NUL)
10147 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010149 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 }
10151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153}
10154
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010155#ifdef FEAT_FLOAT
10156/*
10157 * "exp()" function
10158 */
10159 static void
10160f_exp(argvars, rettv)
10161 typval_T *argvars;
10162 typval_T *rettv;
10163{
10164 float_T f;
10165
10166 rettv->v_type = VAR_FLOAT;
10167 if (get_float_arg(argvars, &f) == OK)
10168 rettv->vval.v_float = exp(f);
10169 else
10170 rettv->vval.v_float = 0.0;
10171}
10172#endif
10173
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174/*
10175 * "expand()" function
10176 */
10177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010178f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010179 typval_T *argvars;
10180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181{
10182 char_u *s;
10183 int len;
10184 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010185 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010187 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010188 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010190 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010191 if (argvars[1].v_type != VAR_UNKNOWN
10192 && argvars[2].v_type != VAR_UNKNOWN
10193 && get_tv_number_chk(&argvars[2], &error)
10194 && !error)
10195 {
10196 rettv->v_type = VAR_LIST;
10197 rettv->vval.v_list = NULL;
10198 }
10199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010200 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 if (*s == '%' || *s == '#' || *s == '<')
10202 {
10203 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010204 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010206 if (rettv->v_type == VAR_LIST)
10207 {
10208 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10209 list_append_string(rettv->vval.v_list, result, -1);
10210 else
10211 vim_free(result);
10212 }
10213 else
10214 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 }
10216 else
10217 {
10218 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010219 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010220 if (argvars[1].v_type != VAR_UNKNOWN
10221 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010222 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 if (!error)
10224 {
10225 ExpandInit(&xpc);
10226 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010227 if (p_wic)
10228 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010229 if (rettv->v_type == VAR_STRING)
10230 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10231 options, WILD_ALL);
10232 else if (rettv_list_alloc(rettv) != FAIL)
10233 {
10234 int i;
10235
10236 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10237 for (i = 0; i < xpc.xp_numfiles; i++)
10238 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10239 ExpandCleanup(&xpc);
10240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010241 }
10242 else
10243 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 }
10245}
10246
10247/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010248 * Go over all entries in "d2" and add them to "d1".
10249 * When "action" is "error" then a duplicate key is an error.
10250 * When "action" is "force" then a duplicate key is overwritten.
10251 * Otherwise duplicate keys are ignored ("action" is "keep").
10252 */
10253 void
10254dict_extend(d1, d2, action)
10255 dict_T *d1;
10256 dict_T *d2;
10257 char_u *action;
10258{
10259 dictitem_T *di1;
10260 hashitem_T *hi2;
10261 int todo;
10262
10263 todo = (int)d2->dv_hashtab.ht_used;
10264 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10265 {
10266 if (!HASHITEM_EMPTY(hi2))
10267 {
10268 --todo;
10269 di1 = dict_find(d1, hi2->hi_key, -1);
10270 if (d1->dv_scope != 0)
10271 {
10272 /* Disallow replacing a builtin function in l: and g:.
10273 * Check the key to be valid when adding to any
10274 * scope. */
10275 if (d1->dv_scope == VAR_DEF_SCOPE
10276 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10277 && var_check_func_name(hi2->hi_key,
10278 di1 == NULL))
10279 break;
10280 if (!valid_varname(hi2->hi_key))
10281 break;
10282 }
10283 if (di1 == NULL)
10284 {
10285 di1 = dictitem_copy(HI2DI(hi2));
10286 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10287 dictitem_free(di1);
10288 }
10289 else if (*action == 'e')
10290 {
10291 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10292 break;
10293 }
10294 else if (*action == 'f' && HI2DI(hi2) != di1)
10295 {
10296 clear_tv(&di1->di_tv);
10297 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10298 }
10299 }
10300 }
10301}
10302
10303/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010304 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010305 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010306 */
10307 static void
10308f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010309 typval_T *argvars;
10310 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010311{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010312 char *arg_errmsg = N_("extend() argument");
10313
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010315 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010316 list_T *l1, *l2;
10317 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010318 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010319 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010320
Bram Moolenaare9a41262005-01-15 22:18:47 +000010321 l1 = argvars[0].vval.v_list;
10322 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010323 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010324 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010325 {
10326 if (argvars[2].v_type != VAR_UNKNOWN)
10327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010328 before = get_tv_number_chk(&argvars[2], &error);
10329 if (error)
10330 return; /* type error; errmsg already given */
10331
Bram Moolenaar758711c2005-02-02 23:11:38 +000010332 if (before == l1->lv_len)
10333 item = NULL;
10334 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010335 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010336 item = list_find(l1, before);
10337 if (item == NULL)
10338 {
10339 EMSGN(_(e_listidx), before);
10340 return;
10341 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010342 }
10343 }
10344 else
10345 item = NULL;
10346 list_extend(l1, l2, item);
10347
Bram Moolenaare9a41262005-01-15 22:18:47 +000010348 copy_tv(&argvars[0], rettv);
10349 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010350 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010351 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10352 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010353 dict_T *d1, *d2;
10354 char_u *action;
10355 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010356
10357 d1 = argvars[0].vval.v_dict;
10358 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010359 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010360 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010361 {
10362 /* Check the third argument. */
10363 if (argvars[2].v_type != VAR_UNKNOWN)
10364 {
10365 static char *(av[]) = {"keep", "force", "error"};
10366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010367 action = get_tv_string_chk(&argvars[2]);
10368 if (action == NULL)
10369 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010370 for (i = 0; i < 3; ++i)
10371 if (STRCMP(action, av[i]) == 0)
10372 break;
10373 if (i == 3)
10374 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010375 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010376 return;
10377 }
10378 }
10379 else
10380 action = (char_u *)"force";
10381
Bram Moolenaara9922d62013-05-30 13:01:18 +020010382 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010383
Bram Moolenaare9a41262005-01-15 22:18:47 +000010384 copy_tv(&argvars[0], rettv);
10385 }
10386 }
10387 else
10388 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010389}
10390
10391/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010392 * "feedkeys()" function
10393 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010394 static void
10395f_feedkeys(argvars, rettv)
10396 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010397 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010398{
10399 int remap = TRUE;
10400 char_u *keys, *flags;
10401 char_u nbuf[NUMBUFLEN];
10402 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010403 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010404
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010405 /* This is not allowed in the sandbox. If the commands would still be
10406 * executed in the sandbox it would be OK, but it probably happens later,
10407 * when "sandbox" is no longer set. */
10408 if (check_secure())
10409 return;
10410
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010411 keys = get_tv_string(&argvars[0]);
10412 if (*keys != NUL)
10413 {
10414 if (argvars[1].v_type != VAR_UNKNOWN)
10415 {
10416 flags = get_tv_string_buf(&argvars[1], nbuf);
10417 for ( ; *flags != NUL; ++flags)
10418 {
10419 switch (*flags)
10420 {
10421 case 'n': remap = FALSE; break;
10422 case 'm': remap = TRUE; break;
10423 case 't': typed = TRUE; break;
10424 }
10425 }
10426 }
10427
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010428 /* Need to escape K_SPECIAL and CSI before putting the string in the
10429 * typeahead buffer. */
10430 keys_esc = vim_strsave_escape_csi(keys);
10431 if (keys_esc != NULL)
10432 {
10433 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010434 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010435 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010436 if (vgetc_busy)
10437 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010438 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010439 }
10440}
10441
10442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 * "filereadable()" function
10444 */
10445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010447 typval_T *argvars;
10448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010450 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 char_u *p;
10452 int n;
10453
Bram Moolenaarc236c162008-07-13 17:41:49 +000010454#ifndef O_NONBLOCK
10455# define O_NONBLOCK 0
10456#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010457 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010458 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10459 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 {
10461 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010462 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 }
10464 else
10465 n = FALSE;
10466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010467 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468}
10469
10470/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010471 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 * rights to write into.
10473 */
10474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010475f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010476 typval_T *argvars;
10477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010479 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480}
10481
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010482static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010483
10484 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010485findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010486 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010487 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010488 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010489{
10490#ifdef FEAT_SEARCHPATH
10491 char_u *fname;
10492 char_u *fresult = NULL;
10493 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10494 char_u *p;
10495 char_u pathbuf[NUMBUFLEN];
10496 int count = 1;
10497 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010498 int error = FALSE;
10499#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010500
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010501 rettv->vval.v_string = NULL;
10502 rettv->v_type = VAR_STRING;
10503
10504#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010505 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010506
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010507 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010509 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10510 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010511 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010512 else
10513 {
10514 if (*p != NUL)
10515 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010517 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010518 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010520 }
10521
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010522 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10523 error = TRUE;
10524
10525 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 do
10528 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010529 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010530 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010531 fresult = find_file_in_path_option(first ? fname : NULL,
10532 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010533 0, first, path,
10534 find_what,
10535 curbuf->b_ffname,
10536 find_what == FINDFILE_DIR
10537 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010538 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010539
10540 if (fresult != NULL && rettv->v_type == VAR_LIST)
10541 list_append_string(rettv->vval.v_list, fresult, -1);
10542
10543 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010545
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010546 if (rettv->v_type == VAR_STRING)
10547 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010548#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010549}
10550
Bram Moolenaar33570922005-01-25 22:26:29 +000010551static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10552static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010553
10554/*
10555 * Implementation of map() and filter().
10556 */
10557 static void
10558filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010559 typval_T *argvars;
10560 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010561 int map;
10562{
10563 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010564 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010565 listitem_T *li, *nli;
10566 list_T *l = NULL;
10567 dictitem_T *di;
10568 hashtab_T *ht;
10569 hashitem_T *hi;
10570 dict_T *d = NULL;
10571 typval_T save_val;
10572 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010574 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010575 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10576 char *arg_errmsg = (map ? N_("map() argument")
10577 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010578 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010579 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010580
Bram Moolenaare9a41262005-01-15 22:18:47 +000010581 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010582 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010583 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010584 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010585 return;
10586 }
10587 else if (argvars[0].v_type == VAR_DICT)
10588 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010589 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010590 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010591 return;
10592 }
10593 else
10594 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010595 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010596 return;
10597 }
10598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010599 expr = get_tv_string_buf_chk(&argvars[1], buf);
10600 /* On type errors, the preceding call has already displayed an error
10601 * message. Avoid a misleading error message for an empty string that
10602 * was not passed as argument. */
10603 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010605 prepare_vimvar(VV_VAL, &save_val);
10606 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010607
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010608 /* We reset "did_emsg" to be able to detect whether an error
10609 * occurred during evaluation of the expression. */
10610 save_did_emsg = did_emsg;
10611 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010612
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010613 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010615 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010616 vimvars[VV_KEY].vv_type = VAR_STRING;
10617
10618 ht = &d->dv_hashtab;
10619 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010620 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010621 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 if (!HASHITEM_EMPTY(hi))
10624 {
10625 --todo;
10626 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010627 if (tv_check_lock(di->di_tv.v_lock,
10628 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010629 break;
10630 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010631 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010632 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010633 break;
10634 if (!map && rem)
10635 dictitem_remove(d, di);
10636 clear_tv(&vimvars[VV_KEY].vv_tv);
10637 }
10638 }
10639 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010640 }
10641 else
10642 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010643 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010645 for (li = l->lv_first; li != NULL; li = nli)
10646 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010647 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010648 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010649 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010650 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010651 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010652 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010653 break;
10654 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010655 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010656 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010657 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010658 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010659
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010660 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010661 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010662
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010663 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010664 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010665
10666 copy_tv(&argvars[0], rettv);
10667}
10668
10669 static int
10670filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010672 char_u *expr;
10673 int map;
10674 int *remp;
10675{
Bram Moolenaar33570922005-01-25 22:26:29 +000010676 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010677 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010678 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010679
Bram Moolenaar33570922005-01-25 22:26:29 +000010680 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010681 s = expr;
10682 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010683 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010684 if (*s != NUL) /* check for trailing chars after expr */
10685 {
10686 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010687 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010688 }
10689 if (map)
10690 {
10691 /* map(): replace the list item value */
10692 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010693 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010694 *tv = rettv;
10695 }
10696 else
10697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010698 int error = FALSE;
10699
Bram Moolenaare9a41262005-01-15 22:18:47 +000010700 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010701 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010702 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010703 /* On type error, nothing has been removed; return FAIL to stop the
10704 * loop. The error message was given by get_tv_number_chk(). */
10705 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010706 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010707 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010708 retval = OK;
10709theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010710 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010711 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010712}
10713
10714/*
10715 * "filter()" function
10716 */
10717 static void
10718f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010719 typval_T *argvars;
10720 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010721{
10722 filter_map(argvars, rettv, FALSE);
10723}
10724
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010725/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010726 * "finddir({fname}[, {path}[, {count}]])" function
10727 */
10728 static void
10729f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010730 typval_T *argvars;
10731 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010732{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010733 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010734}
10735
10736/*
10737 * "findfile({fname}[, {path}[, {count}]])" function
10738 */
10739 static void
10740f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010741 typval_T *argvars;
10742 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010743{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010744 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010745}
10746
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010747#ifdef FEAT_FLOAT
10748/*
10749 * "float2nr({float})" function
10750 */
10751 static void
10752f_float2nr(argvars, rettv)
10753 typval_T *argvars;
10754 typval_T *rettv;
10755{
10756 float_T f;
10757
10758 if (get_float_arg(argvars, &f) == OK)
10759 {
10760 if (f < -0x7fffffff)
10761 rettv->vval.v_number = -0x7fffffff;
10762 else if (f > 0x7fffffff)
10763 rettv->vval.v_number = 0x7fffffff;
10764 else
10765 rettv->vval.v_number = (varnumber_T)f;
10766 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010767}
10768
10769/*
10770 * "floor({float})" function
10771 */
10772 static void
10773f_floor(argvars, rettv)
10774 typval_T *argvars;
10775 typval_T *rettv;
10776{
10777 float_T f;
10778
10779 rettv->v_type = VAR_FLOAT;
10780 if (get_float_arg(argvars, &f) == OK)
10781 rettv->vval.v_float = floor(f);
10782 else
10783 rettv->vval.v_float = 0.0;
10784}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010785
10786/*
10787 * "fmod()" function
10788 */
10789 static void
10790f_fmod(argvars, rettv)
10791 typval_T *argvars;
10792 typval_T *rettv;
10793{
10794 float_T fx, fy;
10795
10796 rettv->v_type = VAR_FLOAT;
10797 if (get_float_arg(argvars, &fx) == OK
10798 && get_float_arg(&argvars[1], &fy) == OK)
10799 rettv->vval.v_float = fmod(fx, fy);
10800 else
10801 rettv->vval.v_float = 0.0;
10802}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010803#endif
10804
Bram Moolenaar0d660222005-01-07 21:51:51 +000010805/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010806 * "fnameescape({string})" function
10807 */
10808 static void
10809f_fnameescape(argvars, rettv)
10810 typval_T *argvars;
10811 typval_T *rettv;
10812{
10813 rettv->vval.v_string = vim_strsave_fnameescape(
10814 get_tv_string(&argvars[0]), FALSE);
10815 rettv->v_type = VAR_STRING;
10816}
10817
10818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 * "fnamemodify({fname}, {mods})" function
10820 */
10821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010822f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010823 typval_T *argvars;
10824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825{
10826 char_u *fname;
10827 char_u *mods;
10828 int usedlen = 0;
10829 int len;
10830 char_u *fbuf = NULL;
10831 char_u buf[NUMBUFLEN];
10832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010833 fname = get_tv_string_chk(&argvars[0]);
10834 mods = get_tv_string_buf_chk(&argvars[1], buf);
10835 if (fname == NULL || mods == NULL)
10836 fname = NULL;
10837 else
10838 {
10839 len = (int)STRLEN(fname);
10840 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010847 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 vim_free(fbuf);
10849}
10850
Bram Moolenaar33570922005-01-25 22:26:29 +000010851static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852
10853/*
10854 * "foldclosed()" function
10855 */
10856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010857foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010858 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010859 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010860 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861{
10862#ifdef FEAT_FOLDING
10863 linenr_T lnum;
10864 linenr_T first, last;
10865
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010866 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10868 {
10869 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10870 {
10871 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 return;
10876 }
10877 }
10878#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880}
10881
10882/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010883 * "foldclosed()" function
10884 */
10885 static void
10886f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010887 typval_T *argvars;
10888 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010889{
10890 foldclosed_both(argvars, rettv, FALSE);
10891}
10892
10893/*
10894 * "foldclosedend()" function
10895 */
10896 static void
10897f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010898 typval_T *argvars;
10899 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010900{
10901 foldclosed_both(argvars, rettv, TRUE);
10902}
10903
10904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 * "foldlevel()" function
10906 */
10907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010909 typval_T *argvars UNUSED;
10910 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911{
10912#ifdef FEAT_FOLDING
10913 linenr_T lnum;
10914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919}
10920
10921/*
10922 * "foldtext()" function
10923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010925f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010926 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928{
10929#ifdef FEAT_FOLDING
10930 linenr_T lnum;
10931 char_u *s;
10932 char_u *r;
10933 int len;
10934 char *txt;
10935#endif
10936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937 rettv->v_type = VAR_STRING;
10938 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010940 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10941 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10942 <= curbuf->b_ml.ml_line_count
10943 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 {
10945 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10947 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 {
10949 if (!linewhite(lnum))
10950 break;
10951 ++lnum;
10952 }
10953
10954 /* Find interesting text in this line. */
10955 s = skipwhite(ml_get(lnum));
10956 /* skip C comment-start */
10957 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010960 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010961 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010962 {
10963 s = skipwhite(ml_get(lnum + 1));
10964 if (*s == '*')
10965 s = skipwhite(s + 1);
10966 }
10967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 txt = _("+-%s%3ld lines: ");
10969 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010970 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 + 20 /* for %3ld */
10972 + STRLEN(s))); /* concatenated */
10973 if (r != NULL)
10974 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010975 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10976 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10977 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 len = (int)STRLEN(r);
10979 STRCAT(r, s);
10980 /* remove 'foldmarker' and 'commentstring' */
10981 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010982 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 }
10984 }
10985#endif
10986}
10987
10988/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010989 * "foldtextresult(lnum)" function
10990 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010992f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010993 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010994 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010995{
10996#ifdef FEAT_FOLDING
10997 linenr_T lnum;
10998 char_u *text;
10999 char_u buf[51];
11000 foldinfo_T foldinfo;
11001 int fold_count;
11002#endif
11003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004 rettv->v_type = VAR_STRING;
11005 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011006#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011008 /* treat illegal types and illegal string values for {lnum} the same */
11009 if (lnum < 0)
11010 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011011 fold_count = foldedCount(curwin, lnum, &foldinfo);
11012 if (fold_count > 0)
11013 {
11014 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11015 &foldinfo, buf);
11016 if (text == buf)
11017 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011018 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011019 }
11020#endif
11021}
11022
11023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 * "foreground()" function
11025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011027f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011028 typval_T *argvars UNUSED;
11029 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031#ifdef FEAT_GUI
11032 if (gui.in_use)
11033 gui_mch_set_foreground();
11034#else
11035# ifdef WIN32
11036 win32_set_foreground();
11037# endif
11038#endif
11039}
11040
11041/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011042 * "function()" function
11043 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011046 typval_T *argvars;
11047 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011048{
11049 char_u *s;
11050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011052 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011053 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011054 /* Don't check an autoload name for existence here. */
11055 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011056 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011057 else
11058 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011059 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011060 {
11061 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011062 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011063
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011064 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11065 * also be called from another script. Using trans_function_name()
11066 * would also work, but some plugins depend on the name being
11067 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011068 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011069 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011070 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011071 if (rettv->vval.v_string != NULL)
11072 {
11073 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011074 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011075 }
11076 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011077 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011078 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011079 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011080 }
11081}
11082
11083/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011084 * "garbagecollect()" function
11085 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011086 static void
11087f_garbagecollect(argvars, rettv)
11088 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011089 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011090{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011091 /* This is postponed until we are back at the toplevel, because we may be
11092 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11093 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011094
11095 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11096 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011097}
11098
11099/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011100 * "get()" function
11101 */
11102 static void
11103f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011104 typval_T *argvars;
11105 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011106{
Bram Moolenaar33570922005-01-25 22:26:29 +000011107 listitem_T *li;
11108 list_T *l;
11109 dictitem_T *di;
11110 dict_T *d;
11111 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011112
Bram Moolenaare9a41262005-01-15 22:18:47 +000011113 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011114 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011115 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011116 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011117 int error = FALSE;
11118
11119 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11120 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011121 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011122 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011123 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124 else if (argvars[0].v_type == VAR_DICT)
11125 {
11126 if ((d = argvars[0].vval.v_dict) != NULL)
11127 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011128 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011129 if (di != NULL)
11130 tv = &di->di_tv;
11131 }
11132 }
11133 else
11134 EMSG2(_(e_listdictarg), "get()");
11135
11136 if (tv == NULL)
11137 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011138 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011139 copy_tv(&argvars[2], rettv);
11140 }
11141 else
11142 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011143}
11144
Bram Moolenaar342337a2005-07-21 21:11:17 +000011145static 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 +000011146
11147/*
11148 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011149 * Return a range (from start to end) of lines in rettv from the specified
11150 * buffer.
11151 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011152 */
11153 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011154get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011155 buf_T *buf;
11156 linenr_T start;
11157 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011158 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011159 typval_T *rettv;
11160{
11161 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011162
Bram Moolenaar959a1432013-12-14 12:17:38 +010011163 rettv->v_type = VAR_STRING;
11164 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011165 if (retlist && rettv_list_alloc(rettv) == FAIL)
11166 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011167
11168 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11169 return;
11170
11171 if (!retlist)
11172 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011173 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11174 p = ml_get_buf(buf, start, FALSE);
11175 else
11176 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011177 rettv->vval.v_string = vim_strsave(p);
11178 }
11179 else
11180 {
11181 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011182 return;
11183
11184 if (start < 1)
11185 start = 1;
11186 if (end > buf->b_ml.ml_line_count)
11187 end = buf->b_ml.ml_line_count;
11188 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011189 if (list_append_string(rettv->vval.v_list,
11190 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011191 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011192 }
11193}
11194
11195/*
11196 * "getbufline()" function
11197 */
11198 static void
11199f_getbufline(argvars, rettv)
11200 typval_T *argvars;
11201 typval_T *rettv;
11202{
11203 linenr_T lnum;
11204 linenr_T end;
11205 buf_T *buf;
11206
11207 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11208 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011209 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011210 --emsg_off;
11211
Bram Moolenaar661b1822005-07-28 22:36:45 +000011212 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011213 if (argvars[2].v_type == VAR_UNKNOWN)
11214 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011215 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011216 end = get_tv_lnum_buf(&argvars[2], buf);
11217
Bram Moolenaar342337a2005-07-21 21:11:17 +000011218 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011219}
11220
Bram Moolenaar0d660222005-01-07 21:51:51 +000011221/*
11222 * "getbufvar()" function
11223 */
11224 static void
11225f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011226 typval_T *argvars;
11227 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011228{
11229 buf_T *buf;
11230 buf_T *save_curbuf;
11231 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011232 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011233 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011235 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11236 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011237 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011238 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011239
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011240 rettv->v_type = VAR_STRING;
11241 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011242
11243 if (buf != NULL && varname != NULL)
11244 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011245 /* set curbuf to be our buf, temporarily */
11246 save_curbuf = curbuf;
11247 curbuf = buf;
11248
Bram Moolenaar0d660222005-01-07 21:51:51 +000011249 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011250 {
11251 if (get_option_tv(&varname, rettv, TRUE) == OK)
11252 done = TRUE;
11253 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011254 else if (STRCMP(varname, "changedtick") == 0)
11255 {
11256 rettv->v_type = VAR_NUMBER;
11257 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011258 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011259 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011260 else
11261 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011262 /* Look up the variable. */
11263 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11264 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11265 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011266 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011267 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011268 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011269 done = TRUE;
11270 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011271 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011272
11273 /* restore previous notion of curbuf */
11274 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011275 }
11276
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011277 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11278 /* use the default value */
11279 copy_tv(&argvars[2], rettv);
11280
Bram Moolenaar0d660222005-01-07 21:51:51 +000011281 --emsg_off;
11282}
11283
11284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 * "getchar()" function
11286 */
11287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011289 typval_T *argvars;
11290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291{
11292 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011293 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011295 /* Position the cursor. Needed after a message that ends in a space. */
11296 windgoto(msg_row, msg_col);
11297
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 ++no_mapping;
11299 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011300 for (;;)
11301 {
11302 if (argvars[0].v_type == VAR_UNKNOWN)
11303 /* getchar(): blocking wait. */
11304 n = safe_vgetc();
11305 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11306 /* getchar(1): only check if char avail */
11307 n = vpeekc();
11308 else if (error || vpeekc() == NUL)
11309 /* illegal argument or getchar(0) and no char avail: return zero */
11310 n = 0;
11311 else
11312 /* getchar(0) and char avail: return char */
11313 n = safe_vgetc();
11314 if (n == K_IGNORE)
11315 continue;
11316 break;
11317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 --no_mapping;
11319 --allow_keys;
11320
Bram Moolenaar219b8702006-11-01 14:32:36 +000011321 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11322 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11323 vimvars[VV_MOUSE_COL].vv_nr = 0;
11324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011325 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 if (IS_SPECIAL(n) || mod_mask != 0)
11327 {
11328 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11329 int i = 0;
11330
11331 /* Turn a special key into three bytes, plus modifier. */
11332 if (mod_mask != 0)
11333 {
11334 temp[i++] = K_SPECIAL;
11335 temp[i++] = KS_MODIFIER;
11336 temp[i++] = mod_mask;
11337 }
11338 if (IS_SPECIAL(n))
11339 {
11340 temp[i++] = K_SPECIAL;
11341 temp[i++] = K_SECOND(n);
11342 temp[i++] = K_THIRD(n);
11343 }
11344#ifdef FEAT_MBYTE
11345 else if (has_mbyte)
11346 i += (*mb_char2bytes)(n, temp + i);
11347#endif
11348 else
11349 temp[i++] = n;
11350 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351 rettv->v_type = VAR_STRING;
11352 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011353
11354#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011355 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011356 {
11357 int row = mouse_row;
11358 int col = mouse_col;
11359 win_T *win;
11360 linenr_T lnum;
11361# ifdef FEAT_WINDOWS
11362 win_T *wp;
11363# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011364 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011365
11366 if (row >= 0 && col >= 0)
11367 {
11368 /* Find the window at the mouse coordinates and compute the
11369 * text position. */
11370 win = mouse_find_win(&row, &col);
11371 (void)mouse_comp_pos(win, &row, &col, &lnum);
11372# ifdef FEAT_WINDOWS
11373 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011374 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011375# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011376 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011377 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11378 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11379 }
11380 }
11381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 }
11383}
11384
11385/*
11386 * "getcharmod()" function
11387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011390 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011393 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394}
11395
11396/*
11397 * "getcmdline()" function
11398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011401 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 rettv->v_type = VAR_STRING;
11405 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406}
11407
11408/*
11409 * "getcmdpos()" function
11410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011413 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011416 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417}
11418
11419/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011420 * "getcmdtype()" function
11421 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011422 static void
11423f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011424 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011425 typval_T *rettv;
11426{
11427 rettv->v_type = VAR_STRING;
11428 rettv->vval.v_string = alloc(2);
11429 if (rettv->vval.v_string != NULL)
11430 {
11431 rettv->vval.v_string[0] = get_cmdline_type();
11432 rettv->vval.v_string[1] = NUL;
11433 }
11434}
11435
11436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 * "getcwd()" function
11438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011441 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011444 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011447 rettv->vval.v_string = NULL;
11448 cwd = alloc(MAXPATHL);
11449 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011451 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11452 {
11453 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011455 if (rettv->vval.v_string != NULL)
11456 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011458 }
11459 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460 }
11461}
11462
11463/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011464 * "getfontname()" function
11465 */
11466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011468 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011469 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011470{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 rettv->v_type = VAR_STRING;
11472 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011473#ifdef FEAT_GUI
11474 if (gui.in_use)
11475 {
11476 GuiFont font;
11477 char_u *name = NULL;
11478
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011479 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011480 {
11481 /* Get the "Normal" font. Either the name saved by
11482 * hl_set_font_name() or from the font ID. */
11483 font = gui.norm_font;
11484 name = hl_get_font_name();
11485 }
11486 else
11487 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011489 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11490 return;
11491 font = gui_mch_get_font(name, FALSE);
11492 if (font == NOFONT)
11493 return; /* Invalid font name, return empty string. */
11494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011496 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011497 gui_mch_free_font(font);
11498 }
11499#endif
11500}
11501
11502/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011503 * "getfperm({fname})" function
11504 */
11505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011506f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011507 typval_T *argvars;
11508 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011509{
11510 char_u *fname;
11511 struct stat st;
11512 char_u *perm = NULL;
11513 char_u flags[] = "rwx";
11514 int i;
11515
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011516 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011519 if (mch_stat((char *)fname, &st) >= 0)
11520 {
11521 perm = vim_strsave((char_u *)"---------");
11522 if (perm != NULL)
11523 {
11524 for (i = 0; i < 9; i++)
11525 {
11526 if (st.st_mode & (1 << (8 - i)))
11527 perm[i] = flags[i % 3];
11528 }
11529 }
11530 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011531 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011532}
11533
11534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535 * "getfsize({fname})" function
11536 */
11537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011538f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011539 typval_T *argvars;
11540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541{
11542 char_u *fname;
11543 struct stat st;
11544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011545 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011547 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548
11549 if (mch_stat((char *)fname, &st) >= 0)
11550 {
11551 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011555 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011556
11557 /* non-perfect check for overflow */
11558 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11559 rettv->vval.v_number = -2;
11560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 }
11562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564}
11565
11566/*
11567 * "getftime({fname})" function
11568 */
11569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011571 typval_T *argvars;
11572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573{
11574 char_u *fname;
11575 struct stat st;
11576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578
11579 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011580 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011582 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583}
11584
11585/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011586 * "getftype({fname})" function
11587 */
11588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011590 typval_T *argvars;
11591 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011592{
11593 char_u *fname;
11594 struct stat st;
11595 char_u *type = NULL;
11596 char *t;
11597
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011598 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011599
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011600 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011601 if (mch_lstat((char *)fname, &st) >= 0)
11602 {
11603#ifdef S_ISREG
11604 if (S_ISREG(st.st_mode))
11605 t = "file";
11606 else if (S_ISDIR(st.st_mode))
11607 t = "dir";
11608# ifdef S_ISLNK
11609 else if (S_ISLNK(st.st_mode))
11610 t = "link";
11611# endif
11612# ifdef S_ISBLK
11613 else if (S_ISBLK(st.st_mode))
11614 t = "bdev";
11615# endif
11616# ifdef S_ISCHR
11617 else if (S_ISCHR(st.st_mode))
11618 t = "cdev";
11619# endif
11620# ifdef S_ISFIFO
11621 else if (S_ISFIFO(st.st_mode))
11622 t = "fifo";
11623# endif
11624# ifdef S_ISSOCK
11625 else if (S_ISSOCK(st.st_mode))
11626 t = "fifo";
11627# endif
11628 else
11629 t = "other";
11630#else
11631# ifdef S_IFMT
11632 switch (st.st_mode & S_IFMT)
11633 {
11634 case S_IFREG: t = "file"; break;
11635 case S_IFDIR: t = "dir"; break;
11636# ifdef S_IFLNK
11637 case S_IFLNK: t = "link"; break;
11638# endif
11639# ifdef S_IFBLK
11640 case S_IFBLK: t = "bdev"; break;
11641# endif
11642# ifdef S_IFCHR
11643 case S_IFCHR: t = "cdev"; break;
11644# endif
11645# ifdef S_IFIFO
11646 case S_IFIFO: t = "fifo"; break;
11647# endif
11648# ifdef S_IFSOCK
11649 case S_IFSOCK: t = "socket"; break;
11650# endif
11651 default: t = "other";
11652 }
11653# else
11654 if (mch_isdir(fname))
11655 t = "dir";
11656 else
11657 t = "file";
11658# endif
11659#endif
11660 type = vim_strsave((char_u *)t);
11661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011662 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011663}
11664
11665/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011666 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011667 */
11668 static void
11669f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011670 typval_T *argvars;
11671 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011672{
11673 linenr_T lnum;
11674 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011675 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011676
11677 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011678 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011679 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011680 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011681 retlist = FALSE;
11682 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011683 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011684 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011685 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011686 retlist = TRUE;
11687 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011688
Bram Moolenaar342337a2005-07-21 21:11:17 +000011689 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011690}
11691
11692/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011693 * "getmatches()" function
11694 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011695 static void
11696f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011697 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011698 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011699{
11700#ifdef FEAT_SEARCH_EXTRA
11701 dict_T *dict;
11702 matchitem_T *cur = curwin->w_match_head;
11703
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011704 if (rettv_list_alloc(rettv) == OK)
11705 {
11706 while (cur != NULL)
11707 {
11708 dict = dict_alloc();
11709 if (dict == NULL)
11710 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011711 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11712 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11713 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11714 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11715 list_append_dict(rettv->vval.v_list, dict);
11716 cur = cur->next;
11717 }
11718 }
11719#endif
11720}
11721
11722/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011723 * "getpid()" function
11724 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011725 static void
11726f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011727 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011728 typval_T *rettv;
11729{
11730 rettv->vval.v_number = mch_get_pid();
11731}
11732
11733/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011734 * "getpos(string)" function
11735 */
11736 static void
11737f_getpos(argvars, rettv)
11738 typval_T *argvars;
11739 typval_T *rettv;
11740{
11741 pos_T *fp;
11742 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011743 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011744
11745 if (rettv_list_alloc(rettv) == OK)
11746 {
11747 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011748 fp = var2fpos(&argvars[0], TRUE, &fnum);
11749 if (fnum != -1)
11750 list_append_number(l, (varnumber_T)fnum);
11751 else
11752 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011753 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11754 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011755 list_append_number(l, (fp != NULL)
11756 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011757 : (varnumber_T)0);
11758 list_append_number(l,
11759#ifdef FEAT_VIRTUALEDIT
11760 (fp != NULL) ? (varnumber_T)fp->coladd :
11761#endif
11762 (varnumber_T)0);
11763 }
11764 else
11765 rettv->vval.v_number = FALSE;
11766}
11767
11768/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011769 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011770 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011771 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011772f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011773 typval_T *argvars UNUSED;
11774 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011775{
11776#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011777 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011778#endif
11779
Bram Moolenaar2641f772005-03-25 21:58:17 +000011780#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011781 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011782 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011783 wp = NULL;
11784 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11785 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011786 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011787 if (wp == NULL)
11788 return;
11789 }
11790
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011791 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011792 }
11793#endif
11794}
11795
11796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 * "getreg()" function
11798 */
11799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011801 typval_T *argvars;
11802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803{
11804 char_u *strregname;
11805 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011806 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011807 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011810 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812 strregname = get_tv_string_chk(&argvars[0]);
11813 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011814 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011816 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011817 if (!error && argvars[2].v_type != VAR_UNKNOWN)
11818 return_list = get_tv_number_chk(&argvars[2], &error);
11819 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011822 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011823
11824 if (error)
11825 return;
11826
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827 regname = (strregname == NULL ? '"' : *strregname);
11828 if (regname == 0)
11829 regname = '"';
11830
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011831 if (return_list)
11832 {
11833 rettv->v_type = VAR_LIST;
11834 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
11835 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
11836 }
11837 else
11838 {
11839 rettv->v_type = VAR_STRING;
11840 rettv->vval.v_string = get_reg_contents(regname,
11841 arg2 ? GREG_EXPR_SRC : 0);
11842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843}
11844
11845/*
11846 * "getregtype()" function
11847 */
11848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011849f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011850 typval_T *argvars;
11851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852{
11853 char_u *strregname;
11854 int regname;
11855 char_u buf[NUMBUFLEN + 2];
11856 long reglen = 0;
11857
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011858 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011859 {
11860 strregname = get_tv_string_chk(&argvars[0]);
11861 if (strregname == NULL) /* type error; errmsg already given */
11862 {
11863 rettv->v_type = VAR_STRING;
11864 rettv->vval.v_string = NULL;
11865 return;
11866 }
11867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 else
11869 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871
11872 regname = (strregname == NULL ? '"' : *strregname);
11873 if (regname == 0)
11874 regname = '"';
11875
11876 buf[0] = NUL;
11877 buf[1] = NUL;
11878 switch (get_reg_type(regname, &reglen))
11879 {
11880 case MLINE: buf[0] = 'V'; break;
11881 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882 case MBLOCK:
11883 buf[0] = Ctrl_V;
11884 sprintf((char *)buf + 1, "%ld", reglen + 1);
11885 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011887 rettv->v_type = VAR_STRING;
11888 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889}
11890
11891/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011892 * "gettabvar()" function
11893 */
11894 static void
11895f_gettabvar(argvars, rettv)
11896 typval_T *argvars;
11897 typval_T *rettv;
11898{
11899 tabpage_T *tp;
11900 dictitem_T *v;
11901 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011902 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011903
11904 rettv->v_type = VAR_STRING;
11905 rettv->vval.v_string = NULL;
11906
11907 varname = get_tv_string_chk(&argvars[1]);
11908 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11909 if (tp != NULL && varname != NULL)
11910 {
11911 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011912 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011913 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011914 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011915 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011916 done = TRUE;
11917 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011918 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011919
11920 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011921 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011922}
11923
11924/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011925 * "gettabwinvar()" function
11926 */
11927 static void
11928f_gettabwinvar(argvars, rettv)
11929 typval_T *argvars;
11930 typval_T *rettv;
11931{
11932 getwinvar(argvars, rettv, 1);
11933}
11934
11935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 * "getwinposx()" function
11937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011939f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011940 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011943 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944#ifdef FEAT_GUI
11945 if (gui.in_use)
11946 {
11947 int x, y;
11948
11949 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011950 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951 }
11952#endif
11953}
11954
11955/*
11956 * "getwinposy()" function
11957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011959f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011960 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011963 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964#ifdef FEAT_GUI
11965 if (gui.in_use)
11966 {
11967 int x, y;
11968
11969 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971 }
11972#endif
11973}
11974
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011975/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011976 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011977 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011978 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011979find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011980 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011981 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011982{
11983#ifdef FEAT_WINDOWS
11984 win_T *wp;
11985#endif
11986 int nr;
11987
11988 nr = get_tv_number_chk(vp, NULL);
11989
11990#ifdef FEAT_WINDOWS
11991 if (nr < 0)
11992 return NULL;
11993 if (nr == 0)
11994 return curwin;
11995
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011996 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11997 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011998 if (--nr <= 0)
11999 break;
12000 return wp;
12001#else
12002 if (nr == 0 || nr == 1)
12003 return curwin;
12004 return NULL;
12005#endif
12006}
12007
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008/*
12009 * "getwinvar()" function
12010 */
12011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012012f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012013 typval_T *argvars;
12014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012016 getwinvar(argvars, rettv, 0);
12017}
12018
12019/*
12020 * getwinvar() and gettabwinvar()
12021 */
12022 static void
12023getwinvar(argvars, rettv, off)
12024 typval_T *argvars;
12025 typval_T *rettv;
12026 int off; /* 1 for gettabwinvar() */
12027{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028 win_T *win, *oldcurwin;
12029 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012030 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012031 tabpage_T *tp = NULL;
12032 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012033 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012035#ifdef FEAT_WINDOWS
12036 if (off == 1)
12037 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12038 else
12039 tp = curtab;
12040#endif
12041 win = find_win_by_nr(&argvars[off], tp);
12042 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012043 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012045 rettv->v_type = VAR_STRING;
12046 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047
12048 if (win != NULL && varname != NULL)
12049 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012050 /* Set curwin to be our win, temporarily. Also set the tabpage,
12051 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012052 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012053
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012055 {
12056 if (get_option_tv(&varname, rettv, 1) == OK)
12057 done = TRUE;
12058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 else
12060 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012061 /* Look up the variable. */
12062 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12063 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012065 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012066 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012067 done = TRUE;
12068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012070
12071 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012072 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 }
12074
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012075 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12076 /* use the default return value */
12077 copy_tv(&argvars[off + 2], rettv);
12078
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079 --emsg_off;
12080}
12081
12082/*
12083 * "glob()" function
12084 */
12085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012086f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012087 typval_T *argvars;
12088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012090 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012092 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012094 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012095 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012096 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012097 if (argvars[1].v_type != VAR_UNKNOWN)
12098 {
12099 if (get_tv_number_chk(&argvars[1], &error))
12100 options |= WILD_KEEP_ALL;
12101 if (argvars[2].v_type != VAR_UNKNOWN
12102 && get_tv_number_chk(&argvars[2], &error))
12103 {
12104 rettv->v_type = VAR_LIST;
12105 rettv->vval.v_list = NULL;
12106 }
12107 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012108 if (!error)
12109 {
12110 ExpandInit(&xpc);
12111 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012112 if (p_wic)
12113 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012114 if (rettv->v_type == VAR_STRING)
12115 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012116 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012117 else if (rettv_list_alloc(rettv) != FAIL)
12118 {
12119 int i;
12120
12121 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12122 NULL, options, WILD_ALL_KEEP);
12123 for (i = 0; i < xpc.xp_numfiles; i++)
12124 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12125
12126 ExpandCleanup(&xpc);
12127 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012128 }
12129 else
12130 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131}
12132
12133/*
12134 * "globpath()" function
12135 */
12136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012137f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012138 typval_T *argvars;
12139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012141 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012143 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012144 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012146 /* When the optional second argument is non-zero, don't remove matches
12147 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12148 if (argvars[2].v_type != VAR_UNKNOWN
12149 && get_tv_number_chk(&argvars[2], &error))
12150 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012151 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012152 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012153 rettv->vval.v_string = NULL;
12154 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012155 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12156 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157}
12158
12159/*
12160 * "has()" function
12161 */
12162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012163f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012164 typval_T *argvars;
12165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166{
12167 int i;
12168 char_u *name;
12169 int n = FALSE;
12170 static char *(has_list[]) =
12171 {
12172#ifdef AMIGA
12173 "amiga",
12174# ifdef FEAT_ARP
12175 "arp",
12176# endif
12177#endif
12178#ifdef __BEOS__
12179 "beos",
12180#endif
12181#ifdef MSDOS
12182# ifdef DJGPP
12183 "dos32",
12184# else
12185 "dos16",
12186# endif
12187#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012188#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189 "mac",
12190#endif
12191#if defined(MACOS_X_UNIX)
12192 "macunix",
12193#endif
12194#ifdef OS2
12195 "os2",
12196#endif
12197#ifdef __QNX__
12198 "qnx",
12199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200#ifdef UNIX
12201 "unix",
12202#endif
12203#ifdef VMS
12204 "vms",
12205#endif
12206#ifdef WIN16
12207 "win16",
12208#endif
12209#ifdef WIN32
12210 "win32",
12211#endif
12212#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12213 "win32unix",
12214#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012215#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216 "win64",
12217#endif
12218#ifdef EBCDIC
12219 "ebcdic",
12220#endif
12221#ifndef CASE_INSENSITIVE_FILENAME
12222 "fname_case",
12223#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012224#ifdef HAVE_ACL
12225 "acl",
12226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227#ifdef FEAT_ARABIC
12228 "arabic",
12229#endif
12230#ifdef FEAT_AUTOCMD
12231 "autocmd",
12232#endif
12233#ifdef FEAT_BEVAL
12234 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012235# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12236 "balloon_multiline",
12237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238#endif
12239#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12240 "builtin_terms",
12241# ifdef ALL_BUILTIN_TCAPS
12242 "all_builtin_terms",
12243# endif
12244#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012245#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12246 || defined(FEAT_GUI_W32) \
12247 || defined(FEAT_GUI_MOTIF))
12248 "browsefilter",
12249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250#ifdef FEAT_BYTEOFF
12251 "byte_offset",
12252#endif
12253#ifdef FEAT_CINDENT
12254 "cindent",
12255#endif
12256#ifdef FEAT_CLIENTSERVER
12257 "clientserver",
12258#endif
12259#ifdef FEAT_CLIPBOARD
12260 "clipboard",
12261#endif
12262#ifdef FEAT_CMDL_COMPL
12263 "cmdline_compl",
12264#endif
12265#ifdef FEAT_CMDHIST
12266 "cmdline_hist",
12267#endif
12268#ifdef FEAT_COMMENTS
12269 "comments",
12270#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012271#ifdef FEAT_CONCEAL
12272 "conceal",
12273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274#ifdef FEAT_CRYPT
12275 "cryptv",
12276#endif
12277#ifdef FEAT_CSCOPE
12278 "cscope",
12279#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012280#ifdef FEAT_CURSORBIND
12281 "cursorbind",
12282#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012283#ifdef CURSOR_SHAPE
12284 "cursorshape",
12285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286#ifdef DEBUG
12287 "debug",
12288#endif
12289#ifdef FEAT_CON_DIALOG
12290 "dialog_con",
12291#endif
12292#ifdef FEAT_GUI_DIALOG
12293 "dialog_gui",
12294#endif
12295#ifdef FEAT_DIFF
12296 "diff",
12297#endif
12298#ifdef FEAT_DIGRAPHS
12299 "digraphs",
12300#endif
12301#ifdef FEAT_DND
12302 "dnd",
12303#endif
12304#ifdef FEAT_EMACS_TAGS
12305 "emacs_tags",
12306#endif
12307 "eval", /* always present, of course! */
12308#ifdef FEAT_EX_EXTRA
12309 "ex_extra",
12310#endif
12311#ifdef FEAT_SEARCH_EXTRA
12312 "extra_search",
12313#endif
12314#ifdef FEAT_FKMAP
12315 "farsi",
12316#endif
12317#ifdef FEAT_SEARCHPATH
12318 "file_in_path",
12319#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012320#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012321 "filterpipe",
12322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323#ifdef FEAT_FIND_ID
12324 "find_in_path",
12325#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012326#ifdef FEAT_FLOAT
12327 "float",
12328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329#ifdef FEAT_FOLDING
12330 "folding",
12331#endif
12332#ifdef FEAT_FOOTER
12333 "footer",
12334#endif
12335#if !defined(USE_SYSTEM) && defined(UNIX)
12336 "fork",
12337#endif
12338#ifdef FEAT_GETTEXT
12339 "gettext",
12340#endif
12341#ifdef FEAT_GUI
12342 "gui",
12343#endif
12344#ifdef FEAT_GUI_ATHENA
12345# ifdef FEAT_GUI_NEXTAW
12346 "gui_neXtaw",
12347# else
12348 "gui_athena",
12349# endif
12350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351#ifdef FEAT_GUI_GTK
12352 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012355#ifdef FEAT_GUI_GNOME
12356 "gui_gnome",
12357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358#ifdef FEAT_GUI_MAC
12359 "gui_mac",
12360#endif
12361#ifdef FEAT_GUI_MOTIF
12362 "gui_motif",
12363#endif
12364#ifdef FEAT_GUI_PHOTON
12365 "gui_photon",
12366#endif
12367#ifdef FEAT_GUI_W16
12368 "gui_win16",
12369#endif
12370#ifdef FEAT_GUI_W32
12371 "gui_win32",
12372#endif
12373#ifdef FEAT_HANGULIN
12374 "hangul_input",
12375#endif
12376#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12377 "iconv",
12378#endif
12379#ifdef FEAT_INS_EXPAND
12380 "insert_expand",
12381#endif
12382#ifdef FEAT_JUMPLIST
12383 "jumplist",
12384#endif
12385#ifdef FEAT_KEYMAP
12386 "keymap",
12387#endif
12388#ifdef FEAT_LANGMAP
12389 "langmap",
12390#endif
12391#ifdef FEAT_LIBCALL
12392 "libcall",
12393#endif
12394#ifdef FEAT_LINEBREAK
12395 "linebreak",
12396#endif
12397#ifdef FEAT_LISP
12398 "lispindent",
12399#endif
12400#ifdef FEAT_LISTCMDS
12401 "listcmds",
12402#endif
12403#ifdef FEAT_LOCALMAP
12404 "localmap",
12405#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012406#ifdef FEAT_LUA
12407# ifndef DYNAMIC_LUA
12408 "lua",
12409# endif
12410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411#ifdef FEAT_MENU
12412 "menu",
12413#endif
12414#ifdef FEAT_SESSION
12415 "mksession",
12416#endif
12417#ifdef FEAT_MODIFY_FNAME
12418 "modify_fname",
12419#endif
12420#ifdef FEAT_MOUSE
12421 "mouse",
12422#endif
12423#ifdef FEAT_MOUSESHAPE
12424 "mouseshape",
12425#endif
12426#if defined(UNIX) || defined(VMS)
12427# ifdef FEAT_MOUSE_DEC
12428 "mouse_dec",
12429# endif
12430# ifdef FEAT_MOUSE_GPM
12431 "mouse_gpm",
12432# endif
12433# ifdef FEAT_MOUSE_JSB
12434 "mouse_jsbterm",
12435# endif
12436# ifdef FEAT_MOUSE_NET
12437 "mouse_netterm",
12438# endif
12439# ifdef FEAT_MOUSE_PTERM
12440 "mouse_pterm",
12441# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012442# ifdef FEAT_MOUSE_SGR
12443 "mouse_sgr",
12444# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012445# ifdef FEAT_SYSMOUSE
12446 "mouse_sysmouse",
12447# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012448# ifdef FEAT_MOUSE_URXVT
12449 "mouse_urxvt",
12450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451# ifdef FEAT_MOUSE_XTERM
12452 "mouse_xterm",
12453# endif
12454#endif
12455#ifdef FEAT_MBYTE
12456 "multi_byte",
12457#endif
12458#ifdef FEAT_MBYTE_IME
12459 "multi_byte_ime",
12460#endif
12461#ifdef FEAT_MULTI_LANG
12462 "multi_lang",
12463#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012464#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012465#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012466 "mzscheme",
12467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469#ifdef FEAT_OLE
12470 "ole",
12471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472#ifdef FEAT_PATH_EXTRA
12473 "path_extra",
12474#endif
12475#ifdef FEAT_PERL
12476#ifndef DYNAMIC_PERL
12477 "perl",
12478#endif
12479#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012480#ifdef FEAT_PERSISTENT_UNDO
12481 "persistent_undo",
12482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483#ifdef FEAT_PYTHON
12484#ifndef DYNAMIC_PYTHON
12485 "python",
12486#endif
12487#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012488#ifdef FEAT_PYTHON3
12489#ifndef DYNAMIC_PYTHON3
12490 "python3",
12491#endif
12492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493#ifdef FEAT_POSTSCRIPT
12494 "postscript",
12495#endif
12496#ifdef FEAT_PRINTER
12497 "printer",
12498#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012499#ifdef FEAT_PROFILE
12500 "profile",
12501#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012502#ifdef FEAT_RELTIME
12503 "reltime",
12504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505#ifdef FEAT_QUICKFIX
12506 "quickfix",
12507#endif
12508#ifdef FEAT_RIGHTLEFT
12509 "rightleft",
12510#endif
12511#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12512 "ruby",
12513#endif
12514#ifdef FEAT_SCROLLBIND
12515 "scrollbind",
12516#endif
12517#ifdef FEAT_CMDL_INFO
12518 "showcmd",
12519 "cmdline_info",
12520#endif
12521#ifdef FEAT_SIGNS
12522 "signs",
12523#endif
12524#ifdef FEAT_SMARTINDENT
12525 "smartindent",
12526#endif
12527#ifdef FEAT_SNIFF
12528 "sniff",
12529#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012530#ifdef STARTUPTIME
12531 "startuptime",
12532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533#ifdef FEAT_STL_OPT
12534 "statusline",
12535#endif
12536#ifdef FEAT_SUN_WORKSHOP
12537 "sun_workshop",
12538#endif
12539#ifdef FEAT_NETBEANS_INTG
12540 "netbeans_intg",
12541#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012542#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012543 "spell",
12544#endif
12545#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546 "syntax",
12547#endif
12548#if defined(USE_SYSTEM) || !defined(UNIX)
12549 "system",
12550#endif
12551#ifdef FEAT_TAG_BINS
12552 "tag_binary",
12553#endif
12554#ifdef FEAT_TAG_OLDSTATIC
12555 "tag_old_static",
12556#endif
12557#ifdef FEAT_TAG_ANYWHITE
12558 "tag_any_white",
12559#endif
12560#ifdef FEAT_TCL
12561# ifndef DYNAMIC_TCL
12562 "tcl",
12563# endif
12564#endif
12565#ifdef TERMINFO
12566 "terminfo",
12567#endif
12568#ifdef FEAT_TERMRESPONSE
12569 "termresponse",
12570#endif
12571#ifdef FEAT_TEXTOBJ
12572 "textobjects",
12573#endif
12574#ifdef HAVE_TGETENT
12575 "tgetent",
12576#endif
12577#ifdef FEAT_TITLE
12578 "title",
12579#endif
12580#ifdef FEAT_TOOLBAR
12581 "toolbar",
12582#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012583#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12584 "unnamedplus",
12585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586#ifdef FEAT_USR_CMDS
12587 "user-commands", /* was accidentally included in 5.4 */
12588 "user_commands",
12589#endif
12590#ifdef FEAT_VIMINFO
12591 "viminfo",
12592#endif
12593#ifdef FEAT_VERTSPLIT
12594 "vertsplit",
12595#endif
12596#ifdef FEAT_VIRTUALEDIT
12597 "virtualedit",
12598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600#ifdef FEAT_VISUALEXTRA
12601 "visualextra",
12602#endif
12603#ifdef FEAT_VREPLACE
12604 "vreplace",
12605#endif
12606#ifdef FEAT_WILDIGN
12607 "wildignore",
12608#endif
12609#ifdef FEAT_WILDMENU
12610 "wildmenu",
12611#endif
12612#ifdef FEAT_WINDOWS
12613 "windows",
12614#endif
12615#ifdef FEAT_WAK
12616 "winaltkeys",
12617#endif
12618#ifdef FEAT_WRITEBACKUP
12619 "writebackup",
12620#endif
12621#ifdef FEAT_XIM
12622 "xim",
12623#endif
12624#ifdef FEAT_XFONTSET
12625 "xfontset",
12626#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012627#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012628 "xpm",
12629 "xpm_w32", /* for backward compatibility */
12630#else
12631# if defined(HAVE_XPM)
12632 "xpm",
12633# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635#ifdef USE_XSMP
12636 "xsmp",
12637#endif
12638#ifdef USE_XSMP_INTERACT
12639 "xsmp_interact",
12640#endif
12641#ifdef FEAT_XCLIPBOARD
12642 "xterm_clipboard",
12643#endif
12644#ifdef FEAT_XTERM_SAVE
12645 "xterm_save",
12646#endif
12647#if defined(UNIX) && defined(FEAT_X11)
12648 "X11",
12649#endif
12650 NULL
12651 };
12652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012653 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 for (i = 0; has_list[i] != NULL; ++i)
12655 if (STRICMP(name, has_list[i]) == 0)
12656 {
12657 n = TRUE;
12658 break;
12659 }
12660
12661 if (n == FALSE)
12662 {
12663 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012664 {
12665 if (name[5] == '-'
12666 && STRLEN(name) > 11
12667 && vim_isdigit(name[6])
12668 && vim_isdigit(name[8])
12669 && vim_isdigit(name[10]))
12670 {
12671 int major = atoi((char *)name + 6);
12672 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012673
12674 /* Expect "patch-9.9.01234". */
12675 n = (major < VIM_VERSION_MAJOR
12676 || (major == VIM_VERSION_MAJOR
12677 && (minor < VIM_VERSION_MINOR
12678 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012679 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012680 }
12681 else
12682 n = has_patch(atoi((char *)name + 5));
12683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684 else if (STRICMP(name, "vim_starting") == 0)
12685 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012686#ifdef FEAT_MBYTE
12687 else if (STRICMP(name, "multi_byte_encoding") == 0)
12688 n = has_mbyte;
12689#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012690#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12691 else if (STRICMP(name, "balloon_multiline") == 0)
12692 n = multiline_balloon_available();
12693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694#ifdef DYNAMIC_TCL
12695 else if (STRICMP(name, "tcl") == 0)
12696 n = tcl_enabled(FALSE);
12697#endif
12698#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12699 else if (STRICMP(name, "iconv") == 0)
12700 n = iconv_enabled(FALSE);
12701#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012702#ifdef DYNAMIC_LUA
12703 else if (STRICMP(name, "lua") == 0)
12704 n = lua_enabled(FALSE);
12705#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012706#ifdef DYNAMIC_MZSCHEME
12707 else if (STRICMP(name, "mzscheme") == 0)
12708 n = mzscheme_enabled(FALSE);
12709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710#ifdef DYNAMIC_RUBY
12711 else if (STRICMP(name, "ruby") == 0)
12712 n = ruby_enabled(FALSE);
12713#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012714#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715#ifdef DYNAMIC_PYTHON
12716 else if (STRICMP(name, "python") == 0)
12717 n = python_enabled(FALSE);
12718#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012719#endif
12720#ifdef FEAT_PYTHON3
12721#ifdef DYNAMIC_PYTHON3
12722 else if (STRICMP(name, "python3") == 0)
12723 n = python3_enabled(FALSE);
12724#endif
12725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726#ifdef DYNAMIC_PERL
12727 else if (STRICMP(name, "perl") == 0)
12728 n = perl_enabled(FALSE);
12729#endif
12730#ifdef FEAT_GUI
12731 else if (STRICMP(name, "gui_running") == 0)
12732 n = (gui.in_use || gui.starting);
12733# ifdef FEAT_GUI_W32
12734 else if (STRICMP(name, "gui_win32s") == 0)
12735 n = gui_is_win32s();
12736# endif
12737# ifdef FEAT_BROWSE
12738 else if (STRICMP(name, "browse") == 0)
12739 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12740# endif
12741#endif
12742#ifdef FEAT_SYN_HL
12743 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012744 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745#endif
12746#if defined(WIN3264)
12747 else if (STRICMP(name, "win95") == 0)
12748 n = mch_windows95();
12749#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012750#ifdef FEAT_NETBEANS_INTG
12751 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012752 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 }
12755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012756 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757}
12758
12759/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012760 * "has_key()" function
12761 */
12762 static void
12763f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012764 typval_T *argvars;
12765 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012766{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012767 if (argvars[0].v_type != VAR_DICT)
12768 {
12769 EMSG(_(e_dictreq));
12770 return;
12771 }
12772 if (argvars[0].vval.v_dict == NULL)
12773 return;
12774
12775 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012776 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012777}
12778
12779/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012780 * "haslocaldir()" function
12781 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012782 static void
12783f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012784 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012785 typval_T *rettv;
12786{
12787 rettv->vval.v_number = (curwin->w_localdir != NULL);
12788}
12789
12790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791 * "hasmapto()" function
12792 */
12793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012795 typval_T *argvars;
12796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797{
12798 char_u *name;
12799 char_u *mode;
12800 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012801 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012803 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012804 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 mode = (char_u *)"nvo";
12806 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012807 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012809 if (argvars[2].v_type != VAR_UNKNOWN)
12810 abbr = get_tv_number(&argvars[2]);
12811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812
Bram Moolenaar2c932302006-03-18 21:42:09 +000012813 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817}
12818
12819/*
12820 * "histadd()" function
12821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012824 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826{
12827#ifdef FEAT_CMDHIST
12828 int histype;
12829 char_u *str;
12830 char_u buf[NUMBUFLEN];
12831#endif
12832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834 if (check_restricted() || check_secure())
12835 return;
12836#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012837 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12838 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 if (histype >= 0)
12840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 if (*str != NUL)
12843 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012844 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847 return;
12848 }
12849 }
12850#endif
12851}
12852
12853/*
12854 * "histdel()" function
12855 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012858 typval_T *argvars UNUSED;
12859 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860{
12861#ifdef FEAT_CMDHIST
12862 int n;
12863 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012864 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012866 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12867 if (str == NULL)
12868 n = 0;
12869 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012871 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012872 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012874 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 else
12877 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012878 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 get_tv_string_buf(&argvars[1], buf));
12880 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881#endif
12882}
12883
12884/*
12885 * "histget()" function
12886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012889 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891{
12892#ifdef FEAT_CMDHIST
12893 int type;
12894 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012895 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012897 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12898 if (str == NULL)
12899 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012901 {
12902 type = get_histtype(str);
12903 if (argvars[1].v_type == VAR_UNKNOWN)
12904 idx = get_history_idx(type);
12905 else
12906 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12907 /* -1 on type error */
12908 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012911 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012913 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914}
12915
12916/*
12917 * "histnr()" function
12918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012920f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012921 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923{
12924 int i;
12925
12926#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012927 char_u *history = get_tv_string_chk(&argvars[0]);
12928
12929 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930 if (i >= HIST_CMD && i < HIST_COUNT)
12931 i = get_history_idx(i);
12932 else
12933#endif
12934 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012935 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936}
12937
12938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939 * "highlightID(name)" function
12940 */
12941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012943 typval_T *argvars;
12944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947}
12948
12949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012950 * "highlight_exists()" function
12951 */
12952 static void
12953f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012954 typval_T *argvars;
12955 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012956{
12957 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12958}
12959
12960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 * "hostname()" function
12962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012964f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012965 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967{
12968 char_u hostname[256];
12969
12970 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012971 rettv->v_type = VAR_STRING;
12972 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973}
12974
12975/*
12976 * iconv() function
12977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012979f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012980 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982{
12983#ifdef FEAT_MBYTE
12984 char_u buf1[NUMBUFLEN];
12985 char_u buf2[NUMBUFLEN];
12986 char_u *from, *to, *str;
12987 vimconv_T vimconv;
12988#endif
12989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012990 rettv->v_type = VAR_STRING;
12991 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992
12993#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012994 str = get_tv_string(&argvars[0]);
12995 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12996 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 vimconv.vc_type = CONV_NONE;
12998 convert_setup(&vimconv, from, to);
12999
13000 /* If the encodings are equal, no conversion needed. */
13001 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013002 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013004 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005
13006 convert_setup(&vimconv, NULL, NULL);
13007 vim_free(from);
13008 vim_free(to);
13009#endif
13010}
13011
13012/*
13013 * "indent()" function
13014 */
13015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013016f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013017 typval_T *argvars;
13018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019{
13020 linenr_T lnum;
13021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013022 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013024 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013026 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027}
13028
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013029/*
13030 * "index()" function
13031 */
13032 static void
13033f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013034 typval_T *argvars;
13035 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013036{
Bram Moolenaar33570922005-01-25 22:26:29 +000013037 list_T *l;
13038 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013039 long idx = 0;
13040 int ic = FALSE;
13041
13042 rettv->vval.v_number = -1;
13043 if (argvars[0].v_type != VAR_LIST)
13044 {
13045 EMSG(_(e_listreq));
13046 return;
13047 }
13048 l = argvars[0].vval.v_list;
13049 if (l != NULL)
13050 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013051 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013052 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013053 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013054 int error = FALSE;
13055
Bram Moolenaar758711c2005-02-02 23:11:38 +000013056 /* Start at specified item. Use the cached index that list_find()
13057 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013058 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013059 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013060 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013061 ic = get_tv_number_chk(&argvars[3], &error);
13062 if (error)
13063 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013064 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013065
Bram Moolenaar758711c2005-02-02 23:11:38 +000013066 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013067 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013068 {
13069 rettv->vval.v_number = idx;
13070 break;
13071 }
13072 }
13073}
13074
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075static int inputsecret_flag = 0;
13076
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013077static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13078
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013080 * This function is used by f_input() and f_inputdialog() functions. The third
13081 * argument to f_input() specifies the type of completion to use at the
13082 * prompt. The third argument to f_inputdialog() specifies the value to return
13083 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084 */
13085 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013086get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013087 typval_T *argvars;
13088 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013089 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013091 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092 char_u *p = NULL;
13093 int c;
13094 char_u buf[NUMBUFLEN];
13095 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013096 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013097 int xp_type = EXPAND_NOTHING;
13098 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013100 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013101 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102
13103#ifdef NO_CONSOLE_INPUT
13104 /* While starting up, there is no place to enter text. */
13105 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107#endif
13108
13109 cmd_silent = FALSE; /* Want to see the prompt. */
13110 if (prompt != NULL)
13111 {
13112 /* Only the part of the message after the last NL is considered as
13113 * prompt for the command line */
13114 p = vim_strrchr(prompt, '\n');
13115 if (p == NULL)
13116 p = prompt;
13117 else
13118 {
13119 ++p;
13120 c = *p;
13121 *p = NUL;
13122 msg_start();
13123 msg_clr_eos();
13124 msg_puts_attr(prompt, echo_attr);
13125 msg_didout = FALSE;
13126 msg_starthere();
13127 *p = c;
13128 }
13129 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013131 if (argvars[1].v_type != VAR_UNKNOWN)
13132 {
13133 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13134 if (defstr != NULL)
13135 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013137 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013138 {
13139 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013140 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013141 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013142
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013143 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013144 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013145
Bram Moolenaar4463f292005-09-25 22:20:24 +000013146 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13147 if (xp_name == NULL)
13148 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013149
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013150 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013151
Bram Moolenaar4463f292005-09-25 22:20:24 +000013152 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13153 &xp_arg) == FAIL)
13154 return;
13155 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013156 }
13157
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013158 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013159 {
13160# ifdef FEAT_EX_EXTRA
13161 int save_ex_normal_busy = ex_normal_busy;
13162 ex_normal_busy = 0;
13163# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013164 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013165 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13166 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013167# ifdef FEAT_EX_EXTRA
13168 ex_normal_busy = save_ex_normal_busy;
13169# endif
13170 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013171 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013172 && argvars[1].v_type != VAR_UNKNOWN
13173 && argvars[2].v_type != VAR_UNKNOWN)
13174 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13175 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013176
13177 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013179 /* since the user typed this, no need to wait for return */
13180 need_wait_return = FALSE;
13181 msg_didout = FALSE;
13182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183 cmd_silent = cmd_silent_save;
13184}
13185
13186/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013187 * "input()" function
13188 * Also handles inputsecret() when inputsecret is set.
13189 */
13190 static void
13191f_input(argvars, rettv)
13192 typval_T *argvars;
13193 typval_T *rettv;
13194{
13195 get_user_input(argvars, rettv, FALSE);
13196}
13197
13198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199 * "inputdialog()" function
13200 */
13201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013203 typval_T *argvars;
13204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205{
13206#if defined(FEAT_GUI_TEXTDIALOG)
13207 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13208 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13209 {
13210 char_u *message;
13211 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013212 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013214 message = get_tv_string_chk(&argvars[0]);
13215 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013216 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013217 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218 else
13219 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013220 if (message != NULL && defstr != NULL
13221 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013222 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013223 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 else
13225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013226 if (message != NULL && defstr != NULL
13227 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013228 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013229 rettv->vval.v_string = vim_strsave(
13230 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013232 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235 }
13236 else
13237#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013238 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239}
13240
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013241/*
13242 * "inputlist()" function
13243 */
13244 static void
13245f_inputlist(argvars, rettv)
13246 typval_T *argvars;
13247 typval_T *rettv;
13248{
13249 listitem_T *li;
13250 int selected;
13251 int mouse_used;
13252
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013253#ifdef NO_CONSOLE_INPUT
13254 /* While starting up, there is no place to enter text. */
13255 if (no_console_input())
13256 return;
13257#endif
13258 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13259 {
13260 EMSG2(_(e_listarg), "inputlist()");
13261 return;
13262 }
13263
13264 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013265 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013266 lines_left = Rows; /* avoid more prompt */
13267 msg_scroll = TRUE;
13268 msg_clr_eos();
13269
13270 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13271 {
13272 msg_puts(get_tv_string(&li->li_tv));
13273 msg_putchar('\n');
13274 }
13275
13276 /* Ask for choice. */
13277 selected = prompt_for_number(&mouse_used);
13278 if (mouse_used)
13279 selected -= lines_left;
13280
13281 rettv->vval.v_number = selected;
13282}
13283
13284
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13286
13287/*
13288 * "inputrestore()" function
13289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013291f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013292 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294{
13295 if (ga_userinput.ga_len > 0)
13296 {
13297 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13299 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013300 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301 }
13302 else if (p_verbose > 1)
13303 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013304 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013305 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306 }
13307}
13308
13309/*
13310 * "inputsave()" function
13311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013313f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013314 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013317 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318 if (ga_grow(&ga_userinput, 1) == OK)
13319 {
13320 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13321 + ga_userinput.ga_len);
13322 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013323 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324 }
13325 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013326 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327}
13328
13329/*
13330 * "inputsecret()" function
13331 */
13332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013333f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013334 typval_T *argvars;
13335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336{
13337 ++cmdline_star;
13338 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013339 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 --cmdline_star;
13341 --inputsecret_flag;
13342}
13343
13344/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013345 * "insert()" function
13346 */
13347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013348f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013349 typval_T *argvars;
13350 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013351{
13352 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013353 listitem_T *item;
13354 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013355 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013356
13357 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013358 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013359 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013360 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013361 {
13362 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013363 before = get_tv_number_chk(&argvars[2], &error);
13364 if (error)
13365 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013366
Bram Moolenaar758711c2005-02-02 23:11:38 +000013367 if (before == l->lv_len)
13368 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013369 else
13370 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013371 item = list_find(l, before);
13372 if (item == NULL)
13373 {
13374 EMSGN(_(e_listidx), before);
13375 l = NULL;
13376 }
13377 }
13378 if (l != NULL)
13379 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013380 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013381 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013382 }
13383 }
13384}
13385
13386/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013387 * "invert(expr)" function
13388 */
13389 static void
13390f_invert(argvars, rettv)
13391 typval_T *argvars;
13392 typval_T *rettv;
13393{
13394 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13395}
13396
13397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398 * "isdirectory()" function
13399 */
13400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013401f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013402 typval_T *argvars;
13403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013405 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406}
13407
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013408/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013409 * "islocked()" function
13410 */
13411 static void
13412f_islocked(argvars, rettv)
13413 typval_T *argvars;
13414 typval_T *rettv;
13415{
13416 lval_T lv;
13417 char_u *end;
13418 dictitem_T *di;
13419
13420 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013421 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13422 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013423 if (end != NULL && lv.ll_name != NULL)
13424 {
13425 if (*end != NUL)
13426 EMSG(_(e_trailing));
13427 else
13428 {
13429 if (lv.ll_tv == NULL)
13430 {
13431 if (check_changedtick(lv.ll_name))
13432 rettv->vval.v_number = 1; /* always locked */
13433 else
13434 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013435 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013436 if (di != NULL)
13437 {
13438 /* Consider a variable locked when:
13439 * 1. the variable itself is locked
13440 * 2. the value of the variable is locked.
13441 * 3. the List or Dict value is locked.
13442 */
13443 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13444 || tv_islocked(&di->di_tv));
13445 }
13446 }
13447 }
13448 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013449 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013450 else if (lv.ll_newkey != NULL)
13451 EMSG2(_(e_dictkey), lv.ll_newkey);
13452 else if (lv.ll_list != NULL)
13453 /* List item. */
13454 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13455 else
13456 /* Dictionary item. */
13457 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13458 }
13459 }
13460
13461 clear_lval(&lv);
13462}
13463
Bram Moolenaar33570922005-01-25 22:26:29 +000013464static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013465
13466/*
13467 * Turn a dict into a list:
13468 * "what" == 0: list of keys
13469 * "what" == 1: list of values
13470 * "what" == 2: list of items
13471 */
13472 static void
13473dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013474 typval_T *argvars;
13475 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013476 int what;
13477{
Bram Moolenaar33570922005-01-25 22:26:29 +000013478 list_T *l2;
13479 dictitem_T *di;
13480 hashitem_T *hi;
13481 listitem_T *li;
13482 listitem_T *li2;
13483 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013484 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013485
Bram Moolenaar8c711452005-01-14 21:53:12 +000013486 if (argvars[0].v_type != VAR_DICT)
13487 {
13488 EMSG(_(e_dictreq));
13489 return;
13490 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013491 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013492 return;
13493
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013494 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013495 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013496
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013497 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013498 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013499 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013500 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013501 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013502 --todo;
13503 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013504
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013505 li = listitem_alloc();
13506 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013507 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013508 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013509
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013510 if (what == 0)
13511 {
13512 /* keys() */
13513 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013514 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013515 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13516 }
13517 else if (what == 1)
13518 {
13519 /* values() */
13520 copy_tv(&di->di_tv, &li->li_tv);
13521 }
13522 else
13523 {
13524 /* items() */
13525 l2 = list_alloc();
13526 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013527 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013528 li->li_tv.vval.v_list = l2;
13529 if (l2 == NULL)
13530 break;
13531 ++l2->lv_refcount;
13532
13533 li2 = listitem_alloc();
13534 if (li2 == NULL)
13535 break;
13536 list_append(l2, li2);
13537 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013538 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013539 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13540
13541 li2 = listitem_alloc();
13542 if (li2 == NULL)
13543 break;
13544 list_append(l2, li2);
13545 copy_tv(&di->di_tv, &li2->li_tv);
13546 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013547 }
13548 }
13549}
13550
13551/*
13552 * "items(dict)" function
13553 */
13554 static void
13555f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013556 typval_T *argvars;
13557 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013558{
13559 dict_list(argvars, rettv, 2);
13560}
13561
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013563 * "join()" function
13564 */
13565 static void
13566f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013567 typval_T *argvars;
13568 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013569{
13570 garray_T ga;
13571 char_u *sep;
13572
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013573 if (argvars[0].v_type != VAR_LIST)
13574 {
13575 EMSG(_(e_listreq));
13576 return;
13577 }
13578 if (argvars[0].vval.v_list == NULL)
13579 return;
13580 if (argvars[1].v_type == VAR_UNKNOWN)
13581 sep = (char_u *)" ";
13582 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013583 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013584
13585 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013586
13587 if (sep != NULL)
13588 {
13589 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013590 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013591 ga_append(&ga, NUL);
13592 rettv->vval.v_string = (char_u *)ga.ga_data;
13593 }
13594 else
13595 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013596}
13597
13598/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013599 * "keys()" function
13600 */
13601 static void
13602f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013603 typval_T *argvars;
13604 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013605{
13606 dict_list(argvars, rettv, 0);
13607}
13608
13609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610 * "last_buffer_nr()" function.
13611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013614 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616{
13617 int n = 0;
13618 buf_T *buf;
13619
13620 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13621 if (n < buf->b_fnum)
13622 n = buf->b_fnum;
13623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013625}
13626
13627/*
13628 * "len()" function
13629 */
13630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013632 typval_T *argvars;
13633 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013634{
13635 switch (argvars[0].v_type)
13636 {
13637 case VAR_STRING:
13638 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639 rettv->vval.v_number = (varnumber_T)STRLEN(
13640 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013641 break;
13642 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013643 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013644 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013645 case VAR_DICT:
13646 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13647 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013648 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013649 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013650 break;
13651 }
13652}
13653
Bram Moolenaar33570922005-01-25 22:26:29 +000013654static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013655
13656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013657libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013658 typval_T *argvars;
13659 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013660 int type;
13661{
13662#ifdef FEAT_LIBCALL
13663 char_u *string_in;
13664 char_u **string_result;
13665 int nr_result;
13666#endif
13667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013669 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013671
13672 if (check_restricted() || check_secure())
13673 return;
13674
13675#ifdef FEAT_LIBCALL
13676 /* The first two args must be strings, otherwise its meaningless */
13677 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13678 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013679 string_in = NULL;
13680 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013681 string_in = argvars[2].vval.v_string;
13682 if (type == VAR_NUMBER)
13683 string_result = NULL;
13684 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013685 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013686 if (mch_libcall(argvars[0].vval.v_string,
13687 argvars[1].vval.v_string,
13688 string_in,
13689 argvars[2].vval.v_number,
13690 string_result,
13691 &nr_result) == OK
13692 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013693 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013694 }
13695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696}
13697
13698/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013699 * "libcall()" function
13700 */
13701 static void
13702f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013703 typval_T *argvars;
13704 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013705{
13706 libcall_common(argvars, rettv, VAR_STRING);
13707}
13708
13709/*
13710 * "libcallnr()" function
13711 */
13712 static void
13713f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013714 typval_T *argvars;
13715 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013716{
13717 libcall_common(argvars, rettv, VAR_NUMBER);
13718}
13719
13720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 * "line(string)" function
13722 */
13723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013724f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013725 typval_T *argvars;
13726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727{
13728 linenr_T lnum = 0;
13729 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013730 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013732 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733 if (fp != NULL)
13734 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013735 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736}
13737
13738/*
13739 * "line2byte(lnum)" function
13740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013742f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013743 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745{
13746#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013747 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748#else
13749 linenr_T lnum;
13750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013751 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13756 if (rettv->vval.v_number >= 0)
13757 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758#endif
13759}
13760
13761/*
13762 * "lispindent(lnum)" function
13763 */
13764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013765f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013766 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768{
13769#ifdef FEAT_LISP
13770 pos_T pos;
13771 linenr_T lnum;
13772
13773 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013774 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13776 {
13777 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013778 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 curwin->w_cursor = pos;
13780 }
13781 else
13782#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013783 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784}
13785
13786/*
13787 * "localtime()" function
13788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013790f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013791 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013794 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795}
13796
Bram Moolenaar33570922005-01-25 22:26:29 +000013797static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798
13799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013800get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013801 typval_T *argvars;
13802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803 int exact;
13804{
13805 char_u *keys;
13806 char_u *which;
13807 char_u buf[NUMBUFLEN];
13808 char_u *keys_buf = NULL;
13809 char_u *rhs;
13810 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013811 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013812 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013813 mapblock_T *mp;
13814 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815
13816 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013817 rettv->v_type = VAR_STRING;
13818 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013820 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 if (*keys == NUL)
13822 return;
13823
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013824 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013826 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013827 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013828 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013829 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013830 if (argvars[3].v_type != VAR_UNKNOWN)
13831 get_dict = get_tv_number(&argvars[3]);
13832 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 else
13835 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013836 if (which == NULL)
13837 return;
13838
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 mode = get_map_mode(&which, 0);
13840
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013841 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013842 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013844
13845 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013847 /* Return a string. */
13848 if (rhs != NULL)
13849 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850
Bram Moolenaarbd743252010-10-20 21:23:33 +020013851 }
13852 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13853 {
13854 /* Return a dictionary. */
13855 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13856 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13857 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858
Bram Moolenaarbd743252010-10-20 21:23:33 +020013859 dict_add_nr_str(dict, "lhs", 0L, lhs);
13860 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13861 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13862 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13863 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13864 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13865 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013866 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013867 dict_add_nr_str(dict, "mode", 0L, mapmode);
13868
13869 vim_free(lhs);
13870 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 }
13872}
13873
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013874#ifdef FEAT_FLOAT
13875/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013876 * "log()" function
13877 */
13878 static void
13879f_log(argvars, rettv)
13880 typval_T *argvars;
13881 typval_T *rettv;
13882{
13883 float_T f;
13884
13885 rettv->v_type = VAR_FLOAT;
13886 if (get_float_arg(argvars, &f) == OK)
13887 rettv->vval.v_float = log(f);
13888 else
13889 rettv->vval.v_float = 0.0;
13890}
13891
13892/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013893 * "log10()" function
13894 */
13895 static void
13896f_log10(argvars, rettv)
13897 typval_T *argvars;
13898 typval_T *rettv;
13899{
13900 float_T f;
13901
13902 rettv->v_type = VAR_FLOAT;
13903 if (get_float_arg(argvars, &f) == OK)
13904 rettv->vval.v_float = log10(f);
13905 else
13906 rettv->vval.v_float = 0.0;
13907}
13908#endif
13909
Bram Moolenaar1dced572012-04-05 16:54:08 +020013910#ifdef FEAT_LUA
13911/*
13912 * "luaeval()" function
13913 */
13914 static void
13915f_luaeval(argvars, rettv)
13916 typval_T *argvars;
13917 typval_T *rettv;
13918{
13919 char_u *str;
13920 char_u buf[NUMBUFLEN];
13921
13922 str = get_tv_string_buf(&argvars[0], buf);
13923 do_luaeval(str, argvars + 1, rettv);
13924}
13925#endif
13926
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013928 * "map()" function
13929 */
13930 static void
13931f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013932 typval_T *argvars;
13933 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013934{
13935 filter_map(argvars, rettv, TRUE);
13936}
13937
13938/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013939 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940 */
13941 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013942f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013943 typval_T *argvars;
13944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013946 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947}
13948
13949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013950 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 */
13952 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013953f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013954 typval_T *argvars;
13955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013957 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958}
13959
Bram Moolenaar33570922005-01-25 22:26:29 +000013960static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961
13962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013963find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013964 typval_T *argvars;
13965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966 int type;
13967{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013968 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013969 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013970 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 char_u *pat;
13972 regmatch_T regmatch;
13973 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013974 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975 char_u *save_cpo;
13976 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013977 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013978 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013979 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013980 list_T *l = NULL;
13981 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013982 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013983 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984
13985 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13986 save_cpo = p_cpo;
13987 p_cpo = (char_u *)"";
13988
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013989 rettv->vval.v_number = -1;
13990 if (type == 3)
13991 {
13992 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013993 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013994 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013995 }
13996 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013998 rettv->v_type = VAR_STRING;
13999 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014002 if (argvars[0].v_type == VAR_LIST)
14003 {
14004 if ((l = argvars[0].vval.v_list) == NULL)
14005 goto theend;
14006 li = l->lv_first;
14007 }
14008 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014009 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014010 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014011 len = (long)STRLEN(str);
14012 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014013
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014014 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14015 if (pat == NULL)
14016 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014017
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014018 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014020 int error = FALSE;
14021
14022 start = get_tv_number_chk(&argvars[2], &error);
14023 if (error)
14024 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014025 if (l != NULL)
14026 {
14027 li = list_find(l, start);
14028 if (li == NULL)
14029 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014030 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014031 }
14032 else
14033 {
14034 if (start < 0)
14035 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014036 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014037 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014038 /* When "count" argument is there ignore matches before "start",
14039 * otherwise skip part of the string. Differs when pattern is "^"
14040 * or "\<". */
14041 if (argvars[3].v_type != VAR_UNKNOWN)
14042 startcol = start;
14043 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014044 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014045 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014046 len -= start;
14047 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014048 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014049
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014050 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014051 nth = get_tv_number_chk(&argvars[3], &error);
14052 if (error)
14053 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 }
14055
14056 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14057 if (regmatch.regprog != NULL)
14058 {
14059 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014060
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014061 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014062 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014063 if (l != NULL)
14064 {
14065 if (li == NULL)
14066 {
14067 match = FALSE;
14068 break;
14069 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014070 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014071 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014072 if (str == NULL)
14073 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014074 }
14075
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014076 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014077
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014078 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014079 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014080 if (l == NULL && !match)
14081 break;
14082
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014083 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014084 if (l != NULL)
14085 {
14086 li = li->li_next;
14087 ++idx;
14088 }
14089 else
14090 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014091#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014092 startcol = (colnr_T)(regmatch.startp[0]
14093 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014094#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014095 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014096#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014097 if (startcol > (colnr_T)len
14098 || str + startcol <= regmatch.startp[0])
14099 {
14100 match = FALSE;
14101 break;
14102 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014103 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014104 }
14105
14106 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014108 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014109 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014110 int i;
14111
14112 /* return list with matched string and submatches */
14113 for (i = 0; i < NSUBEXP; ++i)
14114 {
14115 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014116 {
14117 if (list_append_string(rettv->vval.v_list,
14118 (char_u *)"", 0) == FAIL)
14119 break;
14120 }
14121 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014122 regmatch.startp[i],
14123 (int)(regmatch.endp[i] - regmatch.startp[i]))
14124 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014125 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014126 }
14127 }
14128 else if (type == 2)
14129 {
14130 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014131 if (l != NULL)
14132 copy_tv(&li->li_tv, rettv);
14133 else
14134 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014136 }
14137 else if (l != NULL)
14138 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139 else
14140 {
14141 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014142 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 (varnumber_T)(regmatch.startp[0] - str);
14144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014145 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014147 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148 }
14149 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014150 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151 }
14152
14153theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014154 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 p_cpo = save_cpo;
14156}
14157
14158/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014159 * "match()" function
14160 */
14161 static void
14162f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014163 typval_T *argvars;
14164 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014165{
14166 find_some_match(argvars, rettv, 1);
14167}
14168
14169/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014170 * "matchadd()" function
14171 */
14172 static void
14173f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014174 typval_T *argvars UNUSED;
14175 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014176{
14177#ifdef FEAT_SEARCH_EXTRA
14178 char_u buf[NUMBUFLEN];
14179 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14180 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14181 int prio = 10; /* default priority */
14182 int id = -1;
14183 int error = FALSE;
14184
14185 rettv->vval.v_number = -1;
14186
14187 if (grp == NULL || pat == NULL)
14188 return;
14189 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014190 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014191 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014192 if (argvars[3].v_type != VAR_UNKNOWN)
14193 id = get_tv_number_chk(&argvars[3], &error);
14194 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014195 if (error == TRUE)
14196 return;
14197 if (id >= 1 && id <= 3)
14198 {
14199 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14200 return;
14201 }
14202
14203 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14204#endif
14205}
14206
14207/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014208 * "matcharg()" function
14209 */
14210 static void
14211f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014212 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014213 typval_T *rettv;
14214{
14215 if (rettv_list_alloc(rettv) == OK)
14216 {
14217#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014218 int id = get_tv_number(&argvars[0]);
14219 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014220
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014221 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014222 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014223 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14224 {
14225 list_append_string(rettv->vval.v_list,
14226 syn_id2name(m->hlg_id), -1);
14227 list_append_string(rettv->vval.v_list, m->pattern, -1);
14228 }
14229 else
14230 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014231 list_append_string(rettv->vval.v_list, NULL, -1);
14232 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014233 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014234 }
14235#endif
14236 }
14237}
14238
14239/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014240 * "matchdelete()" function
14241 */
14242 static void
14243f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014244 typval_T *argvars UNUSED;
14245 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014246{
14247#ifdef FEAT_SEARCH_EXTRA
14248 rettv->vval.v_number = match_delete(curwin,
14249 (int)get_tv_number(&argvars[0]), TRUE);
14250#endif
14251}
14252
14253/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014254 * "matchend()" function
14255 */
14256 static void
14257f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014258 typval_T *argvars;
14259 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014260{
14261 find_some_match(argvars, rettv, 0);
14262}
14263
14264/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014265 * "matchlist()" function
14266 */
14267 static void
14268f_matchlist(argvars, rettv)
14269 typval_T *argvars;
14270 typval_T *rettv;
14271{
14272 find_some_match(argvars, rettv, 3);
14273}
14274
14275/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014276 * "matchstr()" function
14277 */
14278 static void
14279f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014280 typval_T *argvars;
14281 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014282{
14283 find_some_match(argvars, rettv, 2);
14284}
14285
Bram Moolenaar33570922005-01-25 22:26:29 +000014286static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014287
14288 static void
14289max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014290 typval_T *argvars;
14291 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014292 int domax;
14293{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014294 long n = 0;
14295 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014296 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014297
14298 if (argvars[0].v_type == VAR_LIST)
14299 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014300 list_T *l;
14301 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014302
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014303 l = argvars[0].vval.v_list;
14304 if (l != NULL)
14305 {
14306 li = l->lv_first;
14307 if (li != NULL)
14308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014309 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014310 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014311 {
14312 li = li->li_next;
14313 if (li == NULL)
14314 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014315 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014316 if (domax ? i > n : i < n)
14317 n = i;
14318 }
14319 }
14320 }
14321 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014322 else if (argvars[0].v_type == VAR_DICT)
14323 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014324 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014325 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014326 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014327 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014328
14329 d = argvars[0].vval.v_dict;
14330 if (d != NULL)
14331 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014332 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014333 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014334 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014335 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014336 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014337 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014338 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014339 if (first)
14340 {
14341 n = i;
14342 first = FALSE;
14343 }
14344 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014345 n = i;
14346 }
14347 }
14348 }
14349 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014350 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014351 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014352 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014353}
14354
14355/*
14356 * "max()" function
14357 */
14358 static void
14359f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014360 typval_T *argvars;
14361 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014362{
14363 max_min(argvars, rettv, TRUE);
14364}
14365
14366/*
14367 * "min()" function
14368 */
14369 static void
14370f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014371 typval_T *argvars;
14372 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014373{
14374 max_min(argvars, rettv, FALSE);
14375}
14376
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014377static int mkdir_recurse __ARGS((char_u *dir, int prot));
14378
14379/*
14380 * Create the directory in which "dir" is located, and higher levels when
14381 * needed.
14382 */
14383 static int
14384mkdir_recurse(dir, prot)
14385 char_u *dir;
14386 int prot;
14387{
14388 char_u *p;
14389 char_u *updir;
14390 int r = FAIL;
14391
14392 /* Get end of directory name in "dir".
14393 * We're done when it's "/" or "c:/". */
14394 p = gettail_sep(dir);
14395 if (p <= get_past_head(dir))
14396 return OK;
14397
14398 /* If the directory exists we're done. Otherwise: create it.*/
14399 updir = vim_strnsave(dir, (int)(p - dir));
14400 if (updir == NULL)
14401 return FAIL;
14402 if (mch_isdir(updir))
14403 r = OK;
14404 else if (mkdir_recurse(updir, prot) == OK)
14405 r = vim_mkdir_emsg(updir, prot);
14406 vim_free(updir);
14407 return r;
14408}
14409
14410#ifdef vim_mkdir
14411/*
14412 * "mkdir()" function
14413 */
14414 static void
14415f_mkdir(argvars, rettv)
14416 typval_T *argvars;
14417 typval_T *rettv;
14418{
14419 char_u *dir;
14420 char_u buf[NUMBUFLEN];
14421 int prot = 0755;
14422
14423 rettv->vval.v_number = FAIL;
14424 if (check_restricted() || check_secure())
14425 return;
14426
14427 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014428 if (*dir == NUL)
14429 rettv->vval.v_number = FAIL;
14430 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014431 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014432 if (*gettail(dir) == NUL)
14433 /* remove trailing slashes */
14434 *gettail_sep(dir) = NUL;
14435
14436 if (argvars[1].v_type != VAR_UNKNOWN)
14437 {
14438 if (argvars[2].v_type != VAR_UNKNOWN)
14439 prot = get_tv_number_chk(&argvars[2], NULL);
14440 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14441 mkdir_recurse(dir, prot);
14442 }
14443 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014444 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014445}
14446#endif
14447
Bram Moolenaar0d660222005-01-07 21:51:51 +000014448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449 * "mode()" function
14450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014452f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014453 typval_T *argvars;
14454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014456 char_u buf[3];
14457
14458 buf[1] = NUL;
14459 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461 if (VIsual_active)
14462 {
14463 if (VIsual_select)
14464 buf[0] = VIsual_mode + 's' - 'v';
14465 else
14466 buf[0] = VIsual_mode;
14467 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014468 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014469 || State == CONFIRM)
14470 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014472 if (State == ASKMORE)
14473 buf[1] = 'm';
14474 else if (State == CONFIRM)
14475 buf[1] = '?';
14476 }
14477 else if (State == EXTERNCMD)
14478 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 else if (State & INSERT)
14480 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014481#ifdef FEAT_VREPLACE
14482 if (State & VREPLACE_FLAG)
14483 {
14484 buf[0] = 'R';
14485 buf[1] = 'v';
14486 }
14487 else
14488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 if (State & REPLACE_FLAG)
14490 buf[0] = 'R';
14491 else
14492 buf[0] = 'i';
14493 }
14494 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014497 if (exmode_active)
14498 buf[1] = 'v';
14499 }
14500 else if (exmode_active)
14501 {
14502 buf[0] = 'c';
14503 buf[1] = 'e';
14504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014508 if (finish_op)
14509 buf[1] = 'o';
14510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014512 /* Clear out the minor mode when the argument is not a non-zero number or
14513 * non-empty string. */
14514 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014515 buf[1] = NUL;
14516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014517 rettv->vval.v_string = vim_strsave(buf);
14518 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519}
14520
Bram Moolenaar429fa852013-04-15 12:27:36 +020014521#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014522/*
14523 * "mzeval()" function
14524 */
14525 static void
14526f_mzeval(argvars, rettv)
14527 typval_T *argvars;
14528 typval_T *rettv;
14529{
14530 char_u *str;
14531 char_u buf[NUMBUFLEN];
14532
14533 str = get_tv_string_buf(&argvars[0], buf);
14534 do_mzeval(str, rettv);
14535}
Bram Moolenaar75676462013-01-30 14:55:42 +010014536
14537 void
14538mzscheme_call_vim(name, args, rettv)
14539 char_u *name;
14540 typval_T *args;
14541 typval_T *rettv;
14542{
14543 typval_T argvars[3];
14544
14545 argvars[0].v_type = VAR_STRING;
14546 argvars[0].vval.v_string = name;
14547 copy_tv(args, &argvars[1]);
14548 argvars[2].v_type = VAR_UNKNOWN;
14549 f_call(argvars, rettv);
14550 clear_tv(&argvars[1]);
14551}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014552#endif
14553
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014555 * "nextnonblank()" function
14556 */
14557 static void
14558f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014559 typval_T *argvars;
14560 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014561{
14562 linenr_T lnum;
14563
14564 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14565 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014566 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014567 {
14568 lnum = 0;
14569 break;
14570 }
14571 if (*skipwhite(ml_get(lnum)) != NUL)
14572 break;
14573 }
14574 rettv->vval.v_number = lnum;
14575}
14576
14577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 * "nr2char()" function
14579 */
14580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014581f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014582 typval_T *argvars;
14583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584{
14585 char_u buf[NUMBUFLEN];
14586
14587#ifdef FEAT_MBYTE
14588 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014589 {
14590 int utf8 = 0;
14591
14592 if (argvars[1].v_type != VAR_UNKNOWN)
14593 utf8 = get_tv_number_chk(&argvars[1], NULL);
14594 if (utf8)
14595 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14596 else
14597 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599 else
14600#endif
14601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014602 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603 buf[1] = NUL;
14604 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014605 rettv->v_type = VAR_STRING;
14606 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014607}
14608
14609/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014610 * "or(expr, expr)" function
14611 */
14612 static void
14613f_or(argvars, rettv)
14614 typval_T *argvars;
14615 typval_T *rettv;
14616{
14617 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14618 | get_tv_number_chk(&argvars[1], NULL);
14619}
14620
14621/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014622 * "pathshorten()" function
14623 */
14624 static void
14625f_pathshorten(argvars, rettv)
14626 typval_T *argvars;
14627 typval_T *rettv;
14628{
14629 char_u *p;
14630
14631 rettv->v_type = VAR_STRING;
14632 p = get_tv_string_chk(&argvars[0]);
14633 if (p == NULL)
14634 rettv->vval.v_string = NULL;
14635 else
14636 {
14637 p = vim_strsave(p);
14638 rettv->vval.v_string = p;
14639 if (p != NULL)
14640 shorten_dir(p);
14641 }
14642}
14643
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014644#ifdef FEAT_FLOAT
14645/*
14646 * "pow()" function
14647 */
14648 static void
14649f_pow(argvars, rettv)
14650 typval_T *argvars;
14651 typval_T *rettv;
14652{
14653 float_T fx, fy;
14654
14655 rettv->v_type = VAR_FLOAT;
14656 if (get_float_arg(argvars, &fx) == OK
14657 && get_float_arg(&argvars[1], &fy) == OK)
14658 rettv->vval.v_float = pow(fx, fy);
14659 else
14660 rettv->vval.v_float = 0.0;
14661}
14662#endif
14663
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014664/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014665 * "prevnonblank()" function
14666 */
14667 static void
14668f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014669 typval_T *argvars;
14670 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014671{
14672 linenr_T lnum;
14673
14674 lnum = get_tv_lnum(argvars);
14675 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14676 lnum = 0;
14677 else
14678 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14679 --lnum;
14680 rettv->vval.v_number = lnum;
14681}
14682
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014683#ifdef HAVE_STDARG_H
14684/* This dummy va_list is here because:
14685 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14686 * - locally in the function results in a "used before set" warning
14687 * - using va_start() to initialize it gives "function with fixed args" error */
14688static va_list ap;
14689#endif
14690
Bram Moolenaar8c711452005-01-14 21:53:12 +000014691/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014692 * "printf()" function
14693 */
14694 static void
14695f_printf(argvars, rettv)
14696 typval_T *argvars;
14697 typval_T *rettv;
14698{
14699 rettv->v_type = VAR_STRING;
14700 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014701#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014702 {
14703 char_u buf[NUMBUFLEN];
14704 int len;
14705 char_u *s;
14706 int saved_did_emsg = did_emsg;
14707 char *fmt;
14708
14709 /* Get the required length, allocate the buffer and do it for real. */
14710 did_emsg = FALSE;
14711 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014712 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014713 if (!did_emsg)
14714 {
14715 s = alloc(len + 1);
14716 if (s != NULL)
14717 {
14718 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014719 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014720 }
14721 }
14722 did_emsg |= saved_did_emsg;
14723 }
14724#endif
14725}
14726
14727/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014728 * "pumvisible()" function
14729 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014730 static void
14731f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014732 typval_T *argvars UNUSED;
14733 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014734{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014735#ifdef FEAT_INS_EXPAND
14736 if (pum_visible())
14737 rettv->vval.v_number = 1;
14738#endif
14739}
14740
Bram Moolenaardb913952012-06-29 12:54:53 +020014741#ifdef FEAT_PYTHON3
14742/*
14743 * "py3eval()" function
14744 */
14745 static void
14746f_py3eval(argvars, rettv)
14747 typval_T *argvars;
14748 typval_T *rettv;
14749{
14750 char_u *str;
14751 char_u buf[NUMBUFLEN];
14752
14753 str = get_tv_string_buf(&argvars[0], buf);
14754 do_py3eval(str, rettv);
14755}
14756#endif
14757
14758#ifdef FEAT_PYTHON
14759/*
14760 * "pyeval()" function
14761 */
14762 static void
14763f_pyeval(argvars, rettv)
14764 typval_T *argvars;
14765 typval_T *rettv;
14766{
14767 char_u *str;
14768 char_u buf[NUMBUFLEN];
14769
14770 str = get_tv_string_buf(&argvars[0], buf);
14771 do_pyeval(str, rettv);
14772}
14773#endif
14774
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014775/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014776 * "range()" function
14777 */
14778 static void
14779f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014780 typval_T *argvars;
14781 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014782{
14783 long start;
14784 long end;
14785 long stride = 1;
14786 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014787 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014789 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014790 if (argvars[1].v_type == VAR_UNKNOWN)
14791 {
14792 end = start - 1;
14793 start = 0;
14794 }
14795 else
14796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014797 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014798 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014799 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014800 }
14801
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014802 if (error)
14803 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014804 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014805 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014806 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014807 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014808 else
14809 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014810 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014811 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014812 if (list_append_number(rettv->vval.v_list,
14813 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014814 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014815 }
14816}
14817
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014818/*
14819 * "readfile()" function
14820 */
14821 static void
14822f_readfile(argvars, rettv)
14823 typval_T *argvars;
14824 typval_T *rettv;
14825{
14826 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014827 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014828 char_u *fname;
14829 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014830 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14831 int io_size = sizeof(buf);
14832 int readlen; /* size of last fread() */
14833 char_u *prev = NULL; /* previously read bytes, if any */
14834 long prevlen = 0; /* length of data in prev */
14835 long prevsize = 0; /* size of prev buffer */
14836 long maxline = MAXLNUM;
14837 long cnt = 0;
14838 char_u *p; /* position in buf */
14839 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014840
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014841 if (argvars[1].v_type != VAR_UNKNOWN)
14842 {
14843 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14844 binary = TRUE;
14845 if (argvars[2].v_type != VAR_UNKNOWN)
14846 maxline = get_tv_number(&argvars[2]);
14847 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014848
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014849 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014850 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014851
14852 /* Always open the file in binary mode, library functions have a mind of
14853 * their own about CR-LF conversion. */
14854 fname = get_tv_string(&argvars[0]);
14855 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14856 {
14857 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14858 return;
14859 }
14860
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014861 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014862 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014863 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014864
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014865 /* This for loop processes what was read, but is also entered at end
14866 * of file so that either:
14867 * - an incomplete line gets written
14868 * - a "binary" file gets an empty line at the end if it ends in a
14869 * newline. */
14870 for (p = buf, start = buf;
14871 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14872 ++p)
14873 {
14874 if (*p == '\n' || readlen <= 0)
14875 {
14876 listitem_T *li;
14877 char_u *s = NULL;
14878 long_u len = p - start;
14879
14880 /* Finished a line. Remove CRs before NL. */
14881 if (readlen > 0 && !binary)
14882 {
14883 while (len > 0 && start[len - 1] == '\r')
14884 --len;
14885 /* removal may cross back to the "prev" string */
14886 if (len == 0)
14887 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14888 --prevlen;
14889 }
14890 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014891 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014892 else
14893 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014894 /* Change "prev" buffer to be the right size. This way
14895 * the bytes are only copied once, and very long lines are
14896 * allocated only once. */
14897 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014898 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014899 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014900 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014901 prev = NULL; /* the list will own the string */
14902 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014903 }
14904 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014905 if (s == NULL)
14906 {
14907 do_outofmem_msg((long_u) prevlen + len + 1);
14908 failed = TRUE;
14909 break;
14910 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014911
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014912 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014913 {
14914 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014915 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014916 break;
14917 }
14918 li->li_tv.v_type = VAR_STRING;
14919 li->li_tv.v_lock = 0;
14920 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014921 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014922
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014923 start = p + 1; /* step over newline */
14924 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014925 break;
14926 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014927 else if (*p == NUL)
14928 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014929#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014930 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14931 * when finding the BF and check the previous two bytes. */
14932 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014933 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014934 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14935 * + 1, these may be in the "prev" string. */
14936 char_u back1 = p >= buf + 1 ? p[-1]
14937 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14938 char_u back2 = p >= buf + 2 ? p[-2]
14939 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14940 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014941
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014942 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014943 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014944 char_u *dest = p - 2;
14945
14946 /* Usually a BOM is at the beginning of a file, and so at
14947 * the beginning of a line; then we can just step over it.
14948 */
14949 if (start == dest)
14950 start = p + 1;
14951 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014952 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014953 /* have to shuffle buf to close gap */
14954 int adjust_prevlen = 0;
14955
14956 if (dest < buf)
14957 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014958 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014959 dest = buf;
14960 }
14961 if (readlen > p - buf + 1)
14962 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14963 readlen -= 3 - adjust_prevlen;
14964 prevlen -= adjust_prevlen;
14965 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014966 }
14967 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014968 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014969#endif
14970 } /* for */
14971
14972 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14973 break;
14974 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014975 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014976 /* There's part of a line in buf, store it in "prev". */
14977 if (p - start + prevlen >= prevsize)
14978 {
14979 /* need bigger "prev" buffer */
14980 char_u *newprev;
14981
14982 /* A common use case is ordinary text files and "prev" gets a
14983 * fragment of a line, so the first allocation is made
14984 * small, to avoid repeatedly 'allocing' large and
14985 * 'reallocing' small. */
14986 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014987 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014988 else
14989 {
14990 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014991 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014992 prevsize = grow50pc > growmin ? grow50pc : growmin;
14993 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014994 newprev = prev == NULL ? alloc(prevsize)
14995 : vim_realloc(prev, prevsize);
14996 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014997 {
14998 do_outofmem_msg((long_u)prevsize);
14999 failed = TRUE;
15000 break;
15001 }
15002 prev = newprev;
15003 }
15004 /* Add the line part to end of "prev". */
15005 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015006 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015007 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015008 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015009
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015010 /*
15011 * For a negative line count use only the lines at the end of the file,
15012 * free the rest.
15013 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015014 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015015 while (cnt > -maxline)
15016 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015017 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015018 --cnt;
15019 }
15020
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015021 if (failed)
15022 {
15023 list_free(rettv->vval.v_list, TRUE);
15024 /* readfile doc says an empty list is returned on error */
15025 rettv->vval.v_list = list_alloc();
15026 }
15027
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015028 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015029 fclose(fd);
15030}
15031
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015032#if defined(FEAT_RELTIME)
15033static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15034
15035/*
15036 * Convert a List to proftime_T.
15037 * Return FAIL when there is something wrong.
15038 */
15039 static int
15040list2proftime(arg, tm)
15041 typval_T *arg;
15042 proftime_T *tm;
15043{
15044 long n1, n2;
15045 int error = FALSE;
15046
15047 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15048 || arg->vval.v_list->lv_len != 2)
15049 return FAIL;
15050 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15051 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15052# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015053 tm->HighPart = n1;
15054 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015055# else
15056 tm->tv_sec = n1;
15057 tm->tv_usec = n2;
15058# endif
15059 return error ? FAIL : OK;
15060}
15061#endif /* FEAT_RELTIME */
15062
15063/*
15064 * "reltime()" function
15065 */
15066 static void
15067f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015068 typval_T *argvars UNUSED;
15069 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015070{
15071#ifdef FEAT_RELTIME
15072 proftime_T res;
15073 proftime_T start;
15074
15075 if (argvars[0].v_type == VAR_UNKNOWN)
15076 {
15077 /* No arguments: get current time. */
15078 profile_start(&res);
15079 }
15080 else if (argvars[1].v_type == VAR_UNKNOWN)
15081 {
15082 if (list2proftime(&argvars[0], &res) == FAIL)
15083 return;
15084 profile_end(&res);
15085 }
15086 else
15087 {
15088 /* Two arguments: compute the difference. */
15089 if (list2proftime(&argvars[0], &start) == FAIL
15090 || list2proftime(&argvars[1], &res) == FAIL)
15091 return;
15092 profile_sub(&res, &start);
15093 }
15094
15095 if (rettv_list_alloc(rettv) == OK)
15096 {
15097 long n1, n2;
15098
15099# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015100 n1 = res.HighPart;
15101 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015102# else
15103 n1 = res.tv_sec;
15104 n2 = res.tv_usec;
15105# endif
15106 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15107 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15108 }
15109#endif
15110}
15111
15112/*
15113 * "reltimestr()" function
15114 */
15115 static void
15116f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015117 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015118 typval_T *rettv;
15119{
15120#ifdef FEAT_RELTIME
15121 proftime_T tm;
15122#endif
15123
15124 rettv->v_type = VAR_STRING;
15125 rettv->vval.v_string = NULL;
15126#ifdef FEAT_RELTIME
15127 if (list2proftime(&argvars[0], &tm) == OK)
15128 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15129#endif
15130}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015131
Bram Moolenaar0d660222005-01-07 21:51:51 +000015132#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15133static void make_connection __ARGS((void));
15134static int check_connection __ARGS((void));
15135
15136 static void
15137make_connection()
15138{
15139 if (X_DISPLAY == NULL
15140# ifdef FEAT_GUI
15141 && !gui.in_use
15142# endif
15143 )
15144 {
15145 x_force_connect = TRUE;
15146 setup_term_clip();
15147 x_force_connect = FALSE;
15148 }
15149}
15150
15151 static int
15152check_connection()
15153{
15154 make_connection();
15155 if (X_DISPLAY == NULL)
15156 {
15157 EMSG(_("E240: No connection to Vim server"));
15158 return FAIL;
15159 }
15160 return OK;
15161}
15162#endif
15163
15164#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015165static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015166
15167 static void
15168remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015169 typval_T *argvars;
15170 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171 int expr;
15172{
15173 char_u *server_name;
15174 char_u *keys;
15175 char_u *r = NULL;
15176 char_u buf[NUMBUFLEN];
15177# ifdef WIN32
15178 HWND w;
15179# else
15180 Window w;
15181# endif
15182
15183 if (check_restricted() || check_secure())
15184 return;
15185
15186# ifdef FEAT_X11
15187 if (check_connection() == FAIL)
15188 return;
15189# endif
15190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015191 server_name = get_tv_string_chk(&argvars[0]);
15192 if (server_name == NULL)
15193 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015194 keys = get_tv_string_buf(&argvars[1], buf);
15195# ifdef WIN32
15196 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15197# else
15198 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15199 < 0)
15200# endif
15201 {
15202 if (r != NULL)
15203 EMSG(r); /* sending worked but evaluation failed */
15204 else
15205 EMSG2(_("E241: Unable to send to %s"), server_name);
15206 return;
15207 }
15208
15209 rettv->vval.v_string = r;
15210
15211 if (argvars[2].v_type != VAR_UNKNOWN)
15212 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015213 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015214 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015215 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015216
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015217 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015218 v.di_tv.v_type = VAR_STRING;
15219 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015220 idvar = get_tv_string_chk(&argvars[2]);
15221 if (idvar != NULL)
15222 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015223 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015224 }
15225}
15226#endif
15227
15228/*
15229 * "remote_expr()" function
15230 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015231 static void
15232f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015233 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015234 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015235{
15236 rettv->v_type = VAR_STRING;
15237 rettv->vval.v_string = NULL;
15238#ifdef FEAT_CLIENTSERVER
15239 remote_common(argvars, rettv, TRUE);
15240#endif
15241}
15242
15243/*
15244 * "remote_foreground()" function
15245 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015246 static void
15247f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015248 typval_T *argvars UNUSED;
15249 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015250{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015251#ifdef FEAT_CLIENTSERVER
15252# ifdef WIN32
15253 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015254 {
15255 char_u *server_name = get_tv_string_chk(&argvars[0]);
15256
15257 if (server_name != NULL)
15258 serverForeground(server_name);
15259 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015260# else
15261 /* Send a foreground() expression to the server. */
15262 argvars[1].v_type = VAR_STRING;
15263 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15264 argvars[2].v_type = VAR_UNKNOWN;
15265 remote_common(argvars, rettv, TRUE);
15266 vim_free(argvars[1].vval.v_string);
15267# endif
15268#endif
15269}
15270
Bram Moolenaar0d660222005-01-07 21:51:51 +000015271 static void
15272f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015273 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015274 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015275{
15276#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015277 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015278 char_u *s = NULL;
15279# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015280 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015281# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015282 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015283
15284 if (check_restricted() || check_secure())
15285 {
15286 rettv->vval.v_number = -1;
15287 return;
15288 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015289 serverid = get_tv_string_chk(&argvars[0]);
15290 if (serverid == NULL)
15291 {
15292 rettv->vval.v_number = -1;
15293 return; /* type error; errmsg already given */
15294 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015295# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015296 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015297 if (n == 0)
15298 rettv->vval.v_number = -1;
15299 else
15300 {
15301 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15302 rettv->vval.v_number = (s != NULL);
15303 }
15304# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015305 if (check_connection() == FAIL)
15306 return;
15307
15308 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015309 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015310# endif
15311
15312 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015314 char_u *retvar;
15315
Bram Moolenaar33570922005-01-25 22:26:29 +000015316 v.di_tv.v_type = VAR_STRING;
15317 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015318 retvar = get_tv_string_chk(&argvars[1]);
15319 if (retvar != NULL)
15320 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015321 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015322 }
15323#else
15324 rettv->vval.v_number = -1;
15325#endif
15326}
15327
Bram Moolenaar0d660222005-01-07 21:51:51 +000015328 static void
15329f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015330 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015331 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015332{
15333 char_u *r = NULL;
15334
15335#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015336 char_u *serverid = get_tv_string_chk(&argvars[0]);
15337
15338 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339 {
15340# ifdef WIN32
15341 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015342 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015343
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015344 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015345 if (n != 0)
15346 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15347 if (r == NULL)
15348# else
15349 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015350 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015351# endif
15352 EMSG(_("E277: Unable to read a server reply"));
15353 }
15354#endif
15355 rettv->v_type = VAR_STRING;
15356 rettv->vval.v_string = r;
15357}
15358
15359/*
15360 * "remote_send()" function
15361 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015362 static void
15363f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015364 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015365 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015366{
15367 rettv->v_type = VAR_STRING;
15368 rettv->vval.v_string = NULL;
15369#ifdef FEAT_CLIENTSERVER
15370 remote_common(argvars, rettv, FALSE);
15371#endif
15372}
15373
15374/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015375 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015376 */
15377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015378f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015379 typval_T *argvars;
15380 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015381{
Bram Moolenaar33570922005-01-25 22:26:29 +000015382 list_T *l;
15383 listitem_T *item, *item2;
15384 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015385 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015386 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015387 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015388 dict_T *d;
15389 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015390 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015391
Bram Moolenaar8c711452005-01-14 21:53:12 +000015392 if (argvars[0].v_type == VAR_DICT)
15393 {
15394 if (argvars[2].v_type != VAR_UNKNOWN)
15395 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015396 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015397 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015398 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015399 key = get_tv_string_chk(&argvars[1]);
15400 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015402 di = dict_find(d, key, -1);
15403 if (di == NULL)
15404 EMSG2(_(e_dictkey), key);
15405 else
15406 {
15407 *rettv = di->di_tv;
15408 init_tv(&di->di_tv);
15409 dictitem_remove(d, di);
15410 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015411 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015412 }
15413 }
15414 else if (argvars[0].v_type != VAR_LIST)
15415 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015416 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015417 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015419 int error = FALSE;
15420
15421 idx = get_tv_number_chk(&argvars[1], &error);
15422 if (error)
15423 ; /* type error: do nothing, errmsg already given */
15424 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015425 EMSGN(_(e_listidx), idx);
15426 else
15427 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015428 if (argvars[2].v_type == VAR_UNKNOWN)
15429 {
15430 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015431 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015432 *rettv = item->li_tv;
15433 vim_free(item);
15434 }
15435 else
15436 {
15437 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015438 end = get_tv_number_chk(&argvars[2], &error);
15439 if (error)
15440 ; /* type error: do nothing */
15441 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015442 EMSGN(_(e_listidx), end);
15443 else
15444 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015445 int cnt = 0;
15446
15447 for (li = item; li != NULL; li = li->li_next)
15448 {
15449 ++cnt;
15450 if (li == item2)
15451 break;
15452 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015453 if (li == NULL) /* didn't find "item2" after "item" */
15454 EMSG(_(e_invrange));
15455 else
15456 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015457 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015458 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015459 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015460 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015461 l->lv_first = item;
15462 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015463 item->li_prev = NULL;
15464 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015465 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015466 }
15467 }
15468 }
15469 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015470 }
15471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472}
15473
15474/*
15475 * "rename({from}, {to})" function
15476 */
15477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015479 typval_T *argvars;
15480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481{
15482 char_u buf[NUMBUFLEN];
15483
15484 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015485 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015487 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15488 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489}
15490
15491/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015492 * "repeat()" function
15493 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015494 static void
15495f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015496 typval_T *argvars;
15497 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015498{
15499 char_u *p;
15500 int n;
15501 int slen;
15502 int len;
15503 char_u *r;
15504 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015505
15506 n = get_tv_number(&argvars[1]);
15507 if (argvars[0].v_type == VAR_LIST)
15508 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015509 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015510 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015511 if (list_extend(rettv->vval.v_list,
15512 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015513 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015514 }
15515 else
15516 {
15517 p = get_tv_string(&argvars[0]);
15518 rettv->v_type = VAR_STRING;
15519 rettv->vval.v_string = NULL;
15520
15521 slen = (int)STRLEN(p);
15522 len = slen * n;
15523 if (len <= 0)
15524 return;
15525
15526 r = alloc(len + 1);
15527 if (r != NULL)
15528 {
15529 for (i = 0; i < n; i++)
15530 mch_memmove(r + i * slen, p, (size_t)slen);
15531 r[len] = NUL;
15532 }
15533
15534 rettv->vval.v_string = r;
15535 }
15536}
15537
15538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539 * "resolve()" function
15540 */
15541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015542f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015543 typval_T *argvars;
15544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545{
15546 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015547#ifdef HAVE_READLINK
15548 char_u *buf = NULL;
15549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015551 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552#ifdef FEAT_SHORTCUT
15553 {
15554 char_u *v = NULL;
15555
15556 v = mch_resolve_shortcut(p);
15557 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015558 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561 }
15562#else
15563# ifdef HAVE_READLINK
15564 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565 char_u *cpy;
15566 int len;
15567 char_u *remain = NULL;
15568 char_u *q;
15569 int is_relative_to_current = FALSE;
15570 int has_trailing_pathsep = FALSE;
15571 int limit = 100;
15572
15573 p = vim_strsave(p);
15574
15575 if (p[0] == '.' && (vim_ispathsep(p[1])
15576 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15577 is_relative_to_current = TRUE;
15578
15579 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015580 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015583 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585
15586 q = getnextcomp(p);
15587 if (*q != NUL)
15588 {
15589 /* Separate the first path component in "p", and keep the
15590 * remainder (beginning with the path separator). */
15591 remain = vim_strsave(q - 1);
15592 q[-1] = NUL;
15593 }
15594
Bram Moolenaard9462e32011-04-11 21:35:11 +020015595 buf = alloc(MAXPATHL + 1);
15596 if (buf == NULL)
15597 goto fail;
15598
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 for (;;)
15600 {
15601 for (;;)
15602 {
15603 len = readlink((char *)p, (char *)buf, MAXPATHL);
15604 if (len <= 0)
15605 break;
15606 buf[len] = NUL;
15607
15608 if (limit-- == 0)
15609 {
15610 vim_free(p);
15611 vim_free(remain);
15612 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015613 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 goto fail;
15615 }
15616
15617 /* Ensure that the result will have a trailing path separator
15618 * if the argument has one. */
15619 if (remain == NULL && has_trailing_pathsep)
15620 add_pathsep(buf);
15621
15622 /* Separate the first path component in the link value and
15623 * concatenate the remainders. */
15624 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15625 if (*q != NUL)
15626 {
15627 if (remain == NULL)
15628 remain = vim_strsave(q - 1);
15629 else
15630 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015631 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 if (cpy != NULL)
15633 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 vim_free(remain);
15635 remain = cpy;
15636 }
15637 }
15638 q[-1] = NUL;
15639 }
15640
15641 q = gettail(p);
15642 if (q > p && *q == NUL)
15643 {
15644 /* Ignore trailing path separator. */
15645 q[-1] = NUL;
15646 q = gettail(p);
15647 }
15648 if (q > p && !mch_isFullName(buf))
15649 {
15650 /* symlink is relative to directory of argument */
15651 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15652 if (cpy != NULL)
15653 {
15654 STRCPY(cpy, p);
15655 STRCPY(gettail(cpy), buf);
15656 vim_free(p);
15657 p = cpy;
15658 }
15659 }
15660 else
15661 {
15662 vim_free(p);
15663 p = vim_strsave(buf);
15664 }
15665 }
15666
15667 if (remain == NULL)
15668 break;
15669
15670 /* Append the first path component of "remain" to "p". */
15671 q = getnextcomp(remain + 1);
15672 len = q - remain - (*q != NUL);
15673 cpy = vim_strnsave(p, STRLEN(p) + len);
15674 if (cpy != NULL)
15675 {
15676 STRNCAT(cpy, remain, len);
15677 vim_free(p);
15678 p = cpy;
15679 }
15680 /* Shorten "remain". */
15681 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015682 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683 else
15684 {
15685 vim_free(remain);
15686 remain = NULL;
15687 }
15688 }
15689
15690 /* If the result is a relative path name, make it explicitly relative to
15691 * the current directory if and only if the argument had this form. */
15692 if (!vim_ispathsep(*p))
15693 {
15694 if (is_relative_to_current
15695 && *p != NUL
15696 && !(p[0] == '.'
15697 && (p[1] == NUL
15698 || vim_ispathsep(p[1])
15699 || (p[1] == '.'
15700 && (p[2] == NUL
15701 || vim_ispathsep(p[2]))))))
15702 {
15703 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015704 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 if (cpy != NULL)
15706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 vim_free(p);
15708 p = cpy;
15709 }
15710 }
15711 else if (!is_relative_to_current)
15712 {
15713 /* Strip leading "./". */
15714 q = p;
15715 while (q[0] == '.' && vim_ispathsep(q[1]))
15716 q += 2;
15717 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015718 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719 }
15720 }
15721
15722 /* Ensure that the result will have no trailing path separator
15723 * if the argument had none. But keep "/" or "//". */
15724 if (!has_trailing_pathsep)
15725 {
15726 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015727 if (after_pathsep(p, q))
15728 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729 }
15730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015731 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 }
15733# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015734 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735# endif
15736#endif
15737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015738 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739
15740#ifdef HAVE_READLINK
15741fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015742 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015744 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745}
15746
15747/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015748 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 */
15750 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015751f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015752 typval_T *argvars;
15753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754{
Bram Moolenaar33570922005-01-25 22:26:29 +000015755 list_T *l;
15756 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757
Bram Moolenaar0d660222005-01-07 21:51:51 +000015758 if (argvars[0].v_type != VAR_LIST)
15759 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015760 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015761 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015762 {
15763 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015764 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015765 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015766 while (li != NULL)
15767 {
15768 ni = li->li_prev;
15769 list_append(l, li);
15770 li = ni;
15771 }
15772 rettv->vval.v_list = l;
15773 rettv->v_type = VAR_LIST;
15774 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015775 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777}
15778
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015779#define SP_NOMOVE 0x01 /* don't move cursor */
15780#define SP_REPEAT 0x02 /* repeat to find outer pair */
15781#define SP_RETCOUNT 0x04 /* return matchcount */
15782#define SP_SETPCMARK 0x08 /* set previous context mark */
15783#define SP_START 0x10 /* accept match at start position */
15784#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15785#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015786
Bram Moolenaar33570922005-01-25 22:26:29 +000015787static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015788
15789/*
15790 * Get flags for a search function.
15791 * Possibly sets "p_ws".
15792 * Returns BACKWARD, FORWARD or zero (for an error).
15793 */
15794 static int
15795get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015796 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015797 int *flagsp;
15798{
15799 int dir = FORWARD;
15800 char_u *flags;
15801 char_u nbuf[NUMBUFLEN];
15802 int mask;
15803
15804 if (varp->v_type != VAR_UNKNOWN)
15805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015806 flags = get_tv_string_buf_chk(varp, nbuf);
15807 if (flags == NULL)
15808 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015809 while (*flags != NUL)
15810 {
15811 switch (*flags)
15812 {
15813 case 'b': dir = BACKWARD; break;
15814 case 'w': p_ws = TRUE; break;
15815 case 'W': p_ws = FALSE; break;
15816 default: mask = 0;
15817 if (flagsp != NULL)
15818 switch (*flags)
15819 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015820 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015821 case 'e': mask = SP_END; break;
15822 case 'm': mask = SP_RETCOUNT; break;
15823 case 'n': mask = SP_NOMOVE; break;
15824 case 'p': mask = SP_SUBPAT; break;
15825 case 'r': mask = SP_REPEAT; break;
15826 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015827 }
15828 if (mask == 0)
15829 {
15830 EMSG2(_(e_invarg2), flags);
15831 dir = 0;
15832 }
15833 else
15834 *flagsp |= mask;
15835 }
15836 if (dir == 0)
15837 break;
15838 ++flags;
15839 }
15840 }
15841 return dir;
15842}
15843
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015845 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015847 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015848search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015849 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015850 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015851 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015853 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854 char_u *pat;
15855 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015856 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 int save_p_ws = p_ws;
15858 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015859 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015860 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015861 proftime_T tm;
15862#ifdef FEAT_RELTIME
15863 long time_limit = 0;
15864#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015865 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015866 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015868 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015869 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015870 if (dir == 0)
15871 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015872 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015873 if (flags & SP_START)
15874 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015875 if (flags & SP_END)
15876 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015877
Bram Moolenaar76929292008-01-06 19:07:36 +000015878 /* Optional arguments: line number to stop searching and timeout. */
15879 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015880 {
15881 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15882 if (lnum_stop < 0)
15883 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015884#ifdef FEAT_RELTIME
15885 if (argvars[3].v_type != VAR_UNKNOWN)
15886 {
15887 time_limit = get_tv_number_chk(&argvars[3], NULL);
15888 if (time_limit < 0)
15889 goto theend;
15890 }
15891#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015892 }
15893
Bram Moolenaar76929292008-01-06 19:07:36 +000015894#ifdef FEAT_RELTIME
15895 /* Set the time limit, if there is one. */
15896 profile_setlimit(time_limit, &tm);
15897#endif
15898
Bram Moolenaar231334e2005-07-25 20:46:57 +000015899 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015900 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015901 * Check to make sure only those flags are set.
15902 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15903 * flags cannot be set. Check for that condition also.
15904 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015905 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015906 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015908 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015909 goto theend;
15910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015912 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015913 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015914 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015915 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015917 if (flags & SP_SUBPAT)
15918 retval = subpatnum;
15919 else
15920 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015921 if (flags & SP_SETPCMARK)
15922 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015923 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015924 if (match_pos != NULL)
15925 {
15926 /* Store the match cursor position */
15927 match_pos->lnum = pos.lnum;
15928 match_pos->col = pos.col + 1;
15929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 /* "/$" will put the cursor after the end of the line, may need to
15931 * correct that here */
15932 check_cursor();
15933 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015934
15935 /* If 'n' flag is used: restore cursor position. */
15936 if (flags & SP_NOMOVE)
15937 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015938 else
15939 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015940theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015942
15943 return retval;
15944}
15945
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015946#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015947
15948/*
15949 * round() is not in C90, use ceil() or floor() instead.
15950 */
15951 float_T
15952vim_round(f)
15953 float_T f;
15954{
15955 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15956}
15957
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015958/*
15959 * "round({float})" function
15960 */
15961 static void
15962f_round(argvars, rettv)
15963 typval_T *argvars;
15964 typval_T *rettv;
15965{
15966 float_T f;
15967
15968 rettv->v_type = VAR_FLOAT;
15969 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015970 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015971 else
15972 rettv->vval.v_float = 0.0;
15973}
15974#endif
15975
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015976/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015977 * "screenattr()" function
15978 */
15979 static void
15980f_screenattr(argvars, rettv)
15981 typval_T *argvars UNUSED;
15982 typval_T *rettv;
15983{
15984 int row;
15985 int col;
15986 int c;
15987
15988 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15989 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15990 if (row < 0 || row >= screen_Rows
15991 || col < 0 || col >= screen_Columns)
15992 c = -1;
15993 else
15994 c = ScreenAttrs[LineOffset[row] + col];
15995 rettv->vval.v_number = c;
15996}
15997
15998/*
15999 * "screenchar()" function
16000 */
16001 static void
16002f_screenchar(argvars, rettv)
16003 typval_T *argvars UNUSED;
16004 typval_T *rettv;
16005{
16006 int row;
16007 int col;
16008 int off;
16009 int c;
16010
16011 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16012 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16013 if (row < 0 || row >= screen_Rows
16014 || col < 0 || col >= screen_Columns)
16015 c = -1;
16016 else
16017 {
16018 off = LineOffset[row] + col;
16019#ifdef FEAT_MBYTE
16020 if (enc_utf8 && ScreenLinesUC[off] != 0)
16021 c = ScreenLinesUC[off];
16022 else
16023#endif
16024 c = ScreenLines[off];
16025 }
16026 rettv->vval.v_number = c;
16027}
16028
16029/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016030 * "screencol()" function
16031 *
16032 * First column is 1 to be consistent with virtcol().
16033 */
16034 static void
16035f_screencol(argvars, rettv)
16036 typval_T *argvars UNUSED;
16037 typval_T *rettv;
16038{
16039 rettv->vval.v_number = screen_screencol() + 1;
16040}
16041
16042/*
16043 * "screenrow()" function
16044 */
16045 static void
16046f_screenrow(argvars, rettv)
16047 typval_T *argvars UNUSED;
16048 typval_T *rettv;
16049{
16050 rettv->vval.v_number = screen_screenrow() + 1;
16051}
16052
16053/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016054 * "search()" function
16055 */
16056 static void
16057f_search(argvars, rettv)
16058 typval_T *argvars;
16059 typval_T *rettv;
16060{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016061 int flags = 0;
16062
16063 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064}
16065
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016067 * "searchdecl()" function
16068 */
16069 static void
16070f_searchdecl(argvars, rettv)
16071 typval_T *argvars;
16072 typval_T *rettv;
16073{
16074 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016075 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016076 int error = FALSE;
16077 char_u *name;
16078
16079 rettv->vval.v_number = 1; /* default: FAIL */
16080
16081 name = get_tv_string_chk(&argvars[0]);
16082 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016083 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016084 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016085 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16086 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16087 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016088 if (!error && name != NULL)
16089 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016090 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016091}
16092
16093/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016094 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016096 static int
16097searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016098 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016099 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100{
16101 char_u *spat, *mpat, *epat;
16102 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 int dir;
16105 int flags = 0;
16106 char_u nbuf1[NUMBUFLEN];
16107 char_u nbuf2[NUMBUFLEN];
16108 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016109 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016110 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016111 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016114 spat = get_tv_string_chk(&argvars[0]);
16115 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16116 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16117 if (spat == NULL || mpat == NULL || epat == NULL)
16118 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 /* Handle the optional fourth argument: flags */
16121 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016122 if (dir == 0)
16123 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016124
16125 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016126 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16127 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016128 if ((flags & (SP_END | SP_SUBPAT)) != 0
16129 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016130 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016131 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016132 goto theend;
16133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016135 /* Using 'r' implies 'W', otherwise it doesn't work. */
16136 if (flags & SP_REPEAT)
16137 p_ws = FALSE;
16138
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016139 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016140 if (argvars[3].v_type == VAR_UNKNOWN
16141 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 skip = (char_u *)"";
16143 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016145 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016146 if (argvars[5].v_type != VAR_UNKNOWN)
16147 {
16148 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16149 if (lnum_stop < 0)
16150 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016151#ifdef FEAT_RELTIME
16152 if (argvars[6].v_type != VAR_UNKNOWN)
16153 {
16154 time_limit = get_tv_number_chk(&argvars[6], NULL);
16155 if (time_limit < 0)
16156 goto theend;
16157 }
16158#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016159 }
16160 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016161 if (skip == NULL)
16162 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016164 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016165 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016166
16167theend:
16168 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016169
16170 return retval;
16171}
16172
16173/*
16174 * "searchpair()" function
16175 */
16176 static void
16177f_searchpair(argvars, rettv)
16178 typval_T *argvars;
16179 typval_T *rettv;
16180{
16181 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16182}
16183
16184/*
16185 * "searchpairpos()" function
16186 */
16187 static void
16188f_searchpairpos(argvars, rettv)
16189 typval_T *argvars;
16190 typval_T *rettv;
16191{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016192 pos_T match_pos;
16193 int lnum = 0;
16194 int col = 0;
16195
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016196 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016197 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016198
16199 if (searchpair_cmn(argvars, &match_pos) > 0)
16200 {
16201 lnum = match_pos.lnum;
16202 col = match_pos.col;
16203 }
16204
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016205 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16206 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016207}
16208
16209/*
16210 * Search for a start/middle/end thing.
16211 * Used by searchpair(), see its documentation for the details.
16212 * Returns 0 or -1 for no match,
16213 */
16214 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016215do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16216 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016217 char_u *spat; /* start pattern */
16218 char_u *mpat; /* middle pattern */
16219 char_u *epat; /* end pattern */
16220 int dir; /* BACKWARD or FORWARD */
16221 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016222 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016223 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016224 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016225 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016226{
16227 char_u *save_cpo;
16228 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16229 long retval = 0;
16230 pos_T pos;
16231 pos_T firstpos;
16232 pos_T foundpos;
16233 pos_T save_cursor;
16234 pos_T save_pos;
16235 int n;
16236 int r;
16237 int nest = 1;
16238 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016239 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016240 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016241
16242 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16243 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016244 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016245
Bram Moolenaar76929292008-01-06 19:07:36 +000016246#ifdef FEAT_RELTIME
16247 /* Set the time limit, if there is one. */
16248 profile_setlimit(time_limit, &tm);
16249#endif
16250
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016251 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16252 * start/middle/end (pat3, for the top pair). */
16253 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16254 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16255 if (pat2 == NULL || pat3 == NULL)
16256 goto theend;
16257 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16258 if (*mpat == NUL)
16259 STRCPY(pat3, pat2);
16260 else
16261 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16262 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016263 if (flags & SP_START)
16264 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016265
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 save_cursor = curwin->w_cursor;
16267 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016268 clearpos(&firstpos);
16269 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270 pat = pat3;
16271 for (;;)
16272 {
16273 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016274 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16276 /* didn't find it or found the first match again: FAIL */
16277 break;
16278
16279 if (firstpos.lnum == 0)
16280 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016281 if (equalpos(pos, foundpos))
16282 {
16283 /* Found the same position again. Can happen with a pattern that
16284 * has "\zs" at the end and searching backwards. Advance one
16285 * character and try again. */
16286 if (dir == BACKWARD)
16287 decl(&pos);
16288 else
16289 incl(&pos);
16290 }
16291 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016293 /* clear the start flag to avoid getting stuck here */
16294 options &= ~SEARCH_START;
16295
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 /* If the skip pattern matches, ignore this match. */
16297 if (*skip != NUL)
16298 {
16299 save_pos = curwin->w_cursor;
16300 curwin->w_cursor = pos;
16301 r = eval_to_bool(skip, &err, NULL, FALSE);
16302 curwin->w_cursor = save_pos;
16303 if (err)
16304 {
16305 /* Evaluating {skip} caused an error, break here. */
16306 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016307 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 break;
16309 }
16310 if (r)
16311 continue;
16312 }
16313
16314 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16315 {
16316 /* Found end when searching backwards or start when searching
16317 * forward: nested pair. */
16318 ++nest;
16319 pat = pat2; /* nested, don't search for middle */
16320 }
16321 else
16322 {
16323 /* Found end when searching forward or start when searching
16324 * backward: end of (nested) pair; or found middle in outer pair. */
16325 if (--nest == 1)
16326 pat = pat3; /* outer level, search for middle */
16327 }
16328
16329 if (nest == 0)
16330 {
16331 /* Found the match: return matchcount or line number. */
16332 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016333 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016335 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016336 if (flags & SP_SETPCMARK)
16337 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338 curwin->w_cursor = pos;
16339 if (!(flags & SP_REPEAT))
16340 break;
16341 nest = 1; /* search for next unmatched */
16342 }
16343 }
16344
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016345 if (match_pos != NULL)
16346 {
16347 /* Store the match cursor position */
16348 match_pos->lnum = curwin->w_cursor.lnum;
16349 match_pos->col = curwin->w_cursor.col + 1;
16350 }
16351
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016353 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354 curwin->w_cursor = save_cursor;
16355
16356theend:
16357 vim_free(pat2);
16358 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016359 if (p_cpo == empty_option)
16360 p_cpo = save_cpo;
16361 else
16362 /* Darn, evaluating the {skip} expression changed the value. */
16363 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016364
16365 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366}
16367
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016368/*
16369 * "searchpos()" function
16370 */
16371 static void
16372f_searchpos(argvars, rettv)
16373 typval_T *argvars;
16374 typval_T *rettv;
16375{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016376 pos_T match_pos;
16377 int lnum = 0;
16378 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016379 int n;
16380 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016381
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016382 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016383 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016384
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016385 n = search_cmn(argvars, &match_pos, &flags);
16386 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016387 {
16388 lnum = match_pos.lnum;
16389 col = match_pos.col;
16390 }
16391
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016392 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16393 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016394 if (flags & SP_SUBPAT)
16395 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016396}
16397
16398
Bram Moolenaar0d660222005-01-07 21:51:51 +000016399 static void
16400f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016401 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016404#ifdef FEAT_CLIENTSERVER
16405 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016406 char_u *server = get_tv_string_chk(&argvars[0]);
16407 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408
Bram Moolenaar0d660222005-01-07 21:51:51 +000016409 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016410 if (server == NULL || reply == NULL)
16411 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016412 if (check_restricted() || check_secure())
16413 return;
16414# ifdef FEAT_X11
16415 if (check_connection() == FAIL)
16416 return;
16417# endif
16418
16419 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016421 EMSG(_("E258: Unable to send to client"));
16422 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016423 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016424 rettv->vval.v_number = 0;
16425#else
16426 rettv->vval.v_number = -1;
16427#endif
16428}
16429
Bram Moolenaar0d660222005-01-07 21:51:51 +000016430 static void
16431f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016432 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016434{
16435 char_u *r = NULL;
16436
16437#ifdef FEAT_CLIENTSERVER
16438# ifdef WIN32
16439 r = serverGetVimNames();
16440# else
16441 make_connection();
16442 if (X_DISPLAY != NULL)
16443 r = serverGetVimNames(X_DISPLAY);
16444# endif
16445#endif
16446 rettv->v_type = VAR_STRING;
16447 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448}
16449
16450/*
16451 * "setbufvar()" function
16452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016454f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016455 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016456 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457{
16458 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016459 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016461 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462 char_u nbuf[NUMBUFLEN];
16463
16464 if (check_restricted() || check_secure())
16465 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016466 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16467 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016468 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469 varp = &argvars[2];
16470
16471 if (buf != NULL && varname != NULL && varp != NULL)
16472 {
16473 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475
16476 if (*varname == '&')
16477 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016478 long numval;
16479 char_u *strval;
16480 int error = FALSE;
16481
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016483 numval = get_tv_number_chk(varp, &error);
16484 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016485 if (!error && strval != NULL)
16486 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487 }
16488 else
16489 {
16490 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16491 if (bufvarname != NULL)
16492 {
16493 STRCPY(bufvarname, "b:");
16494 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016495 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 vim_free(bufvarname);
16497 }
16498 }
16499
16500 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503}
16504
16505/*
16506 * "setcmdpos()" function
16507 */
16508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016509f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016510 typval_T *argvars;
16511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016513 int pos = (int)get_tv_number(&argvars[0]) - 1;
16514
16515 if (pos >= 0)
16516 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517}
16518
16519/*
16520 * "setline()" function
16521 */
16522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016523f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016524 typval_T *argvars;
16525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526{
16527 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016528 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016529 list_T *l = NULL;
16530 listitem_T *li = NULL;
16531 long added = 0;
16532 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016534 lnum = get_tv_lnum(&argvars[0]);
16535 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016537 l = argvars[1].vval.v_list;
16538 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016540 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016541 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016542
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016543 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016544 for (;;)
16545 {
16546 if (l != NULL)
16547 {
16548 /* list argument, get next string */
16549 if (li == NULL)
16550 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016551 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016552 li = li->li_next;
16553 }
16554
16555 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016556 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016557 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016558
16559 /* When coming here from Insert mode, sync undo, so that this can be
16560 * undone separately from what was previously inserted. */
16561 if (u_sync_once == 2)
16562 {
16563 u_sync_once = 1; /* notify that u_sync() was called */
16564 u_sync(TRUE);
16565 }
16566
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016567 if (lnum <= curbuf->b_ml.ml_line_count)
16568 {
16569 /* existing line, replace it */
16570 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16571 {
16572 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016573 if (lnum == curwin->w_cursor.lnum)
16574 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016575 rettv->vval.v_number = 0; /* OK */
16576 }
16577 }
16578 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16579 {
16580 /* lnum is one past the last line, append the line */
16581 ++added;
16582 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16583 rettv->vval.v_number = 0; /* OK */
16584 }
16585
16586 if (l == NULL) /* only one string argument */
16587 break;
16588 ++lnum;
16589 }
16590
16591 if (added > 0)
16592 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593}
16594
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016595static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16596
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016598 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016599 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016600 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016601set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016602 win_T *wp UNUSED;
16603 typval_T *list_arg UNUSED;
16604 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016605 typval_T *rettv;
16606{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016607#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016608 char_u *act;
16609 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016610#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016611
Bram Moolenaar2641f772005-03-25 21:58:17 +000016612 rettv->vval.v_number = -1;
16613
16614#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016615 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016616 EMSG(_(e_listreq));
16617 else
16618 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016619 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016620
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016621 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016622 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016623 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016624 if (act == NULL)
16625 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016626 if (*act == 'a' || *act == 'r')
16627 action = *act;
16628 }
16629
Bram Moolenaar81484f42012-12-05 15:16:47 +010016630 if (l != NULL && set_errorlist(wp, l, action,
16631 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016632 rettv->vval.v_number = 0;
16633 }
16634#endif
16635}
16636
16637/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016638 * "setloclist()" function
16639 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016640 static void
16641f_setloclist(argvars, rettv)
16642 typval_T *argvars;
16643 typval_T *rettv;
16644{
16645 win_T *win;
16646
16647 rettv->vval.v_number = -1;
16648
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016649 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016650 if (win != NULL)
16651 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16652}
16653
16654/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016655 * "setmatches()" function
16656 */
16657 static void
16658f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016659 typval_T *argvars UNUSED;
16660 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016661{
16662#ifdef FEAT_SEARCH_EXTRA
16663 list_T *l;
16664 listitem_T *li;
16665 dict_T *d;
16666
16667 rettv->vval.v_number = -1;
16668 if (argvars[0].v_type != VAR_LIST)
16669 {
16670 EMSG(_(e_listreq));
16671 return;
16672 }
16673 if ((l = argvars[0].vval.v_list) != NULL)
16674 {
16675
16676 /* To some extent make sure that we are dealing with a list from
16677 * "getmatches()". */
16678 li = l->lv_first;
16679 while (li != NULL)
16680 {
16681 if (li->li_tv.v_type != VAR_DICT
16682 || (d = li->li_tv.vval.v_dict) == NULL)
16683 {
16684 EMSG(_(e_invarg));
16685 return;
16686 }
16687 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16688 && dict_find(d, (char_u *)"pattern", -1) != NULL
16689 && dict_find(d, (char_u *)"priority", -1) != NULL
16690 && dict_find(d, (char_u *)"id", -1) != NULL))
16691 {
16692 EMSG(_(e_invarg));
16693 return;
16694 }
16695 li = li->li_next;
16696 }
16697
16698 clear_matches(curwin);
16699 li = l->lv_first;
16700 while (li != NULL)
16701 {
16702 d = li->li_tv.vval.v_dict;
16703 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16704 get_dict_string(d, (char_u *)"pattern", FALSE),
16705 (int)get_dict_number(d, (char_u *)"priority"),
16706 (int)get_dict_number(d, (char_u *)"id"));
16707 li = li->li_next;
16708 }
16709 rettv->vval.v_number = 0;
16710 }
16711#endif
16712}
16713
16714/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016715 * "setpos()" function
16716 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016717 static void
16718f_setpos(argvars, rettv)
16719 typval_T *argvars;
16720 typval_T *rettv;
16721{
16722 pos_T pos;
16723 int fnum;
16724 char_u *name;
16725
Bram Moolenaar08250432008-02-13 11:42:46 +000016726 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016727 name = get_tv_string_chk(argvars);
16728 if (name != NULL)
16729 {
16730 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16731 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016732 if (--pos.col < 0)
16733 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016734 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016735 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016736 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016737 if (fnum == curbuf->b_fnum)
16738 {
16739 curwin->w_cursor = pos;
16740 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016741 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016742 }
16743 else
16744 EMSG(_(e_invarg));
16745 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016746 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16747 {
16748 /* set mark */
16749 if (setmark_pos(name[1], &pos, fnum) == OK)
16750 rettv->vval.v_number = 0;
16751 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016752 else
16753 EMSG(_(e_invarg));
16754 }
16755 }
16756}
16757
16758/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016759 * "setqflist()" function
16760 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016761 static void
16762f_setqflist(argvars, rettv)
16763 typval_T *argvars;
16764 typval_T *rettv;
16765{
16766 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16767}
16768
16769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770 * "setreg()" function
16771 */
16772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016773f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016774 typval_T *argvars;
16775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776{
16777 int regname;
16778 char_u *strregname;
16779 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016780 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781 int append;
16782 char_u yank_type;
16783 long block_len;
16784
16785 block_len = -1;
16786 yank_type = MAUTO;
16787 append = FALSE;
16788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016789 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016790 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016792 if (strregname == NULL)
16793 return; /* type error; errmsg already given */
16794 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 if (regname == 0 || regname == '@')
16796 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016798 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016800 stropt = get_tv_string_chk(&argvars[2]);
16801 if (stropt == NULL)
16802 return; /* type error */
16803 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804 switch (*stropt)
16805 {
16806 case 'a': case 'A': /* append */
16807 append = TRUE;
16808 break;
16809 case 'v': case 'c': /* character-wise selection */
16810 yank_type = MCHAR;
16811 break;
16812 case 'V': case 'l': /* line-wise selection */
16813 yank_type = MLINE;
16814 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 case 'b': case Ctrl_V: /* block-wise selection */
16816 yank_type = MBLOCK;
16817 if (VIM_ISDIGIT(stropt[1]))
16818 {
16819 ++stropt;
16820 block_len = getdigits(&stropt) - 1;
16821 --stropt;
16822 }
16823 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 }
16825 }
16826
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016827 if (argvars[1].v_type == VAR_LIST)
16828 {
16829 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016830 char_u **allocval;
16831 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016832 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016833 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016834 int len = argvars[1].vval.v_list->lv_len;
16835 listitem_T *li;
16836
Bram Moolenaar7d647822014-04-05 21:28:56 +020016837 /* First half: use for pointers to result lines; second half: use for
16838 * pointers to allocated copies. */
16839 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016840 if (lstval == NULL)
16841 return;
16842 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016843 allocval = lstval + len + 2;
16844 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016845
16846 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
16847 li = li->li_next)
16848 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016849 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016850 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020016851 goto free_lstval;
16852 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016853 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016854 /* Need to make a copy, next get_tv_string_buf_chk() will
16855 * overwrite the string. */
16856 strval = vim_strsave(buf);
16857 if (strval == NULL)
16858 goto free_lstval;
16859 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016860 }
16861 *curval++ = strval;
16862 }
16863 *curval++ = NULL;
16864
16865 write_reg_contents_lst(regname, lstval, -1,
16866 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020016867free_lstval:
16868 while (curallocval > allocval)
16869 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016870 vim_free(lstval);
16871 }
16872 else
16873 {
16874 strval = get_tv_string_chk(&argvars[1]);
16875 if (strval == NULL)
16876 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016877 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016879 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016880 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881}
16882
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016883/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016884 * "settabvar()" function
16885 */
16886 static void
16887f_settabvar(argvars, rettv)
16888 typval_T *argvars;
16889 typval_T *rettv;
16890{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016891#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016892 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016893 tabpage_T *tp;
16894#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016895 char_u *varname, *tabvarname;
16896 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016897
16898 rettv->vval.v_number = 0;
16899
16900 if (check_restricted() || check_secure())
16901 return;
16902
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016903#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016904 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016905#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016906 varname = get_tv_string_chk(&argvars[1]);
16907 varp = &argvars[2];
16908
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016909 if (varname != NULL && varp != NULL
16910#ifdef FEAT_WINDOWS
16911 && tp != NULL
16912#endif
16913 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016914 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016915#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016916 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016917 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016918#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016919
16920 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16921 if (tabvarname != NULL)
16922 {
16923 STRCPY(tabvarname, "t:");
16924 STRCPY(tabvarname + 2, varname);
16925 set_var(tabvarname, varp, TRUE);
16926 vim_free(tabvarname);
16927 }
16928
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016929#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016930 /* Restore current tabpage */
16931 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016932 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016933#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016934 }
16935}
16936
16937/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016938 * "settabwinvar()" function
16939 */
16940 static void
16941f_settabwinvar(argvars, rettv)
16942 typval_T *argvars;
16943 typval_T *rettv;
16944{
16945 setwinvar(argvars, rettv, 1);
16946}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947
16948/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016949 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016952f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016953 typval_T *argvars;
16954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016956 setwinvar(argvars, rettv, 0);
16957}
16958
16959/*
16960 * "setwinvar()" and "settabwinvar()" functions
16961 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016962
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016963 static void
16964setwinvar(argvars, rettv, off)
16965 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016966 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016967 int off;
16968{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969 win_T *win;
16970#ifdef FEAT_WINDOWS
16971 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016972 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973#endif
16974 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016975 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016977 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978
16979 if (check_restricted() || check_secure())
16980 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016981
16982#ifdef FEAT_WINDOWS
16983 if (off == 1)
16984 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16985 else
16986 tp = curtab;
16987#endif
16988 win = find_win_by_nr(&argvars[off], tp);
16989 varname = get_tv_string_chk(&argvars[off + 1]);
16990 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991
16992 if (win != NULL && varname != NULL && varp != NULL)
16993 {
16994#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016995 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016996 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997#endif
16998
16999 if (*varname == '&')
17000 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017001 long numval;
17002 char_u *strval;
17003 int error = FALSE;
17004
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017006 numval = get_tv_number_chk(varp, &error);
17007 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017008 if (!error && strval != NULL)
17009 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010 }
17011 else
17012 {
17013 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17014 if (winvarname != NULL)
17015 {
17016 STRCPY(winvarname, "w:");
17017 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017018 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019 vim_free(winvarname);
17020 }
17021 }
17022
17023#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017024 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025#endif
17026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027}
17028
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017029#ifdef FEAT_CRYPT
17030/*
17031 * "sha256({string})" function
17032 */
17033 static void
17034f_sha256(argvars, rettv)
17035 typval_T *argvars;
17036 typval_T *rettv;
17037{
17038 char_u *p;
17039
17040 p = get_tv_string(&argvars[0]);
17041 rettv->vval.v_string = vim_strsave(
17042 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17043 rettv->v_type = VAR_STRING;
17044}
17045#endif /* FEAT_CRYPT */
17046
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017048 * "shellescape({string})" function
17049 */
17050 static void
17051f_shellescape(argvars, rettv)
17052 typval_T *argvars;
17053 typval_T *rettv;
17054{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017055 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017056 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017057 rettv->v_type = VAR_STRING;
17058}
17059
17060/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017061 * shiftwidth() function
17062 */
17063 static void
17064f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017065 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017066 typval_T *rettv;
17067{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017068 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017069}
17070
17071/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017072 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017073 */
17074 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017075f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017076 typval_T *argvars;
17077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017079 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081 p = get_tv_string(&argvars[0]);
17082 rettv->vval.v_string = vim_strsave(p);
17083 simplify_filename(rettv->vval.v_string); /* simplify in place */
17084 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085}
17086
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017087#ifdef FEAT_FLOAT
17088/*
17089 * "sin()" function
17090 */
17091 static void
17092f_sin(argvars, rettv)
17093 typval_T *argvars;
17094 typval_T *rettv;
17095{
17096 float_T f;
17097
17098 rettv->v_type = VAR_FLOAT;
17099 if (get_float_arg(argvars, &f) == OK)
17100 rettv->vval.v_float = sin(f);
17101 else
17102 rettv->vval.v_float = 0.0;
17103}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017104
17105/*
17106 * "sinh()" function
17107 */
17108 static void
17109f_sinh(argvars, rettv)
17110 typval_T *argvars;
17111 typval_T *rettv;
17112{
17113 float_T f;
17114
17115 rettv->v_type = VAR_FLOAT;
17116 if (get_float_arg(argvars, &f) == OK)
17117 rettv->vval.v_float = sinh(f);
17118 else
17119 rettv->vval.v_float = 0.0;
17120}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017121#endif
17122
Bram Moolenaar0d660222005-01-07 21:51:51 +000017123static int
17124#ifdef __BORLANDC__
17125 _RTLENTRYF
17126#endif
17127 item_compare __ARGS((const void *s1, const void *s2));
17128static int
17129#ifdef __BORLANDC__
17130 _RTLENTRYF
17131#endif
17132 item_compare2 __ARGS((const void *s1, const void *s2));
17133
17134static int item_compare_ic;
17135static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017136static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017137static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017138static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017139#define ITEM_COMPARE_FAIL 999
17140
Bram Moolenaar071d4272004-06-13 20:20:40 +000017141/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017142 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017144 static int
17145#ifdef __BORLANDC__
17146_RTLENTRYF
17147#endif
17148item_compare(s1, s2)
17149 const void *s1;
17150 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017152 char_u *p1, *p2;
17153 char_u *tofree1, *tofree2;
17154 int res;
17155 char_u numbuf1[NUMBUFLEN];
17156 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017158 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17159 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017160 if (p1 == NULL)
17161 p1 = (char_u *)"";
17162 if (p2 == NULL)
17163 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017164 if (item_compare_ic)
17165 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017167 res = STRCMP(p1, p2);
17168 vim_free(tofree1);
17169 vim_free(tofree2);
17170 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171}
17172
17173 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017174#ifdef __BORLANDC__
17175_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017177item_compare2(s1, s2)
17178 const void *s1;
17179 const void *s2;
17180{
17181 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017182 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017183 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017184 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017186 /* shortcut after failure in previous call; compare all items equal */
17187 if (item_compare_func_err)
17188 return 0;
17189
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017190 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17191 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017192 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17193 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017194
17195 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017196 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017197 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17198 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017199 clear_tv(&argv[0]);
17200 clear_tv(&argv[1]);
17201
17202 if (res == FAIL)
17203 res = ITEM_COMPARE_FAIL;
17204 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017205 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17206 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017207 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017208 clear_tv(&rettv);
17209 return res;
17210}
17211
17212/*
17213 * "sort({list})" function
17214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017216do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017217 typval_T *argvars;
17218 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017219 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220{
Bram Moolenaar33570922005-01-25 22:26:29 +000017221 list_T *l;
17222 listitem_T *li;
17223 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017224 long len;
17225 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226
Bram Moolenaar0d660222005-01-07 21:51:51 +000017227 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017228 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 else
17230 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017231 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017232 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017233 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017234 return;
17235 rettv->vval.v_list = l;
17236 rettv->v_type = VAR_LIST;
17237 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238
Bram Moolenaar0d660222005-01-07 21:51:51 +000017239 len = list_len(l);
17240 if (len <= 1)
17241 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242
Bram Moolenaar0d660222005-01-07 21:51:51 +000017243 item_compare_ic = FALSE;
17244 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017245 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017246 if (argvars[1].v_type != VAR_UNKNOWN)
17247 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017248 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017249 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017250 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017251 else
17252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017253 int error = FALSE;
17254
17255 i = get_tv_number_chk(&argvars[1], &error);
17256 if (error)
17257 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017258 if (i == 1)
17259 item_compare_ic = TRUE;
17260 else
17261 item_compare_func = get_tv_string(&argvars[1]);
17262 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017263
17264 if (argvars[2].v_type != VAR_UNKNOWN)
17265 {
17266 /* optional third argument: {dict} */
17267 if (argvars[2].v_type != VAR_DICT)
17268 {
17269 EMSG(_(e_dictreq));
17270 return;
17271 }
17272 item_compare_selfdict = argvars[2].vval.v_dict;
17273 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275
Bram Moolenaar0d660222005-01-07 21:51:51 +000017276 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017277 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017278 if (ptrs == NULL)
17279 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280
Bram Moolenaar327aa022014-03-25 18:24:23 +010017281 i = 0;
17282 if (sort)
17283 {
17284 /* sort(): ptrs will be the list to sort */
17285 for (li = l->lv_first; li != NULL; li = li->li_next)
17286 ptrs[i++] = li;
17287
17288 item_compare_func_err = FALSE;
17289 /* test the compare function */
17290 if (item_compare_func != NULL
17291 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017292 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017293 EMSG(_("E702: Sort compare function failed"));
17294 else
17295 {
17296 /* Sort the array with item pointers. */
17297 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17298 item_compare_func == NULL ? item_compare : item_compare2);
17299
17300 if (!item_compare_func_err)
17301 {
17302 /* Clear the List and append the items in sorted order. */
17303 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17304 l->lv_len = 0;
17305 for (i = 0; i < len; ++i)
17306 list_append(l, ptrs[i]);
17307 }
17308 }
17309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017311 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017312 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17313
17314 /* f_uniq(): ptrs will be a stack of items to remove */
17315 item_compare_func_err = FALSE;
17316 item_compare_func_ptr = item_compare_func
17317 ? item_compare2 : item_compare;
17318
17319 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17320 li = li->li_next)
17321 {
17322 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17323 == 0)
17324 ptrs[i++] = li;
17325 if (item_compare_func_err)
17326 {
17327 EMSG(_("E882: Uniq compare function failed"));
17328 break;
17329 }
17330 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017332 if (!item_compare_func_err)
17333 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017334 while (--i >= 0)
17335 {
17336 li = ptrs[i]->li_next;
17337 ptrs[i]->li_next = li->li_next;
17338 if (li->li_next != NULL)
17339 li->li_next->li_prev = ptrs[i];
17340 else
17341 l->lv_last = ptrs[i];
17342 list_fix_watch(l, li);
17343 listitem_free(li);
17344 l->lv_len--;
17345 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017346 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017347 }
17348
17349 vim_free(ptrs);
17350 }
17351}
17352
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017353/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017354 * "sort({list})" function
17355 */
17356 static void
17357f_sort(argvars, rettv)
17358 typval_T *argvars;
17359 typval_T *rettv;
17360{
17361 do_sort_uniq(argvars, rettv, TRUE);
17362}
17363
17364/*
17365 * "uniq({list})" function
17366 */
17367 static void
17368f_uniq(argvars, rettv)
17369 typval_T *argvars;
17370 typval_T *rettv;
17371{
17372 do_sort_uniq(argvars, rettv, FALSE);
17373}
17374
17375/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017376 * "soundfold({word})" function
17377 */
17378 static void
17379f_soundfold(argvars, rettv)
17380 typval_T *argvars;
17381 typval_T *rettv;
17382{
17383 char_u *s;
17384
17385 rettv->v_type = VAR_STRING;
17386 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017387#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017388 rettv->vval.v_string = eval_soundfold(s);
17389#else
17390 rettv->vval.v_string = vim_strsave(s);
17391#endif
17392}
17393
17394/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017395 * "spellbadword()" function
17396 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017397 static void
17398f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017399 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017400 typval_T *rettv;
17401{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017402 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017403 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017404 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017405
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017406 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017407 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017408
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017409#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017410 if (argvars[0].v_type == VAR_UNKNOWN)
17411 {
17412 /* Find the start and length of the badly spelled word. */
17413 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17414 if (len != 0)
17415 word = ml_get_cursor();
17416 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017417 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017418 {
17419 char_u *str = get_tv_string_chk(&argvars[0]);
17420 int capcol = -1;
17421
17422 if (str != NULL)
17423 {
17424 /* Check the argument for spelling. */
17425 while (*str != NUL)
17426 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017427 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017428 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017429 {
17430 word = str;
17431 break;
17432 }
17433 str += len;
17434 }
17435 }
17436 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017437#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017438
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017439 list_append_string(rettv->vval.v_list, word, len);
17440 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017441 attr == HLF_SPB ? "bad" :
17442 attr == HLF_SPR ? "rare" :
17443 attr == HLF_SPL ? "local" :
17444 attr == HLF_SPC ? "caps" :
17445 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017446}
17447
17448/*
17449 * "spellsuggest()" function
17450 */
17451 static void
17452f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017453 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017454 typval_T *rettv;
17455{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017456#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017457 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017458 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017459 int maxcount;
17460 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017461 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017462 listitem_T *li;
17463 int need_capital = FALSE;
17464#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017465
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017466 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017467 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017468
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017469#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017470 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017471 {
17472 str = get_tv_string(&argvars[0]);
17473 if (argvars[1].v_type != VAR_UNKNOWN)
17474 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017475 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017476 if (maxcount <= 0)
17477 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017478 if (argvars[2].v_type != VAR_UNKNOWN)
17479 {
17480 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17481 if (typeerr)
17482 return;
17483 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017484 }
17485 else
17486 maxcount = 25;
17487
Bram Moolenaar4770d092006-01-12 23:22:24 +000017488 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017489
17490 for (i = 0; i < ga.ga_len; ++i)
17491 {
17492 str = ((char_u **)ga.ga_data)[i];
17493
17494 li = listitem_alloc();
17495 if (li == NULL)
17496 vim_free(str);
17497 else
17498 {
17499 li->li_tv.v_type = VAR_STRING;
17500 li->li_tv.v_lock = 0;
17501 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017502 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017503 }
17504 }
17505 ga_clear(&ga);
17506 }
17507#endif
17508}
17509
Bram Moolenaar0d660222005-01-07 21:51:51 +000017510 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017511f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017512 typval_T *argvars;
17513 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017514{
17515 char_u *str;
17516 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017517 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017518 regmatch_T regmatch;
17519 char_u patbuf[NUMBUFLEN];
17520 char_u *save_cpo;
17521 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017522 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017523 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017524 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017525
17526 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17527 save_cpo = p_cpo;
17528 p_cpo = (char_u *)"";
17529
17530 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017531 if (argvars[1].v_type != VAR_UNKNOWN)
17532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017533 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17534 if (pat == NULL)
17535 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017536 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017537 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017538 }
17539 if (pat == NULL || *pat == NUL)
17540 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017541
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017542 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017544 if (typeerr)
17545 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546
Bram Moolenaar0d660222005-01-07 21:51:51 +000017547 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17548 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017550 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017551 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017552 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017553 if (*str == NUL)
17554 match = FALSE; /* empty item at the end */
17555 else
17556 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017557 if (match)
17558 end = regmatch.startp[0];
17559 else
17560 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017561 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17562 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017563 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017564 if (list_append_string(rettv->vval.v_list, str,
17565 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017566 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017567 }
17568 if (!match)
17569 break;
17570 /* Advance to just after the match. */
17571 if (regmatch.endp[0] > str)
17572 col = 0;
17573 else
17574 {
17575 /* Don't get stuck at the same match. */
17576#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017577 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017578#else
17579 col = 1;
17580#endif
17581 }
17582 str = regmatch.endp[0];
17583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584
Bram Moolenaar473de612013-06-08 18:19:48 +020017585 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587
Bram Moolenaar0d660222005-01-07 21:51:51 +000017588 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589}
17590
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017591#ifdef FEAT_FLOAT
17592/*
17593 * "sqrt()" function
17594 */
17595 static void
17596f_sqrt(argvars, rettv)
17597 typval_T *argvars;
17598 typval_T *rettv;
17599{
17600 float_T f;
17601
17602 rettv->v_type = VAR_FLOAT;
17603 if (get_float_arg(argvars, &f) == OK)
17604 rettv->vval.v_float = sqrt(f);
17605 else
17606 rettv->vval.v_float = 0.0;
17607}
17608
17609/*
17610 * "str2float()" function
17611 */
17612 static void
17613f_str2float(argvars, rettv)
17614 typval_T *argvars;
17615 typval_T *rettv;
17616{
17617 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17618
17619 if (*p == '+')
17620 p = skipwhite(p + 1);
17621 (void)string2float(p, &rettv->vval.v_float);
17622 rettv->v_type = VAR_FLOAT;
17623}
17624#endif
17625
Bram Moolenaar2c932302006-03-18 21:42:09 +000017626/*
17627 * "str2nr()" function
17628 */
17629 static void
17630f_str2nr(argvars, rettv)
17631 typval_T *argvars;
17632 typval_T *rettv;
17633{
17634 int base = 10;
17635 char_u *p;
17636 long n;
17637
17638 if (argvars[1].v_type != VAR_UNKNOWN)
17639 {
17640 base = get_tv_number(&argvars[1]);
17641 if (base != 8 && base != 10 && base != 16)
17642 {
17643 EMSG(_(e_invarg));
17644 return;
17645 }
17646 }
17647
17648 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017649 if (*p == '+')
17650 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017651 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17652 rettv->vval.v_number = n;
17653}
17654
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655#ifdef HAVE_STRFTIME
17656/*
17657 * "strftime({format}[, {time}])" function
17658 */
17659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017660f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017661 typval_T *argvars;
17662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663{
17664 char_u result_buf[256];
17665 struct tm *curtime;
17666 time_t seconds;
17667 char_u *p;
17668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017669 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017671 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017672 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673 seconds = time(NULL);
17674 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017675 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676 curtime = localtime(&seconds);
17677 /* MSVC returns NULL for an invalid value of seconds. */
17678 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017679 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680 else
17681 {
17682# ifdef FEAT_MBYTE
17683 vimconv_T conv;
17684 char_u *enc;
17685
17686 conv.vc_type = CONV_NONE;
17687 enc = enc_locale();
17688 convert_setup(&conv, p_enc, enc);
17689 if (conv.vc_type != CONV_NONE)
17690 p = string_convert(&conv, p, NULL);
17691# endif
17692 if (p != NULL)
17693 (void)strftime((char *)result_buf, sizeof(result_buf),
17694 (char *)p, curtime);
17695 else
17696 result_buf[0] = NUL;
17697
17698# ifdef FEAT_MBYTE
17699 if (conv.vc_type != CONV_NONE)
17700 vim_free(p);
17701 convert_setup(&conv, enc, p_enc);
17702 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017703 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 else
17705# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017706 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707
17708# ifdef FEAT_MBYTE
17709 /* Release conversion descriptors */
17710 convert_setup(&conv, NULL, NULL);
17711 vim_free(enc);
17712# endif
17713 }
17714}
17715#endif
17716
17717/*
17718 * "stridx()" function
17719 */
17720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017721f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017722 typval_T *argvars;
17723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724{
17725 char_u buf[NUMBUFLEN];
17726 char_u *needle;
17727 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017728 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017730 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017732 needle = get_tv_string_chk(&argvars[1]);
17733 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017734 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017735 if (needle == NULL || haystack == NULL)
17736 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737
Bram Moolenaar33570922005-01-25 22:26:29 +000017738 if (argvars[2].v_type != VAR_UNKNOWN)
17739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017740 int error = FALSE;
17741
17742 start_idx = get_tv_number_chk(&argvars[2], &error);
17743 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017744 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017745 if (start_idx >= 0)
17746 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017747 }
17748
17749 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17750 if (pos != NULL)
17751 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752}
17753
17754/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017755 * "string()" function
17756 */
17757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017758f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017759 typval_T *argvars;
17760 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017761{
17762 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017763 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017765 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017766 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017767 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017768 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017769 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770}
17771
17772/*
17773 * "strlen()" function
17774 */
17775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017776f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017777 typval_T *argvars;
17778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017780 rettv->vval.v_number = (varnumber_T)(STRLEN(
17781 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782}
17783
17784/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017785 * "strchars()" function
17786 */
17787 static void
17788f_strchars(argvars, rettv)
17789 typval_T *argvars;
17790 typval_T *rettv;
17791{
17792 char_u *s = get_tv_string(&argvars[0]);
17793#ifdef FEAT_MBYTE
17794 varnumber_T len = 0;
17795
17796 while (*s != NUL)
17797 {
17798 mb_cptr2char_adv(&s);
17799 ++len;
17800 }
17801 rettv->vval.v_number = len;
17802#else
17803 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17804#endif
17805}
17806
17807/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017808 * "strdisplaywidth()" function
17809 */
17810 static void
17811f_strdisplaywidth(argvars, rettv)
17812 typval_T *argvars;
17813 typval_T *rettv;
17814{
17815 char_u *s = get_tv_string(&argvars[0]);
17816 int col = 0;
17817
17818 if (argvars[1].v_type != VAR_UNKNOWN)
17819 col = get_tv_number(&argvars[1]);
17820
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017821 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017822}
17823
17824/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017825 * "strwidth()" function
17826 */
17827 static void
17828f_strwidth(argvars, rettv)
17829 typval_T *argvars;
17830 typval_T *rettv;
17831{
17832 char_u *s = get_tv_string(&argvars[0]);
17833
17834 rettv->vval.v_number = (varnumber_T)(
17835#ifdef FEAT_MBYTE
17836 mb_string2cells(s, -1)
17837#else
17838 STRLEN(s)
17839#endif
17840 );
17841}
17842
17843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844 * "strpart()" function
17845 */
17846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017847f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017848 typval_T *argvars;
17849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850{
17851 char_u *p;
17852 int n;
17853 int len;
17854 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017855 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017857 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858 slen = (int)STRLEN(p);
17859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017860 n = get_tv_number_chk(&argvars[1], &error);
17861 if (error)
17862 len = 0;
17863 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017864 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 else
17866 len = slen - n; /* default len: all bytes that are available. */
17867
17868 /*
17869 * Only return the overlap between the specified part and the actual
17870 * string.
17871 */
17872 if (n < 0)
17873 {
17874 len += n;
17875 n = 0;
17876 }
17877 else if (n > slen)
17878 n = slen;
17879 if (len < 0)
17880 len = 0;
17881 else if (n + len > slen)
17882 len = slen - n;
17883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017884 rettv->v_type = VAR_STRING;
17885 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886}
17887
17888/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017889 * "strridx()" function
17890 */
17891 static void
17892f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017893 typval_T *argvars;
17894 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017895{
17896 char_u buf[NUMBUFLEN];
17897 char_u *needle;
17898 char_u *haystack;
17899 char_u *rest;
17900 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017901 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017903 needle = get_tv_string_chk(&argvars[1]);
17904 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017905
17906 rettv->vval.v_number = -1;
17907 if (needle == NULL || haystack == NULL)
17908 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017909
17910 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017911 if (argvars[2].v_type != VAR_UNKNOWN)
17912 {
17913 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017914 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017915 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017916 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017917 }
17918 else
17919 end_idx = haystack_len;
17920
Bram Moolenaar0d660222005-01-07 21:51:51 +000017921 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017922 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017923 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017924 lastmatch = haystack + end_idx;
17925 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017926 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017927 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017928 for (rest = haystack; *rest != '\0'; ++rest)
17929 {
17930 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017931 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017932 break;
17933 lastmatch = rest;
17934 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017935 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017936
17937 if (lastmatch == NULL)
17938 rettv->vval.v_number = -1;
17939 else
17940 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17941}
17942
17943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944 * "strtrans()" function
17945 */
17946 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017947f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017948 typval_T *argvars;
17949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017951 rettv->v_type = VAR_STRING;
17952 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953}
17954
17955/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017956 * "submatch()" function
17957 */
17958 static void
17959f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017960 typval_T *argvars;
17961 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017962{
Bram Moolenaar41571762014-04-02 19:00:58 +020017963 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020017964 int no;
17965 int retList = 0;
17966
17967 no = (int)get_tv_number_chk(&argvars[0], &error);
17968 if (error)
17969 return;
17970 error = FALSE;
17971 if (argvars[1].v_type != VAR_UNKNOWN)
17972 retList = get_tv_number_chk(&argvars[1], &error);
17973 if (error)
17974 return;
17975
17976 if (retList == 0)
17977 {
17978 rettv->v_type = VAR_STRING;
17979 rettv->vval.v_string = reg_submatch(no);
17980 }
17981 else
17982 {
17983 rettv->v_type = VAR_LIST;
17984 rettv->vval.v_list = reg_submatch_list(no);
17985 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017986}
17987
17988/*
17989 * "substitute()" function
17990 */
17991 static void
17992f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017993 typval_T *argvars;
17994 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017995{
17996 char_u patbuf[NUMBUFLEN];
17997 char_u subbuf[NUMBUFLEN];
17998 char_u flagsbuf[NUMBUFLEN];
17999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018000 char_u *str = get_tv_string_chk(&argvars[0]);
18001 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18002 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18003 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18004
Bram Moolenaar0d660222005-01-07 21:51:51 +000018005 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018006 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18007 rettv->vval.v_string = NULL;
18008 else
18009 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018010}
18011
18012/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018013 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018016f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018017 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019{
18020 int id = 0;
18021#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018022 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 long col;
18024 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018025 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018027 lnum = get_tv_lnum(argvars); /* -1 on type error */
18028 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18029 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018031 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018032 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018033 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034#endif
18035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018036 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037}
18038
18039/*
18040 * "synIDattr(id, what [, mode])" function
18041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018043f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018044 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046{
18047 char_u *p = NULL;
18048#ifdef FEAT_SYN_HL
18049 int id;
18050 char_u *what;
18051 char_u *mode;
18052 char_u modebuf[NUMBUFLEN];
18053 int modec;
18054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018055 id = get_tv_number(&argvars[0]);
18056 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018057 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018059 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018061 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 modec = 0; /* replace invalid with current */
18063 }
18064 else
18065 {
18066#ifdef FEAT_GUI
18067 if (gui.in_use)
18068 modec = 'g';
18069 else
18070#endif
18071 if (t_colors > 1)
18072 modec = 'c';
18073 else
18074 modec = 't';
18075 }
18076
18077
18078 switch (TOLOWER_ASC(what[0]))
18079 {
18080 case 'b':
18081 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18082 p = highlight_color(id, what, modec);
18083 else /* bold */
18084 p = highlight_has_attr(id, HL_BOLD, modec);
18085 break;
18086
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018087 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 p = highlight_color(id, what, modec);
18089 break;
18090
18091 case 'i':
18092 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18093 p = highlight_has_attr(id, HL_INVERSE, modec);
18094 else /* italic */
18095 p = highlight_has_attr(id, HL_ITALIC, modec);
18096 break;
18097
18098 case 'n': /* name */
18099 p = get_highlight_name(NULL, id - 1);
18100 break;
18101
18102 case 'r': /* reverse */
18103 p = highlight_has_attr(id, HL_INVERSE, modec);
18104 break;
18105
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018106 case 's':
18107 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18108 p = highlight_color(id, what, modec);
18109 else /* standout */
18110 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111 break;
18112
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018113 case 'u':
18114 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18115 /* underline */
18116 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18117 else
18118 /* undercurl */
18119 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 break;
18121 }
18122
18123 if (p != NULL)
18124 p = vim_strsave(p);
18125#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018126 rettv->v_type = VAR_STRING;
18127 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128}
18129
18130/*
18131 * "synIDtrans(id)" function
18132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018134f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018135 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137{
18138 int id;
18139
18140#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018141 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142
18143 if (id > 0)
18144 id = syn_get_final_id(id);
18145 else
18146#endif
18147 id = 0;
18148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018149 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150}
18151
18152/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018153 * "synconcealed(lnum, col)" function
18154 */
18155 static void
18156f_synconcealed(argvars, rettv)
18157 typval_T *argvars UNUSED;
18158 typval_T *rettv;
18159{
18160#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18161 long lnum;
18162 long col;
18163 int syntax_flags = 0;
18164 int cchar;
18165 int matchid = 0;
18166 char_u str[NUMBUFLEN];
18167#endif
18168
18169 rettv->v_type = VAR_LIST;
18170 rettv->vval.v_list = NULL;
18171
18172#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18173 lnum = get_tv_lnum(argvars); /* -1 on type error */
18174 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18175
18176 vim_memset(str, NUL, sizeof(str));
18177
18178 if (rettv_list_alloc(rettv) != FAIL)
18179 {
18180 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18181 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18182 && curwin->w_p_cole > 0)
18183 {
18184 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18185 syntax_flags = get_syntax_info(&matchid);
18186
18187 /* get the conceal character */
18188 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18189 {
18190 cchar = syn_get_sub_char();
18191 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18192 cchar = lcs_conceal;
18193 if (cchar != NUL)
18194 {
18195# ifdef FEAT_MBYTE
18196 if (has_mbyte)
18197 (*mb_char2bytes)(cchar, str);
18198 else
18199# endif
18200 str[0] = cchar;
18201 }
18202 }
18203 }
18204
18205 list_append_number(rettv->vval.v_list,
18206 (syntax_flags & HL_CONCEAL) != 0);
18207 /* -1 to auto-determine strlen */
18208 list_append_string(rettv->vval.v_list, str, -1);
18209 list_append_number(rettv->vval.v_list, matchid);
18210 }
18211#endif
18212}
18213
18214/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018215 * "synstack(lnum, col)" function
18216 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018217 static void
18218f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018219 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018220 typval_T *rettv;
18221{
18222#ifdef FEAT_SYN_HL
18223 long lnum;
18224 long col;
18225 int i;
18226 int id;
18227#endif
18228
18229 rettv->v_type = VAR_LIST;
18230 rettv->vval.v_list = NULL;
18231
18232#ifdef FEAT_SYN_HL
18233 lnum = get_tv_lnum(argvars); /* -1 on type error */
18234 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18235
18236 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018237 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018238 && rettv_list_alloc(rettv) != FAIL)
18239 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018240 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018241 for (i = 0; ; ++i)
18242 {
18243 id = syn_get_stack_item(i);
18244 if (id < 0)
18245 break;
18246 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18247 break;
18248 }
18249 }
18250#endif
18251}
18252
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018254get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018255 typval_T *argvars;
18256 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018257 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018259 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018261 char_u *infile = NULL;
18262 char_u buf[NUMBUFLEN];
18263 int err = FALSE;
18264 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018265 list_T *list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018267 rettv->v_type = VAR_STRING;
18268 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018269 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018270 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018271
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018272 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018273 {
18274 /*
18275 * Write the string to a temp file, to be used for input of the shell
18276 * command.
18277 */
18278 if ((infile = vim_tempname('i')) == NULL)
18279 {
18280 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018281 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018282 }
18283
18284 fd = mch_fopen((char *)infile, WRITEBIN);
18285 if (fd == NULL)
18286 {
18287 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018288 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018289 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018290 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018291 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018292 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18293 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018294 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018295 else
18296 {
18297 p = get_tv_string_buf_chk(&argvars[1], buf);
18298 if (p == NULL)
18299 {
18300 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018301 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018302 }
18303 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18304 err = TRUE;
18305 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018306 if (fclose(fd) != 0)
18307 err = TRUE;
18308 if (err)
18309 {
18310 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018311 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018312 }
18313 }
18314
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018315 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018317 int len;
18318 listitem_T *li;
18319 char_u *s = NULL;
18320 char_u *start;
18321 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018322 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018324 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18325 SHELL_SILENT | SHELL_COOKED, &len);
18326 if (res == NULL)
18327 goto errret;
18328
18329 list = list_alloc();
18330 if (list == NULL)
18331 goto errret;
18332
18333 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018335 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018336 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018337 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018338 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018339
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018340 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018341 if (s == NULL)
18342 goto errret;
18343
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018344 for (p = s; start < end; ++p, ++start)
18345 *p = *start == NUL ? NL : *start;
18346 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018347
18348 li = listitem_alloc();
18349 if (li == NULL)
18350 {
18351 vim_free(s);
18352 goto errret;
18353 }
18354 li->li_tv.v_type = VAR_STRING;
18355 li->li_tv.vval.v_string = s;
18356 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018358
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018359 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018360 rettv->v_type = VAR_LIST;
18361 rettv->vval.v_list = list;
18362 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018364 else
18365 {
18366 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18367 SHELL_SILENT | SHELL_COOKED, NULL);
18368#ifdef USE_CR
18369 /* translate <CR> into <NL> */
18370 if (res != NULL)
18371 {
18372 char_u *s;
18373
18374 for (s = res; *s; ++s)
18375 {
18376 if (*s == CAR)
18377 *s = NL;
18378 }
18379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380#else
18381# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018382 /* translate <CR><NL> into <NL> */
18383 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018385 char_u *s, *d;
18386
18387 d = res;
18388 for (s = res; *s; ++s)
18389 {
18390 if (s[0] == CAR && s[1] == NL)
18391 ++s;
18392 *d++ = *s;
18393 }
18394 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396# endif
18397#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018398 rettv->vval.v_string = res;
18399 res = NULL;
18400 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018401
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018402errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018403 if (infile != NULL)
18404 {
18405 mch_remove(infile);
18406 vim_free(infile);
18407 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018408 if (res != NULL)
18409 vim_free(res);
18410 if (list != NULL)
18411 list_free(list, TRUE);
18412}
18413
18414/*
18415 * "system()" function
18416 */
18417 static void
18418f_system(argvars, rettv)
18419 typval_T *argvars;
18420 typval_T *rettv;
18421{
18422 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18423}
18424
18425/*
18426 * "systemlist()" function
18427 */
18428 static void
18429f_systemlist(argvars, rettv)
18430 typval_T *argvars;
18431 typval_T *rettv;
18432{
18433 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434}
18435
18436/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018437 * "tabpagebuflist()" function
18438 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018439 static void
18440f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018441 typval_T *argvars UNUSED;
18442 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018443{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018444#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018445 tabpage_T *tp;
18446 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018447
18448 if (argvars[0].v_type == VAR_UNKNOWN)
18449 wp = firstwin;
18450 else
18451 {
18452 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18453 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018454 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018455 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018456 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018457 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018458 for (; wp != NULL; wp = wp->w_next)
18459 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018460 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018461 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018462 }
18463#endif
18464}
18465
18466
18467/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018468 * "tabpagenr()" function
18469 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018470 static void
18471f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018472 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018473 typval_T *rettv;
18474{
18475 int nr = 1;
18476#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018477 char_u *arg;
18478
18479 if (argvars[0].v_type != VAR_UNKNOWN)
18480 {
18481 arg = get_tv_string_chk(&argvars[0]);
18482 nr = 0;
18483 if (arg != NULL)
18484 {
18485 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018486 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018487 else
18488 EMSG2(_(e_invexpr2), arg);
18489 }
18490 }
18491 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018492 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018493#endif
18494 rettv->vval.v_number = nr;
18495}
18496
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018497
18498#ifdef FEAT_WINDOWS
18499static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18500
18501/*
18502 * Common code for tabpagewinnr() and winnr().
18503 */
18504 static int
18505get_winnr(tp, argvar)
18506 tabpage_T *tp;
18507 typval_T *argvar;
18508{
18509 win_T *twin;
18510 int nr = 1;
18511 win_T *wp;
18512 char_u *arg;
18513
18514 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18515 if (argvar->v_type != VAR_UNKNOWN)
18516 {
18517 arg = get_tv_string_chk(argvar);
18518 if (arg == NULL)
18519 nr = 0; /* type error; errmsg already given */
18520 else if (STRCMP(arg, "$") == 0)
18521 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18522 else if (STRCMP(arg, "#") == 0)
18523 {
18524 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18525 if (twin == NULL)
18526 nr = 0;
18527 }
18528 else
18529 {
18530 EMSG2(_(e_invexpr2), arg);
18531 nr = 0;
18532 }
18533 }
18534
18535 if (nr > 0)
18536 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18537 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018538 {
18539 if (wp == NULL)
18540 {
18541 /* didn't find it in this tabpage */
18542 nr = 0;
18543 break;
18544 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018545 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018546 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018547 return nr;
18548}
18549#endif
18550
18551/*
18552 * "tabpagewinnr()" function
18553 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018554 static void
18555f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018556 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018557 typval_T *rettv;
18558{
18559 int nr = 1;
18560#ifdef FEAT_WINDOWS
18561 tabpage_T *tp;
18562
18563 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18564 if (tp == NULL)
18565 nr = 0;
18566 else
18567 nr = get_winnr(tp, &argvars[1]);
18568#endif
18569 rettv->vval.v_number = nr;
18570}
18571
18572
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018573/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018574 * "tagfiles()" function
18575 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018576 static void
18577f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018578 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018579 typval_T *rettv;
18580{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018581 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018582 tagname_T tn;
18583 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018584
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018585 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018586 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018587 fname = alloc(MAXPATHL);
18588 if (fname == NULL)
18589 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018590
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018591 for (first = TRUE; ; first = FALSE)
18592 if (get_tagfname(&tn, first, fname) == FAIL
18593 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018594 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018595 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018596 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018597}
18598
18599/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018600 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018601 */
18602 static void
18603f_taglist(argvars, rettv)
18604 typval_T *argvars;
18605 typval_T *rettv;
18606{
18607 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018608
18609 tag_pattern = get_tv_string(&argvars[0]);
18610
18611 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018612 if (*tag_pattern == NUL)
18613 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018614
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018615 if (rettv_list_alloc(rettv) == OK)
18616 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018617}
18618
18619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620 * "tempname()" function
18621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018623f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018624 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626{
18627 static int x = 'A';
18628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018629 rettv->v_type = VAR_STRING;
18630 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631
18632 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18633 * names. Skip 'I' and 'O', they are used for shell redirection. */
18634 do
18635 {
18636 if (x == 'Z')
18637 x = '0';
18638 else if (x == '9')
18639 x = 'A';
18640 else
18641 {
18642#ifdef EBCDIC
18643 if (x == 'I')
18644 x = 'J';
18645 else if (x == 'R')
18646 x = 'S';
18647 else
18648#endif
18649 ++x;
18650 }
18651 } while (x == 'I' || x == 'O');
18652}
18653
18654/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018655 * "test(list)" function: Just checking the walls...
18656 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018657 static void
18658f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018659 typval_T *argvars UNUSED;
18660 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018661{
18662 /* Used for unit testing. Change the code below to your liking. */
18663#if 0
18664 listitem_T *li;
18665 list_T *l;
18666 char_u *bad, *good;
18667
18668 if (argvars[0].v_type != VAR_LIST)
18669 return;
18670 l = argvars[0].vval.v_list;
18671 if (l == NULL)
18672 return;
18673 li = l->lv_first;
18674 if (li == NULL)
18675 return;
18676 bad = get_tv_string(&li->li_tv);
18677 li = li->li_next;
18678 if (li == NULL)
18679 return;
18680 good = get_tv_string(&li->li_tv);
18681 rettv->vval.v_number = test_edit_score(bad, good);
18682#endif
18683}
18684
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018685#ifdef FEAT_FLOAT
18686/*
18687 * "tan()" function
18688 */
18689 static void
18690f_tan(argvars, rettv)
18691 typval_T *argvars;
18692 typval_T *rettv;
18693{
18694 float_T f;
18695
18696 rettv->v_type = VAR_FLOAT;
18697 if (get_float_arg(argvars, &f) == OK)
18698 rettv->vval.v_float = tan(f);
18699 else
18700 rettv->vval.v_float = 0.0;
18701}
18702
18703/*
18704 * "tanh()" function
18705 */
18706 static void
18707f_tanh(argvars, rettv)
18708 typval_T *argvars;
18709 typval_T *rettv;
18710{
18711 float_T f;
18712
18713 rettv->v_type = VAR_FLOAT;
18714 if (get_float_arg(argvars, &f) == OK)
18715 rettv->vval.v_float = tanh(f);
18716 else
18717 rettv->vval.v_float = 0.0;
18718}
18719#endif
18720
Bram Moolenaard52d9742005-08-21 22:20:28 +000018721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722 * "tolower(string)" function
18723 */
18724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018725f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018726 typval_T *argvars;
18727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728{
18729 char_u *p;
18730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018731 p = vim_strsave(get_tv_string(&argvars[0]));
18732 rettv->v_type = VAR_STRING;
18733 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734
18735 if (p != NULL)
18736 while (*p != NUL)
18737 {
18738#ifdef FEAT_MBYTE
18739 int l;
18740
18741 if (enc_utf8)
18742 {
18743 int c, lc;
18744
18745 c = utf_ptr2char(p);
18746 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018747 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 /* TODO: reallocate string when byte count changes. */
18749 if (utf_char2len(lc) == l)
18750 utf_char2bytes(lc, p);
18751 p += l;
18752 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018753 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 p += l; /* skip multi-byte character */
18755 else
18756#endif
18757 {
18758 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18759 ++p;
18760 }
18761 }
18762}
18763
18764/*
18765 * "toupper(string)" function
18766 */
18767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018768f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018769 typval_T *argvars;
18770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018772 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018773 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774}
18775
18776/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018777 * "tr(string, fromstr, tostr)" function
18778 */
18779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018780f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018781 typval_T *argvars;
18782 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018783{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018784 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018785 char_u *fromstr;
18786 char_u *tostr;
18787 char_u *p;
18788#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018789 int inlen;
18790 int fromlen;
18791 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018792 int idx;
18793 char_u *cpstr;
18794 int cplen;
18795 int first = TRUE;
18796#endif
18797 char_u buf[NUMBUFLEN];
18798 char_u buf2[NUMBUFLEN];
18799 garray_T ga;
18800
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018801 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018802 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18803 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018804
18805 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018806 rettv->v_type = VAR_STRING;
18807 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018808 if (fromstr == NULL || tostr == NULL)
18809 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018810 ga_init2(&ga, (int)sizeof(char), 80);
18811
18812#ifdef FEAT_MBYTE
18813 if (!has_mbyte)
18814#endif
18815 /* not multi-byte: fromstr and tostr must be the same length */
18816 if (STRLEN(fromstr) != STRLEN(tostr))
18817 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018818#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018819error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018820#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018821 EMSG2(_(e_invarg2), fromstr);
18822 ga_clear(&ga);
18823 return;
18824 }
18825
18826 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018827 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018828 {
18829#ifdef FEAT_MBYTE
18830 if (has_mbyte)
18831 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018832 inlen = (*mb_ptr2len)(in_str);
18833 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018834 cplen = inlen;
18835 idx = 0;
18836 for (p = fromstr; *p != NUL; p += fromlen)
18837 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018838 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018839 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018840 {
18841 for (p = tostr; *p != NUL; p += tolen)
18842 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018843 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018844 if (idx-- == 0)
18845 {
18846 cplen = tolen;
18847 cpstr = p;
18848 break;
18849 }
18850 }
18851 if (*p == NUL) /* tostr is shorter than fromstr */
18852 goto error;
18853 break;
18854 }
18855 ++idx;
18856 }
18857
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018858 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018859 {
18860 /* Check that fromstr and tostr have the same number of
18861 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018862 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018863 first = FALSE;
18864 for (p = tostr; *p != NUL; p += tolen)
18865 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018866 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018867 --idx;
18868 }
18869 if (idx != 0)
18870 goto error;
18871 }
18872
18873 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018874 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018875 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018876
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018877 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018878 }
18879 else
18880#endif
18881 {
18882 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018883 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018884 if (p != NULL)
18885 ga_append(&ga, tostr[p - fromstr]);
18886 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018887 ga_append(&ga, *in_str);
18888 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018889 }
18890 }
18891
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018892 /* add a terminating NUL */
18893 ga_grow(&ga, 1);
18894 ga_append(&ga, NUL);
18895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018896 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018897}
18898
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018899#ifdef FEAT_FLOAT
18900/*
18901 * "trunc({float})" function
18902 */
18903 static void
18904f_trunc(argvars, rettv)
18905 typval_T *argvars;
18906 typval_T *rettv;
18907{
18908 float_T f;
18909
18910 rettv->v_type = VAR_FLOAT;
18911 if (get_float_arg(argvars, &f) == OK)
18912 /* trunc() is not in C90, use floor() or ceil() instead. */
18913 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18914 else
18915 rettv->vval.v_float = 0.0;
18916}
18917#endif
18918
Bram Moolenaar8299df92004-07-10 09:47:34 +000018919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920 * "type(expr)" function
18921 */
18922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018923f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018924 typval_T *argvars;
18925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018927 int n;
18928
18929 switch (argvars[0].v_type)
18930 {
18931 case VAR_NUMBER: n = 0; break;
18932 case VAR_STRING: n = 1; break;
18933 case VAR_FUNC: n = 2; break;
18934 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018935 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018936#ifdef FEAT_FLOAT
18937 case VAR_FLOAT: n = 5; break;
18938#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018939 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18940 }
18941 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942}
18943
18944/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018945 * "undofile(name)" function
18946 */
18947 static void
18948f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018949 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018950 typval_T *rettv;
18951{
18952 rettv->v_type = VAR_STRING;
18953#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018954 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018955 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018956
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018957 if (*fname == NUL)
18958 {
18959 /* If there is no file name there will be no undo file. */
18960 rettv->vval.v_string = NULL;
18961 }
18962 else
18963 {
18964 char_u *ffname = FullName_save(fname, FALSE);
18965
18966 if (ffname != NULL)
18967 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18968 vim_free(ffname);
18969 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018970 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018971#else
18972 rettv->vval.v_string = NULL;
18973#endif
18974}
18975
18976/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018977 * "undotree()" function
18978 */
18979 static void
18980f_undotree(argvars, rettv)
18981 typval_T *argvars UNUSED;
18982 typval_T *rettv;
18983{
18984 if (rettv_dict_alloc(rettv) == OK)
18985 {
18986 dict_T *dict = rettv->vval.v_dict;
18987 list_T *list;
18988
Bram Moolenaar730cde92010-06-27 05:18:54 +020018989 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018990 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018991 dict_add_nr_str(dict, "save_last",
18992 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018993 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18994 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018995 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018996
18997 list = list_alloc();
18998 if (list != NULL)
18999 {
19000 u_eval_tree(curbuf->b_u_oldhead, list);
19001 dict_add_list(dict, "entries", list);
19002 }
19003 }
19004}
19005
19006/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019007 * "values(dict)" function
19008 */
19009 static void
19010f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019011 typval_T *argvars;
19012 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019013{
19014 dict_list(argvars, rettv, 1);
19015}
19016
19017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 * "virtcol(string)" function
19019 */
19020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019021f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019022 typval_T *argvars;
19023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024{
19025 colnr_T vcol = 0;
19026 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019027 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019029 fp = var2fpos(&argvars[0], FALSE, &fnum);
19030 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19031 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 {
19033 getvvcol(curwin, fp, NULL, NULL, &vcol);
19034 ++vcol;
19035 }
19036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019037 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038}
19039
19040/*
19041 * "visualmode()" function
19042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019044f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019045 typval_T *argvars UNUSED;
19046 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 char_u str[2];
19049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019050 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 str[0] = curbuf->b_visual_mode_eval;
19052 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019053 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054
19055 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019056 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058}
19059
19060/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019061 * "wildmenumode()" function
19062 */
19063 static void
19064f_wildmenumode(argvars, rettv)
19065 typval_T *argvars UNUSED;
19066 typval_T *rettv UNUSED;
19067{
19068#ifdef FEAT_WILDMENU
19069 if (wild_menu_showing)
19070 rettv->vval.v_number = 1;
19071#endif
19072}
19073
19074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075 * "winbufnr(nr)" function
19076 */
19077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019078f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019079 typval_T *argvars;
19080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081{
19082 win_T *wp;
19083
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019084 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019086 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019088 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089}
19090
19091/*
19092 * "wincol()" function
19093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019095f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019096 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098{
19099 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019100 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101}
19102
19103/*
19104 * "winheight(nr)" function
19105 */
19106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019107f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019108 typval_T *argvars;
19109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110{
19111 win_T *wp;
19112
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019113 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019115 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019117 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118}
19119
19120/*
19121 * "winline()" function
19122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019124f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019125 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019126 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127{
19128 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019129 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130}
19131
19132/*
19133 * "winnr()" function
19134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019136f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019137 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139{
19140 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019141
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019143 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019145 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019146}
19147
19148/*
19149 * "winrestcmd()" function
19150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019152f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019153 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155{
19156#ifdef FEAT_WINDOWS
19157 win_T *wp;
19158 int winnr = 1;
19159 garray_T ga;
19160 char_u buf[50];
19161
19162 ga_init2(&ga, (int)sizeof(char), 70);
19163 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19164 {
19165 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19166 ga_concat(&ga, buf);
19167# ifdef FEAT_VERTSPLIT
19168 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19169 ga_concat(&ga, buf);
19170# endif
19171 ++winnr;
19172 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019173 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019175 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019177 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019179 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180}
19181
19182/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019183 * "winrestview()" function
19184 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019185 static void
19186f_winrestview(argvars, rettv)
19187 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019188 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019189{
19190 dict_T *dict;
19191
19192 if (argvars[0].v_type != VAR_DICT
19193 || (dict = argvars[0].vval.v_dict) == NULL)
19194 EMSG(_(e_invarg));
19195 else
19196 {
19197 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19198 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
19199#ifdef FEAT_VIRTUALEDIT
19200 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
19201#endif
19202 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019203 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019204
Bram Moolenaar6f11a412006-09-06 20:16:42 +000019205 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019206#ifdef FEAT_DIFF
19207 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
19208#endif
19209 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19210 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
19211
19212 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019213 win_new_height(curwin, curwin->w_height);
19214# ifdef FEAT_VERTSPLIT
19215 win_new_width(curwin, W_WIDTH(curwin));
19216# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019217 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019218
19219 if (curwin->w_topline == 0)
19220 curwin->w_topline = 1;
19221 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19222 curwin->w_topline = curbuf->b_ml.ml_line_count;
19223#ifdef FEAT_DIFF
19224 check_topfill(curwin, TRUE);
19225#endif
19226 }
19227}
19228
19229/*
19230 * "winsaveview()" function
19231 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019232 static void
19233f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019234 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019235 typval_T *rettv;
19236{
19237 dict_T *dict;
19238
Bram Moolenaara800b422010-06-27 01:15:55 +020019239 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019240 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019241 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019242
19243 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19244 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19245#ifdef FEAT_VIRTUALEDIT
19246 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19247#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019248 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019249 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19250
19251 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19252#ifdef FEAT_DIFF
19253 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19254#endif
19255 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19256 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19257}
19258
19259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260 * "winwidth(nr)" function
19261 */
19262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019263f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019264 typval_T *argvars;
19265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266{
19267 win_T *wp;
19268
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019269 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019271 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272 else
19273#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019274 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019276 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277#endif
19278}
19279
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019281 * Write list of strings to file
19282 */
19283 static int
19284write_list(fd, list, binary)
19285 FILE *fd;
19286 list_T *list;
19287 int binary;
19288{
19289 listitem_T *li;
19290 int c;
19291 int ret = OK;
19292 char_u *s;
19293
19294 for (li = list->lv_first; li != NULL; li = li->li_next)
19295 {
19296 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19297 {
19298 if (*s == '\n')
19299 c = putc(NUL, fd);
19300 else
19301 c = putc(*s, fd);
19302 if (c == EOF)
19303 {
19304 ret = FAIL;
19305 break;
19306 }
19307 }
19308 if (!binary || li->li_next != NULL)
19309 if (putc('\n', fd) == EOF)
19310 {
19311 ret = FAIL;
19312 break;
19313 }
19314 if (ret == FAIL)
19315 {
19316 EMSG(_(e_write));
19317 break;
19318 }
19319 }
19320 return ret;
19321}
19322
19323/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019324 * "writefile()" function
19325 */
19326 static void
19327f_writefile(argvars, rettv)
19328 typval_T *argvars;
19329 typval_T *rettv;
19330{
19331 int binary = FALSE;
19332 char_u *fname;
19333 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019334 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019335
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019336 if (check_restricted() || check_secure())
19337 return;
19338
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019339 if (argvars[0].v_type != VAR_LIST)
19340 {
19341 EMSG2(_(e_listarg), "writefile()");
19342 return;
19343 }
19344 if (argvars[0].vval.v_list == NULL)
19345 return;
19346
19347 if (argvars[2].v_type != VAR_UNKNOWN
19348 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19349 binary = TRUE;
19350
19351 /* Always open the file in binary mode, library functions have a mind of
19352 * their own about CR-LF conversion. */
19353 fname = get_tv_string(&argvars[1]);
19354 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19355 {
19356 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19357 ret = -1;
19358 }
19359 else
19360 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019361 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19362 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019363 fclose(fd);
19364 }
19365
19366 rettv->vval.v_number = ret;
19367}
19368
19369/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019370 * "xor(expr, expr)" function
19371 */
19372 static void
19373f_xor(argvars, rettv)
19374 typval_T *argvars;
19375 typval_T *rettv;
19376{
19377 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19378 ^ get_tv_number_chk(&argvars[1], NULL);
19379}
19380
19381
19382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019384 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 */
19386 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019387var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019388 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019389 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019390 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019392 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019394 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019395
Bram Moolenaara5525202006-03-02 22:52:09 +000019396 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019397 if (varp->v_type == VAR_LIST)
19398 {
19399 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019400 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019401 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019402 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019403
19404 l = varp->vval.v_list;
19405 if (l == NULL)
19406 return NULL;
19407
19408 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019409 pos.lnum = list_find_nr(l, 0L, &error);
19410 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019411 return NULL; /* invalid line number */
19412
19413 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019414 pos.col = list_find_nr(l, 1L, &error);
19415 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019416 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019417 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019418
19419 /* We accept "$" for the column number: last column. */
19420 li = list_find(l, 1L);
19421 if (li != NULL && li->li_tv.v_type == VAR_STRING
19422 && li->li_tv.vval.v_string != NULL
19423 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19424 pos.col = len + 1;
19425
Bram Moolenaara5525202006-03-02 22:52:09 +000019426 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019427 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019428 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019429 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019430
Bram Moolenaara5525202006-03-02 22:52:09 +000019431#ifdef FEAT_VIRTUALEDIT
19432 /* Get the virtual offset. Defaults to zero. */
19433 pos.coladd = list_find_nr(l, 2L, &error);
19434 if (error)
19435 pos.coladd = 0;
19436#endif
19437
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019438 return &pos;
19439 }
19440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019441 name = get_tv_string_chk(varp);
19442 if (name == NULL)
19443 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019444 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019446 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19447 {
19448 if (VIsual_active)
19449 return &VIsual;
19450 return &curwin->w_cursor;
19451 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019452 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019454 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19456 return NULL;
19457 return pp;
19458 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019459
19460#ifdef FEAT_VIRTUALEDIT
19461 pos.coladd = 0;
19462#endif
19463
Bram Moolenaar477933c2007-07-17 14:32:23 +000019464 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019465 {
19466 pos.col = 0;
19467 if (name[1] == '0') /* "w0": first visible line */
19468 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019469 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019470 pos.lnum = curwin->w_topline;
19471 return &pos;
19472 }
19473 else if (name[1] == '$') /* "w$": last visible line */
19474 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019475 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019476 pos.lnum = curwin->w_botline - 1;
19477 return &pos;
19478 }
19479 }
19480 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019482 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483 {
19484 pos.lnum = curbuf->b_ml.ml_line_count;
19485 pos.col = 0;
19486 }
19487 else
19488 {
19489 pos.lnum = curwin->w_cursor.lnum;
19490 pos.col = (colnr_T)STRLEN(ml_get_curline());
19491 }
19492 return &pos;
19493 }
19494 return NULL;
19495}
19496
19497/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019498 * Convert list in "arg" into a position and optional file number.
19499 * When "fnump" is NULL there is no file number, only 3 items.
19500 * Note that the column is passed on as-is, the caller may want to decrement
19501 * it to use 1 for the first column.
19502 * Return FAIL when conversion is not possible, doesn't check the position for
19503 * validity.
19504 */
19505 static int
19506list2fpos(arg, posp, fnump)
19507 typval_T *arg;
19508 pos_T *posp;
19509 int *fnump;
19510{
19511 list_T *l = arg->vval.v_list;
19512 long i = 0;
19513 long n;
19514
Bram Moolenaarbde35262006-07-23 20:12:24 +000019515 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19516 * when "fnump" isn't NULL and "coladd" is optional. */
19517 if (arg->v_type != VAR_LIST
19518 || l == NULL
19519 || l->lv_len < (fnump == NULL ? 2 : 3)
19520 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019521 return FAIL;
19522
19523 if (fnump != NULL)
19524 {
19525 n = list_find_nr(l, i++, NULL); /* fnum */
19526 if (n < 0)
19527 return FAIL;
19528 if (n == 0)
19529 n = curbuf->b_fnum; /* current buffer */
19530 *fnump = n;
19531 }
19532
19533 n = list_find_nr(l, i++, NULL); /* lnum */
19534 if (n < 0)
19535 return FAIL;
19536 posp->lnum = n;
19537
19538 n = list_find_nr(l, i++, NULL); /* col */
19539 if (n < 0)
19540 return FAIL;
19541 posp->col = n;
19542
19543#ifdef FEAT_VIRTUALEDIT
19544 n = list_find_nr(l, i, NULL);
19545 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019546 posp->coladd = 0;
19547 else
19548 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019549#endif
19550
19551 return OK;
19552}
19553
19554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 * Get the length of an environment variable name.
19556 * Advance "arg" to the first character after the name.
19557 * Return 0 for error.
19558 */
19559 static int
19560get_env_len(arg)
19561 char_u **arg;
19562{
19563 char_u *p;
19564 int len;
19565
19566 for (p = *arg; vim_isIDc(*p); ++p)
19567 ;
19568 if (p == *arg) /* no name found */
19569 return 0;
19570
19571 len = (int)(p - *arg);
19572 *arg = p;
19573 return len;
19574}
19575
19576/*
19577 * Get the length of the name of a function or internal variable.
19578 * "arg" is advanced to the first non-white character after the name.
19579 * Return 0 if something is wrong.
19580 */
19581 static int
19582get_id_len(arg)
19583 char_u **arg;
19584{
19585 char_u *p;
19586 int len;
19587
19588 /* Find the end of the name. */
19589 for (p = *arg; eval_isnamec(*p); ++p)
19590 ;
19591 if (p == *arg) /* no name found */
19592 return 0;
19593
19594 len = (int)(p - *arg);
19595 *arg = skipwhite(p);
19596
19597 return len;
19598}
19599
19600/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019601 * Get the length of the name of a variable or function.
19602 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019604 * Return -1 if curly braces expansion failed.
19605 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 * If the name contains 'magic' {}'s, expand them and return the
19607 * expanded name in an allocated string via 'alias' - caller must free.
19608 */
19609 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019610get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 char_u **arg;
19612 char_u **alias;
19613 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019614 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615{
19616 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 char_u *p;
19618 char_u *expr_start;
19619 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620
19621 *alias = NULL; /* default to no alias */
19622
19623 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19624 && (*arg)[2] == (int)KE_SNR)
19625 {
19626 /* hard coded <SNR>, already translated */
19627 *arg += 3;
19628 return get_id_len(arg) + 3;
19629 }
19630 len = eval_fname_script(*arg);
19631 if (len > 0)
19632 {
19633 /* literal "<SID>", "s:" or "<SNR>" */
19634 *arg += len;
19635 }
19636
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019638 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019640 p = find_name_end(*arg, &expr_start, &expr_end,
19641 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642 if (expr_start != NULL)
19643 {
19644 char_u *temp_string;
19645
19646 if (!evaluate)
19647 {
19648 len += (int)(p - *arg);
19649 *arg = skipwhite(p);
19650 return len;
19651 }
19652
19653 /*
19654 * Include any <SID> etc in the expanded string:
19655 * Thus the -len here.
19656 */
19657 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19658 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019659 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660 *alias = temp_string;
19661 *arg = skipwhite(p);
19662 return (int)STRLEN(temp_string);
19663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664
19665 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019666 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667 EMSG2(_(e_invexpr2), *arg);
19668
19669 return len;
19670}
19671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019672/*
19673 * Find the end of a variable or function name, taking care of magic braces.
19674 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19675 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019676 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019677 * Return a pointer to just after the name. Equal to "arg" if there is no
19678 * valid name.
19679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019681find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682 char_u *arg;
19683 char_u **expr_start;
19684 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019685 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019687 int mb_nest = 0;
19688 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689 char_u *p;
19690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019691 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019693 *expr_start = NULL;
19694 *expr_end = NULL;
19695 }
19696
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019697 /* Quick check for valid starting character. */
19698 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19699 return arg;
19700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019701 for (p = arg; *p != NUL
19702 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019703 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019704 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019705 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019706 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019707 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019708 if (*p == '\'')
19709 {
19710 /* skip over 'string' to avoid counting [ and ] inside it. */
19711 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19712 ;
19713 if (*p == NUL)
19714 break;
19715 }
19716 else if (*p == '"')
19717 {
19718 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19719 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19720 if (*p == '\\' && p[1] != NUL)
19721 ++p;
19722 if (*p == NUL)
19723 break;
19724 }
19725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019726 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019728 if (*p == '[')
19729 ++br_nest;
19730 else if (*p == ']')
19731 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019734 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019736 if (*p == '{')
19737 {
19738 mb_nest++;
19739 if (expr_start != NULL && *expr_start == NULL)
19740 *expr_start = p;
19741 }
19742 else if (*p == '}')
19743 {
19744 mb_nest--;
19745 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19746 *expr_end = p;
19747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 }
19750
19751 return p;
19752}
19753
19754/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019755 * Expands out the 'magic' {}'s in a variable/function name.
19756 * Note that this can call itself recursively, to deal with
19757 * constructs like foo{bar}{baz}{bam}
19758 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19759 * "in_start" ^
19760 * "expr_start" ^
19761 * "expr_end" ^
19762 * "in_end" ^
19763 *
19764 * Returns a new allocated string, which the caller must free.
19765 * Returns NULL for failure.
19766 */
19767 static char_u *
19768make_expanded_name(in_start, expr_start, expr_end, in_end)
19769 char_u *in_start;
19770 char_u *expr_start;
19771 char_u *expr_end;
19772 char_u *in_end;
19773{
19774 char_u c1;
19775 char_u *retval = NULL;
19776 char_u *temp_result;
19777 char_u *nextcmd = NULL;
19778
19779 if (expr_end == NULL || in_end == NULL)
19780 return NULL;
19781 *expr_start = NUL;
19782 *expr_end = NUL;
19783 c1 = *in_end;
19784 *in_end = NUL;
19785
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019786 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019787 if (temp_result != NULL && nextcmd == NULL)
19788 {
19789 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19790 + (in_end - expr_end) + 1));
19791 if (retval != NULL)
19792 {
19793 STRCPY(retval, in_start);
19794 STRCAT(retval, temp_result);
19795 STRCAT(retval, expr_end + 1);
19796 }
19797 }
19798 vim_free(temp_result);
19799
19800 *in_end = c1; /* put char back for error messages */
19801 *expr_start = '{';
19802 *expr_end = '}';
19803
19804 if (retval != NULL)
19805 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019806 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019807 if (expr_start != NULL)
19808 {
19809 /* Further expansion! */
19810 temp_result = make_expanded_name(retval, expr_start,
19811 expr_end, temp_result);
19812 vim_free(retval);
19813 retval = temp_result;
19814 }
19815 }
19816
19817 return retval;
19818}
19819
19820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019822 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 */
19824 static int
19825eval_isnamec(c)
19826 int c;
19827{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019828 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19829}
19830
19831/*
19832 * Return TRUE if character "c" can be used as the first character in a
19833 * variable or function name (excluding '{' and '}').
19834 */
19835 static int
19836eval_isnamec1(c)
19837 int c;
19838{
19839 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840}
19841
19842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 * Set number v: variable to "val".
19844 */
19845 void
19846set_vim_var_nr(idx, val)
19847 int idx;
19848 long val;
19849{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019850 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851}
19852
19853/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019854 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855 */
19856 long
19857get_vim_var_nr(idx)
19858 int idx;
19859{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019860 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861}
19862
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019863/*
19864 * Get string v: variable value. Uses a static buffer, can only be used once.
19865 */
19866 char_u *
19867get_vim_var_str(idx)
19868 int idx;
19869{
19870 return get_tv_string(&vimvars[idx].vv_tv);
19871}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019872
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019874 * Get List v: variable value. Caller must take care of reference count when
19875 * needed.
19876 */
19877 list_T *
19878get_vim_var_list(idx)
19879 int idx;
19880{
19881 return vimvars[idx].vv_list;
19882}
19883
19884/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019885 * Set v:char to character "c".
19886 */
19887 void
19888set_vim_var_char(c)
19889 int c;
19890{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019891 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019892
19893#ifdef FEAT_MBYTE
19894 if (has_mbyte)
19895 buf[(*mb_char2bytes)(c, buf)] = NUL;
19896 else
19897#endif
19898 {
19899 buf[0] = c;
19900 buf[1] = NUL;
19901 }
19902 set_vim_var_string(VV_CHAR, buf, -1);
19903}
19904
19905/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019906 * Set v:count to "count" and v:count1 to "count1".
19907 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 */
19909 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019910set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911 long count;
19912 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019913 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019915 if (set_prevcount)
19916 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019917 vimvars[VV_COUNT].vv_nr = count;
19918 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919}
19920
19921/*
19922 * Set string v: variable to a copy of "val".
19923 */
19924 void
19925set_vim_var_string(idx, val, len)
19926 int idx;
19927 char_u *val;
19928 int len; /* length of "val" to use or -1 (whole string) */
19929{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019930 /* Need to do this (at least) once, since we can't initialize a union.
19931 * Will always be invoked when "v:progname" is set. */
19932 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19933
Bram Moolenaare9a41262005-01-15 22:18:47 +000019934 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019936 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019938 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019940 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941}
19942
19943/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019944 * Set List v: variable to "val".
19945 */
19946 void
19947set_vim_var_list(idx, val)
19948 int idx;
19949 list_T *val;
19950{
19951 list_unref(vimvars[idx].vv_list);
19952 vimvars[idx].vv_list = val;
19953 if (val != NULL)
19954 ++val->lv_refcount;
19955}
19956
19957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958 * Set v:register if needed.
19959 */
19960 void
19961set_reg_var(c)
19962 int c;
19963{
19964 char_u regname;
19965
19966 if (c == 0 || c == ' ')
19967 regname = '"';
19968 else
19969 regname = c;
19970 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019971 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 set_vim_var_string(VV_REG, &regname, 1);
19973}
19974
19975/*
19976 * Get or set v:exception. If "oldval" == NULL, return the current value.
19977 * Otherwise, restore the value to "oldval" and return NULL.
19978 * Must always be called in pairs to save and restore v:exception! Does not
19979 * take care of memory allocations.
19980 */
19981 char_u *
19982v_exception(oldval)
19983 char_u *oldval;
19984{
19985 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019986 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987
Bram Moolenaare9a41262005-01-15 22:18:47 +000019988 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 return NULL;
19990}
19991
19992/*
19993 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19994 * Otherwise, restore the value to "oldval" and return NULL.
19995 * Must always be called in pairs to save and restore v:throwpoint! Does not
19996 * take care of memory allocations.
19997 */
19998 char_u *
19999v_throwpoint(oldval)
20000 char_u *oldval;
20001{
20002 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020003 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004
Bram Moolenaare9a41262005-01-15 22:18:47 +000020005 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 return NULL;
20007}
20008
20009#if defined(FEAT_AUTOCMD) || defined(PROTO)
20010/*
20011 * Set v:cmdarg.
20012 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20013 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20014 * Must always be called in pairs!
20015 */
20016 char_u *
20017set_cmdarg(eap, oldarg)
20018 exarg_T *eap;
20019 char_u *oldarg;
20020{
20021 char_u *oldval;
20022 char_u *newval;
20023 unsigned len;
20024
Bram Moolenaare9a41262005-01-15 22:18:47 +000020025 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020026 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020028 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020029 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020030 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 }
20032
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020033 if (eap->force_bin == FORCE_BIN)
20034 len = 6;
20035 else if (eap->force_bin == FORCE_NOBIN)
20036 len = 8;
20037 else
20038 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020039
20040 if (eap->read_edit)
20041 len += 7;
20042
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020043 if (eap->force_ff != 0)
20044 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20045# ifdef FEAT_MBYTE
20046 if (eap->force_enc != 0)
20047 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020048 if (eap->bad_char != 0)
20049 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020050# endif
20051
20052 newval = alloc(len + 1);
20053 if (newval == NULL)
20054 return NULL;
20055
20056 if (eap->force_bin == FORCE_BIN)
20057 sprintf((char *)newval, " ++bin");
20058 else if (eap->force_bin == FORCE_NOBIN)
20059 sprintf((char *)newval, " ++nobin");
20060 else
20061 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020062
20063 if (eap->read_edit)
20064 STRCAT(newval, " ++edit");
20065
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020066 if (eap->force_ff != 0)
20067 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20068 eap->cmd + eap->force_ff);
20069# ifdef FEAT_MBYTE
20070 if (eap->force_enc != 0)
20071 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20072 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020073 if (eap->bad_char == BAD_KEEP)
20074 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20075 else if (eap->bad_char == BAD_DROP)
20076 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20077 else if (eap->bad_char != 0)
20078 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020079# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020080 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020081 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082}
20083#endif
20084
20085/*
20086 * Get the value of internal variable "name".
20087 * Return OK or FAIL.
20088 */
20089 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020090get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 char_u *name;
20092 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020093 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020094 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020095 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096{
20097 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020098 typval_T *tv = NULL;
20099 typval_T atv;
20100 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102
20103 /* truncate the name, so that we can use strcmp() */
20104 cc = name[len];
20105 name[len] = NUL;
20106
20107 /*
20108 * Check for "b:changedtick".
20109 */
20110 if (STRCMP(name, "b:changedtick") == 0)
20111 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020112 atv.v_type = VAR_NUMBER;
20113 atv.vval.v_number = curbuf->b_changedtick;
20114 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 }
20116
20117 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 * Check for user-defined variables.
20119 */
20120 else
20121 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020122 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020124 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 }
20126
Bram Moolenaare9a41262005-01-15 22:18:47 +000020127 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020129 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020130 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131 ret = FAIL;
20132 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020133 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020134 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135
20136 name[len] = cc;
20137
20138 return ret;
20139}
20140
20141/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020142 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20143 * Also handle function call with Funcref variable: func(expr)
20144 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20145 */
20146 static int
20147handle_subscript(arg, rettv, evaluate, verbose)
20148 char_u **arg;
20149 typval_T *rettv;
20150 int evaluate; /* do more than finding the end */
20151 int verbose; /* give error messages */
20152{
20153 int ret = OK;
20154 dict_T *selfdict = NULL;
20155 char_u *s;
20156 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020157 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020158
20159 while (ret == OK
20160 && (**arg == '['
20161 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020162 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020163 && !vim_iswhite(*(*arg - 1)))
20164 {
20165 if (**arg == '(')
20166 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020167 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020168 if (evaluate)
20169 {
20170 functv = *rettv;
20171 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020172
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020173 /* Invoke the function. Recursive! */
20174 s = functv.vval.v_string;
20175 }
20176 else
20177 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020178 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020179 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20180 &len, evaluate, selfdict);
20181
20182 /* Clear the funcref afterwards, so that deleting it while
20183 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020184 if (evaluate)
20185 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020186
20187 /* Stop the expression evaluation when immediately aborting on
20188 * error, or when an interrupt occurred or an exception was thrown
20189 * but not caught. */
20190 if (aborting())
20191 {
20192 if (ret == OK)
20193 clear_tv(rettv);
20194 ret = FAIL;
20195 }
20196 dict_unref(selfdict);
20197 selfdict = NULL;
20198 }
20199 else /* **arg == '[' || **arg == '.' */
20200 {
20201 dict_unref(selfdict);
20202 if (rettv->v_type == VAR_DICT)
20203 {
20204 selfdict = rettv->vval.v_dict;
20205 if (selfdict != NULL)
20206 ++selfdict->dv_refcount;
20207 }
20208 else
20209 selfdict = NULL;
20210 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20211 {
20212 clear_tv(rettv);
20213 ret = FAIL;
20214 }
20215 }
20216 }
20217 dict_unref(selfdict);
20218 return ret;
20219}
20220
20221/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020222 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020223 * value).
20224 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020225 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020226alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020227{
Bram Moolenaar33570922005-01-25 22:26:29 +000020228 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020229}
20230
20231/*
20232 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233 * The string "s" must have been allocated, it is consumed.
20234 * Return NULL for out of memory, the variable otherwise.
20235 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020236 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020237alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 char_u *s;
20239{
Bram Moolenaar33570922005-01-25 22:26:29 +000020240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020242 rettv = alloc_tv();
20243 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020245 rettv->v_type = VAR_STRING;
20246 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247 }
20248 else
20249 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020250 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251}
20252
20253/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020254 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020256 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020257free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020258 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259{
20260 if (varp != NULL)
20261 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020262 switch (varp->v_type)
20263 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020264 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020265 func_unref(varp->vval.v_string);
20266 /*FALLTHROUGH*/
20267 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020268 vim_free(varp->vval.v_string);
20269 break;
20270 case VAR_LIST:
20271 list_unref(varp->vval.v_list);
20272 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020273 case VAR_DICT:
20274 dict_unref(varp->vval.v_dict);
20275 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020276 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020277#ifdef FEAT_FLOAT
20278 case VAR_FLOAT:
20279#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020280 case VAR_UNKNOWN:
20281 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020282 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020283 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020284 break;
20285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 vim_free(varp);
20287 }
20288}
20289
20290/*
20291 * Free the memory for a variable value and set the value to NULL or 0.
20292 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020293 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020294clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020295 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296{
20297 if (varp != NULL)
20298 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020299 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020301 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020302 func_unref(varp->vval.v_string);
20303 /*FALLTHROUGH*/
20304 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020305 vim_free(varp->vval.v_string);
20306 varp->vval.v_string = NULL;
20307 break;
20308 case VAR_LIST:
20309 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020310 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020311 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020312 case VAR_DICT:
20313 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020314 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020315 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020316 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020317 varp->vval.v_number = 0;
20318 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020319#ifdef FEAT_FLOAT
20320 case VAR_FLOAT:
20321 varp->vval.v_float = 0.0;
20322 break;
20323#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020324 case VAR_UNKNOWN:
20325 break;
20326 default:
20327 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020329 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 }
20331}
20332
20333/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020334 * Set the value of a variable to NULL without freeing items.
20335 */
20336 static void
20337init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020338 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020339{
20340 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020341 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020342}
20343
20344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 * Get the number value of a variable.
20346 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020347 * For incompatible types, return 0.
20348 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20349 * caller of incompatible types: it sets *denote to TRUE if "denote"
20350 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 */
20352 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020353get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020354 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020356 int error = FALSE;
20357
20358 return get_tv_number_chk(varp, &error); /* return 0L on error */
20359}
20360
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020361 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020362get_tv_number_chk(varp, denote)
20363 typval_T *varp;
20364 int *denote;
20365{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020366 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020368 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020370 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020371 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020372#ifdef FEAT_FLOAT
20373 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020374 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020375 break;
20376#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020377 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020378 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020379 break;
20380 case VAR_STRING:
20381 if (varp->vval.v_string != NULL)
20382 vim_str2nr(varp->vval.v_string, NULL, NULL,
20383 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020384 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020385 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020386 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020387 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020388 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020389 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020390 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020391 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020392 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020393 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020395 if (denote == NULL) /* useful for values that must be unsigned */
20396 n = -1;
20397 else
20398 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020399 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400}
20401
20402/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020403 * Get the lnum from the first argument.
20404 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020405 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406 */
20407 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020408get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020409 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410{
Bram Moolenaar33570922005-01-25 22:26:29 +000020411 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 linenr_T lnum;
20413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020414 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415 if (lnum == 0) /* no valid number, try using line() */
20416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020417 rettv.v_type = VAR_NUMBER;
20418 f_line(argvars, &rettv);
20419 lnum = rettv.vval.v_number;
20420 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421 }
20422 return lnum;
20423}
20424
20425/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020426 * Get the lnum from the first argument.
20427 * Also accepts "$", then "buf" is used.
20428 * Returns 0 on error.
20429 */
20430 static linenr_T
20431get_tv_lnum_buf(argvars, buf)
20432 typval_T *argvars;
20433 buf_T *buf;
20434{
20435 if (argvars[0].v_type == VAR_STRING
20436 && argvars[0].vval.v_string != NULL
20437 && argvars[0].vval.v_string[0] == '$'
20438 && buf != NULL)
20439 return buf->b_ml.ml_line_count;
20440 return get_tv_number_chk(&argvars[0], NULL);
20441}
20442
20443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 * Get the string value of a variable.
20445 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020446 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20447 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 * If the String variable has never been set, return an empty string.
20449 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020450 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20451 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 */
20453 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020454get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020455 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456{
20457 static char_u mybuf[NUMBUFLEN];
20458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020459 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460}
20461
20462 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020463get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020464 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020465 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020467 char_u *res = get_tv_string_buf_chk(varp, buf);
20468
20469 return res != NULL ? res : (char_u *)"";
20470}
20471
Bram Moolenaar7d647822014-04-05 21:28:56 +020020472/*
20473 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20474 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020475 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020476get_tv_string_chk(varp)
20477 typval_T *varp;
20478{
20479 static char_u mybuf[NUMBUFLEN];
20480
20481 return get_tv_string_buf_chk(varp, mybuf);
20482}
20483
20484 static char_u *
20485get_tv_string_buf_chk(varp, buf)
20486 typval_T *varp;
20487 char_u *buf;
20488{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020489 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020490 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020491 case VAR_NUMBER:
20492 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20493 return buf;
20494 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020495 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020496 break;
20497 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020498 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020499 break;
20500 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020501 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020502 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020503#ifdef FEAT_FLOAT
20504 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020505 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020506 break;
20507#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020508 case VAR_STRING:
20509 if (varp->vval.v_string != NULL)
20510 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020511 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020512 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020513 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020514 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020516 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517}
20518
20519/*
20520 * Find variable "name" in the list of variables.
20521 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020522 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020523 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020524 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020526 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020527find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020529 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020530 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020533 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534
Bram Moolenaara7043832005-01-21 11:56:39 +000020535 ht = find_var_ht(name, &varname);
20536 if (htp != NULL)
20537 *htp = ht;
20538 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020540 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541}
20542
20543/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020544 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020545 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020547 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020548find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020549 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020550 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020551 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020552 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020553{
Bram Moolenaar33570922005-01-25 22:26:29 +000020554 hashitem_T *hi;
20555
20556 if (*varname == NUL)
20557 {
20558 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020559 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020560 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020561 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020562 case 'g': return &globvars_var;
20563 case 'v': return &vimvars_var;
20564 case 'b': return &curbuf->b_bufvar;
20565 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020566#ifdef FEAT_WINDOWS
20567 case 't': return &curtab->tp_winvar;
20568#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020569 case 'l': return current_funccal == NULL
20570 ? NULL : &current_funccal->l_vars_var;
20571 case 'a': return current_funccal == NULL
20572 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020573 }
20574 return NULL;
20575 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020576
20577 hi = hash_find(ht, varname);
20578 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020579 {
20580 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020581 * worked find the variable again. Don't auto-load a script if it was
20582 * loaded already, otherwise it would be loaded every time when
20583 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020584 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020585 {
20586 /* Note: script_autoload() may make "hi" invalid. It must either
20587 * be obtained again or not used. */
20588 if (!script_autoload(varname, FALSE) || aborting())
20589 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020590 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020591 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020592 if (HASHITEM_EMPTY(hi))
20593 return NULL;
20594 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020595 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020596}
20597
20598/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020599 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020600 * Set "varname" to the start of name without ':'.
20601 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020602 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020603find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604 char_u *name;
20605 char_u **varname;
20606{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020607 hashitem_T *hi;
20608
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609 if (name[1] != ':')
20610 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020611 /* The name must not start with a colon or #. */
20612 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613 return NULL;
20614 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020615
20616 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020617 hi = hash_find(&compat_hashtab, name);
20618 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020619 return &compat_hashtab;
20620
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020622 return &globvarht; /* global variable */
20623 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 }
20625 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020626 if (*name == 'g') /* global variable */
20627 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020628 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20629 */
20630 if (vim_strchr(name + 2, ':') != NULL
20631 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020632 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020634 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020636 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020637#ifdef FEAT_WINDOWS
20638 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020639 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020641 if (*name == 'v') /* v: variable */
20642 return &vimvarht;
20643 if (*name == 'a' && current_funccal != NULL) /* function argument */
20644 return &current_funccal->l_avars.dv_hashtab;
20645 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20646 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647 if (*name == 's' /* script variable */
20648 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20649 return &SCRIPT_VARS(current_SID);
20650 return NULL;
20651}
20652
20653/*
20654 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020655 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656 * Returns NULL when it doesn't exist.
20657 */
20658 char_u *
20659get_var_value(name)
20660 char_u *name;
20661{
Bram Moolenaar33570922005-01-25 22:26:29 +000020662 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020664 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020665 if (v == NULL)
20666 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020667 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020668}
20669
20670/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020671 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672 * sourcing this script and when executing functions defined in the script.
20673 */
20674 void
20675new_script_vars(id)
20676 scid_T id;
20677{
Bram Moolenaara7043832005-01-21 11:56:39 +000020678 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020679 hashtab_T *ht;
20680 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020681
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20683 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020684 /* Re-allocating ga_data means that an ht_array pointing to
20685 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020686 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020687 for (i = 1; i <= ga_scripts.ga_len; ++i)
20688 {
20689 ht = &SCRIPT_VARS(i);
20690 if (ht->ht_mask == HT_INIT_SIZE - 1)
20691 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020692 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020693 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020694 }
20695
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696 while (ga_scripts.ga_len < id)
20697 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020698 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020699 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020700 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 }
20703 }
20704}
20705
20706/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020707 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20708 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020709 */
20710 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020711init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020712 dict_T *dict;
20713 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020714 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715{
Bram Moolenaar33570922005-01-25 22:26:29 +000020716 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020717 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020718 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020719 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020720 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020721 dict_var->di_tv.vval.v_dict = dict;
20722 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020723 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020724 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20725 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726}
20727
20728/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020729 * Unreference a dictionary initialized by init_var_dict().
20730 */
20731 void
20732unref_var_dict(dict)
20733 dict_T *dict;
20734{
20735 /* Now the dict needs to be freed if no one else is using it, go back to
20736 * normal reference counting. */
20737 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20738 dict_unref(dict);
20739}
20740
20741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020742 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020743 * Frees all allocated variables and the value they contain.
20744 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 */
20746 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020747vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020748 hashtab_T *ht;
20749{
20750 vars_clear_ext(ht, TRUE);
20751}
20752
20753/*
20754 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20755 */
20756 static void
20757vars_clear_ext(ht, free_val)
20758 hashtab_T *ht;
20759 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760{
Bram Moolenaara7043832005-01-21 11:56:39 +000020761 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020762 hashitem_T *hi;
20763 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764
Bram Moolenaar33570922005-01-25 22:26:29 +000020765 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020766 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020767 for (hi = ht->ht_array; todo > 0; ++hi)
20768 {
20769 if (!HASHITEM_EMPTY(hi))
20770 {
20771 --todo;
20772
Bram Moolenaar33570922005-01-25 22:26:29 +000020773 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020774 * ht_array might change then. hash_clear() takes care of it
20775 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020776 v = HI2DI(hi);
20777 if (free_val)
20778 clear_tv(&v->di_tv);
20779 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20780 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020781 }
20782 }
20783 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020784 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785}
20786
Bram Moolenaara7043832005-01-21 11:56:39 +000020787/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020788 * Delete a variable from hashtab "ht" at item "hi".
20789 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020792delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020793 hashtab_T *ht;
20794 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795{
Bram Moolenaar33570922005-01-25 22:26:29 +000020796 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020797
20798 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020799 clear_tv(&di->di_tv);
20800 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801}
20802
20803/*
20804 * List the value of one internal variable.
20805 */
20806 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020807list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020808 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020810 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020812 char_u *tofree;
20813 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020814 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020815
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020816 current_copyID += COPYID_INC;
20817 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020818 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020819 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020820 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821}
20822
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020824list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825 char_u *prefix;
20826 char_u *name;
20827 int type;
20828 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020829 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830{
Bram Moolenaar31859182007-08-14 20:41:13 +000020831 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20832 msg_start();
20833 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834 if (name != NULL) /* "a:" vars don't have a name stored */
20835 msg_puts(name);
20836 msg_putchar(' ');
20837 msg_advance(22);
20838 if (type == VAR_NUMBER)
20839 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020840 else if (type == VAR_FUNC)
20841 msg_putchar('*');
20842 else if (type == VAR_LIST)
20843 {
20844 msg_putchar('[');
20845 if (*string == '[')
20846 ++string;
20847 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020848 else if (type == VAR_DICT)
20849 {
20850 msg_putchar('{');
20851 if (*string == '{')
20852 ++string;
20853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854 else
20855 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020856
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020858
20859 if (type == VAR_FUNC)
20860 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020861 if (*first)
20862 {
20863 msg_clr_eos();
20864 *first = FALSE;
20865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866}
20867
20868/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020869 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 * If the variable already exists, the value is updated.
20871 * Otherwise the variable is created.
20872 */
20873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020874set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020876 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020877 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878{
Bram Moolenaar33570922005-01-25 22:26:29 +000020879 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020880 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020881 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020883 ht = find_var_ht(name, &varname);
20884 if (ht == NULL || *varname == NUL)
20885 {
20886 EMSG2(_(e_illvar), name);
20887 return;
20888 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020889 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020890
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020891 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20892 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020893
Bram Moolenaar33570922005-01-25 22:26:29 +000020894 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020896 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020897 if (var_check_ro(v->di_flags, name)
20898 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020899 return;
20900 if (v->di_tv.v_type != tv->v_type
20901 && !((v->di_tv.v_type == VAR_STRING
20902 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020903 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020904 || tv->v_type == VAR_NUMBER))
20905#ifdef FEAT_FLOAT
20906 && !((v->di_tv.v_type == VAR_NUMBER
20907 || v->di_tv.v_type == VAR_FLOAT)
20908 && (tv->v_type == VAR_NUMBER
20909 || tv->v_type == VAR_FLOAT))
20910#endif
20911 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020912 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020913 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020914 return;
20915 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020916
20917 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020918 * Handle setting internal v: variables separately: we don't change
20919 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020920 */
20921 if (ht == &vimvarht)
20922 {
20923 if (v->di_tv.v_type == VAR_STRING)
20924 {
20925 vim_free(v->di_tv.vval.v_string);
20926 if (copy || tv->v_type != VAR_STRING)
20927 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20928 else
20929 {
20930 /* Take over the string to avoid an extra alloc/free. */
20931 v->di_tv.vval.v_string = tv->vval.v_string;
20932 tv->vval.v_string = NULL;
20933 }
20934 }
20935 else if (v->di_tv.v_type != VAR_NUMBER)
20936 EMSG2(_(e_intern2), "set_var()");
20937 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020938 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020939 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020940 if (STRCMP(varname, "searchforward") == 0)
20941 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020942#ifdef FEAT_SEARCH_EXTRA
20943 else if (STRCMP(varname, "hlsearch") == 0)
20944 {
20945 no_hlsearch = !v->di_tv.vval.v_number;
20946 redraw_all_later(SOME_VALID);
20947 }
20948#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020949 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020950 return;
20951 }
20952
20953 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 }
20955 else /* add a new variable */
20956 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020957 /* Can't add "v:" variable. */
20958 if (ht == &vimvarht)
20959 {
20960 EMSG2(_(e_illvar), name);
20961 return;
20962 }
20963
Bram Moolenaar92124a32005-06-17 22:03:40 +000020964 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020965 if (!valid_varname(varname))
20966 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020967
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020968 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20969 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020970 if (v == NULL)
20971 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020972 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020973 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020975 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976 return;
20977 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020978 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020980
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020981 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020982 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020983 else
20984 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020985 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020986 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020987 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989}
20990
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020991/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020992 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020993 * Also give an error message.
20994 */
20995 static int
20996var_check_ro(flags, name)
20997 int flags;
20998 char_u *name;
20999{
21000 if (flags & DI_FLAGS_RO)
21001 {
21002 EMSG2(_(e_readonlyvar), name);
21003 return TRUE;
21004 }
21005 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21006 {
21007 EMSG2(_(e_readonlysbx), name);
21008 return TRUE;
21009 }
21010 return FALSE;
21011}
21012
21013/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021014 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21015 * Also give an error message.
21016 */
21017 static int
21018var_check_fixed(flags, name)
21019 int flags;
21020 char_u *name;
21021{
21022 if (flags & DI_FLAGS_FIX)
21023 {
21024 EMSG2(_("E795: Cannot delete variable %s"), name);
21025 return TRUE;
21026 }
21027 return FALSE;
21028}
21029
21030/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021031 * Check if a funcref is assigned to a valid variable name.
21032 * Return TRUE and give an error if not.
21033 */
21034 static int
21035var_check_func_name(name, new_var)
21036 char_u *name; /* points to start of variable name */
21037 int new_var; /* TRUE when creating the variable */
21038{
21039 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
21040 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21041 ? name[2] : name[0]))
21042 {
21043 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21044 name);
21045 return TRUE;
21046 }
21047 /* Don't allow hiding a function. When "v" is not NULL we might be
21048 * assigning another function to the same var, the type is checked
21049 * below. */
21050 if (new_var && function_exists(name))
21051 {
21052 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21053 name);
21054 return TRUE;
21055 }
21056 return FALSE;
21057}
21058
21059/*
21060 * Check if a variable name is valid.
21061 * Return FALSE and give an error if not.
21062 */
21063 static int
21064valid_varname(varname)
21065 char_u *varname;
21066{
21067 char_u *p;
21068
21069 for (p = varname; *p != NUL; ++p)
21070 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21071 && *p != AUTOLOAD_CHAR)
21072 {
21073 EMSG2(_(e_illvar), varname);
21074 return FALSE;
21075 }
21076 return TRUE;
21077}
21078
21079/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021080 * Return TRUE if typeval "tv" is set to be locked (immutable).
21081 * Also give an error message, using "name".
21082 */
21083 static int
21084tv_check_lock(lock, name)
21085 int lock;
21086 char_u *name;
21087{
21088 if (lock & VAR_LOCKED)
21089 {
21090 EMSG2(_("E741: Value is locked: %s"),
21091 name == NULL ? (char_u *)_("Unknown") : name);
21092 return TRUE;
21093 }
21094 if (lock & VAR_FIXED)
21095 {
21096 EMSG2(_("E742: Cannot change value of %s"),
21097 name == NULL ? (char_u *)_("Unknown") : name);
21098 return TRUE;
21099 }
21100 return FALSE;
21101}
21102
21103/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021104 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021105 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021106 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021107 * It is OK for "from" and "to" to point to the same item. This is used to
21108 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021109 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021110 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021111copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021112 typval_T *from;
21113 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021115 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021116 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021117 switch (from->v_type)
21118 {
21119 case VAR_NUMBER:
21120 to->vval.v_number = from->vval.v_number;
21121 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021122#ifdef FEAT_FLOAT
21123 case VAR_FLOAT:
21124 to->vval.v_float = from->vval.v_float;
21125 break;
21126#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021127 case VAR_STRING:
21128 case VAR_FUNC:
21129 if (from->vval.v_string == NULL)
21130 to->vval.v_string = NULL;
21131 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021132 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021133 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021134 if (from->v_type == VAR_FUNC)
21135 func_ref(to->vval.v_string);
21136 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021137 break;
21138 case VAR_LIST:
21139 if (from->vval.v_list == NULL)
21140 to->vval.v_list = NULL;
21141 else
21142 {
21143 to->vval.v_list = from->vval.v_list;
21144 ++to->vval.v_list->lv_refcount;
21145 }
21146 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021147 case VAR_DICT:
21148 if (from->vval.v_dict == NULL)
21149 to->vval.v_dict = NULL;
21150 else
21151 {
21152 to->vval.v_dict = from->vval.v_dict;
21153 ++to->vval.v_dict->dv_refcount;
21154 }
21155 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021156 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021157 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021158 break;
21159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160}
21161
21162/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021163 * Make a copy of an item.
21164 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021165 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21166 * reference to an already copied list/dict can be used.
21167 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021168 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021169 static int
21170item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021171 typval_T *from;
21172 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021173 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021174 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021175{
21176 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021177 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021178
Bram Moolenaar33570922005-01-25 22:26:29 +000021179 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021180 {
21181 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021182 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021183 }
21184 ++recurse;
21185
21186 switch (from->v_type)
21187 {
21188 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021189#ifdef FEAT_FLOAT
21190 case VAR_FLOAT:
21191#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021192 case VAR_STRING:
21193 case VAR_FUNC:
21194 copy_tv(from, to);
21195 break;
21196 case VAR_LIST:
21197 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021198 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021199 if (from->vval.v_list == NULL)
21200 to->vval.v_list = NULL;
21201 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21202 {
21203 /* use the copy made earlier */
21204 to->vval.v_list = from->vval.v_list->lv_copylist;
21205 ++to->vval.v_list->lv_refcount;
21206 }
21207 else
21208 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21209 if (to->vval.v_list == NULL)
21210 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021211 break;
21212 case VAR_DICT:
21213 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021214 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021215 if (from->vval.v_dict == NULL)
21216 to->vval.v_dict = NULL;
21217 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21218 {
21219 /* use the copy made earlier */
21220 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21221 ++to->vval.v_dict->dv_refcount;
21222 }
21223 else
21224 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21225 if (to->vval.v_dict == NULL)
21226 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021227 break;
21228 default:
21229 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021230 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021231 }
21232 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021233 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021234}
21235
21236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 * ":echo expr1 ..." print each argument separated with a space, add a
21238 * newline at the end.
21239 * ":echon expr1 ..." print each argument plain.
21240 */
21241 void
21242ex_echo(eap)
21243 exarg_T *eap;
21244{
21245 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021246 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021247 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248 char_u *p;
21249 int needclr = TRUE;
21250 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021251 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252
21253 if (eap->skip)
21254 ++emsg_skip;
21255 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21256 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021257 /* If eval1() causes an error message the text from the command may
21258 * still need to be cleared. E.g., "echo 22,44". */
21259 need_clr_eos = needclr;
21260
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021262 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 {
21264 /*
21265 * Report the invalid expression unless the expression evaluation
21266 * has been cancelled due to an aborting error, an interrupt, or an
21267 * exception.
21268 */
21269 if (!aborting())
21270 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021271 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 break;
21273 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021274 need_clr_eos = FALSE;
21275
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 if (!eap->skip)
21277 {
21278 if (atstart)
21279 {
21280 atstart = FALSE;
21281 /* Call msg_start() after eval1(), evaluating the expression
21282 * may cause a message to appear. */
21283 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021284 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021285 /* Mark the saved text as finishing the line, so that what
21286 * follows is displayed on a new line when scrolling back
21287 * at the more prompt. */
21288 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 }
21292 else if (eap->cmdidx == CMD_echo)
21293 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021294 current_copyID += COPYID_INC;
21295 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021296 if (p != NULL)
21297 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021299 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021301 if (*p != TAB && needclr)
21302 {
21303 /* remove any text still there from the command */
21304 msg_clr_eos();
21305 needclr = FALSE;
21306 }
21307 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308 }
21309 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021310 {
21311#ifdef FEAT_MBYTE
21312 if (has_mbyte)
21313 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021314 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021315
21316 (void)msg_outtrans_len_attr(p, i, echo_attr);
21317 p += i - 1;
21318 }
21319 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021320#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021321 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021324 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021326 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 arg = skipwhite(arg);
21328 }
21329 eap->nextcmd = check_nextcmd(arg);
21330
21331 if (eap->skip)
21332 --emsg_skip;
21333 else
21334 {
21335 /* remove text that may still be there from the command */
21336 if (needclr)
21337 msg_clr_eos();
21338 if (eap->cmdidx == CMD_echo)
21339 msg_end();
21340 }
21341}
21342
21343/*
21344 * ":echohl {name}".
21345 */
21346 void
21347ex_echohl(eap)
21348 exarg_T *eap;
21349{
21350 int id;
21351
21352 id = syn_name2id(eap->arg);
21353 if (id == 0)
21354 echo_attr = 0;
21355 else
21356 echo_attr = syn_id2attr(id);
21357}
21358
21359/*
21360 * ":execute expr1 ..." execute the result of an expression.
21361 * ":echomsg expr1 ..." Print a message
21362 * ":echoerr expr1 ..." Print an error
21363 * Each gets spaces around each argument and a newline at the end for
21364 * echo commands
21365 */
21366 void
21367ex_execute(eap)
21368 exarg_T *eap;
21369{
21370 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021371 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372 int ret = OK;
21373 char_u *p;
21374 garray_T ga;
21375 int len;
21376 int save_did_emsg;
21377
21378 ga_init2(&ga, 1, 80);
21379
21380 if (eap->skip)
21381 ++emsg_skip;
21382 while (*arg != NUL && *arg != '|' && *arg != '\n')
21383 {
21384 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021385 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 {
21387 /*
21388 * Report the invalid expression unless the expression evaluation
21389 * has been cancelled due to an aborting error, an interrupt, or an
21390 * exception.
21391 */
21392 if (!aborting())
21393 EMSG2(_(e_invexpr2), p);
21394 ret = FAIL;
21395 break;
21396 }
21397
21398 if (!eap->skip)
21399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021400 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401 len = (int)STRLEN(p);
21402 if (ga_grow(&ga, len + 2) == FAIL)
21403 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021404 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 ret = FAIL;
21406 break;
21407 }
21408 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411 ga.ga_len += len;
21412 }
21413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021414 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 arg = skipwhite(arg);
21416 }
21417
21418 if (ret != FAIL && ga.ga_data != NULL)
21419 {
21420 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021423 out_flush();
21424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425 else if (eap->cmdidx == CMD_echoerr)
21426 {
21427 /* We don't want to abort following commands, restore did_emsg. */
21428 save_did_emsg = did_emsg;
21429 EMSG((char_u *)ga.ga_data);
21430 if (!force_abort)
21431 did_emsg = save_did_emsg;
21432 }
21433 else if (eap->cmdidx == CMD_execute)
21434 do_cmdline((char_u *)ga.ga_data,
21435 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21436 }
21437
21438 ga_clear(&ga);
21439
21440 if (eap->skip)
21441 --emsg_skip;
21442
21443 eap->nextcmd = check_nextcmd(arg);
21444}
21445
21446/*
21447 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21448 * "arg" points to the "&" or '+' when called, to "option" when returning.
21449 * Returns NULL when no option name found. Otherwise pointer to the char
21450 * after the option name.
21451 */
21452 static char_u *
21453find_option_end(arg, opt_flags)
21454 char_u **arg;
21455 int *opt_flags;
21456{
21457 char_u *p = *arg;
21458
21459 ++p;
21460 if (*p == 'g' && p[1] == ':')
21461 {
21462 *opt_flags = OPT_GLOBAL;
21463 p += 2;
21464 }
21465 else if (*p == 'l' && p[1] == ':')
21466 {
21467 *opt_flags = OPT_LOCAL;
21468 p += 2;
21469 }
21470 else
21471 *opt_flags = 0;
21472
21473 if (!ASCII_ISALPHA(*p))
21474 return NULL;
21475 *arg = p;
21476
21477 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21478 p += 4; /* termcap option */
21479 else
21480 while (ASCII_ISALPHA(*p))
21481 ++p;
21482 return p;
21483}
21484
21485/*
21486 * ":function"
21487 */
21488 void
21489ex_function(eap)
21490 exarg_T *eap;
21491{
21492 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021493 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494 int j;
21495 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021497 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 char_u *name = NULL;
21499 char_u *p;
21500 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021501 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 garray_T newargs;
21503 garray_T newlines;
21504 int varargs = FALSE;
21505 int mustend = FALSE;
21506 int flags = 0;
21507 ufunc_T *fp;
21508 int indent;
21509 int nesting;
21510 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021511 dictitem_T *v;
21512 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021513 static int func_nr = 0; /* number for nameless function */
21514 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021515 hashtab_T *ht;
21516 int todo;
21517 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021518 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519
21520 /*
21521 * ":function" without argument: list functions.
21522 */
21523 if (ends_excmd(*eap->arg))
21524 {
21525 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021526 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021527 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021528 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021529 {
21530 if (!HASHITEM_EMPTY(hi))
21531 {
21532 --todo;
21533 fp = HI2UF(hi);
21534 if (!isdigit(*fp->uf_name))
21535 list_func_head(fp, FALSE);
21536 }
21537 }
21538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539 eap->nextcmd = check_nextcmd(eap->arg);
21540 return;
21541 }
21542
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021543 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021544 * ":function /pat": list functions matching pattern.
21545 */
21546 if (*eap->arg == '/')
21547 {
21548 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21549 if (!eap->skip)
21550 {
21551 regmatch_T regmatch;
21552
21553 c = *p;
21554 *p = NUL;
21555 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21556 *p = c;
21557 if (regmatch.regprog != NULL)
21558 {
21559 regmatch.rm_ic = p_ic;
21560
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021561 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021562 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21563 {
21564 if (!HASHITEM_EMPTY(hi))
21565 {
21566 --todo;
21567 fp = HI2UF(hi);
21568 if (!isdigit(*fp->uf_name)
21569 && vim_regexec(&regmatch, fp->uf_name, 0))
21570 list_func_head(fp, FALSE);
21571 }
21572 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021573 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021574 }
21575 }
21576 if (*p == '/')
21577 ++p;
21578 eap->nextcmd = check_nextcmd(p);
21579 return;
21580 }
21581
21582 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021583 * Get the function name. There are these situations:
21584 * func normal function name
21585 * "name" == func, "fudi.fd_dict" == NULL
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020021586 * s:func script-local function name
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021587 * dict.func new dictionary entry
21588 * "name" == NULL, "fudi.fd_dict" set,
21589 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21590 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021591 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021592 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21593 * dict.func existing dict entry that's not a Funcref
21594 * "name" == NULL, "fudi.fd_dict" set,
21595 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021598 name = trans_function_name(&p, eap->skip, 0, &fudi);
21599 paren = (vim_strchr(p, '(') != NULL);
21600 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601 {
21602 /*
21603 * Return on an invalid expression in braces, unless the expression
21604 * evaluation has been cancelled due to an aborting error, an
21605 * interrupt, or an exception.
21606 */
21607 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021608 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021609 if (!eap->skip && fudi.fd_newkey != NULL)
21610 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021611 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 else
21615 eap->skip = TRUE;
21616 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021617
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 /* An error in a function call during evaluation of an expression in magic
21619 * braces should not cause the function not to be defined. */
21620 saved_did_emsg = did_emsg;
21621 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622
21623 /*
21624 * ":function func" with only function name: list function.
21625 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021626 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627 {
21628 if (!ends_excmd(*skipwhite(p)))
21629 {
21630 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021631 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 }
21633 eap->nextcmd = check_nextcmd(p);
21634 if (eap->nextcmd != NULL)
21635 *p = NUL;
21636 if (!eap->skip && !got_int)
21637 {
21638 fp = find_func(name);
21639 if (fp != NULL)
21640 {
21641 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021642 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021644 if (FUNCLINE(fp, j) == NULL)
21645 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 msg_putchar('\n');
21647 msg_outnum((long)(j + 1));
21648 if (j < 9)
21649 msg_putchar(' ');
21650 if (j < 99)
21651 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021652 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 out_flush(); /* show a line at a time */
21654 ui_breakcheck();
21655 }
21656 if (!got_int)
21657 {
21658 msg_putchar('\n');
21659 msg_puts((char_u *)" endfunction");
21660 }
21661 }
21662 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021663 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021665 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 }
21667
21668 /*
21669 * ":function name(arg1, arg2)" Define function.
21670 */
21671 p = skipwhite(p);
21672 if (*p != '(')
21673 {
21674 if (!eap->skip)
21675 {
21676 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021677 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678 }
21679 /* attempt to continue by skipping some text */
21680 if (vim_strchr(p, '(') != NULL)
21681 p = vim_strchr(p, '(');
21682 }
21683 p = skipwhite(p + 1);
21684
21685 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21686 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21687
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021688 if (!eap->skip)
21689 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021690 /* Check the name of the function. Unless it's a dictionary function
21691 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021692 if (name != NULL)
21693 arg = name;
21694 else
21695 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021696 if (arg != NULL && (fudi.fd_di == NULL
21697 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021698 {
21699 if (*arg == K_SPECIAL)
21700 j = 3;
21701 else
21702 j = 0;
21703 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21704 : eval_isnamec(arg[j])))
21705 ++j;
21706 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021707 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021708 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021709 /* Disallow using the g: dict. */
21710 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21711 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021712 }
21713
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714 /*
21715 * Isolate the arguments: "arg1, arg2, ...)"
21716 */
21717 while (*p != ')')
21718 {
21719 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21720 {
21721 varargs = TRUE;
21722 p += 3;
21723 mustend = TRUE;
21724 }
21725 else
21726 {
21727 arg = p;
21728 while (ASCII_ISALNUM(*p) || *p == '_')
21729 ++p;
21730 if (arg == p || isdigit(*arg)
21731 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21732 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21733 {
21734 if (!eap->skip)
21735 EMSG2(_("E125: Illegal argument: %s"), arg);
21736 break;
21737 }
21738 if (ga_grow(&newargs, 1) == FAIL)
21739 goto erret;
21740 c = *p;
21741 *p = NUL;
21742 arg = vim_strsave(arg);
21743 if (arg == NULL)
21744 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021745
21746 /* Check for duplicate argument name. */
21747 for (i = 0; i < newargs.ga_len; ++i)
21748 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21749 {
21750 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021751 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021752 goto erret;
21753 }
21754
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21756 *p = c;
21757 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758 if (*p == ',')
21759 ++p;
21760 else
21761 mustend = TRUE;
21762 }
21763 p = skipwhite(p);
21764 if (mustend && *p != ')')
21765 {
21766 if (!eap->skip)
21767 EMSG2(_(e_invarg2), eap->arg);
21768 break;
21769 }
21770 }
21771 ++p; /* skip the ')' */
21772
Bram Moolenaare9a41262005-01-15 22:18:47 +000021773 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774 for (;;)
21775 {
21776 p = skipwhite(p);
21777 if (STRNCMP(p, "range", 5) == 0)
21778 {
21779 flags |= FC_RANGE;
21780 p += 5;
21781 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021782 else if (STRNCMP(p, "dict", 4) == 0)
21783 {
21784 flags |= FC_DICT;
21785 p += 4;
21786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 else if (STRNCMP(p, "abort", 5) == 0)
21788 {
21789 flags |= FC_ABORT;
21790 p += 5;
21791 }
21792 else
21793 break;
21794 }
21795
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021796 /* When there is a line break use what follows for the function body.
21797 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21798 if (*p == '\n')
21799 line_arg = p + 1;
21800 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021801 EMSG(_(e_trailing));
21802
21803 /*
21804 * Read the body of the function, until ":endfunction" is found.
21805 */
21806 if (KeyTyped)
21807 {
21808 /* Check if the function already exists, don't let the user type the
21809 * whole function before telling him it doesn't work! For a script we
21810 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021811 if (!eap->skip && !eap->forceit)
21812 {
21813 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21814 EMSG(_(e_funcdict));
21815 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021816 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021819 if (!eap->skip && did_emsg)
21820 goto erret;
21821
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822 msg_putchar('\n'); /* don't overwrite the function name */
21823 cmdline_row = msg_row;
21824 }
21825
21826 indent = 2;
21827 nesting = 0;
21828 for (;;)
21829 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021830 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021831 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021832 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021833 saved_wait_return = FALSE;
21834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021836 sourcing_lnum_off = sourcing_lnum;
21837
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021838 if (line_arg != NULL)
21839 {
21840 /* Use eap->arg, split up in parts by line breaks. */
21841 theline = line_arg;
21842 p = vim_strchr(theline, '\n');
21843 if (p == NULL)
21844 line_arg += STRLEN(line_arg);
21845 else
21846 {
21847 *p = NUL;
21848 line_arg = p + 1;
21849 }
21850 }
21851 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852 theline = getcmdline(':', 0L, indent);
21853 else
21854 theline = eap->getline(':', eap->cookie, indent);
21855 if (KeyTyped)
21856 lines_left = Rows - 1;
21857 if (theline == NULL)
21858 {
21859 EMSG(_("E126: Missing :endfunction"));
21860 goto erret;
21861 }
21862
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021863 /* Detect line continuation: sourcing_lnum increased more than one. */
21864 if (sourcing_lnum > sourcing_lnum_off + 1)
21865 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21866 else
21867 sourcing_lnum_off = 0;
21868
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 if (skip_until != NULL)
21870 {
21871 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21872 * don't check for ":endfunc". */
21873 if (STRCMP(theline, skip_until) == 0)
21874 {
21875 vim_free(skip_until);
21876 skip_until = NULL;
21877 }
21878 }
21879 else
21880 {
21881 /* skip ':' and blanks*/
21882 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21883 ;
21884
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021885 /* Check for "endfunction". */
21886 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021888 if (line_arg == NULL)
21889 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890 break;
21891 }
21892
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021893 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894 * at "end". */
21895 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21896 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021897 else if (STRNCMP(p, "if", 2) == 0
21898 || STRNCMP(p, "wh", 2) == 0
21899 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900 || STRNCMP(p, "try", 3) == 0)
21901 indent += 2;
21902
21903 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021904 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021906 if (*p == '!')
21907 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908 p += eval_fname_script(p);
21909 if (ASCII_ISALPHA(*p))
21910 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021911 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 if (*skipwhite(p) == '(')
21913 {
21914 ++nesting;
21915 indent += 2;
21916 }
21917 }
21918 }
21919
21920 /* Check for ":append" or ":insert". */
21921 p = skip_range(p, NULL);
21922 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21923 || (p[0] == 'i'
21924 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21925 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21926 skip_until = vim_strsave((char_u *)".");
21927
21928 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21929 arg = skipwhite(skiptowhite(p));
21930 if (arg[0] == '<' && arg[1] =='<'
21931 && ((p[0] == 'p' && p[1] == 'y'
21932 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21933 || (p[0] == 'p' && p[1] == 'e'
21934 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21935 || (p[0] == 't' && p[1] == 'c'
21936 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021937 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21938 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21940 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021941 || (p[0] == 'm' && p[1] == 'z'
21942 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943 ))
21944 {
21945 /* ":python <<" continues until a dot, like ":append" */
21946 p = skipwhite(arg + 2);
21947 if (*p == NUL)
21948 skip_until = vim_strsave((char_u *)".");
21949 else
21950 skip_until = vim_strsave(p);
21951 }
21952 }
21953
21954 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021955 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021956 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021957 if (line_arg == NULL)
21958 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021960 }
21961
21962 /* Copy the line to newly allocated memory. get_one_sourceline()
21963 * allocates 250 bytes per line, this saves 80% on average. The cost
21964 * is an extra alloc/free. */
21965 p = vim_strsave(theline);
21966 if (p != NULL)
21967 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021968 if (line_arg == NULL)
21969 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021970 theline = p;
21971 }
21972
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021973 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21974
21975 /* Add NULL lines for continuation lines, so that the line count is
21976 * equal to the index in the growarray. */
21977 while (sourcing_lnum_off-- > 0)
21978 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021979
21980 /* Check for end of eap->arg. */
21981 if (line_arg != NULL && *line_arg == NUL)
21982 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983 }
21984
21985 /* Don't define the function when skipping commands or when an error was
21986 * detected. */
21987 if (eap->skip || did_emsg)
21988 goto erret;
21989
21990 /*
21991 * If there are no errors, add the function
21992 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021993 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021994 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021995 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000021996 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021997 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021998 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021999 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022000 goto erret;
22001 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022002
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022003 fp = find_func(name);
22004 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022006 if (!eap->forceit)
22007 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022008 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022009 goto erret;
22010 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022011 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022012 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022013 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022014 name);
22015 goto erret;
22016 }
22017 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022018 ga_clear_strings(&(fp->uf_args));
22019 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022020 vim_free(name);
22021 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 }
22024 else
22025 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022026 char numbuf[20];
22027
22028 fp = NULL;
22029 if (fudi.fd_newkey == NULL && !eap->forceit)
22030 {
22031 EMSG(_(e_funcdict));
22032 goto erret;
22033 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022034 if (fudi.fd_di == NULL)
22035 {
22036 /* Can't add a function to a locked dictionary */
22037 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22038 goto erret;
22039 }
22040 /* Can't change an existing function if it is locked */
22041 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22042 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022043
22044 /* Give the function a sequential number. Can only be used with a
22045 * Funcref! */
22046 vim_free(name);
22047 sprintf(numbuf, "%d", ++func_nr);
22048 name = vim_strsave((char_u *)numbuf);
22049 if (name == NULL)
22050 goto erret;
22051 }
22052
22053 if (fp == NULL)
22054 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022055 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022056 {
22057 int slen, plen;
22058 char_u *scriptname;
22059
22060 /* Check that the autoload name matches the script name. */
22061 j = FAIL;
22062 if (sourcing_name != NULL)
22063 {
22064 scriptname = autoload_name(name);
22065 if (scriptname != NULL)
22066 {
22067 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022068 plen = (int)STRLEN(p);
22069 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022070 if (slen > plen && fnamecmp(p,
22071 sourcing_name + slen - plen) == 0)
22072 j = OK;
22073 vim_free(scriptname);
22074 }
22075 }
22076 if (j == FAIL)
22077 {
22078 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22079 goto erret;
22080 }
22081 }
22082
22083 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 if (fp == NULL)
22085 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022086
22087 if (fudi.fd_dict != NULL)
22088 {
22089 if (fudi.fd_di == NULL)
22090 {
22091 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022092 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022093 if (fudi.fd_di == NULL)
22094 {
22095 vim_free(fp);
22096 goto erret;
22097 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022098 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22099 {
22100 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022101 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022102 goto erret;
22103 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022104 }
22105 else
22106 /* overwrite existing dict entry */
22107 clear_tv(&fudi.fd_di->di_tv);
22108 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022109 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022110 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022111 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022112
22113 /* behave like "dict" was used */
22114 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022115 }
22116
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022118 STRCPY(fp->uf_name, name);
22119 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022121 fp->uf_args = newargs;
22122 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022123#ifdef FEAT_PROFILE
22124 fp->uf_tml_count = NULL;
22125 fp->uf_tml_total = NULL;
22126 fp->uf_tml_self = NULL;
22127 fp->uf_profiling = FALSE;
22128 if (prof_def_func())
22129 func_do_profile(fp);
22130#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022131 fp->uf_varargs = varargs;
22132 fp->uf_flags = flags;
22133 fp->uf_calls = 0;
22134 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022135 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136
22137erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138 ga_clear_strings(&newargs);
22139 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022140ret_free:
22141 vim_free(skip_until);
22142 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022145 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146}
22147
22148/*
22149 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022150 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022152 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022153 * TFN_INT: internal function name OK
22154 * TFN_QUIET: be quiet
22155 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 * Advances "pp" to just after the function name (if no error).
22157 */
22158 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022159trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160 char_u **pp;
22161 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022162 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022163 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022165 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 char_u *start;
22167 char_u *end;
22168 int lead;
22169 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022171 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022172
22173 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022174 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022176
22177 /* Check for hard coded <SNR>: already translated function ID (from a user
22178 * command). */
22179 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22180 && (*pp)[2] == (int)KE_SNR)
22181 {
22182 *pp += 3;
22183 len = get_id_len(pp) + 3;
22184 return vim_strnsave(start, len);
22185 }
22186
22187 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22188 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022190 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022192
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022193 /* Note that TFN_ flags use the same values as GLV_ flags. */
22194 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022195 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022196 if (end == start)
22197 {
22198 if (!skip)
22199 EMSG(_("E129: Function name required"));
22200 goto theend;
22201 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022202 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022203 {
22204 /*
22205 * Report an invalid expression in braces, unless the expression
22206 * evaluation has been cancelled due to an aborting error, an
22207 * interrupt, or an exception.
22208 */
22209 if (!aborting())
22210 {
22211 if (end != NULL)
22212 EMSG2(_(e_invarg2), start);
22213 }
22214 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022215 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022216 goto theend;
22217 }
22218
22219 if (lv.ll_tv != NULL)
22220 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022221 if (fdp != NULL)
22222 {
22223 fdp->fd_dict = lv.ll_dict;
22224 fdp->fd_newkey = lv.ll_newkey;
22225 lv.ll_newkey = NULL;
22226 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022227 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022228 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22229 {
22230 name = vim_strsave(lv.ll_tv->vval.v_string);
22231 *pp = end;
22232 }
22233 else
22234 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022235 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22236 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022237 EMSG(_(e_funcref));
22238 else
22239 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022240 name = NULL;
22241 }
22242 goto theend;
22243 }
22244
22245 if (lv.ll_name == NULL)
22246 {
22247 /* Error found, but continue after the function name. */
22248 *pp = end;
22249 goto theend;
22250 }
22251
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022252 /* Check if the name is a Funcref. If so, use the value. */
22253 if (lv.ll_exp_name != NULL)
22254 {
22255 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022256 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022257 if (name == lv.ll_exp_name)
22258 name = NULL;
22259 }
22260 else
22261 {
22262 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022263 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022264 if (name == *pp)
22265 name = NULL;
22266 }
22267 if (name != NULL)
22268 {
22269 name = vim_strsave(name);
22270 *pp = end;
22271 goto theend;
22272 }
22273
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022274 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022275 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022276 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022277 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22278 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22279 {
22280 /* When there was "s:" already or the name expanded to get a
22281 * leading "s:" then remove it. */
22282 lv.ll_name += 2;
22283 len -= 2;
22284 lead = 2;
22285 }
22286 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022287 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022288 {
22289 if (lead == 2) /* skip over "s:" */
22290 lv.ll_name += 2;
22291 len = (int)(end - lv.ll_name);
22292 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022293
22294 /*
22295 * Copy the function name to allocated memory.
22296 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22297 * Accept <SNR>123_name() outside a script.
22298 */
22299 if (skip)
22300 lead = 0; /* do nothing */
22301 else if (lead > 0)
22302 {
22303 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022304 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22305 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022306 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022307 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022308 if (current_SID <= 0)
22309 {
22310 EMSG(_(e_usingsid));
22311 goto theend;
22312 }
22313 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22314 lead += (int)STRLEN(sid_buf);
22315 }
22316 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022317 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022318 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022319 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
22320 lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022321 goto theend;
22322 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022323 if (!skip)
22324 {
22325 char_u *cp = vim_strchr(lv.ll_name, ':');
22326
22327 if (cp != NULL && cp < end)
22328 {
22329 EMSG2(_("E884: Function name cannot contain a colon: %s"),
22330 lv.ll_name);
22331 goto theend;
22332 }
22333 }
22334
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022335 name = alloc((unsigned)(len + lead + 1));
22336 if (name != NULL)
22337 {
22338 if (lead > 0)
22339 {
22340 name[0] = K_SPECIAL;
22341 name[1] = KS_EXTRA;
22342 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022343 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022344 STRCPY(name + 3, sid_buf);
22345 }
22346 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022347 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022348 }
22349 *pp = end;
22350
22351theend:
22352 clear_lval(&lv);
22353 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354}
22355
22356/*
22357 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22358 * Return 2 if "p" starts with "s:".
22359 * Return 0 otherwise.
22360 */
22361 static int
22362eval_fname_script(p)
22363 char_u *p;
22364{
22365 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22366 || STRNICMP(p + 1, "SNR>", 4) == 0))
22367 return 5;
22368 if (p[0] == 's' && p[1] == ':')
22369 return 2;
22370 return 0;
22371}
22372
22373/*
22374 * Return TRUE if "p" starts with "<SID>" or "s:".
22375 * Only works if eval_fname_script() returned non-zero for "p"!
22376 */
22377 static int
22378eval_fname_sid(p)
22379 char_u *p;
22380{
22381 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22382}
22383
22384/*
22385 * List the head of the function: "name(arg1, arg2)".
22386 */
22387 static void
22388list_func_head(fp, indent)
22389 ufunc_T *fp;
22390 int indent;
22391{
22392 int j;
22393
22394 msg_start();
22395 if (indent)
22396 MSG_PUTS(" ");
22397 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022398 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399 {
22400 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022401 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402 }
22403 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022404 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022406 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 {
22408 if (j)
22409 MSG_PUTS(", ");
22410 msg_puts(FUNCARG(fp, j));
22411 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022412 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 {
22414 if (j)
22415 MSG_PUTS(", ");
22416 MSG_PUTS("...");
22417 }
22418 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022419 if (fp->uf_flags & FC_ABORT)
22420 MSG_PUTS(" abort");
22421 if (fp->uf_flags & FC_RANGE)
22422 MSG_PUTS(" range");
22423 if (fp->uf_flags & FC_DICT)
22424 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022425 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022426 if (p_verbose > 0)
22427 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428}
22429
22430/*
22431 * Find a function by name, return pointer to it in ufuncs.
22432 * Return NULL for unknown function.
22433 */
22434 static ufunc_T *
22435find_func(name)
22436 char_u *name;
22437{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022438 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022440 hi = hash_find(&func_hashtab, name);
22441 if (!HASHITEM_EMPTY(hi))
22442 return HI2UF(hi);
22443 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444}
22445
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022446#if defined(EXITFREE) || defined(PROTO)
22447 void
22448free_all_functions()
22449{
22450 hashitem_T *hi;
22451
22452 /* Need to start all over every time, because func_free() may change the
22453 * hash table. */
22454 while (func_hashtab.ht_used > 0)
22455 for (hi = func_hashtab.ht_array; ; ++hi)
22456 if (!HASHITEM_EMPTY(hi))
22457 {
22458 func_free(HI2UF(hi));
22459 break;
22460 }
22461}
22462#endif
22463
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022464 int
22465translated_function_exists(name)
22466 char_u *name;
22467{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022468 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022469 return find_internal_func(name) >= 0;
22470 return find_func(name) != NULL;
22471}
22472
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022473/*
22474 * Return TRUE if a function "name" exists.
22475 */
22476 static int
22477function_exists(name)
22478 char_u *name;
22479{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022480 char_u *nm = name;
22481 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022482 int n = FALSE;
22483
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022484 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22485 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022486 nm = skipwhite(nm);
22487
22488 /* Only accept "funcname", "funcname ", "funcname (..." and
22489 * "funcname(...", not "funcname!...". */
22490 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022491 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022492 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022493 return n;
22494}
22495
Bram Moolenaara1544c02013-05-30 12:35:52 +020022496 char_u *
22497get_expanded_name(name, check)
22498 char_u *name;
22499 int check;
22500{
22501 char_u *nm = name;
22502 char_u *p;
22503
22504 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22505
22506 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022507 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022508 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022509
Bram Moolenaara1544c02013-05-30 12:35:52 +020022510 vim_free(p);
22511 return NULL;
22512}
22513
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022514/*
22515 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022516 * lower case letter and doesn't contain AUTOLOAD_CHAR.
22517 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022518 */
22519 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022520builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022521 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022522 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022523{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022524 char_u *p;
22525
22526 if (!ASCII_ISLOWER(name[0]))
22527 return FALSE;
22528 p = vim_strchr(name, AUTOLOAD_CHAR);
22529 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022530}
22531
Bram Moolenaar05159a02005-02-26 23:04:13 +000022532#if defined(FEAT_PROFILE) || defined(PROTO)
22533/*
22534 * Start profiling function "fp".
22535 */
22536 static void
22537func_do_profile(fp)
22538 ufunc_T *fp;
22539{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022540 int len = fp->uf_lines.ga_len;
22541
22542 if (len == 0)
22543 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022544 fp->uf_tm_count = 0;
22545 profile_zero(&fp->uf_tm_self);
22546 profile_zero(&fp->uf_tm_total);
22547 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022548 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022549 if (fp->uf_tml_total == NULL)
22550 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022551 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022552 if (fp->uf_tml_self == NULL)
22553 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022554 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022555 fp->uf_tml_idx = -1;
22556 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22557 || fp->uf_tml_self == NULL)
22558 return; /* out of memory */
22559
22560 fp->uf_profiling = TRUE;
22561}
22562
22563/*
22564 * Dump the profiling results for all functions in file "fd".
22565 */
22566 void
22567func_dump_profile(fd)
22568 FILE *fd;
22569{
22570 hashitem_T *hi;
22571 int todo;
22572 ufunc_T *fp;
22573 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022574 ufunc_T **sorttab;
22575 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022576
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022577 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022578 if (todo == 0)
22579 return; /* nothing to dump */
22580
Bram Moolenaar73830342005-02-28 22:48:19 +000022581 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22582
Bram Moolenaar05159a02005-02-26 23:04:13 +000022583 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22584 {
22585 if (!HASHITEM_EMPTY(hi))
22586 {
22587 --todo;
22588 fp = HI2UF(hi);
22589 if (fp->uf_profiling)
22590 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022591 if (sorttab != NULL)
22592 sorttab[st_len++] = fp;
22593
Bram Moolenaar05159a02005-02-26 23:04:13 +000022594 if (fp->uf_name[0] == K_SPECIAL)
22595 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22596 else
22597 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22598 if (fp->uf_tm_count == 1)
22599 fprintf(fd, "Called 1 time\n");
22600 else
22601 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22602 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22603 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22604 fprintf(fd, "\n");
22605 fprintf(fd, "count total (s) self (s)\n");
22606
22607 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22608 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022609 if (FUNCLINE(fp, i) == NULL)
22610 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022611 prof_func_line(fd, fp->uf_tml_count[i],
22612 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022613 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22614 }
22615 fprintf(fd, "\n");
22616 }
22617 }
22618 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022619
22620 if (sorttab != NULL && st_len > 0)
22621 {
22622 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22623 prof_total_cmp);
22624 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22625 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22626 prof_self_cmp);
22627 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22628 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022629
22630 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022631}
Bram Moolenaar73830342005-02-28 22:48:19 +000022632
22633 static void
22634prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22635 FILE *fd;
22636 ufunc_T **sorttab;
22637 int st_len;
22638 char *title;
22639 int prefer_self; /* when equal print only self time */
22640{
22641 int i;
22642 ufunc_T *fp;
22643
22644 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22645 fprintf(fd, "count total (s) self (s) function\n");
22646 for (i = 0; i < 20 && i < st_len; ++i)
22647 {
22648 fp = sorttab[i];
22649 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22650 prefer_self);
22651 if (fp->uf_name[0] == K_SPECIAL)
22652 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22653 else
22654 fprintf(fd, " %s()\n", fp->uf_name);
22655 }
22656 fprintf(fd, "\n");
22657}
22658
22659/*
22660 * Print the count and times for one function or function line.
22661 */
22662 static void
22663prof_func_line(fd, count, total, self, prefer_self)
22664 FILE *fd;
22665 int count;
22666 proftime_T *total;
22667 proftime_T *self;
22668 int prefer_self; /* when equal print only self time */
22669{
22670 if (count > 0)
22671 {
22672 fprintf(fd, "%5d ", count);
22673 if (prefer_self && profile_equal(total, self))
22674 fprintf(fd, " ");
22675 else
22676 fprintf(fd, "%s ", profile_msg(total));
22677 if (!prefer_self && profile_equal(total, self))
22678 fprintf(fd, " ");
22679 else
22680 fprintf(fd, "%s ", profile_msg(self));
22681 }
22682 else
22683 fprintf(fd, " ");
22684}
22685
22686/*
22687 * Compare function for total time sorting.
22688 */
22689 static int
22690#ifdef __BORLANDC__
22691_RTLENTRYF
22692#endif
22693prof_total_cmp(s1, s2)
22694 const void *s1;
22695 const void *s2;
22696{
22697 ufunc_T *p1, *p2;
22698
22699 p1 = *(ufunc_T **)s1;
22700 p2 = *(ufunc_T **)s2;
22701 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22702}
22703
22704/*
22705 * Compare function for self time sorting.
22706 */
22707 static int
22708#ifdef __BORLANDC__
22709_RTLENTRYF
22710#endif
22711prof_self_cmp(s1, s2)
22712 const void *s1;
22713 const void *s2;
22714{
22715 ufunc_T *p1, *p2;
22716
22717 p1 = *(ufunc_T **)s1;
22718 p2 = *(ufunc_T **)s2;
22719 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22720}
22721
Bram Moolenaar05159a02005-02-26 23:04:13 +000022722#endif
22723
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022724/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022725 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022726 * Return TRUE if a package was loaded.
22727 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022728 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022729script_autoload(name, reload)
22730 char_u *name;
22731 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022732{
22733 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022734 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022735 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022736 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022737
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022738 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022739 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022740 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022741 return FALSE;
22742
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022743 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022744
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022745 /* Find the name in the list of previously loaded package names. Skip
22746 * "autoload/", it's always the same. */
22747 for (i = 0; i < ga_loaded.ga_len; ++i)
22748 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22749 break;
22750 if (!reload && i < ga_loaded.ga_len)
22751 ret = FALSE; /* was loaded already */
22752 else
22753 {
22754 /* Remember the name if it wasn't loaded already. */
22755 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22756 {
22757 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22758 tofree = NULL;
22759 }
22760
22761 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022762 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022763 ret = TRUE;
22764 }
22765
22766 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022767 return ret;
22768}
22769
22770/*
22771 * Return the autoload script name for a function or variable name.
22772 * Returns NULL when out of memory.
22773 */
22774 static char_u *
22775autoload_name(name)
22776 char_u *name;
22777{
22778 char_u *p;
22779 char_u *scriptname;
22780
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022781 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022782 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22783 if (scriptname == NULL)
22784 return FALSE;
22785 STRCPY(scriptname, "autoload/");
22786 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022787 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022788 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022789 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022790 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022791 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022792}
22793
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22795
22796/*
22797 * Function given to ExpandGeneric() to obtain the list of user defined
22798 * function names.
22799 */
22800 char_u *
22801get_user_func_name(xp, idx)
22802 expand_T *xp;
22803 int idx;
22804{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022805 static long_u done;
22806 static hashitem_T *hi;
22807 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808
22809 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022811 done = 0;
22812 hi = func_hashtab.ht_array;
22813 }
22814 if (done < func_hashtab.ht_used)
22815 {
22816 if (done++ > 0)
22817 ++hi;
22818 while (HASHITEM_EMPTY(hi))
22819 ++hi;
22820 fp = HI2UF(hi);
22821
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022822 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022823 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022824
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022825 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22826 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022827
22828 cat_func_name(IObuff, fp);
22829 if (xp->xp_context != EXPAND_USER_FUNC)
22830 {
22831 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022832 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833 STRCAT(IObuff, ")");
22834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835 return IObuff;
22836 }
22837 return NULL;
22838}
22839
22840#endif /* FEAT_CMDL_COMPL */
22841
22842/*
22843 * Copy the function name of "fp" to buffer "buf".
22844 * "buf" must be able to hold the function name plus three bytes.
22845 * Takes care of script-local function names.
22846 */
22847 static void
22848cat_func_name(buf, fp)
22849 char_u *buf;
22850 ufunc_T *fp;
22851{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022852 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853 {
22854 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022855 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 }
22857 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022858 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859}
22860
22861/*
22862 * ":delfunction {name}"
22863 */
22864 void
22865ex_delfunction(eap)
22866 exarg_T *eap;
22867{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022868 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 char_u *p;
22870 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022871 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872
22873 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022874 name = trans_function_name(&p, eap->skip, 0, &fudi);
22875 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022877 {
22878 if (fudi.fd_dict != NULL && !eap->skip)
22879 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 if (!ends_excmd(*skipwhite(p)))
22883 {
22884 vim_free(name);
22885 EMSG(_(e_trailing));
22886 return;
22887 }
22888 eap->nextcmd = check_nextcmd(p);
22889 if (eap->nextcmd != NULL)
22890 *p = NUL;
22891
22892 if (!eap->skip)
22893 fp = find_func(name);
22894 vim_free(name);
22895
22896 if (!eap->skip)
22897 {
22898 if (fp == NULL)
22899 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022900 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 return;
22902 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022903 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 {
22905 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22906 return;
22907 }
22908
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022909 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022911 /* Delete the dict item that refers to the function, it will
22912 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022913 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022915 else
22916 func_free(fp);
22917 }
22918}
22919
22920/*
22921 * Free a function and remove it from the list of functions.
22922 */
22923 static void
22924func_free(fp)
22925 ufunc_T *fp;
22926{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022927 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022928
22929 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022930 ga_clear_strings(&(fp->uf_args));
22931 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022932#ifdef FEAT_PROFILE
22933 vim_free(fp->uf_tml_count);
22934 vim_free(fp->uf_tml_total);
22935 vim_free(fp->uf_tml_self);
22936#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022937
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022938 /* remove the function from the function hashtable */
22939 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22940 if (HASHITEM_EMPTY(hi))
22941 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022942 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022943 hash_remove(&func_hashtab, hi);
22944
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022945 vim_free(fp);
22946}
22947
22948/*
22949 * Unreference a Function: decrement the reference count and free it when it
22950 * becomes zero. Only for numbered functions.
22951 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022952 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022953func_unref(name)
22954 char_u *name;
22955{
22956 ufunc_T *fp;
22957
22958 if (name != NULL && isdigit(*name))
22959 {
22960 fp = find_func(name);
22961 if (fp == NULL)
22962 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022963 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022964 {
22965 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022966 * when "uf_calls" becomes zero. */
22967 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022968 func_free(fp);
22969 }
22970 }
22971}
22972
22973/*
22974 * Count a reference to a Function.
22975 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022976 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022977func_ref(name)
22978 char_u *name;
22979{
22980 ufunc_T *fp;
22981
22982 if (name != NULL && isdigit(*name))
22983 {
22984 fp = find_func(name);
22985 if (fp == NULL)
22986 EMSG2(_(e_intern2), "func_ref()");
22987 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022988 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989 }
22990}
22991
22992/*
22993 * Call a user function.
22994 */
22995 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022996call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997 ufunc_T *fp; /* pointer to function */
22998 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022999 typval_T *argvars; /* arguments */
23000 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 linenr_T firstline; /* first line of range */
23002 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023003 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004{
Bram Moolenaar33570922005-01-25 22:26:29 +000023005 char_u *save_sourcing_name;
23006 linenr_T save_sourcing_lnum;
23007 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023008 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023009 int save_did_emsg;
23010 static int depth = 0;
23011 dictitem_T *v;
23012 int fixvar_idx = 0; /* index in fixvar[] */
23013 int i;
23014 int ai;
23015 char_u numbuf[NUMBUFLEN];
23016 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023017#ifdef FEAT_PROFILE
23018 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023019 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021
23022 /* If depth of calling is getting too high, don't execute the function */
23023 if (depth >= p_mfd)
23024 {
23025 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023026 rettv->v_type = VAR_NUMBER;
23027 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028 return;
23029 }
23030 ++depth;
23031
23032 line_breakcheck(); /* check for CTRL-C hit */
23033
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023034 fc = (funccall_T *)alloc(sizeof(funccall_T));
23035 fc->caller = current_funccal;
23036 current_funccal = fc;
23037 fc->func = fp;
23038 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023039 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023040 fc->linenr = 0;
23041 fc->returned = FALSE;
23042 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023044 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23045 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046
Bram Moolenaar33570922005-01-25 22:26:29 +000023047 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023048 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023049 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23050 * each argument variable and saves a lot of time.
23051 */
23052 /*
23053 * Init l: variables.
23054 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023055 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023056 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023057 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023058 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23059 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023060 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023061 name = v->di_key;
23062 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023063 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023064 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023065 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023066 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023067 v->di_tv.vval.v_dict = selfdict;
23068 ++selfdict->dv_refcount;
23069 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023070
Bram Moolenaar33570922005-01-25 22:26:29 +000023071 /*
23072 * Init a: variables.
23073 * Set a:0 to "argcount".
23074 * Set a:000 to a list with room for the "..." arguments.
23075 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023076 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023077 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023078 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023079 /* Use "name" to avoid a warning from some compiler that checks the
23080 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023081 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023082 name = v->di_key;
23083 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023084 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023085 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023086 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023087 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023088 v->di_tv.vval.v_list = &fc->l_varlist;
23089 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23090 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23091 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023092
23093 /*
23094 * Set a:firstline to "firstline" and a:lastline to "lastline".
23095 * Set a:name to named arguments.
23096 * Set a:N to the "..." arguments.
23097 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023098 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023099 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023100 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023101 (varnumber_T)lastline);
23102 for (i = 0; i < argcount; ++i)
23103 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023104 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023105 if (ai < 0)
23106 /* named argument a:name */
23107 name = FUNCARG(fp, i);
23108 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023109 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023110 /* "..." argument a:1, a:2, etc. */
23111 sprintf((char *)numbuf, "%d", ai + 1);
23112 name = numbuf;
23113 }
23114 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23115 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023116 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023117 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23118 }
23119 else
23120 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023121 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23122 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023123 if (v == NULL)
23124 break;
23125 v->di_flags = DI_FLAGS_RO;
23126 }
23127 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023128 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023129
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023130 /* Note: the values are copied directly to avoid alloc/free.
23131 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023132 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023133 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023134
23135 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23136 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023137 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23138 fc->l_listitems[ai].li_tv = argvars[i];
23139 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023140 }
23141 }
23142
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143 /* Don't redraw while executing the function. */
23144 ++RedrawingDisabled;
23145 save_sourcing_name = sourcing_name;
23146 save_sourcing_lnum = sourcing_lnum;
23147 sourcing_lnum = 1;
23148 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023149 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150 if (sourcing_name != NULL)
23151 {
23152 if (save_sourcing_name != NULL
23153 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23154 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23155 else
23156 STRCPY(sourcing_name, "function ");
23157 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23158
23159 if (p_verbose >= 12)
23160 {
23161 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023162 verbose_enter_scroll();
23163
Bram Moolenaar555b2802005-05-19 21:08:39 +000023164 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 if (p_verbose >= 14)
23166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023167 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023168 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023169 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023170 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171
23172 msg_puts((char_u *)"(");
23173 for (i = 0; i < argcount; ++i)
23174 {
23175 if (i > 0)
23176 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023177 if (argvars[i].v_type == VAR_NUMBER)
23178 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179 else
23180 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023181 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
23182 if (s != NULL)
23183 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023184 if (vim_strsize(s) > MSG_BUF_CLEN)
23185 {
23186 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23187 s = buf;
23188 }
23189 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023190 vim_free(tofree);
23191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192 }
23193 }
23194 msg_puts((char_u *)")");
23195 }
23196 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023197
23198 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199 --no_wait_return;
23200 }
23201 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023202#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023203 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023204 {
23205 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23206 func_do_profile(fp);
23207 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023208 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023209 {
23210 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023211 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023212 profile_zero(&fp->uf_tm_children);
23213 }
23214 script_prof_save(&wait_start);
23215 }
23216#endif
23217
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023219 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220 save_did_emsg = did_emsg;
23221 did_emsg = FALSE;
23222
23223 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023224 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023225 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23226
23227 --RedrawingDisabled;
23228
23229 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023230 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023232 clear_tv(rettv);
23233 rettv->v_type = VAR_NUMBER;
23234 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023235 }
23236
Bram Moolenaar05159a02005-02-26 23:04:13 +000023237#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023238 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023239 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023240 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023241 profile_end(&call_start);
23242 profile_sub_wait(&wait_start, &call_start);
23243 profile_add(&fp->uf_tm_total, &call_start);
23244 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023245 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023246 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023247 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23248 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023249 }
23250 }
23251#endif
23252
Bram Moolenaar071d4272004-06-13 20:20:40 +000023253 /* when being verbose, mention the return value */
23254 if (p_verbose >= 12)
23255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023257 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258
Bram Moolenaar071d4272004-06-13 20:20:40 +000023259 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023260 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023261 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023262 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023263 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023264 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023265 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023266 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023267 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023268 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023269 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023270
Bram Moolenaar555b2802005-05-19 21:08:39 +000023271 /* The value may be very long. Skip the middle part, so that we
23272 * have some idea how it starts and ends. smsg() would always
23273 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023274 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023275 if (s != NULL)
23276 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023277 if (vim_strsize(s) > MSG_BUF_CLEN)
23278 {
23279 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23280 s = buf;
23281 }
23282 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023283 vim_free(tofree);
23284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023285 }
23286 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023287
23288 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289 --no_wait_return;
23290 }
23291
23292 vim_free(sourcing_name);
23293 sourcing_name = save_sourcing_name;
23294 sourcing_lnum = save_sourcing_lnum;
23295 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023296#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023297 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023298 script_prof_restore(&wait_start);
23299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300
23301 if (p_verbose >= 12 && sourcing_name != NULL)
23302 {
23303 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023304 verbose_enter_scroll();
23305
Bram Moolenaar555b2802005-05-19 21:08:39 +000023306 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023308
23309 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 --no_wait_return;
23311 }
23312
23313 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023314 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023316
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023317 /* If the a:000 list and the l: and a: dicts are not referenced we can
23318 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023319 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23320 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23321 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23322 {
23323 free_funccal(fc, FALSE);
23324 }
23325 else
23326 {
23327 hashitem_T *hi;
23328 listitem_T *li;
23329 int todo;
23330
23331 /* "fc" is still in use. This can happen when returning "a:000" or
23332 * assigning "l:" to a global variable.
23333 * Link "fc" in the list for garbage collection later. */
23334 fc->caller = previous_funccal;
23335 previous_funccal = fc;
23336
23337 /* Make a copy of the a: variables, since we didn't do that above. */
23338 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23339 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23340 {
23341 if (!HASHITEM_EMPTY(hi))
23342 {
23343 --todo;
23344 v = HI2DI(hi);
23345 copy_tv(&v->di_tv, &v->di_tv);
23346 }
23347 }
23348
23349 /* Make a copy of the a:000 items, since we didn't do that above. */
23350 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23351 copy_tv(&li->li_tv, &li->li_tv);
23352 }
23353}
23354
23355/*
23356 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023357 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023358 */
23359 static int
23360can_free_funccal(fc, copyID)
23361 funccall_T *fc;
23362 int copyID;
23363{
23364 return (fc->l_varlist.lv_copyID != copyID
23365 && fc->l_vars.dv_copyID != copyID
23366 && fc->l_avars.dv_copyID != copyID);
23367}
23368
23369/*
23370 * Free "fc" and what it contains.
23371 */
23372 static void
23373free_funccal(fc, free_val)
23374 funccall_T *fc;
23375 int free_val; /* a: vars were allocated */
23376{
23377 listitem_T *li;
23378
23379 /* The a: variables typevals may not have been allocated, only free the
23380 * allocated variables. */
23381 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23382
23383 /* free all l: variables */
23384 vars_clear(&fc->l_vars.dv_hashtab);
23385
23386 /* Free the a:000 variables if they were allocated. */
23387 if (free_val)
23388 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23389 clear_tv(&li->li_tv);
23390
23391 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392}
23393
23394/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023395 * Add a number variable "name" to dict "dp" with value "nr".
23396 */
23397 static void
23398add_nr_var(dp, v, name, nr)
23399 dict_T *dp;
23400 dictitem_T *v;
23401 char *name;
23402 varnumber_T nr;
23403{
23404 STRCPY(v->di_key, name);
23405 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23406 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23407 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023408 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023409 v->di_tv.vval.v_number = nr;
23410}
23411
23412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023413 * ":return [expr]"
23414 */
23415 void
23416ex_return(eap)
23417 exarg_T *eap;
23418{
23419 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023420 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421 int returning = FALSE;
23422
23423 if (current_funccal == NULL)
23424 {
23425 EMSG(_("E133: :return not inside a function"));
23426 return;
23427 }
23428
23429 if (eap->skip)
23430 ++emsg_skip;
23431
23432 eap->nextcmd = NULL;
23433 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023434 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023435 {
23436 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023437 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023439 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023440 }
23441 /* It's safer to return also on error. */
23442 else if (!eap->skip)
23443 {
23444 /*
23445 * Return unless the expression evaluation has been cancelled due to an
23446 * aborting error, an interrupt, or an exception.
23447 */
23448 if (!aborting())
23449 returning = do_return(eap, FALSE, TRUE, NULL);
23450 }
23451
23452 /* When skipping or the return gets pending, advance to the next command
23453 * in this line (!returning). Otherwise, ignore the rest of the line.
23454 * Following lines will be ignored by get_func_line(). */
23455 if (returning)
23456 eap->nextcmd = NULL;
23457 else if (eap->nextcmd == NULL) /* no argument */
23458 eap->nextcmd = check_nextcmd(arg);
23459
23460 if (eap->skip)
23461 --emsg_skip;
23462}
23463
23464/*
23465 * Return from a function. Possibly makes the return pending. Also called
23466 * for a pending return at the ":endtry" or after returning from an extra
23467 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023468 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023469 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023470 * FALSE when the return gets pending.
23471 */
23472 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023473do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 exarg_T *eap;
23475 int reanimate;
23476 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023477 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478{
23479 int idx;
23480 struct condstack *cstack = eap->cstack;
23481
23482 if (reanimate)
23483 /* Undo the return. */
23484 current_funccal->returned = FALSE;
23485
23486 /*
23487 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23488 * not in its finally clause (which then is to be executed next) is found.
23489 * In this case, make the ":return" pending for execution at the ":endtry".
23490 * Otherwise, return normally.
23491 */
23492 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23493 if (idx >= 0)
23494 {
23495 cstack->cs_pending[idx] = CSTP_RETURN;
23496
23497 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023498 /* A pending return again gets pending. "rettv" points to an
23499 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023501 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023502 else
23503 {
23504 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023505 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023507 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023509 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510 {
23511 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023512 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023513 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 else
23515 EMSG(_(e_outofmem));
23516 }
23517 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023518 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023519
23520 if (reanimate)
23521 {
23522 /* The pending return value could be overwritten by a ":return"
23523 * without argument in a finally clause; reset the default
23524 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023525 current_funccal->rettv->v_type = VAR_NUMBER;
23526 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 }
23528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023529 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 }
23531 else
23532 {
23533 current_funccal->returned = TRUE;
23534
23535 /* If the return is carried out now, store the return value. For
23536 * a return immediately after reanimation, the value is already
23537 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023538 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023540 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023541 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023543 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544 }
23545 }
23546
23547 return idx < 0;
23548}
23549
23550/*
23551 * Free the variable with a pending return value.
23552 */
23553 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023554discard_pending_return(rettv)
23555 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556{
Bram Moolenaar33570922005-01-25 22:26:29 +000023557 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558}
23559
23560/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023561 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562 * is an allocated string. Used by report_pending() for verbose messages.
23563 */
23564 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023565get_return_cmd(rettv)
23566 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023568 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023569 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023570 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023572 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023573 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023574 if (s == NULL)
23575 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023576
23577 STRCPY(IObuff, ":return ");
23578 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23579 if (STRLEN(s) + 8 >= IOSIZE)
23580 STRCPY(IObuff + IOSIZE - 4, "...");
23581 vim_free(tofree);
23582 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583}
23584
23585/*
23586 * Get next function line.
23587 * Called by do_cmdline() to get the next line.
23588 * Returns allocated string, or NULL for end of function.
23589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590 char_u *
23591get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023592 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023594 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595{
Bram Moolenaar33570922005-01-25 22:26:29 +000023596 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023597 ufunc_T *fp = fcp->func;
23598 char_u *retval;
23599 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600
23601 /* If breakpoints have been added/deleted need to check for it. */
23602 if (fcp->dbg_tick != debug_tick)
23603 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023604 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023605 sourcing_lnum);
23606 fcp->dbg_tick = debug_tick;
23607 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023608#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023609 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023610 func_line_end(cookie);
23611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612
Bram Moolenaar05159a02005-02-26 23:04:13 +000023613 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023614 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23615 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616 retval = NULL;
23617 else
23618 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023619 /* Skip NULL lines (continuation lines). */
23620 while (fcp->linenr < gap->ga_len
23621 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23622 ++fcp->linenr;
23623 if (fcp->linenr >= gap->ga_len)
23624 retval = NULL;
23625 else
23626 {
23627 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23628 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023629#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023630 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023631 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023632#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 }
23635
23636 /* Did we encounter a breakpoint? */
23637 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23638 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023639 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023641 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642 sourcing_lnum);
23643 fcp->dbg_tick = debug_tick;
23644 }
23645
23646 return retval;
23647}
23648
Bram Moolenaar05159a02005-02-26 23:04:13 +000023649#if defined(FEAT_PROFILE) || defined(PROTO)
23650/*
23651 * Called when starting to read a function line.
23652 * "sourcing_lnum" must be correct!
23653 * When skipping lines it may not actually be executed, but we won't find out
23654 * until later and we need to store the time now.
23655 */
23656 void
23657func_line_start(cookie)
23658 void *cookie;
23659{
23660 funccall_T *fcp = (funccall_T *)cookie;
23661 ufunc_T *fp = fcp->func;
23662
23663 if (fp->uf_profiling && sourcing_lnum >= 1
23664 && sourcing_lnum <= fp->uf_lines.ga_len)
23665 {
23666 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023667 /* Skip continuation lines. */
23668 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23669 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023670 fp->uf_tml_execed = FALSE;
23671 profile_start(&fp->uf_tml_start);
23672 profile_zero(&fp->uf_tml_children);
23673 profile_get_wait(&fp->uf_tml_wait);
23674 }
23675}
23676
23677/*
23678 * Called when actually executing a function line.
23679 */
23680 void
23681func_line_exec(cookie)
23682 void *cookie;
23683{
23684 funccall_T *fcp = (funccall_T *)cookie;
23685 ufunc_T *fp = fcp->func;
23686
23687 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23688 fp->uf_tml_execed = TRUE;
23689}
23690
23691/*
23692 * Called when done with a function line.
23693 */
23694 void
23695func_line_end(cookie)
23696 void *cookie;
23697{
23698 funccall_T *fcp = (funccall_T *)cookie;
23699 ufunc_T *fp = fcp->func;
23700
23701 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23702 {
23703 if (fp->uf_tml_execed)
23704 {
23705 ++fp->uf_tml_count[fp->uf_tml_idx];
23706 profile_end(&fp->uf_tml_start);
23707 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023708 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023709 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23710 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023711 }
23712 fp->uf_tml_idx = -1;
23713 }
23714}
23715#endif
23716
Bram Moolenaar071d4272004-06-13 20:20:40 +000023717/*
23718 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023719 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 */
23721 int
23722func_has_ended(cookie)
23723 void *cookie;
23724{
Bram Moolenaar33570922005-01-25 22:26:29 +000023725 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023726
23727 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23728 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023729 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023730 || fcp->returned);
23731}
23732
23733/*
23734 * return TRUE if cookie indicates a function which "abort"s on errors.
23735 */
23736 int
23737func_has_abort(cookie)
23738 void *cookie;
23739{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023740 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023741}
23742
23743#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23744typedef enum
23745{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023746 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23747 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23748 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023749} var_flavour_T;
23750
23751static var_flavour_T var_flavour __ARGS((char_u *varname));
23752
23753 static var_flavour_T
23754var_flavour(varname)
23755 char_u *varname;
23756{
23757 char_u *p = varname;
23758
23759 if (ASCII_ISUPPER(*p))
23760 {
23761 while (*(++p))
23762 if (ASCII_ISLOWER(*p))
23763 return VAR_FLAVOUR_SESSION;
23764 return VAR_FLAVOUR_VIMINFO;
23765 }
23766 else
23767 return VAR_FLAVOUR_DEFAULT;
23768}
23769#endif
23770
23771#if defined(FEAT_VIMINFO) || defined(PROTO)
23772/*
23773 * Restore global vars that start with a capital from the viminfo file
23774 */
23775 int
23776read_viminfo_varlist(virp, writing)
23777 vir_T *virp;
23778 int writing;
23779{
23780 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023781 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023782 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023783
23784 if (!writing && (find_viminfo_parameter('!') != NULL))
23785 {
23786 tab = vim_strchr(virp->vir_line + 1, '\t');
23787 if (tab != NULL)
23788 {
23789 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023790 switch (*tab)
23791 {
23792 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023793#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023794 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023795#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023796 case 'D': type = VAR_DICT; break;
23797 case 'L': type = VAR_LIST; break;
23798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023799
23800 tab = vim_strchr(tab, '\t');
23801 if (tab != NULL)
23802 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023803 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023804 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023805 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023806 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023807#ifdef FEAT_FLOAT
23808 else if (type == VAR_FLOAT)
23809 (void)string2float(tab + 1, &tv.vval.v_float);
23810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023811 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023812 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023813 if (type == VAR_DICT || type == VAR_LIST)
23814 {
23815 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23816
23817 if (etv == NULL)
23818 /* Failed to parse back the dict or list, use it as a
23819 * string. */
23820 tv.v_type = VAR_STRING;
23821 else
23822 {
23823 vim_free(tv.vval.v_string);
23824 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023825 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023826 }
23827 }
23828
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023829 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023830
23831 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023832 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023833 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23834 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835 }
23836 }
23837 }
23838
23839 return viminfo_readline(virp);
23840}
23841
23842/*
23843 * Write global vars that start with a capital to the viminfo file
23844 */
23845 void
23846write_viminfo_varlist(fp)
23847 FILE *fp;
23848{
Bram Moolenaar33570922005-01-25 22:26:29 +000023849 hashitem_T *hi;
23850 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023851 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023852 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023853 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023854 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023855 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023856
23857 if (find_viminfo_parameter('!') == NULL)
23858 return;
23859
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023860 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023861
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023862 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023863 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023865 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023867 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023868 this_var = HI2DI(hi);
23869 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023870 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023871 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023872 {
23873 case VAR_STRING: s = "STR"; break;
23874 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023875#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023876 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023877#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023878 case VAR_DICT: s = "DIC"; break;
23879 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023880 default: continue;
23881 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023882 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023883 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023884 if (p != NULL)
23885 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023886 vim_free(tofree);
23887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888 }
23889 }
23890}
23891#endif
23892
23893#if defined(FEAT_SESSION) || defined(PROTO)
23894 int
23895store_session_globals(fd)
23896 FILE *fd;
23897{
Bram Moolenaar33570922005-01-25 22:26:29 +000023898 hashitem_T *hi;
23899 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023900 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901 char_u *p, *t;
23902
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023903 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023904 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023905 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023906 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023908 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023909 this_var = HI2DI(hi);
23910 if ((this_var->di_tv.v_type == VAR_NUMBER
23911 || this_var->di_tv.v_type == VAR_STRING)
23912 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023913 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023914 /* Escape special characters with a backslash. Turn a LF and
23915 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023916 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023917 (char_u *)"\\\"\n\r");
23918 if (p == NULL) /* out of memory */
23919 break;
23920 for (t = p; *t != NUL; ++t)
23921 if (*t == '\n')
23922 *t = 'n';
23923 else if (*t == '\r')
23924 *t = 'r';
23925 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023926 this_var->di_key,
23927 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23928 : ' ',
23929 p,
23930 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23931 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023932 || put_eol(fd) == FAIL)
23933 {
23934 vim_free(p);
23935 return FAIL;
23936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 vim_free(p);
23938 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023939#ifdef FEAT_FLOAT
23940 else if (this_var->di_tv.v_type == VAR_FLOAT
23941 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23942 {
23943 float_T f = this_var->di_tv.vval.v_float;
23944 int sign = ' ';
23945
23946 if (f < 0)
23947 {
23948 f = -f;
23949 sign = '-';
23950 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023951 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023952 this_var->di_key, sign, f) < 0)
23953 || put_eol(fd) == FAIL)
23954 return FAIL;
23955 }
23956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023957 }
23958 }
23959 return OK;
23960}
23961#endif
23962
Bram Moolenaar661b1822005-07-28 22:36:45 +000023963/*
23964 * Display script name where an item was last set.
23965 * Should only be invoked when 'verbose' is non-zero.
23966 */
23967 void
23968last_set_msg(scriptID)
23969 scid_T scriptID;
23970{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023971 char_u *p;
23972
Bram Moolenaar661b1822005-07-28 22:36:45 +000023973 if (scriptID != 0)
23974 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023975 p = home_replace_save(NULL, get_scriptname(scriptID));
23976 if (p != NULL)
23977 {
23978 verbose_enter();
23979 MSG_PUTS(_("\n\tLast set from "));
23980 MSG_PUTS(p);
23981 vim_free(p);
23982 verbose_leave();
23983 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023984 }
23985}
23986
Bram Moolenaard812df62008-11-09 12:46:09 +000023987/*
23988 * List v:oldfiles in a nice way.
23989 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023990 void
23991ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023992 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023993{
23994 list_T *l = vimvars[VV_OLDFILES].vv_list;
23995 listitem_T *li;
23996 int nr = 0;
23997
23998 if (l == NULL)
23999 msg((char_u *)_("No old files"));
24000 else
24001 {
24002 msg_start();
24003 msg_scroll = TRUE;
24004 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24005 {
24006 msg_outnum((long)++nr);
24007 MSG_PUTS(": ");
24008 msg_outtrans(get_tv_string(&li->li_tv));
24009 msg_putchar('\n');
24010 out_flush(); /* output one line at a time */
24011 ui_breakcheck();
24012 }
24013 /* Assume "got_int" was set to truncate the listing. */
24014 got_int = FALSE;
24015
24016#ifdef FEAT_BROWSE_CMD
24017 if (cmdmod.browse)
24018 {
24019 quit_more = FALSE;
24020 nr = prompt_for_number(FALSE);
24021 msg_starthere();
24022 if (nr > 0)
24023 {
24024 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24025 (long)nr);
24026
24027 if (p != NULL)
24028 {
24029 p = expand_env_save(p);
24030 eap->arg = p;
24031 eap->cmdidx = CMD_edit;
24032 cmdmod.browse = FALSE;
24033 do_exedit(eap, NULL);
24034 vim_free(p);
24035 }
24036 }
24037 }
24038#endif
24039 }
24040}
24041
Bram Moolenaar071d4272004-06-13 20:20:40 +000024042#endif /* FEAT_EVAL */
24043
Bram Moolenaar071d4272004-06-13 20:20:40 +000024044
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024045#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024046
24047#ifdef WIN3264
24048/*
24049 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24050 */
24051static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24052static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24053static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24054
24055/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024056 * Get the short path (8.3) for the filename in "fnamep".
24057 * Only works for a valid file name.
24058 * When the path gets longer "fnamep" is changed and the allocated buffer
24059 * is put in "bufp".
24060 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24061 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024062 */
24063 static int
24064get_short_pathname(fnamep, bufp, fnamelen)
24065 char_u **fnamep;
24066 char_u **bufp;
24067 int *fnamelen;
24068{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024069 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024070 char_u *newbuf;
24071
24072 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073 l = GetShortPathName(*fnamep, *fnamep, len);
24074 if (l > len - 1)
24075 {
24076 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024077 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 newbuf = vim_strnsave(*fnamep, l);
24079 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024080 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081
24082 vim_free(*bufp);
24083 *fnamep = *bufp = newbuf;
24084
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024085 /* Really should always succeed, as the buffer is big enough. */
24086 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 }
24088
24089 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024090 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024091}
24092
24093/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024094 * Get the short path (8.3) for the filename in "fname". The converted
24095 * path is returned in "bufp".
24096 *
24097 * Some of the directories specified in "fname" may not exist. This function
24098 * will shorten the existing directories at the beginning of the path and then
24099 * append the remaining non-existing path.
24100 *
24101 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024102 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024103 * bufp - Pointer to an allocated buffer for the filename.
24104 * fnamelen - Length of the filename pointed to by fname
24105 *
24106 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107 */
24108 static int
24109shortpath_for_invalid_fname(fname, bufp, fnamelen)
24110 char_u **fname;
24111 char_u **bufp;
24112 int *fnamelen;
24113{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024114 char_u *short_fname, *save_fname, *pbuf_unused;
24115 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024116 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024117 int old_len, len;
24118 int new_len, sfx_len;
24119 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024120
24121 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024122 old_len = *fnamelen;
24123 save_fname = vim_strnsave(*fname, old_len);
24124 pbuf_unused = NULL;
24125 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024126
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024127 endp = save_fname + old_len - 1; /* Find the end of the copy */
24128 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024129
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024130 /*
24131 * Try shortening the supplied path till it succeeds by removing one
24132 * directory at a time from the tail of the path.
24133 */
24134 len = 0;
24135 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024137 /* go back one path-separator */
24138 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24139 --endp;
24140 if (endp <= save_fname)
24141 break; /* processed the complete path */
24142
24143 /*
24144 * Replace the path separator with a NUL and try to shorten the
24145 * resulting path.
24146 */
24147 ch = *endp;
24148 *endp = 0;
24149 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024150 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024151 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24152 {
24153 retval = FAIL;
24154 goto theend;
24155 }
24156 *endp = ch; /* preserve the string */
24157
24158 if (len > 0)
24159 break; /* successfully shortened the path */
24160
24161 /* failed to shorten the path. Skip the path separator */
24162 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024163 }
24164
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024165 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024167 /*
24168 * Succeeded in shortening the path. Now concatenate the shortened
24169 * path with the remaining path at the tail.
24170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024171
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024172 /* Compute the length of the new path. */
24173 sfx_len = (int)(save_endp - endp) + 1;
24174 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024175
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024176 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024177 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024178 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024179 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024180 /* There is not enough space in the currently allocated string,
24181 * copy it to a buffer big enough. */
24182 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024184 {
24185 retval = FAIL;
24186 goto theend;
24187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188 }
24189 else
24190 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024191 /* Transfer short_fname to the main buffer (it's big enough),
24192 * unless get_short_pathname() did its work in-place. */
24193 *fname = *bufp = save_fname;
24194 if (short_fname != save_fname)
24195 vim_strncpy(save_fname, short_fname, len);
24196 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024197 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024198
24199 /* concat the not-shortened part of the path */
24200 vim_strncpy(*fname + len, endp, sfx_len);
24201 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024202 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024203
24204theend:
24205 vim_free(pbuf_unused);
24206 vim_free(save_fname);
24207
24208 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024209}
24210
24211/*
24212 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024213 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 */
24215 static int
24216shortpath_for_partial(fnamep, bufp, fnamelen)
24217 char_u **fnamep;
24218 char_u **bufp;
24219 int *fnamelen;
24220{
24221 int sepcount, len, tflen;
24222 char_u *p;
24223 char_u *pbuf, *tfname;
24224 int hasTilde;
24225
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024226 /* Count up the path separators from the RHS.. so we know which part
24227 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024229 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024230 if (vim_ispathsep(*p))
24231 ++sepcount;
24232
24233 /* Need full path first (use expand_env() to remove a "~/") */
24234 hasTilde = (**fnamep == '~');
24235 if (hasTilde)
24236 pbuf = tfname = expand_env_save(*fnamep);
24237 else
24238 pbuf = tfname = FullName_save(*fnamep, FALSE);
24239
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024240 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024242 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024244
24245 if (len == 0)
24246 {
24247 /* Don't have a valid filename, so shorten the rest of the
24248 * path if we can. This CAN give us invalid 8.3 filenames, but
24249 * there's not a lot of point in guessing what it might be.
24250 */
24251 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024252 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24253 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254 }
24255
24256 /* Count the paths backward to find the beginning of the desired string. */
24257 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024258 {
24259#ifdef FEAT_MBYTE
24260 if (has_mbyte)
24261 p -= mb_head_off(tfname, p);
24262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024263 if (vim_ispathsep(*p))
24264 {
24265 if (sepcount == 0 || (hasTilde && sepcount == 1))
24266 break;
24267 else
24268 sepcount --;
24269 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024271 if (hasTilde)
24272 {
24273 --p;
24274 if (p >= tfname)
24275 *p = '~';
24276 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024277 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278 }
24279 else
24280 ++p;
24281
24282 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24283 vim_free(*bufp);
24284 *fnamelen = (int)STRLEN(p);
24285 *bufp = pbuf;
24286 *fnamep = p;
24287
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024288 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289}
24290#endif /* WIN3264 */
24291
24292/*
24293 * Adjust a filename, according to a string of modifiers.
24294 * *fnamep must be NUL terminated when called. When returning, the length is
24295 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024296 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297 * When there is an error, *fnamep is set to NULL.
24298 */
24299 int
24300modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24301 char_u *src; /* string with modifiers */
24302 int *usedlen; /* characters after src that are used */
24303 char_u **fnamep; /* file name so far */
24304 char_u **bufp; /* buffer for allocated file name or NULL */
24305 int *fnamelen; /* length of fnamep */
24306{
24307 int valid = 0;
24308 char_u *tail;
24309 char_u *s, *p, *pbuf;
24310 char_u dirname[MAXPATHL];
24311 int c;
24312 int has_fullname = 0;
24313#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024314 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315 int has_shortname = 0;
24316#endif
24317
24318repeat:
24319 /* ":p" - full path/file_name */
24320 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24321 {
24322 has_fullname = 1;
24323
24324 valid |= VALID_PATH;
24325 *usedlen += 2;
24326
24327 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24328 if ((*fnamep)[0] == '~'
24329#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24330 && ((*fnamep)[1] == '/'
24331# ifdef BACKSLASH_IN_FILENAME
24332 || (*fnamep)[1] == '\\'
24333# endif
24334 || (*fnamep)[1] == NUL)
24335
24336#endif
24337 )
24338 {
24339 *fnamep = expand_env_save(*fnamep);
24340 vim_free(*bufp); /* free any allocated file name */
24341 *bufp = *fnamep;
24342 if (*fnamep == NULL)
24343 return -1;
24344 }
24345
24346 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024347 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024348 {
24349 if (vim_ispathsep(*p)
24350 && p[1] == '.'
24351 && (p[2] == NUL
24352 || vim_ispathsep(p[2])
24353 || (p[2] == '.'
24354 && (p[3] == NUL || vim_ispathsep(p[3])))))
24355 break;
24356 }
24357
24358 /* FullName_save() is slow, don't use it when not needed. */
24359 if (*p != NUL || !vim_isAbsName(*fnamep))
24360 {
24361 *fnamep = FullName_save(*fnamep, *p != NUL);
24362 vim_free(*bufp); /* free any allocated file name */
24363 *bufp = *fnamep;
24364 if (*fnamep == NULL)
24365 return -1;
24366 }
24367
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024368#ifdef WIN3264
24369# if _WIN32_WINNT >= 0x0500
24370 if (vim_strchr(*fnamep, '~') != NULL)
24371 {
24372 /* Expand 8.3 filename to full path. Needed to make sure the same
24373 * file does not have two different names.
24374 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24375 p = alloc(_MAX_PATH + 1);
24376 if (p != NULL)
24377 {
24378 if (GetLongPathName(*fnamep, p, MAXPATHL))
24379 {
24380 vim_free(*bufp);
24381 *bufp = *fnamep = p;
24382 }
24383 else
24384 vim_free(p);
24385 }
24386 }
24387# endif
24388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024389 /* Append a path separator to a directory. */
24390 if (mch_isdir(*fnamep))
24391 {
24392 /* Make room for one or two extra characters. */
24393 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24394 vim_free(*bufp); /* free any allocated file name */
24395 *bufp = *fnamep;
24396 if (*fnamep == NULL)
24397 return -1;
24398 add_pathsep(*fnamep);
24399 }
24400 }
24401
24402 /* ":." - path relative to the current directory */
24403 /* ":~" - path relative to the home directory */
24404 /* ":8" - shortname path - postponed till after */
24405 while (src[*usedlen] == ':'
24406 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24407 {
24408 *usedlen += 2;
24409 if (c == '8')
24410 {
24411#ifdef WIN3264
24412 has_shortname = 1; /* Postpone this. */
24413#endif
24414 continue;
24415 }
24416 pbuf = NULL;
24417 /* Need full path first (use expand_env() to remove a "~/") */
24418 if (!has_fullname)
24419 {
24420 if (c == '.' && **fnamep == '~')
24421 p = pbuf = expand_env_save(*fnamep);
24422 else
24423 p = pbuf = FullName_save(*fnamep, FALSE);
24424 }
24425 else
24426 p = *fnamep;
24427
24428 has_fullname = 0;
24429
24430 if (p != NULL)
24431 {
24432 if (c == '.')
24433 {
24434 mch_dirname(dirname, MAXPATHL);
24435 s = shorten_fname(p, dirname);
24436 if (s != NULL)
24437 {
24438 *fnamep = s;
24439 if (pbuf != NULL)
24440 {
24441 vim_free(*bufp); /* free any allocated file name */
24442 *bufp = pbuf;
24443 pbuf = NULL;
24444 }
24445 }
24446 }
24447 else
24448 {
24449 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24450 /* Only replace it when it starts with '~' */
24451 if (*dirname == '~')
24452 {
24453 s = vim_strsave(dirname);
24454 if (s != NULL)
24455 {
24456 *fnamep = s;
24457 vim_free(*bufp);
24458 *bufp = s;
24459 }
24460 }
24461 }
24462 vim_free(pbuf);
24463 }
24464 }
24465
24466 tail = gettail(*fnamep);
24467 *fnamelen = (int)STRLEN(*fnamep);
24468
24469 /* ":h" - head, remove "/file_name", can be repeated */
24470 /* Don't remove the first "/" or "c:\" */
24471 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24472 {
24473 valid |= VALID_HEAD;
24474 *usedlen += 2;
24475 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024476 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024477 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024478 *fnamelen = (int)(tail - *fnamep);
24479#ifdef VMS
24480 if (*fnamelen > 0)
24481 *fnamelen += 1; /* the path separator is part of the path */
24482#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024483 if (*fnamelen == 0)
24484 {
24485 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24486 p = vim_strsave((char_u *)".");
24487 if (p == NULL)
24488 return -1;
24489 vim_free(*bufp);
24490 *bufp = *fnamep = tail = p;
24491 *fnamelen = 1;
24492 }
24493 else
24494 {
24495 while (tail > s && !after_pathsep(s, tail))
24496 mb_ptr_back(*fnamep, tail);
24497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024498 }
24499
24500 /* ":8" - shortname */
24501 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24502 {
24503 *usedlen += 2;
24504#ifdef WIN3264
24505 has_shortname = 1;
24506#endif
24507 }
24508
24509#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024510 /*
24511 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512 */
24513 if (has_shortname)
24514 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024515 /* Copy the string if it is shortened by :h and when it wasn't copied
24516 * yet, because we are going to change it in place. Avoids changing
24517 * the buffer name for "%:8". */
24518 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024519 {
24520 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024521 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024522 return -1;
24523 vim_free(*bufp);
24524 *bufp = *fnamep = p;
24525 }
24526
24527 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024528 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024529 if (!has_fullname && !vim_isAbsName(*fnamep))
24530 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024531 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024532 return -1;
24533 }
24534 else
24535 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024536 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537
Bram Moolenaardc935552011-08-17 15:23:23 +020024538 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024539 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024540 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541 return -1;
24542
24543 if (l == 0)
24544 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024545 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024547 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024548 return -1;
24549 }
24550 *fnamelen = l;
24551 }
24552 }
24553#endif /* WIN3264 */
24554
24555 /* ":t" - tail, just the basename */
24556 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24557 {
24558 *usedlen += 2;
24559 *fnamelen -= (int)(tail - *fnamep);
24560 *fnamep = tail;
24561 }
24562
24563 /* ":e" - extension, can be repeated */
24564 /* ":r" - root, without extension, can be repeated */
24565 while (src[*usedlen] == ':'
24566 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24567 {
24568 /* find a '.' in the tail:
24569 * - for second :e: before the current fname
24570 * - otherwise: The last '.'
24571 */
24572 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24573 s = *fnamep - 2;
24574 else
24575 s = *fnamep + *fnamelen - 1;
24576 for ( ; s > tail; --s)
24577 if (s[0] == '.')
24578 break;
24579 if (src[*usedlen + 1] == 'e') /* :e */
24580 {
24581 if (s > tail)
24582 {
24583 *fnamelen += (int)(*fnamep - (s + 1));
24584 *fnamep = s + 1;
24585#ifdef VMS
24586 /* cut version from the extension */
24587 s = *fnamep + *fnamelen - 1;
24588 for ( ; s > *fnamep; --s)
24589 if (s[0] == ';')
24590 break;
24591 if (s > *fnamep)
24592 *fnamelen = s - *fnamep;
24593#endif
24594 }
24595 else if (*fnamep <= tail)
24596 *fnamelen = 0;
24597 }
24598 else /* :r */
24599 {
24600 if (s > tail) /* remove one extension */
24601 *fnamelen = (int)(s - *fnamep);
24602 }
24603 *usedlen += 2;
24604 }
24605
24606 /* ":s?pat?foo?" - substitute */
24607 /* ":gs?pat?foo?" - global substitute */
24608 if (src[*usedlen] == ':'
24609 && (src[*usedlen + 1] == 's'
24610 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24611 {
24612 char_u *str;
24613 char_u *pat;
24614 char_u *sub;
24615 int sep;
24616 char_u *flags;
24617 int didit = FALSE;
24618
24619 flags = (char_u *)"";
24620 s = src + *usedlen + 2;
24621 if (src[*usedlen + 1] == 'g')
24622 {
24623 flags = (char_u *)"g";
24624 ++s;
24625 }
24626
24627 sep = *s++;
24628 if (sep)
24629 {
24630 /* find end of pattern */
24631 p = vim_strchr(s, sep);
24632 if (p != NULL)
24633 {
24634 pat = vim_strnsave(s, (int)(p - s));
24635 if (pat != NULL)
24636 {
24637 s = p + 1;
24638 /* find end of substitution */
24639 p = vim_strchr(s, sep);
24640 if (p != NULL)
24641 {
24642 sub = vim_strnsave(s, (int)(p - s));
24643 str = vim_strnsave(*fnamep, *fnamelen);
24644 if (sub != NULL && str != NULL)
24645 {
24646 *usedlen = (int)(p + 1 - src);
24647 s = do_string_sub(str, pat, sub, flags);
24648 if (s != NULL)
24649 {
24650 *fnamep = s;
24651 *fnamelen = (int)STRLEN(s);
24652 vim_free(*bufp);
24653 *bufp = s;
24654 didit = TRUE;
24655 }
24656 }
24657 vim_free(sub);
24658 vim_free(str);
24659 }
24660 vim_free(pat);
24661 }
24662 }
24663 /* after using ":s", repeat all the modifiers */
24664 if (didit)
24665 goto repeat;
24666 }
24667 }
24668
Bram Moolenaar26df0922014-02-23 23:39:13 +010024669 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24670 {
24671 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24672 if (p == NULL)
24673 return -1;
24674 vim_free(*bufp);
24675 *bufp = *fnamep = p;
24676 *fnamelen = (int)STRLEN(p);
24677 *usedlen += 2;
24678 }
24679
Bram Moolenaar071d4272004-06-13 20:20:40 +000024680 return valid;
24681}
24682
24683/*
24684 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24685 * "flags" can be "g" to do a global substitute.
24686 * Returns an allocated string, NULL for error.
24687 */
24688 char_u *
24689do_string_sub(str, pat, sub, flags)
24690 char_u *str;
24691 char_u *pat;
24692 char_u *sub;
24693 char_u *flags;
24694{
24695 int sublen;
24696 regmatch_T regmatch;
24697 int i;
24698 int do_all;
24699 char_u *tail;
24700 garray_T ga;
24701 char_u *ret;
24702 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024703 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024704
24705 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24706 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024707 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024708
24709 ga_init2(&ga, 1, 200);
24710
24711 do_all = (flags[0] == 'g');
24712
24713 regmatch.rm_ic = p_ic;
24714 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24715 if (regmatch.regprog != NULL)
24716 {
24717 tail = str;
24718 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24719 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024720 /* Skip empty match except for first match. */
24721 if (regmatch.startp[0] == regmatch.endp[0])
24722 {
24723 if (zero_width == regmatch.startp[0])
24724 {
24725 /* avoid getting stuck on a match with an empty string */
24726 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24727 ++ga.ga_len;
24728 continue;
24729 }
24730 zero_width = regmatch.startp[0];
24731 }
24732
Bram Moolenaar071d4272004-06-13 20:20:40 +000024733 /*
24734 * Get some space for a temporary buffer to do the substitution
24735 * into. It will contain:
24736 * - The text up to where the match is.
24737 * - The substituted text.
24738 * - The text after the match.
24739 */
24740 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24741 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24742 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24743 {
24744 ga_clear(&ga);
24745 break;
24746 }
24747
24748 /* copy the text up to where the match is */
24749 i = (int)(regmatch.startp[0] - tail);
24750 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24751 /* add the substituted text */
24752 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24753 + ga.ga_len + i, TRUE, TRUE, FALSE);
24754 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024755 tail = regmatch.endp[0];
24756 if (*tail == NUL)
24757 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024758 if (!do_all)
24759 break;
24760 }
24761
24762 if (ga.ga_data != NULL)
24763 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24764
Bram Moolenaar473de612013-06-08 18:19:48 +020024765 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766 }
24767
24768 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24769 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024770 if (p_cpo == empty_option)
24771 p_cpo = save_cpo;
24772 else
24773 /* Darn, evaluating {sub} expression changed the value. */
24774 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024775
24776 return ret;
24777}
24778
24779#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */