blob: 5d0885401f085a7d5ba118bab34c0205a53e1a7f [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 Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200114#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000117
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100118#define NAMESPACE_CHAR (char_u *)"abglstvw"
119
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200120static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000121#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124 * Old Vim variables such as "v:version" are also available without the "v:".
125 * Also in functions. We need a special hashtable for them.
126 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000127static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128
129/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000130 * When recursively copying lists and dicts we need to remember which ones we
131 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000132 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133 */
134static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000135#define COPYID_INC 2
136#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137
Bram Moolenaar8502c702014-06-17 12:51:16 +0200138/* Abort conversion to string after a recursion error. */
139static int did_echo_string_emsg = FALSE;
140
Bram Moolenaard9fba312005-06-26 22:34:35 +0000141/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000142 * Array to hold the hashtab with variables local to each sourced script.
143 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000145typedef struct
146{
147 dictitem_T sv_var;
148 dict_T sv_dict;
149} scriptvar_T;
150
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200151static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
152#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
153#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
155static int echo_attr = 0; /* attributes used for ":echo" */
156
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000157/* Values for trans_function_name() argument: */
158#define TFN_INT 1 /* internal function name OK */
159#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100160#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
161
162/* Values for get_lval() flags argument: */
163#define GLV_QUIET TFN_QUIET /* no error messages */
164#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166/*
167 * Structure to hold info for a user function.
168 */
169typedef struct ufunc ufunc_T;
170
171struct ufunc
172{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000173 int uf_varargs; /* variable nr of arguments */
174 int uf_flags;
175 int uf_calls; /* nr of active calls */
176 garray_T uf_args; /* arguments */
177 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000178#ifdef FEAT_PROFILE
179 int uf_profiling; /* TRUE when func is being profiled */
180 /* profiling the function as a whole */
181 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000182 proftime_T uf_tm_total; /* time spent in function + children */
183 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000184 proftime_T uf_tm_children; /* time spent in children this call */
185 /* profiling the function per line */
186 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000187 proftime_T *uf_tml_total; /* time spent in a line + children */
188 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000189 proftime_T uf_tml_start; /* start time for current line */
190 proftime_T uf_tml_children; /* time spent in children for this line */
191 proftime_T uf_tml_wait; /* start wait time for current line */
192 int uf_tml_idx; /* index of line being timed; -1 if none */
193 int uf_tml_execed; /* line being timed was executed */
194#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000195 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000197 int uf_refcount; /* for numbered function: reference count */
198 char_u uf_name[1]; /* name of function (actually longer); can
199 start with <SNR>123_ (<SNR> is K_SPECIAL
200 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201};
202
203/* function flags */
204#define FC_ABORT 1 /* abort function on error */
205#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000206#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207
208/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000209 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000211static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000213/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000214static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
215
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000216/* list heads for garbage collection */
217static dict_T *first_dict = NULL; /* list of all dicts */
218static list_T *first_list = NULL; /* list of all lists */
219
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000220/* From user function to hashitem and back. */
221static ufunc_T dumuf;
222#define UF2HIKEY(fp) ((fp)->uf_name)
223#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
224#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
225
226#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
227#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
Bram Moolenaar33570922005-01-25 22:26:29 +0000229#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
230#define VAR_SHORT_LEN 20 /* short variable name length */
231#define FIXVAR_CNT 12 /* number of fixed variables */
232
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000234typedef struct funccall_S funccall_T;
235
236struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237{
238 ufunc_T *func; /* function being called */
239 int linenr; /* next line to be executed */
240 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000241 struct /* fixed variables for arguments */
242 {
243 dictitem_T var; /* variable (without room for name) */
244 char_u room[VAR_SHORT_LEN]; /* room for the name */
245 } fixvar[FIXVAR_CNT];
246 dict_T l_vars; /* l: local function variables */
247 dictitem_T l_vars_var; /* variable for l: scope */
248 dict_T l_avars; /* a: argument variables */
249 dictitem_T l_avars_var; /* variable for a: scope */
250 list_T l_varlist; /* list for a:000 */
251 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
252 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 linenr_T breakpoint; /* next line with breakpoint or zero */
254 int dbg_tick; /* debug_tick when breakpoint was set */
255 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000256#ifdef FEAT_PROFILE
257 proftime_T prof_child; /* time spent in a child */
258#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000259 funccall_T *caller; /* calling function or NULL */
260};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
262/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000263 * Info used by a ":for" loop.
264 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000266{
267 int fi_semicolon; /* TRUE if ending in '; var]' */
268 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000269 listwatch_T fi_lw; /* keep an eye on the item used. */
270 list_T *fi_list; /* list being used */
271} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000274 * Struct used by trans_function_name()
275 */
276typedef struct
277{
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000279 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000280 dictitem_T *fd_di; /* Dictionary item used */
281} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000282
Bram Moolenaara7043832005-01-21 11:56:39 +0000283
284/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000285 * Array to hold the value of v: variables.
286 * The value is in a dictitem, so that it can also be used in the v: scope.
287 * The reason to use this table anyway is for very quick access to the
288 * variables with the VV_ defines.
289 */
290#include "version.h"
291
292/* values for vv_flags: */
293#define VV_COMPAT 1 /* compatible, also used without "v:" */
294#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000295#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000297#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000298
299static struct vimvar
300{
301 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000302 dictitem_T vv_di; /* value and name for key */
303 char vv_filler[16]; /* space for LONGEST name below!!! */
304 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
305} vimvars[VV_LEN] =
306{
307 /*
308 * The order here must match the VV_ defines in vim.h!
309 * Initializing a union does not work, leave tv.vval empty to get zero's.
310 */
311 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("count1", VAR_NUMBER), VV_RO},
313 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
314 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
315 {VV_NAME("warningmsg", VAR_STRING), 0},
316 {VV_NAME("statusmsg", VAR_STRING), 0},
317 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
319 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
320 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("termresponse", VAR_STRING), VV_RO},
322 {VV_NAME("fname", VAR_STRING), VV_RO},
323 {VV_NAME("lang", VAR_STRING), VV_RO},
324 {VV_NAME("lc_time", VAR_STRING), VV_RO},
325 {VV_NAME("ctype", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
327 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
328 {VV_NAME("fname_in", VAR_STRING), VV_RO},
329 {VV_NAME("fname_out", VAR_STRING), VV_RO},
330 {VV_NAME("fname_new", VAR_STRING), VV_RO},
331 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
332 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
333 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
336 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
337 {VV_NAME("progname", VAR_STRING), VV_RO},
338 {VV_NAME("servername", VAR_STRING), VV_RO},
339 {VV_NAME("dying", VAR_NUMBER), VV_RO},
340 {VV_NAME("exception", VAR_STRING), VV_RO},
341 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
342 {VV_NAME("register", VAR_STRING), VV_RO},
343 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
344 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
346 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000347 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000348 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
349 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000350 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000355 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000356 {VV_NAME("swapname", VAR_STRING), VV_RO},
357 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000358 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200359 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000360 {VV_NAME("mouse_win", VAR_NUMBER), 0},
361 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
362 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000363 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100365 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000366 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200367 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200368 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200369 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200370 {VV_NAME("option_new", VAR_STRING), VV_RO},
371 {VV_NAME("option_old", VAR_STRING), VV_RO},
372 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100373 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000374};
375
376/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000377#define vv_type vv_di.di_tv.v_type
378#define vv_nr vv_di.di_tv.vval.v_number
379#define vv_float vv_di.di_tv.vval.v_float
380#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000381#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200382#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000384
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200385static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000386#define vimvarht vimvardict.dv_hashtab
387
Bram Moolenaara40058a2005-07-11 22:42:07 +0000388static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
389static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000390static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
391static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
392static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
394static void list_glob_vars __ARGS((int *first));
395static void list_buf_vars __ARGS((int *first));
396static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000398static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000399#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000400static void list_vim_vars __ARGS((int *first));
401static void list_script_vars __ARGS((int *first));
402static void list_func_vars __ARGS((int *first));
403static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000404static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
405static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100406static 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 +0000407static void clear_lval __ARGS((lval_T *lp));
408static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
409static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000410static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
411static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
412static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
413static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
414static void item_lock __ARGS((typval_T *tv, int deep, int lock));
415static int tv_islocked __ARGS((typval_T *tv));
416
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
418static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000423static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
424static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000425
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000426static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
429static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
430static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000431static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100433static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
434static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
435static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000436static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000438static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
440static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000441static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100443static 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 +0000444static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000445static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200446static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
448static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000449static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000451static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000453static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
454static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#ifdef FEAT_FLOAT
457static int string2float __ARGS((char_u *text, float_T *value));
458#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
460static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100461static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000462static 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 +0200463static 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 +0000464static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000465static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
468static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200469static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100472static void f_alloc_fail __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100473static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000474static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200477static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000478static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100479static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara803c7f2016-01-15 15:31:39 +0100480static void f_assert_exception __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara260b872016-01-15 20:48:22 +0100481static void f_assert_fails __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100482static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200485static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000486static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200487static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100498static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000499static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100500static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#ifdef FEAT_FLOAT
503static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
504#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000505static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000508static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000510#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000511static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000512static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#ifdef FEAT_FLOAT
518static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200519static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000520#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
524static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200534static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200536#ifdef FEAT_FLOAT
537static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
538#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000541static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000547#ifdef FEAT_FLOAT
548static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200550static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000551#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000552static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000561static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000563static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200567static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000570static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200571static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000579static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000580static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200581static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000582static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000583static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200586static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000587static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100593static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000596static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000610static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100615static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000617static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000629#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200630static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000631static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
632#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200633#ifdef FEAT_LUA
634static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
635#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000640static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200641static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000642static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000643static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000645static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000646static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000649#ifdef vim_mkdir
650static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
651#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100653#ifdef FEAT_MZSCHEME
654static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
655#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100658static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000659static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100660#ifdef FEAT_PERL
661static void f_perleval __ARGS((typval_T *argvars, typval_T *rettv));
662#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000663#ifdef FEAT_FLOAT
664static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
665#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000667static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000668static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200669#ifdef FEAT_PYTHON3
670static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
671#endif
672#ifdef FEAT_PYTHON
673static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
674#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000676static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000677static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#ifdef FEAT_FLOAT
690static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
691#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200692static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100694static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000697static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000699static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
703static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200704static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000707static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000708static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000709static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000710static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000711static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200712static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000713static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100715#ifdef FEAT_CRYPT
716static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
717#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000718static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200719static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000721#ifdef FEAT_FLOAT
722static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200723static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000724#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000725static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000726static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000727static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000729static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
731static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
733#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000734static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200735static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000736#ifdef HAVE_STRFTIME
737static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
739static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200745static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200746static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000752static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200753static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200755static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000756static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000757static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000758static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000759static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000760static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000762static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200763#ifdef FEAT_FLOAT
764static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
766#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
768static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
769static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000770#ifdef FEAT_FLOAT
771static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
772#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200774static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200775static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100776static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
778static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
779static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100780static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
782static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
783static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
784static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
785static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
786static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000787static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
788static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000789static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000790static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaared767a22016-01-03 22:49:16 +0100791static void f_wordcount __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100792static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000793
Bram Moolenaar493c1782014-05-28 14:34:46 +0200794static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000795static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796static int get_env_len __ARGS((char_u **arg));
797static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000798static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000799static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
800#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
801#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
802 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000803static 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 +0000804static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000805static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200806static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000807static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000808static typval_T *alloc_tv __ARGS((void));
809static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000810static void init_tv __ARGS((typval_T *varp));
811static long get_tv_number __ARGS((typval_T *varp));
812static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000813static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000814static char_u *get_tv_string __ARGS((typval_T *varp));
815static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000816static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100817static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
818static 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 +0000819static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
Bram Moolenaarf1f60f82016-01-16 15:40:53 +0100820static funccall_T *get_funccal __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000821static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
822static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000823static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
824static 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 +0000825static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200826static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
827static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200828static int var_check_func_name __ARGS((char_u *name, int new_var));
829static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200830static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000831static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000832static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
833static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
834static int eval_fname_script __ARGS((char_u *p));
835static int eval_fname_sid __ARGS((char_u *p));
836static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000837static ufunc_T *find_func __ARGS((char_u *name));
838static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200839static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000840#ifdef FEAT_PROFILE
841static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000842static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
843static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
844static int
845# ifdef __BORLANDC__
846 _RTLENTRYF
847# endif
848 prof_total_cmp __ARGS((const void *s1, const void *s2));
849static int
850# ifdef __BORLANDC__
851 _RTLENTRYF
852# endif
853 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200855static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000856static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000857static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000858static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000859static 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 +0000860static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
861static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000862static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000863static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
Bram Moolenaarc9703302016-01-17 21:49:33 +0100864static win_T *find_tabwin __ARGS((typval_T *wvp, typval_T *tvp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000865static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000866static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000867static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000868static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200869static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200870static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000871
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200872
873#ifdef EBCDIC
874static int compare_func_name __ARGS((const void *s1, const void *s2));
875static void sortFunctions __ARGS(());
876#endif
877
Bram Moolenaar33570922005-01-25 22:26:29 +0000878/*
879 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000880 */
881 void
882eval_init()
883{
Bram Moolenaar33570922005-01-25 22:26:29 +0000884 int i;
885 struct vimvar *p;
886
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200887 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
888 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200889 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000890 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000891 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000892
893 for (i = 0; i < VV_LEN; ++i)
894 {
895 p = &vimvars[i];
896 STRCPY(p->vv_di.di_key, p->vv_name);
897 if (p->vv_flags & VV_RO)
898 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
899 else if (p->vv_flags & VV_RO_SBX)
900 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
901 else
902 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000903
904 /* add to v: scope dict, unless the value is not always available */
905 if (p->vv_type != VAR_UNKNOWN)
906 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000907 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000908 /* add to compat scope dict */
909 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000910 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000911 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100912 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200913 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100914 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200915 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200916
917#ifdef EBCDIC
918 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100919 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200920 */
921 sortFunctions();
922#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000923}
924
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000925#if defined(EXITFREE) || defined(PROTO)
926 void
927eval_clear()
928{
929 int i;
930 struct vimvar *p;
931
932 for (i = 0; i < VV_LEN; ++i)
933 {
934 p = &vimvars[i];
935 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000936 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000937 vim_free(p->vv_str);
938 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000939 }
940 else if (p->vv_di.di_tv.v_type == VAR_LIST)
941 {
942 list_unref(p->vv_list);
943 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000944 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000945 }
946 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000947 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000948 hash_clear(&compat_hashtab);
949
Bram Moolenaard9fba312005-06-26 22:34:35 +0000950 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100951# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200952 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100953# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000954
955 /* global variables */
956 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000957
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000958 /* autoloaded script names */
959 ga_clear_strings(&ga_loaded);
960
Bram Moolenaarcca74132013-09-25 21:00:28 +0200961 /* Script-local variables. First clear all the variables and in a second
962 * loop free the scriptvar_T, because a variable in one script might hold
963 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200964 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200965 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200966 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200967 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200968 ga_clear(&ga_scripts);
969
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000970 /* unreferenced lists and dicts */
971 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000972
973 /* functions */
974 free_all_functions();
975 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000976}
977#endif
978
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 * Return the name of the executed function.
981 */
982 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100983func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000985 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988/*
989 * Return the address holding the next breakpoint line for a funccall cookie.
990 */
991 linenr_T *
992func_breakpoint(cookie)
993 void *cookie;
994{
Bram Moolenaar33570922005-01-25 22:26:29 +0000995 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996}
997
998/*
999 * Return the address holding the debug tick for a funccall cookie.
1000 */
1001 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001002func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
Bram Moolenaar33570922005-01-25 22:26:29 +00001004 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005}
1006
1007/*
1008 * Return the nesting level for a funccall cookie.
1009 */
1010 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001011func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012{
Bram Moolenaar33570922005-01-25 22:26:29 +00001013 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014}
1015
1016/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001017funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001019/* pointer to list of previously used funccal, still around because some
1020 * item in it is still being used. */
1021funccall_T *previous_funccal = NULL;
1022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023/*
1024 * Return TRUE when a function was ended by a ":return" command.
1025 */
1026 int
1027current_func_returned()
1028{
1029 return current_funccal->returned;
1030}
1031
1032
1033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 * Set an internal variable to a string value. Creates the variable if it does
1035 * not already exist.
1036 */
1037 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001038set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001040 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001041 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
1043 val = vim_strsave(value);
1044 if (val != NULL)
1045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001046 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001047 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001049 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001050 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 }
1052 }
1053}
1054
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001056static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057static char_u *redir_endp = NULL;
1058static char_u *redir_varname = NULL;
1059
1060/*
1061 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001062 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 * Returns OK if successfully completed the setup. FAIL otherwise.
1064 */
1065 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001066var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001067{
1068 int save_emsg;
1069 int err;
1070 typval_T tv;
1071
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001072 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001073 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001074 {
1075 EMSG(_(e_invarg));
1076 return FAIL;
1077 }
1078
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001079 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080 redir_varname = vim_strsave(name);
1081 if (redir_varname == NULL)
1082 return FAIL;
1083
1084 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1085 if (redir_lval == NULL)
1086 {
1087 var_redir_stop();
1088 return FAIL;
1089 }
1090
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001091 /* The output is stored in growarray "redir_ga" until redirection ends. */
1092 ga_init2(&redir_ga, (int)sizeof(char), 500);
1093
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001095 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001096 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1098 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001099 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100 if (redir_endp != NULL && *redir_endp != NUL)
1101 /* Trailing characters are present after the variable name */
1102 EMSG(_(e_trailing));
1103 else
1104 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001105 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 var_redir_stop();
1107 return FAIL;
1108 }
1109
1110 /* check if we can write to the variable: set it to or append an empty
1111 * string */
1112 save_emsg = did_emsg;
1113 did_emsg = FALSE;
1114 tv.v_type = VAR_STRING;
1115 tv.vval.v_string = (char_u *)"";
1116 if (append)
1117 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1118 else
1119 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001120 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001122 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 if (err)
1124 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001125 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 var_redir_stop();
1127 return FAIL;
1128 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129
1130 return OK;
1131}
1132
1133/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 * Append "value[value_len]" to the variable set by var_redir_start().
1135 * The actual appending is postponed until redirection ends, because the value
1136 * appended may in fact be the string we write to, changing it may cause freed
1137 * memory to be used:
1138 * :redir => foo
1139 * :let foo
1140 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 */
1142 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001143var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001145 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146
1147 if (redir_lval == NULL)
1148 return;
1149
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001150 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001151 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001153 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001155 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001156 {
1157 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001158 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001159 }
1160 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162}
1163
1164/*
1165 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001166 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 */
1168 void
1169var_redir_stop()
1170{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001171 typval_T tv;
1172
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173 if (redir_lval != NULL)
1174 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001175 /* If there was no error: assign the text to the variable. */
1176 if (redir_endp != NULL)
1177 {
1178 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1179 tv.v_type = VAR_STRING;
1180 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001181 /* Call get_lval() again, if it's inside a Dict or List it may
1182 * have changed. */
1183 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001184 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001185 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1186 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1187 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001188 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001190 /* free the collected output */
1191 vim_free(redir_ga.ga_data);
1192 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001193
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194 vim_free(redir_lval);
1195 redir_lval = NULL;
1196 }
1197 vim_free(redir_varname);
1198 redir_varname = NULL;
1199}
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201# if defined(FEAT_MBYTE) || defined(PROTO)
1202 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001203eval_charconvert(
1204 char_u *enc_from,
1205 char_u *enc_to,
1206 char_u *fname_from,
1207 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208{
1209 int err = FALSE;
1210
1211 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1212 set_vim_var_string(VV_CC_TO, enc_to, -1);
1213 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1214 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1215 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1216 err = TRUE;
1217 set_vim_var_string(VV_CC_FROM, NULL, -1);
1218 set_vim_var_string(VV_CC_TO, NULL, -1);
1219 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1220 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1221
1222 if (err)
1223 return FAIL;
1224 return OK;
1225}
1226# endif
1227
1228# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1229 int
1230eval_printexpr(fname, args)
1231 char_u *fname;
1232 char_u *args;
1233{
1234 int err = FALSE;
1235
1236 set_vim_var_string(VV_FNAME_IN, fname, -1);
1237 set_vim_var_string(VV_CMDARG, args, -1);
1238 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1239 err = TRUE;
1240 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1241 set_vim_var_string(VV_CMDARG, NULL, -1);
1242
1243 if (err)
1244 {
1245 mch_remove(fname);
1246 return FAIL;
1247 }
1248 return OK;
1249}
1250# endif
1251
1252# if defined(FEAT_DIFF) || defined(PROTO)
1253 void
1254eval_diff(origfile, newfile, outfile)
1255 char_u *origfile;
1256 char_u *newfile;
1257 char_u *outfile;
1258{
1259 int err = FALSE;
1260
1261 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1262 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1263 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1264 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1265 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1266 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1267 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1268}
1269
1270 void
1271eval_patch(origfile, difffile, outfile)
1272 char_u *origfile;
1273 char_u *difffile;
1274 char_u *outfile;
1275{
1276 int err;
1277
1278 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1279 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1280 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1281 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1282 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1283 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1284 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1285}
1286# endif
1287
1288/*
1289 * Top level evaluation function, returning a boolean.
1290 * Sets "error" to TRUE if there was an error.
1291 * Return TRUE or FALSE.
1292 */
1293 int
1294eval_to_bool(arg, error, nextcmd, skip)
1295 char_u *arg;
1296 int *error;
1297 char_u **nextcmd;
1298 int skip; /* only parse, don't execute */
1299{
Bram Moolenaar33570922005-01-25 22:26:29 +00001300 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 int retval = FALSE;
1302
1303 if (skip)
1304 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 else
1308 {
1309 *error = FALSE;
1310 if (!skip)
1311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001312 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315 }
1316 if (skip)
1317 --emsg_skip;
1318
1319 return retval;
1320}
1321
1322/*
1323 * Top level evaluation function, returning a string. If "skip" is TRUE,
1324 * only parsing to "nextcmd" is done, without reporting errors. Return
1325 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1326 */
1327 char_u *
1328eval_to_string_skip(arg, nextcmd, skip)
1329 char_u *arg;
1330 char_u **nextcmd;
1331 int skip; /* only parse, don't execute */
1332{
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 char_u *retval;
1335
1336 if (skip)
1337 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001338 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 retval = NULL;
1340 else
1341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001342 retval = vim_strsave(get_tv_string(&tv));
1343 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
1345 if (skip)
1346 --emsg_skip;
1347
1348 return retval;
1349}
1350
1351/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001352 * Skip over an expression at "*pp".
1353 * Return FAIL for an error, OK otherwise.
1354 */
1355 int
1356skip_expr(pp)
1357 char_u **pp;
1358{
Bram Moolenaar33570922005-01-25 22:26:29 +00001359 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001360
1361 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001362 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001363}
1364
1365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001367 * When "convert" is TRUE convert a List into a sequence of lines and convert
1368 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 * Return pointer to allocated memory, or NULL for failure.
1370 */
1371 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 char_u *arg;
1374 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001375 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
Bram Moolenaar33570922005-01-25 22:26:29 +00001377 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001379 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001380#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001381 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001384 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 retval = NULL;
1386 else
1387 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001388 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 {
1390 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001391 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001392 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001393 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001394 if (tv.vval.v_list->lv_len > 0)
1395 ga_append(&ga, NL);
1396 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001397 ga_append(&ga, NUL);
1398 retval = (char_u *)ga.ga_data;
1399 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001400#ifdef FEAT_FLOAT
1401 else if (convert && tv.v_type == VAR_FLOAT)
1402 {
1403 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1404 retval = vim_strsave(numbuf);
1405 }
1406#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001407 else
1408 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001409 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411
1412 return retval;
1413}
1414
1415/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001416 * Call eval_to_string() without using current local variables and using
1417 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 */
1419 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001420eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 char_u *arg;
1422 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001423 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
1425 char_u *retval;
1426 void *save_funccalp;
1427
1428 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001429 if (use_sandbox)
1430 ++sandbox;
1431 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001432 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001433 if (use_sandbox)
1434 --sandbox;
1435 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 restore_funccal(save_funccalp);
1437 return retval;
1438}
1439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440/*
1441 * Top level evaluation function, returning a number.
1442 * Evaluates "expr" silently.
1443 * Returns -1 for an error.
1444 */
1445 int
1446eval_to_number(expr)
1447 char_u *expr;
1448{
Bram Moolenaar33570922005-01-25 22:26:29 +00001449 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001451 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
1453 ++emsg_off;
1454
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001455 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 retval = -1;
1457 else
1458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001459 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001460 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
1462 --emsg_off;
1463
1464 return retval;
1465}
1466
Bram Moolenaara40058a2005-07-11 22:42:07 +00001467/*
1468 * Prepare v: variable "idx" to be used.
1469 * Save the current typeval in "save_tv".
1470 * When not used yet add the variable to the v: hashtable.
1471 */
1472 static void
1473prepare_vimvar(idx, save_tv)
1474 int idx;
1475 typval_T *save_tv;
1476{
1477 *save_tv = vimvars[idx].vv_tv;
1478 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1479 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1480}
1481
1482/*
1483 * Restore v: variable "idx" to typeval "save_tv".
1484 * When no longer defined, remove the variable from the v: hashtable.
1485 */
1486 static void
1487restore_vimvar(idx, save_tv)
1488 int idx;
1489 typval_T *save_tv;
1490{
1491 hashitem_T *hi;
1492
Bram Moolenaara40058a2005-07-11 22:42:07 +00001493 vimvars[idx].vv_tv = *save_tv;
1494 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1495 {
1496 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1497 if (HASHITEM_EMPTY(hi))
1498 EMSG2(_(e_intern2), "restore_vimvar()");
1499 else
1500 hash_remove(&vimvarht, hi);
1501 }
1502}
1503
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001504#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001505/*
1506 * Evaluate an expression to a list with suggestions.
1507 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001508 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001509 */
1510 list_T *
1511eval_spell_expr(badword, expr)
1512 char_u *badword;
1513 char_u *expr;
1514{
1515 typval_T save_val;
1516 typval_T rettv;
1517 list_T *list = NULL;
1518 char_u *p = skipwhite(expr);
1519
1520 /* Set "v:val" to the bad word. */
1521 prepare_vimvar(VV_VAL, &save_val);
1522 vimvars[VV_VAL].vv_type = VAR_STRING;
1523 vimvars[VV_VAL].vv_str = badword;
1524 if (p_verbose == 0)
1525 ++emsg_off;
1526
1527 if (eval1(&p, &rettv, TRUE) == OK)
1528 {
1529 if (rettv.v_type != VAR_LIST)
1530 clear_tv(&rettv);
1531 else
1532 list = rettv.vval.v_list;
1533 }
1534
1535 if (p_verbose == 0)
1536 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001537 restore_vimvar(VV_VAL, &save_val);
1538
1539 return list;
1540}
1541
1542/*
1543 * "list" is supposed to contain two items: a word and a number. Return the
1544 * word in "pp" and the number as the return value.
1545 * Return -1 if anything isn't right.
1546 * Used to get the good word and score from the eval_spell_expr() result.
1547 */
1548 int
1549get_spellword(list, pp)
1550 list_T *list;
1551 char_u **pp;
1552{
1553 listitem_T *li;
1554
1555 li = list->lv_first;
1556 if (li == NULL)
1557 return -1;
1558 *pp = get_tv_string(&li->li_tv);
1559
1560 li = li->li_next;
1561 if (li == NULL)
1562 return -1;
1563 return get_tv_number(&li->li_tv);
1564}
1565#endif
1566
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001567/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001568 * Top level evaluation function.
1569 * Returns an allocated typval_T with the result.
1570 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001571 */
1572 typval_T *
1573eval_expr(arg, nextcmd)
1574 char_u *arg;
1575 char_u **nextcmd;
1576{
1577 typval_T *tv;
1578
1579 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001580 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001581 {
1582 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001583 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001584 }
1585
1586 return tv;
1587}
1588
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001591 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001592 * Uses argv[argc] for the function arguments. Only Number and String
1593 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001594 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001596 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001597call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 char_u *func;
1599 int argc;
1600 char_u **argv;
1601 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001602 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604{
Bram Moolenaar33570922005-01-25 22:26:29 +00001605 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 long n;
1607 int len;
1608 int i;
1609 int doesrange;
1610 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001613 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617 for (i = 0; i < argc; i++)
1618 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001619 /* Pass a NULL or empty argument as an empty string */
1620 if (argv[i] == NULL || *argv[i] == NUL)
1621 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001622 argvars[i].v_type = VAR_STRING;
1623 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001624 continue;
1625 }
1626
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001627 if (str_arg_only)
1628 len = 0;
1629 else
1630 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001631 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 if (len != 0 && len == (int)STRLEN(argv[i]))
1633 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001634 argvars[i].v_type = VAR_NUMBER;
1635 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 }
1637 else
1638 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001639 argvars[i].v_type = VAR_STRING;
1640 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
1642 }
1643
1644 if (safe)
1645 {
1646 save_funccalp = save_funccal();
1647 ++sandbox;
1648 }
1649
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001650 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1651 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (safe)
1655 {
1656 --sandbox;
1657 restore_funccal(save_funccalp);
1658 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659 vim_free(argvars);
1660
1661 if (ret == FAIL)
1662 clear_tv(rettv);
1663
1664 return ret;
1665}
1666
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001667/*
1668 * Call vimL function "func" and return the result as a number.
1669 * Returns -1 when calling the function fails.
1670 * Uses argv[argc] for the function arguments.
1671 */
1672 long
1673call_func_retnr(func, argc, argv, safe)
1674 char_u *func;
1675 int argc;
1676 char_u **argv;
1677 int safe; /* use the sandbox */
1678{
1679 typval_T rettv;
1680 long retval;
1681
1682 /* All arguments are passed as strings, no conversion to number. */
1683 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1684 return -1;
1685
1686 retval = get_tv_number_chk(&rettv, NULL);
1687 clear_tv(&rettv);
1688 return retval;
1689}
1690
1691#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1692 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1693
Bram Moolenaar4f688582007-07-24 12:34:30 +00001694# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001696 * Call vimL function "func" and return the result as a string.
1697 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698 * Uses argv[argc] for the function arguments.
1699 */
1700 void *
1701call_func_retstr(func, argc, argv, safe)
1702 char_u *func;
1703 int argc;
1704 char_u **argv;
1705 int safe; /* use the sandbox */
1706{
1707 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001708 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001709
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001710 /* All arguments are passed as strings, no conversion to number. */
1711 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712 return NULL;
1713
1714 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001715 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 return retval;
1717}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001718# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001720/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001721 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001723 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 */
1725 void *
1726call_func_retlist(func, argc, argv, safe)
1727 char_u *func;
1728 int argc;
1729 char_u **argv;
1730 int safe; /* use the sandbox */
1731{
1732 typval_T rettv;
1733
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001734 /* All arguments are passed as strings, no conversion to number. */
1735 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736 return NULL;
1737
1738 if (rettv.v_type != VAR_LIST)
1739 {
1740 clear_tv(&rettv);
1741 return NULL;
1742 }
1743
1744 return rettv.vval.v_list;
1745}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#endif
1747
1748/*
1749 * Save the current function call pointer, and set it to NULL.
1750 * Used when executing autocommands and for ":source".
1751 */
1752 void *
1753save_funccal()
1754{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001755 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 current_funccal = NULL;
1758 return (void *)fc;
1759}
1760
1761 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001762restore_funccal(vfc)
1763 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001765 funccall_T *fc = (funccall_T *)vfc;
1766
1767 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768}
1769
Bram Moolenaar05159a02005-02-26 23:04:13 +00001770#if defined(FEAT_PROFILE) || defined(PROTO)
1771/*
1772 * Prepare profiling for entering a child or something else that is not
1773 * counted for the script/function itself.
1774 * Should always be called in pair with prof_child_exit().
1775 */
1776 void
1777prof_child_enter(tm)
1778 proftime_T *tm; /* place to store waittime */
1779{
1780 funccall_T *fc = current_funccal;
1781
1782 if (fc != NULL && fc->func->uf_profiling)
1783 profile_start(&fc->prof_child);
1784 script_prof_save(tm);
1785}
1786
1787/*
1788 * Take care of time spent in a child.
1789 * Should always be called after prof_child_enter().
1790 */
1791 void
1792prof_child_exit(tm)
1793 proftime_T *tm; /* where waittime was stored */
1794{
1795 funccall_T *fc = current_funccal;
1796
1797 if (fc != NULL && fc->func->uf_profiling)
1798 {
1799 profile_end(&fc->prof_child);
1800 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1801 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1802 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1803 }
1804 script_prof_restore(tm);
1805}
1806#endif
1807
1808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#ifdef FEAT_FOLDING
1810/*
1811 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1812 * it in "*cp". Doesn't give error messages.
1813 */
1814 int
1815eval_foldexpr(arg, cp)
1816 char_u *arg;
1817 int *cp;
1818{
Bram Moolenaar33570922005-01-25 22:26:29 +00001819 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 int retval;
1821 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001822 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1823 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001826 if (use_sandbox)
1827 ++sandbox;
1828 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 retval = 0;
1832 else
1833 {
1834 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 if (tv.v_type == VAR_NUMBER)
1836 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001837 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 retval = 0;
1839 else
1840 {
1841 /* If the result is a string, check if there is a non-digit before
1842 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 if (!VIM_ISDIGIT(*s) && *s != '-')
1845 *cp = *s++;
1846 retval = atol((char *)s);
1847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
1850 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001851 if (use_sandbox)
1852 --sandbox;
1853 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
1855 return retval;
1856}
1857#endif
1858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 * ":let" list all variable values
1861 * ":let var1 var2" list variable values
1862 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001863 * ":let var += expr" assignment command.
1864 * ":let var -= expr" assignment command.
1865 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 */
1868 void
1869ex_let(eap)
1870 exarg_T *eap;
1871{
1872 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001874 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 int var_count = 0;
1877 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001878 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001879 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001880 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881
Bram Moolenaardb552d602006-03-23 22:59:57 +00001882 argend = skip_var_list(arg, &var_count, &semicolon);
1883 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001885 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1886 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001887 expr = skipwhite(argend);
1888 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1889 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001891 /*
1892 * ":let" without "=": list variables
1893 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 if (*arg == '[')
1895 EMSG(_(e_invarg));
1896 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001898 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001902 list_glob_vars(&first);
1903 list_buf_vars(&first);
1904 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001905#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001907#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001908 list_script_vars(&first);
1909 list_func_vars(&first);
1910 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 eap->nextcmd = check_nextcmd(arg);
1913 }
1914 else
1915 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 op[0] = '=';
1917 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001918 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001919 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001920 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1921 op[0] = *expr; /* +=, -= or .= */
1922 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001924 else
1925 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (eap->skip)
1928 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (eap->skip)
1931 {
1932 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 --emsg_skip;
1935 }
1936 else if (i != FAIL)
1937 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
1942 }
1943}
1944
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945/*
1946 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1947 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 * When "nextchars" is not NULL it points to a string with characters that
1949 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1950 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 * Returns OK or FAIL;
1952 */
1953 static int
1954ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1955 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001956 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 int copy; /* copy values from "tv", don't move */
1958 int semicolon; /* from skip_var_list() */
1959 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961{
1962 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001963 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001965 listitem_T *item;
1966 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967
1968 if (*arg != '[')
1969 {
1970 /*
1971 * ":let var = expr" or ":for var in list"
1972 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 return FAIL;
1975 return OK;
1976 }
1977
1978 /*
1979 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1980 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001981 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 {
1983 EMSG(_(e_listreq));
1984 return FAIL;
1985 }
1986
1987 i = list_len(l);
1988 if (semicolon == 0 && var_count < i)
1989 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001990 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 return FAIL;
1992 }
1993 if (var_count - semicolon > i)
1994 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001995 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 return FAIL;
1997 }
1998
1999 item = l->lv_first;
2000 while (*arg != ']')
2001 {
2002 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002003 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 item = item->li_next;
2005 if (arg == NULL)
2006 return FAIL;
2007
2008 arg = skipwhite(arg);
2009 if (*arg == ';')
2010 {
2011 /* Put the rest of the list (may be empty) in the var after ';'.
2012 * Create a new list for this. */
2013 l = list_alloc();
2014 if (l == NULL)
2015 return FAIL;
2016 while (item != NULL)
2017 {
2018 list_append_tv(l, &item->li_tv);
2019 item = item->li_next;
2020 }
2021
2022 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002023 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 ltv.vval.v_list = l;
2025 l->lv_refcount = 1;
2026
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002027 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2028 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 clear_tv(&ltv);
2030 if (arg == NULL)
2031 return FAIL;
2032 break;
2033 }
2034 else if (*arg != ',' && *arg != ']')
2035 {
2036 EMSG2(_(e_intern2), "ex_let_vars()");
2037 return FAIL;
2038 }
2039 }
2040
2041 return OK;
2042}
2043
2044/*
2045 * Skip over assignable variable "var" or list of variables "[var, var]".
2046 * Used for ":let varvar = expr" and ":for varvar in expr".
2047 * For "[var, var]" increment "*var_count" for each variable.
2048 * for "[var, var; var]" set "semicolon".
2049 * Return NULL for an error.
2050 */
2051 static char_u *
2052skip_var_list(arg, var_count, semicolon)
2053 char_u *arg;
2054 int *var_count;
2055 int *semicolon;
2056{
2057 char_u *p, *s;
2058
2059 if (*arg == '[')
2060 {
2061 /* "[var, var]": find the matching ']'. */
2062 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002063 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064 {
2065 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2066 s = skip_var_one(p);
2067 if (s == p)
2068 {
2069 EMSG2(_(e_invarg2), p);
2070 return NULL;
2071 }
2072 ++*var_count;
2073
2074 p = skipwhite(s);
2075 if (*p == ']')
2076 break;
2077 else if (*p == ';')
2078 {
2079 if (*semicolon == 1)
2080 {
2081 EMSG(_("Double ; in list of variables"));
2082 return NULL;
2083 }
2084 *semicolon = 1;
2085 }
2086 else if (*p != ',')
2087 {
2088 EMSG2(_(e_invarg2), p);
2089 return NULL;
2090 }
2091 }
2092 return p + 1;
2093 }
2094 else
2095 return skip_var_one(arg);
2096}
2097
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002098/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002099 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002100 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002101 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102 static char_u *
2103skip_var_one(arg)
2104 char_u *arg;
2105{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002106 if (*arg == '@' && arg[1] != NUL)
2107 return arg + 2;
2108 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2109 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002110}
2111
Bram Moolenaara7043832005-01-21 11:56:39 +00002112/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002113 * List variables for hashtab "ht" with prefix "prefix".
2114 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002115 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002116 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002118 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002120 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002121 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122{
Bram Moolenaar33570922005-01-25 22:26:29 +00002123 hashitem_T *hi;
2124 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125 int todo;
2126
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002127 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002128 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2129 {
2130 if (!HASHITEM_EMPTY(hi))
2131 {
2132 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002133 di = HI2DI(hi);
2134 if (empty || di->di_tv.v_type != VAR_STRING
2135 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002137 }
2138 }
2139}
2140
2141/*
2142 * List global variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_glob_vars(first)
2146 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002147{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002149}
2150
2151/*
2152 * List buffer variables.
2153 */
2154 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155list_buf_vars(first)
2156 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002157{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158 char_u numbuf[NUMBUFLEN];
2159
Bram Moolenaar429fa852013-04-15 12:27:36 +02002160 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002162
2163 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2165 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002166}
2167
2168/*
2169 * List window variables.
2170 */
2171 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172list_win_vars(first)
2173 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002174{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002175 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002177}
2178
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179#ifdef FEAT_WINDOWS
2180/*
2181 * List tab page variables.
2182 */
2183 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184list_tab_vars(first)
2185 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002186{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002187 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002189}
2190#endif
2191
Bram Moolenaara7043832005-01-21 11:56:39 +00002192/*
2193 * List Vim variables.
2194 */
2195 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196list_vim_vars(first)
2197 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002199 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200}
2201
2202/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002203 * List script-local variables, if there is a script.
2204 */
2205 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206list_script_vars(first)
2207 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208{
2209 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2211 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002212}
2213
2214/*
2215 * List function variables, if there is a function.
2216 */
2217 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218list_func_vars(first)
2219 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220{
2221 if (current_funccal != NULL)
2222 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002224}
2225
2226/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 * List variables in "arg".
2228 */
2229 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 exarg_T *eap;
2232 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002233 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234{
2235 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 char_u *name_start;
2239 char_u *arg_subsc;
2240 char_u *tofree;
2241 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242
2243 while (!ends_excmd(*arg) && !got_int)
2244 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002247 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2249 {
2250 emsg_severe = TRUE;
2251 EMSG(_(e_trailing));
2252 break;
2253 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 /* get_name_len() takes care of expanding curly braces */
2258 name_start = name = arg;
2259 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2260 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 /* This is mainly to keep test 49 working: when expanding
2263 * curly braces fails overrule the exception error message. */
2264 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 emsg_severe = TRUE;
2267 EMSG2(_(e_invarg2), arg);
2268 break;
2269 }
2270 error = TRUE;
2271 }
2272 else
2273 {
2274 if (tofree != NULL)
2275 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002276 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 else
2279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 /* handle d.key, l[idx], f(expr) */
2281 arg_subsc = arg;
2282 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002290 case 'g': list_glob_vars(first); break;
2291 case 'b': list_buf_vars(first); break;
2292 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002293#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002294 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002295#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002296 case 'v': list_vim_vars(first); break;
2297 case 's': list_script_vars(first); break;
2298 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 default:
2300 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002301 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002302 }
2303 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 {
2305 char_u numbuf[NUMBUFLEN];
2306 char_u *tf;
2307 int c;
2308 char_u *s;
2309
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002310 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311 c = *arg;
2312 *arg = NUL;
2313 list_one_var_a((char_u *)"",
2314 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002315 tv.v_type,
2316 s == NULL ? (char_u *)"" : s,
2317 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318 *arg = c;
2319 vim_free(tf);
2320 }
2321 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002322 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 }
2324 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325
2326 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328
2329 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 }
2331
2332 return arg;
2333}
2334
2335/*
2336 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2337 * Returns a pointer to the char just after the var name.
2338 * Returns NULL if there is an error.
2339 */
2340 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002343 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002345 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347{
2348 int c1;
2349 char_u *name;
2350 char_u *p;
2351 char_u *arg_end = NULL;
2352 int len;
2353 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355
2356 /*
2357 * ":let $VAR = expr": Set environment variable.
2358 */
2359 if (*arg == '$')
2360 {
2361 /* Find the end of the name. */
2362 ++arg;
2363 name = arg;
2364 len = get_env_len(&arg);
2365 if (len == 0)
2366 EMSG2(_(e_invarg2), name - 1);
2367 else
2368 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002369 if (op != NULL && (*op == '+' || *op == '-'))
2370 EMSG2(_(e_letwrong), op);
2371 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002372 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002374 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 {
2376 c1 = name[len];
2377 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 p = get_tv_string_chk(tv);
2379 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 {
2381 int mustfree = FALSE;
2382 char_u *s = vim_getenv(name, &mustfree);
2383
2384 if (s != NULL)
2385 {
2386 p = tofree = concat_str(s, p);
2387 if (mustfree)
2388 vim_free(s);
2389 }
2390 }
2391 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 if (STRICMP(name, "HOME") == 0)
2395 init_homedir();
2396 else if (didset_vim && STRICMP(name, "VIM") == 0)
2397 didset_vim = FALSE;
2398 else if (didset_vimruntime
2399 && STRICMP(name, "VIMRUNTIME") == 0)
2400 didset_vimruntime = FALSE;
2401 arg_end = arg;
2402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002405 }
2406 }
2407 }
2408
2409 /*
2410 * ":let &option = expr": Set option value.
2411 * ":let &l:option = expr": Set local option value.
2412 * ":let &g:option = expr": Set global option value.
2413 */
2414 else if (*arg == '&')
2415 {
2416 /* Find the end of the name. */
2417 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002418 if (p == NULL || (endchars != NULL
2419 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 EMSG(_(e_letunexp));
2421 else
2422 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 long n;
2424 int opt_type;
2425 long numval;
2426 char_u *stringval = NULL;
2427 char_u *s;
2428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 c1 = *p;
2430 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431
2432 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002433 s = get_tv_string_chk(tv); /* != NULL if number or string */
2434 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 {
2436 opt_type = get_option_value(arg, &numval,
2437 &stringval, opt_flags);
2438 if ((opt_type == 1 && *op == '.')
2439 || (opt_type == 0 && *op != '.'))
2440 EMSG2(_(e_letwrong), op);
2441 else
2442 {
2443 if (opt_type == 1) /* number */
2444 {
2445 if (*op == '+')
2446 n = numval + n;
2447 else
2448 n = numval - n;
2449 }
2450 else if (opt_type == 0 && stringval != NULL) /* string */
2451 {
2452 s = concat_str(stringval, s);
2453 vim_free(stringval);
2454 stringval = s;
2455 }
2456 }
2457 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 if (s != NULL)
2459 {
2460 set_option_value(arg, n, s, opt_flags);
2461 arg_end = p;
2462 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 }
2466 }
2467
2468 /*
2469 * ":let @r = expr": Set register contents.
2470 */
2471 else if (*arg == '@')
2472 {
2473 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 if (op != NULL && (*op == '+' || *op == '-'))
2475 EMSG2(_(e_letwrong), op);
2476 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002477 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 EMSG(_(e_letunexp));
2479 else
2480 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002481 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 char_u *s;
2483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 p = get_tv_string_chk(tv);
2485 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002487 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 if (s != NULL)
2489 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002490 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 vim_free(s);
2492 }
2493 }
2494 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002497 arg_end = arg + 1;
2498 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002499 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 }
2501 }
2502
2503 /*
2504 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002507 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002509 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002511 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2515 EMSG(_(e_letunexp));
2516 else
2517 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002518 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 arg_end = p;
2520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 }
2524
2525 else
2526 EMSG2(_(e_invarg2), arg);
2527
2528 return arg_end;
2529}
2530
2531/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2533 */
2534 static int
2535check_changedtick(arg)
2536 char_u *arg;
2537{
2538 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2539 {
2540 EMSG2(_(e_readonlyvar), arg);
2541 return TRUE;
2542 }
2543 return FALSE;
2544}
2545
2546/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 * Get an lval: variable, Dict item or List item that can be assigned a value
2548 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2549 * "name.key", "name.key[expr]" etc.
2550 * Indexing only works if "name" is an existing List or Dictionary.
2551 * "name" points to the start of the name.
2552 * If "rettv" is not NULL it points to the value to be assigned.
2553 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2554 * wrong; must end in space or cmd separator.
2555 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002556 * flags:
2557 * GLV_QUIET: do not give error messages
2558 * GLV_NO_AUTOLOAD: do not use script autoloading
2559 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002561 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 */
2564 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002565get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 typval_T *rettv;
2568 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 int unlet;
2570 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002571 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002572 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 char_u *expr_start, *expr_end;
2576 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 dictitem_T *v;
2578 typval_T var1;
2579 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002581 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002585 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589
2590 if (skip)
2591 {
2592 /* When skipping just find the end of the name. */
2593 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002594 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 }
2596
2597 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002598 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (expr_start != NULL)
2600 {
2601 /* Don't expand the name when we already know there is an error. */
2602 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2603 && *p != '[' && *p != '.')
2604 {
2605 EMSG(_(e_trailing));
2606 return NULL;
2607 }
2608
2609 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2610 if (lp->ll_exp_name == NULL)
2611 {
2612 /* Report an invalid expression in braces, unless the
2613 * expression evaluation has been cancelled due to an
2614 * aborting error, an interrupt, or an exception. */
2615 if (!aborting() && !quiet)
2616 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002617 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 EMSG2(_(e_invarg2), name);
2619 return NULL;
2620 }
2621 }
2622 lp->ll_name = lp->ll_exp_name;
2623 }
2624 else
2625 lp->ll_name = name;
2626
2627 /* Without [idx] or .key we are done. */
2628 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2629 return p;
2630
2631 cc = *p;
2632 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002633 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (v == NULL && !quiet)
2635 EMSG2(_(e_undefvar), lp->ll_name);
2636 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 if (v == NULL)
2638 return NULL;
2639
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 /*
2641 * Loop until no more [idx] or .key is following.
2642 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002643 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2647 && !(lp->ll_tv->v_type == VAR_DICT
2648 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (!quiet)
2651 EMSG(_("E689: Can only index a List or Dictionary"));
2652 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!quiet)
2657 EMSG(_("E708: [:] must come last"));
2658 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 len = -1;
2662 if (*p == '.')
2663 {
2664 key = p + 1;
2665 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2666 ;
2667 if (len == 0)
2668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 if (!quiet)
2670 EMSG(_(e_emptykey));
2671 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 }
2673 p = key + len;
2674 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675 else
2676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (*p == ':')
2680 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 else
2682 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 empty1 = FALSE;
2684 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002686 if (get_tv_string_chk(&var1) == NULL)
2687 {
2688 /* not a number or string */
2689 clear_tv(&var1);
2690 return NULL;
2691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 }
2693
2694 /* Optionally get the second index [ :expr]. */
2695 if (*p == ':')
2696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002700 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701 if (!empty1)
2702 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (rettv != NULL && (rettv->v_type != VAR_LIST
2706 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!quiet)
2709 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 if (!empty1)
2711 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 }
2714 p = skipwhite(p + 1);
2715 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 else
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 if (!empty1)
2723 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002726 if (get_tv_string_chk(&var2) == NULL)
2727 {
2728 /* not a number or string */
2729 if (!empty1)
2730 clear_tv(&var1);
2731 clear_tv(&var2);
2732 return NULL;
2733 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (!quiet)
2743 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 if (!empty1)
2745 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 }
2750
2751 /* Skip to past ']'. */
2752 ++p;
2753 }
2754
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 {
2757 if (len == -1)
2758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002760 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 if (*key == NUL)
2762 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (!quiet)
2764 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 }
2768 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002769 lp->ll_list = NULL;
2770 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002771 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002772
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002773 /* When assigning to a scope dictionary check that a function and
2774 * variable name is valid (only variable name unless it is l: or
2775 * g: dictionary). Disallow overwriting a builtin function. */
2776 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002778 int prevval;
2779 int wrong;
2780
2781 if (len != -1)
2782 {
2783 prevval = key[len];
2784 key[len] = NUL;
2785 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002786 else
2787 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002788 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2789 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002790 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002791 || !valid_varname(key);
2792 if (len != -1)
2793 key[len] = prevval;
2794 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002795 return NULL;
2796 }
2797
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 /* Can't add "v:" variable. */
2801 if (lp->ll_dict == &vimvardict)
2802 {
2803 EMSG2(_(e_illvar), name);
2804 return NULL;
2805 }
2806
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002807 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002811 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 if (len == -1)
2813 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 }
2816 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 if (len == -1)
2821 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 p = NULL;
2824 break;
2825 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002826 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002827 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002828 return NULL;
2829
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 if (len == -1)
2831 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 }
2834 else
2835 {
2836 /*
2837 * Get the number and item for the only or first index of the List.
2838 */
2839 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 else
2842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002843 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 clear_tv(&var1);
2845 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002846 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 lp->ll_list = lp->ll_tv->vval.v_list;
2848 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2849 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002851 if (lp->ll_n1 < 0)
2852 {
2853 lp->ll_n1 = 0;
2854 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2855 }
2856 }
2857 if (lp->ll_li == NULL)
2858 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002861 if (!quiet)
2862 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 }
2865
2866 /*
2867 * May need to find the item or absolute index for the second
2868 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 * When no index given: "lp->ll_empty2" is TRUE.
2870 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002874 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 {
2881 if (!quiet)
2882 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002884 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 }
2887
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2889 if (lp->ll_n1 < 0)
2890 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2891 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002892 {
2893 if (!quiet)
2894 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002896 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002897 }
2898
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002901 }
2902
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 return p;
2904}
2905
2906/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002907 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 */
2909 static void
2910clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002911 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912{
2913 vim_free(lp->ll_exp_name);
2914 vim_free(lp->ll_newkey);
2915}
2916
2917/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002918 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 */
2922 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002923set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002928 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929{
2930 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002931 listitem_T *ri;
2932 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933
2934 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 cc = *endp;
2939 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940 if (op != NULL && *op != '=')
2941 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002942 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002943
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002944 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002945 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002946 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002947 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002949 if ((di == NULL
2950 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2951 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2952 FALSE)))
2953 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 set_var(lp->ll_name, &tv, FALSE);
2955 clear_tv(&tv);
2956 }
2957 }
2958 else
2959 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002963 else if (tv_check_lock(lp->ll_newkey == NULL
2964 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002965 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002966 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 else if (lp->ll_range)
2968 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002969 listitem_T *ll_li = lp->ll_li;
2970 int ll_n1 = lp->ll_n1;
2971
2972 /*
2973 * Check whether any of the list items is locked
2974 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002975 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002976 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002977 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002978 return;
2979 ri = ri->li_next;
2980 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2981 break;
2982 ll_li = ll_li->li_next;
2983 ++ll_n1;
2984 }
2985
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986 /*
2987 * Assign the List values to the list items.
2988 */
2989 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002990 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002991 if (op != NULL && *op != '=')
2992 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2993 else
2994 {
2995 clear_tv(&lp->ll_li->li_tv);
2996 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2997 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 ri = ri->li_next;
2999 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3000 break;
3001 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003004 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 ri = NULL;
3007 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003008 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 lp->ll_li = lp->ll_li->li_next;
3011 ++lp->ll_n1;
3012 }
3013 if (ri != NULL)
3014 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 else if (lp->ll_empty2
3016 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 : lp->ll_n1 != lp->ll_n2)
3018 EMSG(_("E711: List value has not enough items"));
3019 }
3020 else
3021 {
3022 /*
3023 * Assign to a List or Dictionary item.
3024 */
3025 if (lp->ll_newkey != NULL)
3026 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 if (op != NULL && *op != '=')
3028 {
3029 EMSG2(_(e_letwrong), op);
3030 return;
3031 }
3032
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003033 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003034 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003035 if (di == NULL)
3036 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003037 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3038 {
3039 vim_free(di);
3040 return;
3041 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003043 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 else if (op != NULL && *op != '=')
3045 {
3046 tv_op(lp->ll_tv, rettv, op);
3047 return;
3048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003049 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003051
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 /*
3053 * Assign the value to the variable or list item.
3054 */
3055 if (copy)
3056 copy_tv(rettv, lp->ll_tv);
3057 else
3058 {
3059 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003060 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003061 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003062 }
3063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003064}
3065
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003066/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003067 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3068 * Returns OK or FAIL.
3069 */
3070 static int
3071tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003072 typval_T *tv1;
3073 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 char_u *op;
3075{
3076 long n;
3077 char_u numbuf[NUMBUFLEN];
3078 char_u *s;
3079
3080 /* Can't do anything with a Funcref or a Dict on the right. */
3081 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3082 {
3083 switch (tv1->v_type)
3084 {
3085 case VAR_DICT:
3086 case VAR_FUNC:
3087 break;
3088
3089 case VAR_LIST:
3090 if (*op != '+' || tv2->v_type != VAR_LIST)
3091 break;
3092 /* List += List */
3093 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3094 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3095 return OK;
3096
3097 case VAR_NUMBER:
3098 case VAR_STRING:
3099 if (tv2->v_type == VAR_LIST)
3100 break;
3101 if (*op == '+' || *op == '-')
3102 {
3103 /* nr += nr or nr -= nr*/
3104 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003105#ifdef FEAT_FLOAT
3106 if (tv2->v_type == VAR_FLOAT)
3107 {
3108 float_T f = n;
3109
3110 if (*op == '+')
3111 f += tv2->vval.v_float;
3112 else
3113 f -= tv2->vval.v_float;
3114 clear_tv(tv1);
3115 tv1->v_type = VAR_FLOAT;
3116 tv1->vval.v_float = f;
3117 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003118 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003119#endif
3120 {
3121 if (*op == '+')
3122 n += get_tv_number(tv2);
3123 else
3124 n -= get_tv_number(tv2);
3125 clear_tv(tv1);
3126 tv1->v_type = VAR_NUMBER;
3127 tv1->vval.v_number = n;
3128 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129 }
3130 else
3131 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003132 if (tv2->v_type == VAR_FLOAT)
3133 break;
3134
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 /* str .= str */
3136 s = get_tv_string(tv1);
3137 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3138 clear_tv(tv1);
3139 tv1->v_type = VAR_STRING;
3140 tv1->vval.v_string = s;
3141 }
3142 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143
3144#ifdef FEAT_FLOAT
3145 case VAR_FLOAT:
3146 {
3147 float_T f;
3148
3149 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3150 && tv2->v_type != VAR_NUMBER
3151 && tv2->v_type != VAR_STRING))
3152 break;
3153 if (tv2->v_type == VAR_FLOAT)
3154 f = tv2->vval.v_float;
3155 else
3156 f = get_tv_number(tv2);
3157 if (*op == '+')
3158 tv1->vval.v_float += f;
3159 else
3160 tv1->vval.v_float -= f;
3161 }
3162 return OK;
3163#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003164 }
3165 }
3166
3167 EMSG2(_(e_letwrong), op);
3168 return FAIL;
3169}
3170
3171/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172 * Add a watcher to a list.
3173 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003174 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 list_T *l;
3177 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178{
3179 lw->lw_next = l->lv_watch;
3180 l->lv_watch = lw;
3181}
3182
3183/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003184 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185 * No warning when it isn't found...
3186 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003187 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003189 list_T *l;
3190 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191{
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193
3194 lwp = &l->lv_watch;
3195 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3196 {
3197 if (lw == lwrem)
3198 {
3199 *lwp = lw->lw_next;
3200 break;
3201 }
3202 lwp = &lw->lw_next;
3203 }
3204}
3205
3206/*
3207 * Just before removing an item from a list: advance watchers to the next
3208 * item.
3209 */
3210 static void
3211list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003212 list_T *l;
3213 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214{
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216
3217 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3218 if (lw->lw_item == item)
3219 lw->lw_item = item->li_next;
3220}
3221
3222/*
3223 * Evaluate the expression used in a ":for var in expr" command.
3224 * "arg" points to "var".
3225 * Set "*errp" to TRUE for an error, FALSE otherwise;
3226 * Return a pointer that holds the info. Null when there is an error.
3227 */
3228 void *
3229eval_for_line(arg, errp, nextcmdp, skip)
3230 char_u *arg;
3231 int *errp;
3232 char_u **nextcmdp;
3233 int skip;
3234{
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 typval_T tv;
3238 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239
3240 *errp = TRUE; /* default: there is an error */
3241
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 if (fi == NULL)
3244 return NULL;
3245
3246 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3247 if (expr == NULL)
3248 return fi;
3249
3250 expr = skipwhite(expr);
3251 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3252 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003253 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 return fi;
3255 }
3256
3257 if (skip)
3258 ++emsg_skip;
3259 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3260 {
3261 *errp = FALSE;
3262 if (!skip)
3263 {
3264 l = tv.vval.v_list;
3265 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 clear_tv(&tv);
3269 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 else
3271 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003272 /* No need to increment the refcount, it's already set for the
3273 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 fi->fi_list = l;
3275 list_add_watch(l, &fi->fi_lw);
3276 fi->fi_lw.lw_item = l->lv_first;
3277 }
3278 }
3279 }
3280 if (skip)
3281 --emsg_skip;
3282
3283 return fi;
3284}
3285
3286/*
3287 * Use the first item in a ":for" list. Advance to the next.
3288 * Assign the values to the variable (list). "arg" points to the first one.
3289 * Return TRUE when a valid item was found, FALSE when at end of list or
3290 * something wrong.
3291 */
3292 int
3293next_for_item(fi_void, arg)
3294 void *fi_void;
3295 char_u *arg;
3296{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003297 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300
3301 item = fi->fi_lw.lw_item;
3302 if (item == NULL)
3303 result = FALSE;
3304 else
3305 {
3306 fi->fi_lw.lw_item = item->li_next;
3307 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3308 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3309 }
3310 return result;
3311}
3312
3313/*
3314 * Free the structure used to store info used by ":for".
3315 */
3316 void
3317free_for_info(fi_void)
3318 void *fi_void;
3319{
Bram Moolenaar33570922005-01-25 22:26:29 +00003320 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003322 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003323 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003325 list_unref(fi->fi_list);
3326 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 vim_free(fi);
3328}
3329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3331
3332 void
3333set_context_for_expression(xp, arg, cmdidx)
3334 expand_T *xp;
3335 char_u *arg;
3336 cmdidx_T cmdidx;
3337{
3338 int got_eq = FALSE;
3339 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 if (cmdidx == CMD_let)
3343 {
3344 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003345 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 {
3347 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003348 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 {
3350 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003351 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 if (vim_iswhite(*p))
3353 break;
3354 }
3355 return;
3356 }
3357 }
3358 else
3359 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3360 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 while ((xp->xp_pattern = vim_strpbrk(arg,
3362 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3363 {
3364 c = *xp->xp_pattern;
3365 if (c == '&')
3366 {
3367 c = xp->xp_pattern[1];
3368 if (c == '&')
3369 {
3370 ++xp->xp_pattern;
3371 xp->xp_context = cmdidx != CMD_let || got_eq
3372 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3373 }
3374 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003377 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3378 xp->xp_pattern += 2;
3379
3380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
3382 else if (c == '$')
3383 {
3384 /* environment variable */
3385 xp->xp_context = EXPAND_ENV_VARS;
3386 }
3387 else if (c == '=')
3388 {
3389 got_eq = TRUE;
3390 xp->xp_context = EXPAND_EXPRESSION;
3391 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003392 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 && xp->xp_context == EXPAND_FUNCTIONS
3394 && vim_strchr(xp->xp_pattern, '(') == NULL)
3395 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003396 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 break;
3398 }
3399 else if (cmdidx != CMD_let || got_eq)
3400 {
3401 if (c == '"') /* string */
3402 {
3403 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3404 if (c == '\\' && xp->xp_pattern[1] != NUL)
3405 ++xp->xp_pattern;
3406 xp->xp_context = EXPAND_NOTHING;
3407 }
3408 else if (c == '\'') /* literal string */
3409 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003410 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3412 /* skip */ ;
3413 xp->xp_context = EXPAND_NOTHING;
3414 }
3415 else if (c == '|')
3416 {
3417 if (xp->xp_pattern[1] == '|')
3418 {
3419 ++xp->xp_pattern;
3420 xp->xp_context = EXPAND_EXPRESSION;
3421 }
3422 else
3423 xp->xp_context = EXPAND_COMMANDS;
3424 }
3425 else
3426 xp->xp_context = EXPAND_EXPRESSION;
3427 }
3428 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003429 /* Doesn't look like something valid, expand as an expression
3430 * anyway. */
3431 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 arg = xp->xp_pattern;
3433 if (*arg != NUL)
3434 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3435 /* skip */ ;
3436 }
3437 xp->xp_pattern = arg;
3438}
3439
3440#endif /* FEAT_CMDL_COMPL */
3441
3442/*
3443 * ":1,25call func(arg1, arg2)" function call.
3444 */
3445 void
3446ex_call(eap)
3447 exarg_T *eap;
3448{
3449 char_u *arg = eap->arg;
3450 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003452 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003454 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 linenr_T lnum;
3456 int doesrange;
3457 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003458 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003460 if (eap->skip)
3461 {
3462 /* trans_function_name() doesn't work well when skipping, use eval0()
3463 * instead to skip to any following command, e.g. for:
3464 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003465 ++emsg_skip;
3466 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3467 clear_tv(&rettv);
3468 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003469 return;
3470 }
3471
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003472 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003473 if (fudi.fd_newkey != NULL)
3474 {
3475 /* Still need to give an error message for missing key. */
3476 EMSG2(_(e_dictkey), fudi.fd_newkey);
3477 vim_free(fudi.fd_newkey);
3478 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003479 if (tofree == NULL)
3480 return;
3481
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003482 /* Increase refcount on dictionary, it could get deleted when evaluating
3483 * the arguments. */
3484 if (fudi.fd_dict != NULL)
3485 ++fudi.fd_dict->dv_refcount;
3486
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003487 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003488 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003489 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
Bram Moolenaar532c7802005-01-27 14:44:31 +00003491 /* Skip white space to allow ":call func ()". Not good, but required for
3492 * backward compatibility. */
3493 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003494 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496 if (*startarg != '(')
3497 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003498 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 goto end;
3500 }
3501
3502 /*
3503 * When skipping, evaluate the function once, to find the end of the
3504 * arguments.
3505 * When the function takes a range, this is discovered after the first
3506 * call, and the loop is broken.
3507 */
3508 if (eap->skip)
3509 {
3510 ++emsg_skip;
3511 lnum = eap->line2; /* do it once, also with an invalid range */
3512 }
3513 else
3514 lnum = eap->line1;
3515 for ( ; lnum <= eap->line2; ++lnum)
3516 {
3517 if (!eap->skip && eap->addr_count > 0)
3518 {
3519 curwin->w_cursor.lnum = lnum;
3520 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003521#ifdef FEAT_VIRTUALEDIT
3522 curwin->w_cursor.coladd = 0;
3523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
3525 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003526 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003527 eap->line1, eap->line2, &doesrange,
3528 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
3530 failed = TRUE;
3531 break;
3532 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003533
3534 /* Handle a function returning a Funcref, Dictionary or List. */
3535 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3536 {
3537 failed = TRUE;
3538 break;
3539 }
3540
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003541 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (doesrange || eap->skip)
3543 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003546 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003547 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003548 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 if (aborting())
3550 break;
3551 }
3552 if (eap->skip)
3553 --emsg_skip;
3554
3555 if (!failed)
3556 {
3557 /* Check for trailing illegal characters and a following command. */
3558 if (!ends_excmd(*arg))
3559 {
3560 emsg_severe = TRUE;
3561 EMSG(_(e_trailing));
3562 }
3563 else
3564 eap->nextcmd = check_nextcmd(arg);
3565 }
3566
3567end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003568 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003569 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570}
3571
3572/*
3573 * ":unlet[!] var1 ... " command.
3574 */
3575 void
3576ex_unlet(eap)
3577 exarg_T *eap;
3578{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003579 ex_unletlock(eap, eap->arg, 0);
3580}
3581
3582/*
3583 * ":lockvar" and ":unlockvar" commands
3584 */
3585 void
3586ex_lockvar(eap)
3587 exarg_T *eap;
3588{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 int deep = 2;
3591
3592 if (eap->forceit)
3593 deep = -1;
3594 else if (vim_isdigit(*arg))
3595 {
3596 deep = getdigits(&arg);
3597 arg = skipwhite(arg);
3598 }
3599
3600 ex_unletlock(eap, arg, deep);
3601}
3602
3603/*
3604 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3605 */
3606 static void
3607ex_unletlock(eap, argstart, deep)
3608 exarg_T *eap;
3609 char_u *argstart;
3610 int deep;
3611{
3612 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003615 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
3617 do
3618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003620 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003621 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 if (lv.ll_name == NULL)
3623 error = TRUE; /* error but continue parsing */
3624 if (name_end == NULL || (!vim_iswhite(*name_end)
3625 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 if (name_end != NULL)
3628 {
3629 emsg_severe = TRUE;
3630 EMSG(_(e_trailing));
3631 }
3632 if (!(eap->skip || error))
3633 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 break;
3635 }
3636
3637 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 {
3639 if (eap->cmdidx == CMD_unlet)
3640 {
3641 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3642 error = TRUE;
3643 }
3644 else
3645 {
3646 if (do_lock_var(&lv, name_end, deep,
3647 eap->cmdidx == CMD_lockvar) == FAIL)
3648 error = TRUE;
3649 }
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 if (!eap->skip)
3653 clear_lval(&lv);
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 arg = skipwhite(name_end);
3656 } while (!ends_excmd(*arg));
3657
3658 eap->nextcmd = check_nextcmd(arg);
3659}
3660
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003663 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 int forceit;
3666{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 int ret = OK;
3668 int cc;
3669
3670 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 cc = *name_end;
3673 *name_end = NUL;
3674
3675 /* Normal name or expanded name. */
3676 if (check_changedtick(lp->ll_name))
3677 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003682 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003683 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003684 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003685 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 else if (lp->ll_range)
3688 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003689 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003690 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003691 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003692
3693 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3694 {
3695 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003696 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003697 return FAIL;
3698 ll_li = li;
3699 ++ll_n1;
3700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701
3702 /* Delete a range of List items. */
3703 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3704 {
3705 li = lp->ll_li->li_next;
3706 listitem_remove(lp->ll_list, lp->ll_li);
3707 lp->ll_li = li;
3708 ++lp->ll_n1;
3709 }
3710 }
3711 else
3712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 /* unlet a List item. */
3715 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003718 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 }
3720
3721 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003722}
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724/*
3725 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
3728 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003729do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003731 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732{
Bram Moolenaar33570922005-01-25 22:26:29 +00003733 hashtab_T *ht;
3734 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003735 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003736 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003737 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
Bram Moolenaar33570922005-01-25 22:26:29 +00003739 ht = find_var_ht(name, &varname);
3740 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003742 if (ht == &globvarht)
3743 d = &globvardict;
3744 else if (current_funccal != NULL
3745 && ht == &current_funccal->l_vars.dv_hashtab)
3746 d = &current_funccal->l_vars;
3747 else if (ht == &compat_hashtab)
3748 d = &vimvardict;
3749 else
3750 {
3751 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3752 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3753 }
3754 if (d == NULL)
3755 {
3756 EMSG2(_(e_intern2), "do_unlet()");
3757 return FAIL;
3758 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003759 hi = hash_find(ht, varname);
3760 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003761 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003762 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003763 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003764 || var_check_ro(di->di_flags, name, FALSE)
3765 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003766 return FAIL;
3767
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003768 delete_var(ht, hi);
3769 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003772 if (forceit)
3773 return OK;
3774 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 return FAIL;
3776}
3777
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003778/*
3779 * Lock or unlock variable indicated by "lp".
3780 * "deep" is the levels to go (-1 for unlimited);
3781 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3782 */
3783 static int
3784do_lock_var(lp, name_end, deep, lock)
3785 lval_T *lp;
3786 char_u *name_end;
3787 int deep;
3788 int lock;
3789{
3790 int ret = OK;
3791 int cc;
3792 dictitem_T *di;
3793
3794 if (deep == 0) /* nothing to do */
3795 return OK;
3796
3797 if (lp->ll_tv == NULL)
3798 {
3799 cc = *name_end;
3800 *name_end = NUL;
3801
3802 /* Normal name or expanded name. */
3803 if (check_changedtick(lp->ll_name))
3804 ret = FAIL;
3805 else
3806 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003807 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003808 if (di == NULL)
3809 ret = FAIL;
3810 else
3811 {
3812 if (lock)
3813 di->di_flags |= DI_FLAGS_LOCK;
3814 else
3815 di->di_flags &= ~DI_FLAGS_LOCK;
3816 item_lock(&di->di_tv, deep, lock);
3817 }
3818 }
3819 *name_end = cc;
3820 }
3821 else if (lp->ll_range)
3822 {
3823 listitem_T *li = lp->ll_li;
3824
3825 /* (un)lock a range of List items. */
3826 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3827 {
3828 item_lock(&li->li_tv, deep, lock);
3829 li = li->li_next;
3830 ++lp->ll_n1;
3831 }
3832 }
3833 else if (lp->ll_list != NULL)
3834 /* (un)lock a List item. */
3835 item_lock(&lp->ll_li->li_tv, deep, lock);
3836 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003837 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003838 item_lock(&lp->ll_di->di_tv, deep, lock);
3839
3840 return ret;
3841}
3842
3843/*
3844 * Lock or unlock an item. "deep" is nr of levels to go.
3845 */
3846 static void
3847item_lock(tv, deep, lock)
3848 typval_T *tv;
3849 int deep;
3850 int lock;
3851{
3852 static int recurse = 0;
3853 list_T *l;
3854 listitem_T *li;
3855 dict_T *d;
3856 hashitem_T *hi;
3857 int todo;
3858
3859 if (recurse >= DICT_MAXNEST)
3860 {
3861 EMSG(_("E743: variable nested too deep for (un)lock"));
3862 return;
3863 }
3864 if (deep == 0)
3865 return;
3866 ++recurse;
3867
3868 /* lock/unlock the item itself */
3869 if (lock)
3870 tv->v_lock |= VAR_LOCKED;
3871 else
3872 tv->v_lock &= ~VAR_LOCKED;
3873
3874 switch (tv->v_type)
3875 {
3876 case VAR_LIST:
3877 if ((l = tv->vval.v_list) != NULL)
3878 {
3879 if (lock)
3880 l->lv_lock |= VAR_LOCKED;
3881 else
3882 l->lv_lock &= ~VAR_LOCKED;
3883 if (deep < 0 || deep > 1)
3884 /* recursive: lock/unlock the items the List contains */
3885 for (li = l->lv_first; li != NULL; li = li->li_next)
3886 item_lock(&li->li_tv, deep - 1, lock);
3887 }
3888 break;
3889 case VAR_DICT:
3890 if ((d = tv->vval.v_dict) != NULL)
3891 {
3892 if (lock)
3893 d->dv_lock |= VAR_LOCKED;
3894 else
3895 d->dv_lock &= ~VAR_LOCKED;
3896 if (deep < 0 || deep > 1)
3897 {
3898 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003899 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003900 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3901 {
3902 if (!HASHITEM_EMPTY(hi))
3903 {
3904 --todo;
3905 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3906 }
3907 }
3908 }
3909 }
3910 }
3911 --recurse;
3912}
3913
Bram Moolenaara40058a2005-07-11 22:42:07 +00003914/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003915 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3916 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003917 */
3918 static int
3919tv_islocked(tv)
3920 typval_T *tv;
3921{
3922 return (tv->v_lock & VAR_LOCKED)
3923 || (tv->v_type == VAR_LIST
3924 && tv->vval.v_list != NULL
3925 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3926 || (tv->v_type == VAR_DICT
3927 && tv->vval.v_dict != NULL
3928 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3929}
3930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3932/*
3933 * Delete all "menutrans_" variables.
3934 */
3935 void
3936del_menutrans_vars()
3937{
Bram Moolenaar33570922005-01-25 22:26:29 +00003938 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003942 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003943 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 {
3945 if (!HASHITEM_EMPTY(hi))
3946 {
3947 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3949 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 }
3951 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953}
3954#endif
3955
3956#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3957
3958/*
3959 * Local string buffer for the next two functions to store a variable name
3960 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3961 * get_user_var_name().
3962 */
3963
3964static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3965
3966static char_u *varnamebuf = NULL;
3967static int varnamebuflen = 0;
3968
3969/*
3970 * Function to concatenate a prefix and a variable name.
3971 */
3972 static char_u *
3973cat_prefix_varname(prefix, name)
3974 int prefix;
3975 char_u *name;
3976{
3977 int len;
3978
3979 len = (int)STRLEN(name) + 3;
3980 if (len > varnamebuflen)
3981 {
3982 vim_free(varnamebuf);
3983 len += 10; /* some additional space */
3984 varnamebuf = alloc(len);
3985 if (varnamebuf == NULL)
3986 {
3987 varnamebuflen = 0;
3988 return NULL;
3989 }
3990 varnamebuflen = len;
3991 }
3992 *varnamebuf = prefix;
3993 varnamebuf[1] = ':';
3994 STRCPY(varnamebuf + 2, name);
3995 return varnamebuf;
3996}
3997
3998/*
3999 * Function given to ExpandGeneric() to obtain the list of user defined
4000 * (global/buffer/window/built-in) variable names.
4001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 char_u *
4003get_user_var_name(xp, idx)
4004 expand_T *xp;
4005 int idx;
4006{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004007 static long_u gdone;
4008 static long_u bdone;
4009 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004010#ifdef FEAT_WINDOWS
4011 static long_u tdone;
4012#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004013 static int vidx;
4014 static hashitem_T *hi;
4015 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016
4017 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004018 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004019 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020#ifdef FEAT_WINDOWS
4021 tdone = 0;
4022#endif
4023 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004024
4025 /* Global variables */
4026 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004028 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004030 else
4031 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004032 while (HASHITEM_EMPTY(hi))
4033 ++hi;
4034 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4035 return cat_prefix_varname('g', hi->hi_key);
4036 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004038
4039 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004040 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004045 else
4046 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 while (HASHITEM_EMPTY(hi))
4048 ++hi;
4049 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004053 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 return (char_u *)"b:changedtick";
4055 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004056
4057 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004058 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004059 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004061 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004062 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004063 else
4064 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004065 while (HASHITEM_EMPTY(hi))
4066 ++hi;
4067 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004069
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004070#ifdef FEAT_WINDOWS
4071 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004072 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004073 if (tdone < ht->ht_used)
4074 {
4075 if (tdone++ == 0)
4076 hi = ht->ht_array;
4077 else
4078 ++hi;
4079 while (HASHITEM_EMPTY(hi))
4080 ++hi;
4081 return cat_prefix_varname('t', hi->hi_key);
4082 }
4083#endif
4084
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 /* v: variables */
4086 if (vidx < VV_LEN)
4087 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 vim_free(varnamebuf);
4090 varnamebuf = NULL;
4091 varnamebuflen = 0;
4092 return NULL;
4093}
4094
4095#endif /* FEAT_CMDL_COMPL */
4096
4097/*
4098 * types for expressions.
4099 */
4100typedef enum
4101{
4102 TYPE_UNKNOWN = 0
4103 , TYPE_EQUAL /* == */
4104 , TYPE_NEQUAL /* != */
4105 , TYPE_GREATER /* > */
4106 , TYPE_GEQUAL /* >= */
4107 , TYPE_SMALLER /* < */
4108 , TYPE_SEQUAL /* <= */
4109 , TYPE_MATCH /* =~ */
4110 , TYPE_NOMATCH /* !~ */
4111} exptype_T;
4112
4113/*
4114 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4117 */
4118
4119/*
4120 * Handle zero level expression.
4121 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004123 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 * Return OK or FAIL.
4125 */
4126 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 char_u **nextcmd;
4131 int evaluate;
4132{
4133 int ret;
4134 char_u *p;
4135
4136 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 if (ret == FAIL || !ends_excmd(*p))
4139 {
4140 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 /*
4143 * Report the invalid expression unless the expression evaluation has
4144 * been cancelled due to an aborting error, an interrupt, or an
4145 * exception.
4146 */
4147 if (!aborting())
4148 EMSG2(_(e_invexpr2), arg);
4149 ret = FAIL;
4150 }
4151 if (nextcmd != NULL)
4152 *nextcmd = check_nextcmd(p);
4153
4154 return ret;
4155}
4156
4157/*
4158 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004159 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 *
4161 * "arg" must point to the first non-white of the expression.
4162 * "arg" is advanced to the next non-white after the recognized expression.
4163 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004164 * Note: "rettv.v_lock" is not set.
4165 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 * Return OK or FAIL.
4167 */
4168 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 int evaluate;
4173{
4174 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004175 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
4177 /*
4178 * Get the first variable.
4179 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 return FAIL;
4182
4183 if ((*arg)[0] == '?')
4184 {
4185 result = FALSE;
4186 if (evaluate)
4187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 int error = FALSE;
4189
4190 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193 if (error)
4194 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196
4197 /*
4198 * Get the second variable.
4199 */
4200 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 return FAIL;
4203
4204 /*
4205 * Check for the ":".
4206 */
4207 if ((*arg)[0] != ':')
4208 {
4209 EMSG(_("E109: Missing ':' after '?'"));
4210 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213 }
4214
4215 /*
4216 * Get the third variable.
4217 */
4218 *arg = skipwhite(*arg + 1);
4219 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4220 {
4221 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 return FAIL;
4224 }
4225 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228
4229 return OK;
4230}
4231
4232/*
4233 * Handle first level expression:
4234 * expr2 || expr2 || expr2 logical OR
4235 *
4236 * "arg" must point to the first non-white of the expression.
4237 * "arg" is advanced to the next non-white after the recognized expression.
4238 *
4239 * Return OK or FAIL.
4240 */
4241 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 int evaluate;
4246{
Bram Moolenaar33570922005-01-25 22:26:29 +00004247 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 long result;
4249 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 /*
4253 * Get the first variable.
4254 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 return FAIL;
4257
4258 /*
4259 * Repeat until there is no following "||".
4260 */
4261 first = TRUE;
4262 result = FALSE;
4263 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4264 {
4265 if (evaluate && first)
4266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004267 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004269 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 if (error)
4271 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 first = FALSE;
4273 }
4274
4275 /*
4276 * Get the second variable.
4277 */
4278 *arg = skipwhite(*arg + 2);
4279 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4280 return FAIL;
4281
4282 /*
4283 * Compute the result.
4284 */
4285 if (evaluate && !result)
4286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004287 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004290 if (error)
4291 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293 if (evaluate)
4294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 rettv->v_type = VAR_NUMBER;
4296 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298 }
4299
4300 return OK;
4301}
4302
4303/*
4304 * Handle second level expression:
4305 * expr3 && expr3 && expr3 logical AND
4306 *
4307 * "arg" must point to the first non-white of the expression.
4308 * "arg" is advanced to the next non-white after the recognized expression.
4309 *
4310 * Return OK or FAIL.
4311 */
4312 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 int evaluate;
4317{
Bram Moolenaar33570922005-01-25 22:26:29 +00004318 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 long result;
4320 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 /*
4324 * Get the first variable.
4325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return FAIL;
4328
4329 /*
4330 * Repeat until there is no following "&&".
4331 */
4332 first = TRUE;
4333 result = TRUE;
4334 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4335 {
4336 if (evaluate && first)
4337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (error)
4342 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 first = FALSE;
4344 }
4345
4346 /*
4347 * Get the second variable.
4348 */
4349 *arg = skipwhite(*arg + 2);
4350 if (eval4(arg, &var2, evaluate && result) == FAIL)
4351 return FAIL;
4352
4353 /*
4354 * Compute the result.
4355 */
4356 if (evaluate && result)
4357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004361 if (error)
4362 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
4364 if (evaluate)
4365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 rettv->v_type = VAR_NUMBER;
4367 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 }
4370
4371 return OK;
4372}
4373
4374/*
4375 * Handle third level expression:
4376 * var1 == var2
4377 * var1 =~ var2
4378 * var1 != var2
4379 * var1 !~ var2
4380 * var1 > var2
4381 * var1 >= var2
4382 * var1 < var2
4383 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004384 * var1 is var2
4385 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 *
4387 * "arg" must point to the first non-white of the expression.
4388 * "arg" is advanced to the next non-white after the recognized expression.
4389 *
4390 * Return OK or FAIL.
4391 */
4392 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004393eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 int evaluate;
4397{
Bram Moolenaar33570922005-01-25 22:26:29 +00004398 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 char_u *p;
4400 int i;
4401 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004402 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 int len = 2;
4404 long n1, n2;
4405 char_u *s1, *s2;
4406 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4407 regmatch_T regmatch;
4408 int ic;
4409 char_u *save_cpo;
4410
4411 /*
4412 * Get the first variable.
4413 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004414 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 return FAIL;
4416
4417 p = *arg;
4418 switch (p[0])
4419 {
4420 case '=': if (p[1] == '=')
4421 type = TYPE_EQUAL;
4422 else if (p[1] == '~')
4423 type = TYPE_MATCH;
4424 break;
4425 case '!': if (p[1] == '=')
4426 type = TYPE_NEQUAL;
4427 else if (p[1] == '~')
4428 type = TYPE_NOMATCH;
4429 break;
4430 case '>': if (p[1] != '=')
4431 {
4432 type = TYPE_GREATER;
4433 len = 1;
4434 }
4435 else
4436 type = TYPE_GEQUAL;
4437 break;
4438 case '<': if (p[1] != '=')
4439 {
4440 type = TYPE_SMALLER;
4441 len = 1;
4442 }
4443 else
4444 type = TYPE_SEQUAL;
4445 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004446 case 'i': if (p[1] == 's')
4447 {
4448 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4449 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004450 i = p[len];
4451 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004452 {
4453 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4454 type_is = TRUE;
4455 }
4456 }
4457 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 }
4459
4460 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004461 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 */
4463 if (type != TYPE_UNKNOWN)
4464 {
4465 /* extra question mark appended: ignore case */
4466 if (p[len] == '?')
4467 {
4468 ic = TRUE;
4469 ++len;
4470 }
4471 /* extra '#' appended: match case */
4472 else if (p[len] == '#')
4473 {
4474 ic = FALSE;
4475 ++len;
4476 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004477 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 else
4479 ic = p_ic;
4480
4481 /*
4482 * Get the second variable.
4483 */
4484 *arg = skipwhite(p + len);
4485 if (eval5(arg, &var2, evaluate) == FAIL)
4486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004487 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 return FAIL;
4489 }
4490
4491 if (evaluate)
4492 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004493 if (type_is && rettv->v_type != var2.v_type)
4494 {
4495 /* For "is" a different type always means FALSE, for "notis"
4496 * it means TRUE. */
4497 n1 = (type == TYPE_NEQUAL);
4498 }
4499 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4500 {
4501 if (type_is)
4502 {
4503 n1 = (rettv->v_type == var2.v_type
4504 && rettv->vval.v_list == var2.vval.v_list);
4505 if (type == TYPE_NEQUAL)
4506 n1 = !n1;
4507 }
4508 else if (rettv->v_type != var2.v_type
4509 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4510 {
4511 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004512 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004514 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004515 clear_tv(rettv);
4516 clear_tv(&var2);
4517 return FAIL;
4518 }
4519 else
4520 {
4521 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004522 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4523 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004524 if (type == TYPE_NEQUAL)
4525 n1 = !n1;
4526 }
4527 }
4528
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004529 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4530 {
4531 if (type_is)
4532 {
4533 n1 = (rettv->v_type == var2.v_type
4534 && rettv->vval.v_dict == var2.vval.v_dict);
4535 if (type == TYPE_NEQUAL)
4536 n1 = !n1;
4537 }
4538 else if (rettv->v_type != var2.v_type
4539 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4540 {
4541 if (rettv->v_type != var2.v_type)
4542 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4543 else
4544 EMSG(_("E736: Invalid operation for Dictionary"));
4545 clear_tv(rettv);
4546 clear_tv(&var2);
4547 return FAIL;
4548 }
4549 else
4550 {
4551 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004552 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4553 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004554 if (type == TYPE_NEQUAL)
4555 n1 = !n1;
4556 }
4557 }
4558
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4560 {
4561 if (rettv->v_type != var2.v_type
4562 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4563 {
4564 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004565 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004566 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004567 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004568 clear_tv(rettv);
4569 clear_tv(&var2);
4570 return FAIL;
4571 }
4572 else
4573 {
4574 /* Compare two Funcrefs for being equal or unequal. */
4575 if (rettv->vval.v_string == NULL
4576 || var2.vval.v_string == NULL)
4577 n1 = FALSE;
4578 else
4579 n1 = STRCMP(rettv->vval.v_string,
4580 var2.vval.v_string) == 0;
4581 if (type == TYPE_NEQUAL)
4582 n1 = !n1;
4583 }
4584 }
4585
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004586#ifdef FEAT_FLOAT
4587 /*
4588 * If one of the two variables is a float, compare as a float.
4589 * When using "=~" or "!~", always compare as string.
4590 */
4591 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4592 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4593 {
4594 float_T f1, f2;
4595
4596 if (rettv->v_type == VAR_FLOAT)
4597 f1 = rettv->vval.v_float;
4598 else
4599 f1 = get_tv_number(rettv);
4600 if (var2.v_type == VAR_FLOAT)
4601 f2 = var2.vval.v_float;
4602 else
4603 f2 = get_tv_number(&var2);
4604 n1 = FALSE;
4605 switch (type)
4606 {
4607 case TYPE_EQUAL: n1 = (f1 == f2); break;
4608 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4609 case TYPE_GREATER: n1 = (f1 > f2); break;
4610 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4611 case TYPE_SMALLER: n1 = (f1 < f2); break;
4612 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4613 case TYPE_UNKNOWN:
4614 case TYPE_MATCH:
4615 case TYPE_NOMATCH: break; /* avoid gcc warning */
4616 }
4617 }
4618#endif
4619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 /*
4621 * If one of the two variables is a number, compare as a number.
4622 * When using "=~" or "!~", always compare as string.
4623 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004624 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4626 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004627 n1 = get_tv_number(rettv);
4628 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 switch (type)
4630 {
4631 case TYPE_EQUAL: n1 = (n1 == n2); break;
4632 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4633 case TYPE_GREATER: n1 = (n1 > n2); break;
4634 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4635 case TYPE_SMALLER: n1 = (n1 < n2); break;
4636 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4637 case TYPE_UNKNOWN:
4638 case TYPE_MATCH:
4639 case TYPE_NOMATCH: break; /* avoid gcc warning */
4640 }
4641 }
4642 else
4643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 s1 = get_tv_string_buf(rettv, buf1);
4645 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4647 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4648 else
4649 i = 0;
4650 n1 = FALSE;
4651 switch (type)
4652 {
4653 case TYPE_EQUAL: n1 = (i == 0); break;
4654 case TYPE_NEQUAL: n1 = (i != 0); break;
4655 case TYPE_GREATER: n1 = (i > 0); break;
4656 case TYPE_GEQUAL: n1 = (i >= 0); break;
4657 case TYPE_SMALLER: n1 = (i < 0); break;
4658 case TYPE_SEQUAL: n1 = (i <= 0); break;
4659
4660 case TYPE_MATCH:
4661 case TYPE_NOMATCH:
4662 /* avoid 'l' flag in 'cpoptions' */
4663 save_cpo = p_cpo;
4664 p_cpo = (char_u *)"";
4665 regmatch.regprog = vim_regcomp(s2,
4666 RE_MAGIC + RE_STRING);
4667 regmatch.rm_ic = ic;
4668 if (regmatch.regprog != NULL)
4669 {
4670 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004671 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (type == TYPE_NOMATCH)
4673 n1 = !n1;
4674 }
4675 p_cpo = save_cpo;
4676 break;
4677
4678 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4679 }
4680 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004681 clear_tv(rettv);
4682 clear_tv(&var2);
4683 rettv->v_type = VAR_NUMBER;
4684 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 }
4686 }
4687
4688 return OK;
4689}
4690
4691/*
4692 * Handle fourth level expression:
4693 * + number addition
4694 * - number subtraction
4695 * . string concatenation
4696 *
4697 * "arg" must point to the first non-white of the expression.
4698 * "arg" is advanced to the next non-white after the recognized expression.
4699 *
4700 * Return OK or FAIL.
4701 */
4702 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004703eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 int evaluate;
4707{
Bram Moolenaar33570922005-01-25 22:26:29 +00004708 typval_T var2;
4709 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 int op;
4711 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712#ifdef FEAT_FLOAT
4713 float_T f1 = 0, f2 = 0;
4714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 char_u *s1, *s2;
4716 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4717 char_u *p;
4718
4719 /*
4720 * Get the first variable.
4721 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004722 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 return FAIL;
4724
4725 /*
4726 * Repeat computing, until no '+', '-' or '.' is following.
4727 */
4728 for (;;)
4729 {
4730 op = **arg;
4731 if (op != '+' && op != '-' && op != '.')
4732 break;
4733
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734 if ((op != '+' || rettv->v_type != VAR_LIST)
4735#ifdef FEAT_FLOAT
4736 && (op == '.' || rettv->v_type != VAR_FLOAT)
4737#endif
4738 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004739 {
4740 /* For "list + ...", an illegal use of the first operand as
4741 * a number cannot be determined before evaluating the 2nd
4742 * operand: if this is also a list, all is ok.
4743 * For "something . ...", "something - ..." or "non-list + ...",
4744 * we know that the first operand needs to be a string or number
4745 * without evaluating the 2nd operand. So check before to avoid
4746 * side effects after an error. */
4747 if (evaluate && get_tv_string_chk(rettv) == NULL)
4748 {
4749 clear_tv(rettv);
4750 return FAIL;
4751 }
4752 }
4753
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 /*
4755 * Get the second variable.
4756 */
4757 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004758 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004760 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 return FAIL;
4762 }
4763
4764 if (evaluate)
4765 {
4766 /*
4767 * Compute the result.
4768 */
4769 if (op == '.')
4770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004771 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4772 s2 = get_tv_string_buf_chk(&var2, buf2);
4773 if (s2 == NULL) /* type error ? */
4774 {
4775 clear_tv(rettv);
4776 clear_tv(&var2);
4777 return FAIL;
4778 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004779 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 clear_tv(rettv);
4781 rettv->v_type = VAR_STRING;
4782 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004784 else if (op == '+' && rettv->v_type == VAR_LIST
4785 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004786 {
4787 /* concatenate Lists */
4788 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4789 &var3) == FAIL)
4790 {
4791 clear_tv(rettv);
4792 clear_tv(&var2);
4793 return FAIL;
4794 }
4795 clear_tv(rettv);
4796 *rettv = var3;
4797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 else
4799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 int error = FALSE;
4801
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802#ifdef FEAT_FLOAT
4803 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004804 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805 f1 = rettv->vval.v_float;
4806 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808 else
4809#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811 n1 = get_tv_number_chk(rettv, &error);
4812 if (error)
4813 {
4814 /* This can only happen for "list + non-list". For
4815 * "non-list + ..." or "something - ...", we returned
4816 * before evaluating the 2nd operand. */
4817 clear_tv(rettv);
4818 return FAIL;
4819 }
4820#ifdef FEAT_FLOAT
4821 if (var2.v_type == VAR_FLOAT)
4822 f1 = n1;
4823#endif
4824 }
4825#ifdef FEAT_FLOAT
4826 if (var2.v_type == VAR_FLOAT)
4827 {
4828 f2 = var2.vval.v_float;
4829 n2 = 0;
4830 }
4831 else
4832#endif
4833 {
4834 n2 = get_tv_number_chk(&var2, &error);
4835 if (error)
4836 {
4837 clear_tv(rettv);
4838 clear_tv(&var2);
4839 return FAIL;
4840 }
4841#ifdef FEAT_FLOAT
4842 if (rettv->v_type == VAR_FLOAT)
4843 f2 = n2;
4844#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004847
4848#ifdef FEAT_FLOAT
4849 /* If there is a float on either side the result is a float. */
4850 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4851 {
4852 if (op == '+')
4853 f1 = f1 + f2;
4854 else
4855 f1 = f1 - f2;
4856 rettv->v_type = VAR_FLOAT;
4857 rettv->vval.v_float = f1;
4858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004860#endif
4861 {
4862 if (op == '+')
4863 n1 = n1 + n2;
4864 else
4865 n1 = n1 - n2;
4866 rettv->v_type = VAR_NUMBER;
4867 rettv->vval.v_number = n1;
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 }
4873 return OK;
4874}
4875
4876/*
4877 * Handle fifth level expression:
4878 * * number multiplication
4879 * / number division
4880 * % number modulo
4881 *
4882 * "arg" must point to the first non-white of the expression.
4883 * "arg" is advanced to the next non-white after the recognized expression.
4884 *
4885 * Return OK or FAIL.
4886 */
4887 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004888eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004892 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893{
Bram Moolenaar33570922005-01-25 22:26:29 +00004894 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 int op;
4896 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897#ifdef FEAT_FLOAT
4898 int use_float = FALSE;
4899 float_T f1 = 0, f2;
4900#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004901 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
4903 /*
4904 * Get the first variable.
4905 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004906 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 return FAIL;
4908
4909 /*
4910 * Repeat computing, until no '*', '/' or '%' is following.
4911 */
4912 for (;;)
4913 {
4914 op = **arg;
4915 if (op != '*' && op != '/' && op != '%')
4916 break;
4917
4918 if (evaluate)
4919 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920#ifdef FEAT_FLOAT
4921 if (rettv->v_type == VAR_FLOAT)
4922 {
4923 f1 = rettv->vval.v_float;
4924 use_float = TRUE;
4925 n1 = 0;
4926 }
4927 else
4928#endif
4929 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004930 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004931 if (error)
4932 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 else
4935 n1 = 0;
4936
4937 /*
4938 * Get the second variable.
4939 */
4940 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004941 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 return FAIL;
4943
4944 if (evaluate)
4945 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004946#ifdef FEAT_FLOAT
4947 if (var2.v_type == VAR_FLOAT)
4948 {
4949 if (!use_float)
4950 {
4951 f1 = n1;
4952 use_float = TRUE;
4953 }
4954 f2 = var2.vval.v_float;
4955 n2 = 0;
4956 }
4957 else
4958#endif
4959 {
4960 n2 = get_tv_number_chk(&var2, &error);
4961 clear_tv(&var2);
4962 if (error)
4963 return FAIL;
4964#ifdef FEAT_FLOAT
4965 if (use_float)
4966 f2 = n2;
4967#endif
4968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
4970 /*
4971 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974#ifdef FEAT_FLOAT
4975 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977 if (op == '*')
4978 f1 = f1 * f2;
4979 else if (op == '/')
4980 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981# ifdef VMS
4982 /* VMS crashes on divide by zero, work around it */
4983 if (f2 == 0.0)
4984 {
4985 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004986 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004987 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004988 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004989 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004990 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004991 }
4992 else
4993 f1 = f1 / f2;
4994# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 /* We rely on the floating point library to handle divide
4996 * by zero to result in "inf" and not a crash. */
4997 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004998# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005002 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005003 return FAIL;
5004 }
5005 rettv->v_type = VAR_FLOAT;
5006 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
5008 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 if (op == '*')
5012 n1 = n1 * n2;
5013 else if (op == '/')
5014 {
5015 if (n2 == 0) /* give an error message? */
5016 {
5017 if (n1 == 0)
5018 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5019 else if (n1 < 0)
5020 n1 = -0x7fffffffL;
5021 else
5022 n1 = 0x7fffffffL;
5023 }
5024 else
5025 n1 = n1 / n2;
5026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005028 {
5029 if (n2 == 0) /* give an error message? */
5030 n1 = 0;
5031 else
5032 n1 = n1 % n2;
5033 }
5034 rettv->v_type = VAR_NUMBER;
5035 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038 }
5039
5040 return OK;
5041}
5042
5043/*
5044 * Handle sixth level expression:
5045 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005046 * "string" string constant
5047 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 * &option-name option value
5049 * @r register contents
5050 * identifier variable value
5051 * function() function call
5052 * $VAR environment variable
5053 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005054 * [expr, expr] List
5055 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 *
5057 * Also handle:
5058 * ! in front logical NOT
5059 * - in front unary minus
5060 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 * trailing [] subscript in String or List
5062 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 *
5064 * "arg" must point to the first non-white of the expression.
5065 * "arg" is advanced to the next non-white after the recognized expression.
5066 *
5067 * Return OK or FAIL.
5068 */
5069 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005070eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005074 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 long n;
5077 int len;
5078 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 char_u *start_leader, *end_leader;
5080 int ret = OK;
5081 char_u *alias;
5082
5083 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088
5089 /*
5090 * Skip '!' and '-' characters. They are handled later.
5091 */
5092 start_leader = *arg;
5093 while (**arg == '!' || **arg == '-' || **arg == '+')
5094 *arg = skipwhite(*arg + 1);
5095 end_leader = *arg;
5096
5097 switch (**arg)
5098 {
5099 /*
5100 * Number constant.
5101 */
5102 case '0':
5103 case '1':
5104 case '2':
5105 case '3':
5106 case '4':
5107 case '5':
5108 case '6':
5109 case '7':
5110 case '8':
5111 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005112 {
5113#ifdef FEAT_FLOAT
5114 char_u *p = skipdigits(*arg + 1);
5115 int get_float = FALSE;
5116
5117 /* We accept a float when the format matches
5118 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005119 * strict to avoid backwards compatibility problems.
5120 * Don't look for a float after the "." operator, so that
5121 * ":let vers = 1.2.3" doesn't fail. */
5122 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005124 get_float = TRUE;
5125 p = skipdigits(p + 2);
5126 if (*p == 'e' || *p == 'E')
5127 {
5128 ++p;
5129 if (*p == '-' || *p == '+')
5130 ++p;
5131 if (!vim_isdigit(*p))
5132 get_float = FALSE;
5133 else
5134 p = skipdigits(p + 1);
5135 }
5136 if (ASCII_ISALPHA(*p) || *p == '.')
5137 get_float = FALSE;
5138 }
5139 if (get_float)
5140 {
5141 float_T f;
5142
5143 *arg += string2float(*arg, &f);
5144 if (evaluate)
5145 {
5146 rettv->v_type = VAR_FLOAT;
5147 rettv->vval.v_float = f;
5148 }
5149 }
5150 else
5151#endif
5152 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005153 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005154 *arg += len;
5155 if (evaluate)
5156 {
5157 rettv->v_type = VAR_NUMBER;
5158 rettv->vval.v_number = n;
5159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
5161 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
5164 /*
5165 * String constant: "string".
5166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 break;
5169
5170 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 break;
5175
5176 /*
5177 * List: [expr, expr]
5178 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 break;
5181
5182 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 * Dictionary: {key: val, key: val}
5184 */
5185 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5186 break;
5187
5188 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005189 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005191 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 break;
5193
5194 /*
5195 * Environment variable: $VAR.
5196 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005197 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 break;
5199
5200 /*
5201 * Register contents: @r.
5202 */
5203 case '@': ++*arg;
5204 if (evaluate)
5205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005207 rettv->vval.v_string = get_reg_contents(**arg,
5208 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210 if (**arg != NUL)
5211 ++*arg;
5212 break;
5213
5214 /*
5215 * nested expression: (expression).
5216 */
5217 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005218 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 if (**arg == ')')
5220 ++*arg;
5221 else if (ret == OK)
5222 {
5223 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005224 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 ret = FAIL;
5226 }
5227 break;
5228
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 break;
5231 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232
5233 if (ret == NOTDONE)
5234 {
5235 /*
5236 * Must be a variable or function name.
5237 * Can also be a curly-braces kind of name: {expr}.
5238 */
5239 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005240 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 if (alias != NULL)
5242 s = alias;
5243
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005244 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 ret = FAIL;
5246 else
5247 {
5248 if (**arg == '(') /* recursive! */
5249 {
5250 /* If "s" is the name of a variable of type VAR_FUNC
5251 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005252 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253
5254 /* Invoke the function. */
5255 ret = get_func_tv(s, len, rettv, arg,
5256 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005257 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005258
5259 /* If evaluate is FALSE rettv->v_type was not set in
5260 * get_func_tv, but it's needed in handle_subscript() to parse
5261 * what follows. So set it here. */
5262 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5263 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005264 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005265 rettv->v_type = VAR_FUNC;
5266 }
5267
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 /* Stop the expression evaluation when immediately
5269 * aborting on error, or when an interrupt occurred or
5270 * an exception was thrown but not caught. */
5271 if (aborting())
5272 {
5273 if (ret == OK)
5274 clear_tv(rettv);
5275 ret = FAIL;
5276 }
5277 }
5278 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005279 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005280 else
5281 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005283 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 }
5285
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 *arg = skipwhite(*arg);
5287
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5289 * expr(expr). */
5290 if (ret == OK)
5291 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292
5293 /*
5294 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5295 */
5296 if (ret == OK && evaluate && end_leader > start_leader)
5297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005299 int val = 0;
5300#ifdef FEAT_FLOAT
5301 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 if (rettv->v_type == VAR_FLOAT)
5304 f = rettv->vval.v_float;
5305 else
5306#endif
5307 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 clear_tv(rettv);
5311 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 else
5314 {
5315 while (end_leader > start_leader)
5316 {
5317 --end_leader;
5318 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005319 {
5320#ifdef FEAT_FLOAT
5321 if (rettv->v_type == VAR_FLOAT)
5322 f = !f;
5323 else
5324#endif
5325 val = !val;
5326 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005328 {
5329#ifdef FEAT_FLOAT
5330 if (rettv->v_type == VAR_FLOAT)
5331 f = -f;
5332 else
5333#endif
5334 val = -val;
5335 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005336 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005337#ifdef FEAT_FLOAT
5338 if (rettv->v_type == VAR_FLOAT)
5339 {
5340 clear_tv(rettv);
5341 rettv->vval.v_float = f;
5342 }
5343 else
5344#endif
5345 {
5346 clear_tv(rettv);
5347 rettv->v_type = VAR_NUMBER;
5348 rettv->vval.v_number = val;
5349 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
5352
5353 return ret;
5354}
5355
5356/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005357 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5358 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5360 */
5361 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005362eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005364 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005366 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367{
5368 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005369 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371 long len = -1;
5372 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005376 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005378 if (verbose)
5379 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 return FAIL;
5381 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005382#ifdef FEAT_FLOAT
5383 else if (rettv->v_type == VAR_FLOAT)
5384 {
5385 if (verbose)
5386 EMSG(_(e_float_as_string));
5387 return FAIL;
5388 }
5389#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005391 init_tv(&var1);
5392 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 /*
5396 * dict.name
5397 */
5398 key = *arg + 1;
5399 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5400 ;
5401 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005403 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 }
5405 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 /*
5408 * something[idx]
5409 *
5410 * Get the (first) variable from inside the [].
5411 */
5412 *arg = skipwhite(*arg + 1);
5413 if (**arg == ':')
5414 empty1 = TRUE;
5415 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5416 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005417 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5418 {
5419 /* not a number or string */
5420 clear_tv(&var1);
5421 return FAIL;
5422 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005423
5424 /*
5425 * Get the second variable from inside the [:].
5426 */
5427 if (**arg == ':')
5428 {
5429 range = TRUE;
5430 *arg = skipwhite(*arg + 1);
5431 if (**arg == ']')
5432 empty2 = TRUE;
5433 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005435 if (!empty1)
5436 clear_tv(&var1);
5437 return FAIL;
5438 }
5439 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5440 {
5441 /* not a number or string */
5442 if (!empty1)
5443 clear_tv(&var1);
5444 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 return FAIL;
5446 }
5447 }
5448
5449 /* Check for the ']'. */
5450 if (**arg != ']')
5451 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005452 if (verbose)
5453 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005454 clear_tv(&var1);
5455 if (range)
5456 clear_tv(&var2);
5457 return FAIL;
5458 }
5459 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461
5462 if (evaluate)
5463 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464 n1 = 0;
5465 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 n1 = get_tv_number(&var1);
5468 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 }
5470 if (range)
5471 {
5472 if (empty2)
5473 n2 = -1;
5474 else
5475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005476 n2 = get_tv_number(&var2);
5477 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 }
5479 }
5480
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 {
5483 case VAR_NUMBER:
5484 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 len = (long)STRLEN(s);
5487 if (range)
5488 {
5489 /* The resulting variable is a substring. If the indexes
5490 * are out of range the result is empty. */
5491 if (n1 < 0)
5492 {
5493 n1 = len + n1;
5494 if (n1 < 0)
5495 n1 = 0;
5496 }
5497 if (n2 < 0)
5498 n2 = len + n2;
5499 else if (n2 >= len)
5500 n2 = len;
5501 if (n1 >= len || n2 < 0 || n1 > n2)
5502 s = NULL;
5503 else
5504 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5505 }
5506 else
5507 {
5508 /* The resulting variable is a string of a single
5509 * character. If the index is too big or negative the
5510 * result is empty. */
5511 if (n1 >= len || n1 < 0)
5512 s = NULL;
5513 else
5514 s = vim_strnsave(s + n1, 1);
5515 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516 clear_tv(rettv);
5517 rettv->v_type = VAR_STRING;
5518 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 break;
5520
5521 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 if (n1 < 0)
5524 n1 = len + n1;
5525 if (!empty1 && (n1 < 0 || n1 >= len))
5526 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005527 /* For a range we allow invalid values and return an empty
5528 * list. A list index out of range is an error. */
5529 if (!range)
5530 {
5531 if (verbose)
5532 EMSGN(_(e_listidx), n1);
5533 return FAIL;
5534 }
5535 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 }
5537 if (range)
5538 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005539 list_T *l;
5540 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541
5542 if (n2 < 0)
5543 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005544 else if (n2 >= len)
5545 n2 = len - 1;
5546 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005547 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 l = list_alloc();
5549 if (l == NULL)
5550 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 n1 <= n2; ++n1)
5553 {
5554 if (list_append_tv(l, &item->li_tv) == FAIL)
5555 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005556 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 return FAIL;
5558 }
5559 item = item->li_next;
5560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 clear_tv(rettv);
5562 rettv->v_type = VAR_LIST;
5563 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005564 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 }
5566 else
5567 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005568 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 clear_tv(rettv);
5570 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 }
5572 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573
5574 case VAR_DICT:
5575 if (range)
5576 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005577 if (verbose)
5578 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579 if (len == -1)
5580 clear_tv(&var1);
5581 return FAIL;
5582 }
5583 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005584 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585
5586 if (len == -1)
5587 {
5588 key = get_tv_string(&var1);
5589 if (*key == NUL)
5590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005591 if (verbose)
5592 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 clear_tv(&var1);
5594 return FAIL;
5595 }
5596 }
5597
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005598 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005600 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005601 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602 if (len == -1)
5603 clear_tv(&var1);
5604 if (item == NULL)
5605 return FAIL;
5606
5607 copy_tv(&item->di_tv, &var1);
5608 clear_tv(rettv);
5609 *rettv = var1;
5610 }
5611 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 }
5613 }
5614
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615 return OK;
5616}
5617
5618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 * Get an option value.
5620 * "arg" points to the '&' or '+' before the option name.
5621 * "arg" is advanced to character after the option name.
5622 * Return OK or FAIL.
5623 */
5624 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005625get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 int evaluate;
5629{
5630 char_u *option_end;
5631 long numval;
5632 char_u *stringval;
5633 int opt_type;
5634 int c;
5635 int working = (**arg == '+'); /* has("+option") */
5636 int ret = OK;
5637 int opt_flags;
5638
5639 /*
5640 * Isolate the option name and find its value.
5641 */
5642 option_end = find_option_end(arg, &opt_flags);
5643 if (option_end == NULL)
5644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005645 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 EMSG2(_("E112: Option name missing: %s"), *arg);
5647 return FAIL;
5648 }
5649
5650 if (!evaluate)
5651 {
5652 *arg = option_end;
5653 return OK;
5654 }
5655
5656 c = *option_end;
5657 *option_end = NUL;
5658 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
5661 if (opt_type == -3) /* invalid name */
5662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 EMSG2(_("E113: Unknown option: %s"), *arg);
5665 ret = FAIL;
5666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 if (opt_type == -2) /* hidden string option */
5670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 rettv->v_type = VAR_STRING;
5672 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674 else if (opt_type == -1) /* hidden number option */
5675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 rettv->v_type = VAR_NUMBER;
5677 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679 else if (opt_type == 1) /* number option */
5680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 rettv->v_type = VAR_NUMBER;
5682 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684 else /* string option */
5685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 rettv->v_type = VAR_STRING;
5687 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 }
5690 else if (working && (opt_type == -2 || opt_type == -1))
5691 ret = FAIL;
5692
5693 *option_end = c; /* put back for error messages */
5694 *arg = option_end;
5695
5696 return ret;
5697}
5698
5699/*
5700 * Allocate a variable for a string constant.
5701 * Return OK or FAIL.
5702 */
5703 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 int evaluate;
5708{
5709 char_u *p;
5710 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 int extra = 0;
5712
5713 /*
5714 * Find the end of the string, skipping backslashed characters.
5715 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005716 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
5718 if (*p == '\\' && p[1] != NUL)
5719 {
5720 ++p;
5721 /* A "\<x>" form occupies at least 4 characters, and produces up
5722 * to 6 characters: reserve space for 2 extra */
5723 if (*p == '<')
5724 extra += 2;
5725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
5727
5728 if (*p != '"')
5729 {
5730 EMSG2(_("E114: Missing quote: %s"), *arg);
5731 return FAIL;
5732 }
5733
5734 /* If only parsing, set *arg and return here */
5735 if (!evaluate)
5736 {
5737 *arg = p + 1;
5738 return OK;
5739 }
5740
5741 /*
5742 * Copy the string into allocated memory, handling backslashed
5743 * characters.
5744 */
5745 name = alloc((unsigned)(p - *arg + extra));
5746 if (name == NULL)
5747 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 rettv->v_type = VAR_STRING;
5749 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
5753 if (*p == '\\')
5754 {
5755 switch (*++p)
5756 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 case 'b': *name++ = BS; ++p; break;
5758 case 'e': *name++ = ESC; ++p; break;
5759 case 'f': *name++ = FF; ++p; break;
5760 case 'n': *name++ = NL; ++p; break;
5761 case 'r': *name++ = CAR; ++p; break;
5762 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
5764 case 'X': /* hex: "\x1", "\x12" */
5765 case 'x':
5766 case 'u': /* Unicode: "\u0023" */
5767 case 'U':
5768 if (vim_isxdigit(p[1]))
5769 {
5770 int n, nr;
5771 int c = toupper(*p);
5772
5773 if (c == 'X')
5774 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005775 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005777 else
5778 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 nr = 0;
5780 while (--n >= 0 && vim_isxdigit(p[1]))
5781 {
5782 ++p;
5783 nr = (nr << 4) + hex2nr(*p);
5784 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786#ifdef FEAT_MBYTE
5787 /* For "\u" store the number according to
5788 * 'encoding'. */
5789 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 else
5792#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 break;
5796
5797 /* octal: "\1", "\12", "\123" */
5798 case '0':
5799 case '1':
5800 case '2':
5801 case '3':
5802 case '4':
5803 case '5':
5804 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 case '7': *name = *p++ - '0';
5806 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 *name = (*name << 3) + *p++ - '0';
5809 if (*p >= '0' && *p <= '7')
5810 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 break;
5814
5815 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 if (extra != 0)
5818 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 break;
5821 }
5822 /* FALLTHROUGH */
5823
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 break;
5826 }
5827 }
5828 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 *arg = p + 1;
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 return OK;
5836}
5837
5838/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 * Return OK or FAIL.
5841 */
5842 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005843get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 int evaluate;
5847{
5848 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 int reduce = 0;
5851
5852 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 */
5855 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 break;
5861 ++reduce;
5862 ++p;
5863 }
5864 }
5865
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 return FAIL;
5870 }
5871
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 if (!evaluate)
5874 {
5875 *arg = p + 1;
5876 return OK;
5877 }
5878
5879 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 */
5882 str = alloc((unsigned)((p - *arg) - reduce));
5883 if (str == NULL)
5884 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 rettv->v_type = VAR_STRING;
5886 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 break;
5894 ++p;
5895 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 *arg = p + 1;
5900
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901 return OK;
5902}
5903
5904/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905 * Allocate a variable for a List and fill it from "*arg".
5906 * Return OK or FAIL.
5907 */
5908 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005909get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005911 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 int evaluate;
5913{
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 list_T *l = NULL;
5915 typval_T tv;
5916 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917
5918 if (evaluate)
5919 {
5920 l = list_alloc();
5921 if (l == NULL)
5922 return FAIL;
5923 }
5924
5925 *arg = skipwhite(*arg + 1);
5926 while (**arg != ']' && **arg != NUL)
5927 {
5928 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5929 goto failret;
5930 if (evaluate)
5931 {
5932 item = listitem_alloc();
5933 if (item != NULL)
5934 {
5935 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005936 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 list_append(l, item);
5938 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005939 else
5940 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 }
5942
5943 if (**arg == ']')
5944 break;
5945 if (**arg != ',')
5946 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005947 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 goto failret;
5949 }
5950 *arg = skipwhite(*arg + 1);
5951 }
5952
5953 if (**arg != ']')
5954 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956failret:
5957 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005958 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 return FAIL;
5960 }
5961
5962 *arg = skipwhite(*arg + 1);
5963 if (evaluate)
5964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005965 rettv->v_type = VAR_LIST;
5966 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 ++l->lv_refcount;
5968 }
5969
5970 return OK;
5971}
5972
5973/*
5974 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005975 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005977 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978list_alloc()
5979{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005980 list_T *l;
5981
5982 l = (list_T *)alloc_clear(sizeof(list_T));
5983 if (l != NULL)
5984 {
5985 /* Prepend the list to the list of lists for garbage collection. */
5986 if (first_list != NULL)
5987 first_list->lv_used_prev = l;
5988 l->lv_used_prev = NULL;
5989 l->lv_used_next = first_list;
5990 first_list = l;
5991 }
5992 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993}
5994
5995/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005996 * Allocate an empty list for a return value.
5997 * Returns OK or FAIL.
5998 */
5999 static int
6000rettv_list_alloc(rettv)
6001 typval_T *rettv;
6002{
6003 list_T *l = list_alloc();
6004
6005 if (l == NULL)
6006 return FAIL;
6007
6008 rettv->vval.v_list = l;
6009 rettv->v_type = VAR_LIST;
6010 ++l->lv_refcount;
6011 return OK;
6012}
6013
6014/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015 * Unreference a list: decrement the reference count and free it when it
6016 * becomes zero.
6017 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006018 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006020 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006022 if (l != NULL && --l->lv_refcount <= 0)
6023 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024}
6025
6026/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006027 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 * Ignores the reference count.
6029 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006030 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006031list_free(l, recurse)
6032 list_T *l;
6033 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034{
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006037 /* Remove the list from the list of lists for garbage collection. */
6038 if (l->lv_used_prev == NULL)
6039 first_list = l->lv_used_next;
6040 else
6041 l->lv_used_prev->lv_used_next = l->lv_used_next;
6042 if (l->lv_used_next != NULL)
6043 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6044
Bram Moolenaard9fba312005-06-26 22:34:35 +00006045 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006047 /* Remove the item before deleting it. */
6048 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006049 if (recurse || (item->li_tv.v_type != VAR_LIST
6050 && item->li_tv.v_type != VAR_DICT))
6051 clear_tv(&item->li_tv);
6052 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 }
6054 vim_free(l);
6055}
6056
6057/*
6058 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006059 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006061 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062listitem_alloc()
6063{
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065}
6066
6067/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006068 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006070 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006074 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075 vim_free(item);
6076}
6077
6078/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006079 * Remove a list item from a List and free it. Also clears the value.
6080 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006081 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006082listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 list_T *l;
6084 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006085{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006086 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006087 listitem_free(item);
6088}
6089
6090/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 * Get the number of items in a list.
6092 */
6093 static long
6094list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006095 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 if (l == NULL)
6098 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006099 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006100}
6101
6102/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103 * Return TRUE when two lists have exactly the same values.
6104 */
6105 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006106list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 list_T *l1;
6108 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111{
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006114 if (l1 == NULL || l2 == NULL)
6115 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006116 if (l1 == l2)
6117 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006118 if (list_len(l1) != list_len(l2))
6119 return FALSE;
6120
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 for (item1 = l1->lv_first, item2 = l2->lv_first;
6122 item1 != NULL && item2 != NULL;
6123 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125 return FALSE;
6126 return item1 == NULL && item2 == NULL;
6127}
6128
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006129#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6130 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006131/*
6132 * Return the dictitem that an entry in a hashtable points to.
6133 */
6134 dictitem_T *
6135dict_lookup(hi)
6136 hashitem_T *hi;
6137{
6138 return HI2DI(hi);
6139}
6140#endif
6141
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006142/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143 * Return TRUE when two dictionaries have exactly the same key/values.
6144 */
6145 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006146dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 dict_T *d1;
6148 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006150 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006151{
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 hashitem_T *hi;
6153 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006154 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006155
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006156 if (d1 == NULL || d2 == NULL)
6157 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006158 if (d1 == d2)
6159 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006160 if (dict_len(d1) != dict_len(d2))
6161 return FALSE;
6162
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006163 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006165 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006166 if (!HASHITEM_EMPTY(hi))
6167 {
6168 item2 = dict_find(d2, hi->hi_key, -1);
6169 if (item2 == NULL)
6170 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006171 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006172 return FALSE;
6173 --todo;
6174 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006175 }
6176 return TRUE;
6177}
6178
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006179static int tv_equal_recurse_limit;
6180
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006181/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006183 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006184 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185 */
6186 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006187tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006188 typval_T *tv1;
6189 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190 int ic; /* ignore case */
6191 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006192{
6193 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006194 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006195 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006196 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006198 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006199 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006200
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006201 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006202 * recursiveness to a limit. We guess they are equal then.
6203 * A fixed limit has the problem of still taking an awful long time.
6204 * Reduce the limit every time running into it. That should work fine for
6205 * deeply linked structures that are not recursively linked and catch
6206 * recursiveness quickly. */
6207 if (!recursive)
6208 tv_equal_recurse_limit = 1000;
6209 if (recursive_cnt >= tv_equal_recurse_limit)
6210 {
6211 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006212 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006213 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006214
6215 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006216 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006217 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006218 ++recursive_cnt;
6219 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6220 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006221 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006222
6223 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006224 ++recursive_cnt;
6225 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6226 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006227 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006228
6229 case VAR_FUNC:
6230 return (tv1->vval.v_string != NULL
6231 && tv2->vval.v_string != NULL
6232 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6233
6234 case VAR_NUMBER:
6235 return tv1->vval.v_number == tv2->vval.v_number;
6236
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006237#ifdef FEAT_FLOAT
6238 case VAR_FLOAT:
6239 return tv1->vval.v_float == tv2->vval.v_float;
6240#endif
6241
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006242 case VAR_STRING:
6243 s1 = get_tv_string_buf(tv1, buf1);
6244 s2 = get_tv_string_buf(tv2, buf2);
6245 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006246 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006247
6248 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249 return TRUE;
6250}
6251
6252/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 * Locate item with index "n" in list "l" and return it.
6254 * A negative index is counted from the end; -1 is the last item.
6255 * Returns NULL when "n" is out of range.
6256 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006257 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006258list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260 long n;
6261{
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 long idx;
6264
6265 if (l == NULL)
6266 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006267
6268 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006270 n = l->lv_len + n;
6271
6272 /* Check for index out of range. */
6273 if (n < 0 || n >= l->lv_len)
6274 return NULL;
6275
6276 /* When there is a cached index may start search from there. */
6277 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006279 if (n < l->lv_idx / 2)
6280 {
6281 /* closest to the start of the list */
6282 item = l->lv_first;
6283 idx = 0;
6284 }
6285 else if (n > (l->lv_idx + l->lv_len) / 2)
6286 {
6287 /* closest to the end of the list */
6288 item = l->lv_last;
6289 idx = l->lv_len - 1;
6290 }
6291 else
6292 {
6293 /* closest to the cached index */
6294 item = l->lv_idx_item;
6295 idx = l->lv_idx;
6296 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 }
6298 else
6299 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006300 if (n < l->lv_len / 2)
6301 {
6302 /* closest to the start of the list */
6303 item = l->lv_first;
6304 idx = 0;
6305 }
6306 else
6307 {
6308 /* closest to the end of the list */
6309 item = l->lv_last;
6310 idx = l->lv_len - 1;
6311 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313
6314 while (n > idx)
6315 {
6316 /* search forward */
6317 item = item->li_next;
6318 ++idx;
6319 }
6320 while (n < idx)
6321 {
6322 /* search backward */
6323 item = item->li_prev;
6324 --idx;
6325 }
6326
6327 /* cache the used index */
6328 l->lv_idx = idx;
6329 l->lv_idx_item = item;
6330
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 return item;
6332}
6333
6334/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006335 * Get list item "l[idx]" as a number.
6336 */
6337 static long
6338list_find_nr(l, idx, errorp)
6339 list_T *l;
6340 long idx;
6341 int *errorp; /* set to TRUE when something wrong */
6342{
6343 listitem_T *li;
6344
6345 li = list_find(l, idx);
6346 if (li == NULL)
6347 {
6348 if (errorp != NULL)
6349 *errorp = TRUE;
6350 return -1L;
6351 }
6352 return get_tv_number_chk(&li->li_tv, errorp);
6353}
6354
6355/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006356 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6357 */
6358 char_u *
6359list_find_str(l, idx)
6360 list_T *l;
6361 long idx;
6362{
6363 listitem_T *li;
6364
6365 li = list_find(l, idx - 1);
6366 if (li == NULL)
6367 {
6368 EMSGN(_(e_listidx), idx);
6369 return NULL;
6370 }
6371 return get_tv_string(&li->li_tv);
6372}
6373
6374/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006375 * Locate "item" list "l" and return its index.
6376 * Returns -1 when "item" is not in the list.
6377 */
6378 static long
6379list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 list_T *l;
6381 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006382{
6383 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006385
6386 if (l == NULL)
6387 return -1;
6388 idx = 0;
6389 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6390 ++idx;
6391 if (li == NULL)
6392 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006393 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006394}
6395
6396/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397 * Append item "item" to the end of list "l".
6398 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006399 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 list_T *l;
6402 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403{
6404 if (l->lv_last == NULL)
6405 {
6406 /* empty list */
6407 l->lv_first = item;
6408 l->lv_last = item;
6409 item->li_prev = NULL;
6410 }
6411 else
6412 {
6413 l->lv_last->li_next = item;
6414 item->li_prev = l->lv_last;
6415 l->lv_last = item;
6416 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006417 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 item->li_next = NULL;
6419}
6420
6421/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006425 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006427 list_T *l;
6428 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431
Bram Moolenaar05159a02005-02-26 23:04:13 +00006432 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434 copy_tv(tv, &li->li_tv);
6435 list_append(l, li);
6436 return OK;
6437}
6438
6439/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006440 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006441 * Return FAIL when out of memory.
6442 */
6443 int
6444list_append_dict(list, dict)
6445 list_T *list;
6446 dict_T *dict;
6447{
6448 listitem_T *li = listitem_alloc();
6449
6450 if (li == NULL)
6451 return FAIL;
6452 li->li_tv.v_type = VAR_DICT;
6453 li->li_tv.v_lock = 0;
6454 li->li_tv.vval.v_dict = dict;
6455 list_append(list, li);
6456 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457 return OK;
6458}
6459
6460/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006461 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006462 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006463 * Returns FAIL when out of memory.
6464 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006465 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006466list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006467 list_T *l;
6468 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006469 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006470{
6471 listitem_T *li = listitem_alloc();
6472
6473 if (li == NULL)
6474 return FAIL;
6475 list_append(l, li);
6476 li->li_tv.v_type = VAR_STRING;
6477 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006478 if (str == NULL)
6479 li->li_tv.vval.v_string = NULL;
6480 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006481 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006482 return FAIL;
6483 return OK;
6484}
6485
6486/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006487 * Append "n" to list "l".
6488 * Returns FAIL when out of memory.
6489 */
6490 static int
6491list_append_number(l, n)
6492 list_T *l;
6493 varnumber_T n;
6494{
6495 listitem_T *li;
6496
6497 li = listitem_alloc();
6498 if (li == NULL)
6499 return FAIL;
6500 li->li_tv.v_type = VAR_NUMBER;
6501 li->li_tv.v_lock = 0;
6502 li->li_tv.vval.v_number = n;
6503 list_append(l, li);
6504 return OK;
6505}
6506
6507/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509 * If "item" is NULL append at the end.
6510 * Return FAIL when out of memory.
6511 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006512 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006513list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006514 list_T *l;
6515 typval_T *tv;
6516 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517{
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519
6520 if (ni == NULL)
6521 return FAIL;
6522 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006523 list_insert(l, ni, item);
6524 return OK;
6525}
6526
6527 void
6528list_insert(l, ni, item)
6529 list_T *l;
6530 listitem_T *ni;
6531 listitem_T *item;
6532{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 if (item == NULL)
6534 /* Append new item at end of list. */
6535 list_append(l, ni);
6536 else
6537 {
6538 /* Insert new item before existing item. */
6539 ni->li_prev = item->li_prev;
6540 ni->li_next = item;
6541 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006542 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006544 ++l->lv_idx;
6545 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006547 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006549 l->lv_idx_item = NULL;
6550 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006552 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554}
6555
6556/*
6557 * Extend "l1" with "l2".
6558 * If "bef" is NULL append at the end, otherwise insert before this item.
6559 * Returns FAIL when out of memory.
6560 */
6561 static int
6562list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 list_T *l1;
6564 list_T *l2;
6565 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566{
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006568 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006570 /* We also quit the loop when we have inserted the original item count of
6571 * the list, avoid a hang when we extend a list with itself. */
6572 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6574 return FAIL;
6575 return OK;
6576}
6577
6578/*
6579 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6580 * Return FAIL when out of memory.
6581 */
6582 static int
6583list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 list_T *l1;
6585 list_T *l2;
6586 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006587{
Bram Moolenaar33570922005-01-25 22:26:29 +00006588 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006590 if (l1 == NULL || l2 == NULL)
6591 return FAIL;
6592
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006595 if (l == NULL)
6596 return FAIL;
6597 tv->v_type = VAR_LIST;
6598 tv->vval.v_list = l;
6599
6600 /* append all items from the second list */
6601 return list_extend(l, l2, NULL);
6602}
6603
6604/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006605 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608 * Returns NULL when out of memory.
6609 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006610 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006611list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006612 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006614 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615{
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 list_T *copy;
6617 listitem_T *item;
6618 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619
6620 if (orig == NULL)
6621 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622
6623 copy = list_alloc();
6624 if (copy != NULL)
6625 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006626 if (copyID != 0)
6627 {
6628 /* Do this before adding the items, because one of the items may
6629 * refer back to this list. */
6630 orig->lv_copyID = copyID;
6631 orig->lv_copylist = copy;
6632 }
6633 for (item = orig->lv_first; item != NULL && !got_int;
6634 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 {
6636 ni = listitem_alloc();
6637 if (ni == NULL)
6638 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006639 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006640 {
6641 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6642 {
6643 vim_free(ni);
6644 break;
6645 }
6646 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006648 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 list_append(copy, ni);
6650 }
6651 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006652 if (item != NULL)
6653 {
6654 list_unref(copy);
6655 copy = NULL;
6656 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657 }
6658
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 return copy;
6660}
6661
6662/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006664 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006665 * This used to be called list_remove, but that conflicts with a Sun header
6666 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006668 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006669vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006670 list_T *l;
6671 listitem_T *item;
6672 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673{
Bram Moolenaar33570922005-01-25 22:26:29 +00006674 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006676 /* notify watchers */
6677 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006679 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006680 list_fix_watch(l, ip);
6681 if (ip == item2)
6682 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006684
6685 if (item2->li_next == NULL)
6686 l->lv_last = item->li_prev;
6687 else
6688 item2->li_next->li_prev = item->li_prev;
6689 if (item->li_prev == NULL)
6690 l->lv_first = item2->li_next;
6691 else
6692 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006693 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694}
6695
6696/*
6697 * Return an allocated string with the string representation of a list.
6698 * May return NULL.
6699 */
6700 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006701list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006703 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704{
6705 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006706
6707 if (tv->vval.v_list == NULL)
6708 return NULL;
6709 ga_init2(&ga, (int)sizeof(char), 80);
6710 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006711 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006712 {
6713 vim_free(ga.ga_data);
6714 return NULL;
6715 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006716 ga_append(&ga, ']');
6717 ga_append(&ga, NUL);
6718 return (char_u *)ga.ga_data;
6719}
6720
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006721typedef struct join_S {
6722 char_u *s;
6723 char_u *tofree;
6724} join_T;
6725
6726 static int
6727list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6728 garray_T *gap; /* to store the result in */
6729 list_T *l;
6730 char_u *sep;
6731 int echo_style;
6732 int copyID;
6733 garray_T *join_gap; /* to keep each list item string */
6734{
6735 int i;
6736 join_T *p;
6737 int len;
6738 int sumlen = 0;
6739 int first = TRUE;
6740 char_u *tofree;
6741 char_u numbuf[NUMBUFLEN];
6742 listitem_T *item;
6743 char_u *s;
6744
6745 /* Stringify each item in the list. */
6746 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6747 {
6748 if (echo_style)
6749 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6750 else
6751 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6752 if (s == NULL)
6753 return FAIL;
6754
6755 len = (int)STRLEN(s);
6756 sumlen += len;
6757
Bram Moolenaarcde88542015-08-11 19:14:00 +02006758 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006759 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6760 if (tofree != NULL || s != numbuf)
6761 {
6762 p->s = s;
6763 p->tofree = tofree;
6764 }
6765 else
6766 {
6767 p->s = vim_strnsave(s, len);
6768 p->tofree = p->s;
6769 }
6770
6771 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006772 if (did_echo_string_emsg) /* recursion error, bail out */
6773 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006774 }
6775
6776 /* Allocate result buffer with its total size, avoid re-allocation and
6777 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6778 if (join_gap->ga_len >= 2)
6779 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6780 if (ga_grow(gap, sumlen + 2) == FAIL)
6781 return FAIL;
6782
6783 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6784 {
6785 if (first)
6786 first = FALSE;
6787 else
6788 ga_concat(gap, sep);
6789 p = ((join_T *)join_gap->ga_data) + i;
6790
6791 if (p->s != NULL)
6792 ga_concat(gap, p->s);
6793 line_breakcheck();
6794 }
6795
6796 return OK;
6797}
6798
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006799/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006801 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006802 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006804 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006805list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006807 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006808 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006809 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006810 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006812 garray_T join_ga;
6813 int retval;
6814 join_T *p;
6815 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006816
Bram Moolenaard39a7512015-04-16 22:51:22 +02006817 if (l->lv_len < 1)
6818 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006819 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6820 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6821
6822 /* Dispose each item in join_ga. */
6823 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006824 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006825 p = (join_T *)join_ga.ga_data;
6826 for (i = 0; i < join_ga.ga_len; ++i)
6827 {
6828 vim_free(p->tofree);
6829 ++p;
6830 }
6831 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006832 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006833
6834 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006835}
6836
6837/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006838 * Garbage collection for lists and dictionaries.
6839 *
6840 * We use reference counts to be able to free most items right away when they
6841 * are no longer used. But for composite items it's possible that it becomes
6842 * unused while the reference count is > 0: When there is a recursive
6843 * reference. Example:
6844 * :let l = [1, 2, 3]
6845 * :let d = {9: l}
6846 * :let l[1] = d
6847 *
6848 * Since this is quite unusual we handle this with garbage collection: every
6849 * once in a while find out which lists and dicts are not referenced from any
6850 * variable.
6851 *
6852 * Here is a good reference text about garbage collection (refers to Python
6853 * but it applies to all reference-counting mechanisms):
6854 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006855 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006856
6857/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858 * Do garbage collection for lists and dicts.
6859 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006860 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 int
6862garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006863{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006864 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006865 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866 buf_T *buf;
6867 win_T *wp;
6868 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006869 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006870 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006872#ifdef FEAT_WINDOWS
6873 tabpage_T *tp;
6874#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006876 /* Only do this once. */
6877 want_garbage_collect = FALSE;
6878 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006879 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006880
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 /* We advance by two because we add one for items referenced through
6882 * previous_funccal. */
6883 current_copyID += COPYID_INC;
6884 copyID = current_copyID;
6885
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 /*
6887 * 1. Go through all accessible variables and mark all lists and dicts
6888 * with copyID.
6889 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006890
6891 /* Don't free variables in the previous_funccal list unless they are only
6892 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006893 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006894 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6895 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6897 NULL);
6898 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6899 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006900 }
6901
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 /* script-local variables */
6903 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905
6906 /* buffer-local variables */
6907 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006908 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6909 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910
6911 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006912 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6914 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006915#ifdef FEAT_AUTOCMD
6916 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6918 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006919#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006921#ifdef FEAT_WINDOWS
6922 /* tabpage-local variables */
6923 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006924 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6925 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006926#endif
6927
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930
6931 /* function-local variables */
6932 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6933 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6935 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 }
6937
Bram Moolenaard812df62008-11-09 12:46:09 +00006938 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006940
Bram Moolenaar1dced572012-04-05 16:54:08 +02006941#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006943#endif
6944
Bram Moolenaardb913952012-06-29 12:54:53 +02006945#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006947#endif
6948
6949#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006950 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006951#endif
6952
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 /*
6956 * 2. Free lists and dictionaries that are not referenced.
6957 */
6958 did_free = free_unref_items(copyID);
6959
6960 /*
6961 * 3. Check if any funccal can be freed now.
6962 */
6963 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006965 if (can_free_funccal(*pfc, copyID))
6966 {
6967 fc = *pfc;
6968 *pfc = fc->caller;
6969 free_funccal(fc, TRUE);
6970 did_free = TRUE;
6971 did_free_funccal = TRUE;
6972 }
6973 else
6974 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006975 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006976 if (did_free_funccal)
6977 /* When a funccal was freed some more items might be garbage
6978 * collected, so run again. */
6979 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006980 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006981 else if (p_verbose > 0)
6982 {
6983 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6984 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985
6986 return did_free;
6987}
6988
6989/*
6990 * Free lists and dictionaries that are no longer referenced.
6991 */
6992 static int
6993free_unref_items(copyID)
6994 int copyID;
6995{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006996 dict_T *dd, *dd_next;
6997 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006998 int did_free = FALSE;
6999
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007001 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 */
7003 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007004 {
7005 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007006 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007008 /* Free the Dictionary and ordinary items it contains, but don't
7009 * recurse into Lists and Dictionaries, they will be in the list
7010 * of dicts or list of lists. */
7011 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007014 dd = dd_next;
7015 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016
7017 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007018 * Go through the list of lists and free items without the copyID.
7019 * But don't free a list that has a watcher (used in a for loop), these
7020 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 */
7022 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007023 {
7024 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007025 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7026 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007028 /* Free the List and ordinary items it contains, but don't recurse
7029 * into Lists and Dictionaries, they will be in the list of dicts
7030 * or list of lists. */
7031 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007034 ll = ll_next;
7035 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036 return did_free;
7037}
7038
7039/*
7040 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041 * "list_stack" is used to add lists to be marked. Can be NULL.
7042 *
7043 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007045 int
7046set_ref_in_ht(ht, copyID, list_stack)
7047 hashtab_T *ht;
7048 int copyID;
7049 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050{
7051 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054 hashtab_T *cur_ht;
7055 ht_stack_T *ht_stack = NULL;
7056 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 cur_ht = ht;
7059 for (;;)
7060 {
7061 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007062 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007063 /* Mark each item in the hashtab. If the item contains a hashtab
7064 * it is added to ht_stack, if it contains a list it is added to
7065 * list_stack. */
7066 todo = (int)cur_ht->ht_used;
7067 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7068 if (!HASHITEM_EMPTY(hi))
7069 {
7070 --todo;
7071 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7072 &ht_stack, list_stack);
7073 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075
7076 if (ht_stack == NULL)
7077 break;
7078
7079 /* take an item from the stack */
7080 cur_ht = ht_stack->ht;
7081 tempitem = ht_stack;
7082 ht_stack = ht_stack->prev;
7083 free(tempitem);
7084 }
7085
7086 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087}
7088
7089/*
7090 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7092 *
7093 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007095 int
7096set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007097 list_T *l;
7098 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007099 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 listitem_T *li;
7102 int abort = FALSE;
7103 list_T *cur_l;
7104 list_stack_T *list_stack = NULL;
7105 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007106
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007107 cur_l = l;
7108 for (;;)
7109 {
7110 if (!abort)
7111 /* Mark each item in the list. If the item contains a hashtab
7112 * it is added to ht_stack, if it contains a list it is added to
7113 * list_stack. */
7114 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7115 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7116 ht_stack, &list_stack);
7117 if (list_stack == NULL)
7118 break;
7119
7120 /* take an item from the stack */
7121 cur_l = list_stack->list;
7122 tempitem = list_stack;
7123 list_stack = list_stack->prev;
7124 free(tempitem);
7125 }
7126
7127 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007128}
7129
7130/*
7131 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007132 * "list_stack" is used to add lists to be marked. Can be NULL.
7133 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7134 *
7135 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007136 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007137 int
7138set_ref_in_item(tv, copyID, ht_stack, list_stack)
7139 typval_T *tv;
7140 int copyID;
7141 ht_stack_T **ht_stack;
7142 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007143{
7144 dict_T *dd;
7145 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007146 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007147
7148 switch (tv->v_type)
7149 {
7150 case VAR_DICT:
7151 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007152 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007153 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007154 /* Didn't see this dict yet. */
7155 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007156 if (ht_stack == NULL)
7157 {
7158 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7159 }
7160 else
7161 {
7162 ht_stack_T *newitem = (ht_stack_T*)malloc(
7163 sizeof(ht_stack_T));
7164 if (newitem == NULL)
7165 abort = TRUE;
7166 else
7167 {
7168 newitem->ht = &dd->dv_hashtab;
7169 newitem->prev = *ht_stack;
7170 *ht_stack = newitem;
7171 }
7172 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007173 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007174 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007175
7176 case VAR_LIST:
7177 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007178 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007179 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007180 /* Didn't see this list yet. */
7181 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007182 if (list_stack == NULL)
7183 {
7184 abort = set_ref_in_list(ll, copyID, ht_stack);
7185 }
7186 else
7187 {
7188 list_stack_T *newitem = (list_stack_T*)malloc(
7189 sizeof(list_stack_T));
7190 if (newitem == NULL)
7191 abort = TRUE;
7192 else
7193 {
7194 newitem->list = ll;
7195 newitem->prev = *list_stack;
7196 *list_stack = newitem;
7197 }
7198 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007199 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007200 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007201 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007202 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007203}
7204
7205/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206 * Allocate an empty header for a dictionary.
7207 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007208 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209dict_alloc()
7210{
Bram Moolenaar33570922005-01-25 22:26:29 +00007211 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212
Bram Moolenaar33570922005-01-25 22:26:29 +00007213 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007215 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007216 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007217 if (first_dict != NULL)
7218 first_dict->dv_used_prev = d;
7219 d->dv_used_next = first_dict;
7220 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007221 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007222
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007224 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007225 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007226 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007227 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007228 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007229 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230}
7231
7232/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007233 * Allocate an empty dict for a return value.
7234 * Returns OK or FAIL.
7235 */
7236 static int
7237rettv_dict_alloc(rettv)
7238 typval_T *rettv;
7239{
7240 dict_T *d = dict_alloc();
7241
7242 if (d == NULL)
7243 return FAIL;
7244
7245 rettv->vval.v_dict = d;
7246 rettv->v_type = VAR_DICT;
7247 ++d->dv_refcount;
7248 return OK;
7249}
7250
7251
7252/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 * Unreference a Dictionary: decrement the reference count and free it when it
7254 * becomes zero.
7255 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007256 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007260 if (d != NULL && --d->dv_refcount <= 0)
7261 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262}
7263
7264/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007265 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 * Ignores the reference count.
7267 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007268 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007269dict_free(d, recurse)
7270 dict_T *d;
7271 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007273 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007274 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007275 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007277 /* Remove the dict from the list of dicts for garbage collection. */
7278 if (d->dv_used_prev == NULL)
7279 first_dict = d->dv_used_next;
7280 else
7281 d->dv_used_prev->dv_used_next = d->dv_used_next;
7282 if (d->dv_used_next != NULL)
7283 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7284
7285 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007286 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007287 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 if (!HASHITEM_EMPTY(hi))
7291 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007292 /* Remove the item before deleting it, just in case there is
7293 * something recursive causing trouble. */
7294 di = HI2DI(hi);
7295 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007296 if (recurse || (di->di_tv.v_type != VAR_LIST
7297 && di->di_tv.v_type != VAR_DICT))
7298 clear_tv(&di->di_tv);
7299 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 --todo;
7301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007303 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 vim_free(d);
7305}
7306
7307/*
7308 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 * The "key" is copied to the new item.
7310 * Note that the value of the item "di_tv" still needs to be initialized!
7311 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007313 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314dictitem_alloc(key)
7315 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316{
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007319 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007323 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007324 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326}
7327
7328/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329 * Make a copy of a Dictionary item.
7330 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007333 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007334{
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007336
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007337 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7338 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 if (di != NULL)
7340 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007342 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 copy_tv(&org->di_tv, &di->di_tv);
7344 }
7345 return di;
7346}
7347
7348/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 * Remove item "item" from Dictionary "dict" and free it.
7350 */
7351 static void
7352dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 dict_T *dict;
7354 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355{
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 if (HASHITEM_EMPTY(hi))
7360 EMSG2(_(e_intern2), "dictitem_remove()");
7361 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 dictitem_free(item);
7364}
7365
7366/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007367 * Free a dict item. Also clears the value.
7368 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007369 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007370dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007371 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007372{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007373 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007374 if (item->di_flags & DI_FLAGS_ALLOC)
7375 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376}
7377
7378/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7380 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007381 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382 * Returns NULL when out of memory.
7383 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007385dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007388 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389{
Bram Moolenaar33570922005-01-25 22:26:29 +00007390 dict_T *copy;
7391 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394
7395 if (orig == NULL)
7396 return NULL;
7397
7398 copy = dict_alloc();
7399 if (copy != NULL)
7400 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007401 if (copyID != 0)
7402 {
7403 orig->dv_copyID = copyID;
7404 orig->dv_copydict = copy;
7405 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007406 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007407 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007408 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007409 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007410 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007411 --todo;
7412
7413 di = dictitem_alloc(hi->hi_key);
7414 if (di == NULL)
7415 break;
7416 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007417 {
7418 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7419 copyID) == FAIL)
7420 {
7421 vim_free(di);
7422 break;
7423 }
7424 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425 else
7426 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7427 if (dict_add(copy, di) == FAIL)
7428 {
7429 dictitem_free(di);
7430 break;
7431 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007432 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007433 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007434
Bram Moolenaare9a41262005-01-15 22:18:47 +00007435 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007436 if (todo > 0)
7437 {
7438 dict_unref(copy);
7439 copy = NULL;
7440 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007441 }
7442
7443 return copy;
7444}
7445
7446/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007448 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007450 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007452 dict_T *d;
7453 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454{
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456}
7457
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007459 * Add a number or string entry to dictionary "d".
7460 * When "str" is NULL use number "nr", otherwise use "str".
7461 * Returns FAIL when out of memory and when key already exists.
7462 */
7463 int
7464dict_add_nr_str(d, key, nr, str)
7465 dict_T *d;
7466 char *key;
7467 long nr;
7468 char_u *str;
7469{
7470 dictitem_T *item;
7471
7472 item = dictitem_alloc((char_u *)key);
7473 if (item == NULL)
7474 return FAIL;
7475 item->di_tv.v_lock = 0;
7476 if (str == NULL)
7477 {
7478 item->di_tv.v_type = VAR_NUMBER;
7479 item->di_tv.vval.v_number = nr;
7480 }
7481 else
7482 {
7483 item->di_tv.v_type = VAR_STRING;
7484 item->di_tv.vval.v_string = vim_strsave(str);
7485 }
7486 if (dict_add(d, item) == FAIL)
7487 {
7488 dictitem_free(item);
7489 return FAIL;
7490 }
7491 return OK;
7492}
7493
7494/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007495 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007496 * Returns FAIL when out of memory and when key already exists.
7497 */
7498 int
7499dict_add_list(d, key, list)
7500 dict_T *d;
7501 char *key;
7502 list_T *list;
7503{
7504 dictitem_T *item;
7505
7506 item = dictitem_alloc((char_u *)key);
7507 if (item == NULL)
7508 return FAIL;
7509 item->di_tv.v_lock = 0;
7510 item->di_tv.v_type = VAR_LIST;
7511 item->di_tv.vval.v_list = list;
7512 if (dict_add(d, item) == FAIL)
7513 {
7514 dictitem_free(item);
7515 return FAIL;
7516 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007517 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007518 return OK;
7519}
7520
7521/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007522 * Get the number of items in a Dictionary.
7523 */
7524 static long
7525dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007527{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007528 if (d == NULL)
7529 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007530 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007531}
7532
7533/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 * Find item "key[len]" in Dictionary "d".
7535 * If "len" is negative use strlen(key).
7536 * Returns NULL when not found.
7537 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007538 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541 char_u *key;
7542 int len;
7543{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007544#define AKEYLEN 200
7545 char_u buf[AKEYLEN];
7546 char_u *akey;
7547 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550 if (len < 0)
7551 akey = key;
7552 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 tofree = akey = vim_strnsave(key, len);
7555 if (akey == NULL)
7556 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007557 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007558 else
7559 {
7560 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007561 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007562 akey = buf;
7563 }
7564
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566 vim_free(tofree);
7567 if (HASHITEM_EMPTY(hi))
7568 return NULL;
7569 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570}
7571
7572/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007573 * Get a string item from a dictionary.
7574 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007575 * Returns NULL if the entry doesn't exist or out of memory.
7576 */
7577 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007578get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007579 dict_T *d;
7580 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007581 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007582{
7583 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007584 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007585
7586 di = dict_find(d, key, -1);
7587 if (di == NULL)
7588 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007589 s = get_tv_string(&di->di_tv);
7590 if (save && s != NULL)
7591 s = vim_strsave(s);
7592 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007593}
7594
7595/*
7596 * Get a number item from a dictionary.
7597 * Returns 0 if the entry doesn't exist or out of memory.
7598 */
7599 long
7600get_dict_number(d, key)
7601 dict_T *d;
7602 char_u *key;
7603{
7604 dictitem_T *di;
7605
7606 di = dict_find(d, key, -1);
7607 if (di == NULL)
7608 return 0;
7609 return get_tv_number(&di->di_tv);
7610}
7611
7612/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 * Return an allocated string with the string representation of a Dictionary.
7614 * May return NULL.
7615 */
7616 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007617dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007619 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620{
7621 garray_T ga;
7622 int first = TRUE;
7623 char_u *tofree;
7624 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007628 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 return NULL;
7632 ga_init2(&ga, (int)sizeof(char), 80);
7633 ga_append(&ga, '{');
7634
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007635 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007636 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007638 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007640 --todo;
7641
7642 if (first)
7643 first = FALSE;
7644 else
7645 ga_concat(&ga, (char_u *)", ");
7646
7647 tofree = string_quote(hi->hi_key, FALSE);
7648 if (tofree != NULL)
7649 {
7650 ga_concat(&ga, tofree);
7651 vim_free(tofree);
7652 }
7653 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007654 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 if (s != NULL)
7656 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007658 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007659 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007660 line_breakcheck();
7661
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007664 if (todo > 0)
7665 {
7666 vim_free(ga.ga_data);
7667 return NULL;
7668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669
7670 ga_append(&ga, '}');
7671 ga_append(&ga, NUL);
7672 return (char_u *)ga.ga_data;
7673}
7674
7675/*
7676 * Allocate a variable for a Dictionary and fill it from "*arg".
7677 * Return OK or FAIL. Returns NOTDONE for {expr}.
7678 */
7679 static int
7680get_dict_tv(arg, rettv, evaluate)
7681 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 int evaluate;
7684{
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 dict_T *d = NULL;
7686 typval_T tvkey;
7687 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007688 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007689 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692
7693 /*
7694 * First check if it's not a curly-braces thing: {expr}.
7695 * Must do this without evaluating, otherwise a function may be called
7696 * twice. Unfortunately this means we need to call eval1() twice for the
7697 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007698 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007700 if (*start != '}')
7701 {
7702 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7703 return FAIL;
7704 if (*start == '}')
7705 return NOTDONE;
7706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707
7708 if (evaluate)
7709 {
7710 d = dict_alloc();
7711 if (d == NULL)
7712 return FAIL;
7713 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007714 tvkey.v_type = VAR_UNKNOWN;
7715 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716
7717 *arg = skipwhite(*arg + 1);
7718 while (**arg != '}' && **arg != NUL)
7719 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007720 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 goto failret;
7722 if (**arg != ':')
7723 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007724 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007725 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 goto failret;
7727 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007728 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007730 key = get_tv_string_buf_chk(&tvkey, buf);
7731 if (key == NULL || *key == NUL)
7732 {
7733 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7734 if (key != NULL)
7735 EMSG(_(e_emptykey));
7736 clear_tv(&tvkey);
7737 goto failret;
7738 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740
7741 *arg = skipwhite(*arg + 1);
7742 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7743 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007744 if (evaluate)
7745 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007746 goto failret;
7747 }
7748 if (evaluate)
7749 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007750 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007751 if (item != NULL)
7752 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007753 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007754 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 clear_tv(&tv);
7756 goto failret;
7757 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007758 item = dictitem_alloc(key);
7759 clear_tv(&tvkey);
7760 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007763 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007764 if (dict_add(d, item) == FAIL)
7765 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766 }
7767 }
7768
7769 if (**arg == '}')
7770 break;
7771 if (**arg != ',')
7772 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007773 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007774 goto failret;
7775 }
7776 *arg = skipwhite(*arg + 1);
7777 }
7778
7779 if (**arg != '}')
7780 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007781 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007782failret:
7783 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007784 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007785 return FAIL;
7786 }
7787
7788 *arg = skipwhite(*arg + 1);
7789 if (evaluate)
7790 {
7791 rettv->v_type = VAR_DICT;
7792 rettv->vval.v_dict = d;
7793 ++d->dv_refcount;
7794 }
7795
7796 return OK;
7797}
7798
Bram Moolenaar8c711452005-01-14 21:53:12 +00007799/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007800 * Return a string with the string representation of a variable.
7801 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007802 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007803 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007804 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007805 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007806 */
7807 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007808echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007809 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007810 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007811 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007812 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007814 static int recurse = 0;
7815 char_u *r = NULL;
7816
Bram Moolenaar33570922005-01-25 22:26:29 +00007817 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007819 if (!did_echo_string_emsg)
7820 {
7821 /* Only give this message once for a recursive call to avoid
7822 * flooding the user with errors. And stop iterating over lists
7823 * and dicts. */
7824 did_echo_string_emsg = TRUE;
7825 EMSG(_("E724: variable nested too deep for displaying"));
7826 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007827 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007828 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007829 }
7830 ++recurse;
7831
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007832 switch (tv->v_type)
7833 {
7834 case VAR_FUNC:
7835 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007836 r = tv->vval.v_string;
7837 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007838
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007839 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007840 if (tv->vval.v_list == NULL)
7841 {
7842 *tofree = NULL;
7843 r = NULL;
7844 }
7845 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7846 {
7847 *tofree = NULL;
7848 r = (char_u *)"[...]";
7849 }
7850 else
7851 {
7852 tv->vval.v_list->lv_copyID = copyID;
7853 *tofree = list2string(tv, copyID);
7854 r = *tofree;
7855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007857
Bram Moolenaar8c711452005-01-14 21:53:12 +00007858 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007859 if (tv->vval.v_dict == NULL)
7860 {
7861 *tofree = NULL;
7862 r = NULL;
7863 }
7864 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7865 {
7866 *tofree = NULL;
7867 r = (char_u *)"{...}";
7868 }
7869 else
7870 {
7871 tv->vval.v_dict->dv_copyID = copyID;
7872 *tofree = dict2string(tv, copyID);
7873 r = *tofree;
7874 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007875 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007876
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007877 case VAR_STRING:
7878 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 *tofree = NULL;
7880 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007881 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007882
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883#ifdef FEAT_FLOAT
7884 case VAR_FLOAT:
7885 *tofree = NULL;
7886 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7887 r = numbuf;
7888 break;
7889#endif
7890
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007891 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007892 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007893 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007894 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007895
Bram Moolenaar8502c702014-06-17 12:51:16 +02007896 if (--recurse == 0)
7897 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007899}
7900
7901/*
7902 * Return a string with the string representation of a variable.
7903 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7904 * "numbuf" is used for a number.
7905 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007906 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007907 */
7908 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007909tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007910 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007911 char_u **tofree;
7912 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007913 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914{
7915 switch (tv->v_type)
7916 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007917 case VAR_FUNC:
7918 *tofree = string_quote(tv->vval.v_string, TRUE);
7919 return *tofree;
7920 case VAR_STRING:
7921 *tofree = string_quote(tv->vval.v_string, FALSE);
7922 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007923#ifdef FEAT_FLOAT
7924 case VAR_FLOAT:
7925 *tofree = NULL;
7926 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7927 return numbuf;
7928#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007929 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007930 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007931 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007932 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007933 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007934 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007935 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007936 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007937}
7938
7939/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007940 * Return string "str" in ' quotes, doubling ' characters.
7941 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007942 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007943 */
7944 static char_u *
7945string_quote(str, function)
7946 char_u *str;
7947 int function;
7948{
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007950 char_u *p, *r, *s;
7951
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 len = (function ? 13 : 3);
7953 if (str != NULL)
7954 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007955 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 for (p = str; *p != NUL; mb_ptr_adv(p))
7957 if (*p == '\'')
7958 ++len;
7959 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960 s = r = alloc(len);
7961 if (r != NULL)
7962 {
7963 if (function)
7964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007965 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007966 r += 10;
7967 }
7968 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007969 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007970 if (str != NULL)
7971 for (p = str; *p != NUL; )
7972 {
7973 if (*p == '\'')
7974 *r++ = '\'';
7975 MB_COPY_CHAR(p, r);
7976 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007978 if (function)
7979 *r++ = ')';
7980 *r++ = NUL;
7981 }
7982 return s;
7983}
7984
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007985#ifdef FEAT_FLOAT
7986/*
7987 * Convert the string "text" to a floating point number.
7988 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7989 * this always uses a decimal point.
7990 * Returns the length of the text that was consumed.
7991 */
7992 static int
7993string2float(text, value)
7994 char_u *text;
7995 float_T *value; /* result stored here */
7996{
7997 char *s = (char *)text;
7998 float_T f;
7999
8000 f = strtod(s, &s);
8001 *value = f;
8002 return (int)((char_u *)s - text);
8003}
8004#endif
8005
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 * Get the value of an environment variable.
8008 * "arg" is pointing to the '$'. It is advanced to after the name.
8009 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008010 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 */
8012 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 int evaluate;
8017{
8018 char_u *string = NULL;
8019 int len;
8020 int cc;
8021 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008022 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023
8024 ++*arg;
8025 name = *arg;
8026 len = get_env_len(arg);
8027 if (evaluate)
8028 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008029 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008030 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008031
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008032 cc = name[len];
8033 name[len] = NUL;
8034 /* first try vim_getenv(), fast for normal environment vars */
8035 string = vim_getenv(name, &mustfree);
8036 if (string != NULL && *string != NUL)
8037 {
8038 if (!mustfree)
8039 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008041 else
8042 {
8043 if (mustfree)
8044 vim_free(string);
8045
8046 /* next try expanding things like $VIM and ${HOME} */
8047 string = expand_env_save(name - 1);
8048 if (string != NULL && *string == '$')
8049 {
8050 vim_free(string);
8051 string = NULL;
8052 }
8053 }
8054 name[len] = cc;
8055
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008056 rettv->v_type = VAR_STRING;
8057 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 }
8059
8060 return OK;
8061}
8062
8063/*
8064 * Array with names and number of arguments of all internal functions
8065 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8066 */
8067static struct fst
8068{
8069 char *f_name; /* function name */
8070 char f_min_argc; /* minimal number of arguments */
8071 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008072 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008073 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074} functions[] =
8075{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008076#ifdef FEAT_FLOAT
8077 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008078 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008080 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008081 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008082 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"append", 2, 2, f_append},
8084 {"argc", 0, 0, f_argc},
8085 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008086 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008087 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008088#ifdef FEAT_FLOAT
8089 {"asin", 1, 1, f_asin}, /* WJMc */
8090#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008091 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008092 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008093 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008094 {"assert_false", 1, 2, f_assert_false},
8095 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008096#ifdef FEAT_FLOAT
8097 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008098 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008101 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {"bufexists", 1, 1, f_bufexists},
8103 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8104 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8105 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8106 {"buflisted", 1, 1, f_buflisted},
8107 {"bufloaded", 1, 1, f_bufloaded},
8108 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008109 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {"bufwinnr", 1, 1, f_bufwinnr},
8111 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008112 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008113 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008114 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008115#ifdef FEAT_FLOAT
8116 {"ceil", 1, 1, f_ceil},
8117#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008118 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008119 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008121 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008123#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008124 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008125 {"complete_add", 1, 1, f_complete_add},
8126 {"complete_check", 0, 0, f_complete_check},
8127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008129 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008130#ifdef FEAT_FLOAT
8131 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008132 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008133#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008134 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008136 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008137 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008138 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008140 {"diff_filler", 1, 1, f_diff_filler},
8141 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008142 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008144 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {"eventhandler", 0, 0, f_eventhandler},
8146 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008147 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008149#ifdef FEAT_FLOAT
8150 {"exp", 1, 1, f_exp},
8151#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008152 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008153 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008154 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8156 {"filereadable", 1, 1, f_filereadable},
8157 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008158 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008159 {"finddir", 1, 3, f_finddir},
8160 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008161#ifdef FEAT_FLOAT
8162 {"float2nr", 1, 1, f_float2nr},
8163 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008164 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008166 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {"fnamemodify", 2, 2, f_fnamemodify},
8168 {"foldclosed", 1, 1, f_foldclosed},
8169 {"foldclosedend", 1, 1, f_foldclosedend},
8170 {"foldlevel", 1, 1, f_foldlevel},
8171 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008172 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008174 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008175 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008176 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008177 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008178 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"getchar", 0, 1, f_getchar},
8180 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008181 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"getcmdline", 0, 0, f_getcmdline},
8183 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008184 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008185 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008186 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008187 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008188 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008189 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"getfsize", 1, 1, f_getfsize},
8191 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008192 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008193 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008194 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008195 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008196 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008197 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008198 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008199 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008201 {"gettabvar", 2, 3, f_gettabvar},
8202 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {"getwinposx", 0, 0, f_getwinposx},
8204 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008205 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008206 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008207 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008208 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008210 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008211 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008212 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8214 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8215 {"histadd", 2, 2, f_histadd},
8216 {"histdel", 1, 2, f_histdel},
8217 {"histget", 1, 2, f_histget},
8218 {"histnr", 1, 1, f_histnr},
8219 {"hlID", 1, 1, f_hlID},
8220 {"hlexists", 1, 1, f_hlexists},
8221 {"hostname", 0, 0, f_hostname},
8222 {"iconv", 3, 3, f_iconv},
8223 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008224 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008225 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008227 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {"inputrestore", 0, 0, f_inputrestore},
8229 {"inputsave", 0, 0, f_inputsave},
8230 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008231 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008232 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008234 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008235 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008236 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008237 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008239 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {"libcall", 3, 3, f_libcall},
8241 {"libcallnr", 3, 3, f_libcallnr},
8242 {"line", 1, 1, f_line},
8243 {"line2byte", 1, 1, f_line2byte},
8244 {"lispindent", 1, 1, f_lispindent},
8245 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008246#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008247 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008248 {"log10", 1, 1, f_log10},
8249#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008250#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008251 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008252#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008253 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008254 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008255 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008256 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008257 {"matchadd", 2, 5, f_matchadd},
8258 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008259 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008260 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008261 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008262 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008263 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008264 {"max", 1, 1, f_max},
8265 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008266#ifdef vim_mkdir
8267 {"mkdir", 1, 3, f_mkdir},
8268#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008269 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008270#ifdef FEAT_MZSCHEME
8271 {"mzeval", 1, 1, f_mzeval},
8272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008274 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008275 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008276 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008277#ifdef FEAT_PERL
8278 {"perleval", 1, 1, f_perleval},
8279#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008280#ifdef FEAT_FLOAT
8281 {"pow", 2, 2, f_pow},
8282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008284 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008285 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008286#ifdef FEAT_PYTHON3
8287 {"py3eval", 1, 1, f_py3eval},
8288#endif
8289#ifdef FEAT_PYTHON
8290 {"pyeval", 1, 1, f_pyeval},
8291#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008292 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008293 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008294 {"reltime", 0, 2, f_reltime},
8295 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {"remote_expr", 2, 3, f_remote_expr},
8297 {"remote_foreground", 1, 1, f_remote_foreground},
8298 {"remote_peek", 1, 2, f_remote_peek},
8299 {"remote_read", 1, 1, f_remote_read},
8300 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008301 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008303 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008305 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008306#ifdef FEAT_FLOAT
8307 {"round", 1, 1, f_round},
8308#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008309 {"screenattr", 2, 2, f_screenattr},
8310 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008311 {"screencol", 0, 0, f_screencol},
8312 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008313 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008314 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008315 {"searchpair", 3, 7, f_searchpair},
8316 {"searchpairpos", 3, 7, f_searchpairpos},
8317 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {"server2client", 2, 2, f_server2client},
8319 {"serverlist", 0, 0, f_serverlist},
8320 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008321 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {"setcmdpos", 1, 1, f_setcmdpos},
8323 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008324 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008325 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008326 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008327 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008329 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008330 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008332#ifdef FEAT_CRYPT
8333 {"sha256", 1, 1, f_sha256},
8334#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008335 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008336 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008338#ifdef FEAT_FLOAT
8339 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008340 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008341#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008342 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008343 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008344 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008345 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008346 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008347#ifdef FEAT_FLOAT
8348 {"sqrt", 1, 1, f_sqrt},
8349 {"str2float", 1, 1, f_str2float},
8350#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008351 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008352 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008353 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354#ifdef HAVE_STRFTIME
8355 {"strftime", 1, 2, f_strftime},
8356#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008357 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008358 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 {"strlen", 1, 1, f_strlen},
8360 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008361 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008363 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008364 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {"substitute", 4, 4, f_substitute},
8366 {"synID", 3, 3, f_synID},
8367 {"synIDattr", 2, 3, f_synIDattr},
8368 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008369 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008370 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008371 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008372 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008373 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008374 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008375 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008376 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008377 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008378#ifdef FEAT_FLOAT
8379 {"tan", 1, 1, f_tan},
8380 {"tanh", 1, 1, f_tanh},
8381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008383 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {"tolower", 1, 1, f_tolower},
8385 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008386 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008387#ifdef FEAT_FLOAT
8388 {"trunc", 1, 1, f_trunc},
8389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008391 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008392 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008393 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008394 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {"virtcol", 1, 1, f_virtcol},
8396 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008397 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 {"winbufnr", 1, 1, f_winbufnr},
8399 {"wincol", 0, 0, f_wincol},
8400 {"winheight", 1, 1, f_winheight},
8401 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008402 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008404 {"winrestview", 1, 1, f_winrestview},
8405 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008407 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008408 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008409 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410};
8411
8412#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8413
8414/*
8415 * Function given to ExpandGeneric() to obtain the list of internal
8416 * or user defined function names.
8417 */
8418 char_u *
8419get_function_name(xp, idx)
8420 expand_T *xp;
8421 int idx;
8422{
8423 static int intidx = -1;
8424 char_u *name;
8425
8426 if (idx == 0)
8427 intidx = -1;
8428 if (intidx < 0)
8429 {
8430 name = get_user_func_name(xp, idx);
8431 if (name != NULL)
8432 return name;
8433 }
8434 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8435 {
8436 STRCPY(IObuff, functions[intidx].f_name);
8437 STRCAT(IObuff, "(");
8438 if (functions[intidx].f_max_argc == 0)
8439 STRCAT(IObuff, ")");
8440 return IObuff;
8441 }
8442
8443 return NULL;
8444}
8445
8446/*
8447 * Function given to ExpandGeneric() to obtain the list of internal or
8448 * user defined variable or function names.
8449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 char_u *
8451get_expr_name(xp, idx)
8452 expand_T *xp;
8453 int idx;
8454{
8455 static int intidx = -1;
8456 char_u *name;
8457
8458 if (idx == 0)
8459 intidx = -1;
8460 if (intidx < 0)
8461 {
8462 name = get_function_name(xp, idx);
8463 if (name != NULL)
8464 return name;
8465 }
8466 return get_user_var_name(xp, ++intidx);
8467}
8468
8469#endif /* FEAT_CMDL_COMPL */
8470
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008471#if defined(EBCDIC) || defined(PROTO)
8472/*
8473 * Compare struct fst by function name.
8474 */
8475 static int
8476compare_func_name(s1, s2)
8477 const void *s1;
8478 const void *s2;
8479{
8480 struct fst *p1 = (struct fst *)s1;
8481 struct fst *p2 = (struct fst *)s2;
8482
8483 return STRCMP(p1->f_name, p2->f_name);
8484}
8485
8486/*
8487 * Sort the function table by function name.
8488 * The sorting of the table above is ASCII dependant.
8489 * On machines using EBCDIC we have to sort it.
8490 */
8491 static void
8492sortFunctions()
8493{
8494 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8495
8496 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8497}
8498#endif
8499
8500
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501/*
8502 * Find internal function in table above.
8503 * Return index, or -1 if not found
8504 */
8505 static int
8506find_internal_func(name)
8507 char_u *name; /* name of the function */
8508{
8509 int first = 0;
8510 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8511 int cmp;
8512 int x;
8513
8514 /*
8515 * Find the function name in the table. Binary search.
8516 */
8517 while (first <= last)
8518 {
8519 x = first + ((unsigned)(last - first) >> 1);
8520 cmp = STRCMP(name, functions[x].f_name);
8521 if (cmp < 0)
8522 last = x - 1;
8523 else if (cmp > 0)
8524 first = x + 1;
8525 else
8526 return x;
8527 }
8528 return -1;
8529}
8530
8531/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008532 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8533 * name it contains, otherwise return "name".
8534 */
8535 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008536deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008537 char_u *name;
8538 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008539 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008540{
Bram Moolenaar33570922005-01-25 22:26:29 +00008541 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008542 int cc;
8543
8544 cc = name[*lenp];
8545 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008546 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008547 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008549 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008551 {
8552 *lenp = 0;
8553 return (char_u *)""; /* just in case */
8554 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008555 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008556 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008557 }
8558
8559 return name;
8560}
8561
8562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 * Allocate a variable for the result of a function.
8564 * Return OK or FAIL.
8565 */
8566 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008567get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8568 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 char_u *name; /* name of the function */
8570 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 char_u **arg; /* argument, pointing to the '(' */
8573 linenr_T firstline; /* first line of range */
8574 linenr_T lastline; /* last line of range */
8575 int *doesrange; /* return: function handled range */
8576 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008577 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 char_u *argp;
8580 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008581 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 int argcount = 0; /* number of arguments found */
8583
8584 /*
8585 * Get the arguments.
8586 */
8587 argp = *arg;
8588 while (argcount < MAX_FUNC_ARGS)
8589 {
8590 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8591 if (*argp == ')' || *argp == ',' || *argp == NUL)
8592 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8594 {
8595 ret = FAIL;
8596 break;
8597 }
8598 ++argcount;
8599 if (*argp != ',')
8600 break;
8601 }
8602 if (*argp == ')')
8603 ++argp;
8604 else
8605 ret = FAIL;
8606
8607 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008608 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008609 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008611 {
8612 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008613 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008614 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008615 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617
8618 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008619 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620
8621 *arg = skipwhite(argp);
8622 return ret;
8623}
8624
8625
8626/*
8627 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008628 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008629 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 */
8631 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008632call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008634 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008636 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008638 typval_T *argvars; /* vars for arguments, must have "argcount"
8639 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 linenr_T firstline; /* first line of range */
8641 linenr_T lastline; /* last line of range */
8642 int *doesrange; /* return: function handled range */
8643 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008644 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
8646 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647#define ERROR_UNKNOWN 0
8648#define ERROR_TOOMANY 1
8649#define ERROR_TOOFEW 2
8650#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008651#define ERROR_DICT 4
8652#define ERROR_NONE 5
8653#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 int error = ERROR_NONE;
8655 int i;
8656 int llen;
8657 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658#define FLEN_FIXED 40
8659 char_u fname_buf[FLEN_FIXED + 1];
8660 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008661 char_u *name;
8662
8663 /* Make a copy of the name, if it comes from a funcref variable it could
8664 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008665 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008666 if (name == NULL)
8667 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668
8669 /*
8670 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8671 * Change <SNR>123_name() to K_SNR 123_name().
8672 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 llen = eval_fname_script(name);
8675 if (llen > 0)
8676 {
8677 fname_buf[0] = K_SPECIAL;
8678 fname_buf[1] = KS_EXTRA;
8679 fname_buf[2] = (int)KE_SNR;
8680 i = 3;
8681 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8682 {
8683 if (current_SID <= 0)
8684 error = ERROR_SCRIPT;
8685 else
8686 {
8687 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8688 i = (int)STRLEN(fname_buf);
8689 }
8690 }
8691 if (i + STRLEN(name + llen) < FLEN_FIXED)
8692 {
8693 STRCPY(fname_buf + i, name + llen);
8694 fname = fname_buf;
8695 }
8696 else
8697 {
8698 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8699 if (fname == NULL)
8700 error = ERROR_OTHER;
8701 else
8702 {
8703 mch_memmove(fname, fname_buf, (size_t)i);
8704 STRCPY(fname + i, name + llen);
8705 }
8706 }
8707 }
8708 else
8709 fname = name;
8710
8711 *doesrange = FALSE;
8712
8713
8714 /* execute the function if no errors detected and executing */
8715 if (evaluate && error == ERROR_NONE)
8716 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008717 char_u *rfname = fname;
8718
8719 /* Ignore "g:" before a function name. */
8720 if (fname[0] == 'g' && fname[1] == ':')
8721 rfname = fname + 2;
8722
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008723 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8724 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 error = ERROR_UNKNOWN;
8726
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008727 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 {
8729 /*
8730 * User defined function.
8731 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008732 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008733
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008735 /* Trigger FuncUndefined event, may load the function. */
8736 if (fp == NULL
8737 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008738 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008739 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008741 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008742 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 }
8744#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008745 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008746 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008747 {
8748 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008749 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008750 }
8751
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 if (fp != NULL)
8753 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008754 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008756 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008758 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008760 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008761 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 else
8763 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008764 int did_save_redo = FALSE;
8765
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 /*
8767 * Call the user function.
8768 * Save and restore search patterns, script variables and
8769 * redo buffer.
8770 */
8771 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008772#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008773 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008774#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008775 {
8776 saveRedobuff();
8777 did_save_redo = TRUE;
8778 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008779 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008781 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008782 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8783 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8784 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008785 /* Function was unreferenced while being used, free it
8786 * now. */
8787 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008788 if (did_save_redo)
8789 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 restore_search_patterns();
8791 error = ERROR_NONE;
8792 }
8793 }
8794 }
8795 else
8796 {
8797 /*
8798 * Find the function name in the table, call its implementation.
8799 */
8800 i = find_internal_func(fname);
8801 if (i >= 0)
8802 {
8803 if (argcount < functions[i].f_min_argc)
8804 error = ERROR_TOOFEW;
8805 else if (argcount > functions[i].f_max_argc)
8806 error = ERROR_TOOMANY;
8807 else
8808 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008809 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008810 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 error = ERROR_NONE;
8812 }
8813 }
8814 }
8815 /*
8816 * The function call (or "FuncUndefined" autocommand sequence) might
8817 * have been aborted by an error, an interrupt, or an explicitly thrown
8818 * exception that has not been caught so far. This situation can be
8819 * tested for by calling aborting(). For an error in an internal
8820 * function or for the "E132" error in call_user_func(), however, the
8821 * throw point at which the "force_abort" flag (temporarily reset by
8822 * emsg()) is normally updated has not been reached yet. We need to
8823 * update that flag first to make aborting() reliable.
8824 */
8825 update_force_abort();
8826 }
8827 if (error == ERROR_NONE)
8828 ret = OK;
8829
8830 /*
8831 * Report an error unless the argument evaluation or function call has been
8832 * cancelled due to an aborting error, an interrupt, or an exception.
8833 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008834 if (!aborting())
8835 {
8836 switch (error)
8837 {
8838 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008839 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008840 break;
8841 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008842 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008843 break;
8844 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008845 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008846 name);
8847 break;
8848 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008849 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008850 name);
8851 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008852 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008853 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008854 name);
8855 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008856 }
8857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 if (fname != name && fname != fname_buf)
8860 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008861 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862
8863 return ret;
8864}
8865
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008866/*
8867 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008868 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008869 */
8870 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008871emsg_funcname(ermsg, name)
8872 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008873 char_u *name;
8874{
8875 char_u *p;
8876
8877 if (*name == K_SPECIAL)
8878 p = concat_str((char_u *)"<SNR>", name + 3);
8879 else
8880 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008881 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008882 if (p != name)
8883 vim_free(p);
8884}
8885
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008886/*
8887 * Return TRUE for a non-zero Number and a non-empty String.
8888 */
8889 static int
8890non_zero_arg(argvars)
8891 typval_T *argvars;
8892{
8893 return ((argvars[0].v_type == VAR_NUMBER
8894 && argvars[0].vval.v_number != 0)
8895 || (argvars[0].v_type == VAR_STRING
8896 && argvars[0].vval.v_string != NULL
8897 && *argvars[0].vval.v_string != NUL));
8898}
8899
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900/*********************************************
8901 * Implementation of the built-in functions
8902 */
8903
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008904#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008905static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8906
8907/*
8908 * Get the float value of "argvars[0]" into "f".
8909 * Returns FAIL when the argument is not a Number or Float.
8910 */
8911 static int
8912get_float_arg(argvars, f)
8913 typval_T *argvars;
8914 float_T *f;
8915{
8916 if (argvars[0].v_type == VAR_FLOAT)
8917 {
8918 *f = argvars[0].vval.v_float;
8919 return OK;
8920 }
8921 if (argvars[0].v_type == VAR_NUMBER)
8922 {
8923 *f = (float_T)argvars[0].vval.v_number;
8924 return OK;
8925 }
8926 EMSG(_("E808: Number or Float required"));
8927 return FAIL;
8928}
8929
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008930/*
8931 * "abs(expr)" function
8932 */
8933 static void
8934f_abs(argvars, rettv)
8935 typval_T *argvars;
8936 typval_T *rettv;
8937{
8938 if (argvars[0].v_type == VAR_FLOAT)
8939 {
8940 rettv->v_type = VAR_FLOAT;
8941 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8942 }
8943 else
8944 {
8945 varnumber_T n;
8946 int error = FALSE;
8947
8948 n = get_tv_number_chk(&argvars[0], &error);
8949 if (error)
8950 rettv->vval.v_number = -1;
8951 else if (n > 0)
8952 rettv->vval.v_number = n;
8953 else
8954 rettv->vval.v_number = -n;
8955 }
8956}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008957
8958/*
8959 * "acos()" function
8960 */
8961 static void
8962f_acos(argvars, rettv)
8963 typval_T *argvars;
8964 typval_T *rettv;
8965{
8966 float_T f;
8967
8968 rettv->v_type = VAR_FLOAT;
8969 if (get_float_arg(argvars, &f) == OK)
8970 rettv->vval.v_float = acos(f);
8971 else
8972 rettv->vval.v_float = 0.0;
8973}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008974#endif
8975
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008977 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 */
8979 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008980f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008981 typval_T *argvars;
8982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983{
Bram Moolenaar33570922005-01-25 22:26:29 +00008984 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008987 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008989 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008990 && !tv_check_lock(l->lv_lock,
8991 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008992 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008994 }
8995 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008996 EMSG(_(e_listreq));
8997}
8998
8999/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009000 * "alloc_fail(id, countdown, repeat)" function
9001 */
9002 static void
9003f_alloc_fail(argvars, rettv)
9004 typval_T *argvars;
9005 typval_T *rettv UNUSED;
9006{
9007 if (argvars[0].v_type != VAR_NUMBER
9008 || argvars[0].vval.v_number <= 0
9009 || argvars[1].v_type != VAR_NUMBER
9010 || argvars[1].vval.v_number < 0
9011 || argvars[2].v_type != VAR_NUMBER)
9012 EMSG(_(e_invarg));
9013 else
9014 {
9015 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009016 if (alloc_fail_id >= aid_last)
9017 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009018 alloc_fail_countdown = argvars[1].vval.v_number;
9019 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009020 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009021 }
9022}
9023
9024/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009025 * "and(expr, expr)" function
9026 */
9027 static void
9028f_and(argvars, rettv)
9029 typval_T *argvars;
9030 typval_T *rettv;
9031{
9032 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9033 & get_tv_number_chk(&argvars[1], NULL);
9034}
9035
9036/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009037 * "append(lnum, string/list)" function
9038 */
9039 static void
9040f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009041 typval_T *argvars;
9042 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009043{
9044 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009045 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009046 list_T *l = NULL;
9047 listitem_T *li = NULL;
9048 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009049 long added = 0;
9050
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009051 /* When coming here from Insert mode, sync undo, so that this can be
9052 * undone separately from what was previously inserted. */
9053 if (u_sync_once == 2)
9054 {
9055 u_sync_once = 1; /* notify that u_sync() was called */
9056 u_sync(TRUE);
9057 }
9058
Bram Moolenaar0d660222005-01-07 21:51:51 +00009059 lnum = get_tv_lnum(argvars);
9060 if (lnum >= 0
9061 && lnum <= curbuf->b_ml.ml_line_count
9062 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009063 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009064 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009065 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009066 l = argvars[1].vval.v_list;
9067 if (l == NULL)
9068 return;
9069 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009070 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009071 for (;;)
9072 {
9073 if (l == NULL)
9074 tv = &argvars[1]; /* append a string */
9075 else if (li == NULL)
9076 break; /* end of list */
9077 else
9078 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009079 line = get_tv_string_chk(tv);
9080 if (line == NULL) /* type error */
9081 {
9082 rettv->vval.v_number = 1; /* Failed */
9083 break;
9084 }
9085 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009086 ++added;
9087 if (l == NULL)
9088 break;
9089 li = li->li_next;
9090 }
9091
9092 appended_lines_mark(lnum, added);
9093 if (curwin->w_cursor.lnum > lnum)
9094 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009096 else
9097 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098}
9099
9100/*
9101 * "argc()" function
9102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009104f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009105 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109}
9110
9111/*
9112 * "argidx()" function
9113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009116 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009119 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120}
9121
9122/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009123 * "arglistid()" function
9124 */
9125 static void
9126f_arglistid(argvars, rettv)
9127 typval_T *argvars UNUSED;
9128 typval_T *rettv;
9129{
9130 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009131
9132 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009133 wp = find_tabwin(&argvars[0], &argvars[1]);
9134 if (wp != NULL)
9135 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009136}
9137
9138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 * "argv(nr)" function
9140 */
9141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009143 typval_T *argvars;
9144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146 int idx;
9147
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009148 if (argvars[0].v_type != VAR_UNKNOWN)
9149 {
9150 idx = get_tv_number_chk(&argvars[0], NULL);
9151 if (idx >= 0 && idx < ARGCOUNT)
9152 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9153 else
9154 rettv->vval.v_string = NULL;
9155 rettv->v_type = VAR_STRING;
9156 }
9157 else if (rettv_list_alloc(rettv) == OK)
9158 for (idx = 0; idx < ARGCOUNT; ++idx)
9159 list_append_string(rettv->vval.v_list,
9160 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161}
9162
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009163static void prepare_assert_error __ARGS((garray_T*gap));
9164static void fill_assert_error __ARGS((garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv));
9165static void assert_error __ARGS((garray_T *gap));
9166static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9167
9168/*
9169 * Prepare "gap" for an assert error and add the sourcing position.
9170 */
9171 static void
9172prepare_assert_error(gap)
9173 garray_T *gap;
9174{
9175 char buf[NUMBUFLEN];
9176
9177 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009178 if (sourcing_name != NULL)
9179 {
9180 ga_concat(gap, sourcing_name);
9181 if (sourcing_lnum > 0)
9182 ga_concat(gap, (char_u *)" ");
9183 }
9184 if (sourcing_lnum > 0)
9185 {
9186 sprintf(buf, "line %ld", (long)sourcing_lnum);
9187 ga_concat(gap, (char_u *)buf);
9188 }
9189 if (sourcing_name != NULL || sourcing_lnum > 0)
9190 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009191}
9192
9193/*
9194 * Fill "gap" with information about an assert error.
9195 */
9196 static void
9197fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9198 garray_T *gap;
9199 typval_T *opt_msg_tv;
9200 char_u *exp_str;
9201 typval_T *exp_tv;
9202 typval_T *got_tv;
9203{
9204 char_u numbuf[NUMBUFLEN];
9205 char_u *tofree;
9206
9207 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9208 {
9209 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9210 vim_free(tofree);
9211 }
9212 else
9213 {
9214 ga_concat(gap, (char_u *)"Expected ");
9215 if (exp_str == NULL)
9216 {
9217 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9218 vim_free(tofree);
9219 }
9220 else
9221 ga_concat(gap, exp_str);
9222 ga_concat(gap, (char_u *)" but got ");
9223 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9224 vim_free(tofree);
9225 }
9226}
Bram Moolenaar43345542015-11-29 17:35:35 +01009227
9228/*
9229 * Add an assert error to v:errors.
9230 */
9231 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009232assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009233 garray_T *gap;
9234{
9235 struct vimvar *vp = &vimvars[VV_ERRORS];
9236
9237 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9238 /* Make sure v:errors is a list. */
9239 set_vim_var_list(VV_ERRORS, list_alloc());
9240 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9241}
9242
Bram Moolenaar43345542015-11-29 17:35:35 +01009243/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009244 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009245 */
9246 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009247f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009248 typval_T *argvars;
9249 typval_T *rettv UNUSED;
9250{
9251 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009252
9253 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9254 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009255 prepare_assert_error(&ga);
9256 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9257 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009258 ga_clear(&ga);
9259 }
9260}
9261
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009262/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009263 * "assert_exception(string[, msg])" function
9264 */
9265 static void
9266f_assert_exception(argvars, rettv)
9267 typval_T *argvars;
9268 typval_T *rettv UNUSED;
9269{
9270 garray_T ga;
9271 char *error;
9272
9273 error = (char *)get_tv_string_chk(&argvars[0]);
9274 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9275 {
9276 prepare_assert_error(&ga);
9277 ga_concat(&ga, (char_u *)"v:exception is not set");
9278 assert_error(&ga);
9279 ga_clear(&ga);
9280 }
9281 else if (strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
9282 {
9283 prepare_assert_error(&ga);
9284 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9285 &vimvars[VV_EXCEPTION].vv_tv);
9286 assert_error(&ga);
9287 ga_clear(&ga);
9288 }
9289}
9290
9291/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009292 * "assert_fails(cmd [, error])" function
9293 */
9294 static void
9295f_assert_fails(argvars, rettv)
9296 typval_T *argvars;
9297 typval_T *rettv UNUSED;
9298{
9299 char_u *cmd = get_tv_string_chk(&argvars[0]);
9300 garray_T ga;
9301
9302 called_emsg = FALSE;
9303 suppress_errthrow = TRUE;
9304 emsg_silent = TRUE;
9305 do_cmdline_cmd(cmd);
9306 if (!called_emsg)
9307 {
9308 prepare_assert_error(&ga);
9309 ga_concat(&ga, (char_u *)"command did not fail: ");
9310 ga_concat(&ga, cmd);
9311 assert_error(&ga);
9312 ga_clear(&ga);
9313 }
9314 else if (argvars[1].v_type != VAR_UNKNOWN)
9315 {
9316 char_u buf[NUMBUFLEN];
9317 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9318
9319 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9320 {
9321 prepare_assert_error(&ga);
9322 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9323 &vimvars[VV_ERRMSG].vv_tv);
9324 assert_error(&ga);
9325 ga_clear(&ga);
9326 }
9327 }
9328
9329 called_emsg = FALSE;
9330 suppress_errthrow = FALSE;
9331 emsg_silent = FALSE;
9332 emsg_on_display = FALSE;
9333 set_vim_var_string(VV_ERRMSG, NULL, 0);
9334}
9335
9336/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009337 * Common for assert_true() and assert_false().
9338 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009339 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009340assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009341 typval_T *argvars;
9342 int isTrue;
9343{
9344 int error = FALSE;
9345 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009346
9347 if (argvars[0].v_type != VAR_NUMBER
9348 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9349 || error)
9350 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009351 prepare_assert_error(&ga);
9352 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009353 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009354 NULL, &argvars[0]);
9355 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009356 ga_clear(&ga);
9357 }
9358}
9359
9360/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009361 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009362 */
9363 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009364f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009365 typval_T *argvars;
9366 typval_T *rettv UNUSED;
9367{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009368 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009369}
9370
9371/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009372 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009373 */
9374 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009375f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009376 typval_T *argvars;
9377 typval_T *rettv UNUSED;
9378{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009379 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009380}
9381
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009382#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009383/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009384 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009385 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009386 static void
9387f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009388 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009389 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009390{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009391 float_T f;
9392
9393 rettv->v_type = VAR_FLOAT;
9394 if (get_float_arg(argvars, &f) == OK)
9395 rettv->vval.v_float = asin(f);
9396 else
9397 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009398}
9399
9400/*
9401 * "atan()" function
9402 */
9403 static void
9404f_atan(argvars, rettv)
9405 typval_T *argvars;
9406 typval_T *rettv;
9407{
9408 float_T f;
9409
9410 rettv->v_type = VAR_FLOAT;
9411 if (get_float_arg(argvars, &f) == OK)
9412 rettv->vval.v_float = atan(f);
9413 else
9414 rettv->vval.v_float = 0.0;
9415}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009416
9417/*
9418 * "atan2()" function
9419 */
9420 static void
9421f_atan2(argvars, rettv)
9422 typval_T *argvars;
9423 typval_T *rettv;
9424{
9425 float_T fx, fy;
9426
9427 rettv->v_type = VAR_FLOAT;
9428 if (get_float_arg(argvars, &fx) == OK
9429 && get_float_arg(&argvars[1], &fy) == OK)
9430 rettv->vval.v_float = atan2(fx, fy);
9431 else
9432 rettv->vval.v_float = 0.0;
9433}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009434#endif
9435
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436/*
9437 * "browse(save, title, initdir, default)" function
9438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009440f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009441 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443{
9444#ifdef FEAT_BROWSE
9445 int save;
9446 char_u *title;
9447 char_u *initdir;
9448 char_u *defname;
9449 char_u buf[NUMBUFLEN];
9450 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009451 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009453 save = get_tv_number_chk(&argvars[0], &error);
9454 title = get_tv_string_chk(&argvars[1]);
9455 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9456 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009458 if (error || title == NULL || initdir == NULL || defname == NULL)
9459 rettv->vval.v_string = NULL;
9460 else
9461 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009462 do_browse(save ? BROWSE_SAVE : 0,
9463 title, defname, NULL, initdir, NULL, curbuf);
9464#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009465 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009466#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009467 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009468}
9469
9470/*
9471 * "browsedir(title, initdir)" function
9472 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009475 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009476 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009477{
9478#ifdef FEAT_BROWSE
9479 char_u *title;
9480 char_u *initdir;
9481 char_u buf[NUMBUFLEN];
9482
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009483 title = get_tv_string_chk(&argvars[0]);
9484 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009485
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009486 if (title == NULL || initdir == NULL)
9487 rettv->vval.v_string = NULL;
9488 else
9489 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009490 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009494 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495}
9496
Bram Moolenaar33570922005-01-25 22:26:29 +00009497static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009498
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499/*
9500 * Find a buffer by number or exact name.
9501 */
9502 static buf_T *
9503find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009504 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
9506 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009508 if (avar->v_type == VAR_NUMBER)
9509 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009510 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009512 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009513 if (buf == NULL)
9514 {
9515 /* No full path name match, try a match with a URL or a "nofile"
9516 * buffer, these don't use the full path. */
9517 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9518 if (buf->b_fname != NULL
9519 && (path_with_url(buf->b_fname)
9520#ifdef FEAT_QUICKFIX
9521 || bt_nofile(buf)
9522#endif
9523 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009524 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009525 break;
9526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 }
9528 return buf;
9529}
9530
9531/*
9532 * "bufexists(expr)" function
9533 */
9534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009536 typval_T *argvars;
9537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009539 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540}
9541
9542/*
9543 * "buflisted(expr)" function
9544 */
9545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009546f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009547 typval_T *argvars;
9548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
9550 buf_T *buf;
9551
9552 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009553 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554}
9555
9556/*
9557 * "bufloaded(expr)" function
9558 */
9559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009561 typval_T *argvars;
9562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564 buf_T *buf;
9565
9566 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568}
9569
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009570static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009571
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572/*
9573 * Get buffer by number or pattern.
9574 */
9575 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009576get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009577 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009578 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009580 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 int save_magic;
9582 char_u *save_cpo;
9583 buf_T *buf;
9584
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009585 if (tv->v_type == VAR_NUMBER)
9586 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009587 if (tv->v_type != VAR_STRING)
9588 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 if (name == NULL || *name == NUL)
9590 return curbuf;
9591 if (name[0] == '$' && name[1] == NUL)
9592 return lastbuf;
9593
9594 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9595 save_magic = p_magic;
9596 p_magic = TRUE;
9597 save_cpo = p_cpo;
9598 p_cpo = (char_u *)"";
9599
9600 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009601 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602
9603 p_magic = save_magic;
9604 p_cpo = save_cpo;
9605
9606 /* If not found, try expanding the name, like done for bufexists(). */
9607 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609
9610 return buf;
9611}
9612
9613/*
9614 * "bufname(expr)" function
9615 */
9616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009617f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009618 typval_T *argvars;
9619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620{
9621 buf_T *buf;
9622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009623 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009625 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 --emsg_off;
9632}
9633
9634/*
9635 * "bufnr(expr)" function
9636 */
9637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009639 typval_T *argvars;
9640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
9642 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009643 int error = FALSE;
9644 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009646 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009648 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009649 --emsg_off;
9650
9651 /* If the buffer isn't found and the second argument is not zero create a
9652 * new buffer. */
9653 if (buf == NULL
9654 && argvars[1].v_type != VAR_UNKNOWN
9655 && get_tv_number_chk(&argvars[1], &error) != 0
9656 && !error
9657 && (name = get_tv_string_chk(&argvars[0])) != NULL
9658 && !error)
9659 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9660
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009662 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009664 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665}
9666
9667/*
9668 * "bufwinnr(nr)" function
9669 */
9670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009672 typval_T *argvars;
9673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674{
9675#ifdef FEAT_WINDOWS
9676 win_T *wp;
9677 int winnr = 0;
9678#endif
9679 buf_T *buf;
9680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009681 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009683 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684#ifdef FEAT_WINDOWS
9685 for (wp = firstwin; wp; wp = wp->w_next)
9686 {
9687 ++winnr;
9688 if (wp->w_buffer == buf)
9689 break;
9690 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009693 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694#endif
9695 --emsg_off;
9696}
9697
9698/*
9699 * "byte2line(byte)" function
9700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009703 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705{
9706#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708#else
9709 long boff = 0;
9710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009711 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009715 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 (linenr_T)0, &boff);
9717#endif
9718}
9719
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009720 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009721byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009722 typval_T *argvars;
9723 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009724 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009725{
9726#ifdef FEAT_MBYTE
9727 char_u *t;
9728#endif
9729 char_u *str;
9730 long idx;
9731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009732 str = get_tv_string_chk(&argvars[0]);
9733 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009734 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009735 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009736 return;
9737
9738#ifdef FEAT_MBYTE
9739 t = str;
9740 for ( ; idx > 0; idx--)
9741 {
9742 if (*t == NUL) /* EOL reached */
9743 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009744 if (enc_utf8 && comp)
9745 t += utf_ptr2len(t);
9746 else
9747 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009748 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009749 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009750#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009751 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009752 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009753#endif
9754}
9755
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009756/*
9757 * "byteidx()" function
9758 */
9759 static void
9760f_byteidx(argvars, rettv)
9761 typval_T *argvars;
9762 typval_T *rettv;
9763{
9764 byteidx(argvars, rettv, FALSE);
9765}
9766
9767/*
9768 * "byteidxcomp()" function
9769 */
9770 static void
9771f_byteidxcomp(argvars, rettv)
9772 typval_T *argvars;
9773 typval_T *rettv;
9774{
9775 byteidx(argvars, rettv, TRUE);
9776}
9777
Bram Moolenaardb913952012-06-29 12:54:53 +02009778 int
9779func_call(name, args, selfdict, rettv)
9780 char_u *name;
9781 typval_T *args;
9782 dict_T *selfdict;
9783 typval_T *rettv;
9784{
9785 listitem_T *item;
9786 typval_T argv[MAX_FUNC_ARGS + 1];
9787 int argc = 0;
9788 int dummy;
9789 int r = 0;
9790
9791 for (item = args->vval.v_list->lv_first; item != NULL;
9792 item = item->li_next)
9793 {
9794 if (argc == MAX_FUNC_ARGS)
9795 {
9796 EMSG(_("E699: Too many arguments"));
9797 break;
9798 }
9799 /* Make a copy of each argument. This is needed to be able to set
9800 * v_lock to VAR_FIXED in the copy without changing the original list.
9801 */
9802 copy_tv(&item->li_tv, &argv[argc++]);
9803 }
9804
9805 if (item == NULL)
9806 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9807 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9808 &dummy, TRUE, selfdict);
9809
9810 /* Free the arguments. */
9811 while (argc > 0)
9812 clear_tv(&argv[--argc]);
9813
9814 return r;
9815}
9816
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009817/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009818 * "call(func, arglist)" function
9819 */
9820 static void
9821f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009822 typval_T *argvars;
9823 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009824{
9825 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009826 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009827
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009828 if (argvars[1].v_type != VAR_LIST)
9829 {
9830 EMSG(_(e_listreq));
9831 return;
9832 }
9833 if (argvars[1].vval.v_list == NULL)
9834 return;
9835
9836 if (argvars[0].v_type == VAR_FUNC)
9837 func = argvars[0].vval.v_string;
9838 else
9839 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009840 if (*func == NUL)
9841 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009842
Bram Moolenaare9a41262005-01-15 22:18:47 +00009843 if (argvars[2].v_type != VAR_UNKNOWN)
9844 {
9845 if (argvars[2].v_type != VAR_DICT)
9846 {
9847 EMSG(_(e_dictreq));
9848 return;
9849 }
9850 selfdict = argvars[2].vval.v_dict;
9851 }
9852
Bram Moolenaardb913952012-06-29 12:54:53 +02009853 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009854}
9855
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009856#ifdef FEAT_FLOAT
9857/*
9858 * "ceil({float})" function
9859 */
9860 static void
9861f_ceil(argvars, rettv)
9862 typval_T *argvars;
9863 typval_T *rettv;
9864{
9865 float_T f;
9866
9867 rettv->v_type = VAR_FLOAT;
9868 if (get_float_arg(argvars, &f) == OK)
9869 rettv->vval.v_float = ceil(f);
9870 else
9871 rettv->vval.v_float = 0.0;
9872}
9873#endif
9874
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009875/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009876 * "changenr()" function
9877 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009878 static void
9879f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009880 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009881 typval_T *rettv;
9882{
9883 rettv->vval.v_number = curbuf->b_u_seq_cur;
9884}
9885
9886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 * "char2nr(string)" function
9888 */
9889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009891 typval_T *argvars;
9892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893{
9894#ifdef FEAT_MBYTE
9895 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009896 {
9897 int utf8 = 0;
9898
9899 if (argvars[1].v_type != VAR_UNKNOWN)
9900 utf8 = get_tv_number_chk(&argvars[1], NULL);
9901
9902 if (utf8)
9903 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9904 else
9905 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 else
9908#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009909 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910}
9911
9912/*
9913 * "cindent(lnum)" function
9914 */
9915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009916f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009917 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919{
9920#ifdef FEAT_CINDENT
9921 pos_T pos;
9922 linenr_T lnum;
9923
9924 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009925 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9927 {
9928 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009929 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 curwin->w_cursor = pos;
9931 }
9932 else
9933#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009934 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935}
9936
9937/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009938 * "clearmatches()" function
9939 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009940 static void
9941f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009942 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009943 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009944{
9945#ifdef FEAT_SEARCH_EXTRA
9946 clear_matches(curwin);
9947#endif
9948}
9949
9950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 * "col(string)" function
9952 */
9953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009954f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009955 typval_T *argvars;
9956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957{
9958 colnr_T col = 0;
9959 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009960 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009962 fp = var2fpos(&argvars[0], FALSE, &fnum);
9963 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 {
9965 if (fp->col == MAXCOL)
9966 {
9967 /* '> can be MAXCOL, get the length of the line then */
9968 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009969 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 else
9971 col = MAXCOL;
9972 }
9973 else
9974 {
9975 col = fp->col + 1;
9976#ifdef FEAT_VIRTUALEDIT
9977 /* col(".") when the cursor is on the NUL at the end of the line
9978 * because of "coladd" can be seen as an extra column. */
9979 if (virtual_active() && fp == &curwin->w_cursor)
9980 {
9981 char_u *p = ml_get_cursor();
9982
9983 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9984 curwin->w_virtcol - curwin->w_cursor.coladd))
9985 {
9986# ifdef FEAT_MBYTE
9987 int l;
9988
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009989 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 col += l;
9991# else
9992 if (*p != NUL && p[1] == NUL)
9993 ++col;
9994# endif
9995 }
9996 }
9997#endif
9998 }
9999 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001}
10002
Bram Moolenaar572cb562005-08-05 21:35:02 +000010003#if defined(FEAT_INS_EXPAND)
10004/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010005 * "complete()" function
10006 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010007 static void
10008f_complete(argvars, rettv)
10009 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010010 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +000010011{
10012 int startcol;
10013
10014 if ((State & INSERT) == 0)
10015 {
10016 EMSG(_("E785: complete() can only be used in Insert mode"));
10017 return;
10018 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010019
10020 /* Check for undo allowed here, because if something was already inserted
10021 * the line was already saved for undo and this check isn't done. */
10022 if (!undo_allowed())
10023 return;
10024
Bram Moolenaarade00832006-03-10 21:46:58 +000010025 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10026 {
10027 EMSG(_(e_invarg));
10028 return;
10029 }
10030
10031 startcol = get_tv_number_chk(&argvars[0], NULL);
10032 if (startcol <= 0)
10033 return;
10034
10035 set_completion(startcol - 1, argvars[1].vval.v_list);
10036}
10037
10038/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010039 * "complete_add()" function
10040 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010041 static void
10042f_complete_add(argvars, rettv)
10043 typval_T *argvars;
10044 typval_T *rettv;
10045{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010046 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010047}
10048
10049/*
10050 * "complete_check()" function
10051 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010052 static void
10053f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010054 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +000010055 typval_T *rettv;
10056{
10057 int saved = RedrawingDisabled;
10058
10059 RedrawingDisabled = 0;
10060 ins_compl_check_keys(0);
10061 rettv->vval.v_number = compl_interrupted;
10062 RedrawingDisabled = saved;
10063}
10064#endif
10065
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066/*
10067 * "confirm(message, buttons[, default [, type]])" function
10068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010071 typval_T *argvars UNUSED;
10072 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073{
10074#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10075 char_u *message;
10076 char_u *buttons = NULL;
10077 char_u buf[NUMBUFLEN];
10078 char_u buf2[NUMBUFLEN];
10079 int def = 1;
10080 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010081 char_u *typestr;
10082 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010084 message = get_tv_string_chk(&argvars[0]);
10085 if (message == NULL)
10086 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010087 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010089 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10090 if (buttons == NULL)
10091 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010092 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010094 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010095 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010097 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10098 if (typestr == NULL)
10099 error = TRUE;
10100 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 switch (TOUPPER_ASC(*typestr))
10103 {
10104 case 'E': type = VIM_ERROR; break;
10105 case 'Q': type = VIM_QUESTION; break;
10106 case 'I': type = VIM_INFO; break;
10107 case 'W': type = VIM_WARNING; break;
10108 case 'G': type = VIM_GENERIC; break;
10109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 }
10111 }
10112 }
10113 }
10114
10115 if (buttons == NULL || *buttons == NUL)
10116 buttons = (char_u *)_("&Ok");
10117
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010118 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010119 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010120 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121#endif
10122}
10123
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010124/*
10125 * "copy()" function
10126 */
10127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010128f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010129 typval_T *argvars;
10130 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010131{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010132 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010133}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010135#ifdef FEAT_FLOAT
10136/*
10137 * "cos()" function
10138 */
10139 static void
10140f_cos(argvars, rettv)
10141 typval_T *argvars;
10142 typval_T *rettv;
10143{
10144 float_T f;
10145
10146 rettv->v_type = VAR_FLOAT;
10147 if (get_float_arg(argvars, &f) == OK)
10148 rettv->vval.v_float = cos(f);
10149 else
10150 rettv->vval.v_float = 0.0;
10151}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010152
10153/*
10154 * "cosh()" function
10155 */
10156 static void
10157f_cosh(argvars, rettv)
10158 typval_T *argvars;
10159 typval_T *rettv;
10160{
10161 float_T f;
10162
10163 rettv->v_type = VAR_FLOAT;
10164 if (get_float_arg(argvars, &f) == OK)
10165 rettv->vval.v_float = cosh(f);
10166 else
10167 rettv->vval.v_float = 0.0;
10168}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010169#endif
10170
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010172 * "count()" function
10173 */
10174 static void
10175f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010176 typval_T *argvars;
10177 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010178{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010179 long n = 0;
10180 int ic = FALSE;
10181
Bram Moolenaare9a41262005-01-15 22:18:47 +000010182 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010183 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010184 listitem_T *li;
10185 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010186 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010187
Bram Moolenaare9a41262005-01-15 22:18:47 +000010188 if ((l = argvars[0].vval.v_list) != NULL)
10189 {
10190 li = l->lv_first;
10191 if (argvars[2].v_type != VAR_UNKNOWN)
10192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010193 int error = FALSE;
10194
10195 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010196 if (argvars[3].v_type != VAR_UNKNOWN)
10197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 idx = get_tv_number_chk(&argvars[3], &error);
10199 if (!error)
10200 {
10201 li = list_find(l, idx);
10202 if (li == NULL)
10203 EMSGN(_(e_listidx), idx);
10204 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010205 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010206 if (error)
10207 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010208 }
10209
10210 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010211 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010212 ++n;
10213 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010214 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010215 else if (argvars[0].v_type == VAR_DICT)
10216 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010217 int todo;
10218 dict_T *d;
10219 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010220
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010221 if ((d = argvars[0].vval.v_dict) != NULL)
10222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 int error = FALSE;
10224
Bram Moolenaare9a41262005-01-15 22:18:47 +000010225 if (argvars[2].v_type != VAR_UNKNOWN)
10226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010227 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010228 if (argvars[3].v_type != VAR_UNKNOWN)
10229 EMSG(_(e_invarg));
10230 }
10231
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010232 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010233 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010234 {
10235 if (!HASHITEM_EMPTY(hi))
10236 {
10237 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010238 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010239 ++n;
10240 }
10241 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010242 }
10243 }
10244 else
10245 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010246 rettv->vval.v_number = n;
10247}
10248
10249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10251 *
10252 * Checks the existence of a cscope connection.
10253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010255f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010256 typval_T *argvars UNUSED;
10257 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258{
10259#ifdef FEAT_CSCOPE
10260 int num = 0;
10261 char_u *dbpath = NULL;
10262 char_u *prepend = NULL;
10263 char_u buf[NUMBUFLEN];
10264
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010265 if (argvars[0].v_type != VAR_UNKNOWN
10266 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010268 num = (int)get_tv_number(&argvars[0]);
10269 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010270 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010271 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 }
10273
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010274 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275#endif
10276}
10277
10278/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010279 * "cursor(lnum, col)" function, or
10280 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010282 * Moves the cursor to the specified line and column.
10283 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010286f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010287 typval_T *argvars;
10288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289{
10290 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010291#ifdef FEAT_VIRTUALEDIT
10292 long coladd = 0;
10293#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010294 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010296 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010297 if (argvars[1].v_type == VAR_UNKNOWN)
10298 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010299 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010300 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010301
Bram Moolenaar493c1782014-05-28 14:34:46 +020010302 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010303 {
10304 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010305 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010306 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010307 line = pos.lnum;
10308 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010309#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010310 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010311#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010312 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010313 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010314 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010315 set_curswant = FALSE;
10316 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010317 }
10318 else
10319 {
10320 line = get_tv_lnum(argvars);
10321 col = get_tv_number_chk(&argvars[1], NULL);
10322#ifdef FEAT_VIRTUALEDIT
10323 if (argvars[2].v_type != VAR_UNKNOWN)
10324 coladd = get_tv_number_chk(&argvars[2], NULL);
10325#endif
10326 }
10327 if (line < 0 || col < 0
10328#ifdef FEAT_VIRTUALEDIT
10329 || coladd < 0
10330#endif
10331 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010332 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 if (line > 0)
10334 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 if (col > 0)
10336 curwin->w_cursor.col = col - 1;
10337#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010338 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339#endif
10340
10341 /* Make sure the cursor is in a valid position. */
10342 check_cursor();
10343#ifdef FEAT_MBYTE
10344 /* Correct cursor for multi-byte character. */
10345 if (has_mbyte)
10346 mb_adjust_cursor();
10347#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010348
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010349 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010350 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351}
10352
10353/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010354 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 */
10356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010357f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010358 typval_T *argvars;
10359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010361 int noref = 0;
10362
10363 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010364 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010365 if (noref < 0 || noref > 1)
10366 EMSG(_(e_invarg));
10367 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010368 {
10369 current_copyID += COPYID_INC;
10370 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372}
10373
10374/*
10375 * "delete()" function
10376 */
10377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010378f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010379 typval_T *argvars;
10380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010382 char_u nbuf[NUMBUFLEN];
10383 char_u *name;
10384 char_u *flags;
10385
10386 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010388 return;
10389
10390 name = get_tv_string(&argvars[0]);
10391 if (name == NULL || *name == NUL)
10392 {
10393 EMSG(_(e_invarg));
10394 return;
10395 }
10396
10397 if (argvars[1].v_type != VAR_UNKNOWN)
10398 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010400 flags = (char_u *)"";
10401
10402 if (*flags == NUL)
10403 /* delete a file */
10404 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10405 else if (STRCMP(flags, "d") == 0)
10406 /* delete an empty directory */
10407 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10408 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010409 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010410 rettv->vval.v_number = delete_recursive(name);
10411 else
10412 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413}
10414
10415/*
10416 * "did_filetype()" function
10417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010419f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010420 typval_T *argvars UNUSED;
10421 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422{
10423#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010424 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425#endif
10426}
10427
10428/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010429 * "diff_filler()" function
10430 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010432f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010433 typval_T *argvars UNUSED;
10434 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010435{
10436#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010437 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010438#endif
10439}
10440
10441/*
10442 * "diff_hlID()" function
10443 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010445f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010446 typval_T *argvars UNUSED;
10447 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010448{
10449#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010450 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010451 static linenr_T prev_lnum = 0;
10452 static int changedtick = 0;
10453 static int fnum = 0;
10454 static int change_start = 0;
10455 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010456 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010457 int filler_lines;
10458 int col;
10459
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010460 if (lnum < 0) /* ignore type error in {lnum} arg */
10461 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010462 if (lnum != prev_lnum
10463 || changedtick != curbuf->b_changedtick
10464 || fnum != curbuf->b_fnum)
10465 {
10466 /* New line, buffer, change: need to get the values. */
10467 filler_lines = diff_check(curwin, lnum);
10468 if (filler_lines < 0)
10469 {
10470 if (filler_lines == -1)
10471 {
10472 change_start = MAXCOL;
10473 change_end = -1;
10474 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10475 hlID = HLF_ADD; /* added line */
10476 else
10477 hlID = HLF_CHD; /* changed line */
10478 }
10479 else
10480 hlID = HLF_ADD; /* added line */
10481 }
10482 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010483 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010484 prev_lnum = lnum;
10485 changedtick = curbuf->b_changedtick;
10486 fnum = curbuf->b_fnum;
10487 }
10488
10489 if (hlID == HLF_CHD || hlID == HLF_TXD)
10490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010491 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010492 if (col >= change_start && col <= change_end)
10493 hlID = HLF_TXD; /* changed text */
10494 else
10495 hlID = HLF_CHD; /* changed line */
10496 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010497 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010498#endif
10499}
10500
10501/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010502 * "empty({expr})" function
10503 */
10504 static void
10505f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010506 typval_T *argvars;
10507 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010508{
10509 int n;
10510
10511 switch (argvars[0].v_type)
10512 {
10513 case VAR_STRING:
10514 case VAR_FUNC:
10515 n = argvars[0].vval.v_string == NULL
10516 || *argvars[0].vval.v_string == NUL;
10517 break;
10518 case VAR_NUMBER:
10519 n = argvars[0].vval.v_number == 0;
10520 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010521#ifdef FEAT_FLOAT
10522 case VAR_FLOAT:
10523 n = argvars[0].vval.v_float == 0.0;
10524 break;
10525#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010526 case VAR_LIST:
10527 n = argvars[0].vval.v_list == NULL
10528 || argvars[0].vval.v_list->lv_first == NULL;
10529 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010530 case VAR_DICT:
10531 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010532 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010533 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010534 default:
10535 EMSG2(_(e_intern2), "f_empty()");
10536 n = 0;
10537 }
10538
10539 rettv->vval.v_number = n;
10540}
10541
10542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 * "escape({string}, {chars})" function
10544 */
10545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010547 typval_T *argvars;
10548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549{
10550 char_u buf[NUMBUFLEN];
10551
Bram Moolenaar758711c2005-02-02 23:11:38 +000010552 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10553 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010554 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555}
10556
10557/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010558 * "eval()" function
10559 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010560 static void
10561f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010562 typval_T *argvars;
10563 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010564{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010565 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010567 s = get_tv_string_chk(&argvars[0]);
10568 if (s != NULL)
10569 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010570
Bram Moolenaar615b9972015-01-14 17:15:05 +010010571 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010572 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10573 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010574 if (p != NULL && !aborting())
10575 EMSG2(_(e_invexpr2), p);
10576 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010578 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010579 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010580 else if (*s != NUL)
10581 EMSG(_(e_trailing));
10582}
10583
10584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 * "eventhandler()" function
10586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010588f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010589 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010592 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593}
10594
10595/*
10596 * "executable()" function
10597 */
10598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010599f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010600 typval_T *argvars;
10601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010603 char_u *name = get_tv_string(&argvars[0]);
10604
10605 /* Check in $PATH and also check directly if there is a directory name. */
10606 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10607 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010608}
10609
10610/*
10611 * "exepath()" function
10612 */
10613 static void
10614f_exepath(argvars, rettv)
10615 typval_T *argvars;
10616 typval_T *rettv;
10617{
10618 char_u *p = NULL;
10619
Bram Moolenaarb5971142015-03-21 17:32:19 +010010620 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010621 rettv->v_type = VAR_STRING;
10622 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623}
10624
10625/*
10626 * "exists()" function
10627 */
10628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010630 typval_T *argvars;
10631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632{
10633 char_u *p;
10634 char_u *name;
10635 int n = FALSE;
10636 int len = 0;
10637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010638 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 if (*p == '$') /* environment variable */
10640 {
10641 /* first try "normal" environment variables (fast) */
10642 if (mch_getenv(p + 1) != NULL)
10643 n = TRUE;
10644 else
10645 {
10646 /* try expanding things like $VIM and ${HOME} */
10647 p = expand_env_save(p);
10648 if (p != NULL && *p != '$')
10649 n = TRUE;
10650 vim_free(p);
10651 }
10652 }
10653 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010655 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010656 if (*skipwhite(p) != NUL)
10657 n = FALSE; /* trailing garbage */
10658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 else if (*p == '*') /* internal or user defined function */
10660 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010661 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 }
10663 else if (*p == ':')
10664 {
10665 n = cmd_exists(p + 1);
10666 }
10667 else if (*p == '#')
10668 {
10669#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010670 if (p[1] == '#')
10671 n = autocmd_supported(p + 2);
10672 else
10673 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674#endif
10675 }
10676 else /* internal variable */
10677 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010678 char_u *tofree;
10679 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010681 /* get_name_len() takes care of expanding curly braces */
10682 name = p;
10683 len = get_name_len(&p, &tofree, TRUE, FALSE);
10684 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010686 if (tofree != NULL)
10687 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010688 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010689 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010691 /* handle d.key, l[idx], f(expr) */
10692 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10693 if (n)
10694 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 }
10696 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010697 if (*p != NUL)
10698 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010700 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 }
10702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010703 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704}
10705
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010706#ifdef FEAT_FLOAT
10707/*
10708 * "exp()" function
10709 */
10710 static void
10711f_exp(argvars, rettv)
10712 typval_T *argvars;
10713 typval_T *rettv;
10714{
10715 float_T f;
10716
10717 rettv->v_type = VAR_FLOAT;
10718 if (get_float_arg(argvars, &f) == OK)
10719 rettv->vval.v_float = exp(f);
10720 else
10721 rettv->vval.v_float = 0.0;
10722}
10723#endif
10724
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725/*
10726 * "expand()" function
10727 */
10728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010729f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010730 typval_T *argvars;
10731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732{
10733 char_u *s;
10734 int len;
10735 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010736 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010738 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010739 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010742 if (argvars[1].v_type != VAR_UNKNOWN
10743 && argvars[2].v_type != VAR_UNKNOWN
10744 && get_tv_number_chk(&argvars[2], &error)
10745 && !error)
10746 {
10747 rettv->v_type = VAR_LIST;
10748 rettv->vval.v_list = NULL;
10749 }
10750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010751 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 if (*s == '%' || *s == '#' || *s == '<')
10753 {
10754 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010755 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010757 if (rettv->v_type == VAR_LIST)
10758 {
10759 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10760 list_append_string(rettv->vval.v_list, result, -1);
10761 else
10762 vim_free(result);
10763 }
10764 else
10765 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 }
10767 else
10768 {
10769 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010770 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 if (argvars[1].v_type != VAR_UNKNOWN
10772 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010773 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010774 if (!error)
10775 {
10776 ExpandInit(&xpc);
10777 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010778 if (p_wic)
10779 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010780 if (rettv->v_type == VAR_STRING)
10781 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10782 options, WILD_ALL);
10783 else if (rettv_list_alloc(rettv) != FAIL)
10784 {
10785 int i;
10786
10787 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10788 for (i = 0; i < xpc.xp_numfiles; i++)
10789 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10790 ExpandCleanup(&xpc);
10791 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010792 }
10793 else
10794 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 }
10796}
10797
10798/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010799 * Go over all entries in "d2" and add them to "d1".
10800 * When "action" is "error" then a duplicate key is an error.
10801 * When "action" is "force" then a duplicate key is overwritten.
10802 * Otherwise duplicate keys are ignored ("action" is "keep").
10803 */
10804 void
10805dict_extend(d1, d2, action)
10806 dict_T *d1;
10807 dict_T *d2;
10808 char_u *action;
10809{
10810 dictitem_T *di1;
10811 hashitem_T *hi2;
10812 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010813 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010814
10815 todo = (int)d2->dv_hashtab.ht_used;
10816 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10817 {
10818 if (!HASHITEM_EMPTY(hi2))
10819 {
10820 --todo;
10821 di1 = dict_find(d1, hi2->hi_key, -1);
10822 if (d1->dv_scope != 0)
10823 {
10824 /* Disallow replacing a builtin function in l: and g:.
10825 * Check the key to be valid when adding to any
10826 * scope. */
10827 if (d1->dv_scope == VAR_DEF_SCOPE
10828 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10829 && var_check_func_name(hi2->hi_key,
10830 di1 == NULL))
10831 break;
10832 if (!valid_varname(hi2->hi_key))
10833 break;
10834 }
10835 if (di1 == NULL)
10836 {
10837 di1 = dictitem_copy(HI2DI(hi2));
10838 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10839 dictitem_free(di1);
10840 }
10841 else if (*action == 'e')
10842 {
10843 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10844 break;
10845 }
10846 else if (*action == 'f' && HI2DI(hi2) != di1)
10847 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010848 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10849 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010850 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010851 clear_tv(&di1->di_tv);
10852 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10853 }
10854 }
10855 }
10856}
10857
10858/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010859 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010860 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010861 */
10862 static void
10863f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010864 typval_T *argvars;
10865 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010866{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010867 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010868
Bram Moolenaare9a41262005-01-15 22:18:47 +000010869 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010870 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010871 list_T *l1, *l2;
10872 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010873 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010875
Bram Moolenaare9a41262005-01-15 22:18:47 +000010876 l1 = argvars[0].vval.v_list;
10877 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010878 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010879 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010880 {
10881 if (argvars[2].v_type != VAR_UNKNOWN)
10882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010883 before = get_tv_number_chk(&argvars[2], &error);
10884 if (error)
10885 return; /* type error; errmsg already given */
10886
Bram Moolenaar758711c2005-02-02 23:11:38 +000010887 if (before == l1->lv_len)
10888 item = NULL;
10889 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010890 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010891 item = list_find(l1, before);
10892 if (item == NULL)
10893 {
10894 EMSGN(_(e_listidx), before);
10895 return;
10896 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010897 }
10898 }
10899 else
10900 item = NULL;
10901 list_extend(l1, l2, item);
10902
Bram Moolenaare9a41262005-01-15 22:18:47 +000010903 copy_tv(&argvars[0], rettv);
10904 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010905 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010906 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10907 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010908 dict_T *d1, *d2;
10909 char_u *action;
10910 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010911
10912 d1 = argvars[0].vval.v_dict;
10913 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010914 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010915 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010916 {
10917 /* Check the third argument. */
10918 if (argvars[2].v_type != VAR_UNKNOWN)
10919 {
10920 static char *(av[]) = {"keep", "force", "error"};
10921
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010922 action = get_tv_string_chk(&argvars[2]);
10923 if (action == NULL)
10924 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010925 for (i = 0; i < 3; ++i)
10926 if (STRCMP(action, av[i]) == 0)
10927 break;
10928 if (i == 3)
10929 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010930 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010931 return;
10932 }
10933 }
10934 else
10935 action = (char_u *)"force";
10936
Bram Moolenaara9922d62013-05-30 13:01:18 +020010937 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010938
Bram Moolenaare9a41262005-01-15 22:18:47 +000010939 copy_tv(&argvars[0], rettv);
10940 }
10941 }
10942 else
10943 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010944}
10945
10946/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010947 * "feedkeys()" function
10948 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010949 static void
10950f_feedkeys(argvars, rettv)
10951 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010952 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010953{
10954 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010955 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010956 char_u *keys, *flags;
10957 char_u nbuf[NUMBUFLEN];
10958 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010959 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010960
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010961 /* This is not allowed in the sandbox. If the commands would still be
10962 * executed in the sandbox it would be OK, but it probably happens later,
10963 * when "sandbox" is no longer set. */
10964 if (check_secure())
10965 return;
10966
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010967 keys = get_tv_string(&argvars[0]);
10968 if (*keys != NUL)
10969 {
10970 if (argvars[1].v_type != VAR_UNKNOWN)
10971 {
10972 flags = get_tv_string_buf(&argvars[1], nbuf);
10973 for ( ; *flags != NUL; ++flags)
10974 {
10975 switch (*flags)
10976 {
10977 case 'n': remap = FALSE; break;
10978 case 'm': remap = TRUE; break;
10979 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010980 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010981 }
10982 }
10983 }
10984
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010985 /* Need to escape K_SPECIAL and CSI before putting the string in the
10986 * typeahead buffer. */
10987 keys_esc = vim_strsave_escape_csi(keys);
10988 if (keys_esc != NULL)
10989 {
10990 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010991 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010992 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010993 if (vgetc_busy)
10994 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010995 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010996 }
10997}
10998
10999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 * "filereadable()" function
11001 */
11002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011003f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011004 typval_T *argvars;
11005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011007 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 char_u *p;
11009 int n;
11010
Bram Moolenaarc236c162008-07-13 17:41:49 +000011011#ifndef O_NONBLOCK
11012# define O_NONBLOCK 0
11013#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011015 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11016 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 {
11018 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011019 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 }
11021 else
11022 n = FALSE;
11023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025}
11026
11027/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011028 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 * rights to write into.
11030 */
11031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011032f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011033 typval_T *argvars;
11034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011036 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037}
11038
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011039static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011040
11041 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011042findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011043 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011044 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011045 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011046{
11047#ifdef FEAT_SEARCHPATH
11048 char_u *fname;
11049 char_u *fresult = NULL;
11050 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11051 char_u *p;
11052 char_u pathbuf[NUMBUFLEN];
11053 int count = 1;
11054 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011055 int error = FALSE;
11056#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011057
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011058 rettv->vval.v_string = NULL;
11059 rettv->v_type = VAR_STRING;
11060
11061#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011063
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011064 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11067 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011068 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011069 else
11070 {
11071 if (*p != NUL)
11072 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011074 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011075 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011077 }
11078
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011079 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11080 error = TRUE;
11081
11082 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011084 do
11085 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011086 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011087 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011088 fresult = find_file_in_path_option(first ? fname : NULL,
11089 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011090 0, first, path,
11091 find_what,
11092 curbuf->b_ffname,
11093 find_what == FINDFILE_DIR
11094 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011096
11097 if (fresult != NULL && rettv->v_type == VAR_LIST)
11098 list_append_string(rettv->vval.v_list, fresult, -1);
11099
11100 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011101 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011102
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011103 if (rettv->v_type == VAR_STRING)
11104 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011105#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011106}
11107
Bram Moolenaar33570922005-01-25 22:26:29 +000011108static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
11109static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011110
11111/*
11112 * Implementation of map() and filter().
11113 */
11114 static void
11115filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011116 typval_T *argvars;
11117 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011118 int map;
11119{
11120 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011121 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011122 listitem_T *li, *nli;
11123 list_T *l = NULL;
11124 dictitem_T *di;
11125 hashtab_T *ht;
11126 hashitem_T *hi;
11127 dict_T *d = NULL;
11128 typval_T save_val;
11129 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011130 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011131 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011132 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011133 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011134 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011135 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011136 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011137
Bram Moolenaare9a41262005-01-15 22:18:47 +000011138 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011139 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011140 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011141 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011142 return;
11143 }
11144 else if (argvars[0].v_type == VAR_DICT)
11145 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011146 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011147 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011148 return;
11149 }
11150 else
11151 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011152 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011153 return;
11154 }
11155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011156 expr = get_tv_string_buf_chk(&argvars[1], buf);
11157 /* On type errors, the preceding call has already displayed an error
11158 * message. Avoid a misleading error message for an empty string that
11159 * was not passed as argument. */
11160 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011162 prepare_vimvar(VV_VAL, &save_val);
11163 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011164
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011165 /* We reset "did_emsg" to be able to detect whether an error
11166 * occurred during evaluation of the expression. */
11167 save_did_emsg = did_emsg;
11168 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011169
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011170 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011171 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011172 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011173 vimvars[VV_KEY].vv_type = VAR_STRING;
11174
11175 ht = &d->dv_hashtab;
11176 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011177 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011178 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011180 if (!HASHITEM_EMPTY(hi))
11181 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011182 int r;
11183
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011184 --todo;
11185 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011186 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011187 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11188 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011189 break;
11190 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011191 r = filter_map_one(&di->di_tv, expr, map, &rem);
11192 clear_tv(&vimvars[VV_KEY].vv_tv);
11193 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011194 break;
11195 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011196 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011197 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11198 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011199 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011200 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011201 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011202 }
11203 }
11204 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011205 }
11206 else
11207 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011208 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011210 for (li = l->lv_first; li != NULL; li = nli)
11211 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011212 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011213 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011214 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011215 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011216 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011217 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011218 break;
11219 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011220 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011221 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011222 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011223 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011224
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011225 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011226 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011227
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011228 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011229 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011230
11231 copy_tv(&argvars[0], rettv);
11232}
11233
11234 static int
11235filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011236 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011237 char_u *expr;
11238 int map;
11239 int *remp;
11240{
Bram Moolenaar33570922005-01-25 22:26:29 +000011241 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011242 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011243 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011244
Bram Moolenaar33570922005-01-25 22:26:29 +000011245 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011246 s = expr;
11247 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011248 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011249 if (*s != NUL) /* check for trailing chars after expr */
11250 {
11251 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011252 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011253 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011254 }
11255 if (map)
11256 {
11257 /* map(): replace the list item value */
11258 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011259 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011260 *tv = rettv;
11261 }
11262 else
11263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011264 int error = FALSE;
11265
Bram Moolenaare9a41262005-01-15 22:18:47 +000011266 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011267 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011268 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011269 /* On type error, nothing has been removed; return FAIL to stop the
11270 * loop. The error message was given by get_tv_number_chk(). */
11271 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011272 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011273 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011274 retval = OK;
11275theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011276 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011277 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011278}
11279
11280/*
11281 * "filter()" function
11282 */
11283 static void
11284f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011285 typval_T *argvars;
11286 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011287{
11288 filter_map(argvars, rettv, FALSE);
11289}
11290
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011291/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011292 * "finddir({fname}[, {path}[, {count}]])" function
11293 */
11294 static void
11295f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 typval_T *argvars;
11297 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011298{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011299 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011300}
11301
11302/*
11303 * "findfile({fname}[, {path}[, {count}]])" function
11304 */
11305 static void
11306f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011307 typval_T *argvars;
11308 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011309{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011310 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011311}
11312
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011313#ifdef FEAT_FLOAT
11314/*
11315 * "float2nr({float})" function
11316 */
11317 static void
11318f_float2nr(argvars, rettv)
11319 typval_T *argvars;
11320 typval_T *rettv;
11321{
11322 float_T f;
11323
11324 if (get_float_arg(argvars, &f) == OK)
11325 {
11326 if (f < -0x7fffffff)
11327 rettv->vval.v_number = -0x7fffffff;
11328 else if (f > 0x7fffffff)
11329 rettv->vval.v_number = 0x7fffffff;
11330 else
11331 rettv->vval.v_number = (varnumber_T)f;
11332 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011333}
11334
11335/*
11336 * "floor({float})" function
11337 */
11338 static void
11339f_floor(argvars, rettv)
11340 typval_T *argvars;
11341 typval_T *rettv;
11342{
11343 float_T f;
11344
11345 rettv->v_type = VAR_FLOAT;
11346 if (get_float_arg(argvars, &f) == OK)
11347 rettv->vval.v_float = floor(f);
11348 else
11349 rettv->vval.v_float = 0.0;
11350}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011351
11352/*
11353 * "fmod()" function
11354 */
11355 static void
11356f_fmod(argvars, rettv)
11357 typval_T *argvars;
11358 typval_T *rettv;
11359{
11360 float_T fx, fy;
11361
11362 rettv->v_type = VAR_FLOAT;
11363 if (get_float_arg(argvars, &fx) == OK
11364 && get_float_arg(&argvars[1], &fy) == OK)
11365 rettv->vval.v_float = fmod(fx, fy);
11366 else
11367 rettv->vval.v_float = 0.0;
11368}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011369#endif
11370
Bram Moolenaar0d660222005-01-07 21:51:51 +000011371/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011372 * "fnameescape({string})" function
11373 */
11374 static void
11375f_fnameescape(argvars, rettv)
11376 typval_T *argvars;
11377 typval_T *rettv;
11378{
11379 rettv->vval.v_string = vim_strsave_fnameescape(
11380 get_tv_string(&argvars[0]), FALSE);
11381 rettv->v_type = VAR_STRING;
11382}
11383
11384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385 * "fnamemodify({fname}, {mods})" function
11386 */
11387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011389 typval_T *argvars;
11390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391{
11392 char_u *fname;
11393 char_u *mods;
11394 int usedlen = 0;
11395 int len;
11396 char_u *fbuf = NULL;
11397 char_u buf[NUMBUFLEN];
11398
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011399 fname = get_tv_string_chk(&argvars[0]);
11400 mods = get_tv_string_buf_chk(&argvars[1], buf);
11401 if (fname == NULL || mods == NULL)
11402 fname = NULL;
11403 else
11404 {
11405 len = (int)STRLEN(fname);
11406 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011409 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 vim_free(fbuf);
11415}
11416
Bram Moolenaar33570922005-01-25 22:26:29 +000011417static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418
11419/*
11420 * "foldclosed()" function
11421 */
11422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011423foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011424 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011425 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011426 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427{
11428#ifdef FEAT_FOLDING
11429 linenr_T lnum;
11430 linenr_T first, last;
11431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011432 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11434 {
11435 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11436 {
11437 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011438 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 return;
11442 }
11443 }
11444#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446}
11447
11448/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011449 * "foldclosed()" function
11450 */
11451 static void
11452f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011453 typval_T *argvars;
11454 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011455{
11456 foldclosed_both(argvars, rettv, FALSE);
11457}
11458
11459/*
11460 * "foldclosedend()" function
11461 */
11462 static void
11463f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011464 typval_T *argvars;
11465 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011466{
11467 foldclosed_both(argvars, rettv, TRUE);
11468}
11469
11470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 * "foldlevel()" function
11472 */
11473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011475 typval_T *argvars UNUSED;
11476 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477{
11478#ifdef FEAT_FOLDING
11479 linenr_T lnum;
11480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011481 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485}
11486
11487/*
11488 * "foldtext()" function
11489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011492 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494{
11495#ifdef FEAT_FOLDING
11496 linenr_T lnum;
11497 char_u *s;
11498 char_u *r;
11499 int len;
11500 char *txt;
11501#endif
11502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503 rettv->v_type = VAR_STRING;
11504 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011506 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11507 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11508 <= curbuf->b_ml.ml_line_count
11509 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 {
11511 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011512 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11513 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514 {
11515 if (!linewhite(lnum))
11516 break;
11517 ++lnum;
11518 }
11519
11520 /* Find interesting text in this line. */
11521 s = skipwhite(ml_get(lnum));
11522 /* skip C comment-start */
11523 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011526 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011527 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011528 {
11529 s = skipwhite(ml_get(lnum + 1));
11530 if (*s == '*')
11531 s = skipwhite(s + 1);
11532 }
11533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 txt = _("+-%s%3ld lines: ");
11535 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011536 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 + 20 /* for %3ld */
11538 + STRLEN(s))); /* concatenated */
11539 if (r != NULL)
11540 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011541 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11542 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11543 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 len = (int)STRLEN(r);
11545 STRCAT(r, s);
11546 /* remove 'foldmarker' and 'commentstring' */
11547 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 }
11550 }
11551#endif
11552}
11553
11554/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011555 * "foldtextresult(lnum)" function
11556 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011559 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011560 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011561{
11562#ifdef FEAT_FOLDING
11563 linenr_T lnum;
11564 char_u *text;
11565 char_u buf[51];
11566 foldinfo_T foldinfo;
11567 int fold_count;
11568#endif
11569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570 rettv->v_type = VAR_STRING;
11571 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011572#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011573 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011574 /* treat illegal types and illegal string values for {lnum} the same */
11575 if (lnum < 0)
11576 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011577 fold_count = foldedCount(curwin, lnum, &foldinfo);
11578 if (fold_count > 0)
11579 {
11580 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11581 &foldinfo, buf);
11582 if (text == buf)
11583 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011585 }
11586#endif
11587}
11588
11589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 * "foreground()" function
11591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011594 typval_T *argvars UNUSED;
11595 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597#ifdef FEAT_GUI
11598 if (gui.in_use)
11599 gui_mch_set_foreground();
11600#else
11601# ifdef WIN32
11602 win32_set_foreground();
11603# endif
11604#endif
11605}
11606
11607/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011608 * "function()" function
11609 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011611f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011612 typval_T *argvars;
11613 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011614{
11615 char_u *s;
11616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011617 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011618 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011619 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011620 /* Don't check an autoload name for existence here. */
11621 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011622 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011623 else
11624 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011625 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011626 {
11627 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011628 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011629
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011630 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11631 * also be called from another script. Using trans_function_name()
11632 * would also work, but some plugins depend on the name being
11633 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011634 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011635 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011636 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011637 if (rettv->vval.v_string != NULL)
11638 {
11639 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011640 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011641 }
11642 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011643 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011644 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011645 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011646 }
11647}
11648
11649/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011650 * "garbagecollect()" function
11651 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011652 static void
11653f_garbagecollect(argvars, rettv)
11654 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011655 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011656{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011657 /* This is postponed until we are back at the toplevel, because we may be
11658 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11659 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011660
11661 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11662 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011663}
11664
11665/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011666 * "get()" function
11667 */
11668 static void
11669f_get(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{
Bram Moolenaar33570922005-01-25 22:26:29 +000011673 listitem_T *li;
11674 list_T *l;
11675 dictitem_T *di;
11676 dict_T *d;
11677 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011678
Bram Moolenaare9a41262005-01-15 22:18:47 +000011679 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011680 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011681 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011683 int error = FALSE;
11684
11685 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11686 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011687 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011688 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011689 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011690 else if (argvars[0].v_type == VAR_DICT)
11691 {
11692 if ((d = argvars[0].vval.v_dict) != NULL)
11693 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011694 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011695 if (di != NULL)
11696 tv = &di->di_tv;
11697 }
11698 }
11699 else
11700 EMSG2(_(e_listdictarg), "get()");
11701
11702 if (tv == NULL)
11703 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011704 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011705 copy_tv(&argvars[2], rettv);
11706 }
11707 else
11708 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011709}
11710
Bram Moolenaar342337a2005-07-21 21:11:17 +000011711static 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 +000011712
11713/*
11714 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011715 * Return a range (from start to end) of lines in rettv from the specified
11716 * buffer.
11717 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011718 */
11719 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011720get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011721 buf_T *buf;
11722 linenr_T start;
11723 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011724 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011725 typval_T *rettv;
11726{
11727 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011728
Bram Moolenaar959a1432013-12-14 12:17:38 +010011729 rettv->v_type = VAR_STRING;
11730 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011731 if (retlist && rettv_list_alloc(rettv) == FAIL)
11732 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011733
11734 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11735 return;
11736
11737 if (!retlist)
11738 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011739 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11740 p = ml_get_buf(buf, start, FALSE);
11741 else
11742 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011743 rettv->vval.v_string = vim_strsave(p);
11744 }
11745 else
11746 {
11747 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011748 return;
11749
11750 if (start < 1)
11751 start = 1;
11752 if (end > buf->b_ml.ml_line_count)
11753 end = buf->b_ml.ml_line_count;
11754 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011755 if (list_append_string(rettv->vval.v_list,
11756 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011757 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011758 }
11759}
11760
11761/*
11762 * "getbufline()" function
11763 */
11764 static void
11765f_getbufline(argvars, rettv)
11766 typval_T *argvars;
11767 typval_T *rettv;
11768{
11769 linenr_T lnum;
11770 linenr_T end;
11771 buf_T *buf;
11772
11773 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11774 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011775 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011776 --emsg_off;
11777
Bram Moolenaar661b1822005-07-28 22:36:45 +000011778 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011779 if (argvars[2].v_type == VAR_UNKNOWN)
11780 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011781 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011782 end = get_tv_lnum_buf(&argvars[2], buf);
11783
Bram Moolenaar342337a2005-07-21 21:11:17 +000011784 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011785}
11786
Bram Moolenaar0d660222005-01-07 21:51:51 +000011787/*
11788 * "getbufvar()" function
11789 */
11790 static void
11791f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011792 typval_T *argvars;
11793 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011794{
11795 buf_T *buf;
11796 buf_T *save_curbuf;
11797 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011798 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011799 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011801 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11802 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011803 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011804 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011805
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011806 rettv->v_type = VAR_STRING;
11807 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011808
11809 if (buf != NULL && varname != NULL)
11810 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011811 /* set curbuf to be our buf, temporarily */
11812 save_curbuf = curbuf;
11813 curbuf = buf;
11814
Bram Moolenaar0d660222005-01-07 21:51:51 +000011815 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011816 {
11817 if (get_option_tv(&varname, rettv, TRUE) == OK)
11818 done = TRUE;
11819 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011820 else if (STRCMP(varname, "changedtick") == 0)
11821 {
11822 rettv->v_type = VAR_NUMBER;
11823 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011824 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011825 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011826 else
11827 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011828 /* Look up the variable. */
11829 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11830 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11831 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011832 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011833 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011834 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011835 done = TRUE;
11836 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011837 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011838
11839 /* restore previous notion of curbuf */
11840 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011841 }
11842
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011843 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11844 /* use the default value */
11845 copy_tv(&argvars[2], rettv);
11846
Bram Moolenaar0d660222005-01-07 21:51:51 +000011847 --emsg_off;
11848}
11849
11850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 * "getchar()" function
11852 */
11853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011855 typval_T *argvars;
11856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857{
11858 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011859 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011861 /* Position the cursor. Needed after a message that ends in a space. */
11862 windgoto(msg_row, msg_col);
11863
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 ++no_mapping;
11865 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011866 for (;;)
11867 {
11868 if (argvars[0].v_type == VAR_UNKNOWN)
11869 /* getchar(): blocking wait. */
11870 n = safe_vgetc();
11871 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11872 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011873 n = vpeekc_any();
11874 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011875 /* illegal argument or getchar(0) and no char avail: return zero */
11876 n = 0;
11877 else
11878 /* getchar(0) and char avail: return char */
11879 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011880
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011881 if (n == K_IGNORE)
11882 continue;
11883 break;
11884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885 --no_mapping;
11886 --allow_keys;
11887
Bram Moolenaar219b8702006-11-01 14:32:36 +000011888 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11889 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11890 vimvars[VV_MOUSE_COL].vv_nr = 0;
11891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011892 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893 if (IS_SPECIAL(n) || mod_mask != 0)
11894 {
11895 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11896 int i = 0;
11897
11898 /* Turn a special key into three bytes, plus modifier. */
11899 if (mod_mask != 0)
11900 {
11901 temp[i++] = K_SPECIAL;
11902 temp[i++] = KS_MODIFIER;
11903 temp[i++] = mod_mask;
11904 }
11905 if (IS_SPECIAL(n))
11906 {
11907 temp[i++] = K_SPECIAL;
11908 temp[i++] = K_SECOND(n);
11909 temp[i++] = K_THIRD(n);
11910 }
11911#ifdef FEAT_MBYTE
11912 else if (has_mbyte)
11913 i += (*mb_char2bytes)(n, temp + i);
11914#endif
11915 else
11916 temp[i++] = n;
11917 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011918 rettv->v_type = VAR_STRING;
11919 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011920
11921#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011922 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011923 {
11924 int row = mouse_row;
11925 int col = mouse_col;
11926 win_T *win;
11927 linenr_T lnum;
11928# ifdef FEAT_WINDOWS
11929 win_T *wp;
11930# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011931 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011932
11933 if (row >= 0 && col >= 0)
11934 {
11935 /* Find the window at the mouse coordinates and compute the
11936 * text position. */
11937 win = mouse_find_win(&row, &col);
11938 (void)mouse_comp_pos(win, &row, &col, &lnum);
11939# ifdef FEAT_WINDOWS
11940 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011941 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011942# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011943 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011944 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11945 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11946 }
11947 }
11948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 }
11950}
11951
11952/*
11953 * "getcharmod()" function
11954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011956f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011957 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011960 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961}
11962
11963/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011964 * "getcharsearch()" function
11965 */
11966 static void
11967f_getcharsearch(argvars, rettv)
11968 typval_T *argvars UNUSED;
11969 typval_T *rettv;
11970{
11971 if (rettv_dict_alloc(rettv) != FAIL)
11972 {
11973 dict_T *dict = rettv->vval.v_dict;
11974
11975 dict_add_nr_str(dict, "char", 0L, last_csearch());
11976 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11977 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11978 }
11979}
11980
11981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982 * "getcmdline()" function
11983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011985f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011986 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->v_type = VAR_STRING;
11990 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991}
11992
11993/*
11994 * "getcmdpos()" function
11995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011998 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002}
12003
12004/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012005 * "getcmdtype()" function
12006 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012007 static void
12008f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012009 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012010 typval_T *rettv;
12011{
12012 rettv->v_type = VAR_STRING;
12013 rettv->vval.v_string = alloc(2);
12014 if (rettv->vval.v_string != NULL)
12015 {
12016 rettv->vval.v_string[0] = get_cmdline_type();
12017 rettv->vval.v_string[1] = NUL;
12018 }
12019}
12020
12021/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012022 * "getcmdwintype()" function
12023 */
12024 static void
12025f_getcmdwintype(argvars, rettv)
12026 typval_T *argvars UNUSED;
12027 typval_T *rettv;
12028{
12029 rettv->v_type = VAR_STRING;
12030 rettv->vval.v_string = NULL;
12031#ifdef FEAT_CMDWIN
12032 rettv->vval.v_string = alloc(2);
12033 if (rettv->vval.v_string != NULL)
12034 {
12035 rettv->vval.v_string[0] = cmdwin_type;
12036 rettv->vval.v_string[1] = NUL;
12037 }
12038#endif
12039}
12040
12041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042 * "getcwd()" function
12043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012045f_getcwd(argvars, rettv)
Bram Moolenaarc9703302016-01-17 21:49:33 +010012046 typval_T *argvars;
Bram Moolenaar33570922005-01-25 22:26:29 +000012047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012049 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012050 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012053 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012054
12055 wp = find_tabwin(&argvars[0], &argvars[1]);
12056 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012058 if (wp->w_localdir != NULL)
12059 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12060 else if(globaldir != NULL)
12061 rettv->vval.v_string = vim_strsave(globaldir);
12062 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012063 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012064 cwd = alloc(MAXPATHL);
12065 if (cwd != NULL)
12066 {
12067 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12068 rettv->vval.v_string = vim_strsave(cwd);
12069 vim_free(cwd);
12070 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012071 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012072#ifdef BACKSLASH_IN_FILENAME
12073 if (rettv->vval.v_string != NULL)
12074 slash_adjust(rettv->vval.v_string);
12075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 }
12077}
12078
12079/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012080 * "getfontname()" function
12081 */
12082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012083f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012084 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012085 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012086{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087 rettv->v_type = VAR_STRING;
12088 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012089#ifdef FEAT_GUI
12090 if (gui.in_use)
12091 {
12092 GuiFont font;
12093 char_u *name = NULL;
12094
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012095 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012096 {
12097 /* Get the "Normal" font. Either the name saved by
12098 * hl_set_font_name() or from the font ID. */
12099 font = gui.norm_font;
12100 name = hl_get_font_name();
12101 }
12102 else
12103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012104 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012105 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12106 return;
12107 font = gui_mch_get_font(name, FALSE);
12108 if (font == NOFONT)
12109 return; /* Invalid font name, return empty string. */
12110 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012111 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012112 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012113 gui_mch_free_font(font);
12114 }
12115#endif
12116}
12117
12118/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012119 * "getfperm({fname})" function
12120 */
12121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012122f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012123 typval_T *argvars;
12124 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012125{
12126 char_u *fname;
12127 struct stat st;
12128 char_u *perm = NULL;
12129 char_u flags[] = "rwx";
12130 int i;
12131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012132 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012135 if (mch_stat((char *)fname, &st) >= 0)
12136 {
12137 perm = vim_strsave((char_u *)"---------");
12138 if (perm != NULL)
12139 {
12140 for (i = 0; i < 9; i++)
12141 {
12142 if (st.st_mode & (1 << (8 - i)))
12143 perm[i] = flags[i % 3];
12144 }
12145 }
12146 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012148}
12149
12150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 * "getfsize({fname})" function
12152 */
12153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012154f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012155 typval_T *argvars;
12156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157{
12158 char_u *fname;
12159 struct stat st;
12160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012161 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012163 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164
12165 if (mch_stat((char *)fname, &st) >= 0)
12166 {
12167 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012168 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012170 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012171 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012172
12173 /* non-perfect check for overflow */
12174 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12175 rettv->vval.v_number = -2;
12176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 }
12178 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012179 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180}
12181
12182/*
12183 * "getftime({fname})" function
12184 */
12185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012186f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012187 typval_T *argvars;
12188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189{
12190 char_u *fname;
12191 struct stat st;
12192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012193 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194
12195 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012196 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012198 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199}
12200
12201/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012202 * "getftype({fname})" function
12203 */
12204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012205f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012206 typval_T *argvars;
12207 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012208{
12209 char_u *fname;
12210 struct stat st;
12211 char_u *type = NULL;
12212 char *t;
12213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012214 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012216 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012217 if (mch_lstat((char *)fname, &st) >= 0)
12218 {
12219#ifdef S_ISREG
12220 if (S_ISREG(st.st_mode))
12221 t = "file";
12222 else if (S_ISDIR(st.st_mode))
12223 t = "dir";
12224# ifdef S_ISLNK
12225 else if (S_ISLNK(st.st_mode))
12226 t = "link";
12227# endif
12228# ifdef S_ISBLK
12229 else if (S_ISBLK(st.st_mode))
12230 t = "bdev";
12231# endif
12232# ifdef S_ISCHR
12233 else if (S_ISCHR(st.st_mode))
12234 t = "cdev";
12235# endif
12236# ifdef S_ISFIFO
12237 else if (S_ISFIFO(st.st_mode))
12238 t = "fifo";
12239# endif
12240# ifdef S_ISSOCK
12241 else if (S_ISSOCK(st.st_mode))
12242 t = "fifo";
12243# endif
12244 else
12245 t = "other";
12246#else
12247# ifdef S_IFMT
12248 switch (st.st_mode & S_IFMT)
12249 {
12250 case S_IFREG: t = "file"; break;
12251 case S_IFDIR: t = "dir"; break;
12252# ifdef S_IFLNK
12253 case S_IFLNK: t = "link"; break;
12254# endif
12255# ifdef S_IFBLK
12256 case S_IFBLK: t = "bdev"; break;
12257# endif
12258# ifdef S_IFCHR
12259 case S_IFCHR: t = "cdev"; break;
12260# endif
12261# ifdef S_IFIFO
12262 case S_IFIFO: t = "fifo"; break;
12263# endif
12264# ifdef S_IFSOCK
12265 case S_IFSOCK: t = "socket"; break;
12266# endif
12267 default: t = "other";
12268 }
12269# else
12270 if (mch_isdir(fname))
12271 t = "dir";
12272 else
12273 t = "file";
12274# endif
12275#endif
12276 type = vim_strsave((char_u *)t);
12277 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012278 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012279}
12280
12281/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012282 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012283 */
12284 static void
12285f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012286 typval_T *argvars;
12287 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012288{
12289 linenr_T lnum;
12290 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012291 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012292
12293 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012294 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012295 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012296 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012297 retlist = FALSE;
12298 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012299 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012300 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012301 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012302 retlist = TRUE;
12303 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012304
Bram Moolenaar342337a2005-07-21 21:11:17 +000012305 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012306}
12307
12308/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012309 * "getmatches()" function
12310 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012311 static void
12312f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012313 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012314 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012315{
12316#ifdef FEAT_SEARCH_EXTRA
12317 dict_T *dict;
12318 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012319 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012320
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012321 if (rettv_list_alloc(rettv) == OK)
12322 {
12323 while (cur != NULL)
12324 {
12325 dict = dict_alloc();
12326 if (dict == NULL)
12327 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012328 if (cur->match.regprog == NULL)
12329 {
12330 /* match added with matchaddpos() */
12331 for (i = 0; i < MAXPOSMATCH; ++i)
12332 {
12333 llpos_T *llpos;
12334 char buf[6];
12335 list_T *l;
12336
12337 llpos = &cur->pos.pos[i];
12338 if (llpos->lnum == 0)
12339 break;
12340 l = list_alloc();
12341 if (l == NULL)
12342 break;
12343 list_append_number(l, (varnumber_T)llpos->lnum);
12344 if (llpos->col > 0)
12345 {
12346 list_append_number(l, (varnumber_T)llpos->col);
12347 list_append_number(l, (varnumber_T)llpos->len);
12348 }
12349 sprintf(buf, "pos%d", i + 1);
12350 dict_add_list(dict, buf, l);
12351 }
12352 }
12353 else
12354 {
12355 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12356 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012357 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012358 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12359 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012360# ifdef FEAT_CONCEAL
12361 if (cur->conceal_char)
12362 {
12363 char_u buf[MB_MAXBYTES + 1];
12364
12365 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12366 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12367 }
12368# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012369 list_append_dict(rettv->vval.v_list, dict);
12370 cur = cur->next;
12371 }
12372 }
12373#endif
12374}
12375
12376/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012377 * "getpid()" function
12378 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012379 static void
12380f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012381 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012382 typval_T *rettv;
12383{
12384 rettv->vval.v_number = mch_get_pid();
12385}
12386
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012387static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12388
12389/*
12390 * "getcurpos()" function
12391 */
12392 static void
12393f_getcurpos(argvars, rettv)
12394 typval_T *argvars;
12395 typval_T *rettv;
12396{
12397 getpos_both(argvars, rettv, TRUE);
12398}
12399
Bram Moolenaar18081e32008-02-20 19:11:07 +000012400/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012401 * "getpos(string)" function
12402 */
12403 static void
12404f_getpos(argvars, rettv)
12405 typval_T *argvars;
12406 typval_T *rettv;
12407{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012408 getpos_both(argvars, rettv, FALSE);
12409}
12410
12411 static void
12412getpos_both(argvars, rettv, getcurpos)
12413 typval_T *argvars;
12414 typval_T *rettv;
12415 int getcurpos;
12416{
Bram Moolenaara5525202006-03-02 22:52:09 +000012417 pos_T *fp;
12418 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012419 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012420
12421 if (rettv_list_alloc(rettv) == OK)
12422 {
12423 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012424 if (getcurpos)
12425 fp = &curwin->w_cursor;
12426 else
12427 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012428 if (fnum != -1)
12429 list_append_number(l, (varnumber_T)fnum);
12430 else
12431 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012432 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12433 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012434 list_append_number(l, (fp != NULL)
12435 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012436 : (varnumber_T)0);
12437 list_append_number(l,
12438#ifdef FEAT_VIRTUALEDIT
12439 (fp != NULL) ? (varnumber_T)fp->coladd :
12440#endif
12441 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012442 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012443 list_append_number(l, curwin->w_curswant == MAXCOL ?
12444 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012445 }
12446 else
12447 rettv->vval.v_number = FALSE;
12448}
12449
12450/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012451 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012452 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012453 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012454f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012455 typval_T *argvars UNUSED;
12456 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012457{
12458#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012459 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012460#endif
12461
Bram Moolenaar2641f772005-03-25 21:58:17 +000012462#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012463 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012464 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012465 wp = NULL;
12466 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12467 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012468 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012469 if (wp == NULL)
12470 return;
12471 }
12472
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012473 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012474 }
12475#endif
12476}
12477
12478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479 * "getreg()" function
12480 */
12481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012482f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012483 typval_T *argvars;
12484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485{
12486 char_u *strregname;
12487 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012488 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012489 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012490 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012492 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012494 strregname = get_tv_string_chk(&argvars[0]);
12495 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012496 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012498 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012499 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12500 return_list = get_tv_number_chk(&argvars[2], &error);
12501 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012504 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012505
12506 if (error)
12507 return;
12508
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 regname = (strregname == NULL ? '"' : *strregname);
12510 if (regname == 0)
12511 regname = '"';
12512
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012513 if (return_list)
12514 {
12515 rettv->v_type = VAR_LIST;
12516 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12517 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012518 if (rettv->vval.v_list != NULL)
12519 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012520 }
12521 else
12522 {
12523 rettv->v_type = VAR_STRING;
12524 rettv->vval.v_string = get_reg_contents(regname,
12525 arg2 ? GREG_EXPR_SRC : 0);
12526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527}
12528
12529/*
12530 * "getregtype()" function
12531 */
12532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012533f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012534 typval_T *argvars;
12535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536{
12537 char_u *strregname;
12538 int regname;
12539 char_u buf[NUMBUFLEN + 2];
12540 long reglen = 0;
12541
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012542 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012543 {
12544 strregname = get_tv_string_chk(&argvars[0]);
12545 if (strregname == NULL) /* type error; errmsg already given */
12546 {
12547 rettv->v_type = VAR_STRING;
12548 rettv->vval.v_string = NULL;
12549 return;
12550 }
12551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552 else
12553 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012554 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555
12556 regname = (strregname == NULL ? '"' : *strregname);
12557 if (regname == 0)
12558 regname = '"';
12559
12560 buf[0] = NUL;
12561 buf[1] = NUL;
12562 switch (get_reg_type(regname, &reglen))
12563 {
12564 case MLINE: buf[0] = 'V'; break;
12565 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566 case MBLOCK:
12567 buf[0] = Ctrl_V;
12568 sprintf((char *)buf + 1, "%ld", reglen + 1);
12569 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012571 rettv->v_type = VAR_STRING;
12572 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573}
12574
12575/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012576 * "gettabvar()" function
12577 */
12578 static void
12579f_gettabvar(argvars, rettv)
12580 typval_T *argvars;
12581 typval_T *rettv;
12582{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012583 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012584 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012585 dictitem_T *v;
12586 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012587 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012588
12589 rettv->v_type = VAR_STRING;
12590 rettv->vval.v_string = NULL;
12591
12592 varname = get_tv_string_chk(&argvars[1]);
12593 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12594 if (tp != NULL && varname != NULL)
12595 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012596 /* Set tp to be our tabpage, temporarily. Also set the window to the
12597 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012598 if (switch_win(&oldcurwin, &oldtabpage,
12599 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012600 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012601 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012602 /* look up the variable */
12603 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12604 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12605 if (v != NULL)
12606 {
12607 copy_tv(&v->di_tv, rettv);
12608 done = TRUE;
12609 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012610 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012611
12612 /* restore previous notion of curwin */
12613 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012614 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012615
12616 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012617 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012618}
12619
12620/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012621 * "gettabwinvar()" function
12622 */
12623 static void
12624f_gettabwinvar(argvars, rettv)
12625 typval_T *argvars;
12626 typval_T *rettv;
12627{
12628 getwinvar(argvars, rettv, 1);
12629}
12630
12631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632 * "getwinposx()" function
12633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012635f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012636 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012639 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640#ifdef FEAT_GUI
12641 if (gui.in_use)
12642 {
12643 int x, y;
12644
12645 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012646 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647 }
12648#endif
12649}
12650
12651/*
12652 * "getwinposy()" function
12653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012655f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012656 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012659 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660#ifdef FEAT_GUI
12661 if (gui.in_use)
12662 {
12663 int x, y;
12664
12665 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012666 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667 }
12668#endif
12669}
12670
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012671/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012672 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012673 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012674 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012675find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012676 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012677 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012678{
12679#ifdef FEAT_WINDOWS
12680 win_T *wp;
12681#endif
12682 int nr;
12683
12684 nr = get_tv_number_chk(vp, NULL);
12685
12686#ifdef FEAT_WINDOWS
12687 if (nr < 0)
12688 return NULL;
12689 if (nr == 0)
12690 return curwin;
12691
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012692 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12693 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012694 if (--nr <= 0)
12695 break;
12696 return wp;
12697#else
12698 if (nr == 0 || nr == 1)
12699 return curwin;
12700 return NULL;
12701#endif
12702}
12703
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012705 * Find window specified by "wvp" in tabpage "tvp".
12706 */
12707 static win_T *
12708find_tabwin(wvp, tvp)
12709 typval_T *wvp; /* VAR_UNKNOWN for current window */
12710 typval_T *tvp; /* VAR_UNKNOWN for current tab page */
12711{
12712 win_T *wp = NULL;
12713 tabpage_T *tp = NULL;
12714 long n;
12715
12716 if (wvp->v_type != VAR_UNKNOWN)
12717 {
12718 if (tvp->v_type != VAR_UNKNOWN)
12719 {
12720 n = get_tv_number(tvp);
12721 if (n >= 0)
12722 tp = find_tabpage(n);
12723 }
12724 else
12725 tp = curtab;
12726
12727 if (tp != NULL)
12728 wp = find_win_by_nr(wvp, tp);
12729 }
12730 else
12731 wp = curwin;
12732
12733 return wp;
12734}
12735
12736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737 * "getwinvar()" function
12738 */
12739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012740f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012741 typval_T *argvars;
12742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012744 getwinvar(argvars, rettv, 0);
12745}
12746
12747/*
12748 * getwinvar() and gettabwinvar()
12749 */
12750 static void
12751getwinvar(argvars, rettv, off)
12752 typval_T *argvars;
12753 typval_T *rettv;
12754 int off; /* 1 for gettabwinvar() */
12755{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012756 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012758 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012759 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012760 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012761#ifdef FEAT_WINDOWS
12762 win_T *oldcurwin;
12763 tabpage_T *oldtabpage;
12764 int need_switch_win;
12765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012767#ifdef FEAT_WINDOWS
12768 if (off == 1)
12769 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12770 else
12771 tp = curtab;
12772#endif
12773 win = find_win_by_nr(&argvars[off], tp);
12774 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012775 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012777 rettv->v_type = VAR_STRING;
12778 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779
12780 if (win != NULL && varname != NULL)
12781 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012782#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012783 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012784 * otherwise the window is not valid. Only do this when needed,
12785 * autocommands get blocked. */
12786 need_switch_win = !(tp == curtab && win == curwin);
12787 if (!need_switch_win
12788 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12789#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012790 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012791 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012792 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012793 if (get_option_tv(&varname, rettv, 1) == OK)
12794 done = TRUE;
12795 }
12796 else
12797 {
12798 /* Look up the variable. */
12799 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12800 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12801 varname, FALSE);
12802 if (v != NULL)
12803 {
12804 copy_tv(&v->di_tv, rettv);
12805 done = TRUE;
12806 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012809
Bram Moolenaarba117c22015-09-29 16:53:22 +020012810#ifdef FEAT_WINDOWS
12811 if (need_switch_win)
12812 /* restore previous notion of curwin */
12813 restore_win(oldcurwin, oldtabpage, TRUE);
12814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815 }
12816
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012817 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12818 /* use the default return value */
12819 copy_tv(&argvars[off + 2], rettv);
12820
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821 --emsg_off;
12822}
12823
12824/*
12825 * "glob()" function
12826 */
12827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012829 typval_T *argvars;
12830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012832 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012834 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012836 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012837 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012838 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012839 if (argvars[1].v_type != VAR_UNKNOWN)
12840 {
12841 if (get_tv_number_chk(&argvars[1], &error))
12842 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012843 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012844 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012845 if (get_tv_number_chk(&argvars[2], &error))
12846 {
12847 rettv->v_type = VAR_LIST;
12848 rettv->vval.v_list = NULL;
12849 }
12850 if (argvars[3].v_type != VAR_UNKNOWN
12851 && get_tv_number_chk(&argvars[3], &error))
12852 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012853 }
12854 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012855 if (!error)
12856 {
12857 ExpandInit(&xpc);
12858 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012859 if (p_wic)
12860 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012861 if (rettv->v_type == VAR_STRING)
12862 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012863 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012864 else if (rettv_list_alloc(rettv) != FAIL)
12865 {
12866 int i;
12867
12868 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12869 NULL, options, WILD_ALL_KEEP);
12870 for (i = 0; i < xpc.xp_numfiles; i++)
12871 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12872
12873 ExpandCleanup(&xpc);
12874 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012875 }
12876 else
12877 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878}
12879
12880/*
12881 * "globpath()" function
12882 */
12883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012885 typval_T *argvars;
12886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012888 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012890 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012891 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012892 garray_T ga;
12893 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012895 /* When the optional second argument is non-zero, don't remove matches
12896 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012898 if (argvars[2].v_type != VAR_UNKNOWN)
12899 {
12900 if (get_tv_number_chk(&argvars[2], &error))
12901 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012902 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012903 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012904 if (get_tv_number_chk(&argvars[3], &error))
12905 {
12906 rettv->v_type = VAR_LIST;
12907 rettv->vval.v_list = NULL;
12908 }
12909 if (argvars[4].v_type != VAR_UNKNOWN
12910 && get_tv_number_chk(&argvars[4], &error))
12911 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012912 }
12913 }
12914 if (file != NULL && !error)
12915 {
12916 ga_init2(&ga, (int)sizeof(char_u *), 10);
12917 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12918 if (rettv->v_type == VAR_STRING)
12919 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12920 else if (rettv_list_alloc(rettv) != FAIL)
12921 for (i = 0; i < ga.ga_len; ++i)
12922 list_append_string(rettv->vval.v_list,
12923 ((char_u **)(ga.ga_data))[i], -1);
12924 ga_clear_strings(&ga);
12925 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012926 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012927 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928}
12929
12930/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012931 * "glob2regpat()" function
12932 */
12933 static void
12934f_glob2regpat(argvars, rettv)
12935 typval_T *argvars;
12936 typval_T *rettv;
12937{
12938 char_u *pat = get_tv_string_chk(&argvars[0]);
12939
12940 rettv->v_type = VAR_STRING;
12941 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12942}
12943
12944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945 * "has()" function
12946 */
12947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012949 typval_T *argvars;
12950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951{
12952 int i;
12953 char_u *name;
12954 int n = FALSE;
12955 static char *(has_list[]) =
12956 {
12957#ifdef AMIGA
12958 "amiga",
12959# ifdef FEAT_ARP
12960 "arp",
12961# endif
12962#endif
12963#ifdef __BEOS__
12964 "beos",
12965#endif
12966#ifdef MSDOS
12967# ifdef DJGPP
12968 "dos32",
12969# else
12970 "dos16",
12971# endif
12972#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012973#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974 "mac",
12975#endif
12976#if defined(MACOS_X_UNIX)
12977 "macunix",
12978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979#ifdef __QNX__
12980 "qnx",
12981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982#ifdef UNIX
12983 "unix",
12984#endif
12985#ifdef VMS
12986 "vms",
12987#endif
12988#ifdef WIN16
12989 "win16",
12990#endif
12991#ifdef WIN32
12992 "win32",
12993#endif
12994#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12995 "win32unix",
12996#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012997#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998 "win64",
12999#endif
13000#ifdef EBCDIC
13001 "ebcdic",
13002#endif
13003#ifndef CASE_INSENSITIVE_FILENAME
13004 "fname_case",
13005#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013006#ifdef HAVE_ACL
13007 "acl",
13008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009#ifdef FEAT_ARABIC
13010 "arabic",
13011#endif
13012#ifdef FEAT_AUTOCMD
13013 "autocmd",
13014#endif
13015#ifdef FEAT_BEVAL
13016 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013017# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13018 "balloon_multiline",
13019# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020#endif
13021#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13022 "builtin_terms",
13023# ifdef ALL_BUILTIN_TCAPS
13024 "all_builtin_terms",
13025# endif
13026#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013027#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13028 || defined(FEAT_GUI_W32) \
13029 || defined(FEAT_GUI_MOTIF))
13030 "browsefilter",
13031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032#ifdef FEAT_BYTEOFF
13033 "byte_offset",
13034#endif
13035#ifdef FEAT_CINDENT
13036 "cindent",
13037#endif
13038#ifdef FEAT_CLIENTSERVER
13039 "clientserver",
13040#endif
13041#ifdef FEAT_CLIPBOARD
13042 "clipboard",
13043#endif
13044#ifdef FEAT_CMDL_COMPL
13045 "cmdline_compl",
13046#endif
13047#ifdef FEAT_CMDHIST
13048 "cmdline_hist",
13049#endif
13050#ifdef FEAT_COMMENTS
13051 "comments",
13052#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013053#ifdef FEAT_CONCEAL
13054 "conceal",
13055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056#ifdef FEAT_CRYPT
13057 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013058 "crypt-blowfish",
13059 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060#endif
13061#ifdef FEAT_CSCOPE
13062 "cscope",
13063#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013064#ifdef FEAT_CURSORBIND
13065 "cursorbind",
13066#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013067#ifdef CURSOR_SHAPE
13068 "cursorshape",
13069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070#ifdef DEBUG
13071 "debug",
13072#endif
13073#ifdef FEAT_CON_DIALOG
13074 "dialog_con",
13075#endif
13076#ifdef FEAT_GUI_DIALOG
13077 "dialog_gui",
13078#endif
13079#ifdef FEAT_DIFF
13080 "diff",
13081#endif
13082#ifdef FEAT_DIGRAPHS
13083 "digraphs",
13084#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013085#ifdef FEAT_DIRECTX
13086 "directx",
13087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088#ifdef FEAT_DND
13089 "dnd",
13090#endif
13091#ifdef FEAT_EMACS_TAGS
13092 "emacs_tags",
13093#endif
13094 "eval", /* always present, of course! */
13095#ifdef FEAT_EX_EXTRA
13096 "ex_extra",
13097#endif
13098#ifdef FEAT_SEARCH_EXTRA
13099 "extra_search",
13100#endif
13101#ifdef FEAT_FKMAP
13102 "farsi",
13103#endif
13104#ifdef FEAT_SEARCHPATH
13105 "file_in_path",
13106#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013107#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013108 "filterpipe",
13109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110#ifdef FEAT_FIND_ID
13111 "find_in_path",
13112#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013113#ifdef FEAT_FLOAT
13114 "float",
13115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116#ifdef FEAT_FOLDING
13117 "folding",
13118#endif
13119#ifdef FEAT_FOOTER
13120 "footer",
13121#endif
13122#if !defined(USE_SYSTEM) && defined(UNIX)
13123 "fork",
13124#endif
13125#ifdef FEAT_GETTEXT
13126 "gettext",
13127#endif
13128#ifdef FEAT_GUI
13129 "gui",
13130#endif
13131#ifdef FEAT_GUI_ATHENA
13132# ifdef FEAT_GUI_NEXTAW
13133 "gui_neXtaw",
13134# else
13135 "gui_athena",
13136# endif
13137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138#ifdef FEAT_GUI_GTK
13139 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013142#ifdef FEAT_GUI_GNOME
13143 "gui_gnome",
13144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145#ifdef FEAT_GUI_MAC
13146 "gui_mac",
13147#endif
13148#ifdef FEAT_GUI_MOTIF
13149 "gui_motif",
13150#endif
13151#ifdef FEAT_GUI_PHOTON
13152 "gui_photon",
13153#endif
13154#ifdef FEAT_GUI_W16
13155 "gui_win16",
13156#endif
13157#ifdef FEAT_GUI_W32
13158 "gui_win32",
13159#endif
13160#ifdef FEAT_HANGULIN
13161 "hangul_input",
13162#endif
13163#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13164 "iconv",
13165#endif
13166#ifdef FEAT_INS_EXPAND
13167 "insert_expand",
13168#endif
13169#ifdef FEAT_JUMPLIST
13170 "jumplist",
13171#endif
13172#ifdef FEAT_KEYMAP
13173 "keymap",
13174#endif
13175#ifdef FEAT_LANGMAP
13176 "langmap",
13177#endif
13178#ifdef FEAT_LIBCALL
13179 "libcall",
13180#endif
13181#ifdef FEAT_LINEBREAK
13182 "linebreak",
13183#endif
13184#ifdef FEAT_LISP
13185 "lispindent",
13186#endif
13187#ifdef FEAT_LISTCMDS
13188 "listcmds",
13189#endif
13190#ifdef FEAT_LOCALMAP
13191 "localmap",
13192#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013193#ifdef FEAT_LUA
13194# ifndef DYNAMIC_LUA
13195 "lua",
13196# endif
13197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198#ifdef FEAT_MENU
13199 "menu",
13200#endif
13201#ifdef FEAT_SESSION
13202 "mksession",
13203#endif
13204#ifdef FEAT_MODIFY_FNAME
13205 "modify_fname",
13206#endif
13207#ifdef FEAT_MOUSE
13208 "mouse",
13209#endif
13210#ifdef FEAT_MOUSESHAPE
13211 "mouseshape",
13212#endif
13213#if defined(UNIX) || defined(VMS)
13214# ifdef FEAT_MOUSE_DEC
13215 "mouse_dec",
13216# endif
13217# ifdef FEAT_MOUSE_GPM
13218 "mouse_gpm",
13219# endif
13220# ifdef FEAT_MOUSE_JSB
13221 "mouse_jsbterm",
13222# endif
13223# ifdef FEAT_MOUSE_NET
13224 "mouse_netterm",
13225# endif
13226# ifdef FEAT_MOUSE_PTERM
13227 "mouse_pterm",
13228# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013229# ifdef FEAT_MOUSE_SGR
13230 "mouse_sgr",
13231# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013232# ifdef FEAT_SYSMOUSE
13233 "mouse_sysmouse",
13234# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013235# ifdef FEAT_MOUSE_URXVT
13236 "mouse_urxvt",
13237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238# ifdef FEAT_MOUSE_XTERM
13239 "mouse_xterm",
13240# endif
13241#endif
13242#ifdef FEAT_MBYTE
13243 "multi_byte",
13244#endif
13245#ifdef FEAT_MBYTE_IME
13246 "multi_byte_ime",
13247#endif
13248#ifdef FEAT_MULTI_LANG
13249 "multi_lang",
13250#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013251#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013252#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013253 "mzscheme",
13254#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256#ifdef FEAT_OLE
13257 "ole",
13258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259#ifdef FEAT_PATH_EXTRA
13260 "path_extra",
13261#endif
13262#ifdef FEAT_PERL
13263#ifndef DYNAMIC_PERL
13264 "perl",
13265#endif
13266#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013267#ifdef FEAT_PERSISTENT_UNDO
13268 "persistent_undo",
13269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270#ifdef FEAT_PYTHON
13271#ifndef DYNAMIC_PYTHON
13272 "python",
13273#endif
13274#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013275#ifdef FEAT_PYTHON3
13276#ifndef DYNAMIC_PYTHON3
13277 "python3",
13278#endif
13279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280#ifdef FEAT_POSTSCRIPT
13281 "postscript",
13282#endif
13283#ifdef FEAT_PRINTER
13284 "printer",
13285#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013286#ifdef FEAT_PROFILE
13287 "profile",
13288#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013289#ifdef FEAT_RELTIME
13290 "reltime",
13291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292#ifdef FEAT_QUICKFIX
13293 "quickfix",
13294#endif
13295#ifdef FEAT_RIGHTLEFT
13296 "rightleft",
13297#endif
13298#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13299 "ruby",
13300#endif
13301#ifdef FEAT_SCROLLBIND
13302 "scrollbind",
13303#endif
13304#ifdef FEAT_CMDL_INFO
13305 "showcmd",
13306 "cmdline_info",
13307#endif
13308#ifdef FEAT_SIGNS
13309 "signs",
13310#endif
13311#ifdef FEAT_SMARTINDENT
13312 "smartindent",
13313#endif
13314#ifdef FEAT_SNIFF
13315 "sniff",
13316#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013317#ifdef STARTUPTIME
13318 "startuptime",
13319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320#ifdef FEAT_STL_OPT
13321 "statusline",
13322#endif
13323#ifdef FEAT_SUN_WORKSHOP
13324 "sun_workshop",
13325#endif
13326#ifdef FEAT_NETBEANS_INTG
13327 "netbeans_intg",
13328#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013329#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013330 "spell",
13331#endif
13332#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333 "syntax",
13334#endif
13335#if defined(USE_SYSTEM) || !defined(UNIX)
13336 "system",
13337#endif
13338#ifdef FEAT_TAG_BINS
13339 "tag_binary",
13340#endif
13341#ifdef FEAT_TAG_OLDSTATIC
13342 "tag_old_static",
13343#endif
13344#ifdef FEAT_TAG_ANYWHITE
13345 "tag_any_white",
13346#endif
13347#ifdef FEAT_TCL
13348# ifndef DYNAMIC_TCL
13349 "tcl",
13350# endif
13351#endif
13352#ifdef TERMINFO
13353 "terminfo",
13354#endif
13355#ifdef FEAT_TERMRESPONSE
13356 "termresponse",
13357#endif
13358#ifdef FEAT_TEXTOBJ
13359 "textobjects",
13360#endif
13361#ifdef HAVE_TGETENT
13362 "tgetent",
13363#endif
13364#ifdef FEAT_TITLE
13365 "title",
13366#endif
13367#ifdef FEAT_TOOLBAR
13368 "toolbar",
13369#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013370#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13371 "unnamedplus",
13372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373#ifdef FEAT_USR_CMDS
13374 "user-commands", /* was accidentally included in 5.4 */
13375 "user_commands",
13376#endif
13377#ifdef FEAT_VIMINFO
13378 "viminfo",
13379#endif
13380#ifdef FEAT_VERTSPLIT
13381 "vertsplit",
13382#endif
13383#ifdef FEAT_VIRTUALEDIT
13384 "virtualedit",
13385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387#ifdef FEAT_VISUALEXTRA
13388 "visualextra",
13389#endif
13390#ifdef FEAT_VREPLACE
13391 "vreplace",
13392#endif
13393#ifdef FEAT_WILDIGN
13394 "wildignore",
13395#endif
13396#ifdef FEAT_WILDMENU
13397 "wildmenu",
13398#endif
13399#ifdef FEAT_WINDOWS
13400 "windows",
13401#endif
13402#ifdef FEAT_WAK
13403 "winaltkeys",
13404#endif
13405#ifdef FEAT_WRITEBACKUP
13406 "writebackup",
13407#endif
13408#ifdef FEAT_XIM
13409 "xim",
13410#endif
13411#ifdef FEAT_XFONTSET
13412 "xfontset",
13413#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013414#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013415 "xpm",
13416 "xpm_w32", /* for backward compatibility */
13417#else
13418# if defined(HAVE_XPM)
13419 "xpm",
13420# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422#ifdef USE_XSMP
13423 "xsmp",
13424#endif
13425#ifdef USE_XSMP_INTERACT
13426 "xsmp_interact",
13427#endif
13428#ifdef FEAT_XCLIPBOARD
13429 "xterm_clipboard",
13430#endif
13431#ifdef FEAT_XTERM_SAVE
13432 "xterm_save",
13433#endif
13434#if defined(UNIX) && defined(FEAT_X11)
13435 "X11",
13436#endif
13437 NULL
13438 };
13439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013440 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 for (i = 0; has_list[i] != NULL; ++i)
13442 if (STRICMP(name, has_list[i]) == 0)
13443 {
13444 n = TRUE;
13445 break;
13446 }
13447
13448 if (n == FALSE)
13449 {
13450 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013451 {
13452 if (name[5] == '-'
13453 && STRLEN(name) > 11
13454 && vim_isdigit(name[6])
13455 && vim_isdigit(name[8])
13456 && vim_isdigit(name[10]))
13457 {
13458 int major = atoi((char *)name + 6);
13459 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013460
13461 /* Expect "patch-9.9.01234". */
13462 n = (major < VIM_VERSION_MAJOR
13463 || (major == VIM_VERSION_MAJOR
13464 && (minor < VIM_VERSION_MINOR
13465 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013466 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013467 }
13468 else
13469 n = has_patch(atoi((char *)name + 5));
13470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471 else if (STRICMP(name, "vim_starting") == 0)
13472 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013473#ifdef FEAT_MBYTE
13474 else if (STRICMP(name, "multi_byte_encoding") == 0)
13475 n = has_mbyte;
13476#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013477#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13478 else if (STRICMP(name, "balloon_multiline") == 0)
13479 n = multiline_balloon_available();
13480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481#ifdef DYNAMIC_TCL
13482 else if (STRICMP(name, "tcl") == 0)
13483 n = tcl_enabled(FALSE);
13484#endif
13485#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13486 else if (STRICMP(name, "iconv") == 0)
13487 n = iconv_enabled(FALSE);
13488#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013489#ifdef DYNAMIC_LUA
13490 else if (STRICMP(name, "lua") == 0)
13491 n = lua_enabled(FALSE);
13492#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013493#ifdef DYNAMIC_MZSCHEME
13494 else if (STRICMP(name, "mzscheme") == 0)
13495 n = mzscheme_enabled(FALSE);
13496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497#ifdef DYNAMIC_RUBY
13498 else if (STRICMP(name, "ruby") == 0)
13499 n = ruby_enabled(FALSE);
13500#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013501#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502#ifdef DYNAMIC_PYTHON
13503 else if (STRICMP(name, "python") == 0)
13504 n = python_enabled(FALSE);
13505#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013506#endif
13507#ifdef FEAT_PYTHON3
13508#ifdef DYNAMIC_PYTHON3
13509 else if (STRICMP(name, "python3") == 0)
13510 n = python3_enabled(FALSE);
13511#endif
13512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513#ifdef DYNAMIC_PERL
13514 else if (STRICMP(name, "perl") == 0)
13515 n = perl_enabled(FALSE);
13516#endif
13517#ifdef FEAT_GUI
13518 else if (STRICMP(name, "gui_running") == 0)
13519 n = (gui.in_use || gui.starting);
13520# ifdef FEAT_GUI_W32
13521 else if (STRICMP(name, "gui_win32s") == 0)
13522 n = gui_is_win32s();
13523# endif
13524# ifdef FEAT_BROWSE
13525 else if (STRICMP(name, "browse") == 0)
13526 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13527# endif
13528#endif
13529#ifdef FEAT_SYN_HL
13530 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013531 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532#endif
13533#if defined(WIN3264)
13534 else if (STRICMP(name, "win95") == 0)
13535 n = mch_windows95();
13536#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013537#ifdef FEAT_NETBEANS_INTG
13538 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013539 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 }
13542
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013543 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544}
13545
13546/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013547 * "has_key()" function
13548 */
13549 static void
13550f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013551 typval_T *argvars;
13552 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013553{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013554 if (argvars[0].v_type != VAR_DICT)
13555 {
13556 EMSG(_(e_dictreq));
13557 return;
13558 }
13559 if (argvars[0].vval.v_dict == NULL)
13560 return;
13561
13562 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013563 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013564}
13565
13566/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013567 * "haslocaldir()" function
13568 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013569 static void
13570f_haslocaldir(argvars, rettv)
Bram Moolenaarc9703302016-01-17 21:49:33 +010013571 typval_T *argvars;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013572 typval_T *rettv;
13573{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013574 win_T *wp = NULL;
13575
13576 wp = find_tabwin(&argvars[0], &argvars[1]);
13577 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013578}
13579
13580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 * "hasmapto()" function
13582 */
13583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013584f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013585 typval_T *argvars;
13586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587{
13588 char_u *name;
13589 char_u *mode;
13590 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013591 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013593 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013594 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 mode = (char_u *)"nvo";
13596 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013598 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013599 if (argvars[2].v_type != VAR_UNKNOWN)
13600 abbr = get_tv_number(&argvars[2]);
13601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602
Bram Moolenaar2c932302006-03-18 21:42:09 +000013603 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013604 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013606 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607}
13608
13609/*
13610 * "histadd()" function
13611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613f_histadd(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#ifdef FEAT_CMDHIST
13618 int histype;
13619 char_u *str;
13620 char_u buf[NUMBUFLEN];
13621#endif
13622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624 if (check_restricted() || check_secure())
13625 return;
13626#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013627 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13628 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 if (histype >= 0)
13630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 if (*str != NUL)
13633 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013634 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013636 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 return;
13638 }
13639 }
13640#endif
13641}
13642
13643/*
13644 * "histdel()" function
13645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013647f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013648 typval_T *argvars UNUSED;
13649 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650{
13651#ifdef FEAT_CMDHIST
13652 int n;
13653 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013654 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013656 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13657 if (str == NULL)
13658 n = 0;
13659 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013661 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013662 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013664 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013665 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666 else
13667 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013668 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013669 get_tv_string_buf(&argvars[1], buf));
13670 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671#endif
13672}
13673
13674/*
13675 * "histget()" function
13676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013678f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013679 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681{
13682#ifdef FEAT_CMDHIST
13683 int type;
13684 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013685 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013687 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13688 if (str == NULL)
13689 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013691 {
13692 type = get_histtype(str);
13693 if (argvars[1].v_type == VAR_UNKNOWN)
13694 idx = get_history_idx(type);
13695 else
13696 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13697 /* -1 on type error */
13698 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013701 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013703 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704}
13705
13706/*
13707 * "histnr()" function
13708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013711 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713{
13714 int i;
13715
13716#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013717 char_u *history = get_tv_string_chk(&argvars[0]);
13718
13719 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 if (i >= HIST_CMD && i < HIST_COUNT)
13721 i = get_history_idx(i);
13722 else
13723#endif
13724 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013725 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726}
13727
13728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 * "highlightID(name)" function
13730 */
13731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013732f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013733 typval_T *argvars;
13734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013736 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737}
13738
13739/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013740 * "highlight_exists()" function
13741 */
13742 static void
13743f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013744 typval_T *argvars;
13745 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013746{
13747 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13748}
13749
13750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751 * "hostname()" function
13752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013754f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013755 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757{
13758 char_u hostname[256];
13759
13760 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013761 rettv->v_type = VAR_STRING;
13762 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763}
13764
13765/*
13766 * iconv() function
13767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013769f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013770 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772{
13773#ifdef FEAT_MBYTE
13774 char_u buf1[NUMBUFLEN];
13775 char_u buf2[NUMBUFLEN];
13776 char_u *from, *to, *str;
13777 vimconv_T vimconv;
13778#endif
13779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013780 rettv->v_type = VAR_STRING;
13781 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782
13783#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013784 str = get_tv_string(&argvars[0]);
13785 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13786 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787 vimconv.vc_type = CONV_NONE;
13788 convert_setup(&vimconv, from, to);
13789
13790 /* If the encodings are equal, no conversion needed. */
13791 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013792 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013794 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795
13796 convert_setup(&vimconv, NULL, NULL);
13797 vim_free(from);
13798 vim_free(to);
13799#endif
13800}
13801
13802/*
13803 * "indent()" function
13804 */
13805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013806f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013807 typval_T *argvars;
13808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809{
13810 linenr_T lnum;
13811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013812 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013814 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013816 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817}
13818
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013819/*
13820 * "index()" function
13821 */
13822 static void
13823f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013824 typval_T *argvars;
13825 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013826{
Bram Moolenaar33570922005-01-25 22:26:29 +000013827 list_T *l;
13828 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013829 long idx = 0;
13830 int ic = FALSE;
13831
13832 rettv->vval.v_number = -1;
13833 if (argvars[0].v_type != VAR_LIST)
13834 {
13835 EMSG(_(e_listreq));
13836 return;
13837 }
13838 l = argvars[0].vval.v_list;
13839 if (l != NULL)
13840 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013841 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013842 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013844 int error = FALSE;
13845
Bram Moolenaar758711c2005-02-02 23:11:38 +000013846 /* Start at specified item. Use the cached index that list_find()
13847 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013848 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013849 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013850 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013851 ic = get_tv_number_chk(&argvars[3], &error);
13852 if (error)
13853 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013854 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013855
Bram Moolenaar758711c2005-02-02 23:11:38 +000013856 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013857 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013858 {
13859 rettv->vval.v_number = idx;
13860 break;
13861 }
13862 }
13863}
13864
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865static int inputsecret_flag = 0;
13866
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013867static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13868
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013870 * This function is used by f_input() and f_inputdialog() functions. The third
13871 * argument to f_input() specifies the type of completion to use at the
13872 * prompt. The third argument to f_inputdialog() specifies the value to return
13873 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874 */
13875 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013876get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013877 typval_T *argvars;
13878 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013879 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013881 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882 char_u *p = NULL;
13883 int c;
13884 char_u buf[NUMBUFLEN];
13885 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013886 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013887 int xp_type = EXPAND_NOTHING;
13888 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013890 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013891 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892
13893#ifdef NO_CONSOLE_INPUT
13894 /* While starting up, there is no place to enter text. */
13895 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897#endif
13898
13899 cmd_silent = FALSE; /* Want to see the prompt. */
13900 if (prompt != NULL)
13901 {
13902 /* Only the part of the message after the last NL is considered as
13903 * prompt for the command line */
13904 p = vim_strrchr(prompt, '\n');
13905 if (p == NULL)
13906 p = prompt;
13907 else
13908 {
13909 ++p;
13910 c = *p;
13911 *p = NUL;
13912 msg_start();
13913 msg_clr_eos();
13914 msg_puts_attr(prompt, echo_attr);
13915 msg_didout = FALSE;
13916 msg_starthere();
13917 *p = c;
13918 }
13919 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013921 if (argvars[1].v_type != VAR_UNKNOWN)
13922 {
13923 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13924 if (defstr != NULL)
13925 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013927 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013928 {
13929 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013930 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013931 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013932
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013933 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013934 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013935
Bram Moolenaar4463f292005-09-25 22:20:24 +000013936 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13937 if (xp_name == NULL)
13938 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013939
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013940 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013941
Bram Moolenaar4463f292005-09-25 22:20:24 +000013942 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13943 &xp_arg) == FAIL)
13944 return;
13945 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013946 }
13947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013948 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013949 {
13950# ifdef FEAT_EX_EXTRA
13951 int save_ex_normal_busy = ex_normal_busy;
13952 ex_normal_busy = 0;
13953# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013954 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013955 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13956 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013957# ifdef FEAT_EX_EXTRA
13958 ex_normal_busy = save_ex_normal_busy;
13959# endif
13960 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013961 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013962 && argvars[1].v_type != VAR_UNKNOWN
13963 && argvars[2].v_type != VAR_UNKNOWN)
13964 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13965 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013966
13967 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013969 /* since the user typed this, no need to wait for return */
13970 need_wait_return = FALSE;
13971 msg_didout = FALSE;
13972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 cmd_silent = cmd_silent_save;
13974}
13975
13976/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013977 * "input()" function
13978 * Also handles inputsecret() when inputsecret is set.
13979 */
13980 static void
13981f_input(argvars, rettv)
13982 typval_T *argvars;
13983 typval_T *rettv;
13984{
13985 get_user_input(argvars, rettv, FALSE);
13986}
13987
13988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989 * "inputdialog()" function
13990 */
13991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013992f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013993 typval_T *argvars;
13994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995{
13996#if defined(FEAT_GUI_TEXTDIALOG)
13997 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13998 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13999 {
14000 char_u *message;
14001 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014002 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014004 message = get_tv_string_chk(&argvars[0]);
14005 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014006 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014007 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008 else
14009 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014010 if (message != NULL && defstr != NULL
14011 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014012 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014013 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014 else
14015 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014016 if (message != NULL && defstr != NULL
14017 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014018 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014019 rettv->vval.v_string = vim_strsave(
14020 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014022 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014024 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 }
14026 else
14027#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014028 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029}
14030
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014031/*
14032 * "inputlist()" function
14033 */
14034 static void
14035f_inputlist(argvars, rettv)
14036 typval_T *argvars;
14037 typval_T *rettv;
14038{
14039 listitem_T *li;
14040 int selected;
14041 int mouse_used;
14042
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014043#ifdef NO_CONSOLE_INPUT
14044 /* While starting up, there is no place to enter text. */
14045 if (no_console_input())
14046 return;
14047#endif
14048 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14049 {
14050 EMSG2(_(e_listarg), "inputlist()");
14051 return;
14052 }
14053
14054 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014055 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014056 lines_left = Rows; /* avoid more prompt */
14057 msg_scroll = TRUE;
14058 msg_clr_eos();
14059
14060 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14061 {
14062 msg_puts(get_tv_string(&li->li_tv));
14063 msg_putchar('\n');
14064 }
14065
14066 /* Ask for choice. */
14067 selected = prompt_for_number(&mouse_used);
14068 if (mouse_used)
14069 selected -= lines_left;
14070
14071 rettv->vval.v_number = selected;
14072}
14073
14074
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14076
14077/*
14078 * "inputrestore()" function
14079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014081f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014082 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084{
14085 if (ga_userinput.ga_len > 0)
14086 {
14087 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14089 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014090 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091 }
14092 else if (p_verbose > 1)
14093 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014094 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014095 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096 }
14097}
14098
14099/*
14100 * "inputsave()" function
14101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014103f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014104 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014107 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108 if (ga_grow(&ga_userinput, 1) == OK)
14109 {
14110 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14111 + ga_userinput.ga_len);
14112 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014113 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114 }
14115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014116 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014117}
14118
14119/*
14120 * "inputsecret()" function
14121 */
14122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014123f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014124 typval_T *argvars;
14125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126{
14127 ++cmdline_star;
14128 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 --cmdline_star;
14131 --inputsecret_flag;
14132}
14133
14134/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014135 * "insert()" function
14136 */
14137 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014138f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014139 typval_T *argvars;
14140 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014141{
14142 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014143 listitem_T *item;
14144 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014145 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014146
14147 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014148 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014149 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014150 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014151 {
14152 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014153 before = get_tv_number_chk(&argvars[2], &error);
14154 if (error)
14155 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014156
Bram Moolenaar758711c2005-02-02 23:11:38 +000014157 if (before == l->lv_len)
14158 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014159 else
14160 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014161 item = list_find(l, before);
14162 if (item == NULL)
14163 {
14164 EMSGN(_(e_listidx), before);
14165 l = NULL;
14166 }
14167 }
14168 if (l != NULL)
14169 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014170 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014171 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014172 }
14173 }
14174}
14175
14176/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014177 * "invert(expr)" function
14178 */
14179 static void
14180f_invert(argvars, rettv)
14181 typval_T *argvars;
14182 typval_T *rettv;
14183{
14184 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14185}
14186
14187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 * "isdirectory()" function
14189 */
14190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014191f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014192 typval_T *argvars;
14193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014195 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196}
14197
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014198/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014199 * "islocked()" function
14200 */
14201 static void
14202f_islocked(argvars, rettv)
14203 typval_T *argvars;
14204 typval_T *rettv;
14205{
14206 lval_T lv;
14207 char_u *end;
14208 dictitem_T *di;
14209
14210 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014211 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14212 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014213 if (end != NULL && lv.ll_name != NULL)
14214 {
14215 if (*end != NUL)
14216 EMSG(_(e_trailing));
14217 else
14218 {
14219 if (lv.ll_tv == NULL)
14220 {
14221 if (check_changedtick(lv.ll_name))
14222 rettv->vval.v_number = 1; /* always locked */
14223 else
14224 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014225 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014226 if (di != NULL)
14227 {
14228 /* Consider a variable locked when:
14229 * 1. the variable itself is locked
14230 * 2. the value of the variable is locked.
14231 * 3. the List or Dict value is locked.
14232 */
14233 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14234 || tv_islocked(&di->di_tv));
14235 }
14236 }
14237 }
14238 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014239 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014240 else if (lv.ll_newkey != NULL)
14241 EMSG2(_(e_dictkey), lv.ll_newkey);
14242 else if (lv.ll_list != NULL)
14243 /* List item. */
14244 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14245 else
14246 /* Dictionary item. */
14247 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14248 }
14249 }
14250
14251 clear_lval(&lv);
14252}
14253
Bram Moolenaar33570922005-01-25 22:26:29 +000014254static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014255
14256/*
14257 * Turn a dict into a list:
14258 * "what" == 0: list of keys
14259 * "what" == 1: list of values
14260 * "what" == 2: list of items
14261 */
14262 static void
14263dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014264 typval_T *argvars;
14265 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014266 int what;
14267{
Bram Moolenaar33570922005-01-25 22:26:29 +000014268 list_T *l2;
14269 dictitem_T *di;
14270 hashitem_T *hi;
14271 listitem_T *li;
14272 listitem_T *li2;
14273 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014274 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014275
Bram Moolenaar8c711452005-01-14 21:53:12 +000014276 if (argvars[0].v_type != VAR_DICT)
14277 {
14278 EMSG(_(e_dictreq));
14279 return;
14280 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014281 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014282 return;
14283
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014284 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014285 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014286
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014287 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014288 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014290 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014291 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014292 --todo;
14293 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014294
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014295 li = listitem_alloc();
14296 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014297 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014298 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014299
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014300 if (what == 0)
14301 {
14302 /* keys() */
14303 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014304 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014305 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14306 }
14307 else if (what == 1)
14308 {
14309 /* values() */
14310 copy_tv(&di->di_tv, &li->li_tv);
14311 }
14312 else
14313 {
14314 /* items() */
14315 l2 = list_alloc();
14316 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014317 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014318 li->li_tv.vval.v_list = l2;
14319 if (l2 == NULL)
14320 break;
14321 ++l2->lv_refcount;
14322
14323 li2 = listitem_alloc();
14324 if (li2 == NULL)
14325 break;
14326 list_append(l2, li2);
14327 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014328 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014329 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14330
14331 li2 = listitem_alloc();
14332 if (li2 == NULL)
14333 break;
14334 list_append(l2, li2);
14335 copy_tv(&di->di_tv, &li2->li_tv);
14336 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014337 }
14338 }
14339}
14340
14341/*
14342 * "items(dict)" function
14343 */
14344 static void
14345f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014346 typval_T *argvars;
14347 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014348{
14349 dict_list(argvars, rettv, 2);
14350}
14351
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014353 * "join()" function
14354 */
14355 static void
14356f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014357 typval_T *argvars;
14358 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014359{
14360 garray_T ga;
14361 char_u *sep;
14362
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014363 if (argvars[0].v_type != VAR_LIST)
14364 {
14365 EMSG(_(e_listreq));
14366 return;
14367 }
14368 if (argvars[0].vval.v_list == NULL)
14369 return;
14370 if (argvars[1].v_type == VAR_UNKNOWN)
14371 sep = (char_u *)" ";
14372 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014373 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014374
14375 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014376
14377 if (sep != NULL)
14378 {
14379 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014380 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014381 ga_append(&ga, NUL);
14382 rettv->vval.v_string = (char_u *)ga.ga_data;
14383 }
14384 else
14385 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014386}
14387
14388/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014389 * "keys()" function
14390 */
14391 static void
14392f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014393 typval_T *argvars;
14394 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014395{
14396 dict_list(argvars, rettv, 0);
14397}
14398
14399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400 * "last_buffer_nr()" function.
14401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014403f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014404 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406{
14407 int n = 0;
14408 buf_T *buf;
14409
14410 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14411 if (n < buf->b_fnum)
14412 n = buf->b_fnum;
14413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014414 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014415}
14416
14417/*
14418 * "len()" function
14419 */
14420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014421f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014422 typval_T *argvars;
14423 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014424{
14425 switch (argvars[0].v_type)
14426 {
14427 case VAR_STRING:
14428 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 rettv->vval.v_number = (varnumber_T)STRLEN(
14430 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014431 break;
14432 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014434 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014435 case VAR_DICT:
14436 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14437 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014438 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014439 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014440 break;
14441 }
14442}
14443
Bram Moolenaar33570922005-01-25 22:26:29 +000014444static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014445
14446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014447libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014448 typval_T *argvars;
14449 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014450 int type;
14451{
14452#ifdef FEAT_LIBCALL
14453 char_u *string_in;
14454 char_u **string_result;
14455 int nr_result;
14456#endif
14457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014458 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014459 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014460 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014461
14462 if (check_restricted() || check_secure())
14463 return;
14464
14465#ifdef FEAT_LIBCALL
14466 /* The first two args must be strings, otherwise its meaningless */
14467 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14468 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014469 string_in = NULL;
14470 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014471 string_in = argvars[2].vval.v_string;
14472 if (type == VAR_NUMBER)
14473 string_result = NULL;
14474 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014476 if (mch_libcall(argvars[0].vval.v_string,
14477 argvars[1].vval.v_string,
14478 string_in,
14479 argvars[2].vval.v_number,
14480 string_result,
14481 &nr_result) == OK
14482 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014483 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014484 }
14485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486}
14487
14488/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014489 * "libcall()" function
14490 */
14491 static void
14492f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014493 typval_T *argvars;
14494 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014495{
14496 libcall_common(argvars, rettv, VAR_STRING);
14497}
14498
14499/*
14500 * "libcallnr()" function
14501 */
14502 static void
14503f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014504 typval_T *argvars;
14505 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014506{
14507 libcall_common(argvars, rettv, VAR_NUMBER);
14508}
14509
14510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511 * "line(string)" function
14512 */
14513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014514f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014515 typval_T *argvars;
14516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517{
14518 linenr_T lnum = 0;
14519 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014520 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014522 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523 if (fp != NULL)
14524 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014525 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526}
14527
14528/*
14529 * "line2byte(lnum)" function
14530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014532f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014533 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535{
14536#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014537 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538#else
14539 linenr_T lnum;
14540
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014541 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014543 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014545 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14546 if (rettv->vval.v_number >= 0)
14547 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548#endif
14549}
14550
14551/*
14552 * "lispindent(lnum)" function
14553 */
14554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014555f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014556 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558{
14559#ifdef FEAT_LISP
14560 pos_T pos;
14561 linenr_T lnum;
14562
14563 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014564 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14566 {
14567 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014568 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 curwin->w_cursor = pos;
14570 }
14571 else
14572#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014573 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574}
14575
14576/*
14577 * "localtime()" function
14578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014580f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014581 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014584 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585}
14586
Bram Moolenaar33570922005-01-25 22:26:29 +000014587static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588
14589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014590get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014591 typval_T *argvars;
14592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 int exact;
14594{
14595 char_u *keys;
14596 char_u *which;
14597 char_u buf[NUMBUFLEN];
14598 char_u *keys_buf = NULL;
14599 char_u *rhs;
14600 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014601 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014602 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014603 mapblock_T *mp;
14604 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605
14606 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014607 rettv->v_type = VAR_STRING;
14608 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014610 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014611 if (*keys == NUL)
14612 return;
14613
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014614 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014615 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014616 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014617 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014618 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014619 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014620 if (argvars[3].v_type != VAR_UNKNOWN)
14621 get_dict = get_tv_number(&argvars[3]);
14622 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 else
14625 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014626 if (which == NULL)
14627 return;
14628
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629 mode = get_map_mode(&which, 0);
14630
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014631 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014632 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014634
14635 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014637 /* Return a string. */
14638 if (rhs != NULL)
14639 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640
Bram Moolenaarbd743252010-10-20 21:23:33 +020014641 }
14642 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14643 {
14644 /* Return a dictionary. */
14645 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14646 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14647 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648
Bram Moolenaarbd743252010-10-20 21:23:33 +020014649 dict_add_nr_str(dict, "lhs", 0L, lhs);
14650 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14651 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14652 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14653 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14654 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14655 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014656 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014657 dict_add_nr_str(dict, "mode", 0L, mapmode);
14658
14659 vim_free(lhs);
14660 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661 }
14662}
14663
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014664#ifdef FEAT_FLOAT
14665/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014666 * "log()" function
14667 */
14668 static void
14669f_log(argvars, rettv)
14670 typval_T *argvars;
14671 typval_T *rettv;
14672{
14673 float_T f;
14674
14675 rettv->v_type = VAR_FLOAT;
14676 if (get_float_arg(argvars, &f) == OK)
14677 rettv->vval.v_float = log(f);
14678 else
14679 rettv->vval.v_float = 0.0;
14680}
14681
14682/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014683 * "log10()" function
14684 */
14685 static void
14686f_log10(argvars, rettv)
14687 typval_T *argvars;
14688 typval_T *rettv;
14689{
14690 float_T f;
14691
14692 rettv->v_type = VAR_FLOAT;
14693 if (get_float_arg(argvars, &f) == OK)
14694 rettv->vval.v_float = log10(f);
14695 else
14696 rettv->vval.v_float = 0.0;
14697}
14698#endif
14699
Bram Moolenaar1dced572012-04-05 16:54:08 +020014700#ifdef FEAT_LUA
14701/*
14702 * "luaeval()" function
14703 */
14704 static void
14705f_luaeval(argvars, rettv)
14706 typval_T *argvars;
14707 typval_T *rettv;
14708{
14709 char_u *str;
14710 char_u buf[NUMBUFLEN];
14711
14712 str = get_tv_string_buf(&argvars[0], buf);
14713 do_luaeval(str, argvars + 1, rettv);
14714}
14715#endif
14716
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014718 * "map()" function
14719 */
14720 static void
14721f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014722 typval_T *argvars;
14723 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014724{
14725 filter_map(argvars, rettv, TRUE);
14726}
14727
14728/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014729 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730 */
14731 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014732f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014733 typval_T *argvars;
14734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014736 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737}
14738
14739/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014740 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 */
14742 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014743f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014744 typval_T *argvars;
14745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014747 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748}
14749
Bram Moolenaar33570922005-01-25 22:26:29 +000014750static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751
14752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014753find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014754 typval_T *argvars;
14755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 int type;
14757{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014758 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014759 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014760 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 char_u *pat;
14762 regmatch_T regmatch;
14763 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014764 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 char_u *save_cpo;
14766 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014767 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014768 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014769 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014770 list_T *l = NULL;
14771 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014772 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014773 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774
14775 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14776 save_cpo = p_cpo;
14777 p_cpo = (char_u *)"";
14778
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014779 rettv->vval.v_number = -1;
14780 if (type == 3)
14781 {
14782 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014783 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014784 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014785 }
14786 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014788 rettv->v_type = VAR_STRING;
14789 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014792 if (argvars[0].v_type == VAR_LIST)
14793 {
14794 if ((l = argvars[0].vval.v_list) == NULL)
14795 goto theend;
14796 li = l->lv_first;
14797 }
14798 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014799 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014800 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014801 len = (long)STRLEN(str);
14802 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014804 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14805 if (pat == NULL)
14806 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014807
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014808 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014810 int error = FALSE;
14811
14812 start = get_tv_number_chk(&argvars[2], &error);
14813 if (error)
14814 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014815 if (l != NULL)
14816 {
14817 li = list_find(l, start);
14818 if (li == NULL)
14819 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014820 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014821 }
14822 else
14823 {
14824 if (start < 0)
14825 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014826 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014827 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014828 /* When "count" argument is there ignore matches before "start",
14829 * otherwise skip part of the string. Differs when pattern is "^"
14830 * or "\<". */
14831 if (argvars[3].v_type != VAR_UNKNOWN)
14832 startcol = start;
14833 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014834 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014835 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014836 len -= start;
14837 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014838 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014839
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014840 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014841 nth = get_tv_number_chk(&argvars[3], &error);
14842 if (error)
14843 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 }
14845
14846 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14847 if (regmatch.regprog != NULL)
14848 {
14849 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014850
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014851 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014852 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014853 if (l != NULL)
14854 {
14855 if (li == NULL)
14856 {
14857 match = FALSE;
14858 break;
14859 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014860 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014861 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014862 if (str == NULL)
14863 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014864 }
14865
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014866 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014867
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014868 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014869 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014870 if (l == NULL && !match)
14871 break;
14872
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014873 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014874 if (l != NULL)
14875 {
14876 li = li->li_next;
14877 ++idx;
14878 }
14879 else
14880 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014881#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014882 startcol = (colnr_T)(regmatch.startp[0]
14883 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014884#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014885 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014886#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014887 if (startcol > (colnr_T)len
14888 || str + startcol <= regmatch.startp[0])
14889 {
14890 match = FALSE;
14891 break;
14892 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014893 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014894 }
14895
14896 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014898 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014899 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014900 int i;
14901
14902 /* return list with matched string and submatches */
14903 for (i = 0; i < NSUBEXP; ++i)
14904 {
14905 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014906 {
14907 if (list_append_string(rettv->vval.v_list,
14908 (char_u *)"", 0) == FAIL)
14909 break;
14910 }
14911 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014912 regmatch.startp[i],
14913 (int)(regmatch.endp[i] - regmatch.startp[i]))
14914 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014915 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014916 }
14917 }
14918 else if (type == 2)
14919 {
14920 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014921 if (l != NULL)
14922 copy_tv(&li->li_tv, rettv);
14923 else
14924 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014926 }
14927 else if (l != NULL)
14928 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929 else
14930 {
14931 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014932 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933 (varnumber_T)(regmatch.startp[0] - str);
14934 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014935 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014937 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938 }
14939 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014940 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941 }
14942
14943theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014944 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945 p_cpo = save_cpo;
14946}
14947
14948/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014949 * "match()" function
14950 */
14951 static void
14952f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014953 typval_T *argvars;
14954 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014955{
14956 find_some_match(argvars, rettv, 1);
14957}
14958
14959/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014960 * "matchadd()" function
14961 */
14962 static void
14963f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014964 typval_T *argvars UNUSED;
14965 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014966{
14967#ifdef FEAT_SEARCH_EXTRA
14968 char_u buf[NUMBUFLEN];
14969 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14970 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14971 int prio = 10; /* default priority */
14972 int id = -1;
14973 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014974 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014975
14976 rettv->vval.v_number = -1;
14977
14978 if (grp == NULL || pat == NULL)
14979 return;
14980 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014981 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014982 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014983 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014984 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014985 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014986 if (argvars[4].v_type != VAR_UNKNOWN)
14987 {
14988 if (argvars[4].v_type != VAR_DICT)
14989 {
14990 EMSG(_(e_dictreq));
14991 return;
14992 }
14993 if (dict_find(argvars[4].vval.v_dict,
14994 (char_u *)"conceal", -1) != NULL)
14995 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14996 (char_u *)"conceal", FALSE);
14997 }
14998 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014999 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015000 if (error == TRUE)
15001 return;
15002 if (id >= 1 && id <= 3)
15003 {
15004 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15005 return;
15006 }
15007
Bram Moolenaar6561d522015-07-21 15:48:27 +020015008 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15009 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015010#endif
15011}
15012
15013/*
15014 * "matchaddpos()" function
15015 */
15016 static void
15017f_matchaddpos(argvars, rettv)
15018 typval_T *argvars UNUSED;
15019 typval_T *rettv UNUSED;
15020{
15021#ifdef FEAT_SEARCH_EXTRA
15022 char_u buf[NUMBUFLEN];
15023 char_u *group;
15024 int prio = 10;
15025 int id = -1;
15026 int error = FALSE;
15027 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015028 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015029
15030 rettv->vval.v_number = -1;
15031
15032 group = get_tv_string_buf_chk(&argvars[0], buf);
15033 if (group == NULL)
15034 return;
15035
15036 if (argvars[1].v_type != VAR_LIST)
15037 {
15038 EMSG2(_(e_listarg), "matchaddpos()");
15039 return;
15040 }
15041 l = argvars[1].vval.v_list;
15042 if (l == NULL)
15043 return;
15044
15045 if (argvars[2].v_type != VAR_UNKNOWN)
15046 {
15047 prio = get_tv_number_chk(&argvars[2], &error);
15048 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015049 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015050 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015051 if (argvars[4].v_type != VAR_UNKNOWN)
15052 {
15053 if (argvars[4].v_type != VAR_DICT)
15054 {
15055 EMSG(_(e_dictreq));
15056 return;
15057 }
15058 if (dict_find(argvars[4].vval.v_dict,
15059 (char_u *)"conceal", -1) != NULL)
15060 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15061 (char_u *)"conceal", FALSE);
15062 }
15063 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015064 }
15065 if (error == TRUE)
15066 return;
15067
15068 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15069 if (id == 1 || id == 2)
15070 {
15071 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15072 return;
15073 }
15074
Bram Moolenaar6561d522015-07-21 15:48:27 +020015075 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15076 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015077#endif
15078}
15079
15080/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015081 * "matcharg()" function
15082 */
15083 static void
15084f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015085 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015086 typval_T *rettv;
15087{
15088 if (rettv_list_alloc(rettv) == OK)
15089 {
15090#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015091 int id = get_tv_number(&argvars[0]);
15092 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015093
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015094 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015095 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015096 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15097 {
15098 list_append_string(rettv->vval.v_list,
15099 syn_id2name(m->hlg_id), -1);
15100 list_append_string(rettv->vval.v_list, m->pattern, -1);
15101 }
15102 else
15103 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015104 list_append_string(rettv->vval.v_list, NULL, -1);
15105 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015106 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015107 }
15108#endif
15109 }
15110}
15111
15112/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015113 * "matchdelete()" function
15114 */
15115 static void
15116f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015117 typval_T *argvars UNUSED;
15118 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015119{
15120#ifdef FEAT_SEARCH_EXTRA
15121 rettv->vval.v_number = match_delete(curwin,
15122 (int)get_tv_number(&argvars[0]), TRUE);
15123#endif
15124}
15125
15126/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127 * "matchend()" function
15128 */
15129 static void
15130f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015131 typval_T *argvars;
15132 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015133{
15134 find_some_match(argvars, rettv, 0);
15135}
15136
15137/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015138 * "matchlist()" function
15139 */
15140 static void
15141f_matchlist(argvars, rettv)
15142 typval_T *argvars;
15143 typval_T *rettv;
15144{
15145 find_some_match(argvars, rettv, 3);
15146}
15147
15148/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015149 * "matchstr()" function
15150 */
15151 static void
15152f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015153 typval_T *argvars;
15154 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015155{
15156 find_some_match(argvars, rettv, 2);
15157}
15158
Bram Moolenaar33570922005-01-25 22:26:29 +000015159static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015160
15161 static void
15162max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015163 typval_T *argvars;
15164 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015165 int domax;
15166{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015167 long n = 0;
15168 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015169 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015170
15171 if (argvars[0].v_type == VAR_LIST)
15172 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015173 list_T *l;
15174 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015175
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015176 l = argvars[0].vval.v_list;
15177 if (l != NULL)
15178 {
15179 li = l->lv_first;
15180 if (li != NULL)
15181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015182 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015183 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015184 {
15185 li = li->li_next;
15186 if (li == NULL)
15187 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015188 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015189 if (domax ? i > n : i < n)
15190 n = i;
15191 }
15192 }
15193 }
15194 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015195 else if (argvars[0].v_type == VAR_DICT)
15196 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015197 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015198 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015199 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015200 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015201
15202 d = argvars[0].vval.v_dict;
15203 if (d != NULL)
15204 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015205 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015206 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015207 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015208 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015209 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015210 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015211 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015212 if (first)
15213 {
15214 n = i;
15215 first = FALSE;
15216 }
15217 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015218 n = i;
15219 }
15220 }
15221 }
15222 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015223 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015224 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015225 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015226}
15227
15228/*
15229 * "max()" function
15230 */
15231 static void
15232f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015233 typval_T *argvars;
15234 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015235{
15236 max_min(argvars, rettv, TRUE);
15237}
15238
15239/*
15240 * "min()" function
15241 */
15242 static void
15243f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015244 typval_T *argvars;
15245 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015246{
15247 max_min(argvars, rettv, FALSE);
15248}
15249
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015250static int mkdir_recurse __ARGS((char_u *dir, int prot));
15251
15252/*
15253 * Create the directory in which "dir" is located, and higher levels when
15254 * needed.
15255 */
15256 static int
15257mkdir_recurse(dir, prot)
15258 char_u *dir;
15259 int prot;
15260{
15261 char_u *p;
15262 char_u *updir;
15263 int r = FAIL;
15264
15265 /* Get end of directory name in "dir".
15266 * We're done when it's "/" or "c:/". */
15267 p = gettail_sep(dir);
15268 if (p <= get_past_head(dir))
15269 return OK;
15270
15271 /* If the directory exists we're done. Otherwise: create it.*/
15272 updir = vim_strnsave(dir, (int)(p - dir));
15273 if (updir == NULL)
15274 return FAIL;
15275 if (mch_isdir(updir))
15276 r = OK;
15277 else if (mkdir_recurse(updir, prot) == OK)
15278 r = vim_mkdir_emsg(updir, prot);
15279 vim_free(updir);
15280 return r;
15281}
15282
15283#ifdef vim_mkdir
15284/*
15285 * "mkdir()" function
15286 */
15287 static void
15288f_mkdir(argvars, rettv)
15289 typval_T *argvars;
15290 typval_T *rettv;
15291{
15292 char_u *dir;
15293 char_u buf[NUMBUFLEN];
15294 int prot = 0755;
15295
15296 rettv->vval.v_number = FAIL;
15297 if (check_restricted() || check_secure())
15298 return;
15299
15300 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015301 if (*dir == NUL)
15302 rettv->vval.v_number = FAIL;
15303 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015304 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015305 if (*gettail(dir) == NUL)
15306 /* remove trailing slashes */
15307 *gettail_sep(dir) = NUL;
15308
15309 if (argvars[1].v_type != VAR_UNKNOWN)
15310 {
15311 if (argvars[2].v_type != VAR_UNKNOWN)
15312 prot = get_tv_number_chk(&argvars[2], NULL);
15313 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15314 mkdir_recurse(dir, prot);
15315 }
15316 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015317 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015318}
15319#endif
15320
Bram Moolenaar0d660222005-01-07 21:51:51 +000015321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 * "mode()" function
15323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015325f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015326 typval_T *argvars;
15327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015329 char_u buf[3];
15330
15331 buf[1] = NUL;
15332 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334 if (VIsual_active)
15335 {
15336 if (VIsual_select)
15337 buf[0] = VIsual_mode + 's' - 'v';
15338 else
15339 buf[0] = VIsual_mode;
15340 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015341 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015342 || State == CONFIRM)
15343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015345 if (State == ASKMORE)
15346 buf[1] = 'm';
15347 else if (State == CONFIRM)
15348 buf[1] = '?';
15349 }
15350 else if (State == EXTERNCMD)
15351 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 else if (State & INSERT)
15353 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015354#ifdef FEAT_VREPLACE
15355 if (State & VREPLACE_FLAG)
15356 {
15357 buf[0] = 'R';
15358 buf[1] = 'v';
15359 }
15360 else
15361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 if (State & REPLACE_FLAG)
15363 buf[0] = 'R';
15364 else
15365 buf[0] = 'i';
15366 }
15367 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015368 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015370 if (exmode_active)
15371 buf[1] = 'v';
15372 }
15373 else if (exmode_active)
15374 {
15375 buf[0] = 'c';
15376 buf[1] = 'e';
15377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015381 if (finish_op)
15382 buf[1] = 'o';
15383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015385 /* Clear out the minor mode when the argument is not a non-zero number or
15386 * non-empty string. */
15387 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015388 buf[1] = NUL;
15389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390 rettv->vval.v_string = vim_strsave(buf);
15391 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392}
15393
Bram Moolenaar429fa852013-04-15 12:27:36 +020015394#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015395/*
15396 * "mzeval()" function
15397 */
15398 static void
15399f_mzeval(argvars, rettv)
15400 typval_T *argvars;
15401 typval_T *rettv;
15402{
15403 char_u *str;
15404 char_u buf[NUMBUFLEN];
15405
15406 str = get_tv_string_buf(&argvars[0], buf);
15407 do_mzeval(str, rettv);
15408}
Bram Moolenaar75676462013-01-30 14:55:42 +010015409
15410 void
15411mzscheme_call_vim(name, args, rettv)
15412 char_u *name;
15413 typval_T *args;
15414 typval_T *rettv;
15415{
15416 typval_T argvars[3];
15417
15418 argvars[0].v_type = VAR_STRING;
15419 argvars[0].vval.v_string = name;
15420 copy_tv(args, &argvars[1]);
15421 argvars[2].v_type = VAR_UNKNOWN;
15422 f_call(argvars, rettv);
15423 clear_tv(&argvars[1]);
15424}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015425#endif
15426
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015428 * "nextnonblank()" function
15429 */
15430 static void
15431f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015432 typval_T *argvars;
15433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015434{
15435 linenr_T lnum;
15436
15437 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015439 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015440 {
15441 lnum = 0;
15442 break;
15443 }
15444 if (*skipwhite(ml_get(lnum)) != NUL)
15445 break;
15446 }
15447 rettv->vval.v_number = lnum;
15448}
15449
15450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 * "nr2char()" function
15452 */
15453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015454f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015455 typval_T *argvars;
15456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457{
15458 char_u buf[NUMBUFLEN];
15459
15460#ifdef FEAT_MBYTE
15461 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015462 {
15463 int utf8 = 0;
15464
15465 if (argvars[1].v_type != VAR_UNKNOWN)
15466 utf8 = get_tv_number_chk(&argvars[1], NULL);
15467 if (utf8)
15468 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15469 else
15470 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472 else
15473#endif
15474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015475 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476 buf[1] = NUL;
15477 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478 rettv->v_type = VAR_STRING;
15479 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015480}
15481
15482/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015483 * "or(expr, expr)" function
15484 */
15485 static void
15486f_or(argvars, rettv)
15487 typval_T *argvars;
15488 typval_T *rettv;
15489{
15490 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15491 | get_tv_number_chk(&argvars[1], NULL);
15492}
15493
15494/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015495 * "pathshorten()" function
15496 */
15497 static void
15498f_pathshorten(argvars, rettv)
15499 typval_T *argvars;
15500 typval_T *rettv;
15501{
15502 char_u *p;
15503
15504 rettv->v_type = VAR_STRING;
15505 p = get_tv_string_chk(&argvars[0]);
15506 if (p == NULL)
15507 rettv->vval.v_string = NULL;
15508 else
15509 {
15510 p = vim_strsave(p);
15511 rettv->vval.v_string = p;
15512 if (p != NULL)
15513 shorten_dir(p);
15514 }
15515}
15516
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015517#ifdef FEAT_PERL
15518/*
15519 * "perleval()" function
15520 */
15521 static void
15522f_perleval(argvars, rettv)
15523 typval_T *argvars;
15524 typval_T *rettv;
15525{
15526 char_u *str;
15527 char_u buf[NUMBUFLEN];
15528
15529 str = get_tv_string_buf(&argvars[0], buf);
15530 do_perleval(str, rettv);
15531}
15532#endif
15533
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015534#ifdef FEAT_FLOAT
15535/*
15536 * "pow()" function
15537 */
15538 static void
15539f_pow(argvars, rettv)
15540 typval_T *argvars;
15541 typval_T *rettv;
15542{
15543 float_T fx, fy;
15544
15545 rettv->v_type = VAR_FLOAT;
15546 if (get_float_arg(argvars, &fx) == OK
15547 && get_float_arg(&argvars[1], &fy) == OK)
15548 rettv->vval.v_float = pow(fx, fy);
15549 else
15550 rettv->vval.v_float = 0.0;
15551}
15552#endif
15553
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015554/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015555 * "prevnonblank()" function
15556 */
15557 static void
15558f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015559 typval_T *argvars;
15560 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015561{
15562 linenr_T lnum;
15563
15564 lnum = get_tv_lnum(argvars);
15565 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15566 lnum = 0;
15567 else
15568 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15569 --lnum;
15570 rettv->vval.v_number = lnum;
15571}
15572
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015573#ifdef HAVE_STDARG_H
15574/* This dummy va_list is here because:
15575 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15576 * - locally in the function results in a "used before set" warning
15577 * - using va_start() to initialize it gives "function with fixed args" error */
15578static va_list ap;
15579#endif
15580
Bram Moolenaar8c711452005-01-14 21:53:12 +000015581/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015582 * "printf()" function
15583 */
15584 static void
15585f_printf(argvars, rettv)
15586 typval_T *argvars;
15587 typval_T *rettv;
15588{
15589 rettv->v_type = VAR_STRING;
15590 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015591#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015592 {
15593 char_u buf[NUMBUFLEN];
15594 int len;
15595 char_u *s;
15596 int saved_did_emsg = did_emsg;
15597 char *fmt;
15598
15599 /* Get the required length, allocate the buffer and do it for real. */
15600 did_emsg = FALSE;
15601 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015602 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015603 if (!did_emsg)
15604 {
15605 s = alloc(len + 1);
15606 if (s != NULL)
15607 {
15608 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015609 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015610 }
15611 }
15612 did_emsg |= saved_did_emsg;
15613 }
15614#endif
15615}
15616
15617/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015618 * "pumvisible()" function
15619 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015620 static void
15621f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015622 typval_T *argvars UNUSED;
15623 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015624{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015625#ifdef FEAT_INS_EXPAND
15626 if (pum_visible())
15627 rettv->vval.v_number = 1;
15628#endif
15629}
15630
Bram Moolenaardb913952012-06-29 12:54:53 +020015631#ifdef FEAT_PYTHON3
15632/*
15633 * "py3eval()" function
15634 */
15635 static void
15636f_py3eval(argvars, rettv)
15637 typval_T *argvars;
15638 typval_T *rettv;
15639{
15640 char_u *str;
15641 char_u buf[NUMBUFLEN];
15642
15643 str = get_tv_string_buf(&argvars[0], buf);
15644 do_py3eval(str, rettv);
15645}
15646#endif
15647
15648#ifdef FEAT_PYTHON
15649/*
15650 * "pyeval()" function
15651 */
15652 static void
15653f_pyeval(argvars, rettv)
15654 typval_T *argvars;
15655 typval_T *rettv;
15656{
15657 char_u *str;
15658 char_u buf[NUMBUFLEN];
15659
15660 str = get_tv_string_buf(&argvars[0], buf);
15661 do_pyeval(str, rettv);
15662}
15663#endif
15664
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015665/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015666 * "range()" function
15667 */
15668 static void
15669f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015670 typval_T *argvars;
15671 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015672{
15673 long start;
15674 long end;
15675 long stride = 1;
15676 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015677 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015678
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015679 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015680 if (argvars[1].v_type == VAR_UNKNOWN)
15681 {
15682 end = start - 1;
15683 start = 0;
15684 }
15685 else
15686 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015687 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015688 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015689 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015690 }
15691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015692 if (error)
15693 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015694 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015695 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015696 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015697 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015698 else
15699 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015700 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015701 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015702 if (list_append_number(rettv->vval.v_list,
15703 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015704 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015705 }
15706}
15707
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015708/*
15709 * "readfile()" function
15710 */
15711 static void
15712f_readfile(argvars, rettv)
15713 typval_T *argvars;
15714 typval_T *rettv;
15715{
15716 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015717 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015718 char_u *fname;
15719 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015720 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15721 int io_size = sizeof(buf);
15722 int readlen; /* size of last fread() */
15723 char_u *prev = NULL; /* previously read bytes, if any */
15724 long prevlen = 0; /* length of data in prev */
15725 long prevsize = 0; /* size of prev buffer */
15726 long maxline = MAXLNUM;
15727 long cnt = 0;
15728 char_u *p; /* position in buf */
15729 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015730
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015731 if (argvars[1].v_type != VAR_UNKNOWN)
15732 {
15733 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15734 binary = TRUE;
15735 if (argvars[2].v_type != VAR_UNKNOWN)
15736 maxline = get_tv_number(&argvars[2]);
15737 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015738
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015739 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015740 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015741
15742 /* Always open the file in binary mode, library functions have a mind of
15743 * their own about CR-LF conversion. */
15744 fname = get_tv_string(&argvars[0]);
15745 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15746 {
15747 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15748 return;
15749 }
15750
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015751 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015752 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015753 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015754
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015755 /* This for loop processes what was read, but is also entered at end
15756 * of file so that either:
15757 * - an incomplete line gets written
15758 * - a "binary" file gets an empty line at the end if it ends in a
15759 * newline. */
15760 for (p = buf, start = buf;
15761 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15762 ++p)
15763 {
15764 if (*p == '\n' || readlen <= 0)
15765 {
15766 listitem_T *li;
15767 char_u *s = NULL;
15768 long_u len = p - start;
15769
15770 /* Finished a line. Remove CRs before NL. */
15771 if (readlen > 0 && !binary)
15772 {
15773 while (len > 0 && start[len - 1] == '\r')
15774 --len;
15775 /* removal may cross back to the "prev" string */
15776 if (len == 0)
15777 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15778 --prevlen;
15779 }
15780 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015781 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015782 else
15783 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015784 /* Change "prev" buffer to be the right size. This way
15785 * the bytes are only copied once, and very long lines are
15786 * allocated only once. */
15787 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015788 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015789 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015790 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015791 prev = NULL; /* the list will own the string */
15792 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015793 }
15794 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015795 if (s == NULL)
15796 {
15797 do_outofmem_msg((long_u) prevlen + len + 1);
15798 failed = TRUE;
15799 break;
15800 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015801
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015802 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015803 {
15804 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015805 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015806 break;
15807 }
15808 li->li_tv.v_type = VAR_STRING;
15809 li->li_tv.v_lock = 0;
15810 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015811 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015812
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015813 start = p + 1; /* step over newline */
15814 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015815 break;
15816 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015817 else if (*p == NUL)
15818 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015819#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015820 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15821 * when finding the BF and check the previous two bytes. */
15822 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015823 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015824 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15825 * + 1, these may be in the "prev" string. */
15826 char_u back1 = p >= buf + 1 ? p[-1]
15827 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15828 char_u back2 = p >= buf + 2 ? p[-2]
15829 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15830 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015831
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015832 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015833 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015834 char_u *dest = p - 2;
15835
15836 /* Usually a BOM is at the beginning of a file, and so at
15837 * the beginning of a line; then we can just step over it.
15838 */
15839 if (start == dest)
15840 start = p + 1;
15841 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015842 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015843 /* have to shuffle buf to close gap */
15844 int adjust_prevlen = 0;
15845
15846 if (dest < buf)
15847 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015848 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015849 dest = buf;
15850 }
15851 if (readlen > p - buf + 1)
15852 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15853 readlen -= 3 - adjust_prevlen;
15854 prevlen -= adjust_prevlen;
15855 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015856 }
15857 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015858 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015859#endif
15860 } /* for */
15861
15862 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15863 break;
15864 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015865 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015866 /* There's part of a line in buf, store it in "prev". */
15867 if (p - start + prevlen >= prevsize)
15868 {
15869 /* need bigger "prev" buffer */
15870 char_u *newprev;
15871
15872 /* A common use case is ordinary text files and "prev" gets a
15873 * fragment of a line, so the first allocation is made
15874 * small, to avoid repeatedly 'allocing' large and
15875 * 'reallocing' small. */
15876 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015877 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015878 else
15879 {
15880 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015881 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015882 prevsize = grow50pc > growmin ? grow50pc : growmin;
15883 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015884 newprev = prev == NULL ? alloc(prevsize)
15885 : vim_realloc(prev, prevsize);
15886 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015887 {
15888 do_outofmem_msg((long_u)prevsize);
15889 failed = TRUE;
15890 break;
15891 }
15892 prev = newprev;
15893 }
15894 /* Add the line part to end of "prev". */
15895 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015896 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015897 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015898 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015899
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015900 /*
15901 * For a negative line count use only the lines at the end of the file,
15902 * free the rest.
15903 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015904 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015905 while (cnt > -maxline)
15906 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015907 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015908 --cnt;
15909 }
15910
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015911 if (failed)
15912 {
15913 list_free(rettv->vval.v_list, TRUE);
15914 /* readfile doc says an empty list is returned on error */
15915 rettv->vval.v_list = list_alloc();
15916 }
15917
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015918 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015919 fclose(fd);
15920}
15921
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015922#if defined(FEAT_RELTIME)
15923static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15924
15925/*
15926 * Convert a List to proftime_T.
15927 * Return FAIL when there is something wrong.
15928 */
15929 static int
15930list2proftime(arg, tm)
15931 typval_T *arg;
15932 proftime_T *tm;
15933{
15934 long n1, n2;
15935 int error = FALSE;
15936
15937 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15938 || arg->vval.v_list->lv_len != 2)
15939 return FAIL;
15940 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15941 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15942# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015943 tm->HighPart = n1;
15944 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015945# else
15946 tm->tv_sec = n1;
15947 tm->tv_usec = n2;
15948# endif
15949 return error ? FAIL : OK;
15950}
15951#endif /* FEAT_RELTIME */
15952
15953/*
15954 * "reltime()" function
15955 */
15956 static void
15957f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015958 typval_T *argvars UNUSED;
15959 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015960{
15961#ifdef FEAT_RELTIME
15962 proftime_T res;
15963 proftime_T start;
15964
15965 if (argvars[0].v_type == VAR_UNKNOWN)
15966 {
15967 /* No arguments: get current time. */
15968 profile_start(&res);
15969 }
15970 else if (argvars[1].v_type == VAR_UNKNOWN)
15971 {
15972 if (list2proftime(&argvars[0], &res) == FAIL)
15973 return;
15974 profile_end(&res);
15975 }
15976 else
15977 {
15978 /* Two arguments: compute the difference. */
15979 if (list2proftime(&argvars[0], &start) == FAIL
15980 || list2proftime(&argvars[1], &res) == FAIL)
15981 return;
15982 profile_sub(&res, &start);
15983 }
15984
15985 if (rettv_list_alloc(rettv) == OK)
15986 {
15987 long n1, n2;
15988
15989# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015990 n1 = res.HighPart;
15991 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015992# else
15993 n1 = res.tv_sec;
15994 n2 = res.tv_usec;
15995# endif
15996 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15997 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15998 }
15999#endif
16000}
16001
16002/*
16003 * "reltimestr()" function
16004 */
16005 static void
16006f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016007 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016008 typval_T *rettv;
16009{
16010#ifdef FEAT_RELTIME
16011 proftime_T tm;
16012#endif
16013
16014 rettv->v_type = VAR_STRING;
16015 rettv->vval.v_string = NULL;
16016#ifdef FEAT_RELTIME
16017 if (list2proftime(&argvars[0], &tm) == OK)
16018 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16019#endif
16020}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016021
Bram Moolenaar0d660222005-01-07 21:51:51 +000016022#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
16023static void make_connection __ARGS((void));
16024static int check_connection __ARGS((void));
16025
16026 static void
16027make_connection()
16028{
16029 if (X_DISPLAY == NULL
16030# ifdef FEAT_GUI
16031 && !gui.in_use
16032# endif
16033 )
16034 {
16035 x_force_connect = TRUE;
16036 setup_term_clip();
16037 x_force_connect = FALSE;
16038 }
16039}
16040
16041 static int
16042check_connection()
16043{
16044 make_connection();
16045 if (X_DISPLAY == NULL)
16046 {
16047 EMSG(_("E240: No connection to Vim server"));
16048 return FAIL;
16049 }
16050 return OK;
16051}
16052#endif
16053
16054#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016055static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016056
16057 static void
16058remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000016059 typval_T *argvars;
16060 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016061 int expr;
16062{
16063 char_u *server_name;
16064 char_u *keys;
16065 char_u *r = NULL;
16066 char_u buf[NUMBUFLEN];
16067# ifdef WIN32
16068 HWND w;
16069# else
16070 Window w;
16071# endif
16072
16073 if (check_restricted() || check_secure())
16074 return;
16075
16076# ifdef FEAT_X11
16077 if (check_connection() == FAIL)
16078 return;
16079# endif
16080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016081 server_name = get_tv_string_chk(&argvars[0]);
16082 if (server_name == NULL)
16083 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016084 keys = get_tv_string_buf(&argvars[1], buf);
16085# ifdef WIN32
16086 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16087# else
16088 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16089 < 0)
16090# endif
16091 {
16092 if (r != NULL)
16093 EMSG(r); /* sending worked but evaluation failed */
16094 else
16095 EMSG2(_("E241: Unable to send to %s"), server_name);
16096 return;
16097 }
16098
16099 rettv->vval.v_string = r;
16100
16101 if (argvars[2].v_type != VAR_UNKNOWN)
16102 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016103 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016104 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016105 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016106
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016107 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016108 v.di_tv.v_type = VAR_STRING;
16109 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016110 idvar = get_tv_string_chk(&argvars[2]);
16111 if (idvar != NULL)
16112 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016113 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016114 }
16115}
16116#endif
16117
16118/*
16119 * "remote_expr()" function
16120 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016121 static void
16122f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016123 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016124 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016125{
16126 rettv->v_type = VAR_STRING;
16127 rettv->vval.v_string = NULL;
16128#ifdef FEAT_CLIENTSERVER
16129 remote_common(argvars, rettv, TRUE);
16130#endif
16131}
16132
16133/*
16134 * "remote_foreground()" function
16135 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016136 static void
16137f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016138 typval_T *argvars UNUSED;
16139 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016140{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016141#ifdef FEAT_CLIENTSERVER
16142# ifdef WIN32
16143 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016144 {
16145 char_u *server_name = get_tv_string_chk(&argvars[0]);
16146
16147 if (server_name != NULL)
16148 serverForeground(server_name);
16149 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016150# else
16151 /* Send a foreground() expression to the server. */
16152 argvars[1].v_type = VAR_STRING;
16153 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16154 argvars[2].v_type = VAR_UNKNOWN;
16155 remote_common(argvars, rettv, TRUE);
16156 vim_free(argvars[1].vval.v_string);
16157# endif
16158#endif
16159}
16160
Bram Moolenaar0d660222005-01-07 21:51:51 +000016161 static void
16162f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016163 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016164 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016165{
16166#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016167 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016168 char_u *s = NULL;
16169# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016170 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016171# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016172 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016173
16174 if (check_restricted() || check_secure())
16175 {
16176 rettv->vval.v_number = -1;
16177 return;
16178 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016179 serverid = get_tv_string_chk(&argvars[0]);
16180 if (serverid == NULL)
16181 {
16182 rettv->vval.v_number = -1;
16183 return; /* type error; errmsg already given */
16184 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016185# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016186 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016187 if (n == 0)
16188 rettv->vval.v_number = -1;
16189 else
16190 {
16191 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16192 rettv->vval.v_number = (s != NULL);
16193 }
16194# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016195 if (check_connection() == FAIL)
16196 return;
16197
16198 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016199 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016200# endif
16201
16202 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016204 char_u *retvar;
16205
Bram Moolenaar33570922005-01-25 22:26:29 +000016206 v.di_tv.v_type = VAR_STRING;
16207 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016208 retvar = get_tv_string_chk(&argvars[1]);
16209 if (retvar != NULL)
16210 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016211 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016212 }
16213#else
16214 rettv->vval.v_number = -1;
16215#endif
16216}
16217
Bram Moolenaar0d660222005-01-07 21:51:51 +000016218 static void
16219f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016220 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016221 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016222{
16223 char_u *r = NULL;
16224
16225#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016226 char_u *serverid = get_tv_string_chk(&argvars[0]);
16227
16228 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016229 {
16230# ifdef WIN32
16231 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016232 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016233
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016234 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016235 if (n != 0)
16236 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16237 if (r == NULL)
16238# else
16239 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016240 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016241# endif
16242 EMSG(_("E277: Unable to read a server reply"));
16243 }
16244#endif
16245 rettv->v_type = VAR_STRING;
16246 rettv->vval.v_string = r;
16247}
16248
16249/*
16250 * "remote_send()" function
16251 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016252 static void
16253f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016254 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016255 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016256{
16257 rettv->v_type = VAR_STRING;
16258 rettv->vval.v_string = NULL;
16259#ifdef FEAT_CLIENTSERVER
16260 remote_common(argvars, rettv, FALSE);
16261#endif
16262}
16263
16264/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016265 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016266 */
16267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016268f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 typval_T *argvars;
16270 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016271{
Bram Moolenaar33570922005-01-25 22:26:29 +000016272 list_T *l;
16273 listitem_T *item, *item2;
16274 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016275 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016276 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016277 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016278 dict_T *d;
16279 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016280 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016281
Bram Moolenaar8c711452005-01-14 21:53:12 +000016282 if (argvars[0].v_type == VAR_DICT)
16283 {
16284 if (argvars[2].v_type != VAR_UNKNOWN)
16285 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016286 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016287 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016289 key = get_tv_string_chk(&argvars[1]);
16290 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016292 di = dict_find(d, key, -1);
16293 if (di == NULL)
16294 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016295 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16296 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016297 {
16298 *rettv = di->di_tv;
16299 init_tv(&di->di_tv);
16300 dictitem_remove(d, di);
16301 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016302 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016303 }
16304 }
16305 else if (argvars[0].v_type != VAR_LIST)
16306 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016307 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016308 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016310 int error = FALSE;
16311
16312 idx = get_tv_number_chk(&argvars[1], &error);
16313 if (error)
16314 ; /* type error: do nothing, errmsg already given */
16315 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016316 EMSGN(_(e_listidx), idx);
16317 else
16318 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016319 if (argvars[2].v_type == VAR_UNKNOWN)
16320 {
16321 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016322 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016323 *rettv = item->li_tv;
16324 vim_free(item);
16325 }
16326 else
16327 {
16328 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016329 end = get_tv_number_chk(&argvars[2], &error);
16330 if (error)
16331 ; /* type error: do nothing */
16332 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016333 EMSGN(_(e_listidx), end);
16334 else
16335 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016336 int cnt = 0;
16337
16338 for (li = item; li != NULL; li = li->li_next)
16339 {
16340 ++cnt;
16341 if (li == item2)
16342 break;
16343 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016344 if (li == NULL) /* didn't find "item2" after "item" */
16345 EMSG(_(e_invrange));
16346 else
16347 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016348 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016349 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016350 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016351 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016352 l->lv_first = item;
16353 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016354 item->li_prev = NULL;
16355 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016356 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016357 }
16358 }
16359 }
16360 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016361 }
16362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363}
16364
16365/*
16366 * "rename({from}, {to})" function
16367 */
16368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016369f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016370 typval_T *argvars;
16371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372{
16373 char_u buf[NUMBUFLEN];
16374
16375 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016376 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016378 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16379 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380}
16381
16382/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016383 * "repeat()" function
16384 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016385 static void
16386f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016387 typval_T *argvars;
16388 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016389{
16390 char_u *p;
16391 int n;
16392 int slen;
16393 int len;
16394 char_u *r;
16395 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016396
16397 n = get_tv_number(&argvars[1]);
16398 if (argvars[0].v_type == VAR_LIST)
16399 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016400 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016401 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016402 if (list_extend(rettv->vval.v_list,
16403 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016404 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016405 }
16406 else
16407 {
16408 p = get_tv_string(&argvars[0]);
16409 rettv->v_type = VAR_STRING;
16410 rettv->vval.v_string = NULL;
16411
16412 slen = (int)STRLEN(p);
16413 len = slen * n;
16414 if (len <= 0)
16415 return;
16416
16417 r = alloc(len + 1);
16418 if (r != NULL)
16419 {
16420 for (i = 0; i < n; i++)
16421 mch_memmove(r + i * slen, p, (size_t)slen);
16422 r[len] = NUL;
16423 }
16424
16425 rettv->vval.v_string = r;
16426 }
16427}
16428
16429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430 * "resolve()" function
16431 */
16432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016433f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016434 typval_T *argvars;
16435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436{
16437 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016438#ifdef HAVE_READLINK
16439 char_u *buf = NULL;
16440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016442 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443#ifdef FEAT_SHORTCUT
16444 {
16445 char_u *v = NULL;
16446
16447 v = mch_resolve_shortcut(p);
16448 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016449 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016451 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452 }
16453#else
16454# ifdef HAVE_READLINK
16455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456 char_u *cpy;
16457 int len;
16458 char_u *remain = NULL;
16459 char_u *q;
16460 int is_relative_to_current = FALSE;
16461 int has_trailing_pathsep = FALSE;
16462 int limit = 100;
16463
16464 p = vim_strsave(p);
16465
16466 if (p[0] == '.' && (vim_ispathsep(p[1])
16467 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16468 is_relative_to_current = TRUE;
16469
16470 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016471 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016472 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016474 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476
16477 q = getnextcomp(p);
16478 if (*q != NUL)
16479 {
16480 /* Separate the first path component in "p", and keep the
16481 * remainder (beginning with the path separator). */
16482 remain = vim_strsave(q - 1);
16483 q[-1] = NUL;
16484 }
16485
Bram Moolenaard9462e32011-04-11 21:35:11 +020016486 buf = alloc(MAXPATHL + 1);
16487 if (buf == NULL)
16488 goto fail;
16489
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490 for (;;)
16491 {
16492 for (;;)
16493 {
16494 len = readlink((char *)p, (char *)buf, MAXPATHL);
16495 if (len <= 0)
16496 break;
16497 buf[len] = NUL;
16498
16499 if (limit-- == 0)
16500 {
16501 vim_free(p);
16502 vim_free(remain);
16503 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016504 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 goto fail;
16506 }
16507
16508 /* Ensure that the result will have a trailing path separator
16509 * if the argument has one. */
16510 if (remain == NULL && has_trailing_pathsep)
16511 add_pathsep(buf);
16512
16513 /* Separate the first path component in the link value and
16514 * concatenate the remainders. */
16515 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16516 if (*q != NUL)
16517 {
16518 if (remain == NULL)
16519 remain = vim_strsave(q - 1);
16520 else
16521 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016522 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523 if (cpy != NULL)
16524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 vim_free(remain);
16526 remain = cpy;
16527 }
16528 }
16529 q[-1] = NUL;
16530 }
16531
16532 q = gettail(p);
16533 if (q > p && *q == NUL)
16534 {
16535 /* Ignore trailing path separator. */
16536 q[-1] = NUL;
16537 q = gettail(p);
16538 }
16539 if (q > p && !mch_isFullName(buf))
16540 {
16541 /* symlink is relative to directory of argument */
16542 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16543 if (cpy != NULL)
16544 {
16545 STRCPY(cpy, p);
16546 STRCPY(gettail(cpy), buf);
16547 vim_free(p);
16548 p = cpy;
16549 }
16550 }
16551 else
16552 {
16553 vim_free(p);
16554 p = vim_strsave(buf);
16555 }
16556 }
16557
16558 if (remain == NULL)
16559 break;
16560
16561 /* Append the first path component of "remain" to "p". */
16562 q = getnextcomp(remain + 1);
16563 len = q - remain - (*q != NUL);
16564 cpy = vim_strnsave(p, STRLEN(p) + len);
16565 if (cpy != NULL)
16566 {
16567 STRNCAT(cpy, remain, len);
16568 vim_free(p);
16569 p = cpy;
16570 }
16571 /* Shorten "remain". */
16572 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016573 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574 else
16575 {
16576 vim_free(remain);
16577 remain = NULL;
16578 }
16579 }
16580
16581 /* If the result is a relative path name, make it explicitly relative to
16582 * the current directory if and only if the argument had this form. */
16583 if (!vim_ispathsep(*p))
16584 {
16585 if (is_relative_to_current
16586 && *p != NUL
16587 && !(p[0] == '.'
16588 && (p[1] == NUL
16589 || vim_ispathsep(p[1])
16590 || (p[1] == '.'
16591 && (p[2] == NUL
16592 || vim_ispathsep(p[2]))))))
16593 {
16594 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016595 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016596 if (cpy != NULL)
16597 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598 vim_free(p);
16599 p = cpy;
16600 }
16601 }
16602 else if (!is_relative_to_current)
16603 {
16604 /* Strip leading "./". */
16605 q = p;
16606 while (q[0] == '.' && vim_ispathsep(q[1]))
16607 q += 2;
16608 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016609 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016610 }
16611 }
16612
16613 /* Ensure that the result will have no trailing path separator
16614 * if the argument had none. But keep "/" or "//". */
16615 if (!has_trailing_pathsep)
16616 {
16617 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016618 if (after_pathsep(p, q))
16619 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620 }
16621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016622 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 }
16624# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016625 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626# endif
16627#endif
16628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016629 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630
16631#ifdef HAVE_READLINK
16632fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016633 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016635 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636}
16637
16638/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016639 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640 */
16641 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016642f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016643 typval_T *argvars;
16644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645{
Bram Moolenaar33570922005-01-25 22:26:29 +000016646 list_T *l;
16647 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648
Bram Moolenaar0d660222005-01-07 21:51:51 +000016649 if (argvars[0].v_type != VAR_LIST)
16650 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016651 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016652 && !tv_check_lock(l->lv_lock,
16653 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016654 {
16655 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016656 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016657 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016658 while (li != NULL)
16659 {
16660 ni = li->li_prev;
16661 list_append(l, li);
16662 li = ni;
16663 }
16664 rettv->vval.v_list = l;
16665 rettv->v_type = VAR_LIST;
16666 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016667 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669}
16670
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016671#define SP_NOMOVE 0x01 /* don't move cursor */
16672#define SP_REPEAT 0x02 /* repeat to find outer pair */
16673#define SP_RETCOUNT 0x04 /* return matchcount */
16674#define SP_SETPCMARK 0x08 /* set previous context mark */
16675#define SP_START 0x10 /* accept match at start position */
16676#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16677#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016678#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016679
Bram Moolenaar33570922005-01-25 22:26:29 +000016680static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016681
16682/*
16683 * Get flags for a search function.
16684 * Possibly sets "p_ws".
16685 * Returns BACKWARD, FORWARD or zero (for an error).
16686 */
16687 static int
16688get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016689 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016690 int *flagsp;
16691{
16692 int dir = FORWARD;
16693 char_u *flags;
16694 char_u nbuf[NUMBUFLEN];
16695 int mask;
16696
16697 if (varp->v_type != VAR_UNKNOWN)
16698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016699 flags = get_tv_string_buf_chk(varp, nbuf);
16700 if (flags == NULL)
16701 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016702 while (*flags != NUL)
16703 {
16704 switch (*flags)
16705 {
16706 case 'b': dir = BACKWARD; break;
16707 case 'w': p_ws = TRUE; break;
16708 case 'W': p_ws = FALSE; break;
16709 default: mask = 0;
16710 if (flagsp != NULL)
16711 switch (*flags)
16712 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016713 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016714 case 'e': mask = SP_END; break;
16715 case 'm': mask = SP_RETCOUNT; break;
16716 case 'n': mask = SP_NOMOVE; break;
16717 case 'p': mask = SP_SUBPAT; break;
16718 case 'r': mask = SP_REPEAT; break;
16719 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016720 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016721 }
16722 if (mask == 0)
16723 {
16724 EMSG2(_(e_invarg2), flags);
16725 dir = 0;
16726 }
16727 else
16728 *flagsp |= mask;
16729 }
16730 if (dir == 0)
16731 break;
16732 ++flags;
16733 }
16734 }
16735 return dir;
16736}
16737
Bram Moolenaar071d4272004-06-13 20:20:40 +000016738/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016739 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016741 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016742search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016743 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016744 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016745 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016747 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 char_u *pat;
16749 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016750 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751 int save_p_ws = p_ws;
16752 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016753 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016754 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016755 proftime_T tm;
16756#ifdef FEAT_RELTIME
16757 long time_limit = 0;
16758#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016759 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016760 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016762 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016763 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016764 if (dir == 0)
16765 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016766 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016767 if (flags & SP_START)
16768 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016769 if (flags & SP_END)
16770 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016771 if (flags & SP_COLUMN)
16772 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016773
Bram Moolenaar76929292008-01-06 19:07:36 +000016774 /* Optional arguments: line number to stop searching and timeout. */
16775 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016776 {
16777 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16778 if (lnum_stop < 0)
16779 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016780#ifdef FEAT_RELTIME
16781 if (argvars[3].v_type != VAR_UNKNOWN)
16782 {
16783 time_limit = get_tv_number_chk(&argvars[3], NULL);
16784 if (time_limit < 0)
16785 goto theend;
16786 }
16787#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016788 }
16789
Bram Moolenaar76929292008-01-06 19:07:36 +000016790#ifdef FEAT_RELTIME
16791 /* Set the time limit, if there is one. */
16792 profile_setlimit(time_limit, &tm);
16793#endif
16794
Bram Moolenaar231334e2005-07-25 20:46:57 +000016795 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016796 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016797 * Check to make sure only those flags are set.
16798 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16799 * flags cannot be set. Check for that condition also.
16800 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016801 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016802 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016804 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016805 goto theend;
16806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016808 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016809 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016810 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016811 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016813 if (flags & SP_SUBPAT)
16814 retval = subpatnum;
16815 else
16816 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016817 if (flags & SP_SETPCMARK)
16818 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016820 if (match_pos != NULL)
16821 {
16822 /* Store the match cursor position */
16823 match_pos->lnum = pos.lnum;
16824 match_pos->col = pos.col + 1;
16825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 /* "/$" will put the cursor after the end of the line, may need to
16827 * correct that here */
16828 check_cursor();
16829 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016830
16831 /* If 'n' flag is used: restore cursor position. */
16832 if (flags & SP_NOMOVE)
16833 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016834 else
16835 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016836theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016837 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016838
16839 return retval;
16840}
16841
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016842#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016843
16844/*
16845 * round() is not in C90, use ceil() or floor() instead.
16846 */
16847 float_T
16848vim_round(f)
16849 float_T f;
16850{
16851 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16852}
16853
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016854/*
16855 * "round({float})" function
16856 */
16857 static void
16858f_round(argvars, rettv)
16859 typval_T *argvars;
16860 typval_T *rettv;
16861{
16862 float_T f;
16863
16864 rettv->v_type = VAR_FLOAT;
16865 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016866 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016867 else
16868 rettv->vval.v_float = 0.0;
16869}
16870#endif
16871
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016872/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016873 * "screenattr()" function
16874 */
16875 static void
16876f_screenattr(argvars, rettv)
16877 typval_T *argvars UNUSED;
16878 typval_T *rettv;
16879{
16880 int row;
16881 int col;
16882 int c;
16883
16884 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16885 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16886 if (row < 0 || row >= screen_Rows
16887 || col < 0 || col >= screen_Columns)
16888 c = -1;
16889 else
16890 c = ScreenAttrs[LineOffset[row] + col];
16891 rettv->vval.v_number = c;
16892}
16893
16894/*
16895 * "screenchar()" function
16896 */
16897 static void
16898f_screenchar(argvars, rettv)
16899 typval_T *argvars UNUSED;
16900 typval_T *rettv;
16901{
16902 int row;
16903 int col;
16904 int off;
16905 int c;
16906
16907 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16908 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16909 if (row < 0 || row >= screen_Rows
16910 || col < 0 || col >= screen_Columns)
16911 c = -1;
16912 else
16913 {
16914 off = LineOffset[row] + col;
16915#ifdef FEAT_MBYTE
16916 if (enc_utf8 && ScreenLinesUC[off] != 0)
16917 c = ScreenLinesUC[off];
16918 else
16919#endif
16920 c = ScreenLines[off];
16921 }
16922 rettv->vval.v_number = c;
16923}
16924
16925/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016926 * "screencol()" function
16927 *
16928 * First column is 1 to be consistent with virtcol().
16929 */
16930 static void
16931f_screencol(argvars, rettv)
16932 typval_T *argvars UNUSED;
16933 typval_T *rettv;
16934{
16935 rettv->vval.v_number = screen_screencol() + 1;
16936}
16937
16938/*
16939 * "screenrow()" function
16940 */
16941 static void
16942f_screenrow(argvars, rettv)
16943 typval_T *argvars UNUSED;
16944 typval_T *rettv;
16945{
16946 rettv->vval.v_number = screen_screenrow() + 1;
16947}
16948
16949/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016950 * "search()" function
16951 */
16952 static void
16953f_search(argvars, rettv)
16954 typval_T *argvars;
16955 typval_T *rettv;
16956{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016957 int flags = 0;
16958
16959 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960}
16961
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016963 * "searchdecl()" function
16964 */
16965 static void
16966f_searchdecl(argvars, rettv)
16967 typval_T *argvars;
16968 typval_T *rettv;
16969{
16970 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016971 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016972 int error = FALSE;
16973 char_u *name;
16974
16975 rettv->vval.v_number = 1; /* default: FAIL */
16976
16977 name = get_tv_string_chk(&argvars[0]);
16978 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016979 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016980 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016981 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16982 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16983 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016984 if (!error && name != NULL)
16985 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016986 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016987}
16988
16989/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016990 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016992 static int
16993searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016994 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016995 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996{
16997 char_u *spat, *mpat, *epat;
16998 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 int dir;
17001 int flags = 0;
17002 char_u nbuf1[NUMBUFLEN];
17003 char_u nbuf2[NUMBUFLEN];
17004 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017005 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017006 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017007 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017010 spat = get_tv_string_chk(&argvars[0]);
17011 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17012 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17013 if (spat == NULL || mpat == NULL || epat == NULL)
17014 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 /* Handle the optional fourth argument: flags */
17017 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017018 if (dir == 0)
17019 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017020
17021 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017022 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17023 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017024 if ((flags & (SP_END | SP_SUBPAT)) != 0
17025 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017026 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017027 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017028 goto theend;
17029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017031 /* Using 'r' implies 'W', otherwise it doesn't work. */
17032 if (flags & SP_REPEAT)
17033 p_ws = FALSE;
17034
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017035 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017036 if (argvars[3].v_type == VAR_UNKNOWN
17037 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 skip = (char_u *)"";
17039 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017041 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017042 if (argvars[5].v_type != VAR_UNKNOWN)
17043 {
17044 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17045 if (lnum_stop < 0)
17046 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017047#ifdef FEAT_RELTIME
17048 if (argvars[6].v_type != VAR_UNKNOWN)
17049 {
17050 time_limit = get_tv_number_chk(&argvars[6], NULL);
17051 if (time_limit < 0)
17052 goto theend;
17053 }
17054#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017055 }
17056 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017057 if (skip == NULL)
17058 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017060 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017061 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017062
17063theend:
17064 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017065
17066 return retval;
17067}
17068
17069/*
17070 * "searchpair()" function
17071 */
17072 static void
17073f_searchpair(argvars, rettv)
17074 typval_T *argvars;
17075 typval_T *rettv;
17076{
17077 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17078}
17079
17080/*
17081 * "searchpairpos()" function
17082 */
17083 static void
17084f_searchpairpos(argvars, rettv)
17085 typval_T *argvars;
17086 typval_T *rettv;
17087{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017088 pos_T match_pos;
17089 int lnum = 0;
17090 int col = 0;
17091
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017092 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017093 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017094
17095 if (searchpair_cmn(argvars, &match_pos) > 0)
17096 {
17097 lnum = match_pos.lnum;
17098 col = match_pos.col;
17099 }
17100
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017101 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17102 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017103}
17104
17105/*
17106 * Search for a start/middle/end thing.
17107 * Used by searchpair(), see its documentation for the details.
17108 * Returns 0 or -1 for no match,
17109 */
17110 long
Bram Moolenaar76929292008-01-06 19:07:36 +000017111do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
17112 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017113 char_u *spat; /* start pattern */
17114 char_u *mpat; /* middle pattern */
17115 char_u *epat; /* end pattern */
17116 int dir; /* BACKWARD or FORWARD */
17117 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017118 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017119 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017120 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017121 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017122{
17123 char_u *save_cpo;
17124 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17125 long retval = 0;
17126 pos_T pos;
17127 pos_T firstpos;
17128 pos_T foundpos;
17129 pos_T save_cursor;
17130 pos_T save_pos;
17131 int n;
17132 int r;
17133 int nest = 1;
17134 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017135 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017136 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017137
17138 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17139 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017140 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017141
Bram Moolenaar76929292008-01-06 19:07:36 +000017142#ifdef FEAT_RELTIME
17143 /* Set the time limit, if there is one. */
17144 profile_setlimit(time_limit, &tm);
17145#endif
17146
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017147 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17148 * start/middle/end (pat3, for the top pair). */
17149 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17150 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17151 if (pat2 == NULL || pat3 == NULL)
17152 goto theend;
17153 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17154 if (*mpat == NUL)
17155 STRCPY(pat3, pat2);
17156 else
17157 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17158 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017159 if (flags & SP_START)
17160 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017161
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162 save_cursor = curwin->w_cursor;
17163 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017164 clearpos(&firstpos);
17165 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 pat = pat3;
17167 for (;;)
17168 {
17169 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017170 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17172 /* didn't find it or found the first match again: FAIL */
17173 break;
17174
17175 if (firstpos.lnum == 0)
17176 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017177 if (equalpos(pos, foundpos))
17178 {
17179 /* Found the same position again. Can happen with a pattern that
17180 * has "\zs" at the end and searching backwards. Advance one
17181 * character and try again. */
17182 if (dir == BACKWARD)
17183 decl(&pos);
17184 else
17185 incl(&pos);
17186 }
17187 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017189 /* clear the start flag to avoid getting stuck here */
17190 options &= ~SEARCH_START;
17191
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192 /* If the skip pattern matches, ignore this match. */
17193 if (*skip != NUL)
17194 {
17195 save_pos = curwin->w_cursor;
17196 curwin->w_cursor = pos;
17197 r = eval_to_bool(skip, &err, NULL, FALSE);
17198 curwin->w_cursor = save_pos;
17199 if (err)
17200 {
17201 /* Evaluating {skip} caused an error, break here. */
17202 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017203 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204 break;
17205 }
17206 if (r)
17207 continue;
17208 }
17209
17210 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17211 {
17212 /* Found end when searching backwards or start when searching
17213 * forward: nested pair. */
17214 ++nest;
17215 pat = pat2; /* nested, don't search for middle */
17216 }
17217 else
17218 {
17219 /* Found end when searching forward or start when searching
17220 * backward: end of (nested) pair; or found middle in outer pair. */
17221 if (--nest == 1)
17222 pat = pat3; /* outer level, search for middle */
17223 }
17224
17225 if (nest == 0)
17226 {
17227 /* Found the match: return matchcount or line number. */
17228 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017229 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017231 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017232 if (flags & SP_SETPCMARK)
17233 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234 curwin->w_cursor = pos;
17235 if (!(flags & SP_REPEAT))
17236 break;
17237 nest = 1; /* search for next unmatched */
17238 }
17239 }
17240
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017241 if (match_pos != NULL)
17242 {
17243 /* Store the match cursor position */
17244 match_pos->lnum = curwin->w_cursor.lnum;
17245 match_pos->col = curwin->w_cursor.col + 1;
17246 }
17247
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017249 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 curwin->w_cursor = save_cursor;
17251
17252theend:
17253 vim_free(pat2);
17254 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017255 if (p_cpo == empty_option)
17256 p_cpo = save_cpo;
17257 else
17258 /* Darn, evaluating the {skip} expression changed the value. */
17259 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017260
17261 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262}
17263
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017264/*
17265 * "searchpos()" function
17266 */
17267 static void
17268f_searchpos(argvars, rettv)
17269 typval_T *argvars;
17270 typval_T *rettv;
17271{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017272 pos_T match_pos;
17273 int lnum = 0;
17274 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017275 int n;
17276 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017277
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017278 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017279 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017280
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017281 n = search_cmn(argvars, &match_pos, &flags);
17282 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017283 {
17284 lnum = match_pos.lnum;
17285 col = match_pos.col;
17286 }
17287
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017288 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17289 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017290 if (flags & SP_SUBPAT)
17291 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017292}
17293
17294
Bram Moolenaar0d660222005-01-07 21:51:51 +000017295 static void
17296f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017297 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017300#ifdef FEAT_CLIENTSERVER
17301 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017302 char_u *server = get_tv_string_chk(&argvars[0]);
17303 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304
Bram Moolenaar0d660222005-01-07 21:51:51 +000017305 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017306 if (server == NULL || reply == NULL)
17307 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017308 if (check_restricted() || check_secure())
17309 return;
17310# ifdef FEAT_X11
17311 if (check_connection() == FAIL)
17312 return;
17313# endif
17314
17315 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017317 EMSG(_("E258: Unable to send to client"));
17318 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017320 rettv->vval.v_number = 0;
17321#else
17322 rettv->vval.v_number = -1;
17323#endif
17324}
17325
Bram Moolenaar0d660222005-01-07 21:51:51 +000017326 static void
17327f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017328 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017329 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017330{
17331 char_u *r = NULL;
17332
17333#ifdef FEAT_CLIENTSERVER
17334# ifdef WIN32
17335 r = serverGetVimNames();
17336# else
17337 make_connection();
17338 if (X_DISPLAY != NULL)
17339 r = serverGetVimNames(X_DISPLAY);
17340# endif
17341#endif
17342 rettv->v_type = VAR_STRING;
17343 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344}
17345
17346/*
17347 * "setbufvar()" function
17348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017350f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017351 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017352 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353{
17354 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017357 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358 char_u nbuf[NUMBUFLEN];
17359
17360 if (check_restricted() || check_secure())
17361 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017362 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17363 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017364 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365 varp = &argvars[2];
17366
17367 if (buf != NULL && varname != NULL && varp != NULL)
17368 {
17369 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371
17372 if (*varname == '&')
17373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017374 long numval;
17375 char_u *strval;
17376 int error = FALSE;
17377
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017379 numval = get_tv_number_chk(varp, &error);
17380 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017381 if (!error && strval != NULL)
17382 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 }
17384 else
17385 {
17386 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17387 if (bufvarname != NULL)
17388 {
17389 STRCPY(bufvarname, "b:");
17390 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017391 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 vim_free(bufvarname);
17393 }
17394 }
17395
17396 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399}
17400
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017401 static void
17402f_setcharsearch(argvars, rettv)
17403 typval_T *argvars;
17404 typval_T *rettv UNUSED;
17405{
17406 dict_T *d;
17407 dictitem_T *di;
17408 char_u *csearch;
17409
17410 if (argvars[0].v_type != VAR_DICT)
17411 {
17412 EMSG(_(e_dictreq));
17413 return;
17414 }
17415
17416 if ((d = argvars[0].vval.v_dict) != NULL)
17417 {
17418 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17419 if (csearch != NULL)
17420 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017421#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017422 if (enc_utf8)
17423 {
17424 int pcc[MAX_MCO];
17425 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017426
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017427 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17428 }
17429 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017430#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017431 set_last_csearch(PTR2CHAR(csearch),
17432 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017433 }
17434
17435 di = dict_find(d, (char_u *)"forward", -1);
17436 if (di != NULL)
17437 set_csearch_direction(get_tv_number(&di->di_tv)
17438 ? FORWARD : BACKWARD);
17439
17440 di = dict_find(d, (char_u *)"until", -1);
17441 if (di != NULL)
17442 set_csearch_until(!!get_tv_number(&di->di_tv));
17443 }
17444}
17445
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446/*
17447 * "setcmdpos()" function
17448 */
17449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017450f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017451 typval_T *argvars;
17452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017454 int pos = (int)get_tv_number(&argvars[0]) - 1;
17455
17456 if (pos >= 0)
17457 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458}
17459
17460/*
17461 * "setline()" function
17462 */
17463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017464f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017465 typval_T *argvars;
17466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467{
17468 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017469 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017470 list_T *l = NULL;
17471 listitem_T *li = NULL;
17472 long added = 0;
17473 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017475 lnum = get_tv_lnum(&argvars[0]);
17476 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017478 l = argvars[1].vval.v_list;
17479 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017481 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017482 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017483
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017484 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017485 for (;;)
17486 {
17487 if (l != NULL)
17488 {
17489 /* list argument, get next string */
17490 if (li == NULL)
17491 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017492 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017493 li = li->li_next;
17494 }
17495
17496 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017497 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017498 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017499
17500 /* When coming here from Insert mode, sync undo, so that this can be
17501 * undone separately from what was previously inserted. */
17502 if (u_sync_once == 2)
17503 {
17504 u_sync_once = 1; /* notify that u_sync() was called */
17505 u_sync(TRUE);
17506 }
17507
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017508 if (lnum <= curbuf->b_ml.ml_line_count)
17509 {
17510 /* existing line, replace it */
17511 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17512 {
17513 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017514 if (lnum == curwin->w_cursor.lnum)
17515 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017516 rettv->vval.v_number = 0; /* OK */
17517 }
17518 }
17519 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17520 {
17521 /* lnum is one past the last line, append the line */
17522 ++added;
17523 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17524 rettv->vval.v_number = 0; /* OK */
17525 }
17526
17527 if (l == NULL) /* only one string argument */
17528 break;
17529 ++lnum;
17530 }
17531
17532 if (added > 0)
17533 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534}
17535
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017536static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17537
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017539 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017540 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017541 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017542set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017543 win_T *wp UNUSED;
17544 typval_T *list_arg UNUSED;
17545 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017546 typval_T *rettv;
17547{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017548#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017549 char_u *act;
17550 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017551#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017552
Bram Moolenaar2641f772005-03-25 21:58:17 +000017553 rettv->vval.v_number = -1;
17554
17555#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017556 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017557 EMSG(_(e_listreq));
17558 else
17559 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017560 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017561
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017562 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017563 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017564 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017565 if (act == NULL)
17566 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017567 if (*act == 'a' || *act == 'r')
17568 action = *act;
17569 }
17570
Bram Moolenaar81484f42012-12-05 15:16:47 +010017571 if (l != NULL && set_errorlist(wp, l, action,
17572 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017573 rettv->vval.v_number = 0;
17574 }
17575#endif
17576}
17577
17578/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017579 * "setloclist()" function
17580 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017581 static void
17582f_setloclist(argvars, rettv)
17583 typval_T *argvars;
17584 typval_T *rettv;
17585{
17586 win_T *win;
17587
17588 rettv->vval.v_number = -1;
17589
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017590 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017591 if (win != NULL)
17592 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17593}
17594
17595/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017596 * "setmatches()" function
17597 */
17598 static void
17599f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017600 typval_T *argvars UNUSED;
17601 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017602{
17603#ifdef FEAT_SEARCH_EXTRA
17604 list_T *l;
17605 listitem_T *li;
17606 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017607 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017608
17609 rettv->vval.v_number = -1;
17610 if (argvars[0].v_type != VAR_LIST)
17611 {
17612 EMSG(_(e_listreq));
17613 return;
17614 }
17615 if ((l = argvars[0].vval.v_list) != NULL)
17616 {
17617
17618 /* To some extent make sure that we are dealing with a list from
17619 * "getmatches()". */
17620 li = l->lv_first;
17621 while (li != NULL)
17622 {
17623 if (li->li_tv.v_type != VAR_DICT
17624 || (d = li->li_tv.vval.v_dict) == NULL)
17625 {
17626 EMSG(_(e_invarg));
17627 return;
17628 }
17629 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017630 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17631 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017632 && dict_find(d, (char_u *)"priority", -1) != NULL
17633 && dict_find(d, (char_u *)"id", -1) != NULL))
17634 {
17635 EMSG(_(e_invarg));
17636 return;
17637 }
17638 li = li->li_next;
17639 }
17640
17641 clear_matches(curwin);
17642 li = l->lv_first;
17643 while (li != NULL)
17644 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017645 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017646 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017647 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017648 char_u *group;
17649 int priority;
17650 int id;
17651 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017652
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017653 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017654 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17655 {
17656 if (s == NULL)
17657 {
17658 s = list_alloc();
17659 if (s == NULL)
17660 return;
17661 }
17662
17663 /* match from matchaddpos() */
17664 for (i = 1; i < 9; i++)
17665 {
17666 sprintf((char *)buf, (char *)"pos%d", i);
17667 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17668 {
17669 if (di->di_tv.v_type != VAR_LIST)
17670 return;
17671
17672 list_append_tv(s, &di->di_tv);
17673 s->lv_refcount++;
17674 }
17675 else
17676 break;
17677 }
17678 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017679
17680 group = get_dict_string(d, (char_u *)"group", FALSE);
17681 priority = (int)get_dict_number(d, (char_u *)"priority");
17682 id = (int)get_dict_number(d, (char_u *)"id");
17683 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17684 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17685 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017686 if (i == 0)
17687 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017688 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017689 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017690 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017691 }
17692 else
17693 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017694 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017695 list_unref(s);
17696 s = NULL;
17697 }
17698
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017699 li = li->li_next;
17700 }
17701 rettv->vval.v_number = 0;
17702 }
17703#endif
17704}
17705
17706/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017707 * "setpos()" function
17708 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017709 static void
17710f_setpos(argvars, rettv)
17711 typval_T *argvars;
17712 typval_T *rettv;
17713{
17714 pos_T pos;
17715 int fnum;
17716 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017717 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017718
Bram Moolenaar08250432008-02-13 11:42:46 +000017719 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017720 name = get_tv_string_chk(argvars);
17721 if (name != NULL)
17722 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017723 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017724 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017725 if (--pos.col < 0)
17726 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017727 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017728 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017729 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017730 if (fnum == curbuf->b_fnum)
17731 {
17732 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017733 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017734 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017735 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017736 curwin->w_set_curswant = FALSE;
17737 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017738 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017739 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017740 }
17741 else
17742 EMSG(_(e_invarg));
17743 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017744 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17745 {
17746 /* set mark */
17747 if (setmark_pos(name[1], &pos, fnum) == OK)
17748 rettv->vval.v_number = 0;
17749 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017750 else
17751 EMSG(_(e_invarg));
17752 }
17753 }
17754}
17755
17756/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017757 * "setqflist()" function
17758 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017759 static void
17760f_setqflist(argvars, rettv)
17761 typval_T *argvars;
17762 typval_T *rettv;
17763{
17764 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17765}
17766
17767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768 * "setreg()" function
17769 */
17770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017771f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017772 typval_T *argvars;
17773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774{
17775 int regname;
17776 char_u *strregname;
17777 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017778 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779 int append;
17780 char_u yank_type;
17781 long block_len;
17782
17783 block_len = -1;
17784 yank_type = MAUTO;
17785 append = FALSE;
17786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017787 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017788 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017790 if (strregname == NULL)
17791 return; /* type error; errmsg already given */
17792 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793 if (regname == 0 || regname == '@')
17794 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017796 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017798 stropt = get_tv_string_chk(&argvars[2]);
17799 if (stropt == NULL)
17800 return; /* type error */
17801 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802 switch (*stropt)
17803 {
17804 case 'a': case 'A': /* append */
17805 append = TRUE;
17806 break;
17807 case 'v': case 'c': /* character-wise selection */
17808 yank_type = MCHAR;
17809 break;
17810 case 'V': case 'l': /* line-wise selection */
17811 yank_type = MLINE;
17812 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813 case 'b': case Ctrl_V: /* block-wise selection */
17814 yank_type = MBLOCK;
17815 if (VIM_ISDIGIT(stropt[1]))
17816 {
17817 ++stropt;
17818 block_len = getdigits(&stropt) - 1;
17819 --stropt;
17820 }
17821 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822 }
17823 }
17824
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017825 if (argvars[1].v_type == VAR_LIST)
17826 {
17827 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017828 char_u **allocval;
17829 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017830 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017831 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017832 int len = argvars[1].vval.v_list->lv_len;
17833 listitem_T *li;
17834
Bram Moolenaar7d647822014-04-05 21:28:56 +020017835 /* First half: use for pointers to result lines; second half: use for
17836 * pointers to allocated copies. */
17837 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017838 if (lstval == NULL)
17839 return;
17840 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017841 allocval = lstval + len + 2;
17842 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017843
17844 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17845 li = li->li_next)
17846 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017847 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017848 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017849 goto free_lstval;
17850 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017851 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017852 /* Need to make a copy, next get_tv_string_buf_chk() will
17853 * overwrite the string. */
17854 strval = vim_strsave(buf);
17855 if (strval == NULL)
17856 goto free_lstval;
17857 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017858 }
17859 *curval++ = strval;
17860 }
17861 *curval++ = NULL;
17862
17863 write_reg_contents_lst(regname, lstval, -1,
17864 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017865free_lstval:
17866 while (curallocval > allocval)
17867 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017868 vim_free(lstval);
17869 }
17870 else
17871 {
17872 strval = get_tv_string_chk(&argvars[1]);
17873 if (strval == NULL)
17874 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017875 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017877 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017878 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879}
17880
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017881/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017882 * "settabvar()" function
17883 */
17884 static void
17885f_settabvar(argvars, rettv)
17886 typval_T *argvars;
17887 typval_T *rettv;
17888{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017889#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017890 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017891 tabpage_T *tp;
17892#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017893 char_u *varname, *tabvarname;
17894 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017895
17896 rettv->vval.v_number = 0;
17897
17898 if (check_restricted() || check_secure())
17899 return;
17900
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017901#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017902 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017903#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017904 varname = get_tv_string_chk(&argvars[1]);
17905 varp = &argvars[2];
17906
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017907 if (varname != NULL && varp != NULL
17908#ifdef FEAT_WINDOWS
17909 && tp != NULL
17910#endif
17911 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017912 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017913#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017914 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017915 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017916#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017917
17918 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17919 if (tabvarname != NULL)
17920 {
17921 STRCPY(tabvarname, "t:");
17922 STRCPY(tabvarname + 2, varname);
17923 set_var(tabvarname, varp, TRUE);
17924 vim_free(tabvarname);
17925 }
17926
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017927#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017928 /* Restore current tabpage */
17929 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017930 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017931#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017932 }
17933}
17934
17935/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017936 * "settabwinvar()" function
17937 */
17938 static void
17939f_settabwinvar(argvars, rettv)
17940 typval_T *argvars;
17941 typval_T *rettv;
17942{
17943 setwinvar(argvars, rettv, 1);
17944}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945
17946/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017947 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017950f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017951 typval_T *argvars;
17952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017954 setwinvar(argvars, rettv, 0);
17955}
17956
17957/*
17958 * "setwinvar()" and "settabwinvar()" functions
17959 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017960
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017961 static void
17962setwinvar(argvars, rettv, off)
17963 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017964 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017965 int off;
17966{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967 win_T *win;
17968#ifdef FEAT_WINDOWS
17969 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017970 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017971 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972#endif
17973 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017974 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017976 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977
17978 if (check_restricted() || check_secure())
17979 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017980
17981#ifdef FEAT_WINDOWS
17982 if (off == 1)
17983 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17984 else
17985 tp = curtab;
17986#endif
17987 win = find_win_by_nr(&argvars[off], tp);
17988 varname = get_tv_string_chk(&argvars[off + 1]);
17989 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990
17991 if (win != NULL && varname != NULL && varp != NULL)
17992 {
17993#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017994 need_switch_win = !(tp == curtab && win == curwin);
17995 if (!need_switch_win
17996 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017999 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018001 long numval;
18002 char_u *strval;
18003 int error = FALSE;
18004
18005 ++varname;
18006 numval = get_tv_number_chk(varp, &error);
18007 strval = get_tv_string_buf_chk(varp, nbuf);
18008 if (!error && strval != NULL)
18009 set_option_value(varname, numval, strval, OPT_LOCAL);
18010 }
18011 else
18012 {
18013 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18014 if (winvarname != NULL)
18015 {
18016 STRCPY(winvarname, "w:");
18017 STRCPY(winvarname + 2, varname);
18018 set_var(winvarname, varp, TRUE);
18019 vim_free(winvarname);
18020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 }
18022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018024 if (need_switch_win)
18025 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026#endif
18027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028}
18029
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018030#ifdef FEAT_CRYPT
18031/*
18032 * "sha256({string})" function
18033 */
18034 static void
18035f_sha256(argvars, rettv)
18036 typval_T *argvars;
18037 typval_T *rettv;
18038{
18039 char_u *p;
18040
18041 p = get_tv_string(&argvars[0]);
18042 rettv->vval.v_string = vim_strsave(
18043 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18044 rettv->v_type = VAR_STRING;
18045}
18046#endif /* FEAT_CRYPT */
18047
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018049 * "shellescape({string})" function
18050 */
18051 static void
18052f_shellescape(argvars, rettv)
18053 typval_T *argvars;
18054 typval_T *rettv;
18055{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018056 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018057 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018058 rettv->v_type = VAR_STRING;
18059}
18060
18061/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018062 * shiftwidth() function
18063 */
18064 static void
18065f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020018066 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018067 typval_T *rettv;
18068{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018069 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018070}
18071
18072/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018073 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 */
18075 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000018076f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018077 typval_T *argvars;
18078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018080 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081
Bram Moolenaar0d660222005-01-07 21:51:51 +000018082 p = get_tv_string(&argvars[0]);
18083 rettv->vval.v_string = vim_strsave(p);
18084 simplify_filename(rettv->vval.v_string); /* simplify in place */
18085 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086}
18087
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018088#ifdef FEAT_FLOAT
18089/*
18090 * "sin()" function
18091 */
18092 static void
18093f_sin(argvars, rettv)
18094 typval_T *argvars;
18095 typval_T *rettv;
18096{
18097 float_T f;
18098
18099 rettv->v_type = VAR_FLOAT;
18100 if (get_float_arg(argvars, &f) == OK)
18101 rettv->vval.v_float = sin(f);
18102 else
18103 rettv->vval.v_float = 0.0;
18104}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018105
18106/*
18107 * "sinh()" function
18108 */
18109 static void
18110f_sinh(argvars, rettv)
18111 typval_T *argvars;
18112 typval_T *rettv;
18113{
18114 float_T f;
18115
18116 rettv->v_type = VAR_FLOAT;
18117 if (get_float_arg(argvars, &f) == OK)
18118 rettv->vval.v_float = sinh(f);
18119 else
18120 rettv->vval.v_float = 0.0;
18121}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018122#endif
18123
Bram Moolenaar0d660222005-01-07 21:51:51 +000018124static int
18125#ifdef __BORLANDC__
18126 _RTLENTRYF
18127#endif
18128 item_compare __ARGS((const void *s1, const void *s2));
18129static int
18130#ifdef __BORLANDC__
18131 _RTLENTRYF
18132#endif
18133 item_compare2 __ARGS((const void *s1, const void *s2));
18134
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018135/* struct used in the array that's given to qsort() */
18136typedef struct
18137{
18138 listitem_T *item;
18139 int idx;
18140} sortItem_T;
18141
Bram Moolenaar0d660222005-01-07 21:51:51 +000018142static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018143static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018144static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018145static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018146static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018147static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018148static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018149static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018150#define ITEM_COMPARE_FAIL 999
18151
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018153 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018155 static int
18156#ifdef __BORLANDC__
18157_RTLENTRYF
18158#endif
18159item_compare(s1, s2)
18160 const void *s1;
18161 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018163 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018164 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018165 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018166 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018167 int res;
18168 char_u numbuf1[NUMBUFLEN];
18169 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018171 si1 = (sortItem_T *)s1;
18172 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018173 tv1 = &si1->item->li_tv;
18174 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018175
18176 if (item_compare_numbers)
18177 {
18178 long v1 = get_tv_number(tv1);
18179 long v2 = get_tv_number(tv2);
18180
18181 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18182 }
18183
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018184 /* tv2string() puts quotes around a string and allocates memory. Don't do
18185 * that for string variables. Use a single quote when comparing with a
18186 * non-string to do what the docs promise. */
18187 if (tv1->v_type == VAR_STRING)
18188 {
18189 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18190 p1 = (char_u *)"'";
18191 else
18192 p1 = tv1->vval.v_string;
18193 }
18194 else
18195 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18196 if (tv2->v_type == VAR_STRING)
18197 {
18198 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18199 p2 = (char_u *)"'";
18200 else
18201 p2 = tv2->vval.v_string;
18202 }
18203 else
18204 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018205 if (p1 == NULL)
18206 p1 = (char_u *)"";
18207 if (p2 == NULL)
18208 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018209 if (!item_compare_numeric)
18210 {
18211 if (item_compare_ic)
18212 res = STRICMP(p1, p2);
18213 else
18214 res = STRCMP(p1, p2);
18215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018217 {
18218 double n1, n2;
18219 n1 = strtod((char *)p1, (char **)&p1);
18220 n2 = strtod((char *)p2, (char **)&p2);
18221 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18222 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018223
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018224 /* When the result would be zero, compare the item indexes. Makes the
18225 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018226 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018227 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018228
Bram Moolenaar0d660222005-01-07 21:51:51 +000018229 vim_free(tofree1);
18230 vim_free(tofree2);
18231 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232}
18233
18234 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018235#ifdef __BORLANDC__
18236_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018238item_compare2(s1, s2)
18239 const void *s1;
18240 const void *s2;
18241{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018242 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018243 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018244 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018245 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018246 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018248 /* shortcut after failure in previous call; compare all items equal */
18249 if (item_compare_func_err)
18250 return 0;
18251
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018252 si1 = (sortItem_T *)s1;
18253 si2 = (sortItem_T *)s2;
18254
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018255 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018256 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018257 copy_tv(&si1->item->li_tv, &argv[0]);
18258 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018259
18260 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018261 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018262 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18263 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264 clear_tv(&argv[0]);
18265 clear_tv(&argv[1]);
18266
18267 if (res == FAIL)
18268 res = ITEM_COMPARE_FAIL;
18269 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018270 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18271 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018272 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018273 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018274
18275 /* When the result would be zero, compare the pointers themselves. Makes
18276 * the sort stable. */
18277 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018278 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018279
Bram Moolenaar0d660222005-01-07 21:51:51 +000018280 return res;
18281}
18282
18283/*
18284 * "sort({list})" function
18285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018287do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018288 typval_T *argvars;
18289 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018290 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291{
Bram Moolenaar33570922005-01-25 22:26:29 +000018292 list_T *l;
18293 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018294 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018295 long len;
18296 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297
Bram Moolenaar0d660222005-01-07 21:51:51 +000018298 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018299 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300 else
18301 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018302 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018303 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018304 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18305 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018306 return;
18307 rettv->vval.v_list = l;
18308 rettv->v_type = VAR_LIST;
18309 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310
Bram Moolenaar0d660222005-01-07 21:51:51 +000018311 len = list_len(l);
18312 if (len <= 1)
18313 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314
Bram Moolenaar0d660222005-01-07 21:51:51 +000018315 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018316 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018317 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018318 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018319 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018320 if (argvars[1].v_type != VAR_UNKNOWN)
18321 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018322 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018323 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018324 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018325 else
18326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018327 int error = FALSE;
18328
18329 i = get_tv_number_chk(&argvars[1], &error);
18330 if (error)
18331 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018332 if (i == 1)
18333 item_compare_ic = TRUE;
18334 else
18335 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018336 if (item_compare_func != NULL)
18337 {
18338 if (STRCMP(item_compare_func, "n") == 0)
18339 {
18340 item_compare_func = NULL;
18341 item_compare_numeric = TRUE;
18342 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018343 else if (STRCMP(item_compare_func, "N") == 0)
18344 {
18345 item_compare_func = NULL;
18346 item_compare_numbers = TRUE;
18347 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018348 else if (STRCMP(item_compare_func, "i") == 0)
18349 {
18350 item_compare_func = NULL;
18351 item_compare_ic = TRUE;
18352 }
18353 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018354 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018355
18356 if (argvars[2].v_type != VAR_UNKNOWN)
18357 {
18358 /* optional third argument: {dict} */
18359 if (argvars[2].v_type != VAR_DICT)
18360 {
18361 EMSG(_(e_dictreq));
18362 return;
18363 }
18364 item_compare_selfdict = argvars[2].vval.v_dict;
18365 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367
Bram Moolenaar0d660222005-01-07 21:51:51 +000018368 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018369 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018370 if (ptrs == NULL)
18371 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372
Bram Moolenaar327aa022014-03-25 18:24:23 +010018373 i = 0;
18374 if (sort)
18375 {
18376 /* sort(): ptrs will be the list to sort */
18377 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018378 {
18379 ptrs[i].item = li;
18380 ptrs[i].idx = i;
18381 ++i;
18382 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018383
18384 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018385 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018386 /* test the compare function */
18387 if (item_compare_func != NULL
18388 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018389 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018390 EMSG(_("E702: Sort compare function failed"));
18391 else
18392 {
18393 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018394 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018395 item_compare_func == NULL ? item_compare : item_compare2);
18396
18397 if (!item_compare_func_err)
18398 {
18399 /* Clear the List and append the items in sorted order. */
18400 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18401 l->lv_len = 0;
18402 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018403 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018404 }
18405 }
18406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018408 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018409 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18410
18411 /* f_uniq(): ptrs will be a stack of items to remove */
18412 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018413 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018414 item_compare_func_ptr = item_compare_func
18415 ? item_compare2 : item_compare;
18416
18417 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18418 li = li->li_next)
18419 {
18420 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18421 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018422 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018423 if (item_compare_func_err)
18424 {
18425 EMSG(_("E882: Uniq compare function failed"));
18426 break;
18427 }
18428 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018429
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018430 if (!item_compare_func_err)
18431 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018432 while (--i >= 0)
18433 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018434 li = ptrs[i].item->li_next;
18435 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018436 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018437 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018438 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018439 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018440 list_fix_watch(l, li);
18441 listitem_free(li);
18442 l->lv_len--;
18443 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018444 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018445 }
18446
18447 vim_free(ptrs);
18448 }
18449}
18450
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018451/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018452 * "sort({list})" function
18453 */
18454 static void
18455f_sort(argvars, rettv)
18456 typval_T *argvars;
18457 typval_T *rettv;
18458{
18459 do_sort_uniq(argvars, rettv, TRUE);
18460}
18461
18462/*
18463 * "uniq({list})" function
18464 */
18465 static void
18466f_uniq(argvars, rettv)
18467 typval_T *argvars;
18468 typval_T *rettv;
18469{
18470 do_sort_uniq(argvars, rettv, FALSE);
18471}
18472
18473/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018474 * "soundfold({word})" function
18475 */
18476 static void
18477f_soundfold(argvars, rettv)
18478 typval_T *argvars;
18479 typval_T *rettv;
18480{
18481 char_u *s;
18482
18483 rettv->v_type = VAR_STRING;
18484 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018485#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018486 rettv->vval.v_string = eval_soundfold(s);
18487#else
18488 rettv->vval.v_string = vim_strsave(s);
18489#endif
18490}
18491
18492/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018493 * "spellbadword()" function
18494 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018495 static void
18496f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018497 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018498 typval_T *rettv;
18499{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018500 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018501 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018502 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018503
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018504 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018505 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018506
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018507#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018508 if (argvars[0].v_type == VAR_UNKNOWN)
18509 {
18510 /* Find the start and length of the badly spelled word. */
18511 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18512 if (len != 0)
18513 word = ml_get_cursor();
18514 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018515 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018516 {
18517 char_u *str = get_tv_string_chk(&argvars[0]);
18518 int capcol = -1;
18519
18520 if (str != NULL)
18521 {
18522 /* Check the argument for spelling. */
18523 while (*str != NUL)
18524 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018525 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018526 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018527 {
18528 word = str;
18529 break;
18530 }
18531 str += len;
18532 }
18533 }
18534 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018535#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018536
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018537 list_append_string(rettv->vval.v_list, word, len);
18538 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018539 attr == HLF_SPB ? "bad" :
18540 attr == HLF_SPR ? "rare" :
18541 attr == HLF_SPL ? "local" :
18542 attr == HLF_SPC ? "caps" :
18543 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018544}
18545
18546/*
18547 * "spellsuggest()" function
18548 */
18549 static void
18550f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018551 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018552 typval_T *rettv;
18553{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018554#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018555 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018556 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018557 int maxcount;
18558 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018559 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018560 listitem_T *li;
18561 int need_capital = FALSE;
18562#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018563
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018564 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018565 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018566
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018567#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018568 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018569 {
18570 str = get_tv_string(&argvars[0]);
18571 if (argvars[1].v_type != VAR_UNKNOWN)
18572 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018573 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018574 if (maxcount <= 0)
18575 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018576 if (argvars[2].v_type != VAR_UNKNOWN)
18577 {
18578 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18579 if (typeerr)
18580 return;
18581 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018582 }
18583 else
18584 maxcount = 25;
18585
Bram Moolenaar4770d092006-01-12 23:22:24 +000018586 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018587
18588 for (i = 0; i < ga.ga_len; ++i)
18589 {
18590 str = ((char_u **)ga.ga_data)[i];
18591
18592 li = listitem_alloc();
18593 if (li == NULL)
18594 vim_free(str);
18595 else
18596 {
18597 li->li_tv.v_type = VAR_STRING;
18598 li->li_tv.v_lock = 0;
18599 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018600 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018601 }
18602 }
18603 ga_clear(&ga);
18604 }
18605#endif
18606}
18607
Bram Moolenaar0d660222005-01-07 21:51:51 +000018608 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018609f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018610 typval_T *argvars;
18611 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018612{
18613 char_u *str;
18614 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018615 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018616 regmatch_T regmatch;
18617 char_u patbuf[NUMBUFLEN];
18618 char_u *save_cpo;
18619 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018620 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018621 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018622 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018623
18624 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18625 save_cpo = p_cpo;
18626 p_cpo = (char_u *)"";
18627
18628 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018629 if (argvars[1].v_type != VAR_UNKNOWN)
18630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018631 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18632 if (pat == NULL)
18633 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018634 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018635 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018636 }
18637 if (pat == NULL || *pat == NUL)
18638 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018639
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018640 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018642 if (typeerr)
18643 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644
Bram Moolenaar0d660222005-01-07 21:51:51 +000018645 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18646 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018648 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018649 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018650 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018651 if (*str == NUL)
18652 match = FALSE; /* empty item at the end */
18653 else
18654 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018655 if (match)
18656 end = regmatch.startp[0];
18657 else
18658 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018659 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18660 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018661 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018662 if (list_append_string(rettv->vval.v_list, str,
18663 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018664 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018665 }
18666 if (!match)
18667 break;
18668 /* Advance to just after the match. */
18669 if (regmatch.endp[0] > str)
18670 col = 0;
18671 else
18672 {
18673 /* Don't get stuck at the same match. */
18674#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018675 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018676#else
18677 col = 1;
18678#endif
18679 }
18680 str = regmatch.endp[0];
18681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682
Bram Moolenaar473de612013-06-08 18:19:48 +020018683 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685
Bram Moolenaar0d660222005-01-07 21:51:51 +000018686 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687}
18688
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018689#ifdef FEAT_FLOAT
18690/*
18691 * "sqrt()" function
18692 */
18693 static void
18694f_sqrt(argvars, rettv)
18695 typval_T *argvars;
18696 typval_T *rettv;
18697{
18698 float_T f;
18699
18700 rettv->v_type = VAR_FLOAT;
18701 if (get_float_arg(argvars, &f) == OK)
18702 rettv->vval.v_float = sqrt(f);
18703 else
18704 rettv->vval.v_float = 0.0;
18705}
18706
18707/*
18708 * "str2float()" function
18709 */
18710 static void
18711f_str2float(argvars, rettv)
18712 typval_T *argvars;
18713 typval_T *rettv;
18714{
18715 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18716
18717 if (*p == '+')
18718 p = skipwhite(p + 1);
18719 (void)string2float(p, &rettv->vval.v_float);
18720 rettv->v_type = VAR_FLOAT;
18721}
18722#endif
18723
Bram Moolenaar2c932302006-03-18 21:42:09 +000018724/*
18725 * "str2nr()" function
18726 */
18727 static void
18728f_str2nr(argvars, rettv)
18729 typval_T *argvars;
18730 typval_T *rettv;
18731{
18732 int base = 10;
18733 char_u *p;
18734 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018735 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018736
18737 if (argvars[1].v_type != VAR_UNKNOWN)
18738 {
18739 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018740 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018741 {
18742 EMSG(_(e_invarg));
18743 return;
18744 }
18745 }
18746
18747 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018748 if (*p == '+')
18749 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018750 switch (base)
18751 {
18752 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18753 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18754 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18755 default: what = 0;
18756 }
18757 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018758 rettv->vval.v_number = n;
18759}
18760
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761#ifdef HAVE_STRFTIME
18762/*
18763 * "strftime({format}[, {time}])" function
18764 */
18765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018766f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018767 typval_T *argvars;
18768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769{
18770 char_u result_buf[256];
18771 struct tm *curtime;
18772 time_t seconds;
18773 char_u *p;
18774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018775 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018777 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018778 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 seconds = time(NULL);
18780 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018781 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782 curtime = localtime(&seconds);
18783 /* MSVC returns NULL for an invalid value of seconds. */
18784 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018785 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 else
18787 {
18788# ifdef FEAT_MBYTE
18789 vimconv_T conv;
18790 char_u *enc;
18791
18792 conv.vc_type = CONV_NONE;
18793 enc = enc_locale();
18794 convert_setup(&conv, p_enc, enc);
18795 if (conv.vc_type != CONV_NONE)
18796 p = string_convert(&conv, p, NULL);
18797# endif
18798 if (p != NULL)
18799 (void)strftime((char *)result_buf, sizeof(result_buf),
18800 (char *)p, curtime);
18801 else
18802 result_buf[0] = NUL;
18803
18804# ifdef FEAT_MBYTE
18805 if (conv.vc_type != CONV_NONE)
18806 vim_free(p);
18807 convert_setup(&conv, enc, p_enc);
18808 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018809 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810 else
18811# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018812 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813
18814# ifdef FEAT_MBYTE
18815 /* Release conversion descriptors */
18816 convert_setup(&conv, NULL, NULL);
18817 vim_free(enc);
18818# endif
18819 }
18820}
18821#endif
18822
18823/*
18824 * "stridx()" function
18825 */
18826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018827f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018828 typval_T *argvars;
18829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830{
18831 char_u buf[NUMBUFLEN];
18832 char_u *needle;
18833 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018834 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018836 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018838 needle = get_tv_string_chk(&argvars[1]);
18839 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018840 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018841 if (needle == NULL || haystack == NULL)
18842 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843
Bram Moolenaar33570922005-01-25 22:26:29 +000018844 if (argvars[2].v_type != VAR_UNKNOWN)
18845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018846 int error = FALSE;
18847
18848 start_idx = get_tv_number_chk(&argvars[2], &error);
18849 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018850 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018851 if (start_idx >= 0)
18852 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018853 }
18854
18855 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18856 if (pos != NULL)
18857 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858}
18859
18860/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018861 * "string()" function
18862 */
18863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018864f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018865 typval_T *argvars;
18866 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018867{
18868 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018869 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018871 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018872 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018873 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018874 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018875 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876}
18877
18878/*
18879 * "strlen()" function
18880 */
18881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018882f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018883 typval_T *argvars;
18884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018886 rettv->vval.v_number = (varnumber_T)(STRLEN(
18887 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888}
18889
18890/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018891 * "strchars()" function
18892 */
18893 static void
18894f_strchars(argvars, rettv)
18895 typval_T *argvars;
18896 typval_T *rettv;
18897{
18898 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018899 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018900#ifdef FEAT_MBYTE
18901 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018902 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018903#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018904
18905 if (argvars[1].v_type != VAR_UNKNOWN)
18906 skipcc = get_tv_number_chk(&argvars[1], NULL);
18907 if (skipcc < 0 || skipcc > 1)
18908 EMSG(_(e_invarg));
18909 else
18910 {
18911#ifdef FEAT_MBYTE
18912 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18913 while (*s != NUL)
18914 {
18915 func_mb_ptr2char_adv(&s);
18916 ++len;
18917 }
18918 rettv->vval.v_number = len;
18919#else
18920 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18921#endif
18922 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018923}
18924
18925/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018926 * "strdisplaywidth()" function
18927 */
18928 static void
18929f_strdisplaywidth(argvars, rettv)
18930 typval_T *argvars;
18931 typval_T *rettv;
18932{
18933 char_u *s = get_tv_string(&argvars[0]);
18934 int col = 0;
18935
18936 if (argvars[1].v_type != VAR_UNKNOWN)
18937 col = get_tv_number(&argvars[1]);
18938
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018939 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018940}
18941
18942/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018943 * "strwidth()" function
18944 */
18945 static void
18946f_strwidth(argvars, rettv)
18947 typval_T *argvars;
18948 typval_T *rettv;
18949{
18950 char_u *s = get_tv_string(&argvars[0]);
18951
18952 rettv->vval.v_number = (varnumber_T)(
18953#ifdef FEAT_MBYTE
18954 mb_string2cells(s, -1)
18955#else
18956 STRLEN(s)
18957#endif
18958 );
18959}
18960
18961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 * "strpart()" function
18963 */
18964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018965f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018966 typval_T *argvars;
18967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968{
18969 char_u *p;
18970 int n;
18971 int len;
18972 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018973 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018975 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 slen = (int)STRLEN(p);
18977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018978 n = get_tv_number_chk(&argvars[1], &error);
18979 if (error)
18980 len = 0;
18981 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018982 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983 else
18984 len = slen - n; /* default len: all bytes that are available. */
18985
18986 /*
18987 * Only return the overlap between the specified part and the actual
18988 * string.
18989 */
18990 if (n < 0)
18991 {
18992 len += n;
18993 n = 0;
18994 }
18995 else if (n > slen)
18996 n = slen;
18997 if (len < 0)
18998 len = 0;
18999 else if (n + len > slen)
19000 len = slen - n;
19001
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019002 rettv->v_type = VAR_STRING;
19003 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004}
19005
19006/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019007 * "strridx()" function
19008 */
19009 static void
19010f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019011 typval_T *argvars;
19012 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019013{
19014 char_u buf[NUMBUFLEN];
19015 char_u *needle;
19016 char_u *haystack;
19017 char_u *rest;
19018 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019019 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019021 needle = get_tv_string_chk(&argvars[1]);
19022 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019023
19024 rettv->vval.v_number = -1;
19025 if (needle == NULL || haystack == NULL)
19026 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019027
19028 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019029 if (argvars[2].v_type != VAR_UNKNOWN)
19030 {
19031 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019032 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019033 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019034 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019035 }
19036 else
19037 end_idx = haystack_len;
19038
Bram Moolenaar0d660222005-01-07 21:51:51 +000019039 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019040 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019041 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019042 lastmatch = haystack + end_idx;
19043 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019044 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019045 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019046 for (rest = haystack; *rest != '\0'; ++rest)
19047 {
19048 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019049 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019050 break;
19051 lastmatch = rest;
19052 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019053 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019054
19055 if (lastmatch == NULL)
19056 rettv->vval.v_number = -1;
19057 else
19058 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19059}
19060
19061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062 * "strtrans()" function
19063 */
19064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019065f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019066 typval_T *argvars;
19067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019069 rettv->v_type = VAR_STRING;
19070 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071}
19072
19073/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019074 * "submatch()" function
19075 */
19076 static void
19077f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019078 typval_T *argvars;
19079 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019080{
Bram Moolenaar41571762014-04-02 19:00:58 +020019081 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019082 int no;
19083 int retList = 0;
19084
19085 no = (int)get_tv_number_chk(&argvars[0], &error);
19086 if (error)
19087 return;
19088 error = FALSE;
19089 if (argvars[1].v_type != VAR_UNKNOWN)
19090 retList = get_tv_number_chk(&argvars[1], &error);
19091 if (error)
19092 return;
19093
19094 if (retList == 0)
19095 {
19096 rettv->v_type = VAR_STRING;
19097 rettv->vval.v_string = reg_submatch(no);
19098 }
19099 else
19100 {
19101 rettv->v_type = VAR_LIST;
19102 rettv->vval.v_list = reg_submatch_list(no);
19103 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019104}
19105
19106/*
19107 * "substitute()" function
19108 */
19109 static void
19110f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019111 typval_T *argvars;
19112 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019113{
19114 char_u patbuf[NUMBUFLEN];
19115 char_u subbuf[NUMBUFLEN];
19116 char_u flagsbuf[NUMBUFLEN];
19117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019118 char_u *str = get_tv_string_chk(&argvars[0]);
19119 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19120 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19121 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19122
Bram Moolenaar0d660222005-01-07 21:51:51 +000019123 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019124 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19125 rettv->vval.v_string = NULL;
19126 else
19127 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019128}
19129
19130/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019131 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019134f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019135 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137{
19138 int id = 0;
19139#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019140 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 long col;
19142 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019143 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019145 lnum = get_tv_lnum(argvars); /* -1 on type error */
19146 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19147 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019149 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019150 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019151 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152#endif
19153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019154 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155}
19156
19157/*
19158 * "synIDattr(id, what [, mode])" function
19159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019161f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019162 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164{
19165 char_u *p = NULL;
19166#ifdef FEAT_SYN_HL
19167 int id;
19168 char_u *what;
19169 char_u *mode;
19170 char_u modebuf[NUMBUFLEN];
19171 int modec;
19172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019173 id = get_tv_number(&argvars[0]);
19174 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019175 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019177 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019179 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180 modec = 0; /* replace invalid with current */
19181 }
19182 else
19183 {
19184#ifdef FEAT_GUI
19185 if (gui.in_use)
19186 modec = 'g';
19187 else
19188#endif
19189 if (t_colors > 1)
19190 modec = 'c';
19191 else
19192 modec = 't';
19193 }
19194
19195
19196 switch (TOLOWER_ASC(what[0]))
19197 {
19198 case 'b':
19199 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19200 p = highlight_color(id, what, modec);
19201 else /* bold */
19202 p = highlight_has_attr(id, HL_BOLD, modec);
19203 break;
19204
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019205 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 p = highlight_color(id, what, modec);
19207 break;
19208
19209 case 'i':
19210 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19211 p = highlight_has_attr(id, HL_INVERSE, modec);
19212 else /* italic */
19213 p = highlight_has_attr(id, HL_ITALIC, modec);
19214 break;
19215
19216 case 'n': /* name */
19217 p = get_highlight_name(NULL, id - 1);
19218 break;
19219
19220 case 'r': /* reverse */
19221 p = highlight_has_attr(id, HL_INVERSE, modec);
19222 break;
19223
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019224 case 's':
19225 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19226 p = highlight_color(id, what, modec);
19227 else /* standout */
19228 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229 break;
19230
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019231 case 'u':
19232 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19233 /* underline */
19234 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19235 else
19236 /* undercurl */
19237 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238 break;
19239 }
19240
19241 if (p != NULL)
19242 p = vim_strsave(p);
19243#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019244 rettv->v_type = VAR_STRING;
19245 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246}
19247
19248/*
19249 * "synIDtrans(id)" function
19250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019252f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019253 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255{
19256 int id;
19257
19258#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019259 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260
19261 if (id > 0)
19262 id = syn_get_final_id(id);
19263 else
19264#endif
19265 id = 0;
19266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019267 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268}
19269
19270/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019271 * "synconcealed(lnum, col)" function
19272 */
19273 static void
19274f_synconcealed(argvars, rettv)
19275 typval_T *argvars UNUSED;
19276 typval_T *rettv;
19277{
19278#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19279 long lnum;
19280 long col;
19281 int syntax_flags = 0;
19282 int cchar;
19283 int matchid = 0;
19284 char_u str[NUMBUFLEN];
19285#endif
19286
19287 rettv->v_type = VAR_LIST;
19288 rettv->vval.v_list = NULL;
19289
19290#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19291 lnum = get_tv_lnum(argvars); /* -1 on type error */
19292 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19293
19294 vim_memset(str, NUL, sizeof(str));
19295
19296 if (rettv_list_alloc(rettv) != FAIL)
19297 {
19298 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19299 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19300 && curwin->w_p_cole > 0)
19301 {
19302 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19303 syntax_flags = get_syntax_info(&matchid);
19304
19305 /* get the conceal character */
19306 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19307 {
19308 cchar = syn_get_sub_char();
19309 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19310 cchar = lcs_conceal;
19311 if (cchar != NUL)
19312 {
19313# ifdef FEAT_MBYTE
19314 if (has_mbyte)
19315 (*mb_char2bytes)(cchar, str);
19316 else
19317# endif
19318 str[0] = cchar;
19319 }
19320 }
19321 }
19322
19323 list_append_number(rettv->vval.v_list,
19324 (syntax_flags & HL_CONCEAL) != 0);
19325 /* -1 to auto-determine strlen */
19326 list_append_string(rettv->vval.v_list, str, -1);
19327 list_append_number(rettv->vval.v_list, matchid);
19328 }
19329#endif
19330}
19331
19332/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019333 * "synstack(lnum, col)" function
19334 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019335 static void
19336f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019337 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019338 typval_T *rettv;
19339{
19340#ifdef FEAT_SYN_HL
19341 long lnum;
19342 long col;
19343 int i;
19344 int id;
19345#endif
19346
19347 rettv->v_type = VAR_LIST;
19348 rettv->vval.v_list = NULL;
19349
19350#ifdef FEAT_SYN_HL
19351 lnum = get_tv_lnum(argvars); /* -1 on type error */
19352 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19353
19354 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019355 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019356 && rettv_list_alloc(rettv) != FAIL)
19357 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019358 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019359 for (i = 0; ; ++i)
19360 {
19361 id = syn_get_stack_item(i);
19362 if (id < 0)
19363 break;
19364 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19365 break;
19366 }
19367 }
19368#endif
19369}
19370
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019372get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019373 typval_T *argvars;
19374 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019375 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019377 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019379 char_u *infile = NULL;
19380 char_u buf[NUMBUFLEN];
19381 int err = FALSE;
19382 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019383 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019384 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019386 rettv->v_type = VAR_STRING;
19387 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019388 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019389 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019390
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019391 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019392 {
19393 /*
19394 * Write the string to a temp file, to be used for input of the shell
19395 * command.
19396 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019397 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019398 {
19399 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019400 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019401 }
19402
19403 fd = mch_fopen((char *)infile, WRITEBIN);
19404 if (fd == NULL)
19405 {
19406 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019407 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019408 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019409 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019410 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019411 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19412 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019413 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019414 else
19415 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019416 size_t len;
19417
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019418 p = get_tv_string_buf_chk(&argvars[1], buf);
19419 if (p == NULL)
19420 {
19421 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019422 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019423 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019424 len = STRLEN(p);
19425 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019426 err = TRUE;
19427 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019428 if (fclose(fd) != 0)
19429 err = TRUE;
19430 if (err)
19431 {
19432 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019433 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019434 }
19435 }
19436
Bram Moolenaar52a72462014-08-29 15:53:52 +020019437 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19438 * echoes typeahead, that messes up the display. */
19439 if (!msg_silent)
19440 flags += SHELL_COOKED;
19441
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019442 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019444 int len;
19445 listitem_T *li;
19446 char_u *s = NULL;
19447 char_u *start;
19448 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019449 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450
Bram Moolenaar52a72462014-08-29 15:53:52 +020019451 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019452 if (res == NULL)
19453 goto errret;
19454
19455 list = list_alloc();
19456 if (list == NULL)
19457 goto errret;
19458
19459 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019461 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019462 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019463 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019464 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019465
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019466 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019467 if (s == NULL)
19468 goto errret;
19469
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019470 for (p = s; start < end; ++p, ++start)
19471 *p = *start == NUL ? NL : *start;
19472 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019473
19474 li = listitem_alloc();
19475 if (li == NULL)
19476 {
19477 vim_free(s);
19478 goto errret;
19479 }
19480 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019481 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019482 li->li_tv.vval.v_string = s;
19483 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019485
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019486 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019487 rettv->v_type = VAR_LIST;
19488 rettv->vval.v_list = list;
19489 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019491 else
19492 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019493 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019494#ifdef USE_CR
19495 /* translate <CR> into <NL> */
19496 if (res != NULL)
19497 {
19498 char_u *s;
19499
19500 for (s = res; *s; ++s)
19501 {
19502 if (*s == CAR)
19503 *s = NL;
19504 }
19505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506#else
19507# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019508 /* translate <CR><NL> into <NL> */
19509 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019511 char_u *s, *d;
19512
19513 d = res;
19514 for (s = res; *s; ++s)
19515 {
19516 if (s[0] == CAR && s[1] == NL)
19517 ++s;
19518 *d++ = *s;
19519 }
19520 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522# endif
19523#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019524 rettv->vval.v_string = res;
19525 res = NULL;
19526 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019527
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019528errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019529 if (infile != NULL)
19530 {
19531 mch_remove(infile);
19532 vim_free(infile);
19533 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019534 if (res != NULL)
19535 vim_free(res);
19536 if (list != NULL)
19537 list_free(list, TRUE);
19538}
19539
19540/*
19541 * "system()" function
19542 */
19543 static void
19544f_system(argvars, rettv)
19545 typval_T *argvars;
19546 typval_T *rettv;
19547{
19548 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19549}
19550
19551/*
19552 * "systemlist()" function
19553 */
19554 static void
19555f_systemlist(argvars, rettv)
19556 typval_T *argvars;
19557 typval_T *rettv;
19558{
19559 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560}
19561
19562/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019563 * "tabpagebuflist()" function
19564 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019565 static void
19566f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019567 typval_T *argvars UNUSED;
19568 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019569{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019570#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019571 tabpage_T *tp;
19572 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019573
19574 if (argvars[0].v_type == VAR_UNKNOWN)
19575 wp = firstwin;
19576 else
19577 {
19578 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19579 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019580 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019581 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019582 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019583 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019584 for (; wp != NULL; wp = wp->w_next)
19585 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019586 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019587 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019588 }
19589#endif
19590}
19591
19592
19593/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019594 * "tabpagenr()" function
19595 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019596 static void
19597f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019598 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019599 typval_T *rettv;
19600{
19601 int nr = 1;
19602#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019603 char_u *arg;
19604
19605 if (argvars[0].v_type != VAR_UNKNOWN)
19606 {
19607 arg = get_tv_string_chk(&argvars[0]);
19608 nr = 0;
19609 if (arg != NULL)
19610 {
19611 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019612 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019613 else
19614 EMSG2(_(e_invexpr2), arg);
19615 }
19616 }
19617 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019618 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019619#endif
19620 rettv->vval.v_number = nr;
19621}
19622
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019623
19624#ifdef FEAT_WINDOWS
19625static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19626
19627/*
19628 * Common code for tabpagewinnr() and winnr().
19629 */
19630 static int
19631get_winnr(tp, argvar)
19632 tabpage_T *tp;
19633 typval_T *argvar;
19634{
19635 win_T *twin;
19636 int nr = 1;
19637 win_T *wp;
19638 char_u *arg;
19639
19640 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19641 if (argvar->v_type != VAR_UNKNOWN)
19642 {
19643 arg = get_tv_string_chk(argvar);
19644 if (arg == NULL)
19645 nr = 0; /* type error; errmsg already given */
19646 else if (STRCMP(arg, "$") == 0)
19647 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19648 else if (STRCMP(arg, "#") == 0)
19649 {
19650 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19651 if (twin == NULL)
19652 nr = 0;
19653 }
19654 else
19655 {
19656 EMSG2(_(e_invexpr2), arg);
19657 nr = 0;
19658 }
19659 }
19660
19661 if (nr > 0)
19662 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19663 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019664 {
19665 if (wp == NULL)
19666 {
19667 /* didn't find it in this tabpage */
19668 nr = 0;
19669 break;
19670 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019671 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019672 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019673 return nr;
19674}
19675#endif
19676
19677/*
19678 * "tabpagewinnr()" function
19679 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019680 static void
19681f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019682 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019683 typval_T *rettv;
19684{
19685 int nr = 1;
19686#ifdef FEAT_WINDOWS
19687 tabpage_T *tp;
19688
19689 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19690 if (tp == NULL)
19691 nr = 0;
19692 else
19693 nr = get_winnr(tp, &argvars[1]);
19694#endif
19695 rettv->vval.v_number = nr;
19696}
19697
19698
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019699/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019700 * "tagfiles()" function
19701 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019702 static void
19703f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019704 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019705 typval_T *rettv;
19706{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019707 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019708 tagname_T tn;
19709 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019710
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019711 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019712 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019713 fname = alloc(MAXPATHL);
19714 if (fname == NULL)
19715 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019716
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019717 for (first = TRUE; ; first = FALSE)
19718 if (get_tagfname(&tn, first, fname) == FAIL
19719 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019720 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019721 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019722 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019723}
19724
19725/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019726 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019727 */
19728 static void
19729f_taglist(argvars, rettv)
19730 typval_T *argvars;
19731 typval_T *rettv;
19732{
19733 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019734
19735 tag_pattern = get_tv_string(&argvars[0]);
19736
19737 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019738 if (*tag_pattern == NUL)
19739 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019740
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019741 if (rettv_list_alloc(rettv) == OK)
19742 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019743}
19744
19745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746 * "tempname()" function
19747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019749f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752{
19753 static int x = 'A';
19754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019755 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019756 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757
19758 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19759 * names. Skip 'I' and 'O', they are used for shell redirection. */
19760 do
19761 {
19762 if (x == 'Z')
19763 x = '0';
19764 else if (x == '9')
19765 x = 'A';
19766 else
19767 {
19768#ifdef EBCDIC
19769 if (x == 'I')
19770 x = 'J';
19771 else if (x == 'R')
19772 x = 'S';
19773 else
19774#endif
19775 ++x;
19776 }
19777 } while (x == 'I' || x == 'O');
19778}
19779
19780/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019781 * "test(list)" function: Just checking the walls...
19782 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019783 static void
19784f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019785 typval_T *argvars UNUSED;
19786 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019787{
19788 /* Used for unit testing. Change the code below to your liking. */
19789#if 0
19790 listitem_T *li;
19791 list_T *l;
19792 char_u *bad, *good;
19793
19794 if (argvars[0].v_type != VAR_LIST)
19795 return;
19796 l = argvars[0].vval.v_list;
19797 if (l == NULL)
19798 return;
19799 li = l->lv_first;
19800 if (li == NULL)
19801 return;
19802 bad = get_tv_string(&li->li_tv);
19803 li = li->li_next;
19804 if (li == NULL)
19805 return;
19806 good = get_tv_string(&li->li_tv);
19807 rettv->vval.v_number = test_edit_score(bad, good);
19808#endif
19809}
19810
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019811#ifdef FEAT_FLOAT
19812/*
19813 * "tan()" function
19814 */
19815 static void
19816f_tan(argvars, rettv)
19817 typval_T *argvars;
19818 typval_T *rettv;
19819{
19820 float_T f;
19821
19822 rettv->v_type = VAR_FLOAT;
19823 if (get_float_arg(argvars, &f) == OK)
19824 rettv->vval.v_float = tan(f);
19825 else
19826 rettv->vval.v_float = 0.0;
19827}
19828
19829/*
19830 * "tanh()" function
19831 */
19832 static void
19833f_tanh(argvars, rettv)
19834 typval_T *argvars;
19835 typval_T *rettv;
19836{
19837 float_T f;
19838
19839 rettv->v_type = VAR_FLOAT;
19840 if (get_float_arg(argvars, &f) == OK)
19841 rettv->vval.v_float = tanh(f);
19842 else
19843 rettv->vval.v_float = 0.0;
19844}
19845#endif
19846
Bram Moolenaard52d9742005-08-21 22:20:28 +000019847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848 * "tolower(string)" function
19849 */
19850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019851f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019852 typval_T *argvars;
19853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854{
19855 char_u *p;
19856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019857 p = vim_strsave(get_tv_string(&argvars[0]));
19858 rettv->v_type = VAR_STRING;
19859 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860
19861 if (p != NULL)
19862 while (*p != NUL)
19863 {
19864#ifdef FEAT_MBYTE
19865 int l;
19866
19867 if (enc_utf8)
19868 {
19869 int c, lc;
19870
19871 c = utf_ptr2char(p);
19872 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019873 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874 /* TODO: reallocate string when byte count changes. */
19875 if (utf_char2len(lc) == l)
19876 utf_char2bytes(lc, p);
19877 p += l;
19878 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019879 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 p += l; /* skip multi-byte character */
19881 else
19882#endif
19883 {
19884 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19885 ++p;
19886 }
19887 }
19888}
19889
19890/*
19891 * "toupper(string)" function
19892 */
19893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019894f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019895 typval_T *argvars;
19896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019898 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019899 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019900}
19901
19902/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019903 * "tr(string, fromstr, tostr)" function
19904 */
19905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019906f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019907 typval_T *argvars;
19908 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019909{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019910 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019911 char_u *fromstr;
19912 char_u *tostr;
19913 char_u *p;
19914#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019915 int inlen;
19916 int fromlen;
19917 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019918 int idx;
19919 char_u *cpstr;
19920 int cplen;
19921 int first = TRUE;
19922#endif
19923 char_u buf[NUMBUFLEN];
19924 char_u buf2[NUMBUFLEN];
19925 garray_T ga;
19926
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019927 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019928 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19929 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019930
19931 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019932 rettv->v_type = VAR_STRING;
19933 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019934 if (fromstr == NULL || tostr == NULL)
19935 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019936 ga_init2(&ga, (int)sizeof(char), 80);
19937
19938#ifdef FEAT_MBYTE
19939 if (!has_mbyte)
19940#endif
19941 /* not multi-byte: fromstr and tostr must be the same length */
19942 if (STRLEN(fromstr) != STRLEN(tostr))
19943 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019944#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019945error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019946#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019947 EMSG2(_(e_invarg2), fromstr);
19948 ga_clear(&ga);
19949 return;
19950 }
19951
19952 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019953 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019954 {
19955#ifdef FEAT_MBYTE
19956 if (has_mbyte)
19957 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019958 inlen = (*mb_ptr2len)(in_str);
19959 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019960 cplen = inlen;
19961 idx = 0;
19962 for (p = fromstr; *p != NUL; p += fromlen)
19963 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019964 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019965 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019966 {
19967 for (p = tostr; *p != NUL; p += tolen)
19968 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019969 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019970 if (idx-- == 0)
19971 {
19972 cplen = tolen;
19973 cpstr = p;
19974 break;
19975 }
19976 }
19977 if (*p == NUL) /* tostr is shorter than fromstr */
19978 goto error;
19979 break;
19980 }
19981 ++idx;
19982 }
19983
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019984 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019985 {
19986 /* Check that fromstr and tostr have the same number of
19987 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019988 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019989 first = FALSE;
19990 for (p = tostr; *p != NUL; p += tolen)
19991 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019992 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019993 --idx;
19994 }
19995 if (idx != 0)
19996 goto error;
19997 }
19998
Bram Moolenaarcde88542015-08-11 19:14:00 +020019999 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020000 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020001 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020002
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020003 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020004 }
20005 else
20006#endif
20007 {
20008 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020009 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020010 if (p != NULL)
20011 ga_append(&ga, tostr[p - fromstr]);
20012 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020013 ga_append(&ga, *in_str);
20014 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020015 }
20016 }
20017
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020018 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020019 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020020 ga_append(&ga, NUL);
20021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020022 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020023}
20024
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020025#ifdef FEAT_FLOAT
20026/*
20027 * "trunc({float})" function
20028 */
20029 static void
20030f_trunc(argvars, rettv)
20031 typval_T *argvars;
20032 typval_T *rettv;
20033{
20034 float_T f;
20035
20036 rettv->v_type = VAR_FLOAT;
20037 if (get_float_arg(argvars, &f) == OK)
20038 /* trunc() is not in C90, use floor() or ceil() instead. */
20039 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20040 else
20041 rettv->vval.v_float = 0.0;
20042}
20043#endif
20044
Bram Moolenaar8299df92004-07-10 09:47:34 +000020045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 * "type(expr)" function
20047 */
20048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020049f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020050 typval_T *argvars;
20051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020053 int n;
20054
20055 switch (argvars[0].v_type)
20056 {
20057 case VAR_NUMBER: n = 0; break;
20058 case VAR_STRING: n = 1; break;
20059 case VAR_FUNC: n = 2; break;
20060 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020061 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020062#ifdef FEAT_FLOAT
20063 case VAR_FLOAT: n = 5; break;
20064#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020065 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
20066 }
20067 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068}
20069
20070/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020071 * "undofile(name)" function
20072 */
20073 static void
20074f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010020075 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020076 typval_T *rettv;
20077{
20078 rettv->v_type = VAR_STRING;
20079#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020080 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020081 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020082
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020083 if (*fname == NUL)
20084 {
20085 /* If there is no file name there will be no undo file. */
20086 rettv->vval.v_string = NULL;
20087 }
20088 else
20089 {
20090 char_u *ffname = FullName_save(fname, FALSE);
20091
20092 if (ffname != NULL)
20093 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20094 vim_free(ffname);
20095 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020096 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020097#else
20098 rettv->vval.v_string = NULL;
20099#endif
20100}
20101
20102/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020103 * "undotree()" function
20104 */
20105 static void
20106f_undotree(argvars, rettv)
20107 typval_T *argvars UNUSED;
20108 typval_T *rettv;
20109{
20110 if (rettv_dict_alloc(rettv) == OK)
20111 {
20112 dict_T *dict = rettv->vval.v_dict;
20113 list_T *list;
20114
Bram Moolenaar730cde92010-06-27 05:18:54 +020020115 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020116 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020117 dict_add_nr_str(dict, "save_last",
20118 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020119 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20120 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020121 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020122
20123 list = list_alloc();
20124 if (list != NULL)
20125 {
20126 u_eval_tree(curbuf->b_u_oldhead, list);
20127 dict_add_list(dict, "entries", list);
20128 }
20129 }
20130}
20131
20132/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020133 * "values(dict)" function
20134 */
20135 static void
20136f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020137 typval_T *argvars;
20138 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020139{
20140 dict_list(argvars, rettv, 1);
20141}
20142
20143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144 * "virtcol(string)" function
20145 */
20146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020147f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020148 typval_T *argvars;
20149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150{
20151 colnr_T vcol = 0;
20152 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020153 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020155 fp = var2fpos(&argvars[0], FALSE, &fnum);
20156 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20157 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158 {
20159 getvvcol(curwin, fp, NULL, NULL, &vcol);
20160 ++vcol;
20161 }
20162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020163 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164}
20165
20166/*
20167 * "visualmode()" function
20168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020170f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020171 typval_T *argvars UNUSED;
20172 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 char_u str[2];
20175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020176 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 str[0] = curbuf->b_visual_mode_eval;
20178 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020179 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180
20181 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020182 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184}
20185
20186/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020187 * "wildmenumode()" function
20188 */
20189 static void
20190f_wildmenumode(argvars, rettv)
20191 typval_T *argvars UNUSED;
20192 typval_T *rettv UNUSED;
20193{
20194#ifdef FEAT_WILDMENU
20195 if (wild_menu_showing)
20196 rettv->vval.v_number = 1;
20197#endif
20198}
20199
20200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 * "winbufnr(nr)" function
20202 */
20203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020204f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020205 typval_T *argvars;
20206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207{
20208 win_T *wp;
20209
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020210 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020212 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020214 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215}
20216
20217/*
20218 * "wincol()" function
20219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020221f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020222 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224{
20225 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020226 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227}
20228
20229/*
20230 * "winheight(nr)" function
20231 */
20232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020233f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020234 typval_T *argvars;
20235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236{
20237 win_T *wp;
20238
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020239 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020241 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020243 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244}
20245
20246/*
20247 * "winline()" function
20248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020250f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020251 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253{
20254 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020255 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256}
20257
20258/*
20259 * "winnr()" function
20260 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020262f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020263 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265{
20266 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020267
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020269 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020271 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272}
20273
20274/*
20275 * "winrestcmd()" function
20276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020278f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020279 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281{
20282#ifdef FEAT_WINDOWS
20283 win_T *wp;
20284 int winnr = 1;
20285 garray_T ga;
20286 char_u buf[50];
20287
20288 ga_init2(&ga, (int)sizeof(char), 70);
20289 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20290 {
20291 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20292 ga_concat(&ga, buf);
20293# ifdef FEAT_VERTSPLIT
20294 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20295 ga_concat(&ga, buf);
20296# endif
20297 ++winnr;
20298 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020299 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020301 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020303 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020305 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306}
20307
20308/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020309 * "winrestview()" function
20310 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020311 static void
20312f_winrestview(argvars, rettv)
20313 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020314 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020315{
20316 dict_T *dict;
20317
20318 if (argvars[0].v_type != VAR_DICT
20319 || (dict = argvars[0].vval.v_dict) == NULL)
20320 EMSG(_(e_invarg));
20321 else
20322 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020323 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20324 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20325 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20326 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020327#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020328 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20329 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020330#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020331 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20332 {
20333 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20334 curwin->w_set_curswant = FALSE;
20335 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020336
Bram Moolenaar82c25852014-05-28 16:47:16 +020020337 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20338 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020339#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020340 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20341 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020342#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020343 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20344 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20345 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20346 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020347
20348 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020349 win_new_height(curwin, curwin->w_height);
20350# ifdef FEAT_VERTSPLIT
20351 win_new_width(curwin, W_WIDTH(curwin));
20352# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020353 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020354
Bram Moolenaarb851a962014-10-31 15:45:52 +010020355 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020356 curwin->w_topline = 1;
20357 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20358 curwin->w_topline = curbuf->b_ml.ml_line_count;
20359#ifdef FEAT_DIFF
20360 check_topfill(curwin, TRUE);
20361#endif
20362 }
20363}
20364
20365/*
20366 * "winsaveview()" function
20367 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020368 static void
20369f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020370 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020371 typval_T *rettv;
20372{
20373 dict_T *dict;
20374
Bram Moolenaara800b422010-06-27 01:15:55 +020020375 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020376 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020377 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020378
20379 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20380 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20381#ifdef FEAT_VIRTUALEDIT
20382 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20383#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020384 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020385 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20386
20387 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20388#ifdef FEAT_DIFF
20389 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20390#endif
20391 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20392 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20393}
20394
20395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 * "winwidth(nr)" function
20397 */
20398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020399f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020400 typval_T *argvars;
20401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020402{
20403 win_T *wp;
20404
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020405 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020407 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408 else
20409#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020410 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020412 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413#endif
20414}
20415
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020417 * "wordcount()" function
20418 */
20419 static void
20420f_wordcount(argvars, rettv)
20421 typval_T *argvars UNUSED;
20422 typval_T *rettv;
20423{
20424 if (rettv_dict_alloc(rettv) == FAIL)
20425 return;
20426 cursor_pos_info(rettv->vval.v_dict);
20427}
20428
20429/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020430 * Write list of strings to file
20431 */
20432 static int
20433write_list(fd, list, binary)
20434 FILE *fd;
20435 list_T *list;
20436 int binary;
20437{
20438 listitem_T *li;
20439 int c;
20440 int ret = OK;
20441 char_u *s;
20442
20443 for (li = list->lv_first; li != NULL; li = li->li_next)
20444 {
20445 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20446 {
20447 if (*s == '\n')
20448 c = putc(NUL, fd);
20449 else
20450 c = putc(*s, fd);
20451 if (c == EOF)
20452 {
20453 ret = FAIL;
20454 break;
20455 }
20456 }
20457 if (!binary || li->li_next != NULL)
20458 if (putc('\n', fd) == EOF)
20459 {
20460 ret = FAIL;
20461 break;
20462 }
20463 if (ret == FAIL)
20464 {
20465 EMSG(_(e_write));
20466 break;
20467 }
20468 }
20469 return ret;
20470}
20471
20472/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020473 * "writefile()" function
20474 */
20475 static void
20476f_writefile(argvars, rettv)
20477 typval_T *argvars;
20478 typval_T *rettv;
20479{
20480 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020481 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020482 char_u *fname;
20483 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020484 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020485
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020486 if (check_restricted() || check_secure())
20487 return;
20488
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020489 if (argvars[0].v_type != VAR_LIST)
20490 {
20491 EMSG2(_(e_listarg), "writefile()");
20492 return;
20493 }
20494 if (argvars[0].vval.v_list == NULL)
20495 return;
20496
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020497 if (argvars[2].v_type != VAR_UNKNOWN)
20498 {
20499 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20500 binary = TRUE;
20501 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20502 append = TRUE;
20503 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020504
20505 /* Always open the file in binary mode, library functions have a mind of
20506 * their own about CR-LF conversion. */
20507 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020508 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20509 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020510 {
20511 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20512 ret = -1;
20513 }
20514 else
20515 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020516 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20517 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020518 fclose(fd);
20519 }
20520
20521 rettv->vval.v_number = ret;
20522}
20523
20524/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020525 * "xor(expr, expr)" function
20526 */
20527 static void
20528f_xor(argvars, rettv)
20529 typval_T *argvars;
20530 typval_T *rettv;
20531{
20532 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20533 ^ get_tv_number_chk(&argvars[1], NULL);
20534}
20535
20536
20537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020539 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540 */
20541 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020542var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020543 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020544 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020545 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020547 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020549 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550
Bram Moolenaara5525202006-03-02 22:52:09 +000020551 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020552 if (varp->v_type == VAR_LIST)
20553 {
20554 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020555 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020556 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020557 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020558
20559 l = varp->vval.v_list;
20560 if (l == NULL)
20561 return NULL;
20562
20563 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020564 pos.lnum = list_find_nr(l, 0L, &error);
20565 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020566 return NULL; /* invalid line number */
20567
20568 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020569 pos.col = list_find_nr(l, 1L, &error);
20570 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020571 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020572 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020573
20574 /* We accept "$" for the column number: last column. */
20575 li = list_find(l, 1L);
20576 if (li != NULL && li->li_tv.v_type == VAR_STRING
20577 && li->li_tv.vval.v_string != NULL
20578 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20579 pos.col = len + 1;
20580
Bram Moolenaara5525202006-03-02 22:52:09 +000020581 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020582 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020583 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020584 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020585
Bram Moolenaara5525202006-03-02 22:52:09 +000020586#ifdef FEAT_VIRTUALEDIT
20587 /* Get the virtual offset. Defaults to zero. */
20588 pos.coladd = list_find_nr(l, 2L, &error);
20589 if (error)
20590 pos.coladd = 0;
20591#endif
20592
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020593 return &pos;
20594 }
20595
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020596 name = get_tv_string_chk(varp);
20597 if (name == NULL)
20598 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020599 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020601 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20602 {
20603 if (VIsual_active)
20604 return &VIsual;
20605 return &curwin->w_cursor;
20606 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020607 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020609 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20611 return NULL;
20612 return pp;
20613 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020614
20615#ifdef FEAT_VIRTUALEDIT
20616 pos.coladd = 0;
20617#endif
20618
Bram Moolenaar477933c2007-07-17 14:32:23 +000020619 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020620 {
20621 pos.col = 0;
20622 if (name[1] == '0') /* "w0": first visible line */
20623 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020624 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020625 pos.lnum = curwin->w_topline;
20626 return &pos;
20627 }
20628 else if (name[1] == '$') /* "w$": last visible line */
20629 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020630 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020631 pos.lnum = curwin->w_botline - 1;
20632 return &pos;
20633 }
20634 }
20635 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020637 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 {
20639 pos.lnum = curbuf->b_ml.ml_line_count;
20640 pos.col = 0;
20641 }
20642 else
20643 {
20644 pos.lnum = curwin->w_cursor.lnum;
20645 pos.col = (colnr_T)STRLEN(ml_get_curline());
20646 }
20647 return &pos;
20648 }
20649 return NULL;
20650}
20651
20652/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020653 * Convert list in "arg" into a position and optional file number.
20654 * When "fnump" is NULL there is no file number, only 3 items.
20655 * Note that the column is passed on as-is, the caller may want to decrement
20656 * it to use 1 for the first column.
20657 * Return FAIL when conversion is not possible, doesn't check the position for
20658 * validity.
20659 */
20660 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020661list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020662 typval_T *arg;
20663 pos_T *posp;
20664 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020665 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020666{
20667 list_T *l = arg->vval.v_list;
20668 long i = 0;
20669 long n;
20670
Bram Moolenaar493c1782014-05-28 14:34:46 +020020671 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20672 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020673 if (arg->v_type != VAR_LIST
20674 || l == NULL
20675 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020676 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020677 return FAIL;
20678
20679 if (fnump != NULL)
20680 {
20681 n = list_find_nr(l, i++, NULL); /* fnum */
20682 if (n < 0)
20683 return FAIL;
20684 if (n == 0)
20685 n = curbuf->b_fnum; /* current buffer */
20686 *fnump = n;
20687 }
20688
20689 n = list_find_nr(l, i++, NULL); /* lnum */
20690 if (n < 0)
20691 return FAIL;
20692 posp->lnum = n;
20693
20694 n = list_find_nr(l, i++, NULL); /* col */
20695 if (n < 0)
20696 return FAIL;
20697 posp->col = n;
20698
20699#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020700 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020701 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020702 posp->coladd = 0;
20703 else
20704 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020705#endif
20706
Bram Moolenaar493c1782014-05-28 14:34:46 +020020707 if (curswantp != NULL)
20708 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20709
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020710 return OK;
20711}
20712
20713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 * Get the length of an environment variable name.
20715 * Advance "arg" to the first character after the name.
20716 * Return 0 for error.
20717 */
20718 static int
20719get_env_len(arg)
20720 char_u **arg;
20721{
20722 char_u *p;
20723 int len;
20724
20725 for (p = *arg; vim_isIDc(*p); ++p)
20726 ;
20727 if (p == *arg) /* no name found */
20728 return 0;
20729
20730 len = (int)(p - *arg);
20731 *arg = p;
20732 return len;
20733}
20734
20735/*
20736 * Get the length of the name of a function or internal variable.
20737 * "arg" is advanced to the first non-white character after the name.
20738 * Return 0 if something is wrong.
20739 */
20740 static int
20741get_id_len(arg)
20742 char_u **arg;
20743{
20744 char_u *p;
20745 int len;
20746
20747 /* Find the end of the name. */
20748 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020749 {
20750 if (*p == ':')
20751 {
20752 /* "s:" is start of "s:var", but "n:" is not and can be used in
20753 * slice "[n:]". Also "xx:" is not a namespace. */
20754 len = (int)(p - *arg);
20755 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20756 || len > 1)
20757 break;
20758 }
20759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760 if (p == *arg) /* no name found */
20761 return 0;
20762
20763 len = (int)(p - *arg);
20764 *arg = skipwhite(p);
20765
20766 return len;
20767}
20768
20769/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020770 * Get the length of the name of a variable or function.
20771 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020773 * Return -1 if curly braces expansion failed.
20774 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775 * If the name contains 'magic' {}'s, expand them and return the
20776 * expanded name in an allocated string via 'alias' - caller must free.
20777 */
20778 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020779get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 char_u **arg;
20781 char_u **alias;
20782 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020783 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784{
20785 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 char_u *p;
20787 char_u *expr_start;
20788 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789
20790 *alias = NULL; /* default to no alias */
20791
20792 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20793 && (*arg)[2] == (int)KE_SNR)
20794 {
20795 /* hard coded <SNR>, already translated */
20796 *arg += 3;
20797 return get_id_len(arg) + 3;
20798 }
20799 len = eval_fname_script(*arg);
20800 if (len > 0)
20801 {
20802 /* literal "<SID>", "s:" or "<SNR>" */
20803 *arg += len;
20804 }
20805
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020807 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020809 p = find_name_end(*arg, &expr_start, &expr_end,
20810 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 if (expr_start != NULL)
20812 {
20813 char_u *temp_string;
20814
20815 if (!evaluate)
20816 {
20817 len += (int)(p - *arg);
20818 *arg = skipwhite(p);
20819 return len;
20820 }
20821
20822 /*
20823 * Include any <SID> etc in the expanded string:
20824 * Thus the -len here.
20825 */
20826 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20827 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020828 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 *alias = temp_string;
20830 *arg = skipwhite(p);
20831 return (int)STRLEN(temp_string);
20832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833
20834 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020835 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836 EMSG2(_(e_invexpr2), *arg);
20837
20838 return len;
20839}
20840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020841/*
20842 * Find the end of a variable or function name, taking care of magic braces.
20843 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20844 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020845 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020846 * Return a pointer to just after the name. Equal to "arg" if there is no
20847 * valid name.
20848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020850find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 char_u *arg;
20852 char_u **expr_start;
20853 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020854 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020856 int mb_nest = 0;
20857 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020859 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020861 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020863 *expr_start = NULL;
20864 *expr_end = NULL;
20865 }
20866
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020867 /* Quick check for valid starting character. */
20868 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20869 return arg;
20870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020871 for (p = arg; *p != NUL
20872 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020873 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020874 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020875 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020876 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020877 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020878 if (*p == '\'')
20879 {
20880 /* skip over 'string' to avoid counting [ and ] inside it. */
20881 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20882 ;
20883 if (*p == NUL)
20884 break;
20885 }
20886 else if (*p == '"')
20887 {
20888 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20889 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20890 if (*p == '\\' && p[1] != NUL)
20891 ++p;
20892 if (*p == NUL)
20893 break;
20894 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020895 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20896 {
20897 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020898 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020899 len = (int)(p - arg);
20900 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020901 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020902 break;
20903 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020905 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020907 if (*p == '[')
20908 ++br_nest;
20909 else if (*p == ']')
20910 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020913 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020915 if (*p == '{')
20916 {
20917 mb_nest++;
20918 if (expr_start != NULL && *expr_start == NULL)
20919 *expr_start = p;
20920 }
20921 else if (*p == '}')
20922 {
20923 mb_nest--;
20924 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20925 *expr_end = p;
20926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928 }
20929
20930 return p;
20931}
20932
20933/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020934 * Expands out the 'magic' {}'s in a variable/function name.
20935 * Note that this can call itself recursively, to deal with
20936 * constructs like foo{bar}{baz}{bam}
20937 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20938 * "in_start" ^
20939 * "expr_start" ^
20940 * "expr_end" ^
20941 * "in_end" ^
20942 *
20943 * Returns a new allocated string, which the caller must free.
20944 * Returns NULL for failure.
20945 */
20946 static char_u *
20947make_expanded_name(in_start, expr_start, expr_end, in_end)
20948 char_u *in_start;
20949 char_u *expr_start;
20950 char_u *expr_end;
20951 char_u *in_end;
20952{
20953 char_u c1;
20954 char_u *retval = NULL;
20955 char_u *temp_result;
20956 char_u *nextcmd = NULL;
20957
20958 if (expr_end == NULL || in_end == NULL)
20959 return NULL;
20960 *expr_start = NUL;
20961 *expr_end = NUL;
20962 c1 = *in_end;
20963 *in_end = NUL;
20964
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020965 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020966 if (temp_result != NULL && nextcmd == NULL)
20967 {
20968 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20969 + (in_end - expr_end) + 1));
20970 if (retval != NULL)
20971 {
20972 STRCPY(retval, in_start);
20973 STRCAT(retval, temp_result);
20974 STRCAT(retval, expr_end + 1);
20975 }
20976 }
20977 vim_free(temp_result);
20978
20979 *in_end = c1; /* put char back for error messages */
20980 *expr_start = '{';
20981 *expr_end = '}';
20982
20983 if (retval != NULL)
20984 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020985 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020986 if (expr_start != NULL)
20987 {
20988 /* Further expansion! */
20989 temp_result = make_expanded_name(retval, expr_start,
20990 expr_end, temp_result);
20991 vim_free(retval);
20992 retval = temp_result;
20993 }
20994 }
20995
20996 return retval;
20997}
20998
20999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021001 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 */
21003 static int
21004eval_isnamec(c)
21005 int c;
21006{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021007 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21008}
21009
21010/*
21011 * Return TRUE if character "c" can be used as the first character in a
21012 * variable or function name (excluding '{' and '}').
21013 */
21014 static int
21015eval_isnamec1(c)
21016 int c;
21017{
21018 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019}
21020
21021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 * Set number v: variable to "val".
21023 */
21024 void
21025set_vim_var_nr(idx, val)
21026 int idx;
21027 long val;
21028{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021029 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030}
21031
21032/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021033 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034 */
21035 long
21036get_vim_var_nr(idx)
21037 int idx;
21038{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021039 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040}
21041
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021042/*
21043 * Get string v: variable value. Uses a static buffer, can only be used once.
21044 */
21045 char_u *
21046get_vim_var_str(idx)
21047 int idx;
21048{
21049 return get_tv_string(&vimvars[idx].vv_tv);
21050}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021051
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021053 * Get List v: variable value. Caller must take care of reference count when
21054 * needed.
21055 */
21056 list_T *
21057get_vim_var_list(idx)
21058 int idx;
21059{
21060 return vimvars[idx].vv_list;
21061}
21062
21063/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021064 * Set v:char to character "c".
21065 */
21066 void
21067set_vim_var_char(c)
21068 int c;
21069{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021070 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021071
21072#ifdef FEAT_MBYTE
21073 if (has_mbyte)
21074 buf[(*mb_char2bytes)(c, buf)] = NUL;
21075 else
21076#endif
21077 {
21078 buf[0] = c;
21079 buf[1] = NUL;
21080 }
21081 set_vim_var_string(VV_CHAR, buf, -1);
21082}
21083
21084/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021085 * Set v:count to "count" and v:count1 to "count1".
21086 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 */
21088 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021089set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 long count;
21091 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021092 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021094 if (set_prevcount)
21095 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021096 vimvars[VV_COUNT].vv_nr = count;
21097 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098}
21099
21100/*
21101 * Set string v: variable to a copy of "val".
21102 */
21103 void
21104set_vim_var_string(idx, val, len)
21105 int idx;
21106 char_u *val;
21107 int len; /* length of "val" to use or -1 (whole string) */
21108{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021109 /* Need to do this (at least) once, since we can't initialize a union.
21110 * Will always be invoked when "v:progname" is set. */
21111 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
21112
Bram Moolenaare9a41262005-01-15 22:18:47 +000021113 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021115 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021117 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021119 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120}
21121
21122/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021123 * Set List v: variable to "val".
21124 */
21125 void
21126set_vim_var_list(idx, val)
21127 int idx;
21128 list_T *val;
21129{
21130 list_unref(vimvars[idx].vv_list);
21131 vimvars[idx].vv_list = val;
21132 if (val != NULL)
21133 ++val->lv_refcount;
21134}
21135
21136/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021137 * Set Dictionary v: variable to "val".
21138 */
21139 void
21140set_vim_var_dict(idx, val)
21141 int idx;
21142 dict_T *val;
21143{
21144 int todo;
21145 hashitem_T *hi;
21146
21147 dict_unref(vimvars[idx].vv_dict);
21148 vimvars[idx].vv_dict = val;
21149 if (val != NULL)
21150 {
21151 ++val->dv_refcount;
21152
21153 /* Set readonly */
21154 todo = (int)val->dv_hashtab.ht_used;
21155 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21156 {
21157 if (HASHITEM_EMPTY(hi))
21158 continue;
21159 --todo;
21160 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21161 }
21162 }
21163}
21164
21165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166 * Set v:register if needed.
21167 */
21168 void
21169set_reg_var(c)
21170 int c;
21171{
21172 char_u regname;
21173
21174 if (c == 0 || c == ' ')
21175 regname = '"';
21176 else
21177 regname = c;
21178 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021179 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180 set_vim_var_string(VV_REG, &regname, 1);
21181}
21182
21183/*
21184 * Get or set v:exception. If "oldval" == NULL, return the current value.
21185 * Otherwise, restore the value to "oldval" and return NULL.
21186 * Must always be called in pairs to save and restore v:exception! Does not
21187 * take care of memory allocations.
21188 */
21189 char_u *
21190v_exception(oldval)
21191 char_u *oldval;
21192{
21193 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021194 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195
Bram Moolenaare9a41262005-01-15 22:18:47 +000021196 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 return NULL;
21198}
21199
21200/*
21201 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21202 * Otherwise, restore the value to "oldval" and return NULL.
21203 * Must always be called in pairs to save and restore v:throwpoint! Does not
21204 * take care of memory allocations.
21205 */
21206 char_u *
21207v_throwpoint(oldval)
21208 char_u *oldval;
21209{
21210 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021211 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212
Bram Moolenaare9a41262005-01-15 22:18:47 +000021213 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214 return NULL;
21215}
21216
21217#if defined(FEAT_AUTOCMD) || defined(PROTO)
21218/*
21219 * Set v:cmdarg.
21220 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21221 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21222 * Must always be called in pairs!
21223 */
21224 char_u *
21225set_cmdarg(eap, oldarg)
21226 exarg_T *eap;
21227 char_u *oldarg;
21228{
21229 char_u *oldval;
21230 char_u *newval;
21231 unsigned len;
21232
Bram Moolenaare9a41262005-01-15 22:18:47 +000021233 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021234 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021236 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021237 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021238 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239 }
21240
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021241 if (eap->force_bin == FORCE_BIN)
21242 len = 6;
21243 else if (eap->force_bin == FORCE_NOBIN)
21244 len = 8;
21245 else
21246 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021247
21248 if (eap->read_edit)
21249 len += 7;
21250
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021251 if (eap->force_ff != 0)
21252 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21253# ifdef FEAT_MBYTE
21254 if (eap->force_enc != 0)
21255 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021256 if (eap->bad_char != 0)
21257 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021258# endif
21259
21260 newval = alloc(len + 1);
21261 if (newval == NULL)
21262 return NULL;
21263
21264 if (eap->force_bin == FORCE_BIN)
21265 sprintf((char *)newval, " ++bin");
21266 else if (eap->force_bin == FORCE_NOBIN)
21267 sprintf((char *)newval, " ++nobin");
21268 else
21269 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021270
21271 if (eap->read_edit)
21272 STRCAT(newval, " ++edit");
21273
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021274 if (eap->force_ff != 0)
21275 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21276 eap->cmd + eap->force_ff);
21277# ifdef FEAT_MBYTE
21278 if (eap->force_enc != 0)
21279 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21280 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021281 if (eap->bad_char == BAD_KEEP)
21282 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21283 else if (eap->bad_char == BAD_DROP)
21284 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21285 else if (eap->bad_char != 0)
21286 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021287# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021288 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021289 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290}
21291#endif
21292
21293/*
21294 * Get the value of internal variable "name".
21295 * Return OK or FAIL.
21296 */
21297 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021298get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299 char_u *name;
21300 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021301 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021302 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021303 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021304 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305{
21306 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021307 typval_T *tv = NULL;
21308 typval_T atv;
21309 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311
21312 /* truncate the name, so that we can use strcmp() */
21313 cc = name[len];
21314 name[len] = NUL;
21315
21316 /*
21317 * Check for "b:changedtick".
21318 */
21319 if (STRCMP(name, "b:changedtick") == 0)
21320 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021321 atv.v_type = VAR_NUMBER;
21322 atv.vval.v_number = curbuf->b_changedtick;
21323 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 }
21325
21326 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 * Check for user-defined variables.
21328 */
21329 else
21330 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021331 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021333 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021334 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021335 if (dip != NULL)
21336 *dip = v;
21337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 }
21339
Bram Moolenaare9a41262005-01-15 22:18:47 +000021340 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021342 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021343 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344 ret = FAIL;
21345 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021346 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021347 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348
21349 name[len] = cc;
21350
21351 return ret;
21352}
21353
21354/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021355 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21356 * Also handle function call with Funcref variable: func(expr)
21357 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21358 */
21359 static int
21360handle_subscript(arg, rettv, evaluate, verbose)
21361 char_u **arg;
21362 typval_T *rettv;
21363 int evaluate; /* do more than finding the end */
21364 int verbose; /* give error messages */
21365{
21366 int ret = OK;
21367 dict_T *selfdict = NULL;
21368 char_u *s;
21369 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021370 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021371
21372 while (ret == OK
21373 && (**arg == '['
21374 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021375 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021376 && !vim_iswhite(*(*arg - 1)))
21377 {
21378 if (**arg == '(')
21379 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021380 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021381 if (evaluate)
21382 {
21383 functv = *rettv;
21384 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021385
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021386 /* Invoke the function. Recursive! */
21387 s = functv.vval.v_string;
21388 }
21389 else
21390 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021391 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021392 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21393 &len, evaluate, selfdict);
21394
21395 /* Clear the funcref afterwards, so that deleting it while
21396 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021397 if (evaluate)
21398 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021399
21400 /* Stop the expression evaluation when immediately aborting on
21401 * error, or when an interrupt occurred or an exception was thrown
21402 * but not caught. */
21403 if (aborting())
21404 {
21405 if (ret == OK)
21406 clear_tv(rettv);
21407 ret = FAIL;
21408 }
21409 dict_unref(selfdict);
21410 selfdict = NULL;
21411 }
21412 else /* **arg == '[' || **arg == '.' */
21413 {
21414 dict_unref(selfdict);
21415 if (rettv->v_type == VAR_DICT)
21416 {
21417 selfdict = rettv->vval.v_dict;
21418 if (selfdict != NULL)
21419 ++selfdict->dv_refcount;
21420 }
21421 else
21422 selfdict = NULL;
21423 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21424 {
21425 clear_tv(rettv);
21426 ret = FAIL;
21427 }
21428 }
21429 }
21430 dict_unref(selfdict);
21431 return ret;
21432}
21433
21434/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021435 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021436 * value).
21437 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021438 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021439alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021440{
Bram Moolenaar33570922005-01-25 22:26:29 +000021441 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021442}
21443
21444/*
21445 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446 * The string "s" must have been allocated, it is consumed.
21447 * Return NULL for out of memory, the variable otherwise.
21448 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021449 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021450alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451 char_u *s;
21452{
Bram Moolenaar33570922005-01-25 22:26:29 +000021453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021455 rettv = alloc_tv();
21456 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021458 rettv->v_type = VAR_STRING;
21459 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460 }
21461 else
21462 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021463 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464}
21465
21466/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021467 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021469 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021470free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021471 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472{
21473 if (varp != NULL)
21474 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021475 switch (varp->v_type)
21476 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021477 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021478 func_unref(varp->vval.v_string);
21479 /*FALLTHROUGH*/
21480 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021481 vim_free(varp->vval.v_string);
21482 break;
21483 case VAR_LIST:
21484 list_unref(varp->vval.v_list);
21485 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021486 case VAR_DICT:
21487 dict_unref(varp->vval.v_dict);
21488 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021489 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021490#ifdef FEAT_FLOAT
21491 case VAR_FLOAT:
21492#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021493 case VAR_UNKNOWN:
21494 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021495 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021496 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021497 break;
21498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 vim_free(varp);
21500 }
21501}
21502
21503/*
21504 * Free the memory for a variable value and set the value to NULL or 0.
21505 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021506 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021507clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021508 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509{
21510 if (varp != NULL)
21511 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021512 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021514 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021515 func_unref(varp->vval.v_string);
21516 /*FALLTHROUGH*/
21517 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021518 vim_free(varp->vval.v_string);
21519 varp->vval.v_string = NULL;
21520 break;
21521 case VAR_LIST:
21522 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021523 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021524 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021525 case VAR_DICT:
21526 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021527 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021528 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021529 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021530 varp->vval.v_number = 0;
21531 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021532#ifdef FEAT_FLOAT
21533 case VAR_FLOAT:
21534 varp->vval.v_float = 0.0;
21535 break;
21536#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021537 case VAR_UNKNOWN:
21538 break;
21539 default:
21540 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021542 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543 }
21544}
21545
21546/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021547 * Set the value of a variable to NULL without freeing items.
21548 */
21549 static void
21550init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021551 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021552{
21553 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021554 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021555}
21556
21557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558 * Get the number value of a variable.
21559 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021560 * For incompatible types, return 0.
21561 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21562 * caller of incompatible types: it sets *denote to TRUE if "denote"
21563 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 */
21565 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021566get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021567 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021569 int error = FALSE;
21570
21571 return get_tv_number_chk(varp, &error); /* return 0L on error */
21572}
21573
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021574 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021575get_tv_number_chk(varp, denote)
21576 typval_T *varp;
21577 int *denote;
21578{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021579 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021581 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021583 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021584 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021585#ifdef FEAT_FLOAT
21586 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021587 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021588 break;
21589#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021590 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021591 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021592 break;
21593 case VAR_STRING:
21594 if (varp->vval.v_string != NULL)
21595 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021596 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021597 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021598 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021599 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021600 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021601 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021602 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021603 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021604 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021605 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021606 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021608 if (denote == NULL) /* useful for values that must be unsigned */
21609 n = -1;
21610 else
21611 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021612 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613}
21614
21615/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021616 * Get the lnum from the first argument.
21617 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021618 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 */
21620 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021621get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021622 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623{
Bram Moolenaar33570922005-01-25 22:26:29 +000021624 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 linenr_T lnum;
21626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021627 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628 if (lnum == 0) /* no valid number, try using line() */
21629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021630 rettv.v_type = VAR_NUMBER;
21631 f_line(argvars, &rettv);
21632 lnum = rettv.vval.v_number;
21633 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 }
21635 return lnum;
21636}
21637
21638/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021639 * Get the lnum from the first argument.
21640 * Also accepts "$", then "buf" is used.
21641 * Returns 0 on error.
21642 */
21643 static linenr_T
21644get_tv_lnum_buf(argvars, buf)
21645 typval_T *argvars;
21646 buf_T *buf;
21647{
21648 if (argvars[0].v_type == VAR_STRING
21649 && argvars[0].vval.v_string != NULL
21650 && argvars[0].vval.v_string[0] == '$'
21651 && buf != NULL)
21652 return buf->b_ml.ml_line_count;
21653 return get_tv_number_chk(&argvars[0], NULL);
21654}
21655
21656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 * Get the string value of a variable.
21658 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021659 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21660 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 * If the String variable has never been set, return an empty string.
21662 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021663 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21664 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 */
21666 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021667get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021668 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669{
21670 static char_u mybuf[NUMBUFLEN];
21671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021672 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021673}
21674
21675 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021676get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021677 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021678 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021680 char_u *res = get_tv_string_buf_chk(varp, buf);
21681
21682 return res != NULL ? res : (char_u *)"";
21683}
21684
Bram Moolenaar7d647822014-04-05 21:28:56 +020021685/*
21686 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21687 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021688 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021689get_tv_string_chk(varp)
21690 typval_T *varp;
21691{
21692 static char_u mybuf[NUMBUFLEN];
21693
21694 return get_tv_string_buf_chk(varp, mybuf);
21695}
21696
21697 static char_u *
21698get_tv_string_buf_chk(varp, buf)
21699 typval_T *varp;
21700 char_u *buf;
21701{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021702 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021704 case VAR_NUMBER:
21705 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21706 return buf;
21707 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021708 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021709 break;
21710 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021711 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021712 break;
21713 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021714 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021715 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021716#ifdef FEAT_FLOAT
21717 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021718 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021719 break;
21720#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021721 case VAR_STRING:
21722 if (varp->vval.v_string != NULL)
21723 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021724 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021725 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021726 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021727 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021729 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730}
21731
21732/*
21733 * Find variable "name" in the list of variables.
21734 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021735 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021736 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021737 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021739 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021740find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021742 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021743 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021746 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747
Bram Moolenaara7043832005-01-21 11:56:39 +000021748 ht = find_var_ht(name, &varname);
21749 if (htp != NULL)
21750 *htp = ht;
21751 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021753 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754}
21755
21756/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021757 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021758 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021760 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021761find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021762 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021763 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021764 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021765 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021766{
Bram Moolenaar33570922005-01-25 22:26:29 +000021767 hashitem_T *hi;
21768
21769 if (*varname == NUL)
21770 {
21771 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021772 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021773 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021774 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021775 case 'g': return &globvars_var;
21776 case 'v': return &vimvars_var;
21777 case 'b': return &curbuf->b_bufvar;
21778 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021779#ifdef FEAT_WINDOWS
21780 case 't': return &curtab->tp_winvar;
21781#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021782 case 'l': return current_funccal == NULL
21783 ? NULL : &current_funccal->l_vars_var;
21784 case 'a': return current_funccal == NULL
21785 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021786 }
21787 return NULL;
21788 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021789
21790 hi = hash_find(ht, varname);
21791 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021792 {
21793 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021794 * worked find the variable again. Don't auto-load a script if it was
21795 * loaded already, otherwise it would be loaded every time when
21796 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021797 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021798 {
21799 /* Note: script_autoload() may make "hi" invalid. It must either
21800 * be obtained again or not used. */
21801 if (!script_autoload(varname, FALSE) || aborting())
21802 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021803 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021804 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021805 if (HASHITEM_EMPTY(hi))
21806 return NULL;
21807 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021808 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021809}
21810
21811/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021812 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021813 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021814 * Set "varname" to the start of name without ':'.
21815 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021816 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021817find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 char_u *name;
21819 char_u **varname;
21820{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021821 hashitem_T *hi;
21822
Bram Moolenaar73627d02015-08-11 15:46:09 +020021823 if (name[0] == NUL)
21824 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825 if (name[1] != ':')
21826 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021827 /* The name must not start with a colon or #. */
21828 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021829 return NULL;
21830 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021831
21832 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021833 hi = hash_find(&compat_hashtab, name);
21834 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021835 return &compat_hashtab;
21836
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021838 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021839 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840 }
21841 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021842 if (*name == 'g') /* global variable */
21843 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021844 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21845 */
21846 if (vim_strchr(name + 2, ':') != NULL
21847 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021848 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021850 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021852 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021853#ifdef FEAT_WINDOWS
21854 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021855 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021856#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021857 if (*name == 'v') /* v: variable */
21858 return &vimvarht;
21859 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021860 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021861 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021862 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 if (*name == 's' /* script variable */
21864 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21865 return &SCRIPT_VARS(current_SID);
21866 return NULL;
21867}
21868
21869/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021870 * Get function call environment based on bactrace debug level
21871 */
21872 static funccall_T *
21873get_funccal()
21874{
21875 int i;
21876 funccall_T *funccal;
21877 funccall_T *temp_funccal;
21878
21879 funccal = current_funccal;
21880 if (debug_backtrace_level > 0)
21881 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021882 for (i = 0; i < debug_backtrace_level; i++)
21883 {
21884 temp_funccal = funccal->caller;
21885 if (temp_funccal)
21886 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021887 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021888 /* backtrace level overflow. reset to max */
21889 debug_backtrace_level = i;
21890 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021891 }
21892 return funccal;
21893}
21894
21895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021897 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 * Returns NULL when it doesn't exist.
21899 */
21900 char_u *
21901get_var_value(name)
21902 char_u *name;
21903{
Bram Moolenaar33570922005-01-25 22:26:29 +000021904 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021906 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907 if (v == NULL)
21908 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021909 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910}
21911
21912/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021913 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021914 * sourcing this script and when executing functions defined in the script.
21915 */
21916 void
21917new_script_vars(id)
21918 scid_T id;
21919{
Bram Moolenaara7043832005-01-21 11:56:39 +000021920 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021921 hashtab_T *ht;
21922 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021923
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21925 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021926 /* Re-allocating ga_data means that an ht_array pointing to
21927 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021928 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021929 for (i = 1; i <= ga_scripts.ga_len; ++i)
21930 {
21931 ht = &SCRIPT_VARS(i);
21932 if (ht->ht_mask == HT_INIT_SIZE - 1)
21933 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021934 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021935 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021936 }
21937
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 while (ga_scripts.ga_len < id)
21939 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021940 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021941 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021942 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944 }
21945 }
21946}
21947
21948/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021949 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21950 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 */
21952 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021953init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021954 dict_T *dict;
21955 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021956 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021957{
Bram Moolenaar33570922005-01-25 22:26:29 +000021958 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021959 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021960 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021961 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021962 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021963 dict_var->di_tv.vval.v_dict = dict;
21964 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021965 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021966 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21967 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968}
21969
21970/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021971 * Unreference a dictionary initialized by init_var_dict().
21972 */
21973 void
21974unref_var_dict(dict)
21975 dict_T *dict;
21976{
21977 /* Now the dict needs to be freed if no one else is using it, go back to
21978 * normal reference counting. */
21979 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21980 dict_unref(dict);
21981}
21982
21983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021985 * Frees all allocated variables and the value they contain.
21986 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 */
21988 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021989vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021990 hashtab_T *ht;
21991{
21992 vars_clear_ext(ht, TRUE);
21993}
21994
21995/*
21996 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21997 */
21998 static void
21999vars_clear_ext(ht, free_val)
22000 hashtab_T *ht;
22001 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002{
Bram Moolenaara7043832005-01-21 11:56:39 +000022003 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022004 hashitem_T *hi;
22005 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006
Bram Moolenaar33570922005-01-25 22:26:29 +000022007 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022008 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022009 for (hi = ht->ht_array; todo > 0; ++hi)
22010 {
22011 if (!HASHITEM_EMPTY(hi))
22012 {
22013 --todo;
22014
Bram Moolenaar33570922005-01-25 22:26:29 +000022015 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022016 * ht_array might change then. hash_clear() takes care of it
22017 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022018 v = HI2DI(hi);
22019 if (free_val)
22020 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022021 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022022 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022023 }
22024 }
22025 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022026 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027}
22028
Bram Moolenaara7043832005-01-21 11:56:39 +000022029/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022030 * Delete a variable from hashtab "ht" at item "hi".
22031 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000022034delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000022035 hashtab_T *ht;
22036 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037{
Bram Moolenaar33570922005-01-25 22:26:29 +000022038 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022039
22040 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022041 clear_tv(&di->di_tv);
22042 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043}
22044
22045/*
22046 * List the value of one internal variable.
22047 */
22048 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022049list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000022050 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022052 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022054 char_u *tofree;
22055 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022056 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022057
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022058 current_copyID += COPYID_INC;
22059 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000022060 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022061 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022062 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063}
22064
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022066list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 char_u *prefix;
22068 char_u *name;
22069 int type;
22070 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022071 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072{
Bram Moolenaar31859182007-08-14 20:41:13 +000022073 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22074 msg_start();
22075 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076 if (name != NULL) /* "a:" vars don't have a name stored */
22077 msg_puts(name);
22078 msg_putchar(' ');
22079 msg_advance(22);
22080 if (type == VAR_NUMBER)
22081 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022082 else if (type == VAR_FUNC)
22083 msg_putchar('*');
22084 else if (type == VAR_LIST)
22085 {
22086 msg_putchar('[');
22087 if (*string == '[')
22088 ++string;
22089 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022090 else if (type == VAR_DICT)
22091 {
22092 msg_putchar('{');
22093 if (*string == '{')
22094 ++string;
22095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 else
22097 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022098
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022100
22101 if (type == VAR_FUNC)
22102 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022103 if (*first)
22104 {
22105 msg_clr_eos();
22106 *first = FALSE;
22107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108}
22109
22110/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022111 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 * If the variable already exists, the value is updated.
22113 * Otherwise the variable is created.
22114 */
22115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022116set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022118 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022119 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120{
Bram Moolenaar33570922005-01-25 22:26:29 +000022121 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022122 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022123 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022125 ht = find_var_ht(name, &varname);
22126 if (ht == NULL || *varname == NUL)
22127 {
22128 EMSG2(_(e_illvar), name);
22129 return;
22130 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022131 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022132
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022133 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22134 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022135
Bram Moolenaar33570922005-01-25 22:26:29 +000022136 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022138 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022139 if (var_check_ro(v->di_flags, name, FALSE)
22140 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022141 return;
22142 if (v->di_tv.v_type != tv->v_type
22143 && !((v->di_tv.v_type == VAR_STRING
22144 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022145 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022146 || tv->v_type == VAR_NUMBER))
22147#ifdef FEAT_FLOAT
22148 && !((v->di_tv.v_type == VAR_NUMBER
22149 || v->di_tv.v_type == VAR_FLOAT)
22150 && (tv->v_type == VAR_NUMBER
22151 || tv->v_type == VAR_FLOAT))
22152#endif
22153 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022154 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022155 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022156 return;
22157 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022158
22159 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022160 * Handle setting internal v: variables separately where needed to
22161 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022162 */
22163 if (ht == &vimvarht)
22164 {
22165 if (v->di_tv.v_type == VAR_STRING)
22166 {
22167 vim_free(v->di_tv.vval.v_string);
22168 if (copy || tv->v_type != VAR_STRING)
22169 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22170 else
22171 {
22172 /* Take over the string to avoid an extra alloc/free. */
22173 v->di_tv.vval.v_string = tv->vval.v_string;
22174 tv->vval.v_string = NULL;
22175 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022176 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022177 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022178 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022179 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022180 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022181 if (STRCMP(varname, "searchforward") == 0)
22182 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022183#ifdef FEAT_SEARCH_EXTRA
22184 else if (STRCMP(varname, "hlsearch") == 0)
22185 {
22186 no_hlsearch = !v->di_tv.vval.v_number;
22187 redraw_all_later(SOME_VALID);
22188 }
22189#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022190 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022191 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022192 else if (v->di_tv.v_type != tv->v_type)
22193 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022194 }
22195
22196 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 }
22198 else /* add a new variable */
22199 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022200 /* Can't add "v:" variable. */
22201 if (ht == &vimvarht)
22202 {
22203 EMSG2(_(e_illvar), name);
22204 return;
22205 }
22206
Bram Moolenaar92124a32005-06-17 22:03:40 +000022207 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022208 if (!valid_varname(varname))
22209 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022210
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022211 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22212 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022213 if (v == NULL)
22214 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022215 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022216 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022218 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 return;
22220 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022221 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022223
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022224 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022225 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022226 else
22227 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022228 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022229 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022230 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022232}
22233
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022234/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022235 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022236 * Also give an error message.
22237 */
22238 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022239var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022240 int flags;
22241 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022242 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022243{
22244 if (flags & DI_FLAGS_RO)
22245 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022246 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022247 return TRUE;
22248 }
22249 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22250 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022251 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022252 return TRUE;
22253 }
22254 return FALSE;
22255}
22256
22257/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022258 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22259 * Also give an error message.
22260 */
22261 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022262var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022263 int flags;
22264 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022265 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022266{
22267 if (flags & DI_FLAGS_FIX)
22268 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022269 EMSG2(_("E795: Cannot delete variable %s"),
22270 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022271 return TRUE;
22272 }
22273 return FALSE;
22274}
22275
22276/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022277 * Check if a funcref is assigned to a valid variable name.
22278 * Return TRUE and give an error if not.
22279 */
22280 static int
22281var_check_func_name(name, new_var)
22282 char_u *name; /* points to start of variable name */
22283 int new_var; /* TRUE when creating the variable */
22284{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022285 /* Allow for w: b: s: and t:. */
22286 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022287 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22288 ? name[2] : name[0]))
22289 {
22290 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22291 name);
22292 return TRUE;
22293 }
22294 /* Don't allow hiding a function. When "v" is not NULL we might be
22295 * assigning another function to the same var, the type is checked
22296 * below. */
22297 if (new_var && function_exists(name))
22298 {
22299 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22300 name);
22301 return TRUE;
22302 }
22303 return FALSE;
22304}
22305
22306/*
22307 * Check if a variable name is valid.
22308 * Return FALSE and give an error if not.
22309 */
22310 static int
22311valid_varname(varname)
22312 char_u *varname;
22313{
22314 char_u *p;
22315
22316 for (p = varname; *p != NUL; ++p)
22317 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22318 && *p != AUTOLOAD_CHAR)
22319 {
22320 EMSG2(_(e_illvar), varname);
22321 return FALSE;
22322 }
22323 return TRUE;
22324}
22325
22326/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022327 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022328 * Also give an error message, using "name" or _("name") when use_gettext is
22329 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022330 */
22331 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022332tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022333 int lock;
22334 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022335 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022336{
22337 if (lock & VAR_LOCKED)
22338 {
22339 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022340 name == NULL ? (char_u *)_("Unknown")
22341 : use_gettext ? (char_u *)_(name)
22342 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022343 return TRUE;
22344 }
22345 if (lock & VAR_FIXED)
22346 {
22347 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022348 name == NULL ? (char_u *)_("Unknown")
22349 : use_gettext ? (char_u *)_(name)
22350 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022351 return TRUE;
22352 }
22353 return FALSE;
22354}
22355
22356/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022357 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022358 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022359 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022360 * It is OK for "from" and "to" to point to the same item. This is used to
22361 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022362 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022363 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022364copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022365 typval_T *from;
22366 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022368 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022369 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022370 switch (from->v_type)
22371 {
22372 case VAR_NUMBER:
22373 to->vval.v_number = from->vval.v_number;
22374 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022375#ifdef FEAT_FLOAT
22376 case VAR_FLOAT:
22377 to->vval.v_float = from->vval.v_float;
22378 break;
22379#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022380 case VAR_STRING:
22381 case VAR_FUNC:
22382 if (from->vval.v_string == NULL)
22383 to->vval.v_string = NULL;
22384 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022385 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022386 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022387 if (from->v_type == VAR_FUNC)
22388 func_ref(to->vval.v_string);
22389 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022390 break;
22391 case VAR_LIST:
22392 if (from->vval.v_list == NULL)
22393 to->vval.v_list = NULL;
22394 else
22395 {
22396 to->vval.v_list = from->vval.v_list;
22397 ++to->vval.v_list->lv_refcount;
22398 }
22399 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022400 case VAR_DICT:
22401 if (from->vval.v_dict == NULL)
22402 to->vval.v_dict = NULL;
22403 else
22404 {
22405 to->vval.v_dict = from->vval.v_dict;
22406 ++to->vval.v_dict->dv_refcount;
22407 }
22408 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022409 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022410 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022411 break;
22412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413}
22414
22415/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022416 * Make a copy of an item.
22417 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022418 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22419 * reference to an already copied list/dict can be used.
22420 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022421 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022422 static int
22423item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022424 typval_T *from;
22425 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022426 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022427 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022428{
22429 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022430 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022431
Bram Moolenaar33570922005-01-25 22:26:29 +000022432 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022433 {
22434 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022435 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022436 }
22437 ++recurse;
22438
22439 switch (from->v_type)
22440 {
22441 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022442#ifdef FEAT_FLOAT
22443 case VAR_FLOAT:
22444#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022445 case VAR_STRING:
22446 case VAR_FUNC:
22447 copy_tv(from, to);
22448 break;
22449 case VAR_LIST:
22450 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022451 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022452 if (from->vval.v_list == NULL)
22453 to->vval.v_list = NULL;
22454 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22455 {
22456 /* use the copy made earlier */
22457 to->vval.v_list = from->vval.v_list->lv_copylist;
22458 ++to->vval.v_list->lv_refcount;
22459 }
22460 else
22461 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22462 if (to->vval.v_list == NULL)
22463 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022464 break;
22465 case VAR_DICT:
22466 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022467 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022468 if (from->vval.v_dict == NULL)
22469 to->vval.v_dict = NULL;
22470 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22471 {
22472 /* use the copy made earlier */
22473 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22474 ++to->vval.v_dict->dv_refcount;
22475 }
22476 else
22477 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22478 if (to->vval.v_dict == NULL)
22479 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022480 break;
22481 default:
22482 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022483 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022484 }
22485 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022486 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022487}
22488
22489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490 * ":echo expr1 ..." print each argument separated with a space, add a
22491 * newline at the end.
22492 * ":echon expr1 ..." print each argument plain.
22493 */
22494 void
22495ex_echo(eap)
22496 exarg_T *eap;
22497{
22498 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022499 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022500 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501 char_u *p;
22502 int needclr = TRUE;
22503 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022504 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022505
22506 if (eap->skip)
22507 ++emsg_skip;
22508 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22509 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022510 /* If eval1() causes an error message the text from the command may
22511 * still need to be cleared. E.g., "echo 22,44". */
22512 need_clr_eos = needclr;
22513
Bram Moolenaar071d4272004-06-13 20:20:40 +000022514 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022515 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516 {
22517 /*
22518 * Report the invalid expression unless the expression evaluation
22519 * has been cancelled due to an aborting error, an interrupt, or an
22520 * exception.
22521 */
22522 if (!aborting())
22523 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022524 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525 break;
22526 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022527 need_clr_eos = FALSE;
22528
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529 if (!eap->skip)
22530 {
22531 if (atstart)
22532 {
22533 atstart = FALSE;
22534 /* Call msg_start() after eval1(), evaluating the expression
22535 * may cause a message to appear. */
22536 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022537 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022538 /* Mark the saved text as finishing the line, so that what
22539 * follows is displayed on a new line when scrolling back
22540 * at the more prompt. */
22541 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 }
22545 else if (eap->cmdidx == CMD_echo)
22546 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022547 current_copyID += COPYID_INC;
22548 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022549 if (p != NULL)
22550 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022552 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022554 if (*p != TAB && needclr)
22555 {
22556 /* remove any text still there from the command */
22557 msg_clr_eos();
22558 needclr = FALSE;
22559 }
22560 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561 }
22562 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022563 {
22564#ifdef FEAT_MBYTE
22565 if (has_mbyte)
22566 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022567 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022568
22569 (void)msg_outtrans_len_attr(p, i, echo_attr);
22570 p += i - 1;
22571 }
22572 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022574 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022577 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022579 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 arg = skipwhite(arg);
22581 }
22582 eap->nextcmd = check_nextcmd(arg);
22583
22584 if (eap->skip)
22585 --emsg_skip;
22586 else
22587 {
22588 /* remove text that may still be there from the command */
22589 if (needclr)
22590 msg_clr_eos();
22591 if (eap->cmdidx == CMD_echo)
22592 msg_end();
22593 }
22594}
22595
22596/*
22597 * ":echohl {name}".
22598 */
22599 void
22600ex_echohl(eap)
22601 exarg_T *eap;
22602{
22603 int id;
22604
22605 id = syn_name2id(eap->arg);
22606 if (id == 0)
22607 echo_attr = 0;
22608 else
22609 echo_attr = syn_id2attr(id);
22610}
22611
22612/*
22613 * ":execute expr1 ..." execute the result of an expression.
22614 * ":echomsg expr1 ..." Print a message
22615 * ":echoerr expr1 ..." Print an error
22616 * Each gets spaces around each argument and a newline at the end for
22617 * echo commands
22618 */
22619 void
22620ex_execute(eap)
22621 exarg_T *eap;
22622{
22623 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022624 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625 int ret = OK;
22626 char_u *p;
22627 garray_T ga;
22628 int len;
22629 int save_did_emsg;
22630
22631 ga_init2(&ga, 1, 80);
22632
22633 if (eap->skip)
22634 ++emsg_skip;
22635 while (*arg != NUL && *arg != '|' && *arg != '\n')
22636 {
22637 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022638 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639 {
22640 /*
22641 * Report the invalid expression unless the expression evaluation
22642 * has been cancelled due to an aborting error, an interrupt, or an
22643 * exception.
22644 */
22645 if (!aborting())
22646 EMSG2(_(e_invexpr2), p);
22647 ret = FAIL;
22648 break;
22649 }
22650
22651 if (!eap->skip)
22652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022653 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 len = (int)STRLEN(p);
22655 if (ga_grow(&ga, len + 2) == FAIL)
22656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022657 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658 ret = FAIL;
22659 break;
22660 }
22661 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 ga.ga_len += len;
22665 }
22666
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022667 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668 arg = skipwhite(arg);
22669 }
22670
22671 if (ret != FAIL && ga.ga_data != NULL)
22672 {
22673 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022674 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022676 out_flush();
22677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 else if (eap->cmdidx == CMD_echoerr)
22679 {
22680 /* We don't want to abort following commands, restore did_emsg. */
22681 save_did_emsg = did_emsg;
22682 EMSG((char_u *)ga.ga_data);
22683 if (!force_abort)
22684 did_emsg = save_did_emsg;
22685 }
22686 else if (eap->cmdidx == CMD_execute)
22687 do_cmdline((char_u *)ga.ga_data,
22688 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22689 }
22690
22691 ga_clear(&ga);
22692
22693 if (eap->skip)
22694 --emsg_skip;
22695
22696 eap->nextcmd = check_nextcmd(arg);
22697}
22698
22699/*
22700 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22701 * "arg" points to the "&" or '+' when called, to "option" when returning.
22702 * Returns NULL when no option name found. Otherwise pointer to the char
22703 * after the option name.
22704 */
22705 static char_u *
22706find_option_end(arg, opt_flags)
22707 char_u **arg;
22708 int *opt_flags;
22709{
22710 char_u *p = *arg;
22711
22712 ++p;
22713 if (*p == 'g' && p[1] == ':')
22714 {
22715 *opt_flags = OPT_GLOBAL;
22716 p += 2;
22717 }
22718 else if (*p == 'l' && p[1] == ':')
22719 {
22720 *opt_flags = OPT_LOCAL;
22721 p += 2;
22722 }
22723 else
22724 *opt_flags = 0;
22725
22726 if (!ASCII_ISALPHA(*p))
22727 return NULL;
22728 *arg = p;
22729
22730 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22731 p += 4; /* termcap option */
22732 else
22733 while (ASCII_ISALPHA(*p))
22734 ++p;
22735 return p;
22736}
22737
22738/*
22739 * ":function"
22740 */
22741 void
22742ex_function(eap)
22743 exarg_T *eap;
22744{
22745 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022746 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 int j;
22748 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022750 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 char_u *name = NULL;
22752 char_u *p;
22753 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022754 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 garray_T newargs;
22756 garray_T newlines;
22757 int varargs = FALSE;
22758 int mustend = FALSE;
22759 int flags = 0;
22760 ufunc_T *fp;
22761 int indent;
22762 int nesting;
22763 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022764 dictitem_T *v;
22765 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022766 static int func_nr = 0; /* number for nameless function */
22767 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022768 hashtab_T *ht;
22769 int todo;
22770 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022771 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772
22773 /*
22774 * ":function" without argument: list functions.
22775 */
22776 if (ends_excmd(*eap->arg))
22777 {
22778 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022779 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022780 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022781 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022782 {
22783 if (!HASHITEM_EMPTY(hi))
22784 {
22785 --todo;
22786 fp = HI2UF(hi);
22787 if (!isdigit(*fp->uf_name))
22788 list_func_head(fp, FALSE);
22789 }
22790 }
22791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 eap->nextcmd = check_nextcmd(eap->arg);
22793 return;
22794 }
22795
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022796 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022797 * ":function /pat": list functions matching pattern.
22798 */
22799 if (*eap->arg == '/')
22800 {
22801 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22802 if (!eap->skip)
22803 {
22804 regmatch_T regmatch;
22805
22806 c = *p;
22807 *p = NUL;
22808 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22809 *p = c;
22810 if (regmatch.regprog != NULL)
22811 {
22812 regmatch.rm_ic = p_ic;
22813
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022814 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022815 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22816 {
22817 if (!HASHITEM_EMPTY(hi))
22818 {
22819 --todo;
22820 fp = HI2UF(hi);
22821 if (!isdigit(*fp->uf_name)
22822 && vim_regexec(&regmatch, fp->uf_name, 0))
22823 list_func_head(fp, FALSE);
22824 }
22825 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022826 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022827 }
22828 }
22829 if (*p == '/')
22830 ++p;
22831 eap->nextcmd = check_nextcmd(p);
22832 return;
22833 }
22834
22835 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022836 * Get the function name. There are these situations:
22837 * func normal function name
22838 * "name" == func, "fudi.fd_dict" == NULL
22839 * dict.func new dictionary entry
22840 * "name" == NULL, "fudi.fd_dict" set,
22841 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22842 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022843 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022844 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22845 * dict.func existing dict entry that's not a Funcref
22846 * "name" == NULL, "fudi.fd_dict" set,
22847 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022848 * s:func script-local function name
22849 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022852 name = trans_function_name(&p, eap->skip, 0, &fudi);
22853 paren = (vim_strchr(p, '(') != NULL);
22854 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 {
22856 /*
22857 * Return on an invalid expression in braces, unless the expression
22858 * evaluation has been cancelled due to an aborting error, an
22859 * interrupt, or an exception.
22860 */
22861 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022862 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022863 if (!eap->skip && fudi.fd_newkey != NULL)
22864 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022865 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022866 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 else
22869 eap->skip = TRUE;
22870 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022871
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 /* An error in a function call during evaluation of an expression in magic
22873 * braces should not cause the function not to be defined. */
22874 saved_did_emsg = did_emsg;
22875 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876
22877 /*
22878 * ":function func" with only function name: list function.
22879 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022880 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881 {
22882 if (!ends_excmd(*skipwhite(p)))
22883 {
22884 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022885 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 }
22887 eap->nextcmd = check_nextcmd(p);
22888 if (eap->nextcmd != NULL)
22889 *p = NUL;
22890 if (!eap->skip && !got_int)
22891 {
22892 fp = find_func(name);
22893 if (fp != NULL)
22894 {
22895 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022896 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022898 if (FUNCLINE(fp, j) == NULL)
22899 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 msg_putchar('\n');
22901 msg_outnum((long)(j + 1));
22902 if (j < 9)
22903 msg_putchar(' ');
22904 if (j < 99)
22905 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022906 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 out_flush(); /* show a line at a time */
22908 ui_breakcheck();
22909 }
22910 if (!got_int)
22911 {
22912 msg_putchar('\n');
22913 msg_puts((char_u *)" endfunction");
22914 }
22915 }
22916 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022917 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022919 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 }
22921
22922 /*
22923 * ":function name(arg1, arg2)" Define function.
22924 */
22925 p = skipwhite(p);
22926 if (*p != '(')
22927 {
22928 if (!eap->skip)
22929 {
22930 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022931 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932 }
22933 /* attempt to continue by skipping some text */
22934 if (vim_strchr(p, '(') != NULL)
22935 p = vim_strchr(p, '(');
22936 }
22937 p = skipwhite(p + 1);
22938
22939 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22940 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22941
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022942 if (!eap->skip)
22943 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022944 /* Check the name of the function. Unless it's a dictionary function
22945 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022946 if (name != NULL)
22947 arg = name;
22948 else
22949 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022950 if (arg != NULL && (fudi.fd_di == NULL
22951 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022952 {
22953 if (*arg == K_SPECIAL)
22954 j = 3;
22955 else
22956 j = 0;
22957 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22958 : eval_isnamec(arg[j])))
22959 ++j;
22960 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022961 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022962 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022963 /* Disallow using the g: dict. */
22964 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22965 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022966 }
22967
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 /*
22969 * Isolate the arguments: "arg1, arg2, ...)"
22970 */
22971 while (*p != ')')
22972 {
22973 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22974 {
22975 varargs = TRUE;
22976 p += 3;
22977 mustend = TRUE;
22978 }
22979 else
22980 {
22981 arg = p;
22982 while (ASCII_ISALNUM(*p) || *p == '_')
22983 ++p;
22984 if (arg == p || isdigit(*arg)
22985 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22986 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22987 {
22988 if (!eap->skip)
22989 EMSG2(_("E125: Illegal argument: %s"), arg);
22990 break;
22991 }
22992 if (ga_grow(&newargs, 1) == FAIL)
22993 goto erret;
22994 c = *p;
22995 *p = NUL;
22996 arg = vim_strsave(arg);
22997 if (arg == NULL)
22998 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022999
23000 /* Check for duplicate argument name. */
23001 for (i = 0; i < newargs.ga_len; ++i)
23002 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23003 {
23004 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023005 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023006 goto erret;
23007 }
23008
Bram Moolenaar071d4272004-06-13 20:20:40 +000023009 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23010 *p = c;
23011 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012 if (*p == ',')
23013 ++p;
23014 else
23015 mustend = TRUE;
23016 }
23017 p = skipwhite(p);
23018 if (mustend && *p != ')')
23019 {
23020 if (!eap->skip)
23021 EMSG2(_(e_invarg2), eap->arg);
23022 break;
23023 }
23024 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023025 if (*p != ')')
23026 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027 ++p; /* skip the ')' */
23028
Bram Moolenaare9a41262005-01-15 22:18:47 +000023029 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 for (;;)
23031 {
23032 p = skipwhite(p);
23033 if (STRNCMP(p, "range", 5) == 0)
23034 {
23035 flags |= FC_RANGE;
23036 p += 5;
23037 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023038 else if (STRNCMP(p, "dict", 4) == 0)
23039 {
23040 flags |= FC_DICT;
23041 p += 4;
23042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 else if (STRNCMP(p, "abort", 5) == 0)
23044 {
23045 flags |= FC_ABORT;
23046 p += 5;
23047 }
23048 else
23049 break;
23050 }
23051
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023052 /* When there is a line break use what follows for the function body.
23053 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23054 if (*p == '\n')
23055 line_arg = p + 1;
23056 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 EMSG(_(e_trailing));
23058
23059 /*
23060 * Read the body of the function, until ":endfunction" is found.
23061 */
23062 if (KeyTyped)
23063 {
23064 /* Check if the function already exists, don't let the user type the
23065 * whole function before telling him it doesn't work! For a script we
23066 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023067 if (!eap->skip && !eap->forceit)
23068 {
23069 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23070 EMSG(_(e_funcdict));
23071 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023072 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023075 if (!eap->skip && did_emsg)
23076 goto erret;
23077
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078 msg_putchar('\n'); /* don't overwrite the function name */
23079 cmdline_row = msg_row;
23080 }
23081
23082 indent = 2;
23083 nesting = 0;
23084 for (;;)
23085 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023086 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023087 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023088 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023089 saved_wait_return = FALSE;
23090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023092 sourcing_lnum_off = sourcing_lnum;
23093
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023094 if (line_arg != NULL)
23095 {
23096 /* Use eap->arg, split up in parts by line breaks. */
23097 theline = line_arg;
23098 p = vim_strchr(theline, '\n');
23099 if (p == NULL)
23100 line_arg += STRLEN(line_arg);
23101 else
23102 {
23103 *p = NUL;
23104 line_arg = p + 1;
23105 }
23106 }
23107 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 theline = getcmdline(':', 0L, indent);
23109 else
23110 theline = eap->getline(':', eap->cookie, indent);
23111 if (KeyTyped)
23112 lines_left = Rows - 1;
23113 if (theline == NULL)
23114 {
23115 EMSG(_("E126: Missing :endfunction"));
23116 goto erret;
23117 }
23118
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023119 /* Detect line continuation: sourcing_lnum increased more than one. */
23120 if (sourcing_lnum > sourcing_lnum_off + 1)
23121 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23122 else
23123 sourcing_lnum_off = 0;
23124
Bram Moolenaar071d4272004-06-13 20:20:40 +000023125 if (skip_until != NULL)
23126 {
23127 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23128 * don't check for ":endfunc". */
23129 if (STRCMP(theline, skip_until) == 0)
23130 {
23131 vim_free(skip_until);
23132 skip_until = NULL;
23133 }
23134 }
23135 else
23136 {
23137 /* skip ':' and blanks*/
23138 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23139 ;
23140
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023141 /* Check for "endfunction". */
23142 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023144 if (line_arg == NULL)
23145 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146 break;
23147 }
23148
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023149 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150 * at "end". */
23151 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23152 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023153 else if (STRNCMP(p, "if", 2) == 0
23154 || STRNCMP(p, "wh", 2) == 0
23155 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156 || STRNCMP(p, "try", 3) == 0)
23157 indent += 2;
23158
23159 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023160 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023162 if (*p == '!')
23163 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023165 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23166 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023167 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023168 ++nesting;
23169 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170 }
23171 }
23172
23173 /* Check for ":append" or ":insert". */
23174 p = skip_range(p, NULL);
23175 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23176 || (p[0] == 'i'
23177 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23178 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23179 skip_until = vim_strsave((char_u *)".");
23180
23181 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23182 arg = skipwhite(skiptowhite(p));
23183 if (arg[0] == '<' && arg[1] =='<'
23184 && ((p[0] == 'p' && p[1] == 'y'
23185 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23186 || (p[0] == 'p' && p[1] == 'e'
23187 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23188 || (p[0] == 't' && p[1] == 'c'
23189 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023190 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23191 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23193 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023194 || (p[0] == 'm' && p[1] == 'z'
23195 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196 ))
23197 {
23198 /* ":python <<" continues until a dot, like ":append" */
23199 p = skipwhite(arg + 2);
23200 if (*p == NUL)
23201 skip_until = vim_strsave((char_u *)".");
23202 else
23203 skip_until = vim_strsave(p);
23204 }
23205 }
23206
23207 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023208 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023209 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023210 if (line_arg == NULL)
23211 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023213 }
23214
23215 /* Copy the line to newly allocated memory. get_one_sourceline()
23216 * allocates 250 bytes per line, this saves 80% on average. The cost
23217 * is an extra alloc/free. */
23218 p = vim_strsave(theline);
23219 if (p != NULL)
23220 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023221 if (line_arg == NULL)
23222 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023223 theline = p;
23224 }
23225
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023226 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23227
23228 /* Add NULL lines for continuation lines, so that the line count is
23229 * equal to the index in the growarray. */
23230 while (sourcing_lnum_off-- > 0)
23231 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023232
23233 /* Check for end of eap->arg. */
23234 if (line_arg != NULL && *line_arg == NUL)
23235 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236 }
23237
23238 /* Don't define the function when skipping commands or when an error was
23239 * detected. */
23240 if (eap->skip || did_emsg)
23241 goto erret;
23242
23243 /*
23244 * If there are no errors, add the function
23245 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023246 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023247 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023248 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023249 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023250 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023251 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023252 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023253 goto erret;
23254 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023255
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023256 fp = find_func(name);
23257 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023259 if (!eap->forceit)
23260 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023261 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023262 goto erret;
23263 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023264 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023265 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023266 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023267 name);
23268 goto erret;
23269 }
23270 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023271 ga_clear_strings(&(fp->uf_args));
23272 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023273 vim_free(name);
23274 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023276 }
23277 else
23278 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023279 char numbuf[20];
23280
23281 fp = NULL;
23282 if (fudi.fd_newkey == NULL && !eap->forceit)
23283 {
23284 EMSG(_(e_funcdict));
23285 goto erret;
23286 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023287 if (fudi.fd_di == NULL)
23288 {
23289 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023290 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023291 goto erret;
23292 }
23293 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023294 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023295 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023296
23297 /* Give the function a sequential number. Can only be used with a
23298 * Funcref! */
23299 vim_free(name);
23300 sprintf(numbuf, "%d", ++func_nr);
23301 name = vim_strsave((char_u *)numbuf);
23302 if (name == NULL)
23303 goto erret;
23304 }
23305
23306 if (fp == NULL)
23307 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023308 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023309 {
23310 int slen, plen;
23311 char_u *scriptname;
23312
23313 /* Check that the autoload name matches the script name. */
23314 j = FAIL;
23315 if (sourcing_name != NULL)
23316 {
23317 scriptname = autoload_name(name);
23318 if (scriptname != NULL)
23319 {
23320 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023321 plen = (int)STRLEN(p);
23322 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023323 if (slen > plen && fnamecmp(p,
23324 sourcing_name + slen - plen) == 0)
23325 j = OK;
23326 vim_free(scriptname);
23327 }
23328 }
23329 if (j == FAIL)
23330 {
23331 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23332 goto erret;
23333 }
23334 }
23335
23336 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023337 if (fp == NULL)
23338 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023339
23340 if (fudi.fd_dict != NULL)
23341 {
23342 if (fudi.fd_di == NULL)
23343 {
23344 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023345 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023346 if (fudi.fd_di == NULL)
23347 {
23348 vim_free(fp);
23349 goto erret;
23350 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023351 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23352 {
23353 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023354 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023355 goto erret;
23356 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023357 }
23358 else
23359 /* overwrite existing dict entry */
23360 clear_tv(&fudi.fd_di->di_tv);
23361 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023362 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023363 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023364 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023365
23366 /* behave like "dict" was used */
23367 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023368 }
23369
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023371 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023372 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23373 {
23374 vim_free(fp);
23375 goto erret;
23376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023377 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023378 fp->uf_args = newargs;
23379 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023380#ifdef FEAT_PROFILE
23381 fp->uf_tml_count = NULL;
23382 fp->uf_tml_total = NULL;
23383 fp->uf_tml_self = NULL;
23384 fp->uf_profiling = FALSE;
23385 if (prof_def_func())
23386 func_do_profile(fp);
23387#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023388 fp->uf_varargs = varargs;
23389 fp->uf_flags = flags;
23390 fp->uf_calls = 0;
23391 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023392 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023393
23394erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395 ga_clear_strings(&newargs);
23396 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023397ret_free:
23398 vim_free(skip_until);
23399 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023400 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023402 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023403}
23404
23405/*
23406 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023407 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023409 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023410 * TFN_INT: internal function name OK
23411 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023412 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023413 * Advances "pp" to just after the function name (if no error).
23414 */
23415 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023416trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023417 char_u **pp;
23418 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023419 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023420 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023422 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423 char_u *start;
23424 char_u *end;
23425 int lead;
23426 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023427 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023428 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023429
23430 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023431 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023433
23434 /* Check for hard coded <SNR>: already translated function ID (from a user
23435 * command). */
23436 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23437 && (*pp)[2] == (int)KE_SNR)
23438 {
23439 *pp += 3;
23440 len = get_id_len(pp) + 3;
23441 return vim_strnsave(start, len);
23442 }
23443
23444 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23445 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023447 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023448 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023449
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023450 /* Note that TFN_ flags use the same values as GLV_ flags. */
23451 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023452 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023453 if (end == start)
23454 {
23455 if (!skip)
23456 EMSG(_("E129: Function name required"));
23457 goto theend;
23458 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023459 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023460 {
23461 /*
23462 * Report an invalid expression in braces, unless the expression
23463 * evaluation has been cancelled due to an aborting error, an
23464 * interrupt, or an exception.
23465 */
23466 if (!aborting())
23467 {
23468 if (end != NULL)
23469 EMSG2(_(e_invarg2), start);
23470 }
23471 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023472 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023473 goto theend;
23474 }
23475
23476 if (lv.ll_tv != NULL)
23477 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023478 if (fdp != NULL)
23479 {
23480 fdp->fd_dict = lv.ll_dict;
23481 fdp->fd_newkey = lv.ll_newkey;
23482 lv.ll_newkey = NULL;
23483 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023484 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023485 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23486 {
23487 name = vim_strsave(lv.ll_tv->vval.v_string);
23488 *pp = end;
23489 }
23490 else
23491 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023492 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23493 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023494 EMSG(_(e_funcref));
23495 else
23496 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023497 name = NULL;
23498 }
23499 goto theend;
23500 }
23501
23502 if (lv.ll_name == NULL)
23503 {
23504 /* Error found, but continue after the function name. */
23505 *pp = end;
23506 goto theend;
23507 }
23508
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023509 /* Check if the name is a Funcref. If so, use the value. */
23510 if (lv.ll_exp_name != NULL)
23511 {
23512 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023513 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023514 if (name == lv.ll_exp_name)
23515 name = NULL;
23516 }
23517 else
23518 {
23519 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023520 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023521 if (name == *pp)
23522 name = NULL;
23523 }
23524 if (name != NULL)
23525 {
23526 name = vim_strsave(name);
23527 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023528 if (STRNCMP(name, "<SNR>", 5) == 0)
23529 {
23530 /* Change "<SNR>" to the byte sequence. */
23531 name[0] = K_SPECIAL;
23532 name[1] = KS_EXTRA;
23533 name[2] = (int)KE_SNR;
23534 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23535 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023536 goto theend;
23537 }
23538
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023539 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023540 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023541 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023542 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23543 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23544 {
23545 /* When there was "s:" already or the name expanded to get a
23546 * leading "s:" then remove it. */
23547 lv.ll_name += 2;
23548 len -= 2;
23549 lead = 2;
23550 }
23551 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023552 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023553 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023554 /* skip over "s:" and "g:" */
23555 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023556 lv.ll_name += 2;
23557 len = (int)(end - lv.ll_name);
23558 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023559
23560 /*
23561 * Copy the function name to allocated memory.
23562 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23563 * Accept <SNR>123_name() outside a script.
23564 */
23565 if (skip)
23566 lead = 0; /* do nothing */
23567 else if (lead > 0)
23568 {
23569 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023570 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23571 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023572 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023573 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023574 if (current_SID <= 0)
23575 {
23576 EMSG(_(e_usingsid));
23577 goto theend;
23578 }
23579 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23580 lead += (int)STRLEN(sid_buf);
23581 }
23582 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023583 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023584 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023585 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023586 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023587 goto theend;
23588 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023589 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023590 {
23591 char_u *cp = vim_strchr(lv.ll_name, ':');
23592
23593 if (cp != NULL && cp < end)
23594 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023595 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023596 goto theend;
23597 }
23598 }
23599
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023600 name = alloc((unsigned)(len + lead + 1));
23601 if (name != NULL)
23602 {
23603 if (lead > 0)
23604 {
23605 name[0] = K_SPECIAL;
23606 name[1] = KS_EXTRA;
23607 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023608 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023609 STRCPY(name + 3, sid_buf);
23610 }
23611 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023612 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023613 }
23614 *pp = end;
23615
23616theend:
23617 clear_lval(&lv);
23618 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023619}
23620
23621/*
23622 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23623 * Return 2 if "p" starts with "s:".
23624 * Return 0 otherwise.
23625 */
23626 static int
23627eval_fname_script(p)
23628 char_u *p;
23629{
23630 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23631 || STRNICMP(p + 1, "SNR>", 4) == 0))
23632 return 5;
23633 if (p[0] == 's' && p[1] == ':')
23634 return 2;
23635 return 0;
23636}
23637
23638/*
23639 * Return TRUE if "p" starts with "<SID>" or "s:".
23640 * Only works if eval_fname_script() returned non-zero for "p"!
23641 */
23642 static int
23643eval_fname_sid(p)
23644 char_u *p;
23645{
23646 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23647}
23648
23649/*
23650 * List the head of the function: "name(arg1, arg2)".
23651 */
23652 static void
23653list_func_head(fp, indent)
23654 ufunc_T *fp;
23655 int indent;
23656{
23657 int j;
23658
23659 msg_start();
23660 if (indent)
23661 MSG_PUTS(" ");
23662 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023663 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664 {
23665 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023666 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 }
23668 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023669 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023671 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 {
23673 if (j)
23674 MSG_PUTS(", ");
23675 msg_puts(FUNCARG(fp, j));
23676 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023677 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023678 {
23679 if (j)
23680 MSG_PUTS(", ");
23681 MSG_PUTS("...");
23682 }
23683 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023684 if (fp->uf_flags & FC_ABORT)
23685 MSG_PUTS(" abort");
23686 if (fp->uf_flags & FC_RANGE)
23687 MSG_PUTS(" range");
23688 if (fp->uf_flags & FC_DICT)
23689 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023690 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023691 if (p_verbose > 0)
23692 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693}
23694
23695/*
23696 * Find a function by name, return pointer to it in ufuncs.
23697 * Return NULL for unknown function.
23698 */
23699 static ufunc_T *
23700find_func(name)
23701 char_u *name;
23702{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023703 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023705 hi = hash_find(&func_hashtab, name);
23706 if (!HASHITEM_EMPTY(hi))
23707 return HI2UF(hi);
23708 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023709}
23710
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023711#if defined(EXITFREE) || defined(PROTO)
23712 void
23713free_all_functions()
23714{
23715 hashitem_T *hi;
23716
23717 /* Need to start all over every time, because func_free() may change the
23718 * hash table. */
23719 while (func_hashtab.ht_used > 0)
23720 for (hi = func_hashtab.ht_array; ; ++hi)
23721 if (!HASHITEM_EMPTY(hi))
23722 {
23723 func_free(HI2UF(hi));
23724 break;
23725 }
23726}
23727#endif
23728
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023729 int
23730translated_function_exists(name)
23731 char_u *name;
23732{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023733 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023734 return find_internal_func(name) >= 0;
23735 return find_func(name) != NULL;
23736}
23737
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023738/*
23739 * Return TRUE if a function "name" exists.
23740 */
23741 static int
23742function_exists(name)
23743 char_u *name;
23744{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023745 char_u *nm = name;
23746 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023747 int n = FALSE;
23748
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023749 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23750 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023751 nm = skipwhite(nm);
23752
23753 /* Only accept "funcname", "funcname ", "funcname (..." and
23754 * "funcname(...", not "funcname!...". */
23755 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023756 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023757 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023758 return n;
23759}
23760
Bram Moolenaara1544c02013-05-30 12:35:52 +020023761 char_u *
23762get_expanded_name(name, check)
23763 char_u *name;
23764 int check;
23765{
23766 char_u *nm = name;
23767 char_u *p;
23768
23769 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23770
23771 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023772 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023773 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023774
Bram Moolenaara1544c02013-05-30 12:35:52 +020023775 vim_free(p);
23776 return NULL;
23777}
23778
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023779/*
23780 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023781 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23782 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023783 */
23784 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023785builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023786 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023787 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023788{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023789 char_u *p;
23790
23791 if (!ASCII_ISLOWER(name[0]))
23792 return FALSE;
23793 p = vim_strchr(name, AUTOLOAD_CHAR);
23794 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023795}
23796
Bram Moolenaar05159a02005-02-26 23:04:13 +000023797#if defined(FEAT_PROFILE) || defined(PROTO)
23798/*
23799 * Start profiling function "fp".
23800 */
23801 static void
23802func_do_profile(fp)
23803 ufunc_T *fp;
23804{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023805 int len = fp->uf_lines.ga_len;
23806
23807 if (len == 0)
23808 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023809 fp->uf_tm_count = 0;
23810 profile_zero(&fp->uf_tm_self);
23811 profile_zero(&fp->uf_tm_total);
23812 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023813 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023814 if (fp->uf_tml_total == NULL)
23815 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023816 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023817 if (fp->uf_tml_self == NULL)
23818 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023819 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023820 fp->uf_tml_idx = -1;
23821 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23822 || fp->uf_tml_self == NULL)
23823 return; /* out of memory */
23824
23825 fp->uf_profiling = TRUE;
23826}
23827
23828/*
23829 * Dump the profiling results for all functions in file "fd".
23830 */
23831 void
23832func_dump_profile(fd)
23833 FILE *fd;
23834{
23835 hashitem_T *hi;
23836 int todo;
23837 ufunc_T *fp;
23838 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023839 ufunc_T **sorttab;
23840 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023841
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023842 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023843 if (todo == 0)
23844 return; /* nothing to dump */
23845
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023846 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023847
Bram Moolenaar05159a02005-02-26 23:04:13 +000023848 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23849 {
23850 if (!HASHITEM_EMPTY(hi))
23851 {
23852 --todo;
23853 fp = HI2UF(hi);
23854 if (fp->uf_profiling)
23855 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023856 if (sorttab != NULL)
23857 sorttab[st_len++] = fp;
23858
Bram Moolenaar05159a02005-02-26 23:04:13 +000023859 if (fp->uf_name[0] == K_SPECIAL)
23860 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23861 else
23862 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23863 if (fp->uf_tm_count == 1)
23864 fprintf(fd, "Called 1 time\n");
23865 else
23866 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23867 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23868 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23869 fprintf(fd, "\n");
23870 fprintf(fd, "count total (s) self (s)\n");
23871
23872 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23873 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023874 if (FUNCLINE(fp, i) == NULL)
23875 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023876 prof_func_line(fd, fp->uf_tml_count[i],
23877 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023878 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23879 }
23880 fprintf(fd, "\n");
23881 }
23882 }
23883 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023884
23885 if (sorttab != NULL && st_len > 0)
23886 {
23887 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23888 prof_total_cmp);
23889 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23890 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23891 prof_self_cmp);
23892 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23893 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023894
23895 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023896}
Bram Moolenaar73830342005-02-28 22:48:19 +000023897
23898 static void
23899prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23900 FILE *fd;
23901 ufunc_T **sorttab;
23902 int st_len;
23903 char *title;
23904 int prefer_self; /* when equal print only self time */
23905{
23906 int i;
23907 ufunc_T *fp;
23908
23909 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23910 fprintf(fd, "count total (s) self (s) function\n");
23911 for (i = 0; i < 20 && i < st_len; ++i)
23912 {
23913 fp = sorttab[i];
23914 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23915 prefer_self);
23916 if (fp->uf_name[0] == K_SPECIAL)
23917 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23918 else
23919 fprintf(fd, " %s()\n", fp->uf_name);
23920 }
23921 fprintf(fd, "\n");
23922}
23923
23924/*
23925 * Print the count and times for one function or function line.
23926 */
23927 static void
23928prof_func_line(fd, count, total, self, prefer_self)
23929 FILE *fd;
23930 int count;
23931 proftime_T *total;
23932 proftime_T *self;
23933 int prefer_self; /* when equal print only self time */
23934{
23935 if (count > 0)
23936 {
23937 fprintf(fd, "%5d ", count);
23938 if (prefer_self && profile_equal(total, self))
23939 fprintf(fd, " ");
23940 else
23941 fprintf(fd, "%s ", profile_msg(total));
23942 if (!prefer_self && profile_equal(total, self))
23943 fprintf(fd, " ");
23944 else
23945 fprintf(fd, "%s ", profile_msg(self));
23946 }
23947 else
23948 fprintf(fd, " ");
23949}
23950
23951/*
23952 * Compare function for total time sorting.
23953 */
23954 static int
23955#ifdef __BORLANDC__
23956_RTLENTRYF
23957#endif
23958prof_total_cmp(s1, s2)
23959 const void *s1;
23960 const void *s2;
23961{
23962 ufunc_T *p1, *p2;
23963
23964 p1 = *(ufunc_T **)s1;
23965 p2 = *(ufunc_T **)s2;
23966 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23967}
23968
23969/*
23970 * Compare function for self time sorting.
23971 */
23972 static int
23973#ifdef __BORLANDC__
23974_RTLENTRYF
23975#endif
23976prof_self_cmp(s1, s2)
23977 const void *s1;
23978 const void *s2;
23979{
23980 ufunc_T *p1, *p2;
23981
23982 p1 = *(ufunc_T **)s1;
23983 p2 = *(ufunc_T **)s2;
23984 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23985}
23986
Bram Moolenaar05159a02005-02-26 23:04:13 +000023987#endif
23988
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023989/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023990 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023991 * Return TRUE if a package was loaded.
23992 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023993 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023994script_autoload(name, reload)
23995 char_u *name;
23996 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023997{
23998 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023999 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024000 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024001 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024002
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024003 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024004 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024005 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024006 return FALSE;
24007
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024008 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024009
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024010 /* Find the name in the list of previously loaded package names. Skip
24011 * "autoload/", it's always the same. */
24012 for (i = 0; i < ga_loaded.ga_len; ++i)
24013 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24014 break;
24015 if (!reload && i < ga_loaded.ga_len)
24016 ret = FALSE; /* was loaded already */
24017 else
24018 {
24019 /* Remember the name if it wasn't loaded already. */
24020 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24021 {
24022 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24023 tofree = NULL;
24024 }
24025
24026 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024027 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024028 ret = TRUE;
24029 }
24030
24031 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024032 return ret;
24033}
24034
24035/*
24036 * Return the autoload script name for a function or variable name.
24037 * Returns NULL when out of memory.
24038 */
24039 static char_u *
24040autoload_name(name)
24041 char_u *name;
24042{
24043 char_u *p;
24044 char_u *scriptname;
24045
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024046 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024047 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24048 if (scriptname == NULL)
24049 return FALSE;
24050 STRCPY(scriptname, "autoload/");
24051 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024052 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024053 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024054 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024055 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024056 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024057}
24058
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24060
24061/*
24062 * Function given to ExpandGeneric() to obtain the list of user defined
24063 * function names.
24064 */
24065 char_u *
24066get_user_func_name(xp, idx)
24067 expand_T *xp;
24068 int idx;
24069{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024070 static long_u done;
24071 static hashitem_T *hi;
24072 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073
24074 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024075 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024076 done = 0;
24077 hi = func_hashtab.ht_array;
24078 }
24079 if (done < func_hashtab.ht_used)
24080 {
24081 if (done++ > 0)
24082 ++hi;
24083 while (HASHITEM_EMPTY(hi))
24084 ++hi;
24085 fp = HI2UF(hi);
24086
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024087 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024088 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024090 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24091 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024092
24093 cat_func_name(IObuff, fp);
24094 if (xp->xp_context != EXPAND_USER_FUNC)
24095 {
24096 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024097 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024098 STRCAT(IObuff, ")");
24099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024100 return IObuff;
24101 }
24102 return NULL;
24103}
24104
24105#endif /* FEAT_CMDL_COMPL */
24106
24107/*
24108 * Copy the function name of "fp" to buffer "buf".
24109 * "buf" must be able to hold the function name plus three bytes.
24110 * Takes care of script-local function names.
24111 */
24112 static void
24113cat_func_name(buf, fp)
24114 char_u *buf;
24115 ufunc_T *fp;
24116{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024117 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 {
24119 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024120 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 }
24122 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024123 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124}
24125
24126/*
24127 * ":delfunction {name}"
24128 */
24129 void
24130ex_delfunction(eap)
24131 exarg_T *eap;
24132{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024133 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134 char_u *p;
24135 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024136 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024137
24138 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024139 name = trans_function_name(&p, eap->skip, 0, &fudi);
24140 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024142 {
24143 if (fudi.fd_dict != NULL && !eap->skip)
24144 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024147 if (!ends_excmd(*skipwhite(p)))
24148 {
24149 vim_free(name);
24150 EMSG(_(e_trailing));
24151 return;
24152 }
24153 eap->nextcmd = check_nextcmd(p);
24154 if (eap->nextcmd != NULL)
24155 *p = NUL;
24156
24157 if (!eap->skip)
24158 fp = find_func(name);
24159 vim_free(name);
24160
24161 if (!eap->skip)
24162 {
24163 if (fp == NULL)
24164 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024165 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166 return;
24167 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024168 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024169 {
24170 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24171 return;
24172 }
24173
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024174 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024175 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024176 /* Delete the dict item that refers to the function, it will
24177 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024178 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024179 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024180 else
24181 func_free(fp);
24182 }
24183}
24184
24185/*
24186 * Free a function and remove it from the list of functions.
24187 */
24188 static void
24189func_free(fp)
24190 ufunc_T *fp;
24191{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024192 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024193
24194 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024195 ga_clear_strings(&(fp->uf_args));
24196 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024197#ifdef FEAT_PROFILE
24198 vim_free(fp->uf_tml_count);
24199 vim_free(fp->uf_tml_total);
24200 vim_free(fp->uf_tml_self);
24201#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024202
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024203 /* remove the function from the function hashtable */
24204 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24205 if (HASHITEM_EMPTY(hi))
24206 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024207 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024208 hash_remove(&func_hashtab, hi);
24209
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024210 vim_free(fp);
24211}
24212
24213/*
24214 * Unreference a Function: decrement the reference count and free it when it
24215 * becomes zero. Only for numbered functions.
24216 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024217 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024218func_unref(name)
24219 char_u *name;
24220{
24221 ufunc_T *fp;
24222
24223 if (name != NULL && isdigit(*name))
24224 {
24225 fp = find_func(name);
24226 if (fp == NULL)
24227 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024228 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024229 {
24230 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024231 * when "uf_calls" becomes zero. */
24232 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024233 func_free(fp);
24234 }
24235 }
24236}
24237
24238/*
24239 * Count a reference to a Function.
24240 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024241 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024242func_ref(name)
24243 char_u *name;
24244{
24245 ufunc_T *fp;
24246
24247 if (name != NULL && isdigit(*name))
24248 {
24249 fp = find_func(name);
24250 if (fp == NULL)
24251 EMSG2(_(e_intern2), "func_ref()");
24252 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024253 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254 }
24255}
24256
24257/*
24258 * Call a user function.
24259 */
24260 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024261call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024262 ufunc_T *fp; /* pointer to function */
24263 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024264 typval_T *argvars; /* arguments */
24265 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024266 linenr_T firstline; /* first line of range */
24267 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024268 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269{
Bram Moolenaar33570922005-01-25 22:26:29 +000024270 char_u *save_sourcing_name;
24271 linenr_T save_sourcing_lnum;
24272 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024273 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024274 int save_did_emsg;
24275 static int depth = 0;
24276 dictitem_T *v;
24277 int fixvar_idx = 0; /* index in fixvar[] */
24278 int i;
24279 int ai;
24280 char_u numbuf[NUMBUFLEN];
24281 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024282 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024283#ifdef FEAT_PROFILE
24284 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024285 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024287
24288 /* If depth of calling is getting too high, don't execute the function */
24289 if (depth >= p_mfd)
24290 {
24291 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024292 rettv->v_type = VAR_NUMBER;
24293 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024294 return;
24295 }
24296 ++depth;
24297
24298 line_breakcheck(); /* check for CTRL-C hit */
24299
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024300 fc = (funccall_T *)alloc(sizeof(funccall_T));
24301 fc->caller = current_funccal;
24302 current_funccal = fc;
24303 fc->func = fp;
24304 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024305 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024306 fc->linenr = 0;
24307 fc->returned = FALSE;
24308 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024310 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24311 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024312
Bram Moolenaar33570922005-01-25 22:26:29 +000024313 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024314 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024315 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24316 * each argument variable and saves a lot of time.
24317 */
24318 /*
24319 * Init l: variables.
24320 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024321 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024322 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024323 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024324 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24325 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024326 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024327 name = v->di_key;
24328 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024329 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024330 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024331 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024332 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024333 v->di_tv.vval.v_dict = selfdict;
24334 ++selfdict->dv_refcount;
24335 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024336
Bram Moolenaar33570922005-01-25 22:26:29 +000024337 /*
24338 * Init a: variables.
24339 * Set a:0 to "argcount".
24340 * Set a:000 to a list with room for the "..." arguments.
24341 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024342 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024343 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024344 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024345 /* Use "name" to avoid a warning from some compiler that checks the
24346 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024347 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024348 name = v->di_key;
24349 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024350 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024351 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024352 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024353 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024354 v->di_tv.vval.v_list = &fc->l_varlist;
24355 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24356 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24357 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024358
24359 /*
24360 * Set a:firstline to "firstline" and a:lastline to "lastline".
24361 * Set a:name to named arguments.
24362 * Set a:N to the "..." arguments.
24363 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024364 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024365 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024366 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024367 (varnumber_T)lastline);
24368 for (i = 0; i < argcount; ++i)
24369 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024370 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024371 if (ai < 0)
24372 /* named argument a:name */
24373 name = FUNCARG(fp, i);
24374 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024375 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024376 /* "..." argument a:1, a:2, etc. */
24377 sprintf((char *)numbuf, "%d", ai + 1);
24378 name = numbuf;
24379 }
24380 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24381 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024382 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024383 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24384 }
24385 else
24386 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024387 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24388 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024389 if (v == NULL)
24390 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024391 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024392 }
24393 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024394 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024395
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024396 /* Note: the values are copied directly to avoid alloc/free.
24397 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024398 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024399 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024400
24401 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24402 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024403 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24404 fc->l_listitems[ai].li_tv = argvars[i];
24405 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024406 }
24407 }
24408
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409 /* Don't redraw while executing the function. */
24410 ++RedrawingDisabled;
24411 save_sourcing_name = sourcing_name;
24412 save_sourcing_lnum = sourcing_lnum;
24413 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024414 /* need space for function name + ("function " + 3) or "[number]" */
24415 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24416 + STRLEN(fp->uf_name) + 20;
24417 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024418 if (sourcing_name != NULL)
24419 {
24420 if (save_sourcing_name != NULL
24421 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024422 sprintf((char *)sourcing_name, "%s[%d]..",
24423 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024424 else
24425 STRCPY(sourcing_name, "function ");
24426 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24427
24428 if (p_verbose >= 12)
24429 {
24430 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024431 verbose_enter_scroll();
24432
Bram Moolenaar555b2802005-05-19 21:08:39 +000024433 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024434 if (p_verbose >= 14)
24435 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024437 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024438 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024439 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024440
24441 msg_puts((char_u *)"(");
24442 for (i = 0; i < argcount; ++i)
24443 {
24444 if (i > 0)
24445 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024446 if (argvars[i].v_type == VAR_NUMBER)
24447 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024448 else
24449 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024450 /* Do not want errors such as E724 here. */
24451 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024452 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024453 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024454 if (s != NULL)
24455 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024456 if (vim_strsize(s) > MSG_BUF_CLEN)
24457 {
24458 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24459 s = buf;
24460 }
24461 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024462 vim_free(tofree);
24463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024464 }
24465 }
24466 msg_puts((char_u *)")");
24467 }
24468 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024469
24470 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024471 --no_wait_return;
24472 }
24473 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024474#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024475 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024476 {
24477 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24478 func_do_profile(fp);
24479 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024480 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024481 {
24482 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024483 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024484 profile_zero(&fp->uf_tm_children);
24485 }
24486 script_prof_save(&wait_start);
24487 }
24488#endif
24489
Bram Moolenaar071d4272004-06-13 20:20:40 +000024490 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024491 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024492 save_did_emsg = did_emsg;
24493 did_emsg = FALSE;
24494
24495 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024496 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024497 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24498
24499 --RedrawingDisabled;
24500
24501 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024502 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024504 clear_tv(rettv);
24505 rettv->v_type = VAR_NUMBER;
24506 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024507 }
24508
Bram Moolenaar05159a02005-02-26 23:04:13 +000024509#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024510 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024511 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024512 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024513 profile_end(&call_start);
24514 profile_sub_wait(&wait_start, &call_start);
24515 profile_add(&fp->uf_tm_total, &call_start);
24516 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024517 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024518 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024519 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24520 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024521 }
24522 }
24523#endif
24524
Bram Moolenaar071d4272004-06-13 20:20:40 +000024525 /* when being verbose, mention the return value */
24526 if (p_verbose >= 12)
24527 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024528 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024529 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024530
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024532 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024533 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024534 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024535 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024536 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024538 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024539 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024540 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024541 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024542
Bram Moolenaar555b2802005-05-19 21:08:39 +000024543 /* The value may be very long. Skip the middle part, so that we
24544 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024545 * truncate it at the end. Don't want errors such as E724 here. */
24546 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024547 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024548 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024549 if (s != NULL)
24550 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024551 if (vim_strsize(s) > MSG_BUF_CLEN)
24552 {
24553 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24554 s = buf;
24555 }
24556 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024557 vim_free(tofree);
24558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024559 }
24560 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024561
24562 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024563 --no_wait_return;
24564 }
24565
24566 vim_free(sourcing_name);
24567 sourcing_name = save_sourcing_name;
24568 sourcing_lnum = save_sourcing_lnum;
24569 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024570#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024571 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024572 script_prof_restore(&wait_start);
24573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574
24575 if (p_verbose >= 12 && sourcing_name != NULL)
24576 {
24577 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024578 verbose_enter_scroll();
24579
Bram Moolenaar555b2802005-05-19 21:08:39 +000024580 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024581 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024582
24583 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584 --no_wait_return;
24585 }
24586
24587 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024588 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024589 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024590
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024591 /* If the a:000 list and the l: and a: dicts are not referenced we can
24592 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024593 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24594 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24595 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24596 {
24597 free_funccal(fc, FALSE);
24598 }
24599 else
24600 {
24601 hashitem_T *hi;
24602 listitem_T *li;
24603 int todo;
24604
24605 /* "fc" is still in use. This can happen when returning "a:000" or
24606 * assigning "l:" to a global variable.
24607 * Link "fc" in the list for garbage collection later. */
24608 fc->caller = previous_funccal;
24609 previous_funccal = fc;
24610
24611 /* Make a copy of the a: variables, since we didn't do that above. */
24612 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24613 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24614 {
24615 if (!HASHITEM_EMPTY(hi))
24616 {
24617 --todo;
24618 v = HI2DI(hi);
24619 copy_tv(&v->di_tv, &v->di_tv);
24620 }
24621 }
24622
24623 /* Make a copy of the a:000 items, since we didn't do that above. */
24624 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24625 copy_tv(&li->li_tv, &li->li_tv);
24626 }
24627}
24628
24629/*
24630 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024631 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024632 */
24633 static int
24634can_free_funccal(fc, copyID)
24635 funccall_T *fc;
24636 int copyID;
24637{
24638 return (fc->l_varlist.lv_copyID != copyID
24639 && fc->l_vars.dv_copyID != copyID
24640 && fc->l_avars.dv_copyID != copyID);
24641}
24642
24643/*
24644 * Free "fc" and what it contains.
24645 */
24646 static void
24647free_funccal(fc, free_val)
24648 funccall_T *fc;
24649 int free_val; /* a: vars were allocated */
24650{
24651 listitem_T *li;
24652
24653 /* The a: variables typevals may not have been allocated, only free the
24654 * allocated variables. */
24655 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24656
24657 /* free all l: variables */
24658 vars_clear(&fc->l_vars.dv_hashtab);
24659
24660 /* Free the a:000 variables if they were allocated. */
24661 if (free_val)
24662 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24663 clear_tv(&li->li_tv);
24664
24665 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024666}
24667
24668/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024669 * Add a number variable "name" to dict "dp" with value "nr".
24670 */
24671 static void
24672add_nr_var(dp, v, name, nr)
24673 dict_T *dp;
24674 dictitem_T *v;
24675 char *name;
24676 varnumber_T nr;
24677{
24678 STRCPY(v->di_key, name);
24679 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24680 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24681 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024682 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024683 v->di_tv.vval.v_number = nr;
24684}
24685
24686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024687 * ":return [expr]"
24688 */
24689 void
24690ex_return(eap)
24691 exarg_T *eap;
24692{
24693 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024694 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024695 int returning = FALSE;
24696
24697 if (current_funccal == NULL)
24698 {
24699 EMSG(_("E133: :return not inside a function"));
24700 return;
24701 }
24702
24703 if (eap->skip)
24704 ++emsg_skip;
24705
24706 eap->nextcmd = NULL;
24707 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024708 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024709 {
24710 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024711 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024712 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024713 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024714 }
24715 /* It's safer to return also on error. */
24716 else if (!eap->skip)
24717 {
24718 /*
24719 * Return unless the expression evaluation has been cancelled due to an
24720 * aborting error, an interrupt, or an exception.
24721 */
24722 if (!aborting())
24723 returning = do_return(eap, FALSE, TRUE, NULL);
24724 }
24725
24726 /* When skipping or the return gets pending, advance to the next command
24727 * in this line (!returning). Otherwise, ignore the rest of the line.
24728 * Following lines will be ignored by get_func_line(). */
24729 if (returning)
24730 eap->nextcmd = NULL;
24731 else if (eap->nextcmd == NULL) /* no argument */
24732 eap->nextcmd = check_nextcmd(arg);
24733
24734 if (eap->skip)
24735 --emsg_skip;
24736}
24737
24738/*
24739 * Return from a function. Possibly makes the return pending. Also called
24740 * for a pending return at the ":endtry" or after returning from an extra
24741 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024742 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024743 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024744 * FALSE when the return gets pending.
24745 */
24746 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024747do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024748 exarg_T *eap;
24749 int reanimate;
24750 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024751 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024752{
24753 int idx;
24754 struct condstack *cstack = eap->cstack;
24755
24756 if (reanimate)
24757 /* Undo the return. */
24758 current_funccal->returned = FALSE;
24759
24760 /*
24761 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24762 * not in its finally clause (which then is to be executed next) is found.
24763 * In this case, make the ":return" pending for execution at the ":endtry".
24764 * Otherwise, return normally.
24765 */
24766 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24767 if (idx >= 0)
24768 {
24769 cstack->cs_pending[idx] = CSTP_RETURN;
24770
24771 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024772 /* A pending return again gets pending. "rettv" points to an
24773 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024774 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024775 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024776 else
24777 {
24778 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024779 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024780 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024781 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024782
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024783 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024784 {
24785 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024786 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024787 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024788 else
24789 EMSG(_(e_outofmem));
24790 }
24791 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024792 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024793
24794 if (reanimate)
24795 {
24796 /* The pending return value could be overwritten by a ":return"
24797 * without argument in a finally clause; reset the default
24798 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024799 current_funccal->rettv->v_type = VAR_NUMBER;
24800 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024801 }
24802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024803 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804 }
24805 else
24806 {
24807 current_funccal->returned = TRUE;
24808
24809 /* If the return is carried out now, store the return value. For
24810 * a return immediately after reanimation, the value is already
24811 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024812 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024813 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024814 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024815 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024817 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024818 }
24819 }
24820
24821 return idx < 0;
24822}
24823
24824/*
24825 * Free the variable with a pending return value.
24826 */
24827 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024828discard_pending_return(rettv)
24829 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024830{
Bram Moolenaar33570922005-01-25 22:26:29 +000024831 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024832}
24833
24834/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024835 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024836 * is an allocated string. Used by report_pending() for verbose messages.
24837 */
24838 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024839get_return_cmd(rettv)
24840 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024842 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024843 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024844 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024845
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024846 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024847 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024848 if (s == NULL)
24849 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024850
24851 STRCPY(IObuff, ":return ");
24852 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24853 if (STRLEN(s) + 8 >= IOSIZE)
24854 STRCPY(IObuff + IOSIZE - 4, "...");
24855 vim_free(tofree);
24856 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024857}
24858
24859/*
24860 * Get next function line.
24861 * Called by do_cmdline() to get the next line.
24862 * Returns allocated string, or NULL for end of function.
24863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864 char_u *
24865get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024866 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024867 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024868 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869{
Bram Moolenaar33570922005-01-25 22:26:29 +000024870 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024871 ufunc_T *fp = fcp->func;
24872 char_u *retval;
24873 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024874
24875 /* If breakpoints have been added/deleted need to check for it. */
24876 if (fcp->dbg_tick != debug_tick)
24877 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024878 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024879 sourcing_lnum);
24880 fcp->dbg_tick = debug_tick;
24881 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024882#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024883 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024884 func_line_end(cookie);
24885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886
Bram Moolenaar05159a02005-02-26 23:04:13 +000024887 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024888 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24889 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024890 retval = NULL;
24891 else
24892 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024893 /* Skip NULL lines (continuation lines). */
24894 while (fcp->linenr < gap->ga_len
24895 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24896 ++fcp->linenr;
24897 if (fcp->linenr >= gap->ga_len)
24898 retval = NULL;
24899 else
24900 {
24901 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24902 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024903#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024904 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024905 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024906#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908 }
24909
24910 /* Did we encounter a breakpoint? */
24911 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24912 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024913 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024914 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024915 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916 sourcing_lnum);
24917 fcp->dbg_tick = debug_tick;
24918 }
24919
24920 return retval;
24921}
24922
Bram Moolenaar05159a02005-02-26 23:04:13 +000024923#if defined(FEAT_PROFILE) || defined(PROTO)
24924/*
24925 * Called when starting to read a function line.
24926 * "sourcing_lnum" must be correct!
24927 * When skipping lines it may not actually be executed, but we won't find out
24928 * until later and we need to store the time now.
24929 */
24930 void
24931func_line_start(cookie)
24932 void *cookie;
24933{
24934 funccall_T *fcp = (funccall_T *)cookie;
24935 ufunc_T *fp = fcp->func;
24936
24937 if (fp->uf_profiling && sourcing_lnum >= 1
24938 && sourcing_lnum <= fp->uf_lines.ga_len)
24939 {
24940 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024941 /* Skip continuation lines. */
24942 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24943 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024944 fp->uf_tml_execed = FALSE;
24945 profile_start(&fp->uf_tml_start);
24946 profile_zero(&fp->uf_tml_children);
24947 profile_get_wait(&fp->uf_tml_wait);
24948 }
24949}
24950
24951/*
24952 * Called when actually executing a function line.
24953 */
24954 void
24955func_line_exec(cookie)
24956 void *cookie;
24957{
24958 funccall_T *fcp = (funccall_T *)cookie;
24959 ufunc_T *fp = fcp->func;
24960
24961 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24962 fp->uf_tml_execed = TRUE;
24963}
24964
24965/*
24966 * Called when done with a function line.
24967 */
24968 void
24969func_line_end(cookie)
24970 void *cookie;
24971{
24972 funccall_T *fcp = (funccall_T *)cookie;
24973 ufunc_T *fp = fcp->func;
24974
24975 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24976 {
24977 if (fp->uf_tml_execed)
24978 {
24979 ++fp->uf_tml_count[fp->uf_tml_idx];
24980 profile_end(&fp->uf_tml_start);
24981 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024982 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024983 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24984 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024985 }
24986 fp->uf_tml_idx = -1;
24987 }
24988}
24989#endif
24990
Bram Moolenaar071d4272004-06-13 20:20:40 +000024991/*
24992 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024993 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024994 */
24995 int
24996func_has_ended(cookie)
24997 void *cookie;
24998{
Bram Moolenaar33570922005-01-25 22:26:29 +000024999 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025000
25001 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25002 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025003 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004 || fcp->returned);
25005}
25006
25007/*
25008 * return TRUE if cookie indicates a function which "abort"s on errors.
25009 */
25010 int
25011func_has_abort(cookie)
25012 void *cookie;
25013{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025014 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025015}
25016
25017#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25018typedef enum
25019{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025020 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25021 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25022 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025023} var_flavour_T;
25024
25025static var_flavour_T var_flavour __ARGS((char_u *varname));
25026
25027 static var_flavour_T
25028var_flavour(varname)
25029 char_u *varname;
25030{
25031 char_u *p = varname;
25032
25033 if (ASCII_ISUPPER(*p))
25034 {
25035 while (*(++p))
25036 if (ASCII_ISLOWER(*p))
25037 return VAR_FLAVOUR_SESSION;
25038 return VAR_FLAVOUR_VIMINFO;
25039 }
25040 else
25041 return VAR_FLAVOUR_DEFAULT;
25042}
25043#endif
25044
25045#if defined(FEAT_VIMINFO) || defined(PROTO)
25046/*
25047 * Restore global vars that start with a capital from the viminfo file
25048 */
25049 int
25050read_viminfo_varlist(virp, writing)
25051 vir_T *virp;
25052 int writing;
25053{
25054 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025055 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025056 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025057
25058 if (!writing && (find_viminfo_parameter('!') != NULL))
25059 {
25060 tab = vim_strchr(virp->vir_line + 1, '\t');
25061 if (tab != NULL)
25062 {
25063 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025064 switch (*tab)
25065 {
25066 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025067#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025068 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025069#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025070 case 'D': type = VAR_DICT; break;
25071 case 'L': type = VAR_LIST; break;
25072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025073
25074 tab = vim_strchr(tab, '\t');
25075 if (tab != NULL)
25076 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025077 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025078 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025079 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025080 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025081#ifdef FEAT_FLOAT
25082 else if (type == VAR_FLOAT)
25083 (void)string2float(tab + 1, &tv.vval.v_float);
25084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025085 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025086 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025087 if (type == VAR_DICT || type == VAR_LIST)
25088 {
25089 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25090
25091 if (etv == NULL)
25092 /* Failed to parse back the dict or list, use it as a
25093 * string. */
25094 tv.v_type = VAR_STRING;
25095 else
25096 {
25097 vim_free(tv.vval.v_string);
25098 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025099 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025100 }
25101 }
25102
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025103 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025104
25105 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025106 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025107 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25108 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025109 }
25110 }
25111 }
25112
25113 return viminfo_readline(virp);
25114}
25115
25116/*
25117 * Write global vars that start with a capital to the viminfo file
25118 */
25119 void
25120write_viminfo_varlist(fp)
25121 FILE *fp;
25122{
Bram Moolenaar33570922005-01-25 22:26:29 +000025123 hashitem_T *hi;
25124 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025125 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025126 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025127 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025128 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025129 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025130
25131 if (find_viminfo_parameter('!') == NULL)
25132 return;
25133
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025134 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025135
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025136 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025137 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025138 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025139 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025140 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025141 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025142 this_var = HI2DI(hi);
25143 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025144 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025145 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025146 {
25147 case VAR_STRING: s = "STR"; break;
25148 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025149#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025150 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025151#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025152 case VAR_DICT: s = "DIC"; break;
25153 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000025154 default: continue;
25155 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025156 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025157 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025158 if (p != NULL)
25159 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025160 vim_free(tofree);
25161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025162 }
25163 }
25164}
25165#endif
25166
25167#if defined(FEAT_SESSION) || defined(PROTO)
25168 int
25169store_session_globals(fd)
25170 FILE *fd;
25171{
Bram Moolenaar33570922005-01-25 22:26:29 +000025172 hashitem_T *hi;
25173 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025174 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175 char_u *p, *t;
25176
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025177 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025178 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025179 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025180 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025182 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025183 this_var = HI2DI(hi);
25184 if ((this_var->di_tv.v_type == VAR_NUMBER
25185 || this_var->di_tv.v_type == VAR_STRING)
25186 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025187 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025188 /* Escape special characters with a backslash. Turn a LF and
25189 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025190 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025191 (char_u *)"\\\"\n\r");
25192 if (p == NULL) /* out of memory */
25193 break;
25194 for (t = p; *t != NUL; ++t)
25195 if (*t == '\n')
25196 *t = 'n';
25197 else if (*t == '\r')
25198 *t = 'r';
25199 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025200 this_var->di_key,
25201 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25202 : ' ',
25203 p,
25204 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25205 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025206 || put_eol(fd) == FAIL)
25207 {
25208 vim_free(p);
25209 return FAIL;
25210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025211 vim_free(p);
25212 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025213#ifdef FEAT_FLOAT
25214 else if (this_var->di_tv.v_type == VAR_FLOAT
25215 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25216 {
25217 float_T f = this_var->di_tv.vval.v_float;
25218 int sign = ' ';
25219
25220 if (f < 0)
25221 {
25222 f = -f;
25223 sign = '-';
25224 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025225 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025226 this_var->di_key, sign, f) < 0)
25227 || put_eol(fd) == FAIL)
25228 return FAIL;
25229 }
25230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025231 }
25232 }
25233 return OK;
25234}
25235#endif
25236
Bram Moolenaar661b1822005-07-28 22:36:45 +000025237/*
25238 * Display script name where an item was last set.
25239 * Should only be invoked when 'verbose' is non-zero.
25240 */
25241 void
25242last_set_msg(scriptID)
25243 scid_T scriptID;
25244{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025245 char_u *p;
25246
Bram Moolenaar661b1822005-07-28 22:36:45 +000025247 if (scriptID != 0)
25248 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025249 p = home_replace_save(NULL, get_scriptname(scriptID));
25250 if (p != NULL)
25251 {
25252 verbose_enter();
25253 MSG_PUTS(_("\n\tLast set from "));
25254 MSG_PUTS(p);
25255 vim_free(p);
25256 verbose_leave();
25257 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025258 }
25259}
25260
Bram Moolenaard812df62008-11-09 12:46:09 +000025261/*
25262 * List v:oldfiles in a nice way.
25263 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025264 void
25265ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025266 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025267{
25268 list_T *l = vimvars[VV_OLDFILES].vv_list;
25269 listitem_T *li;
25270 int nr = 0;
25271
25272 if (l == NULL)
25273 msg((char_u *)_("No old files"));
25274 else
25275 {
25276 msg_start();
25277 msg_scroll = TRUE;
25278 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25279 {
25280 msg_outnum((long)++nr);
25281 MSG_PUTS(": ");
25282 msg_outtrans(get_tv_string(&li->li_tv));
25283 msg_putchar('\n');
25284 out_flush(); /* output one line at a time */
25285 ui_breakcheck();
25286 }
25287 /* Assume "got_int" was set to truncate the listing. */
25288 got_int = FALSE;
25289
25290#ifdef FEAT_BROWSE_CMD
25291 if (cmdmod.browse)
25292 {
25293 quit_more = FALSE;
25294 nr = prompt_for_number(FALSE);
25295 msg_starthere();
25296 if (nr > 0)
25297 {
25298 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25299 (long)nr);
25300
25301 if (p != NULL)
25302 {
25303 p = expand_env_save(p);
25304 eap->arg = p;
25305 eap->cmdidx = CMD_edit;
25306 cmdmod.browse = FALSE;
25307 do_exedit(eap, NULL);
25308 vim_free(p);
25309 }
25310 }
25311 }
25312#endif
25313 }
25314}
25315
Bram Moolenaar53744302015-07-17 17:38:22 +020025316/* reset v:option_new, v:option_old and v:option_type */
25317 void
25318reset_v_option_vars()
25319{
25320 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25321 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25322 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25323}
25324
25325
Bram Moolenaar071d4272004-06-13 20:20:40 +000025326#endif /* FEAT_EVAL */
25327
Bram Moolenaar071d4272004-06-13 20:20:40 +000025328
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025329#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025330
25331#ifdef WIN3264
25332/*
25333 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25334 */
25335static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25336static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25337static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25338
25339/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025340 * Get the short path (8.3) for the filename in "fnamep".
25341 * Only works for a valid file name.
25342 * When the path gets longer "fnamep" is changed and the allocated buffer
25343 * is put in "bufp".
25344 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25345 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025346 */
25347 static int
25348get_short_pathname(fnamep, bufp, fnamelen)
25349 char_u **fnamep;
25350 char_u **bufp;
25351 int *fnamelen;
25352{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025353 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354 char_u *newbuf;
25355
25356 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357 l = GetShortPathName(*fnamep, *fnamep, len);
25358 if (l > len - 1)
25359 {
25360 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025361 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025362 newbuf = vim_strnsave(*fnamep, l);
25363 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025365
25366 vim_free(*bufp);
25367 *fnamep = *bufp = newbuf;
25368
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025369 /* Really should always succeed, as the buffer is big enough. */
25370 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025371 }
25372
25373 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025374 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025375}
25376
25377/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025378 * Get the short path (8.3) for the filename in "fname". The converted
25379 * path is returned in "bufp".
25380 *
25381 * Some of the directories specified in "fname" may not exist. This function
25382 * will shorten the existing directories at the beginning of the path and then
25383 * append the remaining non-existing path.
25384 *
25385 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025386 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025387 * bufp - Pointer to an allocated buffer for the filename.
25388 * fnamelen - Length of the filename pointed to by fname
25389 *
25390 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 */
25392 static int
25393shortpath_for_invalid_fname(fname, bufp, fnamelen)
25394 char_u **fname;
25395 char_u **bufp;
25396 int *fnamelen;
25397{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025398 char_u *short_fname, *save_fname, *pbuf_unused;
25399 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025400 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025401 int old_len, len;
25402 int new_len, sfx_len;
25403 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404
25405 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025406 old_len = *fnamelen;
25407 save_fname = vim_strnsave(*fname, old_len);
25408 pbuf_unused = NULL;
25409 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025410
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025411 endp = save_fname + old_len - 1; /* Find the end of the copy */
25412 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025413
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025414 /*
25415 * Try shortening the supplied path till it succeeds by removing one
25416 * directory at a time from the tail of the path.
25417 */
25418 len = 0;
25419 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025420 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025421 /* go back one path-separator */
25422 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25423 --endp;
25424 if (endp <= save_fname)
25425 break; /* processed the complete path */
25426
25427 /*
25428 * Replace the path separator with a NUL and try to shorten the
25429 * resulting path.
25430 */
25431 ch = *endp;
25432 *endp = 0;
25433 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025434 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025435 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25436 {
25437 retval = FAIL;
25438 goto theend;
25439 }
25440 *endp = ch; /* preserve the string */
25441
25442 if (len > 0)
25443 break; /* successfully shortened the path */
25444
25445 /* failed to shorten the path. Skip the path separator */
25446 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447 }
25448
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025449 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025450 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025451 /*
25452 * Succeeded in shortening the path. Now concatenate the shortened
25453 * path with the remaining path at the tail.
25454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025456 /* Compute the length of the new path. */
25457 sfx_len = (int)(save_endp - endp) + 1;
25458 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025459
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025460 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025461 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025462 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025463 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025464 /* There is not enough space in the currently allocated string,
25465 * copy it to a buffer big enough. */
25466 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025468 {
25469 retval = FAIL;
25470 goto theend;
25471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025472 }
25473 else
25474 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025475 /* Transfer short_fname to the main buffer (it's big enough),
25476 * unless get_short_pathname() did its work in-place. */
25477 *fname = *bufp = save_fname;
25478 if (short_fname != save_fname)
25479 vim_strncpy(save_fname, short_fname, len);
25480 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025482
25483 /* concat the not-shortened part of the path */
25484 vim_strncpy(*fname + len, endp, sfx_len);
25485 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025487
25488theend:
25489 vim_free(pbuf_unused);
25490 vim_free(save_fname);
25491
25492 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493}
25494
25495/*
25496 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025497 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025498 */
25499 static int
25500shortpath_for_partial(fnamep, bufp, fnamelen)
25501 char_u **fnamep;
25502 char_u **bufp;
25503 int *fnamelen;
25504{
25505 int sepcount, len, tflen;
25506 char_u *p;
25507 char_u *pbuf, *tfname;
25508 int hasTilde;
25509
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025510 /* Count up the path separators from the RHS.. so we know which part
25511 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025512 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025513 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025514 if (vim_ispathsep(*p))
25515 ++sepcount;
25516
25517 /* Need full path first (use expand_env() to remove a "~/") */
25518 hasTilde = (**fnamep == '~');
25519 if (hasTilde)
25520 pbuf = tfname = expand_env_save(*fnamep);
25521 else
25522 pbuf = tfname = FullName_save(*fnamep, FALSE);
25523
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025524 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025525
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025526 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25527 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025528
25529 if (len == 0)
25530 {
25531 /* Don't have a valid filename, so shorten the rest of the
25532 * path if we can. This CAN give us invalid 8.3 filenames, but
25533 * there's not a lot of point in guessing what it might be.
25534 */
25535 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025536 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25537 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025538 }
25539
25540 /* Count the paths backward to find the beginning of the desired string. */
25541 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025542 {
25543#ifdef FEAT_MBYTE
25544 if (has_mbyte)
25545 p -= mb_head_off(tfname, p);
25546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025547 if (vim_ispathsep(*p))
25548 {
25549 if (sepcount == 0 || (hasTilde && sepcount == 1))
25550 break;
25551 else
25552 sepcount --;
25553 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025555 if (hasTilde)
25556 {
25557 --p;
25558 if (p >= tfname)
25559 *p = '~';
25560 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025561 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025562 }
25563 else
25564 ++p;
25565
25566 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25567 vim_free(*bufp);
25568 *fnamelen = (int)STRLEN(p);
25569 *bufp = pbuf;
25570 *fnamep = p;
25571
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025572 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025573}
25574#endif /* WIN3264 */
25575
25576/*
25577 * Adjust a filename, according to a string of modifiers.
25578 * *fnamep must be NUL terminated when called. When returning, the length is
25579 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025580 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025581 * When there is an error, *fnamep is set to NULL.
25582 */
25583 int
25584modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25585 char_u *src; /* string with modifiers */
25586 int *usedlen; /* characters after src that are used */
25587 char_u **fnamep; /* file name so far */
25588 char_u **bufp; /* buffer for allocated file name or NULL */
25589 int *fnamelen; /* length of fnamep */
25590{
25591 int valid = 0;
25592 char_u *tail;
25593 char_u *s, *p, *pbuf;
25594 char_u dirname[MAXPATHL];
25595 int c;
25596 int has_fullname = 0;
25597#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025598 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025599 int has_shortname = 0;
25600#endif
25601
25602repeat:
25603 /* ":p" - full path/file_name */
25604 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25605 {
25606 has_fullname = 1;
25607
25608 valid |= VALID_PATH;
25609 *usedlen += 2;
25610
25611 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25612 if ((*fnamep)[0] == '~'
25613#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25614 && ((*fnamep)[1] == '/'
25615# ifdef BACKSLASH_IN_FILENAME
25616 || (*fnamep)[1] == '\\'
25617# endif
25618 || (*fnamep)[1] == NUL)
25619
25620#endif
25621 )
25622 {
25623 *fnamep = expand_env_save(*fnamep);
25624 vim_free(*bufp); /* free any allocated file name */
25625 *bufp = *fnamep;
25626 if (*fnamep == NULL)
25627 return -1;
25628 }
25629
25630 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025631 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025632 {
25633 if (vim_ispathsep(*p)
25634 && p[1] == '.'
25635 && (p[2] == NUL
25636 || vim_ispathsep(p[2])
25637 || (p[2] == '.'
25638 && (p[3] == NUL || vim_ispathsep(p[3])))))
25639 break;
25640 }
25641
25642 /* FullName_save() is slow, don't use it when not needed. */
25643 if (*p != NUL || !vim_isAbsName(*fnamep))
25644 {
25645 *fnamep = FullName_save(*fnamep, *p != NUL);
25646 vim_free(*bufp); /* free any allocated file name */
25647 *bufp = *fnamep;
25648 if (*fnamep == NULL)
25649 return -1;
25650 }
25651
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025652#ifdef WIN3264
25653# if _WIN32_WINNT >= 0x0500
25654 if (vim_strchr(*fnamep, '~') != NULL)
25655 {
25656 /* Expand 8.3 filename to full path. Needed to make sure the same
25657 * file does not have two different names.
25658 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25659 p = alloc(_MAX_PATH + 1);
25660 if (p != NULL)
25661 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025662 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025663 {
25664 vim_free(*bufp);
25665 *bufp = *fnamep = p;
25666 }
25667 else
25668 vim_free(p);
25669 }
25670 }
25671# endif
25672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025673 /* Append a path separator to a directory. */
25674 if (mch_isdir(*fnamep))
25675 {
25676 /* Make room for one or two extra characters. */
25677 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25678 vim_free(*bufp); /* free any allocated file name */
25679 *bufp = *fnamep;
25680 if (*fnamep == NULL)
25681 return -1;
25682 add_pathsep(*fnamep);
25683 }
25684 }
25685
25686 /* ":." - path relative to the current directory */
25687 /* ":~" - path relative to the home directory */
25688 /* ":8" - shortname path - postponed till after */
25689 while (src[*usedlen] == ':'
25690 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25691 {
25692 *usedlen += 2;
25693 if (c == '8')
25694 {
25695#ifdef WIN3264
25696 has_shortname = 1; /* Postpone this. */
25697#endif
25698 continue;
25699 }
25700 pbuf = NULL;
25701 /* Need full path first (use expand_env() to remove a "~/") */
25702 if (!has_fullname)
25703 {
25704 if (c == '.' && **fnamep == '~')
25705 p = pbuf = expand_env_save(*fnamep);
25706 else
25707 p = pbuf = FullName_save(*fnamep, FALSE);
25708 }
25709 else
25710 p = *fnamep;
25711
25712 has_fullname = 0;
25713
25714 if (p != NULL)
25715 {
25716 if (c == '.')
25717 {
25718 mch_dirname(dirname, MAXPATHL);
25719 s = shorten_fname(p, dirname);
25720 if (s != NULL)
25721 {
25722 *fnamep = s;
25723 if (pbuf != NULL)
25724 {
25725 vim_free(*bufp); /* free any allocated file name */
25726 *bufp = pbuf;
25727 pbuf = NULL;
25728 }
25729 }
25730 }
25731 else
25732 {
25733 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25734 /* Only replace it when it starts with '~' */
25735 if (*dirname == '~')
25736 {
25737 s = vim_strsave(dirname);
25738 if (s != NULL)
25739 {
25740 *fnamep = s;
25741 vim_free(*bufp);
25742 *bufp = s;
25743 }
25744 }
25745 }
25746 vim_free(pbuf);
25747 }
25748 }
25749
25750 tail = gettail(*fnamep);
25751 *fnamelen = (int)STRLEN(*fnamep);
25752
25753 /* ":h" - head, remove "/file_name", can be repeated */
25754 /* Don't remove the first "/" or "c:\" */
25755 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25756 {
25757 valid |= VALID_HEAD;
25758 *usedlen += 2;
25759 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025760 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025761 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025762 *fnamelen = (int)(tail - *fnamep);
25763#ifdef VMS
25764 if (*fnamelen > 0)
25765 *fnamelen += 1; /* the path separator is part of the path */
25766#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025767 if (*fnamelen == 0)
25768 {
25769 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25770 p = vim_strsave((char_u *)".");
25771 if (p == NULL)
25772 return -1;
25773 vim_free(*bufp);
25774 *bufp = *fnamep = tail = p;
25775 *fnamelen = 1;
25776 }
25777 else
25778 {
25779 while (tail > s && !after_pathsep(s, tail))
25780 mb_ptr_back(*fnamep, tail);
25781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025782 }
25783
25784 /* ":8" - shortname */
25785 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25786 {
25787 *usedlen += 2;
25788#ifdef WIN3264
25789 has_shortname = 1;
25790#endif
25791 }
25792
25793#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025794 /*
25795 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025796 */
25797 if (has_shortname)
25798 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025799 /* Copy the string if it is shortened by :h and when it wasn't copied
25800 * yet, because we are going to change it in place. Avoids changing
25801 * the buffer name for "%:8". */
25802 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025803 {
25804 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025805 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025806 return -1;
25807 vim_free(*bufp);
25808 *bufp = *fnamep = p;
25809 }
25810
25811 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025812 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025813 if (!has_fullname && !vim_isAbsName(*fnamep))
25814 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025815 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025816 return -1;
25817 }
25818 else
25819 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025820 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025821
Bram Moolenaardc935552011-08-17 15:23:23 +020025822 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025823 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025824 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025825 return -1;
25826
25827 if (l == 0)
25828 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025829 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025831 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025832 return -1;
25833 }
25834 *fnamelen = l;
25835 }
25836 }
25837#endif /* WIN3264 */
25838
25839 /* ":t" - tail, just the basename */
25840 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25841 {
25842 *usedlen += 2;
25843 *fnamelen -= (int)(tail - *fnamep);
25844 *fnamep = tail;
25845 }
25846
25847 /* ":e" - extension, can be repeated */
25848 /* ":r" - root, without extension, can be repeated */
25849 while (src[*usedlen] == ':'
25850 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25851 {
25852 /* find a '.' in the tail:
25853 * - for second :e: before the current fname
25854 * - otherwise: The last '.'
25855 */
25856 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25857 s = *fnamep - 2;
25858 else
25859 s = *fnamep + *fnamelen - 1;
25860 for ( ; s > tail; --s)
25861 if (s[0] == '.')
25862 break;
25863 if (src[*usedlen + 1] == 'e') /* :e */
25864 {
25865 if (s > tail)
25866 {
25867 *fnamelen += (int)(*fnamep - (s + 1));
25868 *fnamep = s + 1;
25869#ifdef VMS
25870 /* cut version from the extension */
25871 s = *fnamep + *fnamelen - 1;
25872 for ( ; s > *fnamep; --s)
25873 if (s[0] == ';')
25874 break;
25875 if (s > *fnamep)
25876 *fnamelen = s - *fnamep;
25877#endif
25878 }
25879 else if (*fnamep <= tail)
25880 *fnamelen = 0;
25881 }
25882 else /* :r */
25883 {
25884 if (s > tail) /* remove one extension */
25885 *fnamelen = (int)(s - *fnamep);
25886 }
25887 *usedlen += 2;
25888 }
25889
25890 /* ":s?pat?foo?" - substitute */
25891 /* ":gs?pat?foo?" - global substitute */
25892 if (src[*usedlen] == ':'
25893 && (src[*usedlen + 1] == 's'
25894 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25895 {
25896 char_u *str;
25897 char_u *pat;
25898 char_u *sub;
25899 int sep;
25900 char_u *flags;
25901 int didit = FALSE;
25902
25903 flags = (char_u *)"";
25904 s = src + *usedlen + 2;
25905 if (src[*usedlen + 1] == 'g')
25906 {
25907 flags = (char_u *)"g";
25908 ++s;
25909 }
25910
25911 sep = *s++;
25912 if (sep)
25913 {
25914 /* find end of pattern */
25915 p = vim_strchr(s, sep);
25916 if (p != NULL)
25917 {
25918 pat = vim_strnsave(s, (int)(p - s));
25919 if (pat != NULL)
25920 {
25921 s = p + 1;
25922 /* find end of substitution */
25923 p = vim_strchr(s, sep);
25924 if (p != NULL)
25925 {
25926 sub = vim_strnsave(s, (int)(p - s));
25927 str = vim_strnsave(*fnamep, *fnamelen);
25928 if (sub != NULL && str != NULL)
25929 {
25930 *usedlen = (int)(p + 1 - src);
25931 s = do_string_sub(str, pat, sub, flags);
25932 if (s != NULL)
25933 {
25934 *fnamep = s;
25935 *fnamelen = (int)STRLEN(s);
25936 vim_free(*bufp);
25937 *bufp = s;
25938 didit = TRUE;
25939 }
25940 }
25941 vim_free(sub);
25942 vim_free(str);
25943 }
25944 vim_free(pat);
25945 }
25946 }
25947 /* after using ":s", repeat all the modifiers */
25948 if (didit)
25949 goto repeat;
25950 }
25951 }
25952
Bram Moolenaar26df0922014-02-23 23:39:13 +010025953 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25954 {
25955 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25956 if (p == NULL)
25957 return -1;
25958 vim_free(*bufp);
25959 *bufp = *fnamep = p;
25960 *fnamelen = (int)STRLEN(p);
25961 *usedlen += 2;
25962 }
25963
Bram Moolenaar071d4272004-06-13 20:20:40 +000025964 return valid;
25965}
25966
25967/*
25968 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25969 * "flags" can be "g" to do a global substitute.
25970 * Returns an allocated string, NULL for error.
25971 */
25972 char_u *
25973do_string_sub(str, pat, sub, flags)
25974 char_u *str;
25975 char_u *pat;
25976 char_u *sub;
25977 char_u *flags;
25978{
25979 int sublen;
25980 regmatch_T regmatch;
25981 int i;
25982 int do_all;
25983 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025984 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025985 garray_T ga;
25986 char_u *ret;
25987 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025988 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025989
25990 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25991 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025992 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025993
25994 ga_init2(&ga, 1, 200);
25995
25996 do_all = (flags[0] == 'g');
25997
25998 regmatch.rm_ic = p_ic;
25999 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26000 if (regmatch.regprog != NULL)
26001 {
26002 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026003 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026004 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26005 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026006 /* Skip empty match except for first match. */
26007 if (regmatch.startp[0] == regmatch.endp[0])
26008 {
26009 if (zero_width == regmatch.startp[0])
26010 {
26011 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026012 i = MB_PTR2LEN(tail);
26013 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26014 (size_t)i);
26015 ga.ga_len += i;
26016 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026017 continue;
26018 }
26019 zero_width = regmatch.startp[0];
26020 }
26021
Bram Moolenaar071d4272004-06-13 20:20:40 +000026022 /*
26023 * Get some space for a temporary buffer to do the substitution
26024 * into. It will contain:
26025 * - The text up to where the match is.
26026 * - The substituted text.
26027 * - The text after the match.
26028 */
26029 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026030 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026031 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26032 {
26033 ga_clear(&ga);
26034 break;
26035 }
26036
26037 /* copy the text up to where the match is */
26038 i = (int)(regmatch.startp[0] - tail);
26039 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26040 /* add the substituted text */
26041 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26042 + ga.ga_len + i, TRUE, TRUE, FALSE);
26043 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026044 tail = regmatch.endp[0];
26045 if (*tail == NUL)
26046 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026047 if (!do_all)
26048 break;
26049 }
26050
26051 if (ga.ga_data != NULL)
26052 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26053
Bram Moolenaar473de612013-06-08 18:19:48 +020026054 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026055 }
26056
26057 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26058 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026059 if (p_cpo == empty_option)
26060 p_cpo = save_cpo;
26061 else
26062 /* Darn, evaluating {sub} expression changed the value. */
26063 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026064
26065 return ret;
26066}
26067
26068#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */