blob: 565d71d78631075665d10ce7aaccee577d469fc2 [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));
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100812#ifdef FEAT_FLOAT
813static float_T get_tv_float(typval_T *varp);
814#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000815static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000816static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000817static char_u *get_tv_string __ARGS((typval_T *varp));
818static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000819static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100820static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
821static 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 +0000822static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
Bram Moolenaarf1f60f82016-01-16 15:40:53 +0100823static funccall_T *get_funccal __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000824static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
825static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000826static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
827static 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 +0000828static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200829static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
830static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200831static int var_check_func_name __ARGS((char_u *name, int new_var));
832static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200833static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000834static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000835static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
836static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
837static int eval_fname_script __ARGS((char_u *p));
838static int eval_fname_sid __ARGS((char_u *p));
839static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000840static ufunc_T *find_func __ARGS((char_u *name));
841static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200842static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843#ifdef FEAT_PROFILE
844static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000845static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
846static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
847static int
848# ifdef __BORLANDC__
849 _RTLENTRYF
850# endif
851 prof_total_cmp __ARGS((const void *s1, const void *s2));
852static int
853# ifdef __BORLANDC__
854 _RTLENTRYF
855# endif
856 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000857#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200858static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000859static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000860static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000861static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000862static 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 +0000863static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
864static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000865static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000866static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
Bram Moolenaarc9703302016-01-17 21:49:33 +0100867static win_T *find_tabwin __ARGS((typval_T *wvp, typval_T *tvp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000868static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000869static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000870static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000871static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200872static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200873static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000874
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200875
876#ifdef EBCDIC
877static int compare_func_name __ARGS((const void *s1, const void *s2));
878static void sortFunctions __ARGS(());
879#endif
880
Bram Moolenaar33570922005-01-25 22:26:29 +0000881/*
882 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000883 */
884 void
885eval_init()
886{
Bram Moolenaar33570922005-01-25 22:26:29 +0000887 int i;
888 struct vimvar *p;
889
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200890 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
891 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200892 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000893 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000894 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000895
896 for (i = 0; i < VV_LEN; ++i)
897 {
898 p = &vimvars[i];
899 STRCPY(p->vv_di.di_key, p->vv_name);
900 if (p->vv_flags & VV_RO)
901 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
902 else if (p->vv_flags & VV_RO_SBX)
903 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
904 else
905 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000906
907 /* add to v: scope dict, unless the value is not always available */
908 if (p->vv_type != VAR_UNKNOWN)
909 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000910 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000911 /* add to compat scope dict */
912 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000913 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000914 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100915 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200916 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100917 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200918 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200919
920#ifdef EBCDIC
921 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100922 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200923 */
924 sortFunctions();
925#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000926}
927
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000928#if defined(EXITFREE) || defined(PROTO)
929 void
930eval_clear()
931{
932 int i;
933 struct vimvar *p;
934
935 for (i = 0; i < VV_LEN; ++i)
936 {
937 p = &vimvars[i];
938 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000939 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000940 vim_free(p->vv_str);
941 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000942 }
943 else if (p->vv_di.di_tv.v_type == VAR_LIST)
944 {
945 list_unref(p->vv_list);
946 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000947 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000948 }
949 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000950 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000951 hash_clear(&compat_hashtab);
952
Bram Moolenaard9fba312005-06-26 22:34:35 +0000953 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100954# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200955 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100956# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000957
958 /* global variables */
959 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000960
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000961 /* autoloaded script names */
962 ga_clear_strings(&ga_loaded);
963
Bram Moolenaarcca74132013-09-25 21:00:28 +0200964 /* Script-local variables. First clear all the variables and in a second
965 * loop free the scriptvar_T, because a variable in one script might hold
966 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200967 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200968 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200969 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200970 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200971 ga_clear(&ga_scripts);
972
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000973 /* unreferenced lists and dicts */
974 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000975
976 /* functions */
977 free_all_functions();
978 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000979}
980#endif
981
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 * Return the name of the executed function.
984 */
985 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100986func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000988 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990
991/*
992 * Return the address holding the next breakpoint line for a funccall cookie.
993 */
994 linenr_T *
995func_breakpoint(cookie)
996 void *cookie;
997{
Bram Moolenaar33570922005-01-25 22:26:29 +0000998 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999}
1000
1001/*
1002 * Return the address holding the debug tick for a funccall cookie.
1003 */
1004 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001005func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
Bram Moolenaar33570922005-01-25 22:26:29 +00001007 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008}
1009
1010/*
1011 * Return the nesting level for a funccall cookie.
1012 */
1013 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001014func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015{
Bram Moolenaar33570922005-01-25 22:26:29 +00001016 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017}
1018
1019/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001020funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001022/* pointer to list of previously used funccal, still around because some
1023 * item in it is still being used. */
1024funccall_T *previous_funccal = NULL;
1025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026/*
1027 * Return TRUE when a function was ended by a ":return" command.
1028 */
1029 int
1030current_func_returned()
1031{
1032 return current_funccal->returned;
1033}
1034
1035
1036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 * Set an internal variable to a string value. Creates the variable if it does
1038 * not already exist.
1039 */
1040 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001041set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001043 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001044 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
1046 val = vim_strsave(value);
1047 if (val != NULL)
1048 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001049 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001050 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001052 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001053 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 }
1055 }
1056}
1057
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001058static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001059static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060static char_u *redir_endp = NULL;
1061static char_u *redir_varname = NULL;
1062
1063/*
1064 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001065 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 * Returns OK if successfully completed the setup. FAIL otherwise.
1067 */
1068 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001069var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070{
1071 int save_emsg;
1072 int err;
1073 typval_T tv;
1074
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001075 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001076 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001077 {
1078 EMSG(_(e_invarg));
1079 return FAIL;
1080 }
1081
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001082 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 redir_varname = vim_strsave(name);
1084 if (redir_varname == NULL)
1085 return FAIL;
1086
1087 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1088 if (redir_lval == NULL)
1089 {
1090 var_redir_stop();
1091 return FAIL;
1092 }
1093
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001094 /* The output is stored in growarray "redir_ga" until redirection ends. */
1095 ga_init2(&redir_ga, (int)sizeof(char), 500);
1096
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001098 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001099 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1101 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001102 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 if (redir_endp != NULL && *redir_endp != NUL)
1104 /* Trailing characters are present after the variable name */
1105 EMSG(_(e_trailing));
1106 else
1107 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001108 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 var_redir_stop();
1110 return FAIL;
1111 }
1112
1113 /* check if we can write to the variable: set it to or append an empty
1114 * string */
1115 save_emsg = did_emsg;
1116 did_emsg = FALSE;
1117 tv.v_type = VAR_STRING;
1118 tv.vval.v_string = (char_u *)"";
1119 if (append)
1120 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1121 else
1122 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001123 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001125 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 if (err)
1127 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001128 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 var_redir_stop();
1130 return FAIL;
1131 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132
1133 return OK;
1134}
1135
1136/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001137 * Append "value[value_len]" to the variable set by var_redir_start().
1138 * The actual appending is postponed until redirection ends, because the value
1139 * appended may in fact be the string we write to, changing it may cause freed
1140 * memory to be used:
1141 * :redir => foo
1142 * :let foo
1143 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 */
1145 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001146var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001148 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149
1150 if (redir_lval == NULL)
1151 return;
1152
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001154 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001156 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001158 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001159 {
1160 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001161 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001162 }
1163 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165}
1166
1167/*
1168 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001169 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170 */
1171 void
1172var_redir_stop()
1173{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001174 typval_T tv;
1175
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 if (redir_lval != NULL)
1177 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001178 /* If there was no error: assign the text to the variable. */
1179 if (redir_endp != NULL)
1180 {
1181 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1182 tv.v_type = VAR_STRING;
1183 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001184 /* Call get_lval() again, if it's inside a Dict or List it may
1185 * have changed. */
1186 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001187 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001188 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1189 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1190 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001191 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001192
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001193 /* free the collected output */
1194 vim_free(redir_ga.ga_data);
1195 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001196
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197 vim_free(redir_lval);
1198 redir_lval = NULL;
1199 }
1200 vim_free(redir_varname);
1201 redir_varname = NULL;
1202}
1203
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204# if defined(FEAT_MBYTE) || defined(PROTO)
1205 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001206eval_charconvert(
1207 char_u *enc_from,
1208 char_u *enc_to,
1209 char_u *fname_from,
1210 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211{
1212 int err = FALSE;
1213
1214 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1215 set_vim_var_string(VV_CC_TO, enc_to, -1);
1216 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1217 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1218 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1219 err = TRUE;
1220 set_vim_var_string(VV_CC_FROM, NULL, -1);
1221 set_vim_var_string(VV_CC_TO, NULL, -1);
1222 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1223 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1224
1225 if (err)
1226 return FAIL;
1227 return OK;
1228}
1229# endif
1230
1231# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1232 int
1233eval_printexpr(fname, args)
1234 char_u *fname;
1235 char_u *args;
1236{
1237 int err = FALSE;
1238
1239 set_vim_var_string(VV_FNAME_IN, fname, -1);
1240 set_vim_var_string(VV_CMDARG, args, -1);
1241 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1242 err = TRUE;
1243 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1244 set_vim_var_string(VV_CMDARG, NULL, -1);
1245
1246 if (err)
1247 {
1248 mch_remove(fname);
1249 return FAIL;
1250 }
1251 return OK;
1252}
1253# endif
1254
1255# if defined(FEAT_DIFF) || defined(PROTO)
1256 void
1257eval_diff(origfile, newfile, outfile)
1258 char_u *origfile;
1259 char_u *newfile;
1260 char_u *outfile;
1261{
1262 int err = FALSE;
1263
1264 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1265 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1266 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1267 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1268 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1269 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1270 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1271}
1272
1273 void
1274eval_patch(origfile, difffile, outfile)
1275 char_u *origfile;
1276 char_u *difffile;
1277 char_u *outfile;
1278{
1279 int err;
1280
1281 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1282 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1283 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1284 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1285 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1286 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1287 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1288}
1289# endif
1290
1291/*
1292 * Top level evaluation function, returning a boolean.
1293 * Sets "error" to TRUE if there was an error.
1294 * Return TRUE or FALSE.
1295 */
1296 int
1297eval_to_bool(arg, error, nextcmd, skip)
1298 char_u *arg;
1299 int *error;
1300 char_u **nextcmd;
1301 int skip; /* only parse, don't execute */
1302{
Bram Moolenaar33570922005-01-25 22:26:29 +00001303 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 int retval = FALSE;
1305
1306 if (skip)
1307 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001308 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 else
1311 {
1312 *error = FALSE;
1313 if (!skip)
1314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001315 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001316 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 }
1318 }
1319 if (skip)
1320 --emsg_skip;
1321
1322 return retval;
1323}
1324
1325/*
1326 * Top level evaluation function, returning a string. If "skip" is TRUE,
1327 * only parsing to "nextcmd" is done, without reporting errors. Return
1328 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1329 */
1330 char_u *
1331eval_to_string_skip(arg, nextcmd, skip)
1332 char_u *arg;
1333 char_u **nextcmd;
1334 int skip; /* only parse, don't execute */
1335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 char_u *retval;
1338
1339 if (skip)
1340 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001341 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 retval = NULL;
1343 else
1344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001345 retval = vim_strsave(get_tv_string(&tv));
1346 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
1348 if (skip)
1349 --emsg_skip;
1350
1351 return retval;
1352}
1353
1354/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001355 * Skip over an expression at "*pp".
1356 * Return FAIL for an error, OK otherwise.
1357 */
1358 int
1359skip_expr(pp)
1360 char_u **pp;
1361{
Bram Moolenaar33570922005-01-25 22:26:29 +00001362 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001363
1364 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001365 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001366}
1367
1368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001370 * When "convert" is TRUE convert a List into a sequence of lines and convert
1371 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 * Return pointer to allocated memory, or NULL for failure.
1373 */
1374 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001375eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 char_u *arg;
1377 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001378 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379{
Bram Moolenaar33570922005-01-25 22:26:29 +00001380 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001382 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001383#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001384 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001387 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 retval = NULL;
1389 else
1390 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001391 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001392 {
1393 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001394 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001395 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001396 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001397 if (tv.vval.v_list->lv_len > 0)
1398 ga_append(&ga, NL);
1399 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001400 ga_append(&ga, NUL);
1401 retval = (char_u *)ga.ga_data;
1402 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001403#ifdef FEAT_FLOAT
1404 else if (convert && tv.v_type == VAR_FLOAT)
1405 {
1406 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1407 retval = vim_strsave(numbuf);
1408 }
1409#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001410 else
1411 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001412 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 }
1414
1415 return retval;
1416}
1417
1418/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001419 * Call eval_to_string() without using current local variables and using
1420 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 */
1422 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001423eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 char_u *arg;
1425 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001426 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428 char_u *retval;
1429 void *save_funccalp;
1430
1431 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001432 if (use_sandbox)
1433 ++sandbox;
1434 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001435 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001436 if (use_sandbox)
1437 --sandbox;
1438 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 restore_funccal(save_funccalp);
1440 return retval;
1441}
1442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443/*
1444 * Top level evaluation function, returning a number.
1445 * Evaluates "expr" silently.
1446 * Returns -1 for an error.
1447 */
1448 int
1449eval_to_number(expr)
1450 char_u *expr;
1451{
Bram Moolenaar33570922005-01-25 22:26:29 +00001452 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001454 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
1456 ++emsg_off;
1457
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001458 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 retval = -1;
1460 else
1461 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001462 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001463 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 }
1465 --emsg_off;
1466
1467 return retval;
1468}
1469
Bram Moolenaara40058a2005-07-11 22:42:07 +00001470/*
1471 * Prepare v: variable "idx" to be used.
1472 * Save the current typeval in "save_tv".
1473 * When not used yet add the variable to the v: hashtable.
1474 */
1475 static void
1476prepare_vimvar(idx, save_tv)
1477 int idx;
1478 typval_T *save_tv;
1479{
1480 *save_tv = vimvars[idx].vv_tv;
1481 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1482 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1483}
1484
1485/*
1486 * Restore v: variable "idx" to typeval "save_tv".
1487 * When no longer defined, remove the variable from the v: hashtable.
1488 */
1489 static void
1490restore_vimvar(idx, save_tv)
1491 int idx;
1492 typval_T *save_tv;
1493{
1494 hashitem_T *hi;
1495
Bram Moolenaara40058a2005-07-11 22:42:07 +00001496 vimvars[idx].vv_tv = *save_tv;
1497 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1498 {
1499 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1500 if (HASHITEM_EMPTY(hi))
1501 EMSG2(_(e_intern2), "restore_vimvar()");
1502 else
1503 hash_remove(&vimvarht, hi);
1504 }
1505}
1506
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001507#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001508/*
1509 * Evaluate an expression to a list with suggestions.
1510 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001511 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001512 */
1513 list_T *
1514eval_spell_expr(badword, expr)
1515 char_u *badword;
1516 char_u *expr;
1517{
1518 typval_T save_val;
1519 typval_T rettv;
1520 list_T *list = NULL;
1521 char_u *p = skipwhite(expr);
1522
1523 /* Set "v:val" to the bad word. */
1524 prepare_vimvar(VV_VAL, &save_val);
1525 vimvars[VV_VAL].vv_type = VAR_STRING;
1526 vimvars[VV_VAL].vv_str = badword;
1527 if (p_verbose == 0)
1528 ++emsg_off;
1529
1530 if (eval1(&p, &rettv, TRUE) == OK)
1531 {
1532 if (rettv.v_type != VAR_LIST)
1533 clear_tv(&rettv);
1534 else
1535 list = rettv.vval.v_list;
1536 }
1537
1538 if (p_verbose == 0)
1539 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001540 restore_vimvar(VV_VAL, &save_val);
1541
1542 return list;
1543}
1544
1545/*
1546 * "list" is supposed to contain two items: a word and a number. Return the
1547 * word in "pp" and the number as the return value.
1548 * Return -1 if anything isn't right.
1549 * Used to get the good word and score from the eval_spell_expr() result.
1550 */
1551 int
1552get_spellword(list, pp)
1553 list_T *list;
1554 char_u **pp;
1555{
1556 listitem_T *li;
1557
1558 li = list->lv_first;
1559 if (li == NULL)
1560 return -1;
1561 *pp = get_tv_string(&li->li_tv);
1562
1563 li = li->li_next;
1564 if (li == NULL)
1565 return -1;
1566 return get_tv_number(&li->li_tv);
1567}
1568#endif
1569
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001570/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001571 * Top level evaluation function.
1572 * Returns an allocated typval_T with the result.
1573 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001574 */
1575 typval_T *
1576eval_expr(arg, nextcmd)
1577 char_u *arg;
1578 char_u **nextcmd;
1579{
1580 typval_T *tv;
1581
1582 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001583 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001584 {
1585 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001586 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001587 }
1588
1589 return tv;
1590}
1591
1592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001594 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001595 * Uses argv[argc] for the function arguments. Only Number and String
1596 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001597 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001599 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001600call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 char_u *func;
1602 int argc;
1603 char_u **argv;
1604 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001605 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
Bram Moolenaar33570922005-01-25 22:26:29 +00001608 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 long n;
1610 int len;
1611 int i;
1612 int doesrange;
1613 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001616 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
1620 for (i = 0; i < argc; i++)
1621 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001622 /* Pass a NULL or empty argument as an empty string */
1623 if (argv[i] == NULL || *argv[i] == NUL)
1624 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001625 argvars[i].v_type = VAR_STRING;
1626 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001627 continue;
1628 }
1629
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001630 if (str_arg_only)
1631 len = 0;
1632 else
1633 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001634 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 if (len != 0 && len == (int)STRLEN(argv[i]))
1636 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001637 argvars[i].v_type = VAR_NUMBER;
1638 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 }
1640 else
1641 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001642 argvars[i].v_type = VAR_STRING;
1643 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 }
1645 }
1646
1647 if (safe)
1648 {
1649 save_funccalp = save_funccal();
1650 ++sandbox;
1651 }
1652
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1654 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001656 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (safe)
1658 {
1659 --sandbox;
1660 restore_funccal(save_funccalp);
1661 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001662 vim_free(argvars);
1663
1664 if (ret == FAIL)
1665 clear_tv(rettv);
1666
1667 return ret;
1668}
1669
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001670/*
1671 * Call vimL function "func" and return the result as a number.
1672 * Returns -1 when calling the function fails.
1673 * Uses argv[argc] for the function arguments.
1674 */
1675 long
1676call_func_retnr(func, argc, argv, safe)
1677 char_u *func;
1678 int argc;
1679 char_u **argv;
1680 int safe; /* use the sandbox */
1681{
1682 typval_T rettv;
1683 long retval;
1684
1685 /* All arguments are passed as strings, no conversion to number. */
1686 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1687 return -1;
1688
1689 retval = get_tv_number_chk(&rettv, NULL);
1690 clear_tv(&rettv);
1691 return retval;
1692}
1693
1694#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1695 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1696
Bram Moolenaar4f688582007-07-24 12:34:30 +00001697# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001699 * Call vimL function "func" and return the result as a string.
1700 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 * Uses argv[argc] for the function arguments.
1702 */
1703 void *
1704call_func_retstr(func, argc, argv, safe)
1705 char_u *func;
1706 int argc;
1707 char_u **argv;
1708 int safe; /* use the sandbox */
1709{
1710 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001711 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001713 /* All arguments are passed as strings, no conversion to number. */
1714 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001715 return NULL;
1716
1717 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 return retval;
1720}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001721# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001723/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001724 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001726 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727 */
1728 void *
1729call_func_retlist(func, argc, argv, safe)
1730 char_u *func;
1731 int argc;
1732 char_u **argv;
1733 int safe; /* use the sandbox */
1734{
1735 typval_T rettv;
1736
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001737 /* All arguments are passed as strings, no conversion to number. */
1738 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739 return NULL;
1740
1741 if (rettv.v_type != VAR_LIST)
1742 {
1743 clear_tv(&rettv);
1744 return NULL;
1745 }
1746
1747 return rettv.vval.v_list;
1748}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749#endif
1750
1751/*
1752 * Save the current function call pointer, and set it to NULL.
1753 * Used when executing autocommands and for ":source".
1754 */
1755 void *
1756save_funccal()
1757{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001758 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 current_funccal = NULL;
1761 return (void *)fc;
1762}
1763
1764 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001765restore_funccal(vfc)
1766 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001768 funccall_T *fc = (funccall_T *)vfc;
1769
1770 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771}
1772
Bram Moolenaar05159a02005-02-26 23:04:13 +00001773#if defined(FEAT_PROFILE) || defined(PROTO)
1774/*
1775 * Prepare profiling for entering a child or something else that is not
1776 * counted for the script/function itself.
1777 * Should always be called in pair with prof_child_exit().
1778 */
1779 void
1780prof_child_enter(tm)
1781 proftime_T *tm; /* place to store waittime */
1782{
1783 funccall_T *fc = current_funccal;
1784
1785 if (fc != NULL && fc->func->uf_profiling)
1786 profile_start(&fc->prof_child);
1787 script_prof_save(tm);
1788}
1789
1790/*
1791 * Take care of time spent in a child.
1792 * Should always be called after prof_child_enter().
1793 */
1794 void
1795prof_child_exit(tm)
1796 proftime_T *tm; /* where waittime was stored */
1797{
1798 funccall_T *fc = current_funccal;
1799
1800 if (fc != NULL && fc->func->uf_profiling)
1801 {
1802 profile_end(&fc->prof_child);
1803 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1804 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1805 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1806 }
1807 script_prof_restore(tm);
1808}
1809#endif
1810
1811
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812#ifdef FEAT_FOLDING
1813/*
1814 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1815 * it in "*cp". Doesn't give error messages.
1816 */
1817 int
1818eval_foldexpr(arg, cp)
1819 char_u *arg;
1820 int *cp;
1821{
Bram Moolenaar33570922005-01-25 22:26:29 +00001822 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 int retval;
1824 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001825 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1826 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
1828 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001829 if (use_sandbox)
1830 ++sandbox;
1831 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 retval = 0;
1835 else
1836 {
1837 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838 if (tv.v_type == VAR_NUMBER)
1839 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001840 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 retval = 0;
1842 else
1843 {
1844 /* If the result is a string, check if there is a non-digit before
1845 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 if (!VIM_ISDIGIT(*s) && *s != '-')
1848 *cp = *s++;
1849 retval = atol((char *)s);
1850 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 }
1853 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001854 if (use_sandbox)
1855 --sandbox;
1856 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
1858 return retval;
1859}
1860#endif
1861
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 * ":let" list all variable values
1864 * ":let var1 var2" list variable values
1865 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001866 * ":let var += expr" assignment command.
1867 * ":let var -= expr" assignment command.
1868 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 */
1871 void
1872ex_let(eap)
1873 exarg_T *eap;
1874{
1875 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001877 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879 int var_count = 0;
1880 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001881 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001882 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001883 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884
Bram Moolenaardb552d602006-03-23 22:59:57 +00001885 argend = skip_var_list(arg, &var_count, &semicolon);
1886 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001888 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1889 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001890 expr = skipwhite(argend);
1891 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1892 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001894 /*
1895 * ":let" without "=": list variables
1896 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 if (*arg == '[')
1898 EMSG(_(e_invarg));
1899 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001901 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001905 list_glob_vars(&first);
1906 list_buf_vars(&first);
1907 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001908#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001909 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001910#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001911 list_script_vars(&first);
1912 list_func_vars(&first);
1913 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 eap->nextcmd = check_nextcmd(arg);
1916 }
1917 else
1918 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001919 op[0] = '=';
1920 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001921 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001923 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1924 op[0] = *expr; /* +=, -= or .= */
1925 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001926 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001927 else
1928 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (eap->skip)
1931 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 if (eap->skip)
1934 {
1935 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 --emsg_skip;
1938 }
1939 else if (i != FAIL)
1940 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001942 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 }
1945 }
1946}
1947
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948/*
1949 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1950 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001951 * When "nextchars" is not NULL it points to a string with characters that
1952 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1953 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 * Returns OK or FAIL;
1955 */
1956 static int
1957ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1958 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001959 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 int copy; /* copy values from "tv", don't move */
1961 int semicolon; /* from skip_var_list() */
1962 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964{
1965 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001966 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001968 listitem_T *item;
1969 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970
1971 if (*arg != '[')
1972 {
1973 /*
1974 * ":let var = expr" or ":for var in list"
1975 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 return FAIL;
1978 return OK;
1979 }
1980
1981 /*
1982 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1983 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001984 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 {
1986 EMSG(_(e_listreq));
1987 return FAIL;
1988 }
1989
1990 i = list_len(l);
1991 if (semicolon == 0 && var_count < i)
1992 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001993 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 return FAIL;
1995 }
1996 if (var_count - semicolon > i)
1997 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001998 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 return FAIL;
2000 }
2001
2002 item = l->lv_first;
2003 while (*arg != ']')
2004 {
2005 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002006 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007 item = item->li_next;
2008 if (arg == NULL)
2009 return FAIL;
2010
2011 arg = skipwhite(arg);
2012 if (*arg == ';')
2013 {
2014 /* Put the rest of the list (may be empty) in the var after ';'.
2015 * Create a new list for this. */
2016 l = list_alloc();
2017 if (l == NULL)
2018 return FAIL;
2019 while (item != NULL)
2020 {
2021 list_append_tv(l, &item->li_tv);
2022 item = item->li_next;
2023 }
2024
2025 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002026 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002027 ltv.vval.v_list = l;
2028 l->lv_refcount = 1;
2029
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002030 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2031 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 clear_tv(&ltv);
2033 if (arg == NULL)
2034 return FAIL;
2035 break;
2036 }
2037 else if (*arg != ',' && *arg != ']')
2038 {
2039 EMSG2(_(e_intern2), "ex_let_vars()");
2040 return FAIL;
2041 }
2042 }
2043
2044 return OK;
2045}
2046
2047/*
2048 * Skip over assignable variable "var" or list of variables "[var, var]".
2049 * Used for ":let varvar = expr" and ":for varvar in expr".
2050 * For "[var, var]" increment "*var_count" for each variable.
2051 * for "[var, var; var]" set "semicolon".
2052 * Return NULL for an error.
2053 */
2054 static char_u *
2055skip_var_list(arg, var_count, semicolon)
2056 char_u *arg;
2057 int *var_count;
2058 int *semicolon;
2059{
2060 char_u *p, *s;
2061
2062 if (*arg == '[')
2063 {
2064 /* "[var, var]": find the matching ']'. */
2065 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002066 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002067 {
2068 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2069 s = skip_var_one(p);
2070 if (s == p)
2071 {
2072 EMSG2(_(e_invarg2), p);
2073 return NULL;
2074 }
2075 ++*var_count;
2076
2077 p = skipwhite(s);
2078 if (*p == ']')
2079 break;
2080 else if (*p == ';')
2081 {
2082 if (*semicolon == 1)
2083 {
2084 EMSG(_("Double ; in list of variables"));
2085 return NULL;
2086 }
2087 *semicolon = 1;
2088 }
2089 else if (*p != ',')
2090 {
2091 EMSG2(_(e_invarg2), p);
2092 return NULL;
2093 }
2094 }
2095 return p + 1;
2096 }
2097 else
2098 return skip_var_one(arg);
2099}
2100
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002101/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002102 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002103 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002104 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002105 static char_u *
2106skip_var_one(arg)
2107 char_u *arg;
2108{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002109 if (*arg == '@' && arg[1] != NUL)
2110 return arg + 2;
2111 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2112 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002113}
2114
Bram Moolenaara7043832005-01-21 11:56:39 +00002115/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002116 * List variables for hashtab "ht" with prefix "prefix".
2117 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002118 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002119 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002121 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002123 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002124 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125{
Bram Moolenaar33570922005-01-25 22:26:29 +00002126 hashitem_T *hi;
2127 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002128 int todo;
2129
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002130 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2132 {
2133 if (!HASHITEM_EMPTY(hi))
2134 {
2135 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002136 di = HI2DI(hi);
2137 if (empty || di->di_tv.v_type != VAR_STRING
2138 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002140 }
2141 }
2142}
2143
2144/*
2145 * List global variables.
2146 */
2147 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148list_glob_vars(first)
2149 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002150{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002152}
2153
2154/*
2155 * List buffer variables.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_buf_vars(first)
2159 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002160{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002161 char_u numbuf[NUMBUFLEN];
2162
Bram Moolenaar429fa852013-04-15 12:27:36 +02002163 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002165
2166 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2168 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002169}
2170
2171/*
2172 * List window variables.
2173 */
2174 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175list_win_vars(first)
2176 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002177{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002178 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002180}
2181
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002182#ifdef FEAT_WINDOWS
2183/*
2184 * List tab page variables.
2185 */
2186 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187list_tab_vars(first)
2188 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002189{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002190 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002192}
2193#endif
2194
Bram Moolenaara7043832005-01-21 11:56:39 +00002195/*
2196 * List Vim variables.
2197 */
2198 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002199list_vim_vars(first)
2200 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203}
2204
2205/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002206 * List script-local variables, if there is a script.
2207 */
2208 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002209list_script_vars(first)
2210 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002211{
2212 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002213 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2214 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002215}
2216
2217/*
2218 * List function variables, if there is a function.
2219 */
2220 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002221list_func_vars(first)
2222 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002223{
2224 if (current_funccal != NULL)
2225 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002226 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002227}
2228
2229/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 * List variables in "arg".
2231 */
2232 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002233list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 exarg_T *eap;
2235 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002236 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237{
2238 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 char_u *name_start;
2242 char_u *arg_subsc;
2243 char_u *tofree;
2244 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245
2246 while (!ends_excmd(*arg) && !got_int)
2247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002250 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2252 {
2253 emsg_severe = TRUE;
2254 EMSG(_(e_trailing));
2255 break;
2256 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 /* get_name_len() takes care of expanding curly braces */
2261 name_start = name = arg;
2262 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2263 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 /* This is mainly to keep test 49 working: when expanding
2266 * curly braces fails overrule the exception error message. */
2267 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 emsg_severe = TRUE;
2270 EMSG2(_(e_invarg2), arg);
2271 break;
2272 }
2273 error = TRUE;
2274 }
2275 else
2276 {
2277 if (tofree != NULL)
2278 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002279 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 else
2282 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 /* handle d.key, l[idx], f(expr) */
2284 arg_subsc = arg;
2285 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002288 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002292 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002293 case 'g': list_glob_vars(first); break;
2294 case 'b': list_buf_vars(first); break;
2295 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002296#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002297 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002298#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 case 'v': list_vim_vars(first); break;
2300 case 's': list_script_vars(first); break;
2301 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 default:
2303 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002304 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002305 }
2306 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 {
2308 char_u numbuf[NUMBUFLEN];
2309 char_u *tf;
2310 int c;
2311 char_u *s;
2312
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002313 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314 c = *arg;
2315 *arg = NUL;
2316 list_one_var_a((char_u *)"",
2317 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002318 tv.v_type,
2319 s == NULL ? (char_u *)"" : s,
2320 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321 *arg = c;
2322 vim_free(tf);
2323 }
2324 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002325 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 }
2327 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328
2329 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002331
2332 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 }
2334
2335 return arg;
2336}
2337
2338/*
2339 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2340 * Returns a pointer to the char just after the var name.
2341 * Returns NULL if there is an error.
2342 */
2343 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002344ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002346 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002348 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350{
2351 int c1;
2352 char_u *name;
2353 char_u *p;
2354 char_u *arg_end = NULL;
2355 int len;
2356 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002357 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358
2359 /*
2360 * ":let $VAR = expr": Set environment variable.
2361 */
2362 if (*arg == '$')
2363 {
2364 /* Find the end of the name. */
2365 ++arg;
2366 name = arg;
2367 len = get_env_len(&arg);
2368 if (len == 0)
2369 EMSG2(_(e_invarg2), name - 1);
2370 else
2371 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 if (op != NULL && (*op == '+' || *op == '-'))
2373 EMSG2(_(e_letwrong), op);
2374 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002375 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002377 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 {
2379 c1 = name[len];
2380 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002381 p = get_tv_string_chk(tv);
2382 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 {
2384 int mustfree = FALSE;
2385 char_u *s = vim_getenv(name, &mustfree);
2386
2387 if (s != NULL)
2388 {
2389 p = tofree = concat_str(s, p);
2390 if (mustfree)
2391 vim_free(s);
2392 }
2393 }
2394 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002397 if (STRICMP(name, "HOME") == 0)
2398 init_homedir();
2399 else if (didset_vim && STRICMP(name, "VIM") == 0)
2400 didset_vim = FALSE;
2401 else if (didset_vimruntime
2402 && STRICMP(name, "VIMRUNTIME") == 0)
2403 didset_vimruntime = FALSE;
2404 arg_end = arg;
2405 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002408 }
2409 }
2410 }
2411
2412 /*
2413 * ":let &option = expr": Set option value.
2414 * ":let &l:option = expr": Set local option value.
2415 * ":let &g:option = expr": Set global option value.
2416 */
2417 else if (*arg == '&')
2418 {
2419 /* Find the end of the name. */
2420 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002421 if (p == NULL || (endchars != NULL
2422 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 EMSG(_(e_letunexp));
2424 else
2425 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 long n;
2427 int opt_type;
2428 long numval;
2429 char_u *stringval = NULL;
2430 char_u *s;
2431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 c1 = *p;
2433 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434
2435 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002436 s = get_tv_string_chk(tv); /* != NULL if number or string */
2437 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 {
2439 opt_type = get_option_value(arg, &numval,
2440 &stringval, opt_flags);
2441 if ((opt_type == 1 && *op == '.')
2442 || (opt_type == 0 && *op != '.'))
2443 EMSG2(_(e_letwrong), op);
2444 else
2445 {
2446 if (opt_type == 1) /* number */
2447 {
2448 if (*op == '+')
2449 n = numval + n;
2450 else
2451 n = numval - n;
2452 }
2453 else if (opt_type == 0 && stringval != NULL) /* string */
2454 {
2455 s = concat_str(stringval, s);
2456 vim_free(stringval);
2457 stringval = s;
2458 }
2459 }
2460 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461 if (s != NULL)
2462 {
2463 set_option_value(arg, n, s, opt_flags);
2464 arg_end = p;
2465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002467 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 }
2469 }
2470
2471 /*
2472 * ":let @r = expr": Set register contents.
2473 */
2474 else if (*arg == '@')
2475 {
2476 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477 if (op != NULL && (*op == '+' || *op == '-'))
2478 EMSG2(_(e_letwrong), op);
2479 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002480 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 EMSG(_(e_letunexp));
2482 else
2483 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002484 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 char_u *s;
2486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002487 p = get_tv_string_chk(tv);
2488 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002490 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 if (s != NULL)
2492 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002493 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 vim_free(s);
2495 }
2496 }
2497 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002500 arg_end = arg + 1;
2501 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002502 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503 }
2504 }
2505
2506 /*
2507 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002510 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002512 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002514 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2518 EMSG(_(e_letunexp));
2519 else
2520 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002521 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 arg_end = p;
2523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 }
2527
2528 else
2529 EMSG2(_(e_invarg2), arg);
2530
2531 return arg_end;
2532}
2533
2534/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2536 */
2537 static int
2538check_changedtick(arg)
2539 char_u *arg;
2540{
2541 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2542 {
2543 EMSG2(_(e_readonlyvar), arg);
2544 return TRUE;
2545 }
2546 return FALSE;
2547}
2548
2549/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 * Get an lval: variable, Dict item or List item that can be assigned a value
2551 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2552 * "name.key", "name.key[expr]" etc.
2553 * Indexing only works if "name" is an existing List or Dictionary.
2554 * "name" points to the start of the name.
2555 * If "rettv" is not NULL it points to the value to be assigned.
2556 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2557 * wrong; must end in space or cmd separator.
2558 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002559 * flags:
2560 * GLV_QUIET: do not give error messages
2561 * GLV_NO_AUTOLOAD: do not use script autoloading
2562 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002564 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 */
2567 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002568get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002570 typval_T *rettv;
2571 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 int unlet;
2573 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002574 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002575 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 char_u *expr_start, *expr_end;
2579 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 dictitem_T *v;
2581 typval_T var1;
2582 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002588 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592
2593 if (skip)
2594 {
2595 /* When skipping just find the end of the name. */
2596 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002597 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 }
2599
2600 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002601 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (expr_start != NULL)
2603 {
2604 /* Don't expand the name when we already know there is an error. */
2605 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2606 && *p != '[' && *p != '.')
2607 {
2608 EMSG(_(e_trailing));
2609 return NULL;
2610 }
2611
2612 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2613 if (lp->ll_exp_name == NULL)
2614 {
2615 /* Report an invalid expression in braces, unless the
2616 * expression evaluation has been cancelled due to an
2617 * aborting error, an interrupt, or an exception. */
2618 if (!aborting() && !quiet)
2619 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002620 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 EMSG2(_(e_invarg2), name);
2622 return NULL;
2623 }
2624 }
2625 lp->ll_name = lp->ll_exp_name;
2626 }
2627 else
2628 lp->ll_name = name;
2629
2630 /* Without [idx] or .key we are done. */
2631 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2632 return p;
2633
2634 cc = *p;
2635 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002636 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (v == NULL && !quiet)
2638 EMSG2(_(e_undefvar), lp->ll_name);
2639 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002640 if (v == NULL)
2641 return NULL;
2642
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 /*
2644 * Loop until no more [idx] or .key is following.
2645 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002646 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2650 && !(lp->ll_tv->v_type == VAR_DICT
2651 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_("E689: Can only index a List or Dictionary"));
2655 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
2660 EMSG(_("E708: [:] must come last"));
2661 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002663
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 len = -1;
2665 if (*p == '.')
2666 {
2667 key = p + 1;
2668 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2669 ;
2670 if (len == 0)
2671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 if (!quiet)
2673 EMSG(_(e_emptykey));
2674 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
2676 p = key + len;
2677 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 else
2679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 if (*p == ':')
2683 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 else
2685 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 empty1 = FALSE;
2687 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002689 if (get_tv_string_chk(&var1) == NULL)
2690 {
2691 /* not a number or string */
2692 clear_tv(&var1);
2693 return NULL;
2694 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 }
2696
2697 /* Optionally get the second index [ :expr]. */
2698 if (*p == ':')
2699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002703 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 if (!empty1)
2705 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (rettv != NULL && (rettv->v_type != VAR_LIST
2709 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (!quiet)
2712 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 if (!empty1)
2714 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 }
2717 p = skipwhite(p + 1);
2718 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 else
2721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 if (!empty1)
2726 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002729 if (get_tv_string_chk(&var2) == NULL)
2730 {
2731 /* not a number or string */
2732 if (!empty1)
2733 clear_tv(&var1);
2734 clear_tv(&var2);
2735 return NULL;
2736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002742
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (!quiet)
2746 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 if (!empty1)
2748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 }
2753
2754 /* Skip to past ']'. */
2755 ++p;
2756 }
2757
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 {
2760 if (len == -1)
2761 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002763 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 if (*key == NUL)
2765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (!quiet)
2767 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
2771 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 lp->ll_list = NULL;
2773 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002774 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002775
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002776 /* When assigning to a scope dictionary check that a function and
2777 * variable name is valid (only variable name unless it is l: or
2778 * g: dictionary). Disallow overwriting a builtin function. */
2779 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002780 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002781 int prevval;
2782 int wrong;
2783
2784 if (len != -1)
2785 {
2786 prevval = key[len];
2787 key[len] = NUL;
2788 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002789 else
2790 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002791 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2792 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002794 || !valid_varname(key);
2795 if (len != -1)
2796 key[len] = prevval;
2797 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002798 return NULL;
2799 }
2800
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 /* Can't add "v:" variable. */
2804 if (lp->ll_dict == &vimvardict)
2805 {
2806 EMSG2(_(e_illvar), name);
2807 return NULL;
2808 }
2809
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002810 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 if (len == -1)
2816 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 }
2819 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 if (len == -1)
2824 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 p = NULL;
2827 break;
2828 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002829 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002830 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002831 return NULL;
2832
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 if (len == -1)
2834 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 }
2837 else
2838 {
2839 /*
2840 * Get the number and item for the only or first index of the List.
2841 */
2842 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 else
2845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002846 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 clear_tv(&var1);
2848 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002849 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 lp->ll_list = lp->ll_tv->vval.v_list;
2851 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2852 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002854 if (lp->ll_n1 < 0)
2855 {
2856 lp->ll_n1 = 0;
2857 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2858 }
2859 }
2860 if (lp->ll_li == NULL)
2861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 if (!quiet)
2865 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 }
2868
2869 /*
2870 * May need to find the item or absolute index for the second
2871 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 * When no index given: "lp->ll_empty2" is TRUE.
2873 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002877 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002883 {
2884 if (!quiet)
2885 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002887 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 }
2890
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2892 if (lp->ll_n1 < 0)
2893 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2894 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002895 {
2896 if (!quiet)
2897 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002899 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002900 }
2901
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002904 }
2905
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return p;
2907}
2908
2909/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 */
2912 static void
2913clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002914 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915{
2916 vim_free(lp->ll_exp_name);
2917 vim_free(lp->ll_newkey);
2918}
2919
2920/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002921 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002923 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 */
2925 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002927 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002929 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002931 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932{
2933 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002934 listitem_T *ri;
2935 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936
2937 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002938 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 cc = *endp;
2942 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002943 if (op != NULL && *op != '=')
2944 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002945 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002947 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002948 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002949 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002950 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002952 if ((di == NULL
2953 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2954 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2955 FALSE)))
2956 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002957 set_var(lp->ll_name, &tv, FALSE);
2958 clear_tv(&tv);
2959 }
2960 }
2961 else
2962 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002964 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002966 else if (tv_check_lock(lp->ll_newkey == NULL
2967 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002968 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002969 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 else if (lp->ll_range)
2971 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002972 listitem_T *ll_li = lp->ll_li;
2973 int ll_n1 = lp->ll_n1;
2974
2975 /*
2976 * Check whether any of the list items is locked
2977 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002978 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002979 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002980 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002981 return;
2982 ri = ri->li_next;
2983 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2984 break;
2985 ll_li = ll_li->li_next;
2986 ++ll_n1;
2987 }
2988
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 /*
2990 * Assign the List values to the list items.
2991 */
2992 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002993 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002994 if (op != NULL && *op != '=')
2995 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2996 else
2997 {
2998 clear_tv(&lp->ll_li->li_tv);
2999 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3000 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003001 ri = ri->li_next;
3002 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3003 break;
3004 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003007 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 ri = NULL;
3010 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003011 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003012 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 lp->ll_li = lp->ll_li->li_next;
3014 ++lp->ll_n1;
3015 }
3016 if (ri != NULL)
3017 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003018 else if (lp->ll_empty2
3019 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 : lp->ll_n1 != lp->ll_n2)
3021 EMSG(_("E711: List value has not enough items"));
3022 }
3023 else
3024 {
3025 /*
3026 * Assign to a List or Dictionary item.
3027 */
3028 if (lp->ll_newkey != NULL)
3029 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003030 if (op != NULL && *op != '=')
3031 {
3032 EMSG2(_(e_letwrong), op);
3033 return;
3034 }
3035
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003036 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003037 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003038 if (di == NULL)
3039 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003040 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3041 {
3042 vim_free(di);
3043 return;
3044 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003046 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003047 else if (op != NULL && *op != '=')
3048 {
3049 tv_op(lp->ll_tv, rettv, op);
3050 return;
3051 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003052 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003053 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003054
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003055 /*
3056 * Assign the value to the variable or list item.
3057 */
3058 if (copy)
3059 copy_tv(rettv, lp->ll_tv);
3060 else
3061 {
3062 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003063 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003065 }
3066 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003067}
3068
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003069/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3071 * Returns OK or FAIL.
3072 */
3073 static int
3074tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003075 typval_T *tv1;
3076 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003077 char_u *op;
3078{
3079 long n;
3080 char_u numbuf[NUMBUFLEN];
3081 char_u *s;
3082
3083 /* Can't do anything with a Funcref or a Dict on the right. */
3084 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3085 {
3086 switch (tv1->v_type)
3087 {
3088 case VAR_DICT:
3089 case VAR_FUNC:
3090 break;
3091
3092 case VAR_LIST:
3093 if (*op != '+' || tv2->v_type != VAR_LIST)
3094 break;
3095 /* List += List */
3096 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3097 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3098 return OK;
3099
3100 case VAR_NUMBER:
3101 case VAR_STRING:
3102 if (tv2->v_type == VAR_LIST)
3103 break;
3104 if (*op == '+' || *op == '-')
3105 {
3106 /* nr += nr or nr -= nr*/
3107 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003108#ifdef FEAT_FLOAT
3109 if (tv2->v_type == VAR_FLOAT)
3110 {
3111 float_T f = n;
3112
3113 if (*op == '+')
3114 f += tv2->vval.v_float;
3115 else
3116 f -= tv2->vval.v_float;
3117 clear_tv(tv1);
3118 tv1->v_type = VAR_FLOAT;
3119 tv1->vval.v_float = f;
3120 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003121 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122#endif
3123 {
3124 if (*op == '+')
3125 n += get_tv_number(tv2);
3126 else
3127 n -= get_tv_number(tv2);
3128 clear_tv(tv1);
3129 tv1->v_type = VAR_NUMBER;
3130 tv1->vval.v_number = n;
3131 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003132 }
3133 else
3134 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135 if (tv2->v_type == VAR_FLOAT)
3136 break;
3137
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003138 /* str .= str */
3139 s = get_tv_string(tv1);
3140 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3141 clear_tv(tv1);
3142 tv1->v_type = VAR_STRING;
3143 tv1->vval.v_string = s;
3144 }
3145 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003146
3147#ifdef FEAT_FLOAT
3148 case VAR_FLOAT:
3149 {
3150 float_T f;
3151
3152 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3153 && tv2->v_type != VAR_NUMBER
3154 && tv2->v_type != VAR_STRING))
3155 break;
3156 if (tv2->v_type == VAR_FLOAT)
3157 f = tv2->vval.v_float;
3158 else
3159 f = get_tv_number(tv2);
3160 if (*op == '+')
3161 tv1->vval.v_float += f;
3162 else
3163 tv1->vval.v_float -= f;
3164 }
3165 return OK;
3166#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003167 }
3168 }
3169
3170 EMSG2(_(e_letwrong), op);
3171 return FAIL;
3172}
3173
3174/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175 * Add a watcher to a list.
3176 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003177 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003179 list_T *l;
3180 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181{
3182 lw->lw_next = l->lv_watch;
3183 l->lv_watch = lw;
3184}
3185
3186/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003187 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 * No warning when it isn't found...
3189 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003190 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 list_T *l;
3193 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194{
Bram Moolenaar33570922005-01-25 22:26:29 +00003195 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003196
3197 lwp = &l->lv_watch;
3198 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3199 {
3200 if (lw == lwrem)
3201 {
3202 *lwp = lw->lw_next;
3203 break;
3204 }
3205 lwp = &lw->lw_next;
3206 }
3207}
3208
3209/*
3210 * Just before removing an item from a list: advance watchers to the next
3211 * item.
3212 */
3213 static void
3214list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 list_T *l;
3216 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217{
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219
3220 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3221 if (lw->lw_item == item)
3222 lw->lw_item = item->li_next;
3223}
3224
3225/*
3226 * Evaluate the expression used in a ":for var in expr" command.
3227 * "arg" points to "var".
3228 * Set "*errp" to TRUE for an error, FALSE otherwise;
3229 * Return a pointer that holds the info. Null when there is an error.
3230 */
3231 void *
3232eval_for_line(arg, errp, nextcmdp, skip)
3233 char_u *arg;
3234 int *errp;
3235 char_u **nextcmdp;
3236 int skip;
3237{
Bram Moolenaar33570922005-01-25 22:26:29 +00003238 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 typval_T tv;
3241 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242
3243 *errp = TRUE; /* default: there is an error */
3244
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 if (fi == NULL)
3247 return NULL;
3248
3249 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3250 if (expr == NULL)
3251 return fi;
3252
3253 expr = skipwhite(expr);
3254 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3255 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003256 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 return fi;
3258 }
3259
3260 if (skip)
3261 ++emsg_skip;
3262 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3263 {
3264 *errp = FALSE;
3265 if (!skip)
3266 {
3267 l = tv.vval.v_list;
3268 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003269 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 clear_tv(&tv);
3272 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 else
3274 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003275 /* No need to increment the refcount, it's already set for the
3276 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 fi->fi_list = l;
3278 list_add_watch(l, &fi->fi_lw);
3279 fi->fi_lw.lw_item = l->lv_first;
3280 }
3281 }
3282 }
3283 if (skip)
3284 --emsg_skip;
3285
3286 return fi;
3287}
3288
3289/*
3290 * Use the first item in a ":for" list. Advance to the next.
3291 * Assign the values to the variable (list). "arg" points to the first one.
3292 * Return TRUE when a valid item was found, FALSE when at end of list or
3293 * something wrong.
3294 */
3295 int
3296next_for_item(fi_void, arg)
3297 void *fi_void;
3298 char_u *arg;
3299{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003300 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003302 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303
3304 item = fi->fi_lw.lw_item;
3305 if (item == NULL)
3306 result = FALSE;
3307 else
3308 {
3309 fi->fi_lw.lw_item = item->li_next;
3310 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3311 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3312 }
3313 return result;
3314}
3315
3316/*
3317 * Free the structure used to store info used by ":for".
3318 */
3319 void
3320free_for_info(fi_void)
3321 void *fi_void;
3322{
Bram Moolenaar33570922005-01-25 22:26:29 +00003323 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003325 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003326 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003328 list_unref(fi->fi_list);
3329 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 vim_free(fi);
3331}
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3334
3335 void
3336set_context_for_expression(xp, arg, cmdidx)
3337 expand_T *xp;
3338 char_u *arg;
3339 cmdidx_T cmdidx;
3340{
3341 int got_eq = FALSE;
3342 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 if (cmdidx == CMD_let)
3346 {
3347 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003348 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 {
3350 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003351 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 {
3353 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003354 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003355 if (vim_iswhite(*p))
3356 break;
3357 }
3358 return;
3359 }
3360 }
3361 else
3362 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3363 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 while ((xp->xp_pattern = vim_strpbrk(arg,
3365 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3366 {
3367 c = *xp->xp_pattern;
3368 if (c == '&')
3369 {
3370 c = xp->xp_pattern[1];
3371 if (c == '&')
3372 {
3373 ++xp->xp_pattern;
3374 xp->xp_context = cmdidx != CMD_let || got_eq
3375 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3376 }
3377 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003380 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3381 xp->xp_pattern += 2;
3382
3383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 else if (c == '$')
3386 {
3387 /* environment variable */
3388 xp->xp_context = EXPAND_ENV_VARS;
3389 }
3390 else if (c == '=')
3391 {
3392 got_eq = TRUE;
3393 xp->xp_context = EXPAND_EXPRESSION;
3394 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003395 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 && xp->xp_context == EXPAND_FUNCTIONS
3397 && vim_strchr(xp->xp_pattern, '(') == NULL)
3398 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003399 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 break;
3401 }
3402 else if (cmdidx != CMD_let || got_eq)
3403 {
3404 if (c == '"') /* string */
3405 {
3406 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3407 if (c == '\\' && xp->xp_pattern[1] != NUL)
3408 ++xp->xp_pattern;
3409 xp->xp_context = EXPAND_NOTHING;
3410 }
3411 else if (c == '\'') /* literal string */
3412 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003413 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3415 /* skip */ ;
3416 xp->xp_context = EXPAND_NOTHING;
3417 }
3418 else if (c == '|')
3419 {
3420 if (xp->xp_pattern[1] == '|')
3421 {
3422 ++xp->xp_pattern;
3423 xp->xp_context = EXPAND_EXPRESSION;
3424 }
3425 else
3426 xp->xp_context = EXPAND_COMMANDS;
3427 }
3428 else
3429 xp->xp_context = EXPAND_EXPRESSION;
3430 }
3431 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003432 /* Doesn't look like something valid, expand as an expression
3433 * anyway. */
3434 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 arg = xp->xp_pattern;
3436 if (*arg != NUL)
3437 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3438 /* skip */ ;
3439 }
3440 xp->xp_pattern = arg;
3441}
3442
3443#endif /* FEAT_CMDL_COMPL */
3444
3445/*
3446 * ":1,25call func(arg1, arg2)" function call.
3447 */
3448 void
3449ex_call(eap)
3450 exarg_T *eap;
3451{
3452 char_u *arg = eap->arg;
3453 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003455 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003457 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 linenr_T lnum;
3459 int doesrange;
3460 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003461 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003463 if (eap->skip)
3464 {
3465 /* trans_function_name() doesn't work well when skipping, use eval0()
3466 * instead to skip to any following command, e.g. for:
3467 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003468 ++emsg_skip;
3469 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3470 clear_tv(&rettv);
3471 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003472 return;
3473 }
3474
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003475 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003476 if (fudi.fd_newkey != NULL)
3477 {
3478 /* Still need to give an error message for missing key. */
3479 EMSG2(_(e_dictkey), fudi.fd_newkey);
3480 vim_free(fudi.fd_newkey);
3481 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003482 if (tofree == NULL)
3483 return;
3484
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003485 /* Increase refcount on dictionary, it could get deleted when evaluating
3486 * the arguments. */
3487 if (fudi.fd_dict != NULL)
3488 ++fudi.fd_dict->dv_refcount;
3489
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003490 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003491 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003492 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
Bram Moolenaar532c7802005-01-27 14:44:31 +00003494 /* Skip white space to allow ":call func ()". Not good, but required for
3495 * backward compatibility. */
3496 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003497 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
3499 if (*startarg != '(')
3500 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003501 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 goto end;
3503 }
3504
3505 /*
3506 * When skipping, evaluate the function once, to find the end of the
3507 * arguments.
3508 * When the function takes a range, this is discovered after the first
3509 * call, and the loop is broken.
3510 */
3511 if (eap->skip)
3512 {
3513 ++emsg_skip;
3514 lnum = eap->line2; /* do it once, also with an invalid range */
3515 }
3516 else
3517 lnum = eap->line1;
3518 for ( ; lnum <= eap->line2; ++lnum)
3519 {
3520 if (!eap->skip && eap->addr_count > 0)
3521 {
3522 curwin->w_cursor.lnum = lnum;
3523 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003524#ifdef FEAT_VIRTUALEDIT
3525 curwin->w_cursor.coladd = 0;
3526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003529 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003530 eap->line1, eap->line2, &doesrange,
3531 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
3533 failed = TRUE;
3534 break;
3535 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003536
3537 /* Handle a function returning a Funcref, Dictionary or List. */
3538 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3539 {
3540 failed = TRUE;
3541 break;
3542 }
3543
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (doesrange || eap->skip)
3546 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003547
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003549 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003550 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003551 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 if (aborting())
3553 break;
3554 }
3555 if (eap->skip)
3556 --emsg_skip;
3557
3558 if (!failed)
3559 {
3560 /* Check for trailing illegal characters and a following command. */
3561 if (!ends_excmd(*arg))
3562 {
3563 emsg_severe = TRUE;
3564 EMSG(_(e_trailing));
3565 }
3566 else
3567 eap->nextcmd = check_nextcmd(arg);
3568 }
3569
3570end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003571 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003572 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573}
3574
3575/*
3576 * ":unlet[!] var1 ... " command.
3577 */
3578 void
3579ex_unlet(eap)
3580 exarg_T *eap;
3581{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582 ex_unletlock(eap, eap->arg, 0);
3583}
3584
3585/*
3586 * ":lockvar" and ":unlockvar" commands
3587 */
3588 void
3589ex_lockvar(eap)
3590 exarg_T *eap;
3591{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593 int deep = 2;
3594
3595 if (eap->forceit)
3596 deep = -1;
3597 else if (vim_isdigit(*arg))
3598 {
3599 deep = getdigits(&arg);
3600 arg = skipwhite(arg);
3601 }
3602
3603 ex_unletlock(eap, arg, deep);
3604}
3605
3606/*
3607 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3608 */
3609 static void
3610ex_unletlock(eap, argstart, deep)
3611 exarg_T *eap;
3612 char_u *argstart;
3613 int deep;
3614{
3615 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003618 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619
3620 do
3621 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003623 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003624 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 if (lv.ll_name == NULL)
3626 error = TRUE; /* error but continue parsing */
3627 if (name_end == NULL || (!vim_iswhite(*name_end)
3628 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003630 if (name_end != NULL)
3631 {
3632 emsg_severe = TRUE;
3633 EMSG(_(e_trailing));
3634 }
3635 if (!(eap->skip || error))
3636 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 break;
3638 }
3639
3640 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003641 {
3642 if (eap->cmdidx == CMD_unlet)
3643 {
3644 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3645 error = TRUE;
3646 }
3647 else
3648 {
3649 if (do_lock_var(&lv, name_end, deep,
3650 eap->cmdidx == CMD_lockvar) == FAIL)
3651 error = TRUE;
3652 }
3653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003655 if (!eap->skip)
3656 clear_lval(&lv);
3657
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 arg = skipwhite(name_end);
3659 } while (!ends_excmd(*arg));
3660
3661 eap->nextcmd = check_nextcmd(arg);
3662}
3663
Bram Moolenaar8c711452005-01-14 21:53:12 +00003664 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003665do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003666 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003668 int forceit;
3669{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670 int ret = OK;
3671 int cc;
3672
3673 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 cc = *name_end;
3676 *name_end = NUL;
3677
3678 /* Normal name or expanded name. */
3679 if (check_changedtick(lp->ll_name))
3680 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003681 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003683 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003684 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003685 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003686 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003687 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003688 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003689 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 else if (lp->ll_range)
3691 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003692 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003693 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003694 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003695
3696 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3697 {
3698 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003699 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003700 return FAIL;
3701 ll_li = li;
3702 ++ll_n1;
3703 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003704
3705 /* Delete a range of List items. */
3706 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3707 {
3708 li = lp->ll_li->li_next;
3709 listitem_remove(lp->ll_list, lp->ll_li);
3710 lp->ll_li = li;
3711 ++lp->ll_n1;
3712 }
3713 }
3714 else
3715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 /* unlet a List item. */
3718 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003720 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003721 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003722 }
3723
3724 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003725}
3726
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727/*
3728 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003729 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 */
3731 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003732do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003734 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735{
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 hashtab_T *ht;
3737 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003738 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003739 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003740 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
Bram Moolenaar33570922005-01-25 22:26:29 +00003742 ht = find_var_ht(name, &varname);
3743 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003745 if (ht == &globvarht)
3746 d = &globvardict;
3747 else if (current_funccal != NULL
3748 && ht == &current_funccal->l_vars.dv_hashtab)
3749 d = &current_funccal->l_vars;
3750 else if (ht == &compat_hashtab)
3751 d = &vimvardict;
3752 else
3753 {
3754 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3755 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3756 }
3757 if (d == NULL)
3758 {
3759 EMSG2(_(e_intern2), "do_unlet()");
3760 return FAIL;
3761 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003762 hi = hash_find(ht, varname);
3763 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003764 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003765 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003766 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003767 || var_check_ro(di->di_flags, name, FALSE)
3768 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003769 return FAIL;
3770
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003771 delete_var(ht, hi);
3772 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775 if (forceit)
3776 return OK;
3777 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 return FAIL;
3779}
3780
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003781/*
3782 * Lock or unlock variable indicated by "lp".
3783 * "deep" is the levels to go (-1 for unlimited);
3784 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3785 */
3786 static int
3787do_lock_var(lp, name_end, deep, lock)
3788 lval_T *lp;
3789 char_u *name_end;
3790 int deep;
3791 int lock;
3792{
3793 int ret = OK;
3794 int cc;
3795 dictitem_T *di;
3796
3797 if (deep == 0) /* nothing to do */
3798 return OK;
3799
3800 if (lp->ll_tv == NULL)
3801 {
3802 cc = *name_end;
3803 *name_end = NUL;
3804
3805 /* Normal name or expanded name. */
3806 if (check_changedtick(lp->ll_name))
3807 ret = FAIL;
3808 else
3809 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003810 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003811 if (di == NULL)
3812 ret = FAIL;
3813 else
3814 {
3815 if (lock)
3816 di->di_flags |= DI_FLAGS_LOCK;
3817 else
3818 di->di_flags &= ~DI_FLAGS_LOCK;
3819 item_lock(&di->di_tv, deep, lock);
3820 }
3821 }
3822 *name_end = cc;
3823 }
3824 else if (lp->ll_range)
3825 {
3826 listitem_T *li = lp->ll_li;
3827
3828 /* (un)lock a range of List items. */
3829 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3830 {
3831 item_lock(&li->li_tv, deep, lock);
3832 li = li->li_next;
3833 ++lp->ll_n1;
3834 }
3835 }
3836 else if (lp->ll_list != NULL)
3837 /* (un)lock a List item. */
3838 item_lock(&lp->ll_li->li_tv, deep, lock);
3839 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003840 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003841 item_lock(&lp->ll_di->di_tv, deep, lock);
3842
3843 return ret;
3844}
3845
3846/*
3847 * Lock or unlock an item. "deep" is nr of levels to go.
3848 */
3849 static void
3850item_lock(tv, deep, lock)
3851 typval_T *tv;
3852 int deep;
3853 int lock;
3854{
3855 static int recurse = 0;
3856 list_T *l;
3857 listitem_T *li;
3858 dict_T *d;
3859 hashitem_T *hi;
3860 int todo;
3861
3862 if (recurse >= DICT_MAXNEST)
3863 {
3864 EMSG(_("E743: variable nested too deep for (un)lock"));
3865 return;
3866 }
3867 if (deep == 0)
3868 return;
3869 ++recurse;
3870
3871 /* lock/unlock the item itself */
3872 if (lock)
3873 tv->v_lock |= VAR_LOCKED;
3874 else
3875 tv->v_lock &= ~VAR_LOCKED;
3876
3877 switch (tv->v_type)
3878 {
3879 case VAR_LIST:
3880 if ((l = tv->vval.v_list) != NULL)
3881 {
3882 if (lock)
3883 l->lv_lock |= VAR_LOCKED;
3884 else
3885 l->lv_lock &= ~VAR_LOCKED;
3886 if (deep < 0 || deep > 1)
3887 /* recursive: lock/unlock the items the List contains */
3888 for (li = l->lv_first; li != NULL; li = li->li_next)
3889 item_lock(&li->li_tv, deep - 1, lock);
3890 }
3891 break;
3892 case VAR_DICT:
3893 if ((d = tv->vval.v_dict) != NULL)
3894 {
3895 if (lock)
3896 d->dv_lock |= VAR_LOCKED;
3897 else
3898 d->dv_lock &= ~VAR_LOCKED;
3899 if (deep < 0 || deep > 1)
3900 {
3901 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003902 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003903 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3904 {
3905 if (!HASHITEM_EMPTY(hi))
3906 {
3907 --todo;
3908 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3909 }
3910 }
3911 }
3912 }
3913 }
3914 --recurse;
3915}
3916
Bram Moolenaara40058a2005-07-11 22:42:07 +00003917/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003918 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3919 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003920 */
3921 static int
3922tv_islocked(tv)
3923 typval_T *tv;
3924{
3925 return (tv->v_lock & VAR_LOCKED)
3926 || (tv->v_type == VAR_LIST
3927 && tv->vval.v_list != NULL
3928 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3929 || (tv->v_type == VAR_DICT
3930 && tv->vval.v_dict != NULL
3931 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3932}
3933
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3935/*
3936 * Delete all "menutrans_" variables.
3937 */
3938 void
3939del_menutrans_vars()
3940{
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003942 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003945 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 {
3948 if (!HASHITEM_EMPTY(hi))
3949 {
3950 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3952 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 }
3954 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956}
3957#endif
3958
3959#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3960
3961/*
3962 * Local string buffer for the next two functions to store a variable name
3963 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3964 * get_user_var_name().
3965 */
3966
3967static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3968
3969static char_u *varnamebuf = NULL;
3970static int varnamebuflen = 0;
3971
3972/*
3973 * Function to concatenate a prefix and a variable name.
3974 */
3975 static char_u *
3976cat_prefix_varname(prefix, name)
3977 int prefix;
3978 char_u *name;
3979{
3980 int len;
3981
3982 len = (int)STRLEN(name) + 3;
3983 if (len > varnamebuflen)
3984 {
3985 vim_free(varnamebuf);
3986 len += 10; /* some additional space */
3987 varnamebuf = alloc(len);
3988 if (varnamebuf == NULL)
3989 {
3990 varnamebuflen = 0;
3991 return NULL;
3992 }
3993 varnamebuflen = len;
3994 }
3995 *varnamebuf = prefix;
3996 varnamebuf[1] = ':';
3997 STRCPY(varnamebuf + 2, name);
3998 return varnamebuf;
3999}
4000
4001/*
4002 * Function given to ExpandGeneric() to obtain the list of user defined
4003 * (global/buffer/window/built-in) variable names.
4004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 char_u *
4006get_user_var_name(xp, idx)
4007 expand_T *xp;
4008 int idx;
4009{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004010 static long_u gdone;
4011 static long_u bdone;
4012 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004013#ifdef FEAT_WINDOWS
4014 static long_u tdone;
4015#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004016 static int vidx;
4017 static hashitem_T *hi;
4018 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
4020 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004021 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004022 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004023#ifdef FEAT_WINDOWS
4024 tdone = 0;
4025#endif
4026 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004027
4028 /* Global variables */
4029 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004031 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004032 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004033 else
4034 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004035 while (HASHITEM_EMPTY(hi))
4036 ++hi;
4037 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4038 return cat_prefix_varname('g', hi->hi_key);
4039 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004041
4042 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004043 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004046 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004048 else
4049 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004050 while (HASHITEM_EMPTY(hi))
4051 ++hi;
4052 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004054 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004056 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 return (char_u *)"b:changedtick";
4058 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004059
4060 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004061 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004062 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004064 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004065 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004066 else
4067 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004068 while (HASHITEM_EMPTY(hi))
4069 ++hi;
4070 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004072
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004073#ifdef FEAT_WINDOWS
4074 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004075 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004076 if (tdone < ht->ht_used)
4077 {
4078 if (tdone++ == 0)
4079 hi = ht->ht_array;
4080 else
4081 ++hi;
4082 while (HASHITEM_EMPTY(hi))
4083 ++hi;
4084 return cat_prefix_varname('t', hi->hi_key);
4085 }
4086#endif
4087
Bram Moolenaar33570922005-01-25 22:26:29 +00004088 /* v: variables */
4089 if (vidx < VV_LEN)
4090 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091
4092 vim_free(varnamebuf);
4093 varnamebuf = NULL;
4094 varnamebuflen = 0;
4095 return NULL;
4096}
4097
4098#endif /* FEAT_CMDL_COMPL */
4099
4100/*
4101 * types for expressions.
4102 */
4103typedef enum
4104{
4105 TYPE_UNKNOWN = 0
4106 , TYPE_EQUAL /* == */
4107 , TYPE_NEQUAL /* != */
4108 , TYPE_GREATER /* > */
4109 , TYPE_GEQUAL /* >= */
4110 , TYPE_SMALLER /* < */
4111 , TYPE_SEQUAL /* <= */
4112 , TYPE_MATCH /* =~ */
4113 , TYPE_NOMATCH /* !~ */
4114} exptype_T;
4115
4116/*
4117 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4120 */
4121
4122/*
4123 * Handle zero level expression.
4124 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004126 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 * Return OK or FAIL.
4128 */
4129 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 char_u **nextcmd;
4134 int evaluate;
4135{
4136 int ret;
4137 char_u *p;
4138
4139 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 if (ret == FAIL || !ends_excmd(*p))
4142 {
4143 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 /*
4146 * Report the invalid expression unless the expression evaluation has
4147 * been cancelled due to an aborting error, an interrupt, or an
4148 * exception.
4149 */
4150 if (!aborting())
4151 EMSG2(_(e_invexpr2), arg);
4152 ret = FAIL;
4153 }
4154 if (nextcmd != NULL)
4155 *nextcmd = check_nextcmd(p);
4156
4157 return ret;
4158}
4159
4160/*
4161 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004162 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 *
4164 * "arg" must point to the first non-white of the expression.
4165 * "arg" is advanced to the next non-white after the recognized expression.
4166 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004167 * Note: "rettv.v_lock" is not set.
4168 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 * Return OK or FAIL.
4170 */
4171 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 int evaluate;
4176{
4177 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004178 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179
4180 /*
4181 * Get the first variable.
4182 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004183 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 return FAIL;
4185
4186 if ((*arg)[0] == '?')
4187 {
4188 result = FALSE;
4189 if (evaluate)
4190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191 int error = FALSE;
4192
4193 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196 if (error)
4197 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199
4200 /*
4201 * Get the second variable.
4202 */
4203 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 return FAIL;
4206
4207 /*
4208 * Check for the ":".
4209 */
4210 if ((*arg)[0] != ':')
4211 {
4212 EMSG(_("E109: Missing ':' after '?'"));
4213 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 return FAIL;
4216 }
4217
4218 /*
4219 * Get the third variable.
4220 */
4221 *arg = skipwhite(*arg + 1);
4222 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4223 {
4224 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 return FAIL;
4227 }
4228 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231
4232 return OK;
4233}
4234
4235/*
4236 * Handle first level expression:
4237 * expr2 || expr2 || expr2 logical OR
4238 *
4239 * "arg" must point to the first non-white of the expression.
4240 * "arg" is advanced to the next non-white after the recognized expression.
4241 *
4242 * Return OK or FAIL.
4243 */
4244 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 int evaluate;
4249{
Bram Moolenaar33570922005-01-25 22:26:29 +00004250 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 long result;
4252 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004253 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
4255 /*
4256 * Get the first variable.
4257 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 return FAIL;
4260
4261 /*
4262 * Repeat until there is no following "||".
4263 */
4264 first = TRUE;
4265 result = FALSE;
4266 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4267 {
4268 if (evaluate && first)
4269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004273 if (error)
4274 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 first = FALSE;
4276 }
4277
4278 /*
4279 * Get the second variable.
4280 */
4281 *arg = skipwhite(*arg + 2);
4282 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4283 return FAIL;
4284
4285 /*
4286 * Compute the result.
4287 */
4288 if (evaluate && !result)
4289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004290 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004292 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004293 if (error)
4294 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 if (evaluate)
4297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298 rettv->v_type = VAR_NUMBER;
4299 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 }
4302
4303 return OK;
4304}
4305
4306/*
4307 * Handle second level expression:
4308 * expr3 && expr3 && expr3 logical AND
4309 *
4310 * "arg" must point to the first non-white of the expression.
4311 * "arg" is advanced to the next non-white after the recognized expression.
4312 *
4313 * Return OK or FAIL.
4314 */
4315 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 int evaluate;
4320{
Bram Moolenaar33570922005-01-25 22:26:29 +00004321 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 long result;
4323 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
4326 /*
4327 * Get the first variable.
4328 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004329 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 return FAIL;
4331
4332 /*
4333 * Repeat until there is no following "&&".
4334 */
4335 first = TRUE;
4336 result = TRUE;
4337 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4338 {
4339 if (evaluate && first)
4340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004343 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004344 if (error)
4345 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 first = FALSE;
4347 }
4348
4349 /*
4350 * Get the second variable.
4351 */
4352 *arg = skipwhite(*arg + 2);
4353 if (eval4(arg, &var2, evaluate && result) == FAIL)
4354 return FAIL;
4355
4356 /*
4357 * Compute the result.
4358 */
4359 if (evaluate && result)
4360 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004361 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004363 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004364 if (error)
4365 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 if (evaluate)
4368 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004369 rettv->v_type = VAR_NUMBER;
4370 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 }
4372 }
4373
4374 return OK;
4375}
4376
4377/*
4378 * Handle third level expression:
4379 * var1 == var2
4380 * var1 =~ var2
4381 * var1 != var2
4382 * var1 !~ var2
4383 * var1 > var2
4384 * var1 >= var2
4385 * var1 < var2
4386 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004387 * var1 is var2
4388 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 *
4390 * "arg" must point to the first non-white of the expression.
4391 * "arg" is advanced to the next non-white after the recognized expression.
4392 *
4393 * Return OK or FAIL.
4394 */
4395 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 int evaluate;
4400{
Bram Moolenaar33570922005-01-25 22:26:29 +00004401 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 char_u *p;
4403 int i;
4404 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004405 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 int len = 2;
4407 long n1, n2;
4408 char_u *s1, *s2;
4409 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4410 regmatch_T regmatch;
4411 int ic;
4412 char_u *save_cpo;
4413
4414 /*
4415 * Get the first variable.
4416 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004417 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 return FAIL;
4419
4420 p = *arg;
4421 switch (p[0])
4422 {
4423 case '=': if (p[1] == '=')
4424 type = TYPE_EQUAL;
4425 else if (p[1] == '~')
4426 type = TYPE_MATCH;
4427 break;
4428 case '!': if (p[1] == '=')
4429 type = TYPE_NEQUAL;
4430 else if (p[1] == '~')
4431 type = TYPE_NOMATCH;
4432 break;
4433 case '>': if (p[1] != '=')
4434 {
4435 type = TYPE_GREATER;
4436 len = 1;
4437 }
4438 else
4439 type = TYPE_GEQUAL;
4440 break;
4441 case '<': if (p[1] != '=')
4442 {
4443 type = TYPE_SMALLER;
4444 len = 1;
4445 }
4446 else
4447 type = TYPE_SEQUAL;
4448 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004449 case 'i': if (p[1] == 's')
4450 {
4451 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4452 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004453 i = p[len];
4454 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004455 {
4456 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4457 type_is = TRUE;
4458 }
4459 }
4460 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462
4463 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004464 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 */
4466 if (type != TYPE_UNKNOWN)
4467 {
4468 /* extra question mark appended: ignore case */
4469 if (p[len] == '?')
4470 {
4471 ic = TRUE;
4472 ++len;
4473 }
4474 /* extra '#' appended: match case */
4475 else if (p[len] == '#')
4476 {
4477 ic = FALSE;
4478 ++len;
4479 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004480 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 else
4482 ic = p_ic;
4483
4484 /*
4485 * Get the second variable.
4486 */
4487 *arg = skipwhite(p + len);
4488 if (eval5(arg, &var2, evaluate) == FAIL)
4489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004490 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 return FAIL;
4492 }
4493
4494 if (evaluate)
4495 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004496 if (type_is && rettv->v_type != var2.v_type)
4497 {
4498 /* For "is" a different type always means FALSE, for "notis"
4499 * it means TRUE. */
4500 n1 = (type == TYPE_NEQUAL);
4501 }
4502 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4503 {
4504 if (type_is)
4505 {
4506 n1 = (rettv->v_type == var2.v_type
4507 && rettv->vval.v_list == var2.vval.v_list);
4508 if (type == TYPE_NEQUAL)
4509 n1 = !n1;
4510 }
4511 else if (rettv->v_type != var2.v_type
4512 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4513 {
4514 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004515 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004516 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004517 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004518 clear_tv(rettv);
4519 clear_tv(&var2);
4520 return FAIL;
4521 }
4522 else
4523 {
4524 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004525 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4526 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004527 if (type == TYPE_NEQUAL)
4528 n1 = !n1;
4529 }
4530 }
4531
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004532 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4533 {
4534 if (type_is)
4535 {
4536 n1 = (rettv->v_type == var2.v_type
4537 && rettv->vval.v_dict == var2.vval.v_dict);
4538 if (type == TYPE_NEQUAL)
4539 n1 = !n1;
4540 }
4541 else if (rettv->v_type != var2.v_type
4542 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4543 {
4544 if (rettv->v_type != var2.v_type)
4545 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4546 else
4547 EMSG(_("E736: Invalid operation for Dictionary"));
4548 clear_tv(rettv);
4549 clear_tv(&var2);
4550 return FAIL;
4551 }
4552 else
4553 {
4554 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004555 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4556 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004557 if (type == TYPE_NEQUAL)
4558 n1 = !n1;
4559 }
4560 }
4561
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004562 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4563 {
4564 if (rettv->v_type != var2.v_type
4565 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4566 {
4567 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004568 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004569 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004570 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004571 clear_tv(rettv);
4572 clear_tv(&var2);
4573 return FAIL;
4574 }
4575 else
4576 {
4577 /* Compare two Funcrefs for being equal or unequal. */
4578 if (rettv->vval.v_string == NULL
4579 || var2.vval.v_string == NULL)
4580 n1 = FALSE;
4581 else
4582 n1 = STRCMP(rettv->vval.v_string,
4583 var2.vval.v_string) == 0;
4584 if (type == TYPE_NEQUAL)
4585 n1 = !n1;
4586 }
4587 }
4588
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004589#ifdef FEAT_FLOAT
4590 /*
4591 * If one of the two variables is a float, compare as a float.
4592 * When using "=~" or "!~", always compare as string.
4593 */
4594 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4595 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4596 {
4597 float_T f1, f2;
4598
4599 if (rettv->v_type == VAR_FLOAT)
4600 f1 = rettv->vval.v_float;
4601 else
4602 f1 = get_tv_number(rettv);
4603 if (var2.v_type == VAR_FLOAT)
4604 f2 = var2.vval.v_float;
4605 else
4606 f2 = get_tv_number(&var2);
4607 n1 = FALSE;
4608 switch (type)
4609 {
4610 case TYPE_EQUAL: n1 = (f1 == f2); break;
4611 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4612 case TYPE_GREATER: n1 = (f1 > f2); break;
4613 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4614 case TYPE_SMALLER: n1 = (f1 < f2); break;
4615 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4616 case TYPE_UNKNOWN:
4617 case TYPE_MATCH:
4618 case TYPE_NOMATCH: break; /* avoid gcc warning */
4619 }
4620 }
4621#endif
4622
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 /*
4624 * If one of the two variables is a number, compare as a number.
4625 * When using "=~" or "!~", always compare as string.
4626 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004627 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004630 n1 = get_tv_number(rettv);
4631 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 switch (type)
4633 {
4634 case TYPE_EQUAL: n1 = (n1 == n2); break;
4635 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4636 case TYPE_GREATER: n1 = (n1 > n2); break;
4637 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4638 case TYPE_SMALLER: n1 = (n1 < n2); break;
4639 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4640 case TYPE_UNKNOWN:
4641 case TYPE_MATCH:
4642 case TYPE_NOMATCH: break; /* avoid gcc warning */
4643 }
4644 }
4645 else
4646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004647 s1 = get_tv_string_buf(rettv, buf1);
4648 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4650 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4651 else
4652 i = 0;
4653 n1 = FALSE;
4654 switch (type)
4655 {
4656 case TYPE_EQUAL: n1 = (i == 0); break;
4657 case TYPE_NEQUAL: n1 = (i != 0); break;
4658 case TYPE_GREATER: n1 = (i > 0); break;
4659 case TYPE_GEQUAL: n1 = (i >= 0); break;
4660 case TYPE_SMALLER: n1 = (i < 0); break;
4661 case TYPE_SEQUAL: n1 = (i <= 0); break;
4662
4663 case TYPE_MATCH:
4664 case TYPE_NOMATCH:
4665 /* avoid 'l' flag in 'cpoptions' */
4666 save_cpo = p_cpo;
4667 p_cpo = (char_u *)"";
4668 regmatch.regprog = vim_regcomp(s2,
4669 RE_MAGIC + RE_STRING);
4670 regmatch.rm_ic = ic;
4671 if (regmatch.regprog != NULL)
4672 {
4673 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004674 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (type == TYPE_NOMATCH)
4676 n1 = !n1;
4677 }
4678 p_cpo = save_cpo;
4679 break;
4680
4681 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4682 }
4683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004684 clear_tv(rettv);
4685 clear_tv(&var2);
4686 rettv->v_type = VAR_NUMBER;
4687 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 }
4689 }
4690
4691 return OK;
4692}
4693
4694/*
4695 * Handle fourth level expression:
4696 * + number addition
4697 * - number subtraction
4698 * . string concatenation
4699 *
4700 * "arg" must point to the first non-white of the expression.
4701 * "arg" is advanced to the next non-white after the recognized expression.
4702 *
4703 * Return OK or FAIL.
4704 */
4705 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004706eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 int evaluate;
4710{
Bram Moolenaar33570922005-01-25 22:26:29 +00004711 typval_T var2;
4712 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 int op;
4714 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715#ifdef FEAT_FLOAT
4716 float_T f1 = 0, f2 = 0;
4717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 char_u *s1, *s2;
4719 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4720 char_u *p;
4721
4722 /*
4723 * Get the first variable.
4724 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004725 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 return FAIL;
4727
4728 /*
4729 * Repeat computing, until no '+', '-' or '.' is following.
4730 */
4731 for (;;)
4732 {
4733 op = **arg;
4734 if (op != '+' && op != '-' && op != '.')
4735 break;
4736
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004737 if ((op != '+' || rettv->v_type != VAR_LIST)
4738#ifdef FEAT_FLOAT
4739 && (op == '.' || rettv->v_type != VAR_FLOAT)
4740#endif
4741 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004742 {
4743 /* For "list + ...", an illegal use of the first operand as
4744 * a number cannot be determined before evaluating the 2nd
4745 * operand: if this is also a list, all is ok.
4746 * For "something . ...", "something - ..." or "non-list + ...",
4747 * we know that the first operand needs to be a string or number
4748 * without evaluating the 2nd operand. So check before to avoid
4749 * side effects after an error. */
4750 if (evaluate && get_tv_string_chk(rettv) == NULL)
4751 {
4752 clear_tv(rettv);
4753 return FAIL;
4754 }
4755 }
4756
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 /*
4758 * Get the second variable.
4759 */
4760 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004761 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 return FAIL;
4765 }
4766
4767 if (evaluate)
4768 {
4769 /*
4770 * Compute the result.
4771 */
4772 if (op == '.')
4773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004774 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4775 s2 = get_tv_string_buf_chk(&var2, buf2);
4776 if (s2 == NULL) /* type error ? */
4777 {
4778 clear_tv(rettv);
4779 clear_tv(&var2);
4780 return FAIL;
4781 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004782 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 clear_tv(rettv);
4784 rettv->v_type = VAR_STRING;
4785 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004787 else if (op == '+' && rettv->v_type == VAR_LIST
4788 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004789 {
4790 /* concatenate Lists */
4791 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4792 &var3) == FAIL)
4793 {
4794 clear_tv(rettv);
4795 clear_tv(&var2);
4796 return FAIL;
4797 }
4798 clear_tv(rettv);
4799 *rettv = var3;
4800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 else
4802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004803 int error = FALSE;
4804
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805#ifdef FEAT_FLOAT
4806 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808 f1 = rettv->vval.v_float;
4809 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811 else
4812#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004813 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814 n1 = get_tv_number_chk(rettv, &error);
4815 if (error)
4816 {
4817 /* This can only happen for "list + non-list". For
4818 * "non-list + ..." or "something - ...", we returned
4819 * before evaluating the 2nd operand. */
4820 clear_tv(rettv);
4821 return FAIL;
4822 }
4823#ifdef FEAT_FLOAT
4824 if (var2.v_type == VAR_FLOAT)
4825 f1 = n1;
4826#endif
4827 }
4828#ifdef FEAT_FLOAT
4829 if (var2.v_type == VAR_FLOAT)
4830 {
4831 f2 = var2.vval.v_float;
4832 n2 = 0;
4833 }
4834 else
4835#endif
4836 {
4837 n2 = get_tv_number_chk(&var2, &error);
4838 if (error)
4839 {
4840 clear_tv(rettv);
4841 clear_tv(&var2);
4842 return FAIL;
4843 }
4844#ifdef FEAT_FLOAT
4845 if (rettv->v_type == VAR_FLOAT)
4846 f2 = n2;
4847#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004848 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004849 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004850
4851#ifdef FEAT_FLOAT
4852 /* If there is a float on either side the result is a float. */
4853 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4854 {
4855 if (op == '+')
4856 f1 = f1 + f2;
4857 else
4858 f1 = f1 - f2;
4859 rettv->v_type = VAR_FLOAT;
4860 rettv->vval.v_float = f1;
4861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004863#endif
4864 {
4865 if (op == '+')
4866 n1 = n1 + n2;
4867 else
4868 n1 = n1 - n2;
4869 rettv->v_type = VAR_NUMBER;
4870 rettv->vval.v_number = n1;
4871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004873 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
4875 }
4876 return OK;
4877}
4878
4879/*
4880 * Handle fifth level expression:
4881 * * number multiplication
4882 * / number division
4883 * % number modulo
4884 *
4885 * "arg" must point to the first non-white of the expression.
4886 * "arg" is advanced to the next non-white after the recognized expression.
4887 *
4888 * Return OK or FAIL.
4889 */
4890 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004891eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004895 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896{
Bram Moolenaar33570922005-01-25 22:26:29 +00004897 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 int op;
4899 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004900#ifdef FEAT_FLOAT
4901 int use_float = FALSE;
4902 float_T f1 = 0, f2;
4903#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004904 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905
4906 /*
4907 * Get the first variable.
4908 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004909 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 return FAIL;
4911
4912 /*
4913 * Repeat computing, until no '*', '/' or '%' is following.
4914 */
4915 for (;;)
4916 {
4917 op = **arg;
4918 if (op != '*' && op != '/' && op != '%')
4919 break;
4920
4921 if (evaluate)
4922 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004923#ifdef FEAT_FLOAT
4924 if (rettv->v_type == VAR_FLOAT)
4925 {
4926 f1 = rettv->vval.v_float;
4927 use_float = TRUE;
4928 n1 = 0;
4929 }
4930 else
4931#endif
4932 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004933 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004934 if (error)
4935 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937 else
4938 n1 = 0;
4939
4940 /*
4941 * Get the second variable.
4942 */
4943 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004944 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 return FAIL;
4946
4947 if (evaluate)
4948 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949#ifdef FEAT_FLOAT
4950 if (var2.v_type == VAR_FLOAT)
4951 {
4952 if (!use_float)
4953 {
4954 f1 = n1;
4955 use_float = TRUE;
4956 }
4957 f2 = var2.vval.v_float;
4958 n2 = 0;
4959 }
4960 else
4961#endif
4962 {
4963 n2 = get_tv_number_chk(&var2, &error);
4964 clear_tv(&var2);
4965 if (error)
4966 return FAIL;
4967#ifdef FEAT_FLOAT
4968 if (use_float)
4969 f2 = n2;
4970#endif
4971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972
4973 /*
4974 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004975 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977#ifdef FEAT_FLOAT
4978 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 if (op == '*')
4981 f1 = f1 * f2;
4982 else if (op == '/')
4983 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004984# ifdef VMS
4985 /* VMS crashes on divide by zero, work around it */
4986 if (f2 == 0.0)
4987 {
4988 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004989 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004990 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004991 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004992 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004993 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004994 }
4995 else
4996 f1 = f1 / f2;
4997# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004998 /* We rely on the floating point library to handle divide
4999 * by zero to result in "inf" and not a crash. */
5000 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005001# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005004 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005005 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005006 return FAIL;
5007 }
5008 rettv->v_type = VAR_FLOAT;
5009 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
5011 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005014 if (op == '*')
5015 n1 = n1 * n2;
5016 else if (op == '/')
5017 {
5018 if (n2 == 0) /* give an error message? */
5019 {
5020 if (n1 == 0)
5021 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5022 else if (n1 < 0)
5023 n1 = -0x7fffffffL;
5024 else
5025 n1 = 0x7fffffffL;
5026 }
5027 else
5028 n1 = n1 / n2;
5029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005031 {
5032 if (n2 == 0) /* give an error message? */
5033 n1 = 0;
5034 else
5035 n1 = n1 % n2;
5036 }
5037 rettv->v_type = VAR_NUMBER;
5038 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
5041 }
5042
5043 return OK;
5044}
5045
5046/*
5047 * Handle sixth level expression:
5048 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005049 * "string" string constant
5050 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 * &option-name option value
5052 * @r register contents
5053 * identifier variable value
5054 * function() function call
5055 * $VAR environment variable
5056 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005057 * [expr, expr] List
5058 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 *
5060 * Also handle:
5061 * ! in front logical NOT
5062 * - in front unary minus
5063 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 * trailing [] subscript in String or List
5065 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 *
5067 * "arg" must point to the first non-white of the expression.
5068 * "arg" is advanced to the next non-white after the recognized expression.
5069 *
5070 * Return OK or FAIL.
5071 */
5072 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005073eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005077 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 long n;
5080 int len;
5081 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 char_u *start_leader, *end_leader;
5083 int ret = OK;
5084 char_u *alias;
5085
5086 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005088 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
5092 /*
5093 * Skip '!' and '-' characters. They are handled later.
5094 */
5095 start_leader = *arg;
5096 while (**arg == '!' || **arg == '-' || **arg == '+')
5097 *arg = skipwhite(*arg + 1);
5098 end_leader = *arg;
5099
5100 switch (**arg)
5101 {
5102 /*
5103 * Number constant.
5104 */
5105 case '0':
5106 case '1':
5107 case '2':
5108 case '3':
5109 case '4':
5110 case '5':
5111 case '6':
5112 case '7':
5113 case '8':
5114 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005115 {
5116#ifdef FEAT_FLOAT
5117 char_u *p = skipdigits(*arg + 1);
5118 int get_float = FALSE;
5119
5120 /* We accept a float when the format matches
5121 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005122 * strict to avoid backwards compatibility problems.
5123 * Don't look for a float after the "." operator, so that
5124 * ":let vers = 1.2.3" doesn't fail. */
5125 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005127 get_float = TRUE;
5128 p = skipdigits(p + 2);
5129 if (*p == 'e' || *p == 'E')
5130 {
5131 ++p;
5132 if (*p == '-' || *p == '+')
5133 ++p;
5134 if (!vim_isdigit(*p))
5135 get_float = FALSE;
5136 else
5137 p = skipdigits(p + 1);
5138 }
5139 if (ASCII_ISALPHA(*p) || *p == '.')
5140 get_float = FALSE;
5141 }
5142 if (get_float)
5143 {
5144 float_T f;
5145
5146 *arg += string2float(*arg, &f);
5147 if (evaluate)
5148 {
5149 rettv->v_type = VAR_FLOAT;
5150 rettv->vval.v_float = f;
5151 }
5152 }
5153 else
5154#endif
5155 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005156 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005157 *arg += len;
5158 if (evaluate)
5159 {
5160 rettv->v_type = VAR_NUMBER;
5161 rettv->vval.v_number = n;
5162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
5164 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166
5167 /*
5168 * String constant: "string".
5169 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005170 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 break;
5172
5173 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005177 break;
5178
5179 /*
5180 * List: [expr, expr]
5181 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005182 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 break;
5184
5185 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 * Dictionary: {key: val, key: val}
5187 */
5188 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5189 break;
5190
5191 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005192 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005194 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 break;
5196
5197 /*
5198 * Environment variable: $VAR.
5199 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005200 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 break;
5202
5203 /*
5204 * Register contents: @r.
5205 */
5206 case '@': ++*arg;
5207 if (evaluate)
5208 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005209 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005210 rettv->vval.v_string = get_reg_contents(**arg,
5211 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213 if (**arg != NUL)
5214 ++*arg;
5215 break;
5216
5217 /*
5218 * nested expression: (expression).
5219 */
5220 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005221 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 if (**arg == ')')
5223 ++*arg;
5224 else if (ret == OK)
5225 {
5226 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005227 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 ret = FAIL;
5229 }
5230 break;
5231
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 break;
5234 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235
5236 if (ret == NOTDONE)
5237 {
5238 /*
5239 * Must be a variable or function name.
5240 * Can also be a curly-braces kind of name: {expr}.
5241 */
5242 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005243 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005244 if (alias != NULL)
5245 s = alias;
5246
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005247 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 ret = FAIL;
5249 else
5250 {
5251 if (**arg == '(') /* recursive! */
5252 {
5253 /* If "s" is the name of a variable of type VAR_FUNC
5254 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005255 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005256
5257 /* Invoke the function. */
5258 ret = get_func_tv(s, len, rettv, arg,
5259 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005260 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005261
5262 /* If evaluate is FALSE rettv->v_type was not set in
5263 * get_func_tv, but it's needed in handle_subscript() to parse
5264 * what follows. So set it here. */
5265 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5266 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005267 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005268 rettv->v_type = VAR_FUNC;
5269 }
5270
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271 /* Stop the expression evaluation when immediately
5272 * aborting on error, or when an interrupt occurred or
5273 * an exception was thrown but not caught. */
5274 if (aborting())
5275 {
5276 if (ret == OK)
5277 clear_tv(rettv);
5278 ret = FAIL;
5279 }
5280 }
5281 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005282 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005283 else
5284 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005285 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005286 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 }
5288
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 *arg = skipwhite(*arg);
5290
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005291 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5292 * expr(expr). */
5293 if (ret == OK)
5294 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295
5296 /*
5297 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5298 */
5299 if (ret == OK && evaluate && end_leader > start_leader)
5300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005301 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005302 int val = 0;
5303#ifdef FEAT_FLOAT
5304 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005305
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005306 if (rettv->v_type == VAR_FLOAT)
5307 f = rettv->vval.v_float;
5308 else
5309#endif
5310 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 clear_tv(rettv);
5314 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005316 else
5317 {
5318 while (end_leader > start_leader)
5319 {
5320 --end_leader;
5321 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005322 {
5323#ifdef FEAT_FLOAT
5324 if (rettv->v_type == VAR_FLOAT)
5325 f = !f;
5326 else
5327#endif
5328 val = !val;
5329 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005330 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005331 {
5332#ifdef FEAT_FLOAT
5333 if (rettv->v_type == VAR_FLOAT)
5334 f = -f;
5335 else
5336#endif
5337 val = -val;
5338 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005339 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005340#ifdef FEAT_FLOAT
5341 if (rettv->v_type == VAR_FLOAT)
5342 {
5343 clear_tv(rettv);
5344 rettv->vval.v_float = f;
5345 }
5346 else
5347#endif
5348 {
5349 clear_tv(rettv);
5350 rettv->v_type = VAR_NUMBER;
5351 rettv->vval.v_number = val;
5352 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 }
5355
5356 return ret;
5357}
5358
5359/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005360 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5361 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5363 */
5364 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005365eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005367 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005369 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370{
5371 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005372 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374 long len = -1;
5375 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005377 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005379 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005381 if (verbose)
5382 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 return FAIL;
5384 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005385#ifdef FEAT_FLOAT
5386 else if (rettv->v_type == VAR_FLOAT)
5387 {
5388 if (verbose)
5389 EMSG(_(e_float_as_string));
5390 return FAIL;
5391 }
5392#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005394 init_tv(&var1);
5395 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398 /*
5399 * dict.name
5400 */
5401 key = *arg + 1;
5402 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5403 ;
5404 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 }
5408 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005410 /*
5411 * something[idx]
5412 *
5413 * Get the (first) variable from inside the [].
5414 */
5415 *arg = skipwhite(*arg + 1);
5416 if (**arg == ':')
5417 empty1 = TRUE;
5418 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5419 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005420 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5421 {
5422 /* not a number or string */
5423 clear_tv(&var1);
5424 return FAIL;
5425 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005426
5427 /*
5428 * Get the second variable from inside the [:].
5429 */
5430 if (**arg == ':')
5431 {
5432 range = TRUE;
5433 *arg = skipwhite(*arg + 1);
5434 if (**arg == ']')
5435 empty2 = TRUE;
5436 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005438 if (!empty1)
5439 clear_tv(&var1);
5440 return FAIL;
5441 }
5442 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5443 {
5444 /* not a number or string */
5445 if (!empty1)
5446 clear_tv(&var1);
5447 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448 return FAIL;
5449 }
5450 }
5451
5452 /* Check for the ']'. */
5453 if (**arg != ']')
5454 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005455 if (verbose)
5456 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005457 clear_tv(&var1);
5458 if (range)
5459 clear_tv(&var2);
5460 return FAIL;
5461 }
5462 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 }
5464
5465 if (evaluate)
5466 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005467 n1 = 0;
5468 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 n1 = get_tv_number(&var1);
5471 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 }
5473 if (range)
5474 {
5475 if (empty2)
5476 n2 = -1;
5477 else
5478 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005479 n2 = get_tv_number(&var2);
5480 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 }
5482 }
5483
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005484 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 {
5486 case VAR_NUMBER:
5487 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005488 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 len = (long)STRLEN(s);
5490 if (range)
5491 {
5492 /* The resulting variable is a substring. If the indexes
5493 * are out of range the result is empty. */
5494 if (n1 < 0)
5495 {
5496 n1 = len + n1;
5497 if (n1 < 0)
5498 n1 = 0;
5499 }
5500 if (n2 < 0)
5501 n2 = len + n2;
5502 else if (n2 >= len)
5503 n2 = len;
5504 if (n1 >= len || n2 < 0 || n1 > n2)
5505 s = NULL;
5506 else
5507 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5508 }
5509 else
5510 {
5511 /* The resulting variable is a string of a single
5512 * character. If the index is too big or negative the
5513 * result is empty. */
5514 if (n1 >= len || n1 < 0)
5515 s = NULL;
5516 else
5517 s = vim_strnsave(s + n1, 1);
5518 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 clear_tv(rettv);
5520 rettv->v_type = VAR_STRING;
5521 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 break;
5523
5524 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005525 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526 if (n1 < 0)
5527 n1 = len + n1;
5528 if (!empty1 && (n1 < 0 || n1 >= len))
5529 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005530 /* For a range we allow invalid values and return an empty
5531 * list. A list index out of range is an error. */
5532 if (!range)
5533 {
5534 if (verbose)
5535 EMSGN(_(e_listidx), n1);
5536 return FAIL;
5537 }
5538 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005539 }
5540 if (range)
5541 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005542 list_T *l;
5543 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544
5545 if (n2 < 0)
5546 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005547 else if (n2 >= len)
5548 n2 = len - 1;
5549 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005550 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551 l = list_alloc();
5552 if (l == NULL)
5553 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 n1 <= n2; ++n1)
5556 {
5557 if (list_append_tv(l, &item->li_tv) == FAIL)
5558 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005559 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560 return FAIL;
5561 }
5562 item = item->li_next;
5563 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005564 clear_tv(rettv);
5565 rettv->v_type = VAR_LIST;
5566 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005567 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 }
5569 else
5570 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005571 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 clear_tv(rettv);
5573 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005574 }
5575 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005576
5577 case VAR_DICT:
5578 if (range)
5579 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005580 if (verbose)
5581 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005582 if (len == -1)
5583 clear_tv(&var1);
5584 return FAIL;
5585 }
5586 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005587 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588
5589 if (len == -1)
5590 {
5591 key = get_tv_string(&var1);
5592 if (*key == NUL)
5593 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005594 if (verbose)
5595 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596 clear_tv(&var1);
5597 return FAIL;
5598 }
5599 }
5600
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005601 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005603 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005604 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005605 if (len == -1)
5606 clear_tv(&var1);
5607 if (item == NULL)
5608 return FAIL;
5609
5610 copy_tv(&item->di_tv, &var1);
5611 clear_tv(rettv);
5612 *rettv = var1;
5613 }
5614 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615 }
5616 }
5617
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 return OK;
5619}
5620
5621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 * Get an option value.
5623 * "arg" points to the '&' or '+' before the option name.
5624 * "arg" is advanced to character after the option name.
5625 * Return OK or FAIL.
5626 */
5627 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005628get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005630 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 int evaluate;
5632{
5633 char_u *option_end;
5634 long numval;
5635 char_u *stringval;
5636 int opt_type;
5637 int c;
5638 int working = (**arg == '+'); /* has("+option") */
5639 int ret = OK;
5640 int opt_flags;
5641
5642 /*
5643 * Isolate the option name and find its value.
5644 */
5645 option_end = find_option_end(arg, &opt_flags);
5646 if (option_end == NULL)
5647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 EMSG2(_("E112: Option name missing: %s"), *arg);
5650 return FAIL;
5651 }
5652
5653 if (!evaluate)
5654 {
5655 *arg = option_end;
5656 return OK;
5657 }
5658
5659 c = *option_end;
5660 *option_end = NUL;
5661 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005662 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
5664 if (opt_type == -3) /* invalid name */
5665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005666 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 EMSG2(_("E113: Unknown option: %s"), *arg);
5668 ret = FAIL;
5669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 {
5672 if (opt_type == -2) /* hidden string option */
5673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005674 rettv->v_type = VAR_STRING;
5675 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677 else if (opt_type == -1) /* hidden number option */
5678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 rettv->v_type = VAR_NUMBER;
5680 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682 else if (opt_type == 1) /* number option */
5683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 rettv->v_type = VAR_NUMBER;
5685 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
5687 else /* string option */
5688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005689 rettv->v_type = VAR_STRING;
5690 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 }
5693 else if (working && (opt_type == -2 || opt_type == -1))
5694 ret = FAIL;
5695
5696 *option_end = c; /* put back for error messages */
5697 *arg = option_end;
5698
5699 return ret;
5700}
5701
5702/*
5703 * Allocate a variable for a string constant.
5704 * Return OK or FAIL.
5705 */
5706 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005707get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 int evaluate;
5711{
5712 char_u *p;
5713 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 int extra = 0;
5715
5716 /*
5717 * Find the end of the string, skipping backslashed characters.
5718 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005719 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 if (*p == '\\' && p[1] != NUL)
5722 {
5723 ++p;
5724 /* A "\<x>" form occupies at least 4 characters, and produces up
5725 * to 6 characters: reserve space for 2 extra */
5726 if (*p == '<')
5727 extra += 2;
5728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 }
5730
5731 if (*p != '"')
5732 {
5733 EMSG2(_("E114: Missing quote: %s"), *arg);
5734 return FAIL;
5735 }
5736
5737 /* If only parsing, set *arg and return here */
5738 if (!evaluate)
5739 {
5740 *arg = p + 1;
5741 return OK;
5742 }
5743
5744 /*
5745 * Copy the string into allocated memory, handling backslashed
5746 * characters.
5747 */
5748 name = alloc((unsigned)(p - *arg + extra));
5749 if (name == NULL)
5750 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 rettv->v_type = VAR_STRING;
5752 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 {
5756 if (*p == '\\')
5757 {
5758 switch (*++p)
5759 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 case 'b': *name++ = BS; ++p; break;
5761 case 'e': *name++ = ESC; ++p; break;
5762 case 'f': *name++ = FF; ++p; break;
5763 case 'n': *name++ = NL; ++p; break;
5764 case 'r': *name++ = CAR; ++p; break;
5765 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
5767 case 'X': /* hex: "\x1", "\x12" */
5768 case 'x':
5769 case 'u': /* Unicode: "\u0023" */
5770 case 'U':
5771 if (vim_isxdigit(p[1]))
5772 {
5773 int n, nr;
5774 int c = toupper(*p);
5775
5776 if (c == 'X')
5777 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005778 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005780 else
5781 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 nr = 0;
5783 while (--n >= 0 && vim_isxdigit(p[1]))
5784 {
5785 ++p;
5786 nr = (nr << 4) + hex2nr(*p);
5787 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789#ifdef FEAT_MBYTE
5790 /* For "\u" store the number according to
5791 * 'encoding'. */
5792 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 else
5795#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 break;
5799
5800 /* octal: "\1", "\12", "\123" */
5801 case '0':
5802 case '1':
5803 case '2':
5804 case '3':
5805 case '4':
5806 case '5':
5807 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 case '7': *name = *p++ - '0';
5809 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 *name = (*name << 3) + *p++ - '0';
5812 if (*p >= '0' && *p <= '7')
5813 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 break;
5817
5818 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 if (extra != 0)
5821 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 break;
5824 }
5825 /* FALLTHROUGH */
5826
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 break;
5829 }
5830 }
5831 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 *arg = p + 1;
5837
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 return OK;
5839}
5840
5841/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 * Return OK or FAIL.
5844 */
5845 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005846get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 int evaluate;
5850{
5851 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 int reduce = 0;
5854
5855 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 */
5858 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 break;
5864 ++reduce;
5865 ++p;
5866 }
5867 }
5868
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 return FAIL;
5873 }
5874
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 if (!evaluate)
5877 {
5878 *arg = p + 1;
5879 return OK;
5880 }
5881
5882 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005883 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 */
5885 str = alloc((unsigned)((p - *arg) - reduce));
5886 if (str == NULL)
5887 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 rettv->v_type = VAR_STRING;
5889 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 break;
5897 ++p;
5898 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005901 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 *arg = p + 1;
5903
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 return OK;
5905}
5906
5907/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908 * Allocate a variable for a List and fill it from "*arg".
5909 * Return OK or FAIL.
5910 */
5911 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005912get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915 int evaluate;
5916{
Bram Moolenaar33570922005-01-25 22:26:29 +00005917 list_T *l = NULL;
5918 typval_T tv;
5919 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920
5921 if (evaluate)
5922 {
5923 l = list_alloc();
5924 if (l == NULL)
5925 return FAIL;
5926 }
5927
5928 *arg = skipwhite(*arg + 1);
5929 while (**arg != ']' && **arg != NUL)
5930 {
5931 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5932 goto failret;
5933 if (evaluate)
5934 {
5935 item = listitem_alloc();
5936 if (item != NULL)
5937 {
5938 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005939 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 list_append(l, item);
5941 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005942 else
5943 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 }
5945
5946 if (**arg == ']')
5947 break;
5948 if (**arg != ',')
5949 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005950 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 goto failret;
5952 }
5953 *arg = skipwhite(*arg + 1);
5954 }
5955
5956 if (**arg != ']')
5957 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005958 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959failret:
5960 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005961 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 return FAIL;
5963 }
5964
5965 *arg = skipwhite(*arg + 1);
5966 if (evaluate)
5967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005968 rettv->v_type = VAR_LIST;
5969 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970 ++l->lv_refcount;
5971 }
5972
5973 return OK;
5974}
5975
5976/*
5977 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005978 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005980 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981list_alloc()
5982{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005983 list_T *l;
5984
5985 l = (list_T *)alloc_clear(sizeof(list_T));
5986 if (l != NULL)
5987 {
5988 /* Prepend the list to the list of lists for garbage collection. */
5989 if (first_list != NULL)
5990 first_list->lv_used_prev = l;
5991 l->lv_used_prev = NULL;
5992 l->lv_used_next = first_list;
5993 first_list = l;
5994 }
5995 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996}
5997
5998/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005999 * Allocate an empty list for a return value.
6000 * Returns OK or FAIL.
6001 */
6002 static int
6003rettv_list_alloc(rettv)
6004 typval_T *rettv;
6005{
6006 list_T *l = list_alloc();
6007
6008 if (l == NULL)
6009 return FAIL;
6010
6011 rettv->vval.v_list = l;
6012 rettv->v_type = VAR_LIST;
6013 ++l->lv_refcount;
6014 return OK;
6015}
6016
6017/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018 * Unreference a list: decrement the reference count and free it when it
6019 * becomes zero.
6020 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006021 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006025 if (l != NULL && --l->lv_refcount <= 0)
6026 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027}
6028
6029/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006030 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031 * Ignores the reference count.
6032 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006033 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006034list_free(l, recurse)
6035 list_T *l;
6036 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037{
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006040 /* Remove the list from the list of lists for garbage collection. */
6041 if (l->lv_used_prev == NULL)
6042 first_list = l->lv_used_next;
6043 else
6044 l->lv_used_prev->lv_used_next = l->lv_used_next;
6045 if (l->lv_used_next != NULL)
6046 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6047
Bram Moolenaard9fba312005-06-26 22:34:35 +00006048 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006050 /* Remove the item before deleting it. */
6051 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006052 if (recurse || (item->li_tv.v_type != VAR_LIST
6053 && item->li_tv.v_type != VAR_DICT))
6054 clear_tv(&item->li_tv);
6055 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 }
6057 vim_free(l);
6058}
6059
6060/*
6061 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006062 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006064 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065listitem_alloc()
6066{
Bram Moolenaar33570922005-01-25 22:26:29 +00006067 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068}
6069
6070/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006071 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006073 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006075 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006077 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078 vim_free(item);
6079}
6080
6081/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006082 * Remove a list item from a List and free it. Also clears the value.
6083 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006084 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006085listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 list_T *l;
6087 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006088{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006089 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006090 listitem_free(item);
6091}
6092
6093/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094 * Get the number of items in a list.
6095 */
6096 static long
6097list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006099{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006100 if (l == NULL)
6101 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006102 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006103}
6104
6105/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106 * Return TRUE when two lists have exactly the same values.
6107 */
6108 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006109list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006110 list_T *l1;
6111 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006113 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114{
Bram Moolenaar33570922005-01-25 22:26:29 +00006115 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006116
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006117 if (l1 == NULL || l2 == NULL)
6118 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006119 if (l1 == l2)
6120 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006121 if (list_len(l1) != list_len(l2))
6122 return FALSE;
6123
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006124 for (item1 = l1->lv_first, item2 = l2->lv_first;
6125 item1 != NULL && item2 != NULL;
6126 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006127 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006128 return FALSE;
6129 return item1 == NULL && item2 == NULL;
6130}
6131
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006132#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6133 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006134/*
6135 * Return the dictitem that an entry in a hashtable points to.
6136 */
6137 dictitem_T *
6138dict_lookup(hi)
6139 hashitem_T *hi;
6140{
6141 return HI2DI(hi);
6142}
6143#endif
6144
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006145/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006146 * Return TRUE when two dictionaries have exactly the same key/values.
6147 */
6148 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006149dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006150 dict_T *d1;
6151 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006152 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006153 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154{
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 hashitem_T *hi;
6156 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006157 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006158
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006159 if (d1 == NULL || d2 == NULL)
6160 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006161 if (d1 == d2)
6162 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006163 if (dict_len(d1) != dict_len(d2))
6164 return FALSE;
6165
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006166 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006168 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006169 if (!HASHITEM_EMPTY(hi))
6170 {
6171 item2 = dict_find(d2, hi->hi_key, -1);
6172 if (item2 == NULL)
6173 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006174 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006175 return FALSE;
6176 --todo;
6177 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006178 }
6179 return TRUE;
6180}
6181
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006182static int tv_equal_recurse_limit;
6183
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006184/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006186 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006187 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006188 */
6189 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006191 typval_T *tv1;
6192 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006193 int ic; /* ignore case */
6194 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006195{
6196 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006197 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006198 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006199 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006200
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006201 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006202 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006203
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006204 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205 * recursiveness to a limit. We guess they are equal then.
6206 * A fixed limit has the problem of still taking an awful long time.
6207 * Reduce the limit every time running into it. That should work fine for
6208 * deeply linked structures that are not recursively linked and catch
6209 * recursiveness quickly. */
6210 if (!recursive)
6211 tv_equal_recurse_limit = 1000;
6212 if (recursive_cnt >= tv_equal_recurse_limit)
6213 {
6214 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006215 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006216 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006217
6218 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006219 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006220 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006221 ++recursive_cnt;
6222 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6223 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006224 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006225
6226 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006227 ++recursive_cnt;
6228 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6229 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006230 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006231
6232 case VAR_FUNC:
6233 return (tv1->vval.v_string != NULL
6234 && tv2->vval.v_string != NULL
6235 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6236
6237 case VAR_NUMBER:
6238 return tv1->vval.v_number == tv2->vval.v_number;
6239
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006240#ifdef FEAT_FLOAT
6241 case VAR_FLOAT:
6242 return tv1->vval.v_float == tv2->vval.v_float;
6243#endif
6244
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006245 case VAR_STRING:
6246 s1 = get_tv_string_buf(tv1, buf1);
6247 s2 = get_tv_string_buf(tv2, buf2);
6248 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006250
6251 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006252 return TRUE;
6253}
6254
6255/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 * Locate item with index "n" in list "l" and return it.
6257 * A negative index is counted from the end; -1 is the last item.
6258 * Returns NULL when "n" is out of range.
6259 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006260 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 long n;
6264{
Bram Moolenaar33570922005-01-25 22:26:29 +00006265 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006266 long idx;
6267
6268 if (l == NULL)
6269 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006270
6271 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006272 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006273 n = l->lv_len + n;
6274
6275 /* Check for index out of range. */
6276 if (n < 0 || n >= l->lv_len)
6277 return NULL;
6278
6279 /* When there is a cached index may start search from there. */
6280 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006281 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006282 if (n < l->lv_idx / 2)
6283 {
6284 /* closest to the start of the list */
6285 item = l->lv_first;
6286 idx = 0;
6287 }
6288 else if (n > (l->lv_idx + l->lv_len) / 2)
6289 {
6290 /* closest to the end of the list */
6291 item = l->lv_last;
6292 idx = l->lv_len - 1;
6293 }
6294 else
6295 {
6296 /* closest to the cached index */
6297 item = l->lv_idx_item;
6298 idx = l->lv_idx;
6299 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006300 }
6301 else
6302 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006303 if (n < l->lv_len / 2)
6304 {
6305 /* closest to the start of the list */
6306 item = l->lv_first;
6307 idx = 0;
6308 }
6309 else
6310 {
6311 /* closest to the end of the list */
6312 item = l->lv_last;
6313 idx = l->lv_len - 1;
6314 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006316
6317 while (n > idx)
6318 {
6319 /* search forward */
6320 item = item->li_next;
6321 ++idx;
6322 }
6323 while (n < idx)
6324 {
6325 /* search backward */
6326 item = item->li_prev;
6327 --idx;
6328 }
6329
6330 /* cache the used index */
6331 l->lv_idx = idx;
6332 l->lv_idx_item = item;
6333
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006334 return item;
6335}
6336
6337/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006338 * Get list item "l[idx]" as a number.
6339 */
6340 static long
6341list_find_nr(l, idx, errorp)
6342 list_T *l;
6343 long idx;
6344 int *errorp; /* set to TRUE when something wrong */
6345{
6346 listitem_T *li;
6347
6348 li = list_find(l, idx);
6349 if (li == NULL)
6350 {
6351 if (errorp != NULL)
6352 *errorp = TRUE;
6353 return -1L;
6354 }
6355 return get_tv_number_chk(&li->li_tv, errorp);
6356}
6357
6358/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006359 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6360 */
6361 char_u *
6362list_find_str(l, idx)
6363 list_T *l;
6364 long idx;
6365{
6366 listitem_T *li;
6367
6368 li = list_find(l, idx - 1);
6369 if (li == NULL)
6370 {
6371 EMSGN(_(e_listidx), idx);
6372 return NULL;
6373 }
6374 return get_tv_string(&li->li_tv);
6375}
6376
6377/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006378 * Locate "item" list "l" and return its index.
6379 * Returns -1 when "item" is not in the list.
6380 */
6381 static long
6382list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006383 list_T *l;
6384 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006385{
6386 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006387 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006388
6389 if (l == NULL)
6390 return -1;
6391 idx = 0;
6392 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6393 ++idx;
6394 if (li == NULL)
6395 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006396 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006397}
6398
6399/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400 * Append item "item" to the end of list "l".
6401 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006402 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 list_T *l;
6405 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006406{
6407 if (l->lv_last == NULL)
6408 {
6409 /* empty list */
6410 l->lv_first = item;
6411 l->lv_last = item;
6412 item->li_prev = NULL;
6413 }
6414 else
6415 {
6416 l->lv_last->li_next = item;
6417 item->li_prev = l->lv_last;
6418 l->lv_last = item;
6419 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006420 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421 item->li_next = NULL;
6422}
6423
6424/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006425 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006428 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 list_T *l;
6431 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006432{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006433 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006434
Bram Moolenaar05159a02005-02-26 23:04:13 +00006435 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006436 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006437 copy_tv(tv, &li->li_tv);
6438 list_append(l, li);
6439 return OK;
6440}
6441
6442/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006443 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006444 * Return FAIL when out of memory.
6445 */
6446 int
6447list_append_dict(list, dict)
6448 list_T *list;
6449 dict_T *dict;
6450{
6451 listitem_T *li = listitem_alloc();
6452
6453 if (li == NULL)
6454 return FAIL;
6455 li->li_tv.v_type = VAR_DICT;
6456 li->li_tv.v_lock = 0;
6457 li->li_tv.vval.v_dict = dict;
6458 list_append(list, li);
6459 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006460 return OK;
6461}
6462
6463/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006464 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006465 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006466 * Returns FAIL when out of memory.
6467 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006468 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006469list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006470 list_T *l;
6471 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006472 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006473{
6474 listitem_T *li = listitem_alloc();
6475
6476 if (li == NULL)
6477 return FAIL;
6478 list_append(l, li);
6479 li->li_tv.v_type = VAR_STRING;
6480 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006481 if (str == NULL)
6482 li->li_tv.vval.v_string = NULL;
6483 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006484 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006485 return FAIL;
6486 return OK;
6487}
6488
6489/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006490 * Append "n" to list "l".
6491 * Returns FAIL when out of memory.
6492 */
6493 static int
6494list_append_number(l, n)
6495 list_T *l;
6496 varnumber_T n;
6497{
6498 listitem_T *li;
6499
6500 li = listitem_alloc();
6501 if (li == NULL)
6502 return FAIL;
6503 li->li_tv.v_type = VAR_NUMBER;
6504 li->li_tv.v_lock = 0;
6505 li->li_tv.vval.v_number = n;
6506 list_append(l, li);
6507 return OK;
6508}
6509
6510/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006511 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512 * If "item" is NULL append at the end.
6513 * Return FAIL when out of memory.
6514 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006515 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 list_T *l;
6518 typval_T *tv;
6519 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520{
Bram Moolenaar33570922005-01-25 22:26:29 +00006521 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522
6523 if (ni == NULL)
6524 return FAIL;
6525 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006526 list_insert(l, ni, item);
6527 return OK;
6528}
6529
6530 void
6531list_insert(l, ni, item)
6532 list_T *l;
6533 listitem_T *ni;
6534 listitem_T *item;
6535{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 if (item == NULL)
6537 /* Append new item at end of list. */
6538 list_append(l, ni);
6539 else
6540 {
6541 /* Insert new item before existing item. */
6542 ni->li_prev = item->li_prev;
6543 ni->li_next = item;
6544 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006545 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006547 ++l->lv_idx;
6548 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006550 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006552 l->lv_idx_item = NULL;
6553 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006555 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006556 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557}
6558
6559/*
6560 * Extend "l1" with "l2".
6561 * If "bef" is NULL append at the end, otherwise insert before this item.
6562 * Returns FAIL when out of memory.
6563 */
6564 static int
6565list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006566 list_T *l1;
6567 list_T *l2;
6568 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569{
Bram Moolenaar33570922005-01-25 22:26:29 +00006570 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006571 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006572
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006573 /* We also quit the loop when we have inserted the original item count of
6574 * the list, avoid a hang when we extend a list with itself. */
6575 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6577 return FAIL;
6578 return OK;
6579}
6580
6581/*
6582 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6583 * Return FAIL when out of memory.
6584 */
6585 static int
6586list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 list_T *l1;
6588 list_T *l2;
6589 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006590{
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006592
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006593 if (l1 == NULL || l2 == NULL)
6594 return FAIL;
6595
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006596 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006597 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006598 if (l == NULL)
6599 return FAIL;
6600 tv->v_type = VAR_LIST;
6601 tv->vval.v_list = l;
6602
6603 /* append all items from the second list */
6604 return list_extend(l, l2, NULL);
6605}
6606
6607/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006608 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611 * Returns NULL when out of memory.
6612 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006613 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006614list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006615 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006617 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618{
Bram Moolenaar33570922005-01-25 22:26:29 +00006619 list_T *copy;
6620 listitem_T *item;
6621 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622
6623 if (orig == NULL)
6624 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625
6626 copy = list_alloc();
6627 if (copy != NULL)
6628 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006629 if (copyID != 0)
6630 {
6631 /* Do this before adding the items, because one of the items may
6632 * refer back to this list. */
6633 orig->lv_copyID = copyID;
6634 orig->lv_copylist = copy;
6635 }
6636 for (item = orig->lv_first; item != NULL && !got_int;
6637 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 {
6639 ni = listitem_alloc();
6640 if (ni == NULL)
6641 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006642 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006643 {
6644 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6645 {
6646 vim_free(ni);
6647 break;
6648 }
6649 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006651 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006652 list_append(copy, ni);
6653 }
6654 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006655 if (item != NULL)
6656 {
6657 list_unref(copy);
6658 copy = NULL;
6659 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006660 }
6661
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662 return copy;
6663}
6664
6665/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006666 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006667 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006668 * This used to be called list_remove, but that conflicts with a Sun header
6669 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006671 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006672vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006673 list_T *l;
6674 listitem_T *item;
6675 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006676{
Bram Moolenaar33570922005-01-25 22:26:29 +00006677 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006679 /* notify watchers */
6680 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006681 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006682 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006683 list_fix_watch(l, ip);
6684 if (ip == item2)
6685 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006686 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006687
6688 if (item2->li_next == NULL)
6689 l->lv_last = item->li_prev;
6690 else
6691 item2->li_next->li_prev = item->li_prev;
6692 if (item->li_prev == NULL)
6693 l->lv_first = item2->li_next;
6694 else
6695 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006696 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006697}
6698
6699/*
6700 * Return an allocated string with the string representation of a list.
6701 * May return NULL.
6702 */
6703 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006704list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006705 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006706 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006707{
6708 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006709
6710 if (tv->vval.v_list == NULL)
6711 return NULL;
6712 ga_init2(&ga, (int)sizeof(char), 80);
6713 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006714 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006715 {
6716 vim_free(ga.ga_data);
6717 return NULL;
6718 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006719 ga_append(&ga, ']');
6720 ga_append(&ga, NUL);
6721 return (char_u *)ga.ga_data;
6722}
6723
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724typedef struct join_S {
6725 char_u *s;
6726 char_u *tofree;
6727} join_T;
6728
6729 static int
6730list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6731 garray_T *gap; /* to store the result in */
6732 list_T *l;
6733 char_u *sep;
6734 int echo_style;
6735 int copyID;
6736 garray_T *join_gap; /* to keep each list item string */
6737{
6738 int i;
6739 join_T *p;
6740 int len;
6741 int sumlen = 0;
6742 int first = TRUE;
6743 char_u *tofree;
6744 char_u numbuf[NUMBUFLEN];
6745 listitem_T *item;
6746 char_u *s;
6747
6748 /* Stringify each item in the list. */
6749 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6750 {
6751 if (echo_style)
6752 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6753 else
6754 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6755 if (s == NULL)
6756 return FAIL;
6757
6758 len = (int)STRLEN(s);
6759 sumlen += len;
6760
Bram Moolenaarcde88542015-08-11 19:14:00 +02006761 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006762 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6763 if (tofree != NULL || s != numbuf)
6764 {
6765 p->s = s;
6766 p->tofree = tofree;
6767 }
6768 else
6769 {
6770 p->s = vim_strnsave(s, len);
6771 p->tofree = p->s;
6772 }
6773
6774 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006775 if (did_echo_string_emsg) /* recursion error, bail out */
6776 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006777 }
6778
6779 /* Allocate result buffer with its total size, avoid re-allocation and
6780 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6781 if (join_gap->ga_len >= 2)
6782 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6783 if (ga_grow(gap, sumlen + 2) == FAIL)
6784 return FAIL;
6785
6786 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6787 {
6788 if (first)
6789 first = FALSE;
6790 else
6791 ga_concat(gap, sep);
6792 p = ((join_T *)join_gap->ga_data) + i;
6793
6794 if (p->s != NULL)
6795 ga_concat(gap, p->s);
6796 line_breakcheck();
6797 }
6798
6799 return OK;
6800}
6801
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006802/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006804 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006805 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006807 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006808list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006809 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006810 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006812 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006813 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006814{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006815 garray_T join_ga;
6816 int retval;
6817 join_T *p;
6818 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006819
Bram Moolenaard39a7512015-04-16 22:51:22 +02006820 if (l->lv_len < 1)
6821 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006822 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6823 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6824
6825 /* Dispose each item in join_ga. */
6826 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006827 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006828 p = (join_T *)join_ga.ga_data;
6829 for (i = 0; i < join_ga.ga_len; ++i)
6830 {
6831 vim_free(p->tofree);
6832 ++p;
6833 }
6834 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006835 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006836
6837 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006838}
6839
6840/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006841 * Garbage collection for lists and dictionaries.
6842 *
6843 * We use reference counts to be able to free most items right away when they
6844 * are no longer used. But for composite items it's possible that it becomes
6845 * unused while the reference count is > 0: When there is a recursive
6846 * reference. Example:
6847 * :let l = [1, 2, 3]
6848 * :let d = {9: l}
6849 * :let l[1] = d
6850 *
6851 * Since this is quite unusual we handle this with garbage collection: every
6852 * once in a while find out which lists and dicts are not referenced from any
6853 * variable.
6854 *
6855 * Here is a good reference text about garbage collection (refers to Python
6856 * but it applies to all reference-counting mechanisms):
6857 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006858 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006859
6860/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 * Do garbage collection for lists and dicts.
6862 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006863 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864 int
6865garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006866{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006867 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006868 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869 buf_T *buf;
6870 win_T *wp;
6871 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006872 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006873 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006875#ifdef FEAT_WINDOWS
6876 tabpage_T *tp;
6877#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006879 /* Only do this once. */
6880 want_garbage_collect = FALSE;
6881 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006882 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006883
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006884 /* We advance by two because we add one for items referenced through
6885 * previous_funccal. */
6886 current_copyID += COPYID_INC;
6887 copyID = current_copyID;
6888
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889 /*
6890 * 1. Go through all accessible variables and mark all lists and dicts
6891 * with copyID.
6892 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006893
6894 /* Don't free variables in the previous_funccal list unless they are only
6895 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006896 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006897 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6898 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006899 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6900 NULL);
6901 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6902 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006903 }
6904
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905 /* script-local variables */
6906 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908
6909 /* buffer-local variables */
6910 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006911 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6912 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913
6914 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006915 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006916 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6917 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006918#ifdef FEAT_AUTOCMD
6919 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006920 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6921 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006922#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006924#ifdef FEAT_WINDOWS
6925 /* tabpage-local variables */
6926 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6928 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006929#endif
6930
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006933
6934 /* function-local variables */
6935 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6936 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006937 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6938 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006939 }
6940
Bram Moolenaard812df62008-11-09 12:46:09 +00006941 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006943
Bram Moolenaar1dced572012-04-05 16:54:08 +02006944#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006945 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006946#endif
6947
Bram Moolenaardb913952012-06-29 12:54:53 +02006948#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006950#endif
6951
6952#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006954#endif
6955
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006956 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006958 /*
6959 * 2. Free lists and dictionaries that are not referenced.
6960 */
6961 did_free = free_unref_items(copyID);
6962
6963 /*
6964 * 3. Check if any funccal can be freed now.
6965 */
6966 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006968 if (can_free_funccal(*pfc, copyID))
6969 {
6970 fc = *pfc;
6971 *pfc = fc->caller;
6972 free_funccal(fc, TRUE);
6973 did_free = TRUE;
6974 did_free_funccal = TRUE;
6975 }
6976 else
6977 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006978 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006979 if (did_free_funccal)
6980 /* When a funccal was freed some more items might be garbage
6981 * collected, so run again. */
6982 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006983 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006984 else if (p_verbose > 0)
6985 {
6986 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6987 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006988
6989 return did_free;
6990}
6991
6992/*
6993 * Free lists and dictionaries that are no longer referenced.
6994 */
6995 static int
6996free_unref_items(copyID)
6997 int copyID;
6998{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006999 dict_T *dd, *dd_next;
7000 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007001 int did_free = FALSE;
7002
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007004 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 */
7006 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007007 {
7008 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007009 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007011 /* Free the Dictionary and ordinary items it contains, but don't
7012 * recurse into Lists and Dictionaries, they will be in the list
7013 * of dicts or list of lists. */
7014 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007017 dd = dd_next;
7018 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019
7020 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007021 * Go through the list of lists and free items without the copyID.
7022 * But don't free a list that has a watcher (used in a for loop), these
7023 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 */
7025 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007026 {
7027 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007028 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7029 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007031 /* Free the List and ordinary items it contains, but don't recurse
7032 * into Lists and Dictionaries, they will be in the list of dicts
7033 * or list of lists. */
7034 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007037 ll = ll_next;
7038 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 return did_free;
7040}
7041
7042/*
7043 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044 * "list_stack" is used to add lists to be marked. Can be NULL.
7045 *
7046 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007047 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048 int
7049set_ref_in_ht(ht, copyID, list_stack)
7050 hashtab_T *ht;
7051 int copyID;
7052 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053{
7054 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007055 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007057 hashtab_T *cur_ht;
7058 ht_stack_T *ht_stack = NULL;
7059 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007061 cur_ht = ht;
7062 for (;;)
7063 {
7064 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007065 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007066 /* Mark each item in the hashtab. If the item contains a hashtab
7067 * it is added to ht_stack, if it contains a list it is added to
7068 * list_stack. */
7069 todo = (int)cur_ht->ht_used;
7070 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7071 if (!HASHITEM_EMPTY(hi))
7072 {
7073 --todo;
7074 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7075 &ht_stack, list_stack);
7076 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007078
7079 if (ht_stack == NULL)
7080 break;
7081
7082 /* take an item from the stack */
7083 cur_ht = ht_stack->ht;
7084 tempitem = ht_stack;
7085 ht_stack = ht_stack->prev;
7086 free(tempitem);
7087 }
7088
7089 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090}
7091
7092/*
7093 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007094 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7095 *
7096 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007097 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007098 int
7099set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100 list_T *l;
7101 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007102 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007103{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 listitem_T *li;
7105 int abort = FALSE;
7106 list_T *cur_l;
7107 list_stack_T *list_stack = NULL;
7108 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007110 cur_l = l;
7111 for (;;)
7112 {
7113 if (!abort)
7114 /* Mark each item in the list. If the item contains a hashtab
7115 * it is added to ht_stack, if it contains a list it is added to
7116 * list_stack. */
7117 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7118 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7119 ht_stack, &list_stack);
7120 if (list_stack == NULL)
7121 break;
7122
7123 /* take an item from the stack */
7124 cur_l = list_stack->list;
7125 tempitem = list_stack;
7126 list_stack = list_stack->prev;
7127 free(tempitem);
7128 }
7129
7130 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007131}
7132
7133/*
7134 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 * "list_stack" is used to add lists to be marked. Can be NULL.
7136 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7137 *
7138 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007139 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007140 int
7141set_ref_in_item(tv, copyID, ht_stack, list_stack)
7142 typval_T *tv;
7143 int copyID;
7144 ht_stack_T **ht_stack;
7145 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007146{
7147 dict_T *dd;
7148 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007149 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007150
7151 switch (tv->v_type)
7152 {
7153 case VAR_DICT:
7154 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007155 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007156 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007157 /* Didn't see this dict yet. */
7158 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007159 if (ht_stack == NULL)
7160 {
7161 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7162 }
7163 else
7164 {
7165 ht_stack_T *newitem = (ht_stack_T*)malloc(
7166 sizeof(ht_stack_T));
7167 if (newitem == NULL)
7168 abort = TRUE;
7169 else
7170 {
7171 newitem->ht = &dd->dv_hashtab;
7172 newitem->prev = *ht_stack;
7173 *ht_stack = newitem;
7174 }
7175 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007176 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007177 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007178
7179 case VAR_LIST:
7180 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007181 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007182 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007183 /* Didn't see this list yet. */
7184 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007185 if (list_stack == NULL)
7186 {
7187 abort = set_ref_in_list(ll, copyID, ht_stack);
7188 }
7189 else
7190 {
7191 list_stack_T *newitem = (list_stack_T*)malloc(
7192 sizeof(list_stack_T));
7193 if (newitem == NULL)
7194 abort = TRUE;
7195 else
7196 {
7197 newitem->list = ll;
7198 newitem->prev = *list_stack;
7199 *list_stack = newitem;
7200 }
7201 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007202 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007203 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007204 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007205 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007206}
7207
7208/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209 * Allocate an empty header for a dictionary.
7210 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007211 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212dict_alloc()
7213{
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007215
Bram Moolenaar33570922005-01-25 22:26:29 +00007216 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007217 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007218 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007219 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007220 if (first_dict != NULL)
7221 first_dict->dv_used_prev = d;
7222 d->dv_used_next = first_dict;
7223 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007224 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007225
Bram Moolenaar33570922005-01-25 22:26:29 +00007226 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007227 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007228 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007229 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007230 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007231 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007232 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233}
7234
7235/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007236 * Allocate an empty dict for a return value.
7237 * Returns OK or FAIL.
7238 */
7239 static int
7240rettv_dict_alloc(rettv)
7241 typval_T *rettv;
7242{
7243 dict_T *d = dict_alloc();
7244
7245 if (d == NULL)
7246 return FAIL;
7247
7248 rettv->vval.v_dict = d;
7249 rettv->v_type = VAR_DICT;
7250 ++d->dv_refcount;
7251 return OK;
7252}
7253
7254
7255/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256 * Unreference a Dictionary: decrement the reference count and free it when it
7257 * becomes zero.
7258 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007259 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007261 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007263 if (d != NULL && --d->dv_refcount <= 0)
7264 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265}
7266
7267/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007268 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269 * Ignores the reference count.
7270 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007271 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007272dict_free(d, recurse)
7273 dict_T *d;
7274 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007276 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007278 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007279
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007280 /* Remove the dict from the list of dicts for garbage collection. */
7281 if (d->dv_used_prev == NULL)
7282 first_dict = d->dv_used_next;
7283 else
7284 d->dv_used_prev->dv_used_next = d->dv_used_next;
7285 if (d->dv_used_next != NULL)
7286 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7287
7288 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007289 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007290 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007291 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293 if (!HASHITEM_EMPTY(hi))
7294 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007295 /* Remove the item before deleting it, just in case there is
7296 * something recursive causing trouble. */
7297 di = HI2DI(hi);
7298 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007299 if (recurse || (di->di_tv.v_type != VAR_LIST
7300 && di->di_tv.v_type != VAR_DICT))
7301 clear_tv(&di->di_tv);
7302 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303 --todo;
7304 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007306 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007307 vim_free(d);
7308}
7309
7310/*
7311 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312 * The "key" is copied to the new item.
7313 * Note that the value of the item "di_tv" still needs to be initialized!
7314 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007316 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317dictitem_alloc(key)
7318 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007319{
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007322 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007324 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007326 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007327 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329}
7330
7331/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332 * Make a copy of a Dictionary item.
7333 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007337{
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007340 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7341 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342 if (di != NULL)
7343 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007344 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007345 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007346 copy_tv(&org->di_tv, &di->di_tv);
7347 }
7348 return di;
7349}
7350
7351/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007352 * Remove item "item" from Dictionary "dict" and free it.
7353 */
7354 static void
7355dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 dict_T *dict;
7357 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007358{
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007360
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362 if (HASHITEM_EMPTY(hi))
7363 EMSG2(_(e_intern2), "dictitem_remove()");
7364 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366 dictitem_free(item);
7367}
7368
7369/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007370 * Free a dict item. Also clears the value.
7371 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007372 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007373dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007375{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007377 if (item->di_flags & DI_FLAGS_ALLOC)
7378 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379}
7380
7381/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7383 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007384 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007385 * Returns NULL when out of memory.
7386 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007388dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007391 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392{
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 dict_T *copy;
7394 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007395 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007396 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007397
7398 if (orig == NULL)
7399 return NULL;
7400
7401 copy = dict_alloc();
7402 if (copy != NULL)
7403 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007404 if (copyID != 0)
7405 {
7406 orig->dv_copyID = copyID;
7407 orig->dv_copydict = copy;
7408 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007409 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007410 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007411 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007412 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007414 --todo;
7415
7416 di = dictitem_alloc(hi->hi_key);
7417 if (di == NULL)
7418 break;
7419 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007420 {
7421 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7422 copyID) == FAIL)
7423 {
7424 vim_free(di);
7425 break;
7426 }
7427 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007428 else
7429 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7430 if (dict_add(copy, di) == FAIL)
7431 {
7432 dictitem_free(di);
7433 break;
7434 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007435 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007436 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007437
Bram Moolenaare9a41262005-01-15 22:18:47 +00007438 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007439 if (todo > 0)
7440 {
7441 dict_unref(copy);
7442 copy = NULL;
7443 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007444 }
7445
7446 return copy;
7447}
7448
7449/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007450 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007451 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007453 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 dict_T *d;
7456 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007457{
Bram Moolenaar33570922005-01-25 22:26:29 +00007458 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007459}
7460
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007462 * Add a number or string entry to dictionary "d".
7463 * When "str" is NULL use number "nr", otherwise use "str".
7464 * Returns FAIL when out of memory and when key already exists.
7465 */
7466 int
7467dict_add_nr_str(d, key, nr, str)
7468 dict_T *d;
7469 char *key;
7470 long nr;
7471 char_u *str;
7472{
7473 dictitem_T *item;
7474
7475 item = dictitem_alloc((char_u *)key);
7476 if (item == NULL)
7477 return FAIL;
7478 item->di_tv.v_lock = 0;
7479 if (str == NULL)
7480 {
7481 item->di_tv.v_type = VAR_NUMBER;
7482 item->di_tv.vval.v_number = nr;
7483 }
7484 else
7485 {
7486 item->di_tv.v_type = VAR_STRING;
7487 item->di_tv.vval.v_string = vim_strsave(str);
7488 }
7489 if (dict_add(d, item) == FAIL)
7490 {
7491 dictitem_free(item);
7492 return FAIL;
7493 }
7494 return OK;
7495}
7496
7497/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007498 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007499 * Returns FAIL when out of memory and when key already exists.
7500 */
7501 int
7502dict_add_list(d, key, list)
7503 dict_T *d;
7504 char *key;
7505 list_T *list;
7506{
7507 dictitem_T *item;
7508
7509 item = dictitem_alloc((char_u *)key);
7510 if (item == NULL)
7511 return FAIL;
7512 item->di_tv.v_lock = 0;
7513 item->di_tv.v_type = VAR_LIST;
7514 item->di_tv.vval.v_list = list;
7515 if (dict_add(d, item) == FAIL)
7516 {
7517 dictitem_free(item);
7518 return FAIL;
7519 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007520 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007521 return OK;
7522}
7523
7524/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007525 * Get the number of items in a Dictionary.
7526 */
7527 static long
7528dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007530{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007531 if (d == NULL)
7532 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007533 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007534}
7535
7536/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 * Find item "key[len]" in Dictionary "d".
7538 * If "len" is negative use strlen(key).
7539 * Returns NULL when not found.
7540 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007541 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007542dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007544 char_u *key;
7545 int len;
7546{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547#define AKEYLEN 200
7548 char_u buf[AKEYLEN];
7549 char_u *akey;
7550 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007551 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007553 if (len < 0)
7554 akey = key;
7555 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007556 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007557 tofree = akey = vim_strnsave(key, len);
7558 if (akey == NULL)
7559 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007560 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007561 else
7562 {
7563 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007564 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 akey = buf;
7566 }
7567
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007569 vim_free(tofree);
7570 if (HASHITEM_EMPTY(hi))
7571 return NULL;
7572 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573}
7574
7575/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007576 * Get a string item from a dictionary.
7577 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007578 * Returns NULL if the entry doesn't exist or out of memory.
7579 */
7580 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007581get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007582 dict_T *d;
7583 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007584 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007585{
7586 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007587 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007588
7589 di = dict_find(d, key, -1);
7590 if (di == NULL)
7591 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007592 s = get_tv_string(&di->di_tv);
7593 if (save && s != NULL)
7594 s = vim_strsave(s);
7595 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007596}
7597
7598/*
7599 * Get a number item from a dictionary.
7600 * Returns 0 if the entry doesn't exist or out of memory.
7601 */
7602 long
7603get_dict_number(d, key)
7604 dict_T *d;
7605 char_u *key;
7606{
7607 dictitem_T *di;
7608
7609 di = dict_find(d, key, -1);
7610 if (di == NULL)
7611 return 0;
7612 return get_tv_number(&di->di_tv);
7613}
7614
7615/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007616 * Return an allocated string with the string representation of a Dictionary.
7617 * May return NULL.
7618 */
7619 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007622 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623{
7624 garray_T ga;
7625 int first = TRUE;
7626 char_u *tofree;
7627 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007631 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007632
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007633 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634 return NULL;
7635 ga_init2(&ga, (int)sizeof(char), 80);
7636 ga_append(&ga, '{');
7637
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007638 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007639 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007641 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007642 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007643 --todo;
7644
7645 if (first)
7646 first = FALSE;
7647 else
7648 ga_concat(&ga, (char_u *)", ");
7649
7650 tofree = string_quote(hi->hi_key, FALSE);
7651 if (tofree != NULL)
7652 {
7653 ga_concat(&ga, tofree);
7654 vim_free(tofree);
7655 }
7656 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007657 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007658 if (s != NULL)
7659 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007661 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007662 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007663 line_breakcheck();
7664
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007666 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007667 if (todo > 0)
7668 {
7669 vim_free(ga.ga_data);
7670 return NULL;
7671 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672
7673 ga_append(&ga, '}');
7674 ga_append(&ga, NUL);
7675 return (char_u *)ga.ga_data;
7676}
7677
7678/*
7679 * Allocate a variable for a Dictionary and fill it from "*arg".
7680 * Return OK or FAIL. Returns NOTDONE for {expr}.
7681 */
7682 static int
7683get_dict_tv(arg, rettv, evaluate)
7684 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686 int evaluate;
7687{
Bram Moolenaar33570922005-01-25 22:26:29 +00007688 dict_T *d = NULL;
7689 typval_T tvkey;
7690 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007691 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007694 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695
7696 /*
7697 * First check if it's not a curly-braces thing: {expr}.
7698 * Must do this without evaluating, otherwise a function may be called
7699 * twice. Unfortunately this means we need to call eval1() twice for the
7700 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007701 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007703 if (*start != '}')
7704 {
7705 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7706 return FAIL;
7707 if (*start == '}')
7708 return NOTDONE;
7709 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710
7711 if (evaluate)
7712 {
7713 d = dict_alloc();
7714 if (d == NULL)
7715 return FAIL;
7716 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007717 tvkey.v_type = VAR_UNKNOWN;
7718 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719
7720 *arg = skipwhite(*arg + 1);
7721 while (**arg != '}' && **arg != NUL)
7722 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007723 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724 goto failret;
7725 if (**arg != ':')
7726 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007727 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007728 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 goto failret;
7730 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007731 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007733 key = get_tv_string_buf_chk(&tvkey, buf);
7734 if (key == NULL || *key == NUL)
7735 {
7736 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7737 if (key != NULL)
7738 EMSG(_(e_emptykey));
7739 clear_tv(&tvkey);
7740 goto failret;
7741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007742 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007743
7744 *arg = skipwhite(*arg + 1);
7745 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7746 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007747 if (evaluate)
7748 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007749 goto failret;
7750 }
7751 if (evaluate)
7752 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007753 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007754 if (item != NULL)
7755 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007756 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007757 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 clear_tv(&tv);
7759 goto failret;
7760 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007761 item = dictitem_alloc(key);
7762 clear_tv(&tvkey);
7763 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007765 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007766 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007767 if (dict_add(d, item) == FAIL)
7768 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007769 }
7770 }
7771
7772 if (**arg == '}')
7773 break;
7774 if (**arg != ',')
7775 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007776 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007777 goto failret;
7778 }
7779 *arg = skipwhite(*arg + 1);
7780 }
7781
7782 if (**arg != '}')
7783 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007784 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007785failret:
7786 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007787 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007788 return FAIL;
7789 }
7790
7791 *arg = skipwhite(*arg + 1);
7792 if (evaluate)
7793 {
7794 rettv->v_type = VAR_DICT;
7795 rettv->vval.v_dict = d;
7796 ++d->dv_refcount;
7797 }
7798
7799 return OK;
7800}
7801
Bram Moolenaar8c711452005-01-14 21:53:12 +00007802/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007803 * Return a string with the string representation of a variable.
7804 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007805 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007806 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007807 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007808 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007809 */
7810 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007811echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007812 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007814 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007815 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007816{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007817 static int recurse = 0;
7818 char_u *r = NULL;
7819
Bram Moolenaar33570922005-01-25 22:26:29 +00007820 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007821 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007822 if (!did_echo_string_emsg)
7823 {
7824 /* Only give this message once for a recursive call to avoid
7825 * flooding the user with errors. And stop iterating over lists
7826 * and dicts. */
7827 did_echo_string_emsg = TRUE;
7828 EMSG(_("E724: variable nested too deep for displaying"));
7829 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007830 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007831 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007832 }
7833 ++recurse;
7834
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007835 switch (tv->v_type)
7836 {
7837 case VAR_FUNC:
7838 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007839 r = tv->vval.v_string;
7840 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007841
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007842 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007843 if (tv->vval.v_list == NULL)
7844 {
7845 *tofree = NULL;
7846 r = NULL;
7847 }
7848 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7849 {
7850 *tofree = NULL;
7851 r = (char_u *)"[...]";
7852 }
7853 else
7854 {
7855 tv->vval.v_list->lv_copyID = copyID;
7856 *tofree = list2string(tv, copyID);
7857 r = *tofree;
7858 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007859 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007860
Bram Moolenaar8c711452005-01-14 21:53:12 +00007861 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007862 if (tv->vval.v_dict == NULL)
7863 {
7864 *tofree = NULL;
7865 r = NULL;
7866 }
7867 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7868 {
7869 *tofree = NULL;
7870 r = (char_u *)"{...}";
7871 }
7872 else
7873 {
7874 tv->vval.v_dict->dv_copyID = copyID;
7875 *tofree = dict2string(tv, copyID);
7876 r = *tofree;
7877 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007878 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007879
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007880 case VAR_STRING:
7881 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007882 *tofree = NULL;
7883 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007884 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007885
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007886#ifdef FEAT_FLOAT
7887 case VAR_FLOAT:
7888 *tofree = NULL;
7889 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7890 r = numbuf;
7891 break;
7892#endif
7893
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007895 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007896 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898
Bram Moolenaar8502c702014-06-17 12:51:16 +02007899 if (--recurse == 0)
7900 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007901 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007902}
7903
7904/*
7905 * Return a string with the string representation of a variable.
7906 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7907 * "numbuf" is used for a number.
7908 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007909 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007910 */
7911 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007912tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007913 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914 char_u **tofree;
7915 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007916 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007917{
7918 switch (tv->v_type)
7919 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007920 case VAR_FUNC:
7921 *tofree = string_quote(tv->vval.v_string, TRUE);
7922 return *tofree;
7923 case VAR_STRING:
7924 *tofree = string_quote(tv->vval.v_string, FALSE);
7925 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007926#ifdef FEAT_FLOAT
7927 case VAR_FLOAT:
7928 *tofree = NULL;
7929 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7930 return numbuf;
7931#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007932 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007933 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007935 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007936 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007937 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007938 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007939 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007940}
7941
7942/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 * Return string "str" in ' quotes, doubling ' characters.
7944 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007945 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007946 */
7947 static char_u *
7948string_quote(str, function)
7949 char_u *str;
7950 int function;
7951{
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007953 char_u *p, *r, *s;
7954
Bram Moolenaar33570922005-01-25 22:26:29 +00007955 len = (function ? 13 : 3);
7956 if (str != NULL)
7957 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007958 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007959 for (p = str; *p != NUL; mb_ptr_adv(p))
7960 if (*p == '\'')
7961 ++len;
7962 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007963 s = r = alloc(len);
7964 if (r != NULL)
7965 {
7966 if (function)
7967 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007968 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007969 r += 10;
7970 }
7971 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007972 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007973 if (str != NULL)
7974 for (p = str; *p != NUL; )
7975 {
7976 if (*p == '\'')
7977 *r++ = '\'';
7978 MB_COPY_CHAR(p, r);
7979 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007980 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007981 if (function)
7982 *r++ = ')';
7983 *r++ = NUL;
7984 }
7985 return s;
7986}
7987
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007988#ifdef FEAT_FLOAT
7989/*
7990 * Convert the string "text" to a floating point number.
7991 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7992 * this always uses a decimal point.
7993 * Returns the length of the text that was consumed.
7994 */
7995 static int
7996string2float(text, value)
7997 char_u *text;
7998 float_T *value; /* result stored here */
7999{
8000 char *s = (char *)text;
8001 float_T f;
8002
8003 f = strtod(s, &s);
8004 *value = f;
8005 return (int)((char_u *)s - text);
8006}
8007#endif
8008
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 * Get the value of an environment variable.
8011 * "arg" is pointing to the '$'. It is advanced to after the name.
8012 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008013 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 */
8015 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008016get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 int evaluate;
8020{
8021 char_u *string = NULL;
8022 int len;
8023 int cc;
8024 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008025 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026
8027 ++*arg;
8028 name = *arg;
8029 len = get_env_len(arg);
8030 if (evaluate)
8031 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008032 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008033 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008034
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008035 cc = name[len];
8036 name[len] = NUL;
8037 /* first try vim_getenv(), fast for normal environment vars */
8038 string = vim_getenv(name, &mustfree);
8039 if (string != NULL && *string != NUL)
8040 {
8041 if (!mustfree)
8042 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008044 else
8045 {
8046 if (mustfree)
8047 vim_free(string);
8048
8049 /* next try expanding things like $VIM and ${HOME} */
8050 string = expand_env_save(name - 1);
8051 if (string != NULL && *string == '$')
8052 {
8053 vim_free(string);
8054 string = NULL;
8055 }
8056 }
8057 name[len] = cc;
8058
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008059 rettv->v_type = VAR_STRING;
8060 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 }
8062
8063 return OK;
8064}
8065
8066/*
8067 * Array with names and number of arguments of all internal functions
8068 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8069 */
8070static struct fst
8071{
8072 char *f_name; /* function name */
8073 char f_min_argc; /* minimal number of arguments */
8074 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008075 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008076 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077} functions[] =
8078{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079#ifdef FEAT_FLOAT
8080 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008081 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008082#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008083 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008084 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008085 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {"append", 2, 2, f_append},
8087 {"argc", 0, 0, f_argc},
8088 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008089 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008090 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008091#ifdef FEAT_FLOAT
8092 {"asin", 1, 1, f_asin}, /* WJMc */
8093#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008094 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008095 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008096 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008097 {"assert_false", 1, 2, f_assert_false},
8098 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099#ifdef FEAT_FLOAT
8100 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008101 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008104 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 {"bufexists", 1, 1, f_bufexists},
8106 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8107 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8108 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8109 {"buflisted", 1, 1, f_buflisted},
8110 {"bufloaded", 1, 1, f_bufloaded},
8111 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008112 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {"bufwinnr", 1, 1, f_bufwinnr},
8114 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008115 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008116 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008117 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008118#ifdef FEAT_FLOAT
8119 {"ceil", 1, 1, f_ceil},
8120#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008121 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008122 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008124 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008126#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008127 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008128 {"complete_add", 1, 1, f_complete_add},
8129 {"complete_check", 0, 0, f_complete_check},
8130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008132 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008133#ifdef FEAT_FLOAT
8134 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008135 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008136#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008137 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008139 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008140 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008141 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008143 {"diff_filler", 1, 1, f_diff_filler},
8144 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008145 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008147 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"eventhandler", 0, 0, f_eventhandler},
8149 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008150 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008152#ifdef FEAT_FLOAT
8153 {"exp", 1, 1, f_exp},
8154#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008155 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008156 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008157 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8159 {"filereadable", 1, 1, f_filereadable},
8160 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008161 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008162 {"finddir", 1, 3, f_finddir},
8163 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008164#ifdef FEAT_FLOAT
8165 {"float2nr", 1, 1, f_float2nr},
8166 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008167 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008168#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008169 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"fnamemodify", 2, 2, f_fnamemodify},
8171 {"foldclosed", 1, 1, f_foldclosed},
8172 {"foldclosedend", 1, 1, f_foldclosedend},
8173 {"foldlevel", 1, 1, f_foldlevel},
8174 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008175 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008177 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008178 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008179 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008180 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008181 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"getchar", 0, 1, f_getchar},
8183 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008184 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {"getcmdline", 0, 0, f_getcmdline},
8186 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008187 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008188 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008189 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008190 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008191 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008192 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {"getfsize", 1, 1, f_getfsize},
8194 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008195 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008196 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008197 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008198 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008199 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008200 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008201 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008202 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008204 {"gettabvar", 2, 3, f_gettabvar},
8205 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {"getwinposx", 0, 0, f_getwinposx},
8207 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008208 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008209 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008210 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008211 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008213 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008214 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008215 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8217 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8218 {"histadd", 2, 2, f_histadd},
8219 {"histdel", 1, 2, f_histdel},
8220 {"histget", 1, 2, f_histget},
8221 {"histnr", 1, 1, f_histnr},
8222 {"hlID", 1, 1, f_hlID},
8223 {"hlexists", 1, 1, f_hlexists},
8224 {"hostname", 0, 0, f_hostname},
8225 {"iconv", 3, 3, f_iconv},
8226 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008227 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008228 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008230 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {"inputrestore", 0, 0, f_inputrestore},
8232 {"inputsave", 0, 0, f_inputsave},
8233 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008234 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008235 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008237 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008238 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008239 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008240 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008242 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {"libcall", 3, 3, f_libcall},
8244 {"libcallnr", 3, 3, f_libcallnr},
8245 {"line", 1, 1, f_line},
8246 {"line2byte", 1, 1, f_line2byte},
8247 {"lispindent", 1, 1, f_lispindent},
8248 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008249#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008250 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008251 {"log10", 1, 1, f_log10},
8252#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008253#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008254 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008255#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008256 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008257 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008258 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008259 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008260 {"matchadd", 2, 5, f_matchadd},
8261 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008262 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008263 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008264 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008265 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008266 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008267 {"max", 1, 1, f_max},
8268 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008269#ifdef vim_mkdir
8270 {"mkdir", 1, 3, f_mkdir},
8271#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008272 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008273#ifdef FEAT_MZSCHEME
8274 {"mzeval", 1, 1, f_mzeval},
8275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008277 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008278 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008279 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008280#ifdef FEAT_PERL
8281 {"perleval", 1, 1, f_perleval},
8282#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008283#ifdef FEAT_FLOAT
8284 {"pow", 2, 2, f_pow},
8285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008287 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008288 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008289#ifdef FEAT_PYTHON3
8290 {"py3eval", 1, 1, f_py3eval},
8291#endif
8292#ifdef FEAT_PYTHON
8293 {"pyeval", 1, 1, f_pyeval},
8294#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008295 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008296 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008297 {"reltime", 0, 2, f_reltime},
8298 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {"remote_expr", 2, 3, f_remote_expr},
8300 {"remote_foreground", 1, 1, f_remote_foreground},
8301 {"remote_peek", 1, 2, f_remote_peek},
8302 {"remote_read", 1, 1, f_remote_read},
8303 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008304 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008306 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008308 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008309#ifdef FEAT_FLOAT
8310 {"round", 1, 1, f_round},
8311#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008312 {"screenattr", 2, 2, f_screenattr},
8313 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008314 {"screencol", 0, 0, f_screencol},
8315 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008316 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008317 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008318 {"searchpair", 3, 7, f_searchpair},
8319 {"searchpairpos", 3, 7, f_searchpairpos},
8320 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {"server2client", 2, 2, f_server2client},
8322 {"serverlist", 0, 0, f_serverlist},
8323 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008324 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {"setcmdpos", 1, 1, f_setcmdpos},
8326 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008327 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008328 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008329 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008330 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008332 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008333 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008335#ifdef FEAT_CRYPT
8336 {"sha256", 1, 1, f_sha256},
8337#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008338 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008339 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008341#ifdef FEAT_FLOAT
8342 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008343 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008344#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008345 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008346 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008347 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008348 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008349 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008350#ifdef FEAT_FLOAT
8351 {"sqrt", 1, 1, f_sqrt},
8352 {"str2float", 1, 1, f_str2float},
8353#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008354 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008355 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008356 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357#ifdef HAVE_STRFTIME
8358 {"strftime", 1, 2, f_strftime},
8359#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008360 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008361 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {"strlen", 1, 1, f_strlen},
8363 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008364 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008366 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008367 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 {"substitute", 4, 4, f_substitute},
8369 {"synID", 3, 3, f_synID},
8370 {"synIDattr", 2, 3, f_synIDattr},
8371 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008372 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008373 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008374 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008375 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008376 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008377 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008378 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008379 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008380 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008381#ifdef FEAT_FLOAT
8382 {"tan", 1, 1, f_tan},
8383 {"tanh", 1, 1, f_tanh},
8384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008386 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {"tolower", 1, 1, f_tolower},
8388 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008389 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008390#ifdef FEAT_FLOAT
8391 {"trunc", 1, 1, f_trunc},
8392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008394 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008395 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008396 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008397 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 {"virtcol", 1, 1, f_virtcol},
8399 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008400 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 {"winbufnr", 1, 1, f_winbufnr},
8402 {"wincol", 0, 0, f_wincol},
8403 {"winheight", 1, 1, f_winheight},
8404 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008405 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008407 {"winrestview", 1, 1, f_winrestview},
8408 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008410 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008411 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008412 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413};
8414
8415#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8416
8417/*
8418 * Function given to ExpandGeneric() to obtain the list of internal
8419 * or user defined function names.
8420 */
8421 char_u *
8422get_function_name(xp, idx)
8423 expand_T *xp;
8424 int idx;
8425{
8426 static int intidx = -1;
8427 char_u *name;
8428
8429 if (idx == 0)
8430 intidx = -1;
8431 if (intidx < 0)
8432 {
8433 name = get_user_func_name(xp, idx);
8434 if (name != NULL)
8435 return name;
8436 }
8437 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8438 {
8439 STRCPY(IObuff, functions[intidx].f_name);
8440 STRCAT(IObuff, "(");
8441 if (functions[intidx].f_max_argc == 0)
8442 STRCAT(IObuff, ")");
8443 return IObuff;
8444 }
8445
8446 return NULL;
8447}
8448
8449/*
8450 * Function given to ExpandGeneric() to obtain the list of internal or
8451 * user defined variable or function names.
8452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 char_u *
8454get_expr_name(xp, idx)
8455 expand_T *xp;
8456 int idx;
8457{
8458 static int intidx = -1;
8459 char_u *name;
8460
8461 if (idx == 0)
8462 intidx = -1;
8463 if (intidx < 0)
8464 {
8465 name = get_function_name(xp, idx);
8466 if (name != NULL)
8467 return name;
8468 }
8469 return get_user_var_name(xp, ++intidx);
8470}
8471
8472#endif /* FEAT_CMDL_COMPL */
8473
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008474#if defined(EBCDIC) || defined(PROTO)
8475/*
8476 * Compare struct fst by function name.
8477 */
8478 static int
8479compare_func_name(s1, s2)
8480 const void *s1;
8481 const void *s2;
8482{
8483 struct fst *p1 = (struct fst *)s1;
8484 struct fst *p2 = (struct fst *)s2;
8485
8486 return STRCMP(p1->f_name, p2->f_name);
8487}
8488
8489/*
8490 * Sort the function table by function name.
8491 * The sorting of the table above is ASCII dependant.
8492 * On machines using EBCDIC we have to sort it.
8493 */
8494 static void
8495sortFunctions()
8496{
8497 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8498
8499 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8500}
8501#endif
8502
8503
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504/*
8505 * Find internal function in table above.
8506 * Return index, or -1 if not found
8507 */
8508 static int
8509find_internal_func(name)
8510 char_u *name; /* name of the function */
8511{
8512 int first = 0;
8513 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8514 int cmp;
8515 int x;
8516
8517 /*
8518 * Find the function name in the table. Binary search.
8519 */
8520 while (first <= last)
8521 {
8522 x = first + ((unsigned)(last - first) >> 1);
8523 cmp = STRCMP(name, functions[x].f_name);
8524 if (cmp < 0)
8525 last = x - 1;
8526 else if (cmp > 0)
8527 first = x + 1;
8528 else
8529 return x;
8530 }
8531 return -1;
8532}
8533
8534/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008535 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8536 * name it contains, otherwise return "name".
8537 */
8538 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008539deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008540 char_u *name;
8541 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008542 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008543{
Bram Moolenaar33570922005-01-25 22:26:29 +00008544 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008545 int cc;
8546
8547 cc = name[*lenp];
8548 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008549 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008550 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008551 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008552 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008553 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008554 {
8555 *lenp = 0;
8556 return (char_u *)""; /* just in case */
8557 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008558 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008559 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008560 }
8561
8562 return name;
8563}
8564
8565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 * Allocate a variable for the result of a function.
8567 * Return OK or FAIL.
8568 */
8569 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008570get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8571 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 char_u *name; /* name of the function */
8573 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 char_u **arg; /* argument, pointing to the '(' */
8576 linenr_T firstline; /* first line of range */
8577 linenr_T lastline; /* last line of range */
8578 int *doesrange; /* return: function handled range */
8579 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008580 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581{
8582 char_u *argp;
8583 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008584 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 int argcount = 0; /* number of arguments found */
8586
8587 /*
8588 * Get the arguments.
8589 */
8590 argp = *arg;
8591 while (argcount < MAX_FUNC_ARGS)
8592 {
8593 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8594 if (*argp == ')' || *argp == ',' || *argp == NUL)
8595 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8597 {
8598 ret = FAIL;
8599 break;
8600 }
8601 ++argcount;
8602 if (*argp != ',')
8603 break;
8604 }
8605 if (*argp == ')')
8606 ++argp;
8607 else
8608 ret = FAIL;
8609
8610 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008611 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008612 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008614 {
8615 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008616 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008617 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008618 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620
8621 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623
8624 *arg = skipwhite(argp);
8625 return ret;
8626}
8627
8628
8629/*
8630 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008631 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008632 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 */
8634 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008635call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008636 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008637 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008639 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008641 typval_T *argvars; /* vars for arguments, must have "argcount"
8642 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 linenr_T firstline; /* first line of range */
8644 linenr_T lastline; /* last line of range */
8645 int *doesrange; /* return: function handled range */
8646 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008647 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648{
8649 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650#define ERROR_UNKNOWN 0
8651#define ERROR_TOOMANY 1
8652#define ERROR_TOOFEW 2
8653#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008654#define ERROR_DICT 4
8655#define ERROR_NONE 5
8656#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 int error = ERROR_NONE;
8658 int i;
8659 int llen;
8660 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661#define FLEN_FIXED 40
8662 char_u fname_buf[FLEN_FIXED + 1];
8663 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008664 char_u *name;
8665
8666 /* Make a copy of the name, if it comes from a funcref variable it could
8667 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008668 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008669 if (name == NULL)
8670 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671
8672 /*
8673 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8674 * Change <SNR>123_name() to K_SNR 123_name().
8675 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 llen = eval_fname_script(name);
8678 if (llen > 0)
8679 {
8680 fname_buf[0] = K_SPECIAL;
8681 fname_buf[1] = KS_EXTRA;
8682 fname_buf[2] = (int)KE_SNR;
8683 i = 3;
8684 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8685 {
8686 if (current_SID <= 0)
8687 error = ERROR_SCRIPT;
8688 else
8689 {
8690 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8691 i = (int)STRLEN(fname_buf);
8692 }
8693 }
8694 if (i + STRLEN(name + llen) < FLEN_FIXED)
8695 {
8696 STRCPY(fname_buf + i, name + llen);
8697 fname = fname_buf;
8698 }
8699 else
8700 {
8701 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8702 if (fname == NULL)
8703 error = ERROR_OTHER;
8704 else
8705 {
8706 mch_memmove(fname, fname_buf, (size_t)i);
8707 STRCPY(fname + i, name + llen);
8708 }
8709 }
8710 }
8711 else
8712 fname = name;
8713
8714 *doesrange = FALSE;
8715
8716
8717 /* execute the function if no errors detected and executing */
8718 if (evaluate && error == ERROR_NONE)
8719 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008720 char_u *rfname = fname;
8721
8722 /* Ignore "g:" before a function name. */
8723 if (fname[0] == 'g' && fname[1] == ':')
8724 rfname = fname + 2;
8725
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008726 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8727 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 error = ERROR_UNKNOWN;
8729
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008730 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {
8732 /*
8733 * User defined function.
8734 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008735 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008736
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008738 /* Trigger FuncUndefined event, may load the function. */
8739 if (fp == NULL
8740 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008741 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008742 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008744 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008745 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 }
8747#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008748 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008749 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008750 {
8751 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008752 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008753 }
8754
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 if (fp != NULL)
8756 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008757 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008759 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008761 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008763 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008764 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 else
8766 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008767 int did_save_redo = FALSE;
8768
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 /*
8770 * Call the user function.
8771 * Save and restore search patterns, script variables and
8772 * redo buffer.
8773 */
8774 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008775#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008776 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008777#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008778 {
8779 saveRedobuff();
8780 did_save_redo = TRUE;
8781 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008782 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008783 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008784 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008785 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8786 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8787 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008788 /* Function was unreferenced while being used, free it
8789 * now. */
8790 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008791 if (did_save_redo)
8792 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 restore_search_patterns();
8794 error = ERROR_NONE;
8795 }
8796 }
8797 }
8798 else
8799 {
8800 /*
8801 * Find the function name in the table, call its implementation.
8802 */
8803 i = find_internal_func(fname);
8804 if (i >= 0)
8805 {
8806 if (argcount < functions[i].f_min_argc)
8807 error = ERROR_TOOFEW;
8808 else if (argcount > functions[i].f_max_argc)
8809 error = ERROR_TOOMANY;
8810 else
8811 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008812 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008813 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 error = ERROR_NONE;
8815 }
8816 }
8817 }
8818 /*
8819 * The function call (or "FuncUndefined" autocommand sequence) might
8820 * have been aborted by an error, an interrupt, or an explicitly thrown
8821 * exception that has not been caught so far. This situation can be
8822 * tested for by calling aborting(). For an error in an internal
8823 * function or for the "E132" error in call_user_func(), however, the
8824 * throw point at which the "force_abort" flag (temporarily reset by
8825 * emsg()) is normally updated has not been reached yet. We need to
8826 * update that flag first to make aborting() reliable.
8827 */
8828 update_force_abort();
8829 }
8830 if (error == ERROR_NONE)
8831 ret = OK;
8832
8833 /*
8834 * Report an error unless the argument evaluation or function call has been
8835 * cancelled due to an aborting error, an interrupt, or an exception.
8836 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008837 if (!aborting())
8838 {
8839 switch (error)
8840 {
8841 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008842 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008843 break;
8844 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008845 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008846 break;
8847 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008848 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008849 name);
8850 break;
8851 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008852 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008853 name);
8854 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008855 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008856 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008857 name);
8858 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008859 }
8860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 if (fname != name && fname != fname_buf)
8863 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008864 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865
8866 return ret;
8867}
8868
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008869/*
8870 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008871 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008872 */
8873 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008874emsg_funcname(ermsg, name)
8875 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008876 char_u *name;
8877{
8878 char_u *p;
8879
8880 if (*name == K_SPECIAL)
8881 p = concat_str((char_u *)"<SNR>", name + 3);
8882 else
8883 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008884 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008885 if (p != name)
8886 vim_free(p);
8887}
8888
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008889/*
8890 * Return TRUE for a non-zero Number and a non-empty String.
8891 */
8892 static int
8893non_zero_arg(argvars)
8894 typval_T *argvars;
8895{
8896 return ((argvars[0].v_type == VAR_NUMBER
8897 && argvars[0].vval.v_number != 0)
8898 || (argvars[0].v_type == VAR_STRING
8899 && argvars[0].vval.v_string != NULL
8900 && *argvars[0].vval.v_string != NUL));
8901}
8902
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903/*********************************************
8904 * Implementation of the built-in functions
8905 */
8906
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008907#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008908static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8909
8910/*
8911 * Get the float value of "argvars[0]" into "f".
8912 * Returns FAIL when the argument is not a Number or Float.
8913 */
8914 static int
8915get_float_arg(argvars, f)
8916 typval_T *argvars;
8917 float_T *f;
8918{
8919 if (argvars[0].v_type == VAR_FLOAT)
8920 {
8921 *f = argvars[0].vval.v_float;
8922 return OK;
8923 }
8924 if (argvars[0].v_type == VAR_NUMBER)
8925 {
8926 *f = (float_T)argvars[0].vval.v_number;
8927 return OK;
8928 }
8929 EMSG(_("E808: Number or Float required"));
8930 return FAIL;
8931}
8932
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008933/*
8934 * "abs(expr)" function
8935 */
8936 static void
8937f_abs(argvars, rettv)
8938 typval_T *argvars;
8939 typval_T *rettv;
8940{
8941 if (argvars[0].v_type == VAR_FLOAT)
8942 {
8943 rettv->v_type = VAR_FLOAT;
8944 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8945 }
8946 else
8947 {
8948 varnumber_T n;
8949 int error = FALSE;
8950
8951 n = get_tv_number_chk(&argvars[0], &error);
8952 if (error)
8953 rettv->vval.v_number = -1;
8954 else if (n > 0)
8955 rettv->vval.v_number = n;
8956 else
8957 rettv->vval.v_number = -n;
8958 }
8959}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008960
8961/*
8962 * "acos()" function
8963 */
8964 static void
8965f_acos(argvars, rettv)
8966 typval_T *argvars;
8967 typval_T *rettv;
8968{
8969 float_T f;
8970
8971 rettv->v_type = VAR_FLOAT;
8972 if (get_float_arg(argvars, &f) == OK)
8973 rettv->vval.v_float = acos(f);
8974 else
8975 rettv->vval.v_float = 0.0;
8976}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008977#endif
8978
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008980 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 */
8982 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008983f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008984 typval_T *argvars;
8985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986{
Bram Moolenaar33570922005-01-25 22:26:29 +00008987 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008990 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008992 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008993 && !tv_check_lock(l->lv_lock,
8994 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008995 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008997 }
8998 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008999 EMSG(_(e_listreq));
9000}
9001
9002/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009003 * "alloc_fail(id, countdown, repeat)" function
9004 */
9005 static void
9006f_alloc_fail(argvars, rettv)
9007 typval_T *argvars;
9008 typval_T *rettv UNUSED;
9009{
9010 if (argvars[0].v_type != VAR_NUMBER
9011 || argvars[0].vval.v_number <= 0
9012 || argvars[1].v_type != VAR_NUMBER
9013 || argvars[1].vval.v_number < 0
9014 || argvars[2].v_type != VAR_NUMBER)
9015 EMSG(_(e_invarg));
9016 else
9017 {
9018 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009019 if (alloc_fail_id >= aid_last)
9020 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009021 alloc_fail_countdown = argvars[1].vval.v_number;
9022 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009023 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009024 }
9025}
9026
9027/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009028 * "and(expr, expr)" function
9029 */
9030 static void
9031f_and(argvars, rettv)
9032 typval_T *argvars;
9033 typval_T *rettv;
9034{
9035 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9036 & get_tv_number_chk(&argvars[1], NULL);
9037}
9038
9039/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009040 * "append(lnum, string/list)" function
9041 */
9042 static void
9043f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009044 typval_T *argvars;
9045 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009046{
9047 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009048 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009049 list_T *l = NULL;
9050 listitem_T *li = NULL;
9051 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009052 long added = 0;
9053
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009054 /* When coming here from Insert mode, sync undo, so that this can be
9055 * undone separately from what was previously inserted. */
9056 if (u_sync_once == 2)
9057 {
9058 u_sync_once = 1; /* notify that u_sync() was called */
9059 u_sync(TRUE);
9060 }
9061
Bram Moolenaar0d660222005-01-07 21:51:51 +00009062 lnum = get_tv_lnum(argvars);
9063 if (lnum >= 0
9064 && lnum <= curbuf->b_ml.ml_line_count
9065 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009066 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009067 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009068 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009069 l = argvars[1].vval.v_list;
9070 if (l == NULL)
9071 return;
9072 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009073 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009074 for (;;)
9075 {
9076 if (l == NULL)
9077 tv = &argvars[1]; /* append a string */
9078 else if (li == NULL)
9079 break; /* end of list */
9080 else
9081 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009082 line = get_tv_string_chk(tv);
9083 if (line == NULL) /* type error */
9084 {
9085 rettv->vval.v_number = 1; /* Failed */
9086 break;
9087 }
9088 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009089 ++added;
9090 if (l == NULL)
9091 break;
9092 li = li->li_next;
9093 }
9094
9095 appended_lines_mark(lnum, added);
9096 if (curwin->w_cursor.lnum > lnum)
9097 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009099 else
9100 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101}
9102
9103/*
9104 * "argc()" function
9105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009108 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112}
9113
9114/*
9115 * "argidx()" function
9116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009119 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123}
9124
9125/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009126 * "arglistid()" function
9127 */
9128 static void
9129f_arglistid(argvars, rettv)
9130 typval_T *argvars UNUSED;
9131 typval_T *rettv;
9132{
9133 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009134
9135 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009136 wp = find_tabwin(&argvars[0], &argvars[1]);
9137 if (wp != NULL)
9138 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009139}
9140
9141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 * "argv(nr)" function
9143 */
9144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009145f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009146 typval_T *argvars;
9147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148{
9149 int idx;
9150
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009151 if (argvars[0].v_type != VAR_UNKNOWN)
9152 {
9153 idx = get_tv_number_chk(&argvars[0], NULL);
9154 if (idx >= 0 && idx < ARGCOUNT)
9155 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9156 else
9157 rettv->vval.v_string = NULL;
9158 rettv->v_type = VAR_STRING;
9159 }
9160 else if (rettv_list_alloc(rettv) == OK)
9161 for (idx = 0; idx < ARGCOUNT; ++idx)
9162 list_append_string(rettv->vval.v_list,
9163 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164}
9165
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009166static void prepare_assert_error __ARGS((garray_T*gap));
9167static 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));
9168static void assert_error __ARGS((garray_T *gap));
9169static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9170
9171/*
9172 * Prepare "gap" for an assert error and add the sourcing position.
9173 */
9174 static void
9175prepare_assert_error(gap)
9176 garray_T *gap;
9177{
9178 char buf[NUMBUFLEN];
9179
9180 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009181 if (sourcing_name != NULL)
9182 {
9183 ga_concat(gap, sourcing_name);
9184 if (sourcing_lnum > 0)
9185 ga_concat(gap, (char_u *)" ");
9186 }
9187 if (sourcing_lnum > 0)
9188 {
9189 sprintf(buf, "line %ld", (long)sourcing_lnum);
9190 ga_concat(gap, (char_u *)buf);
9191 }
9192 if (sourcing_name != NULL || sourcing_lnum > 0)
9193 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009194}
9195
9196/*
9197 * Fill "gap" with information about an assert error.
9198 */
9199 static void
9200fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9201 garray_T *gap;
9202 typval_T *opt_msg_tv;
9203 char_u *exp_str;
9204 typval_T *exp_tv;
9205 typval_T *got_tv;
9206{
9207 char_u numbuf[NUMBUFLEN];
9208 char_u *tofree;
9209
9210 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9211 {
9212 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9213 vim_free(tofree);
9214 }
9215 else
9216 {
9217 ga_concat(gap, (char_u *)"Expected ");
9218 if (exp_str == NULL)
9219 {
9220 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9221 vim_free(tofree);
9222 }
9223 else
9224 ga_concat(gap, exp_str);
9225 ga_concat(gap, (char_u *)" but got ");
9226 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9227 vim_free(tofree);
9228 }
9229}
Bram Moolenaar43345542015-11-29 17:35:35 +01009230
9231/*
9232 * Add an assert error to v:errors.
9233 */
9234 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009235assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009236 garray_T *gap;
9237{
9238 struct vimvar *vp = &vimvars[VV_ERRORS];
9239
9240 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9241 /* Make sure v:errors is a list. */
9242 set_vim_var_list(VV_ERRORS, list_alloc());
9243 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9244}
9245
Bram Moolenaar43345542015-11-29 17:35:35 +01009246/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009247 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009248 */
9249 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009250f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009251 typval_T *argvars;
9252 typval_T *rettv UNUSED;
9253{
9254 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009255
9256 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9257 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009258 prepare_assert_error(&ga);
9259 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9260 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009261 ga_clear(&ga);
9262 }
9263}
9264
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009265/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009266 * "assert_exception(string[, msg])" function
9267 */
9268 static void
9269f_assert_exception(argvars, rettv)
9270 typval_T *argvars;
9271 typval_T *rettv UNUSED;
9272{
9273 garray_T ga;
9274 char *error;
9275
9276 error = (char *)get_tv_string_chk(&argvars[0]);
9277 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9278 {
9279 prepare_assert_error(&ga);
9280 ga_concat(&ga, (char_u *)"v:exception is not set");
9281 assert_error(&ga);
9282 ga_clear(&ga);
9283 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009284 else if (error != NULL
9285 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009286 {
9287 prepare_assert_error(&ga);
9288 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9289 &vimvars[VV_EXCEPTION].vv_tv);
9290 assert_error(&ga);
9291 ga_clear(&ga);
9292 }
9293}
9294
9295/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009296 * "assert_fails(cmd [, error])" function
9297 */
9298 static void
9299f_assert_fails(argvars, rettv)
9300 typval_T *argvars;
9301 typval_T *rettv UNUSED;
9302{
9303 char_u *cmd = get_tv_string_chk(&argvars[0]);
9304 garray_T ga;
9305
9306 called_emsg = FALSE;
9307 suppress_errthrow = TRUE;
9308 emsg_silent = TRUE;
9309 do_cmdline_cmd(cmd);
9310 if (!called_emsg)
9311 {
9312 prepare_assert_error(&ga);
9313 ga_concat(&ga, (char_u *)"command did not fail: ");
9314 ga_concat(&ga, cmd);
9315 assert_error(&ga);
9316 ga_clear(&ga);
9317 }
9318 else if (argvars[1].v_type != VAR_UNKNOWN)
9319 {
9320 char_u buf[NUMBUFLEN];
9321 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9322
9323 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9324 {
9325 prepare_assert_error(&ga);
9326 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9327 &vimvars[VV_ERRMSG].vv_tv);
9328 assert_error(&ga);
9329 ga_clear(&ga);
9330 }
9331 }
9332
9333 called_emsg = FALSE;
9334 suppress_errthrow = FALSE;
9335 emsg_silent = FALSE;
9336 emsg_on_display = FALSE;
9337 set_vim_var_string(VV_ERRMSG, NULL, 0);
9338}
9339
9340/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009341 * Common for assert_true() and assert_false().
9342 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009343 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009344assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009345 typval_T *argvars;
9346 int isTrue;
9347{
9348 int error = FALSE;
9349 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009350
9351 if (argvars[0].v_type != VAR_NUMBER
9352 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9353 || error)
9354 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009355 prepare_assert_error(&ga);
9356 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009357 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009358 NULL, &argvars[0]);
9359 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009360 ga_clear(&ga);
9361 }
9362}
9363
9364/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009365 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009366 */
9367 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009368f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009369 typval_T *argvars;
9370 typval_T *rettv UNUSED;
9371{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009372 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009373}
9374
9375/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009376 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009377 */
9378 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009379f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009380 typval_T *argvars;
9381 typval_T *rettv UNUSED;
9382{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009383 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009384}
9385
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009386#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009387/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009388 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009389 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009390 static void
9391f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009392 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009393 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009394{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009395 float_T f;
9396
9397 rettv->v_type = VAR_FLOAT;
9398 if (get_float_arg(argvars, &f) == OK)
9399 rettv->vval.v_float = asin(f);
9400 else
9401 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009402}
9403
9404/*
9405 * "atan()" function
9406 */
9407 static void
9408f_atan(argvars, rettv)
9409 typval_T *argvars;
9410 typval_T *rettv;
9411{
9412 float_T f;
9413
9414 rettv->v_type = VAR_FLOAT;
9415 if (get_float_arg(argvars, &f) == OK)
9416 rettv->vval.v_float = atan(f);
9417 else
9418 rettv->vval.v_float = 0.0;
9419}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009420
9421/*
9422 * "atan2()" function
9423 */
9424 static void
9425f_atan2(argvars, rettv)
9426 typval_T *argvars;
9427 typval_T *rettv;
9428{
9429 float_T fx, fy;
9430
9431 rettv->v_type = VAR_FLOAT;
9432 if (get_float_arg(argvars, &fx) == OK
9433 && get_float_arg(&argvars[1], &fy) == OK)
9434 rettv->vval.v_float = atan2(fx, fy);
9435 else
9436 rettv->vval.v_float = 0.0;
9437}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009438#endif
9439
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440/*
9441 * "browse(save, title, initdir, default)" function
9442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009445 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447{
9448#ifdef FEAT_BROWSE
9449 int save;
9450 char_u *title;
9451 char_u *initdir;
9452 char_u *defname;
9453 char_u buf[NUMBUFLEN];
9454 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009455 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009457 save = get_tv_number_chk(&argvars[0], &error);
9458 title = get_tv_string_chk(&argvars[1]);
9459 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9460 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009462 if (error || title == NULL || initdir == NULL || defname == NULL)
9463 rettv->vval.v_string = NULL;
9464 else
9465 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009466 do_browse(save ? BROWSE_SAVE : 0,
9467 title, defname, NULL, initdir, NULL, curbuf);
9468#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009469 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009470#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009472}
9473
9474/*
9475 * "browsedir(title, initdir)" function
9476 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009479 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009480 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009481{
9482#ifdef FEAT_BROWSE
9483 char_u *title;
9484 char_u *initdir;
9485 char_u buf[NUMBUFLEN];
9486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009487 title = get_tv_string_chk(&argvars[0]);
9488 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009489
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009490 if (title == NULL || initdir == NULL)
9491 rettv->vval.v_string = NULL;
9492 else
9493 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009494 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499}
9500
Bram Moolenaar33570922005-01-25 22:26:29 +00009501static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009502
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503/*
9504 * Find a buffer by number or exact name.
9505 */
9506 static buf_T *
9507find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009508 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
9510 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009512 if (avar->v_type == VAR_NUMBER)
9513 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009514 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009516 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009517 if (buf == NULL)
9518 {
9519 /* No full path name match, try a match with a URL or a "nofile"
9520 * buffer, these don't use the full path. */
9521 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9522 if (buf->b_fname != NULL
9523 && (path_with_url(buf->b_fname)
9524#ifdef FEAT_QUICKFIX
9525 || bt_nofile(buf)
9526#endif
9527 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009528 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009529 break;
9530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 }
9532 return buf;
9533}
9534
9535/*
9536 * "bufexists(expr)" function
9537 */
9538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009539f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009540 typval_T *argvars;
9541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009543 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544}
9545
9546/*
9547 * "buflisted(expr)" function
9548 */
9549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009550f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009551 typval_T *argvars;
9552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553{
9554 buf_T *buf;
9555
9556 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558}
9559
9560/*
9561 * "bufloaded(expr)" function
9562 */
9563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009565 typval_T *argvars;
9566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567{
9568 buf_T *buf;
9569
9570 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572}
9573
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009574static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009575
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576/*
9577 * Get buffer by number or pattern.
9578 */
9579 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009580get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009581 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009582 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 int save_magic;
9586 char_u *save_cpo;
9587 buf_T *buf;
9588
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589 if (tv->v_type == VAR_NUMBER)
9590 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009591 if (tv->v_type != VAR_STRING)
9592 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 if (name == NULL || *name == NUL)
9594 return curbuf;
9595 if (name[0] == '$' && name[1] == NUL)
9596 return lastbuf;
9597
9598 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9599 save_magic = p_magic;
9600 p_magic = TRUE;
9601 save_cpo = p_cpo;
9602 p_cpo = (char_u *)"";
9603
9604 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009605 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606
9607 p_magic = save_magic;
9608 p_cpo = save_cpo;
9609
9610 /* If not found, try expanding the name, like done for bufexists(). */
9611 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613
9614 return buf;
9615}
9616
9617/*
9618 * "bufname(expr)" function
9619 */
9620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009622 typval_T *argvars;
9623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624{
9625 buf_T *buf;
9626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009627 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009629 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 --emsg_off;
9636}
9637
9638/*
9639 * "bufnr(expr)" function
9640 */
9641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009643 typval_T *argvars;
9644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645{
9646 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009647 int error = FALSE;
9648 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009650 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009652 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009653 --emsg_off;
9654
9655 /* If the buffer isn't found and the second argument is not zero create a
9656 * new buffer. */
9657 if (buf == NULL
9658 && argvars[1].v_type != VAR_UNKNOWN
9659 && get_tv_number_chk(&argvars[1], &error) != 0
9660 && !error
9661 && (name = get_tv_string_chk(&argvars[0])) != NULL
9662 && !error)
9663 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9664
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009666 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009668 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669}
9670
9671/*
9672 * "bufwinnr(nr)" function
9673 */
9674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009675f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009676 typval_T *argvars;
9677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678{
9679#ifdef FEAT_WINDOWS
9680 win_T *wp;
9681 int winnr = 0;
9682#endif
9683 buf_T *buf;
9684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009685 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009687 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688#ifdef FEAT_WINDOWS
9689 for (wp = firstwin; wp; wp = wp->w_next)
9690 {
9691 ++winnr;
9692 if (wp->w_buffer == buf)
9693 break;
9694 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698#endif
9699 --emsg_off;
9700}
9701
9702/*
9703 * "byte2line(byte)" function
9704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009707 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709{
9710#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712#else
9713 long boff = 0;
9714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009715 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009717 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 (linenr_T)0, &boff);
9721#endif
9722}
9723
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009724 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009725byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009726 typval_T *argvars;
9727 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009728 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009729{
9730#ifdef FEAT_MBYTE
9731 char_u *t;
9732#endif
9733 char_u *str;
9734 long idx;
9735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009736 str = get_tv_string_chk(&argvars[0]);
9737 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009740 return;
9741
9742#ifdef FEAT_MBYTE
9743 t = str;
9744 for ( ; idx > 0; idx--)
9745 {
9746 if (*t == NUL) /* EOL reached */
9747 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009748 if (enc_utf8 && comp)
9749 t += utf_ptr2len(t);
9750 else
9751 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009752 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009753 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009754#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009755 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009757#endif
9758}
9759
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009760/*
9761 * "byteidx()" function
9762 */
9763 static void
9764f_byteidx(argvars, rettv)
9765 typval_T *argvars;
9766 typval_T *rettv;
9767{
9768 byteidx(argvars, rettv, FALSE);
9769}
9770
9771/*
9772 * "byteidxcomp()" function
9773 */
9774 static void
9775f_byteidxcomp(argvars, rettv)
9776 typval_T *argvars;
9777 typval_T *rettv;
9778{
9779 byteidx(argvars, rettv, TRUE);
9780}
9781
Bram Moolenaardb913952012-06-29 12:54:53 +02009782 int
9783func_call(name, args, selfdict, rettv)
9784 char_u *name;
9785 typval_T *args;
9786 dict_T *selfdict;
9787 typval_T *rettv;
9788{
9789 listitem_T *item;
9790 typval_T argv[MAX_FUNC_ARGS + 1];
9791 int argc = 0;
9792 int dummy;
9793 int r = 0;
9794
9795 for (item = args->vval.v_list->lv_first; item != NULL;
9796 item = item->li_next)
9797 {
9798 if (argc == MAX_FUNC_ARGS)
9799 {
9800 EMSG(_("E699: Too many arguments"));
9801 break;
9802 }
9803 /* Make a copy of each argument. This is needed to be able to set
9804 * v_lock to VAR_FIXED in the copy without changing the original list.
9805 */
9806 copy_tv(&item->li_tv, &argv[argc++]);
9807 }
9808
9809 if (item == NULL)
9810 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9811 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9812 &dummy, TRUE, selfdict);
9813
9814 /* Free the arguments. */
9815 while (argc > 0)
9816 clear_tv(&argv[--argc]);
9817
9818 return r;
9819}
9820
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009821/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009822 * "call(func, arglist)" function
9823 */
9824 static void
9825f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009826 typval_T *argvars;
9827 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009828{
9829 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009830 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009831
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009832 if (argvars[1].v_type != VAR_LIST)
9833 {
9834 EMSG(_(e_listreq));
9835 return;
9836 }
9837 if (argvars[1].vval.v_list == NULL)
9838 return;
9839
9840 if (argvars[0].v_type == VAR_FUNC)
9841 func = argvars[0].vval.v_string;
9842 else
9843 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009844 if (*func == NUL)
9845 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009846
Bram Moolenaare9a41262005-01-15 22:18:47 +00009847 if (argvars[2].v_type != VAR_UNKNOWN)
9848 {
9849 if (argvars[2].v_type != VAR_DICT)
9850 {
9851 EMSG(_(e_dictreq));
9852 return;
9853 }
9854 selfdict = argvars[2].vval.v_dict;
9855 }
9856
Bram Moolenaardb913952012-06-29 12:54:53 +02009857 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009858}
9859
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009860#ifdef FEAT_FLOAT
9861/*
9862 * "ceil({float})" function
9863 */
9864 static void
9865f_ceil(argvars, rettv)
9866 typval_T *argvars;
9867 typval_T *rettv;
9868{
9869 float_T f;
9870
9871 rettv->v_type = VAR_FLOAT;
9872 if (get_float_arg(argvars, &f) == OK)
9873 rettv->vval.v_float = ceil(f);
9874 else
9875 rettv->vval.v_float = 0.0;
9876}
9877#endif
9878
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009879/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009880 * "changenr()" function
9881 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009882 static void
9883f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009884 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009885 typval_T *rettv;
9886{
9887 rettv->vval.v_number = curbuf->b_u_seq_cur;
9888}
9889
9890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 * "char2nr(string)" function
9892 */
9893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009895 typval_T *argvars;
9896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897{
9898#ifdef FEAT_MBYTE
9899 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009900 {
9901 int utf8 = 0;
9902
9903 if (argvars[1].v_type != VAR_UNKNOWN)
9904 utf8 = get_tv_number_chk(&argvars[1], NULL);
9905
9906 if (utf8)
9907 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9908 else
9909 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 else
9912#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009913 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914}
9915
9916/*
9917 * "cindent(lnum)" function
9918 */
9919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009920f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009921 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923{
9924#ifdef FEAT_CINDENT
9925 pos_T pos;
9926 linenr_T lnum;
9927
9928 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009929 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9931 {
9932 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009933 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 curwin->w_cursor = pos;
9935 }
9936 else
9937#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009938 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939}
9940
9941/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009942 * "clearmatches()" function
9943 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009944 static void
9945f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009946 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009947 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009948{
9949#ifdef FEAT_SEARCH_EXTRA
9950 clear_matches(curwin);
9951#endif
9952}
9953
9954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 * "col(string)" function
9956 */
9957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009958f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009959 typval_T *argvars;
9960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961{
9962 colnr_T col = 0;
9963 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009964 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009966 fp = var2fpos(&argvars[0], FALSE, &fnum);
9967 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 {
9969 if (fp->col == MAXCOL)
9970 {
9971 /* '> can be MAXCOL, get the length of the line then */
9972 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009973 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 else
9975 col = MAXCOL;
9976 }
9977 else
9978 {
9979 col = fp->col + 1;
9980#ifdef FEAT_VIRTUALEDIT
9981 /* col(".") when the cursor is on the NUL at the end of the line
9982 * because of "coladd" can be seen as an extra column. */
9983 if (virtual_active() && fp == &curwin->w_cursor)
9984 {
9985 char_u *p = ml_get_cursor();
9986
9987 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9988 curwin->w_virtcol - curwin->w_cursor.coladd))
9989 {
9990# ifdef FEAT_MBYTE
9991 int l;
9992
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009993 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 col += l;
9995# else
9996 if (*p != NUL && p[1] == NUL)
9997 ++col;
9998# endif
9999 }
10000 }
10001#endif
10002 }
10003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010004 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005}
10006
Bram Moolenaar572cb562005-08-05 21:35:02 +000010007#if defined(FEAT_INS_EXPAND)
10008/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010009 * "complete()" function
10010 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010011 static void
10012f_complete(argvars, rettv)
10013 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010014 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +000010015{
10016 int startcol;
10017
10018 if ((State & INSERT) == 0)
10019 {
10020 EMSG(_("E785: complete() can only be used in Insert mode"));
10021 return;
10022 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010023
10024 /* Check for undo allowed here, because if something was already inserted
10025 * the line was already saved for undo and this check isn't done. */
10026 if (!undo_allowed())
10027 return;
10028
Bram Moolenaarade00832006-03-10 21:46:58 +000010029 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10030 {
10031 EMSG(_(e_invarg));
10032 return;
10033 }
10034
10035 startcol = get_tv_number_chk(&argvars[0], NULL);
10036 if (startcol <= 0)
10037 return;
10038
10039 set_completion(startcol - 1, argvars[1].vval.v_list);
10040}
10041
10042/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010043 * "complete_add()" function
10044 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010045 static void
10046f_complete_add(argvars, rettv)
10047 typval_T *argvars;
10048 typval_T *rettv;
10049{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010050 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010051}
10052
10053/*
10054 * "complete_check()" function
10055 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010056 static void
10057f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010058 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +000010059 typval_T *rettv;
10060{
10061 int saved = RedrawingDisabled;
10062
10063 RedrawingDisabled = 0;
10064 ins_compl_check_keys(0);
10065 rettv->vval.v_number = compl_interrupted;
10066 RedrawingDisabled = saved;
10067}
10068#endif
10069
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070/*
10071 * "confirm(message, buttons[, default [, type]])" function
10072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010074f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010075 typval_T *argvars UNUSED;
10076 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077{
10078#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10079 char_u *message;
10080 char_u *buttons = NULL;
10081 char_u buf[NUMBUFLEN];
10082 char_u buf2[NUMBUFLEN];
10083 int def = 1;
10084 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010085 char_u *typestr;
10086 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010088 message = get_tv_string_chk(&argvars[0]);
10089 if (message == NULL)
10090 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010091 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010093 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10094 if (buttons == NULL)
10095 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010096 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010099 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010101 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10102 if (typestr == NULL)
10103 error = TRUE;
10104 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010106 switch (TOUPPER_ASC(*typestr))
10107 {
10108 case 'E': type = VIM_ERROR; break;
10109 case 'Q': type = VIM_QUESTION; break;
10110 case 'I': type = VIM_INFO; break;
10111 case 'W': type = VIM_WARNING; break;
10112 case 'G': type = VIM_GENERIC; break;
10113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 }
10115 }
10116 }
10117 }
10118
10119 if (buttons == NULL || *buttons == NUL)
10120 buttons = (char_u *)_("&Ok");
10121
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010122 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010123 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010124 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125#endif
10126}
10127
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010128/*
10129 * "copy()" function
10130 */
10131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010132f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010133 typval_T *argvars;
10134 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010135{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010136 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010137}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010139#ifdef FEAT_FLOAT
10140/*
10141 * "cos()" function
10142 */
10143 static void
10144f_cos(argvars, rettv)
10145 typval_T *argvars;
10146 typval_T *rettv;
10147{
10148 float_T f;
10149
10150 rettv->v_type = VAR_FLOAT;
10151 if (get_float_arg(argvars, &f) == OK)
10152 rettv->vval.v_float = cos(f);
10153 else
10154 rettv->vval.v_float = 0.0;
10155}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010156
10157/*
10158 * "cosh()" function
10159 */
10160 static void
10161f_cosh(argvars, rettv)
10162 typval_T *argvars;
10163 typval_T *rettv;
10164{
10165 float_T f;
10166
10167 rettv->v_type = VAR_FLOAT;
10168 if (get_float_arg(argvars, &f) == OK)
10169 rettv->vval.v_float = cosh(f);
10170 else
10171 rettv->vval.v_float = 0.0;
10172}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010173#endif
10174
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010176 * "count()" function
10177 */
10178 static void
10179f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010180 typval_T *argvars;
10181 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010182{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010183 long n = 0;
10184 int ic = FALSE;
10185
Bram Moolenaare9a41262005-01-15 22:18:47 +000010186 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010187 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010188 listitem_T *li;
10189 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010190 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010191
Bram Moolenaare9a41262005-01-15 22:18:47 +000010192 if ((l = argvars[0].vval.v_list) != NULL)
10193 {
10194 li = l->lv_first;
10195 if (argvars[2].v_type != VAR_UNKNOWN)
10196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010197 int error = FALSE;
10198
10199 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010200 if (argvars[3].v_type != VAR_UNKNOWN)
10201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010202 idx = get_tv_number_chk(&argvars[3], &error);
10203 if (!error)
10204 {
10205 li = list_find(l, idx);
10206 if (li == NULL)
10207 EMSGN(_(e_listidx), idx);
10208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010209 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010210 if (error)
10211 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010212 }
10213
10214 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010215 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010216 ++n;
10217 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010218 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010219 else if (argvars[0].v_type == VAR_DICT)
10220 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010221 int todo;
10222 dict_T *d;
10223 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010224
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010225 if ((d = argvars[0].vval.v_dict) != NULL)
10226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010227 int error = FALSE;
10228
Bram Moolenaare9a41262005-01-15 22:18:47 +000010229 if (argvars[2].v_type != VAR_UNKNOWN)
10230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010232 if (argvars[3].v_type != VAR_UNKNOWN)
10233 EMSG(_(e_invarg));
10234 }
10235
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010236 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010237 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010238 {
10239 if (!HASHITEM_EMPTY(hi))
10240 {
10241 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010242 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010243 ++n;
10244 }
10245 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010246 }
10247 }
10248 else
10249 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010250 rettv->vval.v_number = n;
10251}
10252
10253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10255 *
10256 * Checks the existence of a cscope connection.
10257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010259f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010260 typval_T *argvars UNUSED;
10261 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262{
10263#ifdef FEAT_CSCOPE
10264 int num = 0;
10265 char_u *dbpath = NULL;
10266 char_u *prepend = NULL;
10267 char_u buf[NUMBUFLEN];
10268
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010269 if (argvars[0].v_type != VAR_UNKNOWN
10270 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010272 num = (int)get_tv_number(&argvars[0]);
10273 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010274 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010275 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 }
10277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010278 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279#endif
10280}
10281
10282/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010283 * "cursor(lnum, col)" function, or
10284 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010286 * Moves the cursor to the specified line and column.
10287 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010290f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010291 typval_T *argvars;
10292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293{
10294 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010295#ifdef FEAT_VIRTUALEDIT
10296 long coladd = 0;
10297#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010298 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010300 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010301 if (argvars[1].v_type == VAR_UNKNOWN)
10302 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010303 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010304 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010305
Bram Moolenaar493c1782014-05-28 14:34:46 +020010306 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010307 {
10308 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010309 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010310 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010311 line = pos.lnum;
10312 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010313#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010314 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010315#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010316 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010317 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010318 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010319 set_curswant = FALSE;
10320 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010321 }
10322 else
10323 {
10324 line = get_tv_lnum(argvars);
10325 col = get_tv_number_chk(&argvars[1], NULL);
10326#ifdef FEAT_VIRTUALEDIT
10327 if (argvars[2].v_type != VAR_UNKNOWN)
10328 coladd = get_tv_number_chk(&argvars[2], NULL);
10329#endif
10330 }
10331 if (line < 0 || col < 0
10332#ifdef FEAT_VIRTUALEDIT
10333 || coladd < 0
10334#endif
10335 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010336 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 if (line > 0)
10338 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 if (col > 0)
10340 curwin->w_cursor.col = col - 1;
10341#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010342 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343#endif
10344
10345 /* Make sure the cursor is in a valid position. */
10346 check_cursor();
10347#ifdef FEAT_MBYTE
10348 /* Correct cursor for multi-byte character. */
10349 if (has_mbyte)
10350 mb_adjust_cursor();
10351#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010352
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010353 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010354 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355}
10356
10357/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010358 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 */
10360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010361f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010362 typval_T *argvars;
10363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010365 int noref = 0;
10366
10367 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010368 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010369 if (noref < 0 || noref > 1)
10370 EMSG(_(e_invarg));
10371 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010372 {
10373 current_copyID += COPYID_INC;
10374 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376}
10377
10378/*
10379 * "delete()" function
10380 */
10381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010382f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010383 typval_T *argvars;
10384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010386 char_u nbuf[NUMBUFLEN];
10387 char_u *name;
10388 char_u *flags;
10389
10390 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010392 return;
10393
10394 name = get_tv_string(&argvars[0]);
10395 if (name == NULL || *name == NUL)
10396 {
10397 EMSG(_(e_invarg));
10398 return;
10399 }
10400
10401 if (argvars[1].v_type != VAR_UNKNOWN)
10402 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010404 flags = (char_u *)"";
10405
10406 if (*flags == NUL)
10407 /* delete a file */
10408 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10409 else if (STRCMP(flags, "d") == 0)
10410 /* delete an empty directory */
10411 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10412 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010413 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010414 rettv->vval.v_number = delete_recursive(name);
10415 else
10416 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417}
10418
10419/*
10420 * "did_filetype()" function
10421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010423f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010424 typval_T *argvars UNUSED;
10425 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426{
10427#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010428 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429#endif
10430}
10431
10432/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010433 * "diff_filler()" function
10434 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010437 typval_T *argvars UNUSED;
10438 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010439{
10440#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010441 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010442#endif
10443}
10444
10445/*
10446 * "diff_hlID()" function
10447 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010449f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010450 typval_T *argvars UNUSED;
10451 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010452{
10453#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010455 static linenr_T prev_lnum = 0;
10456 static int changedtick = 0;
10457 static int fnum = 0;
10458 static int change_start = 0;
10459 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010460 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010461 int filler_lines;
10462 int col;
10463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010464 if (lnum < 0) /* ignore type error in {lnum} arg */
10465 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010466 if (lnum != prev_lnum
10467 || changedtick != curbuf->b_changedtick
10468 || fnum != curbuf->b_fnum)
10469 {
10470 /* New line, buffer, change: need to get the values. */
10471 filler_lines = diff_check(curwin, lnum);
10472 if (filler_lines < 0)
10473 {
10474 if (filler_lines == -1)
10475 {
10476 change_start = MAXCOL;
10477 change_end = -1;
10478 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10479 hlID = HLF_ADD; /* added line */
10480 else
10481 hlID = HLF_CHD; /* changed line */
10482 }
10483 else
10484 hlID = HLF_ADD; /* added line */
10485 }
10486 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010487 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010488 prev_lnum = lnum;
10489 changedtick = curbuf->b_changedtick;
10490 fnum = curbuf->b_fnum;
10491 }
10492
10493 if (hlID == HLF_CHD || hlID == HLF_TXD)
10494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010495 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010496 if (col >= change_start && col <= change_end)
10497 hlID = HLF_TXD; /* changed text */
10498 else
10499 hlID = HLF_CHD; /* changed line */
10500 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010501 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010502#endif
10503}
10504
10505/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010506 * "empty({expr})" function
10507 */
10508 static void
10509f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010510 typval_T *argvars;
10511 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010512{
10513 int n;
10514
10515 switch (argvars[0].v_type)
10516 {
10517 case VAR_STRING:
10518 case VAR_FUNC:
10519 n = argvars[0].vval.v_string == NULL
10520 || *argvars[0].vval.v_string == NUL;
10521 break;
10522 case VAR_NUMBER:
10523 n = argvars[0].vval.v_number == 0;
10524 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010525#ifdef FEAT_FLOAT
10526 case VAR_FLOAT:
10527 n = argvars[0].vval.v_float == 0.0;
10528 break;
10529#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010530 case VAR_LIST:
10531 n = argvars[0].vval.v_list == NULL
10532 || argvars[0].vval.v_list->lv_first == NULL;
10533 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010534 case VAR_DICT:
10535 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010536 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010537 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010538 default:
10539 EMSG2(_(e_intern2), "f_empty()");
10540 n = 0;
10541 }
10542
10543 rettv->vval.v_number = n;
10544}
10545
10546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 * "escape({string}, {chars})" function
10548 */
10549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010550f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010551 typval_T *argvars;
10552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553{
10554 char_u buf[NUMBUFLEN];
10555
Bram Moolenaar758711c2005-02-02 23:11:38 +000010556 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10557 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010558 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559}
10560
10561/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010562 * "eval()" function
10563 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010564 static void
10565f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 typval_T *argvars;
10567 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010568{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010569 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571 s = get_tv_string_chk(&argvars[0]);
10572 if (s != NULL)
10573 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010574
Bram Moolenaar615b9972015-01-14 17:15:05 +010010575 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010576 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10577 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010578 if (p != NULL && !aborting())
10579 EMSG2(_(e_invexpr2), p);
10580 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010581 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010582 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010584 else if (*s != NUL)
10585 EMSG(_(e_trailing));
10586}
10587
10588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 * "eventhandler()" function
10590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010592f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010593 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010596 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597}
10598
10599/*
10600 * "executable()" function
10601 */
10602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010604 typval_T *argvars;
10605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010607 char_u *name = get_tv_string(&argvars[0]);
10608
10609 /* Check in $PATH and also check directly if there is a directory name. */
10610 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10611 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010612}
10613
10614/*
10615 * "exepath()" function
10616 */
10617 static void
10618f_exepath(argvars, rettv)
10619 typval_T *argvars;
10620 typval_T *rettv;
10621{
10622 char_u *p = NULL;
10623
Bram Moolenaarb5971142015-03-21 17:32:19 +010010624 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010625 rettv->v_type = VAR_STRING;
10626 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627}
10628
10629/*
10630 * "exists()" function
10631 */
10632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010633f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010634 typval_T *argvars;
10635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636{
10637 char_u *p;
10638 char_u *name;
10639 int n = FALSE;
10640 int len = 0;
10641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010642 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 if (*p == '$') /* environment variable */
10644 {
10645 /* first try "normal" environment variables (fast) */
10646 if (mch_getenv(p + 1) != NULL)
10647 n = TRUE;
10648 else
10649 {
10650 /* try expanding things like $VIM and ${HOME} */
10651 p = expand_env_save(p);
10652 if (p != NULL && *p != '$')
10653 n = TRUE;
10654 vim_free(p);
10655 }
10656 }
10657 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010659 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010660 if (*skipwhite(p) != NUL)
10661 n = FALSE; /* trailing garbage */
10662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 else if (*p == '*') /* internal or user defined function */
10664 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010665 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 }
10667 else if (*p == ':')
10668 {
10669 n = cmd_exists(p + 1);
10670 }
10671 else if (*p == '#')
10672 {
10673#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010674 if (p[1] == '#')
10675 n = autocmd_supported(p + 2);
10676 else
10677 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678#endif
10679 }
10680 else /* internal variable */
10681 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010682 char_u *tofree;
10683 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010685 /* get_name_len() takes care of expanding curly braces */
10686 name = p;
10687 len = get_name_len(&p, &tofree, TRUE, FALSE);
10688 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010690 if (tofree != NULL)
10691 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010692 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010693 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010695 /* handle d.key, l[idx], f(expr) */
10696 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10697 if (n)
10698 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 }
10700 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010701 if (*p != NUL)
10702 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010704 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 }
10706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010707 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708}
10709
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010710#ifdef FEAT_FLOAT
10711/*
10712 * "exp()" function
10713 */
10714 static void
10715f_exp(argvars, rettv)
10716 typval_T *argvars;
10717 typval_T *rettv;
10718{
10719 float_T f;
10720
10721 rettv->v_type = VAR_FLOAT;
10722 if (get_float_arg(argvars, &f) == OK)
10723 rettv->vval.v_float = exp(f);
10724 else
10725 rettv->vval.v_float = 0.0;
10726}
10727#endif
10728
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729/*
10730 * "expand()" function
10731 */
10732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010734 typval_T *argvars;
10735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736{
10737 char_u *s;
10738 int len;
10739 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010740 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010742 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010743 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010745 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010746 if (argvars[1].v_type != VAR_UNKNOWN
10747 && argvars[2].v_type != VAR_UNKNOWN
10748 && get_tv_number_chk(&argvars[2], &error)
10749 && !error)
10750 {
10751 rettv->v_type = VAR_LIST;
10752 rettv->vval.v_list = NULL;
10753 }
10754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010755 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 if (*s == '%' || *s == '#' || *s == '<')
10757 {
10758 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010759 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010761 if (rettv->v_type == VAR_LIST)
10762 {
10763 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10764 list_append_string(rettv->vval.v_list, result, -1);
10765 else
10766 vim_free(result);
10767 }
10768 else
10769 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 }
10771 else
10772 {
10773 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010774 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010775 if (argvars[1].v_type != VAR_UNKNOWN
10776 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010777 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010778 if (!error)
10779 {
10780 ExpandInit(&xpc);
10781 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010782 if (p_wic)
10783 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010784 if (rettv->v_type == VAR_STRING)
10785 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10786 options, WILD_ALL);
10787 else if (rettv_list_alloc(rettv) != FAIL)
10788 {
10789 int i;
10790
10791 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10792 for (i = 0; i < xpc.xp_numfiles; i++)
10793 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10794 ExpandCleanup(&xpc);
10795 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010796 }
10797 else
10798 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 }
10800}
10801
10802/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010803 * Go over all entries in "d2" and add them to "d1".
10804 * When "action" is "error" then a duplicate key is an error.
10805 * When "action" is "force" then a duplicate key is overwritten.
10806 * Otherwise duplicate keys are ignored ("action" is "keep").
10807 */
10808 void
10809dict_extend(d1, d2, action)
10810 dict_T *d1;
10811 dict_T *d2;
10812 char_u *action;
10813{
10814 dictitem_T *di1;
10815 hashitem_T *hi2;
10816 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010817 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010818
10819 todo = (int)d2->dv_hashtab.ht_used;
10820 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10821 {
10822 if (!HASHITEM_EMPTY(hi2))
10823 {
10824 --todo;
10825 di1 = dict_find(d1, hi2->hi_key, -1);
10826 if (d1->dv_scope != 0)
10827 {
10828 /* Disallow replacing a builtin function in l: and g:.
10829 * Check the key to be valid when adding to any
10830 * scope. */
10831 if (d1->dv_scope == VAR_DEF_SCOPE
10832 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10833 && var_check_func_name(hi2->hi_key,
10834 di1 == NULL))
10835 break;
10836 if (!valid_varname(hi2->hi_key))
10837 break;
10838 }
10839 if (di1 == NULL)
10840 {
10841 di1 = dictitem_copy(HI2DI(hi2));
10842 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10843 dictitem_free(di1);
10844 }
10845 else if (*action == 'e')
10846 {
10847 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10848 break;
10849 }
10850 else if (*action == 'f' && HI2DI(hi2) != di1)
10851 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010852 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10853 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010854 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010855 clear_tv(&di1->di_tv);
10856 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10857 }
10858 }
10859 }
10860}
10861
10862/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010863 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010864 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010865 */
10866 static void
10867f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010868 typval_T *argvars;
10869 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010870{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010871 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010872
Bram Moolenaare9a41262005-01-15 22:18:47 +000010873 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010874 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010875 list_T *l1, *l2;
10876 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010877 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010878 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010879
Bram Moolenaare9a41262005-01-15 22:18:47 +000010880 l1 = argvars[0].vval.v_list;
10881 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010882 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010883 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010884 {
10885 if (argvars[2].v_type != VAR_UNKNOWN)
10886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010887 before = get_tv_number_chk(&argvars[2], &error);
10888 if (error)
10889 return; /* type error; errmsg already given */
10890
Bram Moolenaar758711c2005-02-02 23:11:38 +000010891 if (before == l1->lv_len)
10892 item = NULL;
10893 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010894 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010895 item = list_find(l1, before);
10896 if (item == NULL)
10897 {
10898 EMSGN(_(e_listidx), before);
10899 return;
10900 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010901 }
10902 }
10903 else
10904 item = NULL;
10905 list_extend(l1, l2, item);
10906
Bram Moolenaare9a41262005-01-15 22:18:47 +000010907 copy_tv(&argvars[0], rettv);
10908 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010909 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010910 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10911 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010912 dict_T *d1, *d2;
10913 char_u *action;
10914 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010915
10916 d1 = argvars[0].vval.v_dict;
10917 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010918 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010919 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010920 {
10921 /* Check the third argument. */
10922 if (argvars[2].v_type != VAR_UNKNOWN)
10923 {
10924 static char *(av[]) = {"keep", "force", "error"};
10925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010926 action = get_tv_string_chk(&argvars[2]);
10927 if (action == NULL)
10928 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010929 for (i = 0; i < 3; ++i)
10930 if (STRCMP(action, av[i]) == 0)
10931 break;
10932 if (i == 3)
10933 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010934 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010935 return;
10936 }
10937 }
10938 else
10939 action = (char_u *)"force";
10940
Bram Moolenaara9922d62013-05-30 13:01:18 +020010941 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010942
Bram Moolenaare9a41262005-01-15 22:18:47 +000010943 copy_tv(&argvars[0], rettv);
10944 }
10945 }
10946 else
10947 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010948}
10949
10950/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010951 * "feedkeys()" function
10952 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010953 static void
10954f_feedkeys(argvars, rettv)
10955 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010956 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010957{
10958 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010959 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010960 char_u *keys, *flags;
10961 char_u nbuf[NUMBUFLEN];
10962 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010963 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010964 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010965
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010966 /* This is not allowed in the sandbox. If the commands would still be
10967 * executed in the sandbox it would be OK, but it probably happens later,
10968 * when "sandbox" is no longer set. */
10969 if (check_secure())
10970 return;
10971
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010972 keys = get_tv_string(&argvars[0]);
10973 if (*keys != NUL)
10974 {
10975 if (argvars[1].v_type != VAR_UNKNOWN)
10976 {
10977 flags = get_tv_string_buf(&argvars[1], nbuf);
10978 for ( ; *flags != NUL; ++flags)
10979 {
10980 switch (*flags)
10981 {
10982 case 'n': remap = FALSE; break;
10983 case 'm': remap = TRUE; break;
10984 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010985 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010986 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010987 }
10988 }
10989 }
10990
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010991 /* Need to escape K_SPECIAL and CSI before putting the string in the
10992 * typeahead buffer. */
10993 keys_esc = vim_strsave_escape_csi(keys);
10994 if (keys_esc != NULL)
10995 {
10996 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010997 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010998 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010999 if (vgetc_busy)
11000 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011001 if (execute)
11002 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011003 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011004 }
11005}
11006
11007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 * "filereadable()" function
11009 */
11010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011011f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011012 typval_T *argvars;
11013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011015 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 char_u *p;
11017 int n;
11018
Bram Moolenaarc236c162008-07-13 17:41:49 +000011019#ifndef O_NONBLOCK
11020# define O_NONBLOCK 0
11021#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011023 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11024 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 {
11026 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011027 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 }
11029 else
11030 n = FALSE;
11031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011032 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033}
11034
11035/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011036 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 * rights to write into.
11038 */
11039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011041 typval_T *argvars;
11042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011044 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045}
11046
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011047static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011048
11049 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011050findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011051 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011052 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011053 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011054{
11055#ifdef FEAT_SEARCHPATH
11056 char_u *fname;
11057 char_u *fresult = NULL;
11058 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11059 char_u *p;
11060 char_u pathbuf[NUMBUFLEN];
11061 int count = 1;
11062 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011063 int error = FALSE;
11064#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011065
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011066 rettv->vval.v_string = NULL;
11067 rettv->v_type = VAR_STRING;
11068
11069#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011071
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011072 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011074 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11075 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011076 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077 else
11078 {
11079 if (*p != NUL)
11080 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011082 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011083 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011084 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011085 }
11086
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011087 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11088 error = TRUE;
11089
11090 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011092 do
11093 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011094 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011095 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011096 fresult = find_file_in_path_option(first ? fname : NULL,
11097 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011098 0, first, path,
11099 find_what,
11100 curbuf->b_ffname,
11101 find_what == FINDFILE_DIR
11102 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011103 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011104
11105 if (fresult != NULL && rettv->v_type == VAR_LIST)
11106 list_append_string(rettv->vval.v_list, fresult, -1);
11107
11108 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011110
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011111 if (rettv->v_type == VAR_STRING)
11112 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011113#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011114}
11115
Bram Moolenaar33570922005-01-25 22:26:29 +000011116static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
11117static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011118
11119/*
11120 * Implementation of map() and filter().
11121 */
11122 static void
11123filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011124 typval_T *argvars;
11125 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011126 int map;
11127{
11128 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011129 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011130 listitem_T *li, *nli;
11131 list_T *l = NULL;
11132 dictitem_T *di;
11133 hashtab_T *ht;
11134 hashitem_T *hi;
11135 dict_T *d = NULL;
11136 typval_T save_val;
11137 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011138 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011139 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011140 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011141 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011142 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011143 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011144 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011145
Bram Moolenaare9a41262005-01-15 22:18:47 +000011146 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011147 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011148 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011149 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011150 return;
11151 }
11152 else if (argvars[0].v_type == VAR_DICT)
11153 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011154 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011155 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011156 return;
11157 }
11158 else
11159 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011160 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011161 return;
11162 }
11163
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011164 expr = get_tv_string_buf_chk(&argvars[1], buf);
11165 /* On type errors, the preceding call has already displayed an error
11166 * message. Avoid a misleading error message for an empty string that
11167 * was not passed as argument. */
11168 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011170 prepare_vimvar(VV_VAL, &save_val);
11171 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011172
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011173 /* We reset "did_emsg" to be able to detect whether an error
11174 * occurred during evaluation of the expression. */
11175 save_did_emsg = did_emsg;
11176 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011177
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011178 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011179 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011180 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011181 vimvars[VV_KEY].vv_type = VAR_STRING;
11182
11183 ht = &d->dv_hashtab;
11184 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011185 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011186 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011188 if (!HASHITEM_EMPTY(hi))
11189 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011190 int r;
11191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011192 --todo;
11193 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011194 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011195 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11196 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011197 break;
11198 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011199 r = filter_map_one(&di->di_tv, expr, map, &rem);
11200 clear_tv(&vimvars[VV_KEY].vv_tv);
11201 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011202 break;
11203 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011204 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011205 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11206 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011207 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011208 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011209 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011210 }
11211 }
11212 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011213 }
11214 else
11215 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011216 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11217
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011218 for (li = l->lv_first; li != NULL; li = nli)
11219 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011220 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011221 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011223 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011224 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011225 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011226 break;
11227 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011229 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011230 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011231 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011232
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011233 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011234 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011235
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011236 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011237 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011238
11239 copy_tv(&argvars[0], rettv);
11240}
11241
11242 static int
11243filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011244 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011245 char_u *expr;
11246 int map;
11247 int *remp;
11248{
Bram Moolenaar33570922005-01-25 22:26:29 +000011249 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011250 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011251 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011252
Bram Moolenaar33570922005-01-25 22:26:29 +000011253 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011254 s = expr;
11255 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011256 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011257 if (*s != NUL) /* check for trailing chars after expr */
11258 {
11259 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011260 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011261 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011262 }
11263 if (map)
11264 {
11265 /* map(): replace the list item value */
11266 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011267 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011268 *tv = rettv;
11269 }
11270 else
11271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011272 int error = FALSE;
11273
Bram Moolenaare9a41262005-01-15 22:18:47 +000011274 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011275 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011276 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011277 /* On type error, nothing has been removed; return FAIL to stop the
11278 * loop. The error message was given by get_tv_number_chk(). */
11279 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011280 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011281 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011282 retval = OK;
11283theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011285 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011286}
11287
11288/*
11289 * "filter()" function
11290 */
11291 static void
11292f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011293 typval_T *argvars;
11294 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011295{
11296 filter_map(argvars, rettv, FALSE);
11297}
11298
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011299/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011300 * "finddir({fname}[, {path}[, {count}]])" function
11301 */
11302 static void
11303f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011304 typval_T *argvars;
11305 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011306{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011307 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011308}
11309
11310/*
11311 * "findfile({fname}[, {path}[, {count}]])" function
11312 */
11313 static void
11314f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011315 typval_T *argvars;
11316 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011317{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011318 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011319}
11320
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011321#ifdef FEAT_FLOAT
11322/*
11323 * "float2nr({float})" function
11324 */
11325 static void
11326f_float2nr(argvars, rettv)
11327 typval_T *argvars;
11328 typval_T *rettv;
11329{
11330 float_T f;
11331
11332 if (get_float_arg(argvars, &f) == OK)
11333 {
11334 if (f < -0x7fffffff)
11335 rettv->vval.v_number = -0x7fffffff;
11336 else if (f > 0x7fffffff)
11337 rettv->vval.v_number = 0x7fffffff;
11338 else
11339 rettv->vval.v_number = (varnumber_T)f;
11340 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011341}
11342
11343/*
11344 * "floor({float})" function
11345 */
11346 static void
11347f_floor(argvars, rettv)
11348 typval_T *argvars;
11349 typval_T *rettv;
11350{
11351 float_T f;
11352
11353 rettv->v_type = VAR_FLOAT;
11354 if (get_float_arg(argvars, &f) == OK)
11355 rettv->vval.v_float = floor(f);
11356 else
11357 rettv->vval.v_float = 0.0;
11358}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011359
11360/*
11361 * "fmod()" function
11362 */
11363 static void
11364f_fmod(argvars, rettv)
11365 typval_T *argvars;
11366 typval_T *rettv;
11367{
11368 float_T fx, fy;
11369
11370 rettv->v_type = VAR_FLOAT;
11371 if (get_float_arg(argvars, &fx) == OK
11372 && get_float_arg(&argvars[1], &fy) == OK)
11373 rettv->vval.v_float = fmod(fx, fy);
11374 else
11375 rettv->vval.v_float = 0.0;
11376}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011377#endif
11378
Bram Moolenaar0d660222005-01-07 21:51:51 +000011379/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011380 * "fnameescape({string})" function
11381 */
11382 static void
11383f_fnameescape(argvars, rettv)
11384 typval_T *argvars;
11385 typval_T *rettv;
11386{
11387 rettv->vval.v_string = vim_strsave_fnameescape(
11388 get_tv_string(&argvars[0]), FALSE);
11389 rettv->v_type = VAR_STRING;
11390}
11391
11392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 * "fnamemodify({fname}, {mods})" function
11394 */
11395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011397 typval_T *argvars;
11398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399{
11400 char_u *fname;
11401 char_u *mods;
11402 int usedlen = 0;
11403 int len;
11404 char_u *fbuf = NULL;
11405 char_u buf[NUMBUFLEN];
11406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011407 fname = get_tv_string_chk(&argvars[0]);
11408 mods = get_tv_string_buf_chk(&argvars[1], buf);
11409 if (fname == NULL || mods == NULL)
11410 fname = NULL;
11411 else
11412 {
11413 len = (int)STRLEN(fname);
11414 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011419 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 vim_free(fbuf);
11423}
11424
Bram Moolenaar33570922005-01-25 22:26:29 +000011425static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426
11427/*
11428 * "foldclosed()" function
11429 */
11430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011432 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011433 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011434 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435{
11436#ifdef FEAT_FOLDING
11437 linenr_T lnum;
11438 linenr_T first, last;
11439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11442 {
11443 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11444 {
11445 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 return;
11450 }
11451 }
11452#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454}
11455
11456/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011457 * "foldclosed()" function
11458 */
11459 static void
11460f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011461 typval_T *argvars;
11462 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011463{
11464 foldclosed_both(argvars, rettv, FALSE);
11465}
11466
11467/*
11468 * "foldclosedend()" function
11469 */
11470 static void
11471f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011472 typval_T *argvars;
11473 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011474{
11475 foldclosed_both(argvars, rettv, TRUE);
11476}
11477
11478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 * "foldlevel()" function
11480 */
11481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011483 typval_T *argvars UNUSED;
11484 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485{
11486#ifdef FEAT_FOLDING
11487 linenr_T lnum;
11488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011489 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493}
11494
11495/*
11496 * "foldtext()" function
11497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011500 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502{
11503#ifdef FEAT_FOLDING
11504 linenr_T lnum;
11505 char_u *s;
11506 char_u *r;
11507 int len;
11508 char *txt;
11509#endif
11510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511 rettv->v_type = VAR_STRING;
11512 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011514 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11515 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11516 <= curbuf->b_ml.ml_line_count
11517 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 {
11519 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011520 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11521 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 {
11523 if (!linewhite(lnum))
11524 break;
11525 ++lnum;
11526 }
11527
11528 /* Find interesting text in this line. */
11529 s = skipwhite(ml_get(lnum));
11530 /* skip C comment-start */
11531 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011534 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011535 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011536 {
11537 s = skipwhite(ml_get(lnum + 1));
11538 if (*s == '*')
11539 s = skipwhite(s + 1);
11540 }
11541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 txt = _("+-%s%3ld lines: ");
11543 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011544 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545 + 20 /* for %3ld */
11546 + STRLEN(s))); /* concatenated */
11547 if (r != NULL)
11548 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011549 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11550 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11551 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552 len = (int)STRLEN(r);
11553 STRCAT(r, s);
11554 /* remove 'foldmarker' and 'commentstring' */
11555 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011556 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 }
11558 }
11559#endif
11560}
11561
11562/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011563 * "foldtextresult(lnum)" function
11564 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011567 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011568 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011569{
11570#ifdef FEAT_FOLDING
11571 linenr_T lnum;
11572 char_u *text;
11573 char_u buf[51];
11574 foldinfo_T foldinfo;
11575 int fold_count;
11576#endif
11577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011578 rettv->v_type = VAR_STRING;
11579 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011580#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011581 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011582 /* treat illegal types and illegal string values for {lnum} the same */
11583 if (lnum < 0)
11584 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011585 fold_count = foldedCount(curwin, lnum, &foldinfo);
11586 if (fold_count > 0)
11587 {
11588 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11589 &foldinfo, buf);
11590 if (text == buf)
11591 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011593 }
11594#endif
11595}
11596
11597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598 * "foreground()" function
11599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011601f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011602 typval_T *argvars UNUSED;
11603 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605#ifdef FEAT_GUI
11606 if (gui.in_use)
11607 gui_mch_set_foreground();
11608#else
11609# ifdef WIN32
11610 win32_set_foreground();
11611# endif
11612#endif
11613}
11614
11615/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011616 * "function()" function
11617 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011620 typval_T *argvars;
11621 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011622{
11623 char_u *s;
11624
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011625 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011626 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011627 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011628 /* Don't check an autoload name for existence here. */
11629 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011630 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011631 else
11632 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011633 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011634 {
11635 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011636 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011637
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011638 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11639 * also be called from another script. Using trans_function_name()
11640 * would also work, but some plugins depend on the name being
11641 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011642 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011643 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011644 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011645 if (rettv->vval.v_string != NULL)
11646 {
11647 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011648 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011649 }
11650 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011651 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011652 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011653 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011654 }
11655}
11656
11657/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011658 * "garbagecollect()" function
11659 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011660 static void
11661f_garbagecollect(argvars, rettv)
11662 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011663 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011664{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011665 /* This is postponed until we are back at the toplevel, because we may be
11666 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11667 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011668
11669 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11670 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011671}
11672
11673/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011674 * "get()" function
11675 */
11676 static void
11677f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011678 typval_T *argvars;
11679 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011680{
Bram Moolenaar33570922005-01-25 22:26:29 +000011681 listitem_T *li;
11682 list_T *l;
11683 dictitem_T *di;
11684 dict_T *d;
11685 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011686
Bram Moolenaare9a41262005-01-15 22:18:47 +000011687 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011688 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011689 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011691 int error = FALSE;
11692
11693 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11694 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011695 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011696 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011697 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011698 else if (argvars[0].v_type == VAR_DICT)
11699 {
11700 if ((d = argvars[0].vval.v_dict) != NULL)
11701 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011702 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011703 if (di != NULL)
11704 tv = &di->di_tv;
11705 }
11706 }
11707 else
11708 EMSG2(_(e_listdictarg), "get()");
11709
11710 if (tv == NULL)
11711 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011712 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011713 copy_tv(&argvars[2], rettv);
11714 }
11715 else
11716 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011717}
11718
Bram Moolenaar342337a2005-07-21 21:11:17 +000011719static 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 +000011720
11721/*
11722 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011723 * Return a range (from start to end) of lines in rettv from the specified
11724 * buffer.
11725 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011726 */
11727 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011728get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011729 buf_T *buf;
11730 linenr_T start;
11731 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011732 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011733 typval_T *rettv;
11734{
11735 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011736
Bram Moolenaar959a1432013-12-14 12:17:38 +010011737 rettv->v_type = VAR_STRING;
11738 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011739 if (retlist && rettv_list_alloc(rettv) == FAIL)
11740 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011741
11742 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11743 return;
11744
11745 if (!retlist)
11746 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011747 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11748 p = ml_get_buf(buf, start, FALSE);
11749 else
11750 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011751 rettv->vval.v_string = vim_strsave(p);
11752 }
11753 else
11754 {
11755 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011756 return;
11757
11758 if (start < 1)
11759 start = 1;
11760 if (end > buf->b_ml.ml_line_count)
11761 end = buf->b_ml.ml_line_count;
11762 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011763 if (list_append_string(rettv->vval.v_list,
11764 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011765 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011766 }
11767}
11768
11769/*
11770 * "getbufline()" function
11771 */
11772 static void
11773f_getbufline(argvars, rettv)
11774 typval_T *argvars;
11775 typval_T *rettv;
11776{
11777 linenr_T lnum;
11778 linenr_T end;
11779 buf_T *buf;
11780
11781 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11782 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011783 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011784 --emsg_off;
11785
Bram Moolenaar661b1822005-07-28 22:36:45 +000011786 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011787 if (argvars[2].v_type == VAR_UNKNOWN)
11788 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011789 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011790 end = get_tv_lnum_buf(&argvars[2], buf);
11791
Bram Moolenaar342337a2005-07-21 21:11:17 +000011792 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011793}
11794
Bram Moolenaar0d660222005-01-07 21:51:51 +000011795/*
11796 * "getbufvar()" function
11797 */
11798 static void
11799f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011800 typval_T *argvars;
11801 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011802{
11803 buf_T *buf;
11804 buf_T *save_curbuf;
11805 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011806 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011807 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011809 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11810 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011811 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011812 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011813
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011814 rettv->v_type = VAR_STRING;
11815 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011816
11817 if (buf != NULL && varname != NULL)
11818 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011819 /* set curbuf to be our buf, temporarily */
11820 save_curbuf = curbuf;
11821 curbuf = buf;
11822
Bram Moolenaar0d660222005-01-07 21:51:51 +000011823 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011824 {
11825 if (get_option_tv(&varname, rettv, TRUE) == OK)
11826 done = TRUE;
11827 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011828 else if (STRCMP(varname, "changedtick") == 0)
11829 {
11830 rettv->v_type = VAR_NUMBER;
11831 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011832 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011833 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011834 else
11835 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011836 /* Look up the variable. */
11837 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11838 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11839 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011840 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011841 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011842 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011843 done = TRUE;
11844 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011845 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011846
11847 /* restore previous notion of curbuf */
11848 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011849 }
11850
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011851 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11852 /* use the default value */
11853 copy_tv(&argvars[2], rettv);
11854
Bram Moolenaar0d660222005-01-07 21:51:51 +000011855 --emsg_off;
11856}
11857
11858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 * "getchar()" function
11860 */
11861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011862f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011863 typval_T *argvars;
11864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865{
11866 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011867 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011869 /* Position the cursor. Needed after a message that ends in a space. */
11870 windgoto(msg_row, msg_col);
11871
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 ++no_mapping;
11873 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011874 for (;;)
11875 {
11876 if (argvars[0].v_type == VAR_UNKNOWN)
11877 /* getchar(): blocking wait. */
11878 n = safe_vgetc();
11879 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11880 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011881 n = vpeekc_any();
11882 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011883 /* illegal argument or getchar(0) and no char avail: return zero */
11884 n = 0;
11885 else
11886 /* getchar(0) and char avail: return char */
11887 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011888
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011889 if (n == K_IGNORE)
11890 continue;
11891 break;
11892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893 --no_mapping;
11894 --allow_keys;
11895
Bram Moolenaar219b8702006-11-01 14:32:36 +000011896 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11897 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11898 vimvars[VV_MOUSE_COL].vv_nr = 0;
11899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011900 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901 if (IS_SPECIAL(n) || mod_mask != 0)
11902 {
11903 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11904 int i = 0;
11905
11906 /* Turn a special key into three bytes, plus modifier. */
11907 if (mod_mask != 0)
11908 {
11909 temp[i++] = K_SPECIAL;
11910 temp[i++] = KS_MODIFIER;
11911 temp[i++] = mod_mask;
11912 }
11913 if (IS_SPECIAL(n))
11914 {
11915 temp[i++] = K_SPECIAL;
11916 temp[i++] = K_SECOND(n);
11917 temp[i++] = K_THIRD(n);
11918 }
11919#ifdef FEAT_MBYTE
11920 else if (has_mbyte)
11921 i += (*mb_char2bytes)(n, temp + i);
11922#endif
11923 else
11924 temp[i++] = n;
11925 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011926 rettv->v_type = VAR_STRING;
11927 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011928
11929#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011930 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011931 {
11932 int row = mouse_row;
11933 int col = mouse_col;
11934 win_T *win;
11935 linenr_T lnum;
11936# ifdef FEAT_WINDOWS
11937 win_T *wp;
11938# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011939 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011940
11941 if (row >= 0 && col >= 0)
11942 {
11943 /* Find the window at the mouse coordinates and compute the
11944 * text position. */
11945 win = mouse_find_win(&row, &col);
11946 (void)mouse_comp_pos(win, &row, &col, &lnum);
11947# ifdef FEAT_WINDOWS
11948 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011949 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011950# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011951 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011952 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11953 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11954 }
11955 }
11956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 }
11958}
11959
11960/*
11961 * "getcharmod()" function
11962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011964f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011965 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969}
11970
11971/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011972 * "getcharsearch()" function
11973 */
11974 static void
11975f_getcharsearch(argvars, rettv)
11976 typval_T *argvars UNUSED;
11977 typval_T *rettv;
11978{
11979 if (rettv_dict_alloc(rettv) != FAIL)
11980 {
11981 dict_T *dict = rettv->vval.v_dict;
11982
11983 dict_add_nr_str(dict, "char", 0L, last_csearch());
11984 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11985 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11986 }
11987}
11988
11989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 * "getcmdline()" function
11991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011994 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997 rettv->v_type = VAR_STRING;
11998 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999}
12000
12001/*
12002 * "getcmdpos()" function
12003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012006 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012009 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010}
12011
12012/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012013 * "getcmdtype()" function
12014 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012015 static void
12016f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012017 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012018 typval_T *rettv;
12019{
12020 rettv->v_type = VAR_STRING;
12021 rettv->vval.v_string = alloc(2);
12022 if (rettv->vval.v_string != NULL)
12023 {
12024 rettv->vval.v_string[0] = get_cmdline_type();
12025 rettv->vval.v_string[1] = NUL;
12026 }
12027}
12028
12029/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012030 * "getcmdwintype()" function
12031 */
12032 static void
12033f_getcmdwintype(argvars, rettv)
12034 typval_T *argvars UNUSED;
12035 typval_T *rettv;
12036{
12037 rettv->v_type = VAR_STRING;
12038 rettv->vval.v_string = NULL;
12039#ifdef FEAT_CMDWIN
12040 rettv->vval.v_string = alloc(2);
12041 if (rettv->vval.v_string != NULL)
12042 {
12043 rettv->vval.v_string[0] = cmdwin_type;
12044 rettv->vval.v_string[1] = NUL;
12045 }
12046#endif
12047}
12048
12049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 * "getcwd()" function
12051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053f_getcwd(argvars, rettv)
Bram Moolenaarc9703302016-01-17 21:49:33 +010012054 typval_T *argvars;
Bram Moolenaar33570922005-01-25 22:26:29 +000012055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012057 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012058 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012060 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012061 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012062
12063 wp = find_tabwin(&argvars[0], &argvars[1]);
12064 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012066 if (wp->w_localdir != NULL)
12067 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12068 else if(globaldir != NULL)
12069 rettv->vval.v_string = vim_strsave(globaldir);
12070 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012071 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012072 cwd = alloc(MAXPATHL);
12073 if (cwd != NULL)
12074 {
12075 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12076 rettv->vval.v_string = vim_strsave(cwd);
12077 vim_free(cwd);
12078 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012079 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012080#ifdef BACKSLASH_IN_FILENAME
12081 if (rettv->vval.v_string != NULL)
12082 slash_adjust(rettv->vval.v_string);
12083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 }
12085}
12086
12087/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012088 * "getfontname()" function
12089 */
12090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012091f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012092 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012093 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012094{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095 rettv->v_type = VAR_STRING;
12096 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012097#ifdef FEAT_GUI
12098 if (gui.in_use)
12099 {
12100 GuiFont font;
12101 char_u *name = NULL;
12102
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012103 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012104 {
12105 /* Get the "Normal" font. Either the name saved by
12106 * hl_set_font_name() or from the font ID. */
12107 font = gui.norm_font;
12108 name = hl_get_font_name();
12109 }
12110 else
12111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012112 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012113 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12114 return;
12115 font = gui_mch_get_font(name, FALSE);
12116 if (font == NOFONT)
12117 return; /* Invalid font name, return empty string. */
12118 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012119 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012120 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012121 gui_mch_free_font(font);
12122 }
12123#endif
12124}
12125
12126/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012127 * "getfperm({fname})" function
12128 */
12129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012130f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012131 typval_T *argvars;
12132 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012133{
12134 char_u *fname;
12135 struct stat st;
12136 char_u *perm = NULL;
12137 char_u flags[] = "rwx";
12138 int i;
12139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012140 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012143 if (mch_stat((char *)fname, &st) >= 0)
12144 {
12145 perm = vim_strsave((char_u *)"---------");
12146 if (perm != NULL)
12147 {
12148 for (i = 0; i < 9; i++)
12149 {
12150 if (st.st_mode & (1 << (8 - i)))
12151 perm[i] = flags[i % 3];
12152 }
12153 }
12154 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012156}
12157
12158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 * "getfsize({fname})" function
12160 */
12161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012162f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012163 typval_T *argvars;
12164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165{
12166 char_u *fname;
12167 struct stat st;
12168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012171 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172
12173 if (mch_stat((char *)fname, &st) >= 0)
12174 {
12175 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012176 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012179 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012180
12181 /* non-perfect check for overflow */
12182 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12183 rettv->vval.v_number = -2;
12184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185 }
12186 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012187 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188}
12189
12190/*
12191 * "getftime({fname})" function
12192 */
12193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012195 typval_T *argvars;
12196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197{
12198 char_u *fname;
12199 struct stat st;
12200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012201 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202
12203 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012206 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207}
12208
12209/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012210 * "getftype({fname})" function
12211 */
12212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012213f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012214 typval_T *argvars;
12215 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012216{
12217 char_u *fname;
12218 struct stat st;
12219 char_u *type = NULL;
12220 char *t;
12221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012222 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012224 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012225 if (mch_lstat((char *)fname, &st) >= 0)
12226 {
12227#ifdef S_ISREG
12228 if (S_ISREG(st.st_mode))
12229 t = "file";
12230 else if (S_ISDIR(st.st_mode))
12231 t = "dir";
12232# ifdef S_ISLNK
12233 else if (S_ISLNK(st.st_mode))
12234 t = "link";
12235# endif
12236# ifdef S_ISBLK
12237 else if (S_ISBLK(st.st_mode))
12238 t = "bdev";
12239# endif
12240# ifdef S_ISCHR
12241 else if (S_ISCHR(st.st_mode))
12242 t = "cdev";
12243# endif
12244# ifdef S_ISFIFO
12245 else if (S_ISFIFO(st.st_mode))
12246 t = "fifo";
12247# endif
12248# ifdef S_ISSOCK
12249 else if (S_ISSOCK(st.st_mode))
12250 t = "fifo";
12251# endif
12252 else
12253 t = "other";
12254#else
12255# ifdef S_IFMT
12256 switch (st.st_mode & S_IFMT)
12257 {
12258 case S_IFREG: t = "file"; break;
12259 case S_IFDIR: t = "dir"; break;
12260# ifdef S_IFLNK
12261 case S_IFLNK: t = "link"; break;
12262# endif
12263# ifdef S_IFBLK
12264 case S_IFBLK: t = "bdev"; break;
12265# endif
12266# ifdef S_IFCHR
12267 case S_IFCHR: t = "cdev"; break;
12268# endif
12269# ifdef S_IFIFO
12270 case S_IFIFO: t = "fifo"; break;
12271# endif
12272# ifdef S_IFSOCK
12273 case S_IFSOCK: t = "socket"; break;
12274# endif
12275 default: t = "other";
12276 }
12277# else
12278 if (mch_isdir(fname))
12279 t = "dir";
12280 else
12281 t = "file";
12282# endif
12283#endif
12284 type = vim_strsave((char_u *)t);
12285 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012286 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012287}
12288
12289/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012290 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012291 */
12292 static void
12293f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012294 typval_T *argvars;
12295 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012296{
12297 linenr_T lnum;
12298 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012299 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012300
12301 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012302 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012303 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012304 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012305 retlist = FALSE;
12306 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012307 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012308 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012309 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012310 retlist = TRUE;
12311 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012312
Bram Moolenaar342337a2005-07-21 21:11:17 +000012313 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012314}
12315
12316/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012317 * "getmatches()" function
12318 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012319 static void
12320f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012321 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012322 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012323{
12324#ifdef FEAT_SEARCH_EXTRA
12325 dict_T *dict;
12326 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012327 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012328
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012329 if (rettv_list_alloc(rettv) == OK)
12330 {
12331 while (cur != NULL)
12332 {
12333 dict = dict_alloc();
12334 if (dict == NULL)
12335 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012336 if (cur->match.regprog == NULL)
12337 {
12338 /* match added with matchaddpos() */
12339 for (i = 0; i < MAXPOSMATCH; ++i)
12340 {
12341 llpos_T *llpos;
12342 char buf[6];
12343 list_T *l;
12344
12345 llpos = &cur->pos.pos[i];
12346 if (llpos->lnum == 0)
12347 break;
12348 l = list_alloc();
12349 if (l == NULL)
12350 break;
12351 list_append_number(l, (varnumber_T)llpos->lnum);
12352 if (llpos->col > 0)
12353 {
12354 list_append_number(l, (varnumber_T)llpos->col);
12355 list_append_number(l, (varnumber_T)llpos->len);
12356 }
12357 sprintf(buf, "pos%d", i + 1);
12358 dict_add_list(dict, buf, l);
12359 }
12360 }
12361 else
12362 {
12363 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12364 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012365 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012366 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12367 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012368# ifdef FEAT_CONCEAL
12369 if (cur->conceal_char)
12370 {
12371 char_u buf[MB_MAXBYTES + 1];
12372
12373 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12374 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12375 }
12376# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012377 list_append_dict(rettv->vval.v_list, dict);
12378 cur = cur->next;
12379 }
12380 }
12381#endif
12382}
12383
12384/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012385 * "getpid()" function
12386 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012387 static void
12388f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012389 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012390 typval_T *rettv;
12391{
12392 rettv->vval.v_number = mch_get_pid();
12393}
12394
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012395static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12396
12397/*
12398 * "getcurpos()" function
12399 */
12400 static void
12401f_getcurpos(argvars, rettv)
12402 typval_T *argvars;
12403 typval_T *rettv;
12404{
12405 getpos_both(argvars, rettv, TRUE);
12406}
12407
Bram Moolenaar18081e32008-02-20 19:11:07 +000012408/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012409 * "getpos(string)" function
12410 */
12411 static void
12412f_getpos(argvars, rettv)
12413 typval_T *argvars;
12414 typval_T *rettv;
12415{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012416 getpos_both(argvars, rettv, FALSE);
12417}
12418
12419 static void
12420getpos_both(argvars, rettv, getcurpos)
12421 typval_T *argvars;
12422 typval_T *rettv;
12423 int getcurpos;
12424{
Bram Moolenaara5525202006-03-02 22:52:09 +000012425 pos_T *fp;
12426 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012427 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012428
12429 if (rettv_list_alloc(rettv) == OK)
12430 {
12431 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012432 if (getcurpos)
12433 fp = &curwin->w_cursor;
12434 else
12435 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012436 if (fnum != -1)
12437 list_append_number(l, (varnumber_T)fnum);
12438 else
12439 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012440 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12441 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012442 list_append_number(l, (fp != NULL)
12443 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012444 : (varnumber_T)0);
12445 list_append_number(l,
12446#ifdef FEAT_VIRTUALEDIT
12447 (fp != NULL) ? (varnumber_T)fp->coladd :
12448#endif
12449 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012450 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012451 list_append_number(l, curwin->w_curswant == MAXCOL ?
12452 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012453 }
12454 else
12455 rettv->vval.v_number = FALSE;
12456}
12457
12458/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012459 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012460 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012461 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012462f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012463 typval_T *argvars UNUSED;
12464 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012465{
12466#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012467 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012468#endif
12469
Bram Moolenaar2641f772005-03-25 21:58:17 +000012470#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012471 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012472 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012473 wp = NULL;
12474 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12475 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012476 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012477 if (wp == NULL)
12478 return;
12479 }
12480
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012481 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012482 }
12483#endif
12484}
12485
12486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487 * "getreg()" function
12488 */
12489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012490f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012491 typval_T *argvars;
12492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493{
12494 char_u *strregname;
12495 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012496 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012497 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012498 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012500 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012502 strregname = get_tv_string_chk(&argvars[0]);
12503 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012504 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012506 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012507 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12508 return_list = get_tv_number_chk(&argvars[2], &error);
12509 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012512 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012513
12514 if (error)
12515 return;
12516
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517 regname = (strregname == NULL ? '"' : *strregname);
12518 if (regname == 0)
12519 regname = '"';
12520
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012521 if (return_list)
12522 {
12523 rettv->v_type = VAR_LIST;
12524 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12525 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012526 if (rettv->vval.v_list != NULL)
12527 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012528 }
12529 else
12530 {
12531 rettv->v_type = VAR_STRING;
12532 rettv->vval.v_string = get_reg_contents(regname,
12533 arg2 ? GREG_EXPR_SRC : 0);
12534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535}
12536
12537/*
12538 * "getregtype()" function
12539 */
12540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012541f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012542 typval_T *argvars;
12543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544{
12545 char_u *strregname;
12546 int regname;
12547 char_u buf[NUMBUFLEN + 2];
12548 long reglen = 0;
12549
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012550 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012551 {
12552 strregname = get_tv_string_chk(&argvars[0]);
12553 if (strregname == NULL) /* type error; errmsg already given */
12554 {
12555 rettv->v_type = VAR_STRING;
12556 rettv->vval.v_string = NULL;
12557 return;
12558 }
12559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560 else
12561 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012562 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563
12564 regname = (strregname == NULL ? '"' : *strregname);
12565 if (regname == 0)
12566 regname = '"';
12567
12568 buf[0] = NUL;
12569 buf[1] = NUL;
12570 switch (get_reg_type(regname, &reglen))
12571 {
12572 case MLINE: buf[0] = 'V'; break;
12573 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574 case MBLOCK:
12575 buf[0] = Ctrl_V;
12576 sprintf((char *)buf + 1, "%ld", reglen + 1);
12577 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012579 rettv->v_type = VAR_STRING;
12580 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581}
12582
12583/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012584 * "gettabvar()" function
12585 */
12586 static void
12587f_gettabvar(argvars, rettv)
12588 typval_T *argvars;
12589 typval_T *rettv;
12590{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012591 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012592 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012593 dictitem_T *v;
12594 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012595 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012596
12597 rettv->v_type = VAR_STRING;
12598 rettv->vval.v_string = NULL;
12599
12600 varname = get_tv_string_chk(&argvars[1]);
12601 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12602 if (tp != NULL && varname != NULL)
12603 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012604 /* Set tp to be our tabpage, temporarily. Also set the window to the
12605 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012606 if (switch_win(&oldcurwin, &oldtabpage,
12607 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012608 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012609 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012610 /* look up the variable */
12611 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12612 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12613 if (v != NULL)
12614 {
12615 copy_tv(&v->di_tv, rettv);
12616 done = TRUE;
12617 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012618 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012619
12620 /* restore previous notion of curwin */
12621 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012622 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012623
12624 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012625 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012626}
12627
12628/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012629 * "gettabwinvar()" function
12630 */
12631 static void
12632f_gettabwinvar(argvars, rettv)
12633 typval_T *argvars;
12634 typval_T *rettv;
12635{
12636 getwinvar(argvars, rettv, 1);
12637}
12638
12639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640 * "getwinposx()" function
12641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012644 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012647 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648#ifdef FEAT_GUI
12649 if (gui.in_use)
12650 {
12651 int x, y;
12652
12653 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012654 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655 }
12656#endif
12657}
12658
12659/*
12660 * "getwinposy()" function
12661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012663f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012664 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012667 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668#ifdef FEAT_GUI
12669 if (gui.in_use)
12670 {
12671 int x, y;
12672
12673 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012674 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675 }
12676#endif
12677}
12678
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012679/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012680 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012681 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012682 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012683find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012684 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012685 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012686{
12687#ifdef FEAT_WINDOWS
12688 win_T *wp;
12689#endif
12690 int nr;
12691
12692 nr = get_tv_number_chk(vp, NULL);
12693
12694#ifdef FEAT_WINDOWS
12695 if (nr < 0)
12696 return NULL;
12697 if (nr == 0)
12698 return curwin;
12699
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012700 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12701 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012702 if (--nr <= 0)
12703 break;
12704 return wp;
12705#else
12706 if (nr == 0 || nr == 1)
12707 return curwin;
12708 return NULL;
12709#endif
12710}
12711
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012713 * Find window specified by "wvp" in tabpage "tvp".
12714 */
12715 static win_T *
12716find_tabwin(wvp, tvp)
12717 typval_T *wvp; /* VAR_UNKNOWN for current window */
12718 typval_T *tvp; /* VAR_UNKNOWN for current tab page */
12719{
12720 win_T *wp = NULL;
12721 tabpage_T *tp = NULL;
12722 long n;
12723
12724 if (wvp->v_type != VAR_UNKNOWN)
12725 {
12726 if (tvp->v_type != VAR_UNKNOWN)
12727 {
12728 n = get_tv_number(tvp);
12729 if (n >= 0)
12730 tp = find_tabpage(n);
12731 }
12732 else
12733 tp = curtab;
12734
12735 if (tp != NULL)
12736 wp = find_win_by_nr(wvp, tp);
12737 }
12738 else
12739 wp = curwin;
12740
12741 return wp;
12742}
12743
12744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745 * "getwinvar()" function
12746 */
12747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012748f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012749 typval_T *argvars;
12750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012752 getwinvar(argvars, rettv, 0);
12753}
12754
12755/*
12756 * getwinvar() and gettabwinvar()
12757 */
12758 static void
12759getwinvar(argvars, rettv, off)
12760 typval_T *argvars;
12761 typval_T *rettv;
12762 int off; /* 1 for gettabwinvar() */
12763{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012764 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012766 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012767 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012768 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012769#ifdef FEAT_WINDOWS
12770 win_T *oldcurwin;
12771 tabpage_T *oldtabpage;
12772 int need_switch_win;
12773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012775#ifdef FEAT_WINDOWS
12776 if (off == 1)
12777 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12778 else
12779 tp = curtab;
12780#endif
12781 win = find_win_by_nr(&argvars[off], tp);
12782 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012783 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012785 rettv->v_type = VAR_STRING;
12786 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787
12788 if (win != NULL && varname != NULL)
12789 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012790#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012791 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012792 * otherwise the window is not valid. Only do this when needed,
12793 * autocommands get blocked. */
12794 need_switch_win = !(tp == curtab && win == curwin);
12795 if (!need_switch_win
12796 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12797#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012798 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012799 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012800 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012801 if (get_option_tv(&varname, rettv, 1) == OK)
12802 done = TRUE;
12803 }
12804 else
12805 {
12806 /* Look up the variable. */
12807 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12808 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12809 varname, FALSE);
12810 if (v != NULL)
12811 {
12812 copy_tv(&v->di_tv, rettv);
12813 done = TRUE;
12814 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012817
Bram Moolenaarba117c22015-09-29 16:53:22 +020012818#ifdef FEAT_WINDOWS
12819 if (need_switch_win)
12820 /* restore previous notion of curwin */
12821 restore_win(oldcurwin, oldtabpage, TRUE);
12822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 }
12824
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012825 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12826 /* use the default return value */
12827 copy_tv(&argvars[off + 2], rettv);
12828
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829 --emsg_off;
12830}
12831
12832/*
12833 * "glob()" function
12834 */
12835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012836f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012837 typval_T *argvars;
12838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012840 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012842 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012844 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012845 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012847 if (argvars[1].v_type != VAR_UNKNOWN)
12848 {
12849 if (get_tv_number_chk(&argvars[1], &error))
12850 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012851 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012852 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012853 if (get_tv_number_chk(&argvars[2], &error))
12854 {
12855 rettv->v_type = VAR_LIST;
12856 rettv->vval.v_list = NULL;
12857 }
12858 if (argvars[3].v_type != VAR_UNKNOWN
12859 && get_tv_number_chk(&argvars[3], &error))
12860 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012861 }
12862 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012863 if (!error)
12864 {
12865 ExpandInit(&xpc);
12866 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012867 if (p_wic)
12868 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012869 if (rettv->v_type == VAR_STRING)
12870 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012871 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012872 else if (rettv_list_alloc(rettv) != FAIL)
12873 {
12874 int i;
12875
12876 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12877 NULL, options, WILD_ALL_KEEP);
12878 for (i = 0; i < xpc.xp_numfiles; i++)
12879 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12880
12881 ExpandCleanup(&xpc);
12882 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012883 }
12884 else
12885 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886}
12887
12888/*
12889 * "globpath()" function
12890 */
12891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012892f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012893 typval_T *argvars;
12894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012896 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012898 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012899 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012900 garray_T ga;
12901 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012903 /* When the optional second argument is non-zero, don't remove matches
12904 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012906 if (argvars[2].v_type != VAR_UNKNOWN)
12907 {
12908 if (get_tv_number_chk(&argvars[2], &error))
12909 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012910 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012911 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012912 if (get_tv_number_chk(&argvars[3], &error))
12913 {
12914 rettv->v_type = VAR_LIST;
12915 rettv->vval.v_list = NULL;
12916 }
12917 if (argvars[4].v_type != VAR_UNKNOWN
12918 && get_tv_number_chk(&argvars[4], &error))
12919 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012920 }
12921 }
12922 if (file != NULL && !error)
12923 {
12924 ga_init2(&ga, (int)sizeof(char_u *), 10);
12925 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12926 if (rettv->v_type == VAR_STRING)
12927 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12928 else if (rettv_list_alloc(rettv) != FAIL)
12929 for (i = 0; i < ga.ga_len; ++i)
12930 list_append_string(rettv->vval.v_list,
12931 ((char_u **)(ga.ga_data))[i], -1);
12932 ga_clear_strings(&ga);
12933 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012934 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012935 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936}
12937
12938/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012939 * "glob2regpat()" function
12940 */
12941 static void
12942f_glob2regpat(argvars, rettv)
12943 typval_T *argvars;
12944 typval_T *rettv;
12945{
12946 char_u *pat = get_tv_string_chk(&argvars[0]);
12947
12948 rettv->v_type = VAR_STRING;
12949 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12950}
12951
12952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953 * "has()" function
12954 */
12955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012956f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012957 typval_T *argvars;
12958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959{
12960 int i;
12961 char_u *name;
12962 int n = FALSE;
12963 static char *(has_list[]) =
12964 {
12965#ifdef AMIGA
12966 "amiga",
12967# ifdef FEAT_ARP
12968 "arp",
12969# endif
12970#endif
12971#ifdef __BEOS__
12972 "beos",
12973#endif
12974#ifdef MSDOS
12975# ifdef DJGPP
12976 "dos32",
12977# else
12978 "dos16",
12979# endif
12980#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012981#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982 "mac",
12983#endif
12984#if defined(MACOS_X_UNIX)
12985 "macunix",
12986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987#ifdef __QNX__
12988 "qnx",
12989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990#ifdef UNIX
12991 "unix",
12992#endif
12993#ifdef VMS
12994 "vms",
12995#endif
12996#ifdef WIN16
12997 "win16",
12998#endif
12999#ifdef WIN32
13000 "win32",
13001#endif
13002#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13003 "win32unix",
13004#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013005#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013006 "win64",
13007#endif
13008#ifdef EBCDIC
13009 "ebcdic",
13010#endif
13011#ifndef CASE_INSENSITIVE_FILENAME
13012 "fname_case",
13013#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013014#ifdef HAVE_ACL
13015 "acl",
13016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017#ifdef FEAT_ARABIC
13018 "arabic",
13019#endif
13020#ifdef FEAT_AUTOCMD
13021 "autocmd",
13022#endif
13023#ifdef FEAT_BEVAL
13024 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013025# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13026 "balloon_multiline",
13027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028#endif
13029#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13030 "builtin_terms",
13031# ifdef ALL_BUILTIN_TCAPS
13032 "all_builtin_terms",
13033# endif
13034#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013035#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13036 || defined(FEAT_GUI_W32) \
13037 || defined(FEAT_GUI_MOTIF))
13038 "browsefilter",
13039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040#ifdef FEAT_BYTEOFF
13041 "byte_offset",
13042#endif
13043#ifdef FEAT_CINDENT
13044 "cindent",
13045#endif
13046#ifdef FEAT_CLIENTSERVER
13047 "clientserver",
13048#endif
13049#ifdef FEAT_CLIPBOARD
13050 "clipboard",
13051#endif
13052#ifdef FEAT_CMDL_COMPL
13053 "cmdline_compl",
13054#endif
13055#ifdef FEAT_CMDHIST
13056 "cmdline_hist",
13057#endif
13058#ifdef FEAT_COMMENTS
13059 "comments",
13060#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013061#ifdef FEAT_CONCEAL
13062 "conceal",
13063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064#ifdef FEAT_CRYPT
13065 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013066 "crypt-blowfish",
13067 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068#endif
13069#ifdef FEAT_CSCOPE
13070 "cscope",
13071#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013072#ifdef FEAT_CURSORBIND
13073 "cursorbind",
13074#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013075#ifdef CURSOR_SHAPE
13076 "cursorshape",
13077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078#ifdef DEBUG
13079 "debug",
13080#endif
13081#ifdef FEAT_CON_DIALOG
13082 "dialog_con",
13083#endif
13084#ifdef FEAT_GUI_DIALOG
13085 "dialog_gui",
13086#endif
13087#ifdef FEAT_DIFF
13088 "diff",
13089#endif
13090#ifdef FEAT_DIGRAPHS
13091 "digraphs",
13092#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013093#ifdef FEAT_DIRECTX
13094 "directx",
13095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096#ifdef FEAT_DND
13097 "dnd",
13098#endif
13099#ifdef FEAT_EMACS_TAGS
13100 "emacs_tags",
13101#endif
13102 "eval", /* always present, of course! */
13103#ifdef FEAT_EX_EXTRA
13104 "ex_extra",
13105#endif
13106#ifdef FEAT_SEARCH_EXTRA
13107 "extra_search",
13108#endif
13109#ifdef FEAT_FKMAP
13110 "farsi",
13111#endif
13112#ifdef FEAT_SEARCHPATH
13113 "file_in_path",
13114#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013115#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013116 "filterpipe",
13117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118#ifdef FEAT_FIND_ID
13119 "find_in_path",
13120#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013121#ifdef FEAT_FLOAT
13122 "float",
13123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124#ifdef FEAT_FOLDING
13125 "folding",
13126#endif
13127#ifdef FEAT_FOOTER
13128 "footer",
13129#endif
13130#if !defined(USE_SYSTEM) && defined(UNIX)
13131 "fork",
13132#endif
13133#ifdef FEAT_GETTEXT
13134 "gettext",
13135#endif
13136#ifdef FEAT_GUI
13137 "gui",
13138#endif
13139#ifdef FEAT_GUI_ATHENA
13140# ifdef FEAT_GUI_NEXTAW
13141 "gui_neXtaw",
13142# else
13143 "gui_athena",
13144# endif
13145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146#ifdef FEAT_GUI_GTK
13147 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013150#ifdef FEAT_GUI_GNOME
13151 "gui_gnome",
13152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153#ifdef FEAT_GUI_MAC
13154 "gui_mac",
13155#endif
13156#ifdef FEAT_GUI_MOTIF
13157 "gui_motif",
13158#endif
13159#ifdef FEAT_GUI_PHOTON
13160 "gui_photon",
13161#endif
13162#ifdef FEAT_GUI_W16
13163 "gui_win16",
13164#endif
13165#ifdef FEAT_GUI_W32
13166 "gui_win32",
13167#endif
13168#ifdef FEAT_HANGULIN
13169 "hangul_input",
13170#endif
13171#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13172 "iconv",
13173#endif
13174#ifdef FEAT_INS_EXPAND
13175 "insert_expand",
13176#endif
13177#ifdef FEAT_JUMPLIST
13178 "jumplist",
13179#endif
13180#ifdef FEAT_KEYMAP
13181 "keymap",
13182#endif
13183#ifdef FEAT_LANGMAP
13184 "langmap",
13185#endif
13186#ifdef FEAT_LIBCALL
13187 "libcall",
13188#endif
13189#ifdef FEAT_LINEBREAK
13190 "linebreak",
13191#endif
13192#ifdef FEAT_LISP
13193 "lispindent",
13194#endif
13195#ifdef FEAT_LISTCMDS
13196 "listcmds",
13197#endif
13198#ifdef FEAT_LOCALMAP
13199 "localmap",
13200#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013201#ifdef FEAT_LUA
13202# ifndef DYNAMIC_LUA
13203 "lua",
13204# endif
13205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206#ifdef FEAT_MENU
13207 "menu",
13208#endif
13209#ifdef FEAT_SESSION
13210 "mksession",
13211#endif
13212#ifdef FEAT_MODIFY_FNAME
13213 "modify_fname",
13214#endif
13215#ifdef FEAT_MOUSE
13216 "mouse",
13217#endif
13218#ifdef FEAT_MOUSESHAPE
13219 "mouseshape",
13220#endif
13221#if defined(UNIX) || defined(VMS)
13222# ifdef FEAT_MOUSE_DEC
13223 "mouse_dec",
13224# endif
13225# ifdef FEAT_MOUSE_GPM
13226 "mouse_gpm",
13227# endif
13228# ifdef FEAT_MOUSE_JSB
13229 "mouse_jsbterm",
13230# endif
13231# ifdef FEAT_MOUSE_NET
13232 "mouse_netterm",
13233# endif
13234# ifdef FEAT_MOUSE_PTERM
13235 "mouse_pterm",
13236# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013237# ifdef FEAT_MOUSE_SGR
13238 "mouse_sgr",
13239# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013240# ifdef FEAT_SYSMOUSE
13241 "mouse_sysmouse",
13242# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013243# ifdef FEAT_MOUSE_URXVT
13244 "mouse_urxvt",
13245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246# ifdef FEAT_MOUSE_XTERM
13247 "mouse_xterm",
13248# endif
13249#endif
13250#ifdef FEAT_MBYTE
13251 "multi_byte",
13252#endif
13253#ifdef FEAT_MBYTE_IME
13254 "multi_byte_ime",
13255#endif
13256#ifdef FEAT_MULTI_LANG
13257 "multi_lang",
13258#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013259#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013260#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013261 "mzscheme",
13262#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264#ifdef FEAT_OLE
13265 "ole",
13266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267#ifdef FEAT_PATH_EXTRA
13268 "path_extra",
13269#endif
13270#ifdef FEAT_PERL
13271#ifndef DYNAMIC_PERL
13272 "perl",
13273#endif
13274#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013275#ifdef FEAT_PERSISTENT_UNDO
13276 "persistent_undo",
13277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278#ifdef FEAT_PYTHON
13279#ifndef DYNAMIC_PYTHON
13280 "python",
13281#endif
13282#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013283#ifdef FEAT_PYTHON3
13284#ifndef DYNAMIC_PYTHON3
13285 "python3",
13286#endif
13287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288#ifdef FEAT_POSTSCRIPT
13289 "postscript",
13290#endif
13291#ifdef FEAT_PRINTER
13292 "printer",
13293#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013294#ifdef FEAT_PROFILE
13295 "profile",
13296#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013297#ifdef FEAT_RELTIME
13298 "reltime",
13299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300#ifdef FEAT_QUICKFIX
13301 "quickfix",
13302#endif
13303#ifdef FEAT_RIGHTLEFT
13304 "rightleft",
13305#endif
13306#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13307 "ruby",
13308#endif
13309#ifdef FEAT_SCROLLBIND
13310 "scrollbind",
13311#endif
13312#ifdef FEAT_CMDL_INFO
13313 "showcmd",
13314 "cmdline_info",
13315#endif
13316#ifdef FEAT_SIGNS
13317 "signs",
13318#endif
13319#ifdef FEAT_SMARTINDENT
13320 "smartindent",
13321#endif
13322#ifdef FEAT_SNIFF
13323 "sniff",
13324#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013325#ifdef STARTUPTIME
13326 "startuptime",
13327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328#ifdef FEAT_STL_OPT
13329 "statusline",
13330#endif
13331#ifdef FEAT_SUN_WORKSHOP
13332 "sun_workshop",
13333#endif
13334#ifdef FEAT_NETBEANS_INTG
13335 "netbeans_intg",
13336#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013337#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013338 "spell",
13339#endif
13340#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341 "syntax",
13342#endif
13343#if defined(USE_SYSTEM) || !defined(UNIX)
13344 "system",
13345#endif
13346#ifdef FEAT_TAG_BINS
13347 "tag_binary",
13348#endif
13349#ifdef FEAT_TAG_OLDSTATIC
13350 "tag_old_static",
13351#endif
13352#ifdef FEAT_TAG_ANYWHITE
13353 "tag_any_white",
13354#endif
13355#ifdef FEAT_TCL
13356# ifndef DYNAMIC_TCL
13357 "tcl",
13358# endif
13359#endif
13360#ifdef TERMINFO
13361 "terminfo",
13362#endif
13363#ifdef FEAT_TERMRESPONSE
13364 "termresponse",
13365#endif
13366#ifdef FEAT_TEXTOBJ
13367 "textobjects",
13368#endif
13369#ifdef HAVE_TGETENT
13370 "tgetent",
13371#endif
13372#ifdef FEAT_TITLE
13373 "title",
13374#endif
13375#ifdef FEAT_TOOLBAR
13376 "toolbar",
13377#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013378#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13379 "unnamedplus",
13380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381#ifdef FEAT_USR_CMDS
13382 "user-commands", /* was accidentally included in 5.4 */
13383 "user_commands",
13384#endif
13385#ifdef FEAT_VIMINFO
13386 "viminfo",
13387#endif
13388#ifdef FEAT_VERTSPLIT
13389 "vertsplit",
13390#endif
13391#ifdef FEAT_VIRTUALEDIT
13392 "virtualedit",
13393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395#ifdef FEAT_VISUALEXTRA
13396 "visualextra",
13397#endif
13398#ifdef FEAT_VREPLACE
13399 "vreplace",
13400#endif
13401#ifdef FEAT_WILDIGN
13402 "wildignore",
13403#endif
13404#ifdef FEAT_WILDMENU
13405 "wildmenu",
13406#endif
13407#ifdef FEAT_WINDOWS
13408 "windows",
13409#endif
13410#ifdef FEAT_WAK
13411 "winaltkeys",
13412#endif
13413#ifdef FEAT_WRITEBACKUP
13414 "writebackup",
13415#endif
13416#ifdef FEAT_XIM
13417 "xim",
13418#endif
13419#ifdef FEAT_XFONTSET
13420 "xfontset",
13421#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013422#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013423 "xpm",
13424 "xpm_w32", /* for backward compatibility */
13425#else
13426# if defined(HAVE_XPM)
13427 "xpm",
13428# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430#ifdef USE_XSMP
13431 "xsmp",
13432#endif
13433#ifdef USE_XSMP_INTERACT
13434 "xsmp_interact",
13435#endif
13436#ifdef FEAT_XCLIPBOARD
13437 "xterm_clipboard",
13438#endif
13439#ifdef FEAT_XTERM_SAVE
13440 "xterm_save",
13441#endif
13442#if defined(UNIX) && defined(FEAT_X11)
13443 "X11",
13444#endif
13445 NULL
13446 };
13447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013448 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449 for (i = 0; has_list[i] != NULL; ++i)
13450 if (STRICMP(name, has_list[i]) == 0)
13451 {
13452 n = TRUE;
13453 break;
13454 }
13455
13456 if (n == FALSE)
13457 {
13458 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013459 {
13460 if (name[5] == '-'
13461 && STRLEN(name) > 11
13462 && vim_isdigit(name[6])
13463 && vim_isdigit(name[8])
13464 && vim_isdigit(name[10]))
13465 {
13466 int major = atoi((char *)name + 6);
13467 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013468
13469 /* Expect "patch-9.9.01234". */
13470 n = (major < VIM_VERSION_MAJOR
13471 || (major == VIM_VERSION_MAJOR
13472 && (minor < VIM_VERSION_MINOR
13473 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013474 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013475 }
13476 else
13477 n = has_patch(atoi((char *)name + 5));
13478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479 else if (STRICMP(name, "vim_starting") == 0)
13480 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013481#ifdef FEAT_MBYTE
13482 else if (STRICMP(name, "multi_byte_encoding") == 0)
13483 n = has_mbyte;
13484#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013485#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13486 else if (STRICMP(name, "balloon_multiline") == 0)
13487 n = multiline_balloon_available();
13488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489#ifdef DYNAMIC_TCL
13490 else if (STRICMP(name, "tcl") == 0)
13491 n = tcl_enabled(FALSE);
13492#endif
13493#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13494 else if (STRICMP(name, "iconv") == 0)
13495 n = iconv_enabled(FALSE);
13496#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013497#ifdef DYNAMIC_LUA
13498 else if (STRICMP(name, "lua") == 0)
13499 n = lua_enabled(FALSE);
13500#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013501#ifdef DYNAMIC_MZSCHEME
13502 else if (STRICMP(name, "mzscheme") == 0)
13503 n = mzscheme_enabled(FALSE);
13504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505#ifdef DYNAMIC_RUBY
13506 else if (STRICMP(name, "ruby") == 0)
13507 n = ruby_enabled(FALSE);
13508#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013509#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510#ifdef DYNAMIC_PYTHON
13511 else if (STRICMP(name, "python") == 0)
13512 n = python_enabled(FALSE);
13513#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013514#endif
13515#ifdef FEAT_PYTHON3
13516#ifdef DYNAMIC_PYTHON3
13517 else if (STRICMP(name, "python3") == 0)
13518 n = python3_enabled(FALSE);
13519#endif
13520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521#ifdef DYNAMIC_PERL
13522 else if (STRICMP(name, "perl") == 0)
13523 n = perl_enabled(FALSE);
13524#endif
13525#ifdef FEAT_GUI
13526 else if (STRICMP(name, "gui_running") == 0)
13527 n = (gui.in_use || gui.starting);
13528# ifdef FEAT_GUI_W32
13529 else if (STRICMP(name, "gui_win32s") == 0)
13530 n = gui_is_win32s();
13531# endif
13532# ifdef FEAT_BROWSE
13533 else if (STRICMP(name, "browse") == 0)
13534 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13535# endif
13536#endif
13537#ifdef FEAT_SYN_HL
13538 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013539 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540#endif
13541#if defined(WIN3264)
13542 else if (STRICMP(name, "win95") == 0)
13543 n = mch_windows95();
13544#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013545#ifdef FEAT_NETBEANS_INTG
13546 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013547 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549 }
13550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552}
13553
13554/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013555 * "has_key()" function
13556 */
13557 static void
13558f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013559 typval_T *argvars;
13560 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013561{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013562 if (argvars[0].v_type != VAR_DICT)
13563 {
13564 EMSG(_(e_dictreq));
13565 return;
13566 }
13567 if (argvars[0].vval.v_dict == NULL)
13568 return;
13569
13570 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013571 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013572}
13573
13574/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013575 * "haslocaldir()" function
13576 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013577 static void
13578f_haslocaldir(argvars, rettv)
Bram Moolenaarc9703302016-01-17 21:49:33 +010013579 typval_T *argvars;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013580 typval_T *rettv;
13581{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013582 win_T *wp = NULL;
13583
13584 wp = find_tabwin(&argvars[0], &argvars[1]);
13585 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013586}
13587
13588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 * "hasmapto()" function
13590 */
13591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013593 typval_T *argvars;
13594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595{
13596 char_u *name;
13597 char_u *mode;
13598 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013599 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013601 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013602 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603 mode = (char_u *)"nvo";
13604 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013605 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013606 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013607 if (argvars[2].v_type != VAR_UNKNOWN)
13608 abbr = get_tv_number(&argvars[2]);
13609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610
Bram Moolenaar2c932302006-03-18 21:42:09 +000013611 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013612 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615}
13616
13617/*
13618 * "histadd()" function
13619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013621f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013622 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624{
13625#ifdef FEAT_CMDHIST
13626 int histype;
13627 char_u *str;
13628 char_u buf[NUMBUFLEN];
13629#endif
13630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 if (check_restricted() || check_secure())
13633 return;
13634#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013635 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13636 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 if (histype >= 0)
13638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 if (*str != NUL)
13641 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013642 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 return;
13646 }
13647 }
13648#endif
13649}
13650
13651/*
13652 * "histdel()" function
13653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013655f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013656 typval_T *argvars UNUSED;
13657 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658{
13659#ifdef FEAT_CMDHIST
13660 int n;
13661 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013662 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013664 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13665 if (str == NULL)
13666 n = 0;
13667 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013669 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013670 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013672 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013673 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 else
13675 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013676 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677 get_tv_string_buf(&argvars[1], buf));
13678 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679#endif
13680}
13681
13682/*
13683 * "histget()" function
13684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013686f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013687 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689{
13690#ifdef FEAT_CMDHIST
13691 int type;
13692 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013693 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013695 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13696 if (str == NULL)
13697 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013699 {
13700 type = get_histtype(str);
13701 if (argvars[1].v_type == VAR_UNKNOWN)
13702 idx = get_history_idx(type);
13703 else
13704 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13705 /* -1 on type error */
13706 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013709 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013711 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712}
13713
13714/*
13715 * "histnr()" function
13716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013718f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013719 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721{
13722 int i;
13723
13724#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013725 char_u *history = get_tv_string_chk(&argvars[0]);
13726
13727 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728 if (i >= HIST_CMD && i < HIST_COUNT)
13729 i = get_history_idx(i);
13730 else
13731#endif
13732 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734}
13735
13736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 * "highlightID(name)" function
13738 */
13739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013740f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013741 typval_T *argvars;
13742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013744 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745}
13746
13747/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013748 * "highlight_exists()" function
13749 */
13750 static void
13751f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013752 typval_T *argvars;
13753 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013754{
13755 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13756}
13757
13758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759 * "hostname()" function
13760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013762f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013763 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765{
13766 char_u hostname[256];
13767
13768 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013769 rettv->v_type = VAR_STRING;
13770 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771}
13772
13773/*
13774 * iconv() function
13775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013777f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013778 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780{
13781#ifdef FEAT_MBYTE
13782 char_u buf1[NUMBUFLEN];
13783 char_u buf2[NUMBUFLEN];
13784 char_u *from, *to, *str;
13785 vimconv_T vimconv;
13786#endif
13787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013788 rettv->v_type = VAR_STRING;
13789 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790
13791#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013792 str = get_tv_string(&argvars[0]);
13793 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13794 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 vimconv.vc_type = CONV_NONE;
13796 convert_setup(&vimconv, from, to);
13797
13798 /* If the encodings are equal, no conversion needed. */
13799 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013800 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013802 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803
13804 convert_setup(&vimconv, NULL, NULL);
13805 vim_free(from);
13806 vim_free(to);
13807#endif
13808}
13809
13810/*
13811 * "indent()" function
13812 */
13813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013814f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013815 typval_T *argvars;
13816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817{
13818 linenr_T lnum;
13819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013820 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013822 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013824 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825}
13826
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013827/*
13828 * "index()" function
13829 */
13830 static void
13831f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013832 typval_T *argvars;
13833 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013834{
Bram Moolenaar33570922005-01-25 22:26:29 +000013835 list_T *l;
13836 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013837 long idx = 0;
13838 int ic = FALSE;
13839
13840 rettv->vval.v_number = -1;
13841 if (argvars[0].v_type != VAR_LIST)
13842 {
13843 EMSG(_(e_listreq));
13844 return;
13845 }
13846 l = argvars[0].vval.v_list;
13847 if (l != NULL)
13848 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013849 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013850 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013852 int error = FALSE;
13853
Bram Moolenaar758711c2005-02-02 23:11:38 +000013854 /* Start at specified item. Use the cached index that list_find()
13855 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013856 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013857 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013858 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013859 ic = get_tv_number_chk(&argvars[3], &error);
13860 if (error)
13861 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013862 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013863
Bram Moolenaar758711c2005-02-02 23:11:38 +000013864 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013865 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013866 {
13867 rettv->vval.v_number = idx;
13868 break;
13869 }
13870 }
13871}
13872
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873static int inputsecret_flag = 0;
13874
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013875static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13876
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013878 * This function is used by f_input() and f_inputdialog() functions. The third
13879 * argument to f_input() specifies the type of completion to use at the
13880 * prompt. The third argument to f_inputdialog() specifies the value to return
13881 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882 */
13883 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013884get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013885 typval_T *argvars;
13886 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013887 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013889 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890 char_u *p = NULL;
13891 int c;
13892 char_u buf[NUMBUFLEN];
13893 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013894 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013895 int xp_type = EXPAND_NOTHING;
13896 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013898 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013899 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900
13901#ifdef NO_CONSOLE_INPUT
13902 /* While starting up, there is no place to enter text. */
13903 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905#endif
13906
13907 cmd_silent = FALSE; /* Want to see the prompt. */
13908 if (prompt != NULL)
13909 {
13910 /* Only the part of the message after the last NL is considered as
13911 * prompt for the command line */
13912 p = vim_strrchr(prompt, '\n');
13913 if (p == NULL)
13914 p = prompt;
13915 else
13916 {
13917 ++p;
13918 c = *p;
13919 *p = NUL;
13920 msg_start();
13921 msg_clr_eos();
13922 msg_puts_attr(prompt, echo_attr);
13923 msg_didout = FALSE;
13924 msg_starthere();
13925 *p = c;
13926 }
13927 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013929 if (argvars[1].v_type != VAR_UNKNOWN)
13930 {
13931 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13932 if (defstr != NULL)
13933 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013935 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013936 {
13937 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013938 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013939 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013940
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013941 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013942 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013943
Bram Moolenaar4463f292005-09-25 22:20:24 +000013944 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13945 if (xp_name == NULL)
13946 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013947
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013948 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013949
Bram Moolenaar4463f292005-09-25 22:20:24 +000013950 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13951 &xp_arg) == FAIL)
13952 return;
13953 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013954 }
13955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013956 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013957 {
13958# ifdef FEAT_EX_EXTRA
13959 int save_ex_normal_busy = ex_normal_busy;
13960 ex_normal_busy = 0;
13961# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013962 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013963 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13964 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013965# ifdef FEAT_EX_EXTRA
13966 ex_normal_busy = save_ex_normal_busy;
13967# endif
13968 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013969 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013970 && argvars[1].v_type != VAR_UNKNOWN
13971 && argvars[2].v_type != VAR_UNKNOWN)
13972 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13973 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013974
13975 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013977 /* since the user typed this, no need to wait for return */
13978 need_wait_return = FALSE;
13979 msg_didout = FALSE;
13980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981 cmd_silent = cmd_silent_save;
13982}
13983
13984/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013985 * "input()" function
13986 * Also handles inputsecret() when inputsecret is set.
13987 */
13988 static void
13989f_input(argvars, rettv)
13990 typval_T *argvars;
13991 typval_T *rettv;
13992{
13993 get_user_input(argvars, rettv, FALSE);
13994}
13995
13996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997 * "inputdialog()" function
13998 */
13999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014000f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014001 typval_T *argvars;
14002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003{
14004#if defined(FEAT_GUI_TEXTDIALOG)
14005 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14006 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14007 {
14008 char_u *message;
14009 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014010 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014012 message = get_tv_string_chk(&argvars[0]);
14013 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014014 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014015 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 else
14017 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014018 if (message != NULL && defstr != NULL
14019 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014020 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014021 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 else
14023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014024 if (message != NULL && defstr != NULL
14025 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014026 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014027 rettv->vval.v_string = vim_strsave(
14028 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014030 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014032 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033 }
14034 else
14035#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014036 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037}
14038
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014039/*
14040 * "inputlist()" function
14041 */
14042 static void
14043f_inputlist(argvars, rettv)
14044 typval_T *argvars;
14045 typval_T *rettv;
14046{
14047 listitem_T *li;
14048 int selected;
14049 int mouse_used;
14050
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014051#ifdef NO_CONSOLE_INPUT
14052 /* While starting up, there is no place to enter text. */
14053 if (no_console_input())
14054 return;
14055#endif
14056 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14057 {
14058 EMSG2(_(e_listarg), "inputlist()");
14059 return;
14060 }
14061
14062 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014063 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014064 lines_left = Rows; /* avoid more prompt */
14065 msg_scroll = TRUE;
14066 msg_clr_eos();
14067
14068 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14069 {
14070 msg_puts(get_tv_string(&li->li_tv));
14071 msg_putchar('\n');
14072 }
14073
14074 /* Ask for choice. */
14075 selected = prompt_for_number(&mouse_used);
14076 if (mouse_used)
14077 selected -= lines_left;
14078
14079 rettv->vval.v_number = selected;
14080}
14081
14082
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14084
14085/*
14086 * "inputrestore()" function
14087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014089f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014090 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092{
14093 if (ga_userinput.ga_len > 0)
14094 {
14095 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14097 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014098 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099 }
14100 else if (p_verbose > 1)
14101 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014102 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014103 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104 }
14105}
14106
14107/*
14108 * "inputsave()" function
14109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014111f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014112 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014115 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 if (ga_grow(&ga_userinput, 1) == OK)
14117 {
14118 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14119 + ga_userinput.ga_len);
14120 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014121 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 }
14123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014124 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125}
14126
14127/*
14128 * "inputsecret()" function
14129 */
14130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014131f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014132 typval_T *argvars;
14133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134{
14135 ++cmdline_star;
14136 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014137 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138 --cmdline_star;
14139 --inputsecret_flag;
14140}
14141
14142/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014143 * "insert()" function
14144 */
14145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014146f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014147 typval_T *argvars;
14148 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014149{
14150 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014151 listitem_T *item;
14152 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014153 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014154
14155 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014156 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014157 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014158 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014159 {
14160 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014161 before = get_tv_number_chk(&argvars[2], &error);
14162 if (error)
14163 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014164
Bram Moolenaar758711c2005-02-02 23:11:38 +000014165 if (before == l->lv_len)
14166 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014167 else
14168 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014169 item = list_find(l, before);
14170 if (item == NULL)
14171 {
14172 EMSGN(_(e_listidx), before);
14173 l = NULL;
14174 }
14175 }
14176 if (l != NULL)
14177 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014178 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014179 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014180 }
14181 }
14182}
14183
14184/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014185 * "invert(expr)" function
14186 */
14187 static void
14188f_invert(argvars, rettv)
14189 typval_T *argvars;
14190 typval_T *rettv;
14191{
14192 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14193}
14194
14195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196 * "isdirectory()" function
14197 */
14198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014199f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014200 typval_T *argvars;
14201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014203 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204}
14205
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014206/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014207 * "islocked()" function
14208 */
14209 static void
14210f_islocked(argvars, rettv)
14211 typval_T *argvars;
14212 typval_T *rettv;
14213{
14214 lval_T lv;
14215 char_u *end;
14216 dictitem_T *di;
14217
14218 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014219 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14220 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014221 if (end != NULL && lv.ll_name != NULL)
14222 {
14223 if (*end != NUL)
14224 EMSG(_(e_trailing));
14225 else
14226 {
14227 if (lv.ll_tv == NULL)
14228 {
14229 if (check_changedtick(lv.ll_name))
14230 rettv->vval.v_number = 1; /* always locked */
14231 else
14232 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014233 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014234 if (di != NULL)
14235 {
14236 /* Consider a variable locked when:
14237 * 1. the variable itself is locked
14238 * 2. the value of the variable is locked.
14239 * 3. the List or Dict value is locked.
14240 */
14241 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14242 || tv_islocked(&di->di_tv));
14243 }
14244 }
14245 }
14246 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014247 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014248 else if (lv.ll_newkey != NULL)
14249 EMSG2(_(e_dictkey), lv.ll_newkey);
14250 else if (lv.ll_list != NULL)
14251 /* List item. */
14252 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14253 else
14254 /* Dictionary item. */
14255 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14256 }
14257 }
14258
14259 clear_lval(&lv);
14260}
14261
Bram Moolenaar33570922005-01-25 22:26:29 +000014262static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014263
14264/*
14265 * Turn a dict into a list:
14266 * "what" == 0: list of keys
14267 * "what" == 1: list of values
14268 * "what" == 2: list of items
14269 */
14270 static void
14271dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014272 typval_T *argvars;
14273 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014274 int what;
14275{
Bram Moolenaar33570922005-01-25 22:26:29 +000014276 list_T *l2;
14277 dictitem_T *di;
14278 hashitem_T *hi;
14279 listitem_T *li;
14280 listitem_T *li2;
14281 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014282 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014283
Bram Moolenaar8c711452005-01-14 21:53:12 +000014284 if (argvars[0].v_type != VAR_DICT)
14285 {
14286 EMSG(_(e_dictreq));
14287 return;
14288 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014289 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014290 return;
14291
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014292 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014293 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014294
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014295 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014296 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014297 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014298 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014300 --todo;
14301 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014302
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014303 li = listitem_alloc();
14304 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014305 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014306 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014307
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014308 if (what == 0)
14309 {
14310 /* keys() */
14311 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014312 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014313 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14314 }
14315 else if (what == 1)
14316 {
14317 /* values() */
14318 copy_tv(&di->di_tv, &li->li_tv);
14319 }
14320 else
14321 {
14322 /* items() */
14323 l2 = list_alloc();
14324 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014325 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014326 li->li_tv.vval.v_list = l2;
14327 if (l2 == NULL)
14328 break;
14329 ++l2->lv_refcount;
14330
14331 li2 = listitem_alloc();
14332 if (li2 == NULL)
14333 break;
14334 list_append(l2, li2);
14335 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014336 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014337 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14338
14339 li2 = listitem_alloc();
14340 if (li2 == NULL)
14341 break;
14342 list_append(l2, li2);
14343 copy_tv(&di->di_tv, &li2->li_tv);
14344 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014345 }
14346 }
14347}
14348
14349/*
14350 * "items(dict)" function
14351 */
14352 static void
14353f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014354 typval_T *argvars;
14355 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014356{
14357 dict_list(argvars, rettv, 2);
14358}
14359
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014361 * "join()" function
14362 */
14363 static void
14364f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014365 typval_T *argvars;
14366 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014367{
14368 garray_T ga;
14369 char_u *sep;
14370
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014371 if (argvars[0].v_type != VAR_LIST)
14372 {
14373 EMSG(_(e_listreq));
14374 return;
14375 }
14376 if (argvars[0].vval.v_list == NULL)
14377 return;
14378 if (argvars[1].v_type == VAR_UNKNOWN)
14379 sep = (char_u *)" ";
14380 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014381 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014382
14383 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014384
14385 if (sep != NULL)
14386 {
14387 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014388 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389 ga_append(&ga, NUL);
14390 rettv->vval.v_string = (char_u *)ga.ga_data;
14391 }
14392 else
14393 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014394}
14395
14396/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014397 * "keys()" function
14398 */
14399 static void
14400f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014401 typval_T *argvars;
14402 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014403{
14404 dict_list(argvars, rettv, 0);
14405}
14406
14407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 * "last_buffer_nr()" function.
14409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014412 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414{
14415 int n = 0;
14416 buf_T *buf;
14417
14418 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14419 if (n < buf->b_fnum)
14420 n = buf->b_fnum;
14421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014422 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014423}
14424
14425/*
14426 * "len()" function
14427 */
14428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014430 typval_T *argvars;
14431 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014432{
14433 switch (argvars[0].v_type)
14434 {
14435 case VAR_STRING:
14436 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014437 rettv->vval.v_number = (varnumber_T)STRLEN(
14438 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014439 break;
14440 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014441 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014442 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014443 case VAR_DICT:
14444 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14445 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014446 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014447 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014448 break;
14449 }
14450}
14451
Bram Moolenaar33570922005-01-25 22:26:29 +000014452static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014453
14454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014455libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014456 typval_T *argvars;
14457 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014458 int type;
14459{
14460#ifdef FEAT_LIBCALL
14461 char_u *string_in;
14462 char_u **string_result;
14463 int nr_result;
14464#endif
14465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014466 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014467 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014468 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014469
14470 if (check_restricted() || check_secure())
14471 return;
14472
14473#ifdef FEAT_LIBCALL
14474 /* The first two args must be strings, otherwise its meaningless */
14475 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14476 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014477 string_in = NULL;
14478 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014479 string_in = argvars[2].vval.v_string;
14480 if (type == VAR_NUMBER)
14481 string_result = NULL;
14482 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014483 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014484 if (mch_libcall(argvars[0].vval.v_string,
14485 argvars[1].vval.v_string,
14486 string_in,
14487 argvars[2].vval.v_number,
14488 string_result,
14489 &nr_result) == OK
14490 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014491 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014492 }
14493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494}
14495
14496/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014497 * "libcall()" function
14498 */
14499 static void
14500f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014501 typval_T *argvars;
14502 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014503{
14504 libcall_common(argvars, rettv, VAR_STRING);
14505}
14506
14507/*
14508 * "libcallnr()" function
14509 */
14510 static void
14511f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014512 typval_T *argvars;
14513 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014514{
14515 libcall_common(argvars, rettv, VAR_NUMBER);
14516}
14517
14518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519 * "line(string)" function
14520 */
14521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014522f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014523 typval_T *argvars;
14524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525{
14526 linenr_T lnum = 0;
14527 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014528 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014530 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 if (fp != NULL)
14532 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014533 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534}
14535
14536/*
14537 * "line2byte(lnum)" function
14538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014540f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014541 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543{
14544#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014545 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546#else
14547 linenr_T lnum;
14548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014549 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014551 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014553 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14554 if (rettv->vval.v_number >= 0)
14555 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556#endif
14557}
14558
14559/*
14560 * "lispindent(lnum)" function
14561 */
14562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014563f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014564 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566{
14567#ifdef FEAT_LISP
14568 pos_T pos;
14569 linenr_T lnum;
14570
14571 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014572 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14574 {
14575 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014576 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577 curwin->w_cursor = pos;
14578 }
14579 else
14580#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014581 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582}
14583
14584/*
14585 * "localtime()" function
14586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014589 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014592 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593}
14594
Bram Moolenaar33570922005-01-25 22:26:29 +000014595static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596
14597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014598get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014599 typval_T *argvars;
14600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601 int exact;
14602{
14603 char_u *keys;
14604 char_u *which;
14605 char_u buf[NUMBUFLEN];
14606 char_u *keys_buf = NULL;
14607 char_u *rhs;
14608 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014609 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014610 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014611 mapblock_T *mp;
14612 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613
14614 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014615 rettv->v_type = VAR_STRING;
14616 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014618 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 if (*keys == NUL)
14620 return;
14621
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014622 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014624 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014625 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014626 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014627 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014628 if (argvars[3].v_type != VAR_UNKNOWN)
14629 get_dict = get_tv_number(&argvars[3]);
14630 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632 else
14633 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014634 if (which == NULL)
14635 return;
14636
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637 mode = get_map_mode(&which, 0);
14638
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014639 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014640 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014642
14643 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014645 /* Return a string. */
14646 if (rhs != NULL)
14647 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648
Bram Moolenaarbd743252010-10-20 21:23:33 +020014649 }
14650 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14651 {
14652 /* Return a dictionary. */
14653 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14654 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14655 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656
Bram Moolenaarbd743252010-10-20 21:23:33 +020014657 dict_add_nr_str(dict, "lhs", 0L, lhs);
14658 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14659 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14660 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14661 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14662 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14663 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014664 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014665 dict_add_nr_str(dict, "mode", 0L, mapmode);
14666
14667 vim_free(lhs);
14668 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 }
14670}
14671
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014672#ifdef FEAT_FLOAT
14673/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014674 * "log()" function
14675 */
14676 static void
14677f_log(argvars, rettv)
14678 typval_T *argvars;
14679 typval_T *rettv;
14680{
14681 float_T f;
14682
14683 rettv->v_type = VAR_FLOAT;
14684 if (get_float_arg(argvars, &f) == OK)
14685 rettv->vval.v_float = log(f);
14686 else
14687 rettv->vval.v_float = 0.0;
14688}
14689
14690/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014691 * "log10()" function
14692 */
14693 static void
14694f_log10(argvars, rettv)
14695 typval_T *argvars;
14696 typval_T *rettv;
14697{
14698 float_T f;
14699
14700 rettv->v_type = VAR_FLOAT;
14701 if (get_float_arg(argvars, &f) == OK)
14702 rettv->vval.v_float = log10(f);
14703 else
14704 rettv->vval.v_float = 0.0;
14705}
14706#endif
14707
Bram Moolenaar1dced572012-04-05 16:54:08 +020014708#ifdef FEAT_LUA
14709/*
14710 * "luaeval()" function
14711 */
14712 static void
14713f_luaeval(argvars, rettv)
14714 typval_T *argvars;
14715 typval_T *rettv;
14716{
14717 char_u *str;
14718 char_u buf[NUMBUFLEN];
14719
14720 str = get_tv_string_buf(&argvars[0], buf);
14721 do_luaeval(str, argvars + 1, rettv);
14722}
14723#endif
14724
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014726 * "map()" function
14727 */
14728 static void
14729f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014730 typval_T *argvars;
14731 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014732{
14733 filter_map(argvars, rettv, TRUE);
14734}
14735
14736/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014737 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738 */
14739 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014740f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014741 typval_T *argvars;
14742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014744 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745}
14746
14747/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014748 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 */
14750 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014751f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014752 typval_T *argvars;
14753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014755 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756}
14757
Bram Moolenaar33570922005-01-25 22:26:29 +000014758static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759
14760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014761find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014762 typval_T *argvars;
14763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764 int type;
14765{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014766 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014767 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014768 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 char_u *pat;
14770 regmatch_T regmatch;
14771 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014772 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773 char_u *save_cpo;
14774 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014775 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014776 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014777 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014778 list_T *l = NULL;
14779 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014780 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014781 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782
14783 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14784 save_cpo = p_cpo;
14785 p_cpo = (char_u *)"";
14786
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014787 rettv->vval.v_number = -1;
14788 if (type == 3)
14789 {
14790 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014791 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014792 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014793 }
14794 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014796 rettv->v_type = VAR_STRING;
14797 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014799
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014800 if (argvars[0].v_type == VAR_LIST)
14801 {
14802 if ((l = argvars[0].vval.v_list) == NULL)
14803 goto theend;
14804 li = l->lv_first;
14805 }
14806 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014807 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014808 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014809 len = (long)STRLEN(str);
14810 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014811
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014812 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14813 if (pat == NULL)
14814 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014815
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014816 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014818 int error = FALSE;
14819
14820 start = get_tv_number_chk(&argvars[2], &error);
14821 if (error)
14822 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014823 if (l != NULL)
14824 {
14825 li = list_find(l, start);
14826 if (li == NULL)
14827 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014828 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014829 }
14830 else
14831 {
14832 if (start < 0)
14833 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014834 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014835 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014836 /* When "count" argument is there ignore matches before "start",
14837 * otherwise skip part of the string. Differs when pattern is "^"
14838 * or "\<". */
14839 if (argvars[3].v_type != VAR_UNKNOWN)
14840 startcol = start;
14841 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014842 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014843 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014844 len -= start;
14845 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014846 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014847
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014848 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014849 nth = get_tv_number_chk(&argvars[3], &error);
14850 if (error)
14851 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 }
14853
14854 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14855 if (regmatch.regprog != NULL)
14856 {
14857 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014858
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014859 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014860 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014861 if (l != NULL)
14862 {
14863 if (li == NULL)
14864 {
14865 match = FALSE;
14866 break;
14867 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014868 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014869 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014870 if (str == NULL)
14871 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014872 }
14873
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014874 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014875
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014876 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014877 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014878 if (l == NULL && !match)
14879 break;
14880
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014881 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014882 if (l != NULL)
14883 {
14884 li = li->li_next;
14885 ++idx;
14886 }
14887 else
14888 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014889#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014890 startcol = (colnr_T)(regmatch.startp[0]
14891 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014892#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014893 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014894#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014895 if (startcol > (colnr_T)len
14896 || str + startcol <= regmatch.startp[0])
14897 {
14898 match = FALSE;
14899 break;
14900 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014901 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014902 }
14903
14904 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014906 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014907 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014908 int i;
14909
14910 /* return list with matched string and submatches */
14911 for (i = 0; i < NSUBEXP; ++i)
14912 {
14913 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014914 {
14915 if (list_append_string(rettv->vval.v_list,
14916 (char_u *)"", 0) == FAIL)
14917 break;
14918 }
14919 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014920 regmatch.startp[i],
14921 (int)(regmatch.endp[i] - regmatch.startp[i]))
14922 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014923 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014924 }
14925 }
14926 else if (type == 2)
14927 {
14928 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014929 if (l != NULL)
14930 copy_tv(&li->li_tv, rettv);
14931 else
14932 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014934 }
14935 else if (l != NULL)
14936 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 else
14938 {
14939 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014940 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941 (varnumber_T)(regmatch.startp[0] - str);
14942 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014943 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014944 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014945 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946 }
14947 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014948 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 }
14950
14951theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014952 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953 p_cpo = save_cpo;
14954}
14955
14956/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014957 * "match()" function
14958 */
14959 static void
14960f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014961 typval_T *argvars;
14962 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014963{
14964 find_some_match(argvars, rettv, 1);
14965}
14966
14967/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014968 * "matchadd()" function
14969 */
14970 static void
14971f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014972 typval_T *argvars UNUSED;
14973 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014974{
14975#ifdef FEAT_SEARCH_EXTRA
14976 char_u buf[NUMBUFLEN];
14977 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14978 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14979 int prio = 10; /* default priority */
14980 int id = -1;
14981 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014982 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014983
14984 rettv->vval.v_number = -1;
14985
14986 if (grp == NULL || pat == NULL)
14987 return;
14988 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014989 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014990 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014991 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014992 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014993 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014994 if (argvars[4].v_type != VAR_UNKNOWN)
14995 {
14996 if (argvars[4].v_type != VAR_DICT)
14997 {
14998 EMSG(_(e_dictreq));
14999 return;
15000 }
15001 if (dict_find(argvars[4].vval.v_dict,
15002 (char_u *)"conceal", -1) != NULL)
15003 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15004 (char_u *)"conceal", FALSE);
15005 }
15006 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015007 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015008 if (error == TRUE)
15009 return;
15010 if (id >= 1 && id <= 3)
15011 {
15012 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15013 return;
15014 }
15015
Bram Moolenaar6561d522015-07-21 15:48:27 +020015016 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15017 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015018#endif
15019}
15020
15021/*
15022 * "matchaddpos()" function
15023 */
15024 static void
15025f_matchaddpos(argvars, rettv)
15026 typval_T *argvars UNUSED;
15027 typval_T *rettv UNUSED;
15028{
15029#ifdef FEAT_SEARCH_EXTRA
15030 char_u buf[NUMBUFLEN];
15031 char_u *group;
15032 int prio = 10;
15033 int id = -1;
15034 int error = FALSE;
15035 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015036 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015037
15038 rettv->vval.v_number = -1;
15039
15040 group = get_tv_string_buf_chk(&argvars[0], buf);
15041 if (group == NULL)
15042 return;
15043
15044 if (argvars[1].v_type != VAR_LIST)
15045 {
15046 EMSG2(_(e_listarg), "matchaddpos()");
15047 return;
15048 }
15049 l = argvars[1].vval.v_list;
15050 if (l == NULL)
15051 return;
15052
15053 if (argvars[2].v_type != VAR_UNKNOWN)
15054 {
15055 prio = get_tv_number_chk(&argvars[2], &error);
15056 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015057 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015058 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015059 if (argvars[4].v_type != VAR_UNKNOWN)
15060 {
15061 if (argvars[4].v_type != VAR_DICT)
15062 {
15063 EMSG(_(e_dictreq));
15064 return;
15065 }
15066 if (dict_find(argvars[4].vval.v_dict,
15067 (char_u *)"conceal", -1) != NULL)
15068 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15069 (char_u *)"conceal", FALSE);
15070 }
15071 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015072 }
15073 if (error == TRUE)
15074 return;
15075
15076 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15077 if (id == 1 || id == 2)
15078 {
15079 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15080 return;
15081 }
15082
Bram Moolenaar6561d522015-07-21 15:48:27 +020015083 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15084 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015085#endif
15086}
15087
15088/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015089 * "matcharg()" function
15090 */
15091 static void
15092f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015093 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015094 typval_T *rettv;
15095{
15096 if (rettv_list_alloc(rettv) == OK)
15097 {
15098#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015099 int id = get_tv_number(&argvars[0]);
15100 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015101
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015102 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015103 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015104 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15105 {
15106 list_append_string(rettv->vval.v_list,
15107 syn_id2name(m->hlg_id), -1);
15108 list_append_string(rettv->vval.v_list, m->pattern, -1);
15109 }
15110 else
15111 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015112 list_append_string(rettv->vval.v_list, NULL, -1);
15113 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015114 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015115 }
15116#endif
15117 }
15118}
15119
15120/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015121 * "matchdelete()" function
15122 */
15123 static void
15124f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015125 typval_T *argvars UNUSED;
15126 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015127{
15128#ifdef FEAT_SEARCH_EXTRA
15129 rettv->vval.v_number = match_delete(curwin,
15130 (int)get_tv_number(&argvars[0]), TRUE);
15131#endif
15132}
15133
15134/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015135 * "matchend()" function
15136 */
15137 static void
15138f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015139 typval_T *argvars;
15140 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015141{
15142 find_some_match(argvars, rettv, 0);
15143}
15144
15145/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015146 * "matchlist()" function
15147 */
15148 static void
15149f_matchlist(argvars, rettv)
15150 typval_T *argvars;
15151 typval_T *rettv;
15152{
15153 find_some_match(argvars, rettv, 3);
15154}
15155
15156/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015157 * "matchstr()" function
15158 */
15159 static void
15160f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015161 typval_T *argvars;
15162 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015163{
15164 find_some_match(argvars, rettv, 2);
15165}
15166
Bram Moolenaar33570922005-01-25 22:26:29 +000015167static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015168
15169 static void
15170max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015171 typval_T *argvars;
15172 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015173 int domax;
15174{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015175 long n = 0;
15176 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015177 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015178
15179 if (argvars[0].v_type == VAR_LIST)
15180 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015181 list_T *l;
15182 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015183
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015184 l = argvars[0].vval.v_list;
15185 if (l != NULL)
15186 {
15187 li = l->lv_first;
15188 if (li != NULL)
15189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015190 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015191 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015192 {
15193 li = li->li_next;
15194 if (li == NULL)
15195 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015196 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015197 if (domax ? i > n : i < n)
15198 n = i;
15199 }
15200 }
15201 }
15202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015203 else if (argvars[0].v_type == VAR_DICT)
15204 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015205 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015206 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015207 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015208 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015209
15210 d = argvars[0].vval.v_dict;
15211 if (d != NULL)
15212 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015213 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015214 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015215 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015216 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015217 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015218 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015219 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015220 if (first)
15221 {
15222 n = i;
15223 first = FALSE;
15224 }
15225 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015226 n = i;
15227 }
15228 }
15229 }
15230 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015231 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015232 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015233 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015234}
15235
15236/*
15237 * "max()" function
15238 */
15239 static void
15240f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015241 typval_T *argvars;
15242 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015243{
15244 max_min(argvars, rettv, TRUE);
15245}
15246
15247/*
15248 * "min()" function
15249 */
15250 static void
15251f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015252 typval_T *argvars;
15253 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015254{
15255 max_min(argvars, rettv, FALSE);
15256}
15257
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015258static int mkdir_recurse __ARGS((char_u *dir, int prot));
15259
15260/*
15261 * Create the directory in which "dir" is located, and higher levels when
15262 * needed.
15263 */
15264 static int
15265mkdir_recurse(dir, prot)
15266 char_u *dir;
15267 int prot;
15268{
15269 char_u *p;
15270 char_u *updir;
15271 int r = FAIL;
15272
15273 /* Get end of directory name in "dir".
15274 * We're done when it's "/" or "c:/". */
15275 p = gettail_sep(dir);
15276 if (p <= get_past_head(dir))
15277 return OK;
15278
15279 /* If the directory exists we're done. Otherwise: create it.*/
15280 updir = vim_strnsave(dir, (int)(p - dir));
15281 if (updir == NULL)
15282 return FAIL;
15283 if (mch_isdir(updir))
15284 r = OK;
15285 else if (mkdir_recurse(updir, prot) == OK)
15286 r = vim_mkdir_emsg(updir, prot);
15287 vim_free(updir);
15288 return r;
15289}
15290
15291#ifdef vim_mkdir
15292/*
15293 * "mkdir()" function
15294 */
15295 static void
15296f_mkdir(argvars, rettv)
15297 typval_T *argvars;
15298 typval_T *rettv;
15299{
15300 char_u *dir;
15301 char_u buf[NUMBUFLEN];
15302 int prot = 0755;
15303
15304 rettv->vval.v_number = FAIL;
15305 if (check_restricted() || check_secure())
15306 return;
15307
15308 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015309 if (*dir == NUL)
15310 rettv->vval.v_number = FAIL;
15311 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015312 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015313 if (*gettail(dir) == NUL)
15314 /* remove trailing slashes */
15315 *gettail_sep(dir) = NUL;
15316
15317 if (argvars[1].v_type != VAR_UNKNOWN)
15318 {
15319 if (argvars[2].v_type != VAR_UNKNOWN)
15320 prot = get_tv_number_chk(&argvars[2], NULL);
15321 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15322 mkdir_recurse(dir, prot);
15323 }
15324 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015325 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015326}
15327#endif
15328
Bram Moolenaar0d660222005-01-07 21:51:51 +000015329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015330 * "mode()" function
15331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015333f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015334 typval_T *argvars;
15335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015337 char_u buf[3];
15338
15339 buf[1] = NUL;
15340 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342 if (VIsual_active)
15343 {
15344 if (VIsual_select)
15345 buf[0] = VIsual_mode + 's' - 'v';
15346 else
15347 buf[0] = VIsual_mode;
15348 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015349 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015350 || State == CONFIRM)
15351 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015353 if (State == ASKMORE)
15354 buf[1] = 'm';
15355 else if (State == CONFIRM)
15356 buf[1] = '?';
15357 }
15358 else if (State == EXTERNCMD)
15359 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 else if (State & INSERT)
15361 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015362#ifdef FEAT_VREPLACE
15363 if (State & VREPLACE_FLAG)
15364 {
15365 buf[0] = 'R';
15366 buf[1] = 'v';
15367 }
15368 else
15369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370 if (State & REPLACE_FLAG)
15371 buf[0] = 'R';
15372 else
15373 buf[0] = 'i';
15374 }
15375 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015376 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015378 if (exmode_active)
15379 buf[1] = 'v';
15380 }
15381 else if (exmode_active)
15382 {
15383 buf[0] = 'c';
15384 buf[1] = 'e';
15385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015389 if (finish_op)
15390 buf[1] = 'o';
15391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015393 /* Clear out the minor mode when the argument is not a non-zero number or
15394 * non-empty string. */
15395 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015396 buf[1] = NUL;
15397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015398 rettv->vval.v_string = vim_strsave(buf);
15399 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400}
15401
Bram Moolenaar429fa852013-04-15 12:27:36 +020015402#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015403/*
15404 * "mzeval()" function
15405 */
15406 static void
15407f_mzeval(argvars, rettv)
15408 typval_T *argvars;
15409 typval_T *rettv;
15410{
15411 char_u *str;
15412 char_u buf[NUMBUFLEN];
15413
15414 str = get_tv_string_buf(&argvars[0], buf);
15415 do_mzeval(str, rettv);
15416}
Bram Moolenaar75676462013-01-30 14:55:42 +010015417
15418 void
15419mzscheme_call_vim(name, args, rettv)
15420 char_u *name;
15421 typval_T *args;
15422 typval_T *rettv;
15423{
15424 typval_T argvars[3];
15425
15426 argvars[0].v_type = VAR_STRING;
15427 argvars[0].vval.v_string = name;
15428 copy_tv(args, &argvars[1]);
15429 argvars[2].v_type = VAR_UNKNOWN;
15430 f_call(argvars, rettv);
15431 clear_tv(&argvars[1]);
15432}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015433#endif
15434
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015436 * "nextnonblank()" function
15437 */
15438 static void
15439f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015440 typval_T *argvars;
15441 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015442{
15443 linenr_T lnum;
15444
15445 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015447 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015448 {
15449 lnum = 0;
15450 break;
15451 }
15452 if (*skipwhite(ml_get(lnum)) != NUL)
15453 break;
15454 }
15455 rettv->vval.v_number = lnum;
15456}
15457
15458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459 * "nr2char()" function
15460 */
15461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015462f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015463 typval_T *argvars;
15464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465{
15466 char_u buf[NUMBUFLEN];
15467
15468#ifdef FEAT_MBYTE
15469 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015470 {
15471 int utf8 = 0;
15472
15473 if (argvars[1].v_type != VAR_UNKNOWN)
15474 utf8 = get_tv_number_chk(&argvars[1], NULL);
15475 if (utf8)
15476 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15477 else
15478 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480 else
15481#endif
15482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015483 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 buf[1] = NUL;
15485 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015486 rettv->v_type = VAR_STRING;
15487 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015488}
15489
15490/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015491 * "or(expr, expr)" function
15492 */
15493 static void
15494f_or(argvars, rettv)
15495 typval_T *argvars;
15496 typval_T *rettv;
15497{
15498 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15499 | get_tv_number_chk(&argvars[1], NULL);
15500}
15501
15502/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015503 * "pathshorten()" function
15504 */
15505 static void
15506f_pathshorten(argvars, rettv)
15507 typval_T *argvars;
15508 typval_T *rettv;
15509{
15510 char_u *p;
15511
15512 rettv->v_type = VAR_STRING;
15513 p = get_tv_string_chk(&argvars[0]);
15514 if (p == NULL)
15515 rettv->vval.v_string = NULL;
15516 else
15517 {
15518 p = vim_strsave(p);
15519 rettv->vval.v_string = p;
15520 if (p != NULL)
15521 shorten_dir(p);
15522 }
15523}
15524
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015525#ifdef FEAT_PERL
15526/*
15527 * "perleval()" function
15528 */
15529 static void
15530f_perleval(argvars, rettv)
15531 typval_T *argvars;
15532 typval_T *rettv;
15533{
15534 char_u *str;
15535 char_u buf[NUMBUFLEN];
15536
15537 str = get_tv_string_buf(&argvars[0], buf);
15538 do_perleval(str, rettv);
15539}
15540#endif
15541
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015542#ifdef FEAT_FLOAT
15543/*
15544 * "pow()" function
15545 */
15546 static void
15547f_pow(argvars, rettv)
15548 typval_T *argvars;
15549 typval_T *rettv;
15550{
15551 float_T fx, fy;
15552
15553 rettv->v_type = VAR_FLOAT;
15554 if (get_float_arg(argvars, &fx) == OK
15555 && get_float_arg(&argvars[1], &fy) == OK)
15556 rettv->vval.v_float = pow(fx, fy);
15557 else
15558 rettv->vval.v_float = 0.0;
15559}
15560#endif
15561
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015562/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015563 * "prevnonblank()" function
15564 */
15565 static void
15566f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015567 typval_T *argvars;
15568 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015569{
15570 linenr_T lnum;
15571
15572 lnum = get_tv_lnum(argvars);
15573 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15574 lnum = 0;
15575 else
15576 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15577 --lnum;
15578 rettv->vval.v_number = lnum;
15579}
15580
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015581#ifdef HAVE_STDARG_H
15582/* This dummy va_list is here because:
15583 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15584 * - locally in the function results in a "used before set" warning
15585 * - using va_start() to initialize it gives "function with fixed args" error */
15586static va_list ap;
15587#endif
15588
Bram Moolenaar8c711452005-01-14 21:53:12 +000015589/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015590 * "printf()" function
15591 */
15592 static void
15593f_printf(argvars, rettv)
15594 typval_T *argvars;
15595 typval_T *rettv;
15596{
15597 rettv->v_type = VAR_STRING;
15598 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015599#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015600 {
15601 char_u buf[NUMBUFLEN];
15602 int len;
15603 char_u *s;
15604 int saved_did_emsg = did_emsg;
15605 char *fmt;
15606
15607 /* Get the required length, allocate the buffer and do it for real. */
15608 did_emsg = FALSE;
15609 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015610 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015611 if (!did_emsg)
15612 {
15613 s = alloc(len + 1);
15614 if (s != NULL)
15615 {
15616 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015617 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015618 }
15619 }
15620 did_emsg |= saved_did_emsg;
15621 }
15622#endif
15623}
15624
15625/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015626 * "pumvisible()" function
15627 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015628 static void
15629f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015630 typval_T *argvars UNUSED;
15631 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015632{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015633#ifdef FEAT_INS_EXPAND
15634 if (pum_visible())
15635 rettv->vval.v_number = 1;
15636#endif
15637}
15638
Bram Moolenaardb913952012-06-29 12:54:53 +020015639#ifdef FEAT_PYTHON3
15640/*
15641 * "py3eval()" function
15642 */
15643 static void
15644f_py3eval(argvars, rettv)
15645 typval_T *argvars;
15646 typval_T *rettv;
15647{
15648 char_u *str;
15649 char_u buf[NUMBUFLEN];
15650
15651 str = get_tv_string_buf(&argvars[0], buf);
15652 do_py3eval(str, rettv);
15653}
15654#endif
15655
15656#ifdef FEAT_PYTHON
15657/*
15658 * "pyeval()" function
15659 */
15660 static void
15661f_pyeval(argvars, rettv)
15662 typval_T *argvars;
15663 typval_T *rettv;
15664{
15665 char_u *str;
15666 char_u buf[NUMBUFLEN];
15667
15668 str = get_tv_string_buf(&argvars[0], buf);
15669 do_pyeval(str, rettv);
15670}
15671#endif
15672
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015673/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015674 * "range()" function
15675 */
15676 static void
15677f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015678 typval_T *argvars;
15679 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015680{
15681 long start;
15682 long end;
15683 long stride = 1;
15684 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015685 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015687 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015688 if (argvars[1].v_type == VAR_UNKNOWN)
15689 {
15690 end = start - 1;
15691 start = 0;
15692 }
15693 else
15694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015695 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015696 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015697 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015698 }
15699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015700 if (error)
15701 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015702 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015703 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015704 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015705 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015706 else
15707 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015708 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015709 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015710 if (list_append_number(rettv->vval.v_list,
15711 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015712 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015713 }
15714}
15715
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015716/*
15717 * "readfile()" function
15718 */
15719 static void
15720f_readfile(argvars, rettv)
15721 typval_T *argvars;
15722 typval_T *rettv;
15723{
15724 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015725 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015726 char_u *fname;
15727 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015728 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15729 int io_size = sizeof(buf);
15730 int readlen; /* size of last fread() */
15731 char_u *prev = NULL; /* previously read bytes, if any */
15732 long prevlen = 0; /* length of data in prev */
15733 long prevsize = 0; /* size of prev buffer */
15734 long maxline = MAXLNUM;
15735 long cnt = 0;
15736 char_u *p; /* position in buf */
15737 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015738
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015739 if (argvars[1].v_type != VAR_UNKNOWN)
15740 {
15741 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15742 binary = TRUE;
15743 if (argvars[2].v_type != VAR_UNKNOWN)
15744 maxline = get_tv_number(&argvars[2]);
15745 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015746
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015747 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015748 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015749
15750 /* Always open the file in binary mode, library functions have a mind of
15751 * their own about CR-LF conversion. */
15752 fname = get_tv_string(&argvars[0]);
15753 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15754 {
15755 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15756 return;
15757 }
15758
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015759 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015760 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015761 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015762
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015763 /* This for loop processes what was read, but is also entered at end
15764 * of file so that either:
15765 * - an incomplete line gets written
15766 * - a "binary" file gets an empty line at the end if it ends in a
15767 * newline. */
15768 for (p = buf, start = buf;
15769 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15770 ++p)
15771 {
15772 if (*p == '\n' || readlen <= 0)
15773 {
15774 listitem_T *li;
15775 char_u *s = NULL;
15776 long_u len = p - start;
15777
15778 /* Finished a line. Remove CRs before NL. */
15779 if (readlen > 0 && !binary)
15780 {
15781 while (len > 0 && start[len - 1] == '\r')
15782 --len;
15783 /* removal may cross back to the "prev" string */
15784 if (len == 0)
15785 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15786 --prevlen;
15787 }
15788 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015789 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015790 else
15791 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015792 /* Change "prev" buffer to be the right size. This way
15793 * the bytes are only copied once, and very long lines are
15794 * allocated only once. */
15795 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015796 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015797 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015798 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015799 prev = NULL; /* the list will own the string */
15800 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015801 }
15802 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015803 if (s == NULL)
15804 {
15805 do_outofmem_msg((long_u) prevlen + len + 1);
15806 failed = TRUE;
15807 break;
15808 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015809
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015810 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015811 {
15812 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015813 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015814 break;
15815 }
15816 li->li_tv.v_type = VAR_STRING;
15817 li->li_tv.v_lock = 0;
15818 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015819 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015820
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015821 start = p + 1; /* step over newline */
15822 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015823 break;
15824 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015825 else if (*p == NUL)
15826 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015827#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015828 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15829 * when finding the BF and check the previous two bytes. */
15830 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015831 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015832 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15833 * + 1, these may be in the "prev" string. */
15834 char_u back1 = p >= buf + 1 ? p[-1]
15835 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15836 char_u back2 = p >= buf + 2 ? p[-2]
15837 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15838 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015839
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015840 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015841 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015842 char_u *dest = p - 2;
15843
15844 /* Usually a BOM is at the beginning of a file, and so at
15845 * the beginning of a line; then we can just step over it.
15846 */
15847 if (start == dest)
15848 start = p + 1;
15849 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015850 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015851 /* have to shuffle buf to close gap */
15852 int adjust_prevlen = 0;
15853
15854 if (dest < buf)
15855 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015856 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015857 dest = buf;
15858 }
15859 if (readlen > p - buf + 1)
15860 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15861 readlen -= 3 - adjust_prevlen;
15862 prevlen -= adjust_prevlen;
15863 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015864 }
15865 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015866 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015867#endif
15868 } /* for */
15869
15870 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15871 break;
15872 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015873 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015874 /* There's part of a line in buf, store it in "prev". */
15875 if (p - start + prevlen >= prevsize)
15876 {
15877 /* need bigger "prev" buffer */
15878 char_u *newprev;
15879
15880 /* A common use case is ordinary text files and "prev" gets a
15881 * fragment of a line, so the first allocation is made
15882 * small, to avoid repeatedly 'allocing' large and
15883 * 'reallocing' small. */
15884 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015885 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015886 else
15887 {
15888 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015889 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015890 prevsize = grow50pc > growmin ? grow50pc : growmin;
15891 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015892 newprev = prev == NULL ? alloc(prevsize)
15893 : vim_realloc(prev, prevsize);
15894 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015895 {
15896 do_outofmem_msg((long_u)prevsize);
15897 failed = TRUE;
15898 break;
15899 }
15900 prev = newprev;
15901 }
15902 /* Add the line part to end of "prev". */
15903 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015904 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015905 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015906 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015907
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015908 /*
15909 * For a negative line count use only the lines at the end of the file,
15910 * free the rest.
15911 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015912 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015913 while (cnt > -maxline)
15914 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015915 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015916 --cnt;
15917 }
15918
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015919 if (failed)
15920 {
15921 list_free(rettv->vval.v_list, TRUE);
15922 /* readfile doc says an empty list is returned on error */
15923 rettv->vval.v_list = list_alloc();
15924 }
15925
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015926 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015927 fclose(fd);
15928}
15929
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015930#if defined(FEAT_RELTIME)
15931static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15932
15933/*
15934 * Convert a List to proftime_T.
15935 * Return FAIL when there is something wrong.
15936 */
15937 static int
15938list2proftime(arg, tm)
15939 typval_T *arg;
15940 proftime_T *tm;
15941{
15942 long n1, n2;
15943 int error = FALSE;
15944
15945 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15946 || arg->vval.v_list->lv_len != 2)
15947 return FAIL;
15948 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15949 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15950# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015951 tm->HighPart = n1;
15952 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015953# else
15954 tm->tv_sec = n1;
15955 tm->tv_usec = n2;
15956# endif
15957 return error ? FAIL : OK;
15958}
15959#endif /* FEAT_RELTIME */
15960
15961/*
15962 * "reltime()" function
15963 */
15964 static void
15965f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015966 typval_T *argvars UNUSED;
15967 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015968{
15969#ifdef FEAT_RELTIME
15970 proftime_T res;
15971 proftime_T start;
15972
15973 if (argvars[0].v_type == VAR_UNKNOWN)
15974 {
15975 /* No arguments: get current time. */
15976 profile_start(&res);
15977 }
15978 else if (argvars[1].v_type == VAR_UNKNOWN)
15979 {
15980 if (list2proftime(&argvars[0], &res) == FAIL)
15981 return;
15982 profile_end(&res);
15983 }
15984 else
15985 {
15986 /* Two arguments: compute the difference. */
15987 if (list2proftime(&argvars[0], &start) == FAIL
15988 || list2proftime(&argvars[1], &res) == FAIL)
15989 return;
15990 profile_sub(&res, &start);
15991 }
15992
15993 if (rettv_list_alloc(rettv) == OK)
15994 {
15995 long n1, n2;
15996
15997# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015998 n1 = res.HighPart;
15999 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016000# else
16001 n1 = res.tv_sec;
16002 n2 = res.tv_usec;
16003# endif
16004 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16005 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16006 }
16007#endif
16008}
16009
16010/*
16011 * "reltimestr()" function
16012 */
16013 static void
16014f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016015 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016016 typval_T *rettv;
16017{
16018#ifdef FEAT_RELTIME
16019 proftime_T tm;
16020#endif
16021
16022 rettv->v_type = VAR_STRING;
16023 rettv->vval.v_string = NULL;
16024#ifdef FEAT_RELTIME
16025 if (list2proftime(&argvars[0], &tm) == OK)
16026 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16027#endif
16028}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016029
Bram Moolenaar0d660222005-01-07 21:51:51 +000016030#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
16031static void make_connection __ARGS((void));
16032static int check_connection __ARGS((void));
16033
16034 static void
16035make_connection()
16036{
16037 if (X_DISPLAY == NULL
16038# ifdef FEAT_GUI
16039 && !gui.in_use
16040# endif
16041 )
16042 {
16043 x_force_connect = TRUE;
16044 setup_term_clip();
16045 x_force_connect = FALSE;
16046 }
16047}
16048
16049 static int
16050check_connection()
16051{
16052 make_connection();
16053 if (X_DISPLAY == NULL)
16054 {
16055 EMSG(_("E240: No connection to Vim server"));
16056 return FAIL;
16057 }
16058 return OK;
16059}
16060#endif
16061
16062#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016063static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016064
16065 static void
16066remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000016067 typval_T *argvars;
16068 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016069 int expr;
16070{
16071 char_u *server_name;
16072 char_u *keys;
16073 char_u *r = NULL;
16074 char_u buf[NUMBUFLEN];
16075# ifdef WIN32
16076 HWND w;
16077# else
16078 Window w;
16079# endif
16080
16081 if (check_restricted() || check_secure())
16082 return;
16083
16084# ifdef FEAT_X11
16085 if (check_connection() == FAIL)
16086 return;
16087# endif
16088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016089 server_name = get_tv_string_chk(&argvars[0]);
16090 if (server_name == NULL)
16091 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016092 keys = get_tv_string_buf(&argvars[1], buf);
16093# ifdef WIN32
16094 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16095# else
16096 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16097 < 0)
16098# endif
16099 {
16100 if (r != NULL)
16101 EMSG(r); /* sending worked but evaluation failed */
16102 else
16103 EMSG2(_("E241: Unable to send to %s"), server_name);
16104 return;
16105 }
16106
16107 rettv->vval.v_string = r;
16108
16109 if (argvars[2].v_type != VAR_UNKNOWN)
16110 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016111 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016112 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016113 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016114
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016115 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016116 v.di_tv.v_type = VAR_STRING;
16117 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016118 idvar = get_tv_string_chk(&argvars[2]);
16119 if (idvar != NULL)
16120 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016121 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016122 }
16123}
16124#endif
16125
16126/*
16127 * "remote_expr()" function
16128 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016129 static void
16130f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016131 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016132 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016133{
16134 rettv->v_type = VAR_STRING;
16135 rettv->vval.v_string = NULL;
16136#ifdef FEAT_CLIENTSERVER
16137 remote_common(argvars, rettv, TRUE);
16138#endif
16139}
16140
16141/*
16142 * "remote_foreground()" function
16143 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016144 static void
16145f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016146 typval_T *argvars UNUSED;
16147 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016148{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016149#ifdef FEAT_CLIENTSERVER
16150# ifdef WIN32
16151 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016152 {
16153 char_u *server_name = get_tv_string_chk(&argvars[0]);
16154
16155 if (server_name != NULL)
16156 serverForeground(server_name);
16157 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016158# else
16159 /* Send a foreground() expression to the server. */
16160 argvars[1].v_type = VAR_STRING;
16161 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16162 argvars[2].v_type = VAR_UNKNOWN;
16163 remote_common(argvars, rettv, TRUE);
16164 vim_free(argvars[1].vval.v_string);
16165# endif
16166#endif
16167}
16168
Bram Moolenaar0d660222005-01-07 21:51:51 +000016169 static void
16170f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016171 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016172 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016173{
16174#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016175 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016176 char_u *s = NULL;
16177# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016178 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016179# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016180 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016181
16182 if (check_restricted() || check_secure())
16183 {
16184 rettv->vval.v_number = -1;
16185 return;
16186 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016187 serverid = get_tv_string_chk(&argvars[0]);
16188 if (serverid == NULL)
16189 {
16190 rettv->vval.v_number = -1;
16191 return; /* type error; errmsg already given */
16192 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016193# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016194 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016195 if (n == 0)
16196 rettv->vval.v_number = -1;
16197 else
16198 {
16199 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16200 rettv->vval.v_number = (s != NULL);
16201 }
16202# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016203 if (check_connection() == FAIL)
16204 return;
16205
16206 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016207 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016208# endif
16209
16210 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016212 char_u *retvar;
16213
Bram Moolenaar33570922005-01-25 22:26:29 +000016214 v.di_tv.v_type = VAR_STRING;
16215 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016216 retvar = get_tv_string_chk(&argvars[1]);
16217 if (retvar != NULL)
16218 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016219 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016220 }
16221#else
16222 rettv->vval.v_number = -1;
16223#endif
16224}
16225
Bram Moolenaar0d660222005-01-07 21:51:51 +000016226 static void
16227f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016228 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016229 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016230{
16231 char_u *r = NULL;
16232
16233#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016234 char_u *serverid = get_tv_string_chk(&argvars[0]);
16235
16236 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016237 {
16238# ifdef WIN32
16239 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016240 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016241
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016242 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016243 if (n != 0)
16244 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16245 if (r == NULL)
16246# else
16247 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016248 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016249# endif
16250 EMSG(_("E277: Unable to read a server reply"));
16251 }
16252#endif
16253 rettv->v_type = VAR_STRING;
16254 rettv->vval.v_string = r;
16255}
16256
16257/*
16258 * "remote_send()" function
16259 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016260 static void
16261f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016262 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016263 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016264{
16265 rettv->v_type = VAR_STRING;
16266 rettv->vval.v_string = NULL;
16267#ifdef FEAT_CLIENTSERVER
16268 remote_common(argvars, rettv, FALSE);
16269#endif
16270}
16271
16272/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016273 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016274 */
16275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016276f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016277 typval_T *argvars;
16278 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016279{
Bram Moolenaar33570922005-01-25 22:26:29 +000016280 list_T *l;
16281 listitem_T *item, *item2;
16282 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016283 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016284 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016285 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016286 dict_T *d;
16287 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016288 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016289
Bram Moolenaar8c711452005-01-14 21:53:12 +000016290 if (argvars[0].v_type == VAR_DICT)
16291 {
16292 if (argvars[2].v_type != VAR_UNKNOWN)
16293 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016294 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016295 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016297 key = get_tv_string_chk(&argvars[1]);
16298 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016300 di = dict_find(d, key, -1);
16301 if (di == NULL)
16302 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016303 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16304 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016305 {
16306 *rettv = di->di_tv;
16307 init_tv(&di->di_tv);
16308 dictitem_remove(d, di);
16309 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016310 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016311 }
16312 }
16313 else if (argvars[0].v_type != VAR_LIST)
16314 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016315 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016316 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016318 int error = FALSE;
16319
16320 idx = get_tv_number_chk(&argvars[1], &error);
16321 if (error)
16322 ; /* type error: do nothing, errmsg already given */
16323 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016324 EMSGN(_(e_listidx), idx);
16325 else
16326 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016327 if (argvars[2].v_type == VAR_UNKNOWN)
16328 {
16329 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016330 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016331 *rettv = item->li_tv;
16332 vim_free(item);
16333 }
16334 else
16335 {
16336 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016337 end = get_tv_number_chk(&argvars[2], &error);
16338 if (error)
16339 ; /* type error: do nothing */
16340 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016341 EMSGN(_(e_listidx), end);
16342 else
16343 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016344 int cnt = 0;
16345
16346 for (li = item; li != NULL; li = li->li_next)
16347 {
16348 ++cnt;
16349 if (li == item2)
16350 break;
16351 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016352 if (li == NULL) /* didn't find "item2" after "item" */
16353 EMSG(_(e_invrange));
16354 else
16355 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016356 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016357 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016358 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016359 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016360 l->lv_first = item;
16361 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016362 item->li_prev = NULL;
16363 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016364 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016365 }
16366 }
16367 }
16368 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016369 }
16370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371}
16372
16373/*
16374 * "rename({from}, {to})" function
16375 */
16376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016377f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016378 typval_T *argvars;
16379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380{
16381 char_u buf[NUMBUFLEN];
16382
16383 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016384 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016386 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16387 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388}
16389
16390/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016391 * "repeat()" function
16392 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016393 static void
16394f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016395 typval_T *argvars;
16396 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016397{
16398 char_u *p;
16399 int n;
16400 int slen;
16401 int len;
16402 char_u *r;
16403 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016404
16405 n = get_tv_number(&argvars[1]);
16406 if (argvars[0].v_type == VAR_LIST)
16407 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016408 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016409 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016410 if (list_extend(rettv->vval.v_list,
16411 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016412 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016413 }
16414 else
16415 {
16416 p = get_tv_string(&argvars[0]);
16417 rettv->v_type = VAR_STRING;
16418 rettv->vval.v_string = NULL;
16419
16420 slen = (int)STRLEN(p);
16421 len = slen * n;
16422 if (len <= 0)
16423 return;
16424
16425 r = alloc(len + 1);
16426 if (r != NULL)
16427 {
16428 for (i = 0; i < n; i++)
16429 mch_memmove(r + i * slen, p, (size_t)slen);
16430 r[len] = NUL;
16431 }
16432
16433 rettv->vval.v_string = r;
16434 }
16435}
16436
16437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 * "resolve()" function
16439 */
16440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016441f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016442 typval_T *argvars;
16443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444{
16445 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016446#ifdef HAVE_READLINK
16447 char_u *buf = NULL;
16448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016450 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451#ifdef FEAT_SHORTCUT
16452 {
16453 char_u *v = NULL;
16454
16455 v = mch_resolve_shortcut(p);
16456 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016457 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016459 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 }
16461#else
16462# ifdef HAVE_READLINK
16463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 char_u *cpy;
16465 int len;
16466 char_u *remain = NULL;
16467 char_u *q;
16468 int is_relative_to_current = FALSE;
16469 int has_trailing_pathsep = FALSE;
16470 int limit = 100;
16471
16472 p = vim_strsave(p);
16473
16474 if (p[0] == '.' && (vim_ispathsep(p[1])
16475 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16476 is_relative_to_current = TRUE;
16477
16478 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016479 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016482 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016484
16485 q = getnextcomp(p);
16486 if (*q != NUL)
16487 {
16488 /* Separate the first path component in "p", and keep the
16489 * remainder (beginning with the path separator). */
16490 remain = vim_strsave(q - 1);
16491 q[-1] = NUL;
16492 }
16493
Bram Moolenaard9462e32011-04-11 21:35:11 +020016494 buf = alloc(MAXPATHL + 1);
16495 if (buf == NULL)
16496 goto fail;
16497
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498 for (;;)
16499 {
16500 for (;;)
16501 {
16502 len = readlink((char *)p, (char *)buf, MAXPATHL);
16503 if (len <= 0)
16504 break;
16505 buf[len] = NUL;
16506
16507 if (limit-- == 0)
16508 {
16509 vim_free(p);
16510 vim_free(remain);
16511 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016512 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513 goto fail;
16514 }
16515
16516 /* Ensure that the result will have a trailing path separator
16517 * if the argument has one. */
16518 if (remain == NULL && has_trailing_pathsep)
16519 add_pathsep(buf);
16520
16521 /* Separate the first path component in the link value and
16522 * concatenate the remainders. */
16523 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16524 if (*q != NUL)
16525 {
16526 if (remain == NULL)
16527 remain = vim_strsave(q - 1);
16528 else
16529 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016530 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 if (cpy != NULL)
16532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533 vim_free(remain);
16534 remain = cpy;
16535 }
16536 }
16537 q[-1] = NUL;
16538 }
16539
16540 q = gettail(p);
16541 if (q > p && *q == NUL)
16542 {
16543 /* Ignore trailing path separator. */
16544 q[-1] = NUL;
16545 q = gettail(p);
16546 }
16547 if (q > p && !mch_isFullName(buf))
16548 {
16549 /* symlink is relative to directory of argument */
16550 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16551 if (cpy != NULL)
16552 {
16553 STRCPY(cpy, p);
16554 STRCPY(gettail(cpy), buf);
16555 vim_free(p);
16556 p = cpy;
16557 }
16558 }
16559 else
16560 {
16561 vim_free(p);
16562 p = vim_strsave(buf);
16563 }
16564 }
16565
16566 if (remain == NULL)
16567 break;
16568
16569 /* Append the first path component of "remain" to "p". */
16570 q = getnextcomp(remain + 1);
16571 len = q - remain - (*q != NUL);
16572 cpy = vim_strnsave(p, STRLEN(p) + len);
16573 if (cpy != NULL)
16574 {
16575 STRNCAT(cpy, remain, len);
16576 vim_free(p);
16577 p = cpy;
16578 }
16579 /* Shorten "remain". */
16580 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016581 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582 else
16583 {
16584 vim_free(remain);
16585 remain = NULL;
16586 }
16587 }
16588
16589 /* If the result is a relative path name, make it explicitly relative to
16590 * the current directory if and only if the argument had this form. */
16591 if (!vim_ispathsep(*p))
16592 {
16593 if (is_relative_to_current
16594 && *p != NUL
16595 && !(p[0] == '.'
16596 && (p[1] == NUL
16597 || vim_ispathsep(p[1])
16598 || (p[1] == '.'
16599 && (p[2] == NUL
16600 || vim_ispathsep(p[2]))))))
16601 {
16602 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016603 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604 if (cpy != NULL)
16605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606 vim_free(p);
16607 p = cpy;
16608 }
16609 }
16610 else if (!is_relative_to_current)
16611 {
16612 /* Strip leading "./". */
16613 q = p;
16614 while (q[0] == '.' && vim_ispathsep(q[1]))
16615 q += 2;
16616 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016617 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618 }
16619 }
16620
16621 /* Ensure that the result will have no trailing path separator
16622 * if the argument had none. But keep "/" or "//". */
16623 if (!has_trailing_pathsep)
16624 {
16625 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016626 if (after_pathsep(p, q))
16627 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 }
16629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016630 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631 }
16632# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016633 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634# endif
16635#endif
16636
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016637 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638
16639#ifdef HAVE_READLINK
16640fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016641 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016643 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644}
16645
16646/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016647 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 */
16649 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016650f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016651 typval_T *argvars;
16652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653{
Bram Moolenaar33570922005-01-25 22:26:29 +000016654 list_T *l;
16655 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656
Bram Moolenaar0d660222005-01-07 21:51:51 +000016657 if (argvars[0].v_type != VAR_LIST)
16658 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016659 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016660 && !tv_check_lock(l->lv_lock,
16661 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016662 {
16663 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016664 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016665 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016666 while (li != NULL)
16667 {
16668 ni = li->li_prev;
16669 list_append(l, li);
16670 li = ni;
16671 }
16672 rettv->vval.v_list = l;
16673 rettv->v_type = VAR_LIST;
16674 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016675 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677}
16678
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016679#define SP_NOMOVE 0x01 /* don't move cursor */
16680#define SP_REPEAT 0x02 /* repeat to find outer pair */
16681#define SP_RETCOUNT 0x04 /* return matchcount */
16682#define SP_SETPCMARK 0x08 /* set previous context mark */
16683#define SP_START 0x10 /* accept match at start position */
16684#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16685#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016686#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016687
Bram Moolenaar33570922005-01-25 22:26:29 +000016688static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016689
16690/*
16691 * Get flags for a search function.
16692 * Possibly sets "p_ws".
16693 * Returns BACKWARD, FORWARD or zero (for an error).
16694 */
16695 static int
16696get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016697 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016698 int *flagsp;
16699{
16700 int dir = FORWARD;
16701 char_u *flags;
16702 char_u nbuf[NUMBUFLEN];
16703 int mask;
16704
16705 if (varp->v_type != VAR_UNKNOWN)
16706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016707 flags = get_tv_string_buf_chk(varp, nbuf);
16708 if (flags == NULL)
16709 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016710 while (*flags != NUL)
16711 {
16712 switch (*flags)
16713 {
16714 case 'b': dir = BACKWARD; break;
16715 case 'w': p_ws = TRUE; break;
16716 case 'W': p_ws = FALSE; break;
16717 default: mask = 0;
16718 if (flagsp != NULL)
16719 switch (*flags)
16720 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016721 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016722 case 'e': mask = SP_END; break;
16723 case 'm': mask = SP_RETCOUNT; break;
16724 case 'n': mask = SP_NOMOVE; break;
16725 case 'p': mask = SP_SUBPAT; break;
16726 case 'r': mask = SP_REPEAT; break;
16727 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016728 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016729 }
16730 if (mask == 0)
16731 {
16732 EMSG2(_(e_invarg2), flags);
16733 dir = 0;
16734 }
16735 else
16736 *flagsp |= mask;
16737 }
16738 if (dir == 0)
16739 break;
16740 ++flags;
16741 }
16742 }
16743 return dir;
16744}
16745
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016747 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016749 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016750search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016751 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016752 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016753 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016755 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 char_u *pat;
16757 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016758 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759 int save_p_ws = p_ws;
16760 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016761 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016762 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016763 proftime_T tm;
16764#ifdef FEAT_RELTIME
16765 long time_limit = 0;
16766#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016767 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016768 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016770 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016771 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016772 if (dir == 0)
16773 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016774 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016775 if (flags & SP_START)
16776 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016777 if (flags & SP_END)
16778 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016779 if (flags & SP_COLUMN)
16780 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016781
Bram Moolenaar76929292008-01-06 19:07:36 +000016782 /* Optional arguments: line number to stop searching and timeout. */
16783 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016784 {
16785 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16786 if (lnum_stop < 0)
16787 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016788#ifdef FEAT_RELTIME
16789 if (argvars[3].v_type != VAR_UNKNOWN)
16790 {
16791 time_limit = get_tv_number_chk(&argvars[3], NULL);
16792 if (time_limit < 0)
16793 goto theend;
16794 }
16795#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016796 }
16797
Bram Moolenaar76929292008-01-06 19:07:36 +000016798#ifdef FEAT_RELTIME
16799 /* Set the time limit, if there is one. */
16800 profile_setlimit(time_limit, &tm);
16801#endif
16802
Bram Moolenaar231334e2005-07-25 20:46:57 +000016803 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016804 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016805 * Check to make sure only those flags are set.
16806 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16807 * flags cannot be set. Check for that condition also.
16808 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016809 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016810 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016811 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016812 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016813 goto theend;
16814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016816 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016817 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016818 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016819 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016821 if (flags & SP_SUBPAT)
16822 retval = subpatnum;
16823 else
16824 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016825 if (flags & SP_SETPCMARK)
16826 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016828 if (match_pos != NULL)
16829 {
16830 /* Store the match cursor position */
16831 match_pos->lnum = pos.lnum;
16832 match_pos->col = pos.col + 1;
16833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 /* "/$" will put the cursor after the end of the line, may need to
16835 * correct that here */
16836 check_cursor();
16837 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016838
16839 /* If 'n' flag is used: restore cursor position. */
16840 if (flags & SP_NOMOVE)
16841 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016842 else
16843 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016844theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016846
16847 return retval;
16848}
16849
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016850#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016851
16852/*
16853 * round() is not in C90, use ceil() or floor() instead.
16854 */
16855 float_T
16856vim_round(f)
16857 float_T f;
16858{
16859 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16860}
16861
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016862/*
16863 * "round({float})" function
16864 */
16865 static void
16866f_round(argvars, rettv)
16867 typval_T *argvars;
16868 typval_T *rettv;
16869{
16870 float_T f;
16871
16872 rettv->v_type = VAR_FLOAT;
16873 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016874 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016875 else
16876 rettv->vval.v_float = 0.0;
16877}
16878#endif
16879
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016880/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016881 * "screenattr()" function
16882 */
16883 static void
16884f_screenattr(argvars, rettv)
16885 typval_T *argvars UNUSED;
16886 typval_T *rettv;
16887{
16888 int row;
16889 int col;
16890 int c;
16891
16892 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16893 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16894 if (row < 0 || row >= screen_Rows
16895 || col < 0 || col >= screen_Columns)
16896 c = -1;
16897 else
16898 c = ScreenAttrs[LineOffset[row] + col];
16899 rettv->vval.v_number = c;
16900}
16901
16902/*
16903 * "screenchar()" function
16904 */
16905 static void
16906f_screenchar(argvars, rettv)
16907 typval_T *argvars UNUSED;
16908 typval_T *rettv;
16909{
16910 int row;
16911 int col;
16912 int off;
16913 int c;
16914
16915 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16916 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16917 if (row < 0 || row >= screen_Rows
16918 || col < 0 || col >= screen_Columns)
16919 c = -1;
16920 else
16921 {
16922 off = LineOffset[row] + col;
16923#ifdef FEAT_MBYTE
16924 if (enc_utf8 && ScreenLinesUC[off] != 0)
16925 c = ScreenLinesUC[off];
16926 else
16927#endif
16928 c = ScreenLines[off];
16929 }
16930 rettv->vval.v_number = c;
16931}
16932
16933/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016934 * "screencol()" function
16935 *
16936 * First column is 1 to be consistent with virtcol().
16937 */
16938 static void
16939f_screencol(argvars, rettv)
16940 typval_T *argvars UNUSED;
16941 typval_T *rettv;
16942{
16943 rettv->vval.v_number = screen_screencol() + 1;
16944}
16945
16946/*
16947 * "screenrow()" function
16948 */
16949 static void
16950f_screenrow(argvars, rettv)
16951 typval_T *argvars UNUSED;
16952 typval_T *rettv;
16953{
16954 rettv->vval.v_number = screen_screenrow() + 1;
16955}
16956
16957/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016958 * "search()" function
16959 */
16960 static void
16961f_search(argvars, rettv)
16962 typval_T *argvars;
16963 typval_T *rettv;
16964{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016965 int flags = 0;
16966
16967 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968}
16969
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016971 * "searchdecl()" function
16972 */
16973 static void
16974f_searchdecl(argvars, rettv)
16975 typval_T *argvars;
16976 typval_T *rettv;
16977{
16978 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016979 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016980 int error = FALSE;
16981 char_u *name;
16982
16983 rettv->vval.v_number = 1; /* default: FAIL */
16984
16985 name = get_tv_string_chk(&argvars[0]);
16986 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016987 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016988 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016989 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16990 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16991 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016992 if (!error && name != NULL)
16993 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016994 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016995}
16996
16997/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016998 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017000 static int
17001searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000017002 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017003 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004{
17005 char_u *spat, *mpat, *epat;
17006 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008 int dir;
17009 int flags = 0;
17010 char_u nbuf1[NUMBUFLEN];
17011 char_u nbuf2[NUMBUFLEN];
17012 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017013 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017014 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017015 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017018 spat = get_tv_string_chk(&argvars[0]);
17019 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17020 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17021 if (spat == NULL || mpat == NULL || epat == NULL)
17022 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 /* Handle the optional fourth argument: flags */
17025 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017026 if (dir == 0)
17027 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017028
17029 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017030 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17031 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017032 if ((flags & (SP_END | SP_SUBPAT)) != 0
17033 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017034 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017035 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017036 goto theend;
17037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017039 /* Using 'r' implies 'W', otherwise it doesn't work. */
17040 if (flags & SP_REPEAT)
17041 p_ws = FALSE;
17042
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017043 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017044 if (argvars[3].v_type == VAR_UNKNOWN
17045 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046 skip = (char_u *)"";
17047 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017049 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017050 if (argvars[5].v_type != VAR_UNKNOWN)
17051 {
17052 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17053 if (lnum_stop < 0)
17054 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017055#ifdef FEAT_RELTIME
17056 if (argvars[6].v_type != VAR_UNKNOWN)
17057 {
17058 time_limit = get_tv_number_chk(&argvars[6], NULL);
17059 if (time_limit < 0)
17060 goto theend;
17061 }
17062#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017063 }
17064 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017065 if (skip == NULL)
17066 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017068 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017069 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017070
17071theend:
17072 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017073
17074 return retval;
17075}
17076
17077/*
17078 * "searchpair()" function
17079 */
17080 static void
17081f_searchpair(argvars, rettv)
17082 typval_T *argvars;
17083 typval_T *rettv;
17084{
17085 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17086}
17087
17088/*
17089 * "searchpairpos()" function
17090 */
17091 static void
17092f_searchpairpos(argvars, rettv)
17093 typval_T *argvars;
17094 typval_T *rettv;
17095{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017096 pos_T match_pos;
17097 int lnum = 0;
17098 int col = 0;
17099
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017100 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017101 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017102
17103 if (searchpair_cmn(argvars, &match_pos) > 0)
17104 {
17105 lnum = match_pos.lnum;
17106 col = match_pos.col;
17107 }
17108
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017109 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17110 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017111}
17112
17113/*
17114 * Search for a start/middle/end thing.
17115 * Used by searchpair(), see its documentation for the details.
17116 * Returns 0 or -1 for no match,
17117 */
17118 long
Bram Moolenaar76929292008-01-06 19:07:36 +000017119do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
17120 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017121 char_u *spat; /* start pattern */
17122 char_u *mpat; /* middle pattern */
17123 char_u *epat; /* end pattern */
17124 int dir; /* BACKWARD or FORWARD */
17125 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017126 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017127 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017128 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017129 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017130{
17131 char_u *save_cpo;
17132 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17133 long retval = 0;
17134 pos_T pos;
17135 pos_T firstpos;
17136 pos_T foundpos;
17137 pos_T save_cursor;
17138 pos_T save_pos;
17139 int n;
17140 int r;
17141 int nest = 1;
17142 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017143 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017144 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017145
17146 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17147 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017148 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017149
Bram Moolenaar76929292008-01-06 19:07:36 +000017150#ifdef FEAT_RELTIME
17151 /* Set the time limit, if there is one. */
17152 profile_setlimit(time_limit, &tm);
17153#endif
17154
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017155 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17156 * start/middle/end (pat3, for the top pair). */
17157 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17158 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17159 if (pat2 == NULL || pat3 == NULL)
17160 goto theend;
17161 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17162 if (*mpat == NUL)
17163 STRCPY(pat3, pat2);
17164 else
17165 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17166 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017167 if (flags & SP_START)
17168 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017169
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 save_cursor = curwin->w_cursor;
17171 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017172 clearpos(&firstpos);
17173 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 pat = pat3;
17175 for (;;)
17176 {
17177 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017178 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17180 /* didn't find it or found the first match again: FAIL */
17181 break;
17182
17183 if (firstpos.lnum == 0)
17184 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017185 if (equalpos(pos, foundpos))
17186 {
17187 /* Found the same position again. Can happen with a pattern that
17188 * has "\zs" at the end and searching backwards. Advance one
17189 * character and try again. */
17190 if (dir == BACKWARD)
17191 decl(&pos);
17192 else
17193 incl(&pos);
17194 }
17195 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017197 /* clear the start flag to avoid getting stuck here */
17198 options &= ~SEARCH_START;
17199
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 /* If the skip pattern matches, ignore this match. */
17201 if (*skip != NUL)
17202 {
17203 save_pos = curwin->w_cursor;
17204 curwin->w_cursor = pos;
17205 r = eval_to_bool(skip, &err, NULL, FALSE);
17206 curwin->w_cursor = save_pos;
17207 if (err)
17208 {
17209 /* Evaluating {skip} caused an error, break here. */
17210 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017211 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212 break;
17213 }
17214 if (r)
17215 continue;
17216 }
17217
17218 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17219 {
17220 /* Found end when searching backwards or start when searching
17221 * forward: nested pair. */
17222 ++nest;
17223 pat = pat2; /* nested, don't search for middle */
17224 }
17225 else
17226 {
17227 /* Found end when searching forward or start when searching
17228 * backward: end of (nested) pair; or found middle in outer pair. */
17229 if (--nest == 1)
17230 pat = pat3; /* outer level, search for middle */
17231 }
17232
17233 if (nest == 0)
17234 {
17235 /* Found the match: return matchcount or line number. */
17236 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017237 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017239 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017240 if (flags & SP_SETPCMARK)
17241 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242 curwin->w_cursor = pos;
17243 if (!(flags & SP_REPEAT))
17244 break;
17245 nest = 1; /* search for next unmatched */
17246 }
17247 }
17248
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017249 if (match_pos != NULL)
17250 {
17251 /* Store the match cursor position */
17252 match_pos->lnum = curwin->w_cursor.lnum;
17253 match_pos->col = curwin->w_cursor.col + 1;
17254 }
17255
Bram Moolenaar071d4272004-06-13 20:20:40 +000017256 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017257 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 curwin->w_cursor = save_cursor;
17259
17260theend:
17261 vim_free(pat2);
17262 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017263 if (p_cpo == empty_option)
17264 p_cpo = save_cpo;
17265 else
17266 /* Darn, evaluating the {skip} expression changed the value. */
17267 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017268
17269 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270}
17271
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017272/*
17273 * "searchpos()" function
17274 */
17275 static void
17276f_searchpos(argvars, rettv)
17277 typval_T *argvars;
17278 typval_T *rettv;
17279{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017280 pos_T match_pos;
17281 int lnum = 0;
17282 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017283 int n;
17284 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017285
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017286 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017287 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017288
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017289 n = search_cmn(argvars, &match_pos, &flags);
17290 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017291 {
17292 lnum = match_pos.lnum;
17293 col = match_pos.col;
17294 }
17295
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017296 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17297 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017298 if (flags & SP_SUBPAT)
17299 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017300}
17301
17302
Bram Moolenaar0d660222005-01-07 21:51:51 +000017303 static void
17304f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017305 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017308#ifdef FEAT_CLIENTSERVER
17309 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017310 char_u *server = get_tv_string_chk(&argvars[0]);
17311 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312
Bram Moolenaar0d660222005-01-07 21:51:51 +000017313 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017314 if (server == NULL || reply == NULL)
17315 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017316 if (check_restricted() || check_secure())
17317 return;
17318# ifdef FEAT_X11
17319 if (check_connection() == FAIL)
17320 return;
17321# endif
17322
17323 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017325 EMSG(_("E258: Unable to send to client"));
17326 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017328 rettv->vval.v_number = 0;
17329#else
17330 rettv->vval.v_number = -1;
17331#endif
17332}
17333
Bram Moolenaar0d660222005-01-07 21:51:51 +000017334 static void
17335f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017336 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017337 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017338{
17339 char_u *r = NULL;
17340
17341#ifdef FEAT_CLIENTSERVER
17342# ifdef WIN32
17343 r = serverGetVimNames();
17344# else
17345 make_connection();
17346 if (X_DISPLAY != NULL)
17347 r = serverGetVimNames(X_DISPLAY);
17348# endif
17349#endif
17350 rettv->v_type = VAR_STRING;
17351 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352}
17353
17354/*
17355 * "setbufvar()" function
17356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017358f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017359 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017360 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361{
17362 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017365 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 char_u nbuf[NUMBUFLEN];
17367
17368 if (check_restricted() || check_secure())
17369 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017370 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17371 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017372 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 varp = &argvars[2];
17374
17375 if (buf != NULL && varname != NULL && varp != NULL)
17376 {
17377 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379
17380 if (*varname == '&')
17381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017382 long numval;
17383 char_u *strval;
17384 int error = FALSE;
17385
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017387 numval = get_tv_number_chk(varp, &error);
17388 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017389 if (!error && strval != NULL)
17390 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391 }
17392 else
17393 {
17394 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17395 if (bufvarname != NULL)
17396 {
17397 STRCPY(bufvarname, "b:");
17398 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017399 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400 vim_free(bufvarname);
17401 }
17402 }
17403
17404 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407}
17408
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017409 static void
17410f_setcharsearch(argvars, rettv)
17411 typval_T *argvars;
17412 typval_T *rettv UNUSED;
17413{
17414 dict_T *d;
17415 dictitem_T *di;
17416 char_u *csearch;
17417
17418 if (argvars[0].v_type != VAR_DICT)
17419 {
17420 EMSG(_(e_dictreq));
17421 return;
17422 }
17423
17424 if ((d = argvars[0].vval.v_dict) != NULL)
17425 {
17426 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17427 if (csearch != NULL)
17428 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017429#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017430 if (enc_utf8)
17431 {
17432 int pcc[MAX_MCO];
17433 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017434
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017435 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17436 }
17437 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017438#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017439 set_last_csearch(PTR2CHAR(csearch),
17440 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017441 }
17442
17443 di = dict_find(d, (char_u *)"forward", -1);
17444 if (di != NULL)
17445 set_csearch_direction(get_tv_number(&di->di_tv)
17446 ? FORWARD : BACKWARD);
17447
17448 di = dict_find(d, (char_u *)"until", -1);
17449 if (di != NULL)
17450 set_csearch_until(!!get_tv_number(&di->di_tv));
17451 }
17452}
17453
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454/*
17455 * "setcmdpos()" function
17456 */
17457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017458f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017459 typval_T *argvars;
17460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017462 int pos = (int)get_tv_number(&argvars[0]) - 1;
17463
17464 if (pos >= 0)
17465 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017466}
17467
17468/*
17469 * "setline()" function
17470 */
17471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017472f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017473 typval_T *argvars;
17474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475{
17476 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017477 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017478 list_T *l = NULL;
17479 listitem_T *li = NULL;
17480 long added = 0;
17481 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017483 lnum = get_tv_lnum(&argvars[0]);
17484 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017486 l = argvars[1].vval.v_list;
17487 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017489 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017490 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017491
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017492 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017493 for (;;)
17494 {
17495 if (l != NULL)
17496 {
17497 /* list argument, get next string */
17498 if (li == NULL)
17499 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017500 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017501 li = li->li_next;
17502 }
17503
17504 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017505 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017506 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017507
17508 /* When coming here from Insert mode, sync undo, so that this can be
17509 * undone separately from what was previously inserted. */
17510 if (u_sync_once == 2)
17511 {
17512 u_sync_once = 1; /* notify that u_sync() was called */
17513 u_sync(TRUE);
17514 }
17515
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017516 if (lnum <= curbuf->b_ml.ml_line_count)
17517 {
17518 /* existing line, replace it */
17519 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17520 {
17521 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017522 if (lnum == curwin->w_cursor.lnum)
17523 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017524 rettv->vval.v_number = 0; /* OK */
17525 }
17526 }
17527 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17528 {
17529 /* lnum is one past the last line, append the line */
17530 ++added;
17531 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17532 rettv->vval.v_number = 0; /* OK */
17533 }
17534
17535 if (l == NULL) /* only one string argument */
17536 break;
17537 ++lnum;
17538 }
17539
17540 if (added > 0)
17541 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542}
17543
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017544static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17545
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017547 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017548 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017549 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017550set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017551 win_T *wp UNUSED;
17552 typval_T *list_arg UNUSED;
17553 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017554 typval_T *rettv;
17555{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017556#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017557 char_u *act;
17558 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017559#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017560
Bram Moolenaar2641f772005-03-25 21:58:17 +000017561 rettv->vval.v_number = -1;
17562
17563#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017564 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017565 EMSG(_(e_listreq));
17566 else
17567 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017568 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017569
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017570 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017571 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017572 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017573 if (act == NULL)
17574 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017575 if (*act == 'a' || *act == 'r')
17576 action = *act;
17577 }
17578
Bram Moolenaar81484f42012-12-05 15:16:47 +010017579 if (l != NULL && set_errorlist(wp, l, action,
17580 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017581 rettv->vval.v_number = 0;
17582 }
17583#endif
17584}
17585
17586/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017587 * "setloclist()" function
17588 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017589 static void
17590f_setloclist(argvars, rettv)
17591 typval_T *argvars;
17592 typval_T *rettv;
17593{
17594 win_T *win;
17595
17596 rettv->vval.v_number = -1;
17597
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017598 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017599 if (win != NULL)
17600 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17601}
17602
17603/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017604 * "setmatches()" function
17605 */
17606 static void
17607f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017608 typval_T *argvars UNUSED;
17609 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017610{
17611#ifdef FEAT_SEARCH_EXTRA
17612 list_T *l;
17613 listitem_T *li;
17614 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017615 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017616
17617 rettv->vval.v_number = -1;
17618 if (argvars[0].v_type != VAR_LIST)
17619 {
17620 EMSG(_(e_listreq));
17621 return;
17622 }
17623 if ((l = argvars[0].vval.v_list) != NULL)
17624 {
17625
17626 /* To some extent make sure that we are dealing with a list from
17627 * "getmatches()". */
17628 li = l->lv_first;
17629 while (li != NULL)
17630 {
17631 if (li->li_tv.v_type != VAR_DICT
17632 || (d = li->li_tv.vval.v_dict) == NULL)
17633 {
17634 EMSG(_(e_invarg));
17635 return;
17636 }
17637 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017638 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17639 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017640 && dict_find(d, (char_u *)"priority", -1) != NULL
17641 && dict_find(d, (char_u *)"id", -1) != NULL))
17642 {
17643 EMSG(_(e_invarg));
17644 return;
17645 }
17646 li = li->li_next;
17647 }
17648
17649 clear_matches(curwin);
17650 li = l->lv_first;
17651 while (li != NULL)
17652 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017653 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017654 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017655 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017656 char_u *group;
17657 int priority;
17658 int id;
17659 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017660
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017661 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017662 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17663 {
17664 if (s == NULL)
17665 {
17666 s = list_alloc();
17667 if (s == NULL)
17668 return;
17669 }
17670
17671 /* match from matchaddpos() */
17672 for (i = 1; i < 9; i++)
17673 {
17674 sprintf((char *)buf, (char *)"pos%d", i);
17675 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17676 {
17677 if (di->di_tv.v_type != VAR_LIST)
17678 return;
17679
17680 list_append_tv(s, &di->di_tv);
17681 s->lv_refcount++;
17682 }
17683 else
17684 break;
17685 }
17686 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017687
17688 group = get_dict_string(d, (char_u *)"group", FALSE);
17689 priority = (int)get_dict_number(d, (char_u *)"priority");
17690 id = (int)get_dict_number(d, (char_u *)"id");
17691 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17692 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17693 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017694 if (i == 0)
17695 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017696 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017697 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017698 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017699 }
17700 else
17701 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017702 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017703 list_unref(s);
17704 s = NULL;
17705 }
17706
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017707 li = li->li_next;
17708 }
17709 rettv->vval.v_number = 0;
17710 }
17711#endif
17712}
17713
17714/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017715 * "setpos()" function
17716 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017717 static void
17718f_setpos(argvars, rettv)
17719 typval_T *argvars;
17720 typval_T *rettv;
17721{
17722 pos_T pos;
17723 int fnum;
17724 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017725 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017726
Bram Moolenaar08250432008-02-13 11:42:46 +000017727 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017728 name = get_tv_string_chk(argvars);
17729 if (name != NULL)
17730 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017731 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017732 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017733 if (--pos.col < 0)
17734 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017735 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017736 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017737 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017738 if (fnum == curbuf->b_fnum)
17739 {
17740 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017741 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017742 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017743 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017744 curwin->w_set_curswant = FALSE;
17745 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017746 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017747 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017748 }
17749 else
17750 EMSG(_(e_invarg));
17751 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017752 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17753 {
17754 /* set mark */
17755 if (setmark_pos(name[1], &pos, fnum) == OK)
17756 rettv->vval.v_number = 0;
17757 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017758 else
17759 EMSG(_(e_invarg));
17760 }
17761 }
17762}
17763
17764/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017765 * "setqflist()" function
17766 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017767 static void
17768f_setqflist(argvars, rettv)
17769 typval_T *argvars;
17770 typval_T *rettv;
17771{
17772 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17773}
17774
17775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 * "setreg()" function
17777 */
17778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017779f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017780 typval_T *argvars;
17781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782{
17783 int regname;
17784 char_u *strregname;
17785 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017786 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 int append;
17788 char_u yank_type;
17789 long block_len;
17790
17791 block_len = -1;
17792 yank_type = MAUTO;
17793 append = FALSE;
17794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017795 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017796 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017798 if (strregname == NULL)
17799 return; /* type error; errmsg already given */
17800 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801 if (regname == 0 || regname == '@')
17802 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017804 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017806 stropt = get_tv_string_chk(&argvars[2]);
17807 if (stropt == NULL)
17808 return; /* type error */
17809 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810 switch (*stropt)
17811 {
17812 case 'a': case 'A': /* append */
17813 append = TRUE;
17814 break;
17815 case 'v': case 'c': /* character-wise selection */
17816 yank_type = MCHAR;
17817 break;
17818 case 'V': case 'l': /* line-wise selection */
17819 yank_type = MLINE;
17820 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821 case 'b': case Ctrl_V: /* block-wise selection */
17822 yank_type = MBLOCK;
17823 if (VIM_ISDIGIT(stropt[1]))
17824 {
17825 ++stropt;
17826 block_len = getdigits(&stropt) - 1;
17827 --stropt;
17828 }
17829 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830 }
17831 }
17832
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017833 if (argvars[1].v_type == VAR_LIST)
17834 {
17835 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017836 char_u **allocval;
17837 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017838 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017839 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017840 int len = argvars[1].vval.v_list->lv_len;
17841 listitem_T *li;
17842
Bram Moolenaar7d647822014-04-05 21:28:56 +020017843 /* First half: use for pointers to result lines; second half: use for
17844 * pointers to allocated copies. */
17845 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017846 if (lstval == NULL)
17847 return;
17848 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017849 allocval = lstval + len + 2;
17850 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017851
17852 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17853 li = li->li_next)
17854 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017855 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017856 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017857 goto free_lstval;
17858 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017859 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017860 /* Need to make a copy, next get_tv_string_buf_chk() will
17861 * overwrite the string. */
17862 strval = vim_strsave(buf);
17863 if (strval == NULL)
17864 goto free_lstval;
17865 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017866 }
17867 *curval++ = strval;
17868 }
17869 *curval++ = NULL;
17870
17871 write_reg_contents_lst(regname, lstval, -1,
17872 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017873free_lstval:
17874 while (curallocval > allocval)
17875 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017876 vim_free(lstval);
17877 }
17878 else
17879 {
17880 strval = get_tv_string_chk(&argvars[1]);
17881 if (strval == NULL)
17882 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017883 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017885 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017886 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887}
17888
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017889/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017890 * "settabvar()" function
17891 */
17892 static void
17893f_settabvar(argvars, rettv)
17894 typval_T *argvars;
17895 typval_T *rettv;
17896{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017897#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017898 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017899 tabpage_T *tp;
17900#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017901 char_u *varname, *tabvarname;
17902 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017903
17904 rettv->vval.v_number = 0;
17905
17906 if (check_restricted() || check_secure())
17907 return;
17908
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017909#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017910 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017911#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017912 varname = get_tv_string_chk(&argvars[1]);
17913 varp = &argvars[2];
17914
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017915 if (varname != NULL && varp != NULL
17916#ifdef FEAT_WINDOWS
17917 && tp != NULL
17918#endif
17919 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017920 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017921#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017922 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017923 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017924#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017925
17926 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17927 if (tabvarname != NULL)
17928 {
17929 STRCPY(tabvarname, "t:");
17930 STRCPY(tabvarname + 2, varname);
17931 set_var(tabvarname, varp, TRUE);
17932 vim_free(tabvarname);
17933 }
17934
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017935#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017936 /* Restore current tabpage */
17937 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017938 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017939#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017940 }
17941}
17942
17943/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017944 * "settabwinvar()" function
17945 */
17946 static void
17947f_settabwinvar(argvars, rettv)
17948 typval_T *argvars;
17949 typval_T *rettv;
17950{
17951 setwinvar(argvars, rettv, 1);
17952}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953
17954/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017955 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017958f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017959 typval_T *argvars;
17960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017962 setwinvar(argvars, rettv, 0);
17963}
17964
17965/*
17966 * "setwinvar()" and "settabwinvar()" functions
17967 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017968
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017969 static void
17970setwinvar(argvars, rettv, off)
17971 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017972 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017973 int off;
17974{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 win_T *win;
17976#ifdef FEAT_WINDOWS
17977 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017978 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017979 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980#endif
17981 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017982 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017984 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017985
17986 if (check_restricted() || check_secure())
17987 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017988
17989#ifdef FEAT_WINDOWS
17990 if (off == 1)
17991 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17992 else
17993 tp = curtab;
17994#endif
17995 win = find_win_by_nr(&argvars[off], tp);
17996 varname = get_tv_string_chk(&argvars[off + 1]);
17997 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998
17999 if (win != NULL && varname != NULL && varp != NULL)
18000 {
18001#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018002 need_switch_win = !(tp == curtab && win == curwin);
18003 if (!need_switch_win
18004 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018007 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018009 long numval;
18010 char_u *strval;
18011 int error = FALSE;
18012
18013 ++varname;
18014 numval = get_tv_number_chk(varp, &error);
18015 strval = get_tv_string_buf_chk(varp, nbuf);
18016 if (!error && strval != NULL)
18017 set_option_value(varname, numval, strval, OPT_LOCAL);
18018 }
18019 else
18020 {
18021 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18022 if (winvarname != NULL)
18023 {
18024 STRCPY(winvarname, "w:");
18025 STRCPY(winvarname + 2, varname);
18026 set_var(winvarname, varp, TRUE);
18027 vim_free(winvarname);
18028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029 }
18030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018032 if (need_switch_win)
18033 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034#endif
18035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036}
18037
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018038#ifdef FEAT_CRYPT
18039/*
18040 * "sha256({string})" function
18041 */
18042 static void
18043f_sha256(argvars, rettv)
18044 typval_T *argvars;
18045 typval_T *rettv;
18046{
18047 char_u *p;
18048
18049 p = get_tv_string(&argvars[0]);
18050 rettv->vval.v_string = vim_strsave(
18051 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18052 rettv->v_type = VAR_STRING;
18053}
18054#endif /* FEAT_CRYPT */
18055
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018057 * "shellescape({string})" function
18058 */
18059 static void
18060f_shellescape(argvars, rettv)
18061 typval_T *argvars;
18062 typval_T *rettv;
18063{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018064 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018065 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018066 rettv->v_type = VAR_STRING;
18067}
18068
18069/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018070 * shiftwidth() function
18071 */
18072 static void
18073f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020018074 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018075 typval_T *rettv;
18076{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018077 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018078}
18079
18080/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018081 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 */
18083 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000018084f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018085 typval_T *argvars;
18086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018088 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089
Bram Moolenaar0d660222005-01-07 21:51:51 +000018090 p = get_tv_string(&argvars[0]);
18091 rettv->vval.v_string = vim_strsave(p);
18092 simplify_filename(rettv->vval.v_string); /* simplify in place */
18093 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094}
18095
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018096#ifdef FEAT_FLOAT
18097/*
18098 * "sin()" function
18099 */
18100 static void
18101f_sin(argvars, rettv)
18102 typval_T *argvars;
18103 typval_T *rettv;
18104{
18105 float_T f;
18106
18107 rettv->v_type = VAR_FLOAT;
18108 if (get_float_arg(argvars, &f) == OK)
18109 rettv->vval.v_float = sin(f);
18110 else
18111 rettv->vval.v_float = 0.0;
18112}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018113
18114/*
18115 * "sinh()" function
18116 */
18117 static void
18118f_sinh(argvars, rettv)
18119 typval_T *argvars;
18120 typval_T *rettv;
18121{
18122 float_T f;
18123
18124 rettv->v_type = VAR_FLOAT;
18125 if (get_float_arg(argvars, &f) == OK)
18126 rettv->vval.v_float = sinh(f);
18127 else
18128 rettv->vval.v_float = 0.0;
18129}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018130#endif
18131
Bram Moolenaar0d660222005-01-07 21:51:51 +000018132static int
18133#ifdef __BORLANDC__
18134 _RTLENTRYF
18135#endif
18136 item_compare __ARGS((const void *s1, const void *s2));
18137static int
18138#ifdef __BORLANDC__
18139 _RTLENTRYF
18140#endif
18141 item_compare2 __ARGS((const void *s1, const void *s2));
18142
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018143/* struct used in the array that's given to qsort() */
18144typedef struct
18145{
18146 listitem_T *item;
18147 int idx;
18148} sortItem_T;
18149
Bram Moolenaar0d660222005-01-07 21:51:51 +000018150static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018151static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018152static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018153#ifdef FEAT_FLOAT
18154static int item_compare_float;
18155#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018156static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018157static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018158static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018159static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018160static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018161#define ITEM_COMPARE_FAIL 999
18162
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018164 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018166 static int
18167#ifdef __BORLANDC__
18168_RTLENTRYF
18169#endif
18170item_compare(s1, s2)
18171 const void *s1;
18172 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018174 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018175 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018176 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018177 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018178 int res;
18179 char_u numbuf1[NUMBUFLEN];
18180 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018182 si1 = (sortItem_T *)s1;
18183 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018184 tv1 = &si1->item->li_tv;
18185 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018186
18187 if (item_compare_numbers)
18188 {
18189 long v1 = get_tv_number(tv1);
18190 long v2 = get_tv_number(tv2);
18191
18192 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18193 }
18194
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018195#ifdef FEAT_FLOAT
18196 if (item_compare_float)
18197 {
18198 float_T v1 = get_tv_float(tv1);
18199 float_T v2 = get_tv_float(tv2);
18200
18201 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18202 }
18203#endif
18204
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018205 /* tv2string() puts quotes around a string and allocates memory. Don't do
18206 * that for string variables. Use a single quote when comparing with a
18207 * non-string to do what the docs promise. */
18208 if (tv1->v_type == VAR_STRING)
18209 {
18210 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18211 p1 = (char_u *)"'";
18212 else
18213 p1 = tv1->vval.v_string;
18214 }
18215 else
18216 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18217 if (tv2->v_type == VAR_STRING)
18218 {
18219 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18220 p2 = (char_u *)"'";
18221 else
18222 p2 = tv2->vval.v_string;
18223 }
18224 else
18225 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018226 if (p1 == NULL)
18227 p1 = (char_u *)"";
18228 if (p2 == NULL)
18229 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018230 if (!item_compare_numeric)
18231 {
18232 if (item_compare_ic)
18233 res = STRICMP(p1, p2);
18234 else
18235 res = STRCMP(p1, p2);
18236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018238 {
18239 double n1, n2;
18240 n1 = strtod((char *)p1, (char **)&p1);
18241 n2 = strtod((char *)p2, (char **)&p2);
18242 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18243 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018244
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018245 /* When the result would be zero, compare the item indexes. Makes the
18246 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018247 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018248 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018249
Bram Moolenaar0d660222005-01-07 21:51:51 +000018250 vim_free(tofree1);
18251 vim_free(tofree2);
18252 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253}
18254
18255 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018256#ifdef __BORLANDC__
18257_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018259item_compare2(s1, s2)
18260 const void *s1;
18261 const void *s2;
18262{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018263 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018265 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018266 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018267 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018269 /* shortcut after failure in previous call; compare all items equal */
18270 if (item_compare_func_err)
18271 return 0;
18272
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018273 si1 = (sortItem_T *)s1;
18274 si2 = (sortItem_T *)s2;
18275
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018276 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018277 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018278 copy_tv(&si1->item->li_tv, &argv[0]);
18279 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018280
18281 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018282 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018283 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18284 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018285 clear_tv(&argv[0]);
18286 clear_tv(&argv[1]);
18287
18288 if (res == FAIL)
18289 res = ITEM_COMPARE_FAIL;
18290 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018291 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18292 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018293 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018294 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018295
18296 /* When the result would be zero, compare the pointers themselves. Makes
18297 * the sort stable. */
18298 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018299 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018300
Bram Moolenaar0d660222005-01-07 21:51:51 +000018301 return res;
18302}
18303
18304/*
18305 * "sort({list})" function
18306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018308do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018309 typval_T *argvars;
18310 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018311 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312{
Bram Moolenaar33570922005-01-25 22:26:29 +000018313 list_T *l;
18314 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018315 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018316 long len;
18317 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318
Bram Moolenaar0d660222005-01-07 21:51:51 +000018319 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018320 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 else
18322 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018323 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018324 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018325 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18326 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018327 return;
18328 rettv->vval.v_list = l;
18329 rettv->v_type = VAR_LIST;
18330 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331
Bram Moolenaar0d660222005-01-07 21:51:51 +000018332 len = list_len(l);
18333 if (len <= 1)
18334 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335
Bram Moolenaar0d660222005-01-07 21:51:51 +000018336 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018337 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018338 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018339#ifdef FEAT_FLOAT
18340 item_compare_float = FALSE;
18341#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018342 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018343 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018344 if (argvars[1].v_type != VAR_UNKNOWN)
18345 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018346 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018347 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018348 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018349 else
18350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018351 int error = FALSE;
18352
18353 i = get_tv_number_chk(&argvars[1], &error);
18354 if (error)
18355 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018356 if (i == 1)
18357 item_compare_ic = TRUE;
18358 else
18359 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018360 if (item_compare_func != NULL)
18361 {
18362 if (STRCMP(item_compare_func, "n") == 0)
18363 {
18364 item_compare_func = NULL;
18365 item_compare_numeric = TRUE;
18366 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018367 else if (STRCMP(item_compare_func, "N") == 0)
18368 {
18369 item_compare_func = NULL;
18370 item_compare_numbers = TRUE;
18371 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018372#ifdef FEAT_FLOAT
18373 else if (STRCMP(item_compare_func, "f") == 0)
18374 {
18375 item_compare_func = NULL;
18376 item_compare_float = TRUE;
18377 }
18378#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018379 else if (STRCMP(item_compare_func, "i") == 0)
18380 {
18381 item_compare_func = NULL;
18382 item_compare_ic = TRUE;
18383 }
18384 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018385 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018386
18387 if (argvars[2].v_type != VAR_UNKNOWN)
18388 {
18389 /* optional third argument: {dict} */
18390 if (argvars[2].v_type != VAR_DICT)
18391 {
18392 EMSG(_(e_dictreq));
18393 return;
18394 }
18395 item_compare_selfdict = argvars[2].vval.v_dict;
18396 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398
Bram Moolenaar0d660222005-01-07 21:51:51 +000018399 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018400 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018401 if (ptrs == NULL)
18402 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403
Bram Moolenaar327aa022014-03-25 18:24:23 +010018404 i = 0;
18405 if (sort)
18406 {
18407 /* sort(): ptrs will be the list to sort */
18408 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018409 {
18410 ptrs[i].item = li;
18411 ptrs[i].idx = i;
18412 ++i;
18413 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018414
18415 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018416 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018417 /* test the compare function */
18418 if (item_compare_func != NULL
18419 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018420 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018421 EMSG(_("E702: Sort compare function failed"));
18422 else
18423 {
18424 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018425 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018426 item_compare_func == NULL ? item_compare : item_compare2);
18427
18428 if (!item_compare_func_err)
18429 {
18430 /* Clear the List and append the items in sorted order. */
18431 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18432 l->lv_len = 0;
18433 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018434 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018435 }
18436 }
18437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018439 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018440 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18441
18442 /* f_uniq(): ptrs will be a stack of items to remove */
18443 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018444 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018445 item_compare_func_ptr = item_compare_func
18446 ? item_compare2 : item_compare;
18447
18448 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18449 li = li->li_next)
18450 {
18451 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18452 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018453 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018454 if (item_compare_func_err)
18455 {
18456 EMSG(_("E882: Uniq compare function failed"));
18457 break;
18458 }
18459 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018461 if (!item_compare_func_err)
18462 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018463 while (--i >= 0)
18464 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018465 li = ptrs[i].item->li_next;
18466 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018467 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018468 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018469 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018470 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018471 list_fix_watch(l, li);
18472 listitem_free(li);
18473 l->lv_len--;
18474 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018475 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018476 }
18477
18478 vim_free(ptrs);
18479 }
18480}
18481
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018482/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018483 * "sort({list})" function
18484 */
18485 static void
18486f_sort(argvars, rettv)
18487 typval_T *argvars;
18488 typval_T *rettv;
18489{
18490 do_sort_uniq(argvars, rettv, TRUE);
18491}
18492
18493/*
18494 * "uniq({list})" function
18495 */
18496 static void
18497f_uniq(argvars, rettv)
18498 typval_T *argvars;
18499 typval_T *rettv;
18500{
18501 do_sort_uniq(argvars, rettv, FALSE);
18502}
18503
18504/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018505 * "soundfold({word})" function
18506 */
18507 static void
18508f_soundfold(argvars, rettv)
18509 typval_T *argvars;
18510 typval_T *rettv;
18511{
18512 char_u *s;
18513
18514 rettv->v_type = VAR_STRING;
18515 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018516#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018517 rettv->vval.v_string = eval_soundfold(s);
18518#else
18519 rettv->vval.v_string = vim_strsave(s);
18520#endif
18521}
18522
18523/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018524 * "spellbadword()" function
18525 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018526 static void
18527f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018528 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018529 typval_T *rettv;
18530{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018531 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018532 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018533 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018534
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018535 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018536 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018537
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018538#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018539 if (argvars[0].v_type == VAR_UNKNOWN)
18540 {
18541 /* Find the start and length of the badly spelled word. */
18542 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18543 if (len != 0)
18544 word = ml_get_cursor();
18545 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018546 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018547 {
18548 char_u *str = get_tv_string_chk(&argvars[0]);
18549 int capcol = -1;
18550
18551 if (str != NULL)
18552 {
18553 /* Check the argument for spelling. */
18554 while (*str != NUL)
18555 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018556 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018557 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018558 {
18559 word = str;
18560 break;
18561 }
18562 str += len;
18563 }
18564 }
18565 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018566#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018567
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018568 list_append_string(rettv->vval.v_list, word, len);
18569 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018570 attr == HLF_SPB ? "bad" :
18571 attr == HLF_SPR ? "rare" :
18572 attr == HLF_SPL ? "local" :
18573 attr == HLF_SPC ? "caps" :
18574 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018575}
18576
18577/*
18578 * "spellsuggest()" function
18579 */
18580 static void
18581f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018582 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018583 typval_T *rettv;
18584{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018585#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018586 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018587 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018588 int maxcount;
18589 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018590 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018591 listitem_T *li;
18592 int need_capital = FALSE;
18593#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018594
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018595 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018596 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018597
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018598#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018599 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018600 {
18601 str = get_tv_string(&argvars[0]);
18602 if (argvars[1].v_type != VAR_UNKNOWN)
18603 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018604 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018605 if (maxcount <= 0)
18606 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018607 if (argvars[2].v_type != VAR_UNKNOWN)
18608 {
18609 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18610 if (typeerr)
18611 return;
18612 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018613 }
18614 else
18615 maxcount = 25;
18616
Bram Moolenaar4770d092006-01-12 23:22:24 +000018617 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018618
18619 for (i = 0; i < ga.ga_len; ++i)
18620 {
18621 str = ((char_u **)ga.ga_data)[i];
18622
18623 li = listitem_alloc();
18624 if (li == NULL)
18625 vim_free(str);
18626 else
18627 {
18628 li->li_tv.v_type = VAR_STRING;
18629 li->li_tv.v_lock = 0;
18630 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018631 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018632 }
18633 }
18634 ga_clear(&ga);
18635 }
18636#endif
18637}
18638
Bram Moolenaar0d660222005-01-07 21:51:51 +000018639 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018640f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018641 typval_T *argvars;
18642 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018643{
18644 char_u *str;
18645 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018646 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018647 regmatch_T regmatch;
18648 char_u patbuf[NUMBUFLEN];
18649 char_u *save_cpo;
18650 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018651 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018652 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018653 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018654
18655 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18656 save_cpo = p_cpo;
18657 p_cpo = (char_u *)"";
18658
18659 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018660 if (argvars[1].v_type != VAR_UNKNOWN)
18661 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018662 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18663 if (pat == NULL)
18664 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018665 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018666 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018667 }
18668 if (pat == NULL || *pat == NUL)
18669 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018670
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018671 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018673 if (typeerr)
18674 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675
Bram Moolenaar0d660222005-01-07 21:51:51 +000018676 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18677 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018679 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018680 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018681 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018682 if (*str == NUL)
18683 match = FALSE; /* empty item at the end */
18684 else
18685 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018686 if (match)
18687 end = regmatch.startp[0];
18688 else
18689 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018690 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18691 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018692 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018693 if (list_append_string(rettv->vval.v_list, str,
18694 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018695 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018696 }
18697 if (!match)
18698 break;
18699 /* Advance to just after the match. */
18700 if (regmatch.endp[0] > str)
18701 col = 0;
18702 else
18703 {
18704 /* Don't get stuck at the same match. */
18705#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018706 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018707#else
18708 col = 1;
18709#endif
18710 }
18711 str = regmatch.endp[0];
18712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713
Bram Moolenaar473de612013-06-08 18:19:48 +020018714 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716
Bram Moolenaar0d660222005-01-07 21:51:51 +000018717 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718}
18719
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018720#ifdef FEAT_FLOAT
18721/*
18722 * "sqrt()" function
18723 */
18724 static void
18725f_sqrt(argvars, rettv)
18726 typval_T *argvars;
18727 typval_T *rettv;
18728{
18729 float_T f;
18730
18731 rettv->v_type = VAR_FLOAT;
18732 if (get_float_arg(argvars, &f) == OK)
18733 rettv->vval.v_float = sqrt(f);
18734 else
18735 rettv->vval.v_float = 0.0;
18736}
18737
18738/*
18739 * "str2float()" function
18740 */
18741 static void
18742f_str2float(argvars, rettv)
18743 typval_T *argvars;
18744 typval_T *rettv;
18745{
18746 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18747
18748 if (*p == '+')
18749 p = skipwhite(p + 1);
18750 (void)string2float(p, &rettv->vval.v_float);
18751 rettv->v_type = VAR_FLOAT;
18752}
18753#endif
18754
Bram Moolenaar2c932302006-03-18 21:42:09 +000018755/*
18756 * "str2nr()" function
18757 */
18758 static void
18759f_str2nr(argvars, rettv)
18760 typval_T *argvars;
18761 typval_T *rettv;
18762{
18763 int base = 10;
18764 char_u *p;
18765 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018766 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018767
18768 if (argvars[1].v_type != VAR_UNKNOWN)
18769 {
18770 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018771 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018772 {
18773 EMSG(_(e_invarg));
18774 return;
18775 }
18776 }
18777
18778 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018779 if (*p == '+')
18780 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018781 switch (base)
18782 {
18783 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18784 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18785 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18786 default: what = 0;
18787 }
18788 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018789 rettv->vval.v_number = n;
18790}
18791
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792#ifdef HAVE_STRFTIME
18793/*
18794 * "strftime({format}[, {time}])" function
18795 */
18796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018797f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018798 typval_T *argvars;
18799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800{
18801 char_u result_buf[256];
18802 struct tm *curtime;
18803 time_t seconds;
18804 char_u *p;
18805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018806 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018808 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018809 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810 seconds = time(NULL);
18811 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018812 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813 curtime = localtime(&seconds);
18814 /* MSVC returns NULL for an invalid value of seconds. */
18815 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018816 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817 else
18818 {
18819# ifdef FEAT_MBYTE
18820 vimconv_T conv;
18821 char_u *enc;
18822
18823 conv.vc_type = CONV_NONE;
18824 enc = enc_locale();
18825 convert_setup(&conv, p_enc, enc);
18826 if (conv.vc_type != CONV_NONE)
18827 p = string_convert(&conv, p, NULL);
18828# endif
18829 if (p != NULL)
18830 (void)strftime((char *)result_buf, sizeof(result_buf),
18831 (char *)p, curtime);
18832 else
18833 result_buf[0] = NUL;
18834
18835# ifdef FEAT_MBYTE
18836 if (conv.vc_type != CONV_NONE)
18837 vim_free(p);
18838 convert_setup(&conv, enc, p_enc);
18839 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018840 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841 else
18842# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018843 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844
18845# ifdef FEAT_MBYTE
18846 /* Release conversion descriptors */
18847 convert_setup(&conv, NULL, NULL);
18848 vim_free(enc);
18849# endif
18850 }
18851}
18852#endif
18853
18854/*
18855 * "stridx()" function
18856 */
18857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018858f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018859 typval_T *argvars;
18860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861{
18862 char_u buf[NUMBUFLEN];
18863 char_u *needle;
18864 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018865 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018867 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018869 needle = get_tv_string_chk(&argvars[1]);
18870 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018871 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018872 if (needle == NULL || haystack == NULL)
18873 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874
Bram Moolenaar33570922005-01-25 22:26:29 +000018875 if (argvars[2].v_type != VAR_UNKNOWN)
18876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018877 int error = FALSE;
18878
18879 start_idx = get_tv_number_chk(&argvars[2], &error);
18880 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018881 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018882 if (start_idx >= 0)
18883 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018884 }
18885
18886 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18887 if (pos != NULL)
18888 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889}
18890
18891/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018892 * "string()" function
18893 */
18894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018895f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018896 typval_T *argvars;
18897 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018898{
18899 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018900 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018902 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018903 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018904 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018905 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018906 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907}
18908
18909/*
18910 * "strlen()" function
18911 */
18912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018913f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018914 typval_T *argvars;
18915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018917 rettv->vval.v_number = (varnumber_T)(STRLEN(
18918 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919}
18920
18921/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018922 * "strchars()" function
18923 */
18924 static void
18925f_strchars(argvars, rettv)
18926 typval_T *argvars;
18927 typval_T *rettv;
18928{
18929 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018930 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018931#ifdef FEAT_MBYTE
18932 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018933 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018934#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018935
18936 if (argvars[1].v_type != VAR_UNKNOWN)
18937 skipcc = get_tv_number_chk(&argvars[1], NULL);
18938 if (skipcc < 0 || skipcc > 1)
18939 EMSG(_(e_invarg));
18940 else
18941 {
18942#ifdef FEAT_MBYTE
18943 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18944 while (*s != NUL)
18945 {
18946 func_mb_ptr2char_adv(&s);
18947 ++len;
18948 }
18949 rettv->vval.v_number = len;
18950#else
18951 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18952#endif
18953 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018954}
18955
18956/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018957 * "strdisplaywidth()" function
18958 */
18959 static void
18960f_strdisplaywidth(argvars, rettv)
18961 typval_T *argvars;
18962 typval_T *rettv;
18963{
18964 char_u *s = get_tv_string(&argvars[0]);
18965 int col = 0;
18966
18967 if (argvars[1].v_type != VAR_UNKNOWN)
18968 col = get_tv_number(&argvars[1]);
18969
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018970 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018971}
18972
18973/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018974 * "strwidth()" function
18975 */
18976 static void
18977f_strwidth(argvars, rettv)
18978 typval_T *argvars;
18979 typval_T *rettv;
18980{
18981 char_u *s = get_tv_string(&argvars[0]);
18982
18983 rettv->vval.v_number = (varnumber_T)(
18984#ifdef FEAT_MBYTE
18985 mb_string2cells(s, -1)
18986#else
18987 STRLEN(s)
18988#endif
18989 );
18990}
18991
18992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993 * "strpart()" function
18994 */
18995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018996f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018997 typval_T *argvars;
18998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999{
19000 char_u *p;
19001 int n;
19002 int len;
19003 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019004 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019006 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 slen = (int)STRLEN(p);
19008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019009 n = get_tv_number_chk(&argvars[1], &error);
19010 if (error)
19011 len = 0;
19012 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019013 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 else
19015 len = slen - n; /* default len: all bytes that are available. */
19016
19017 /*
19018 * Only return the overlap between the specified part and the actual
19019 * string.
19020 */
19021 if (n < 0)
19022 {
19023 len += n;
19024 n = 0;
19025 }
19026 else if (n > slen)
19027 n = slen;
19028 if (len < 0)
19029 len = 0;
19030 else if (n + len > slen)
19031 len = slen - n;
19032
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019033 rettv->v_type = VAR_STRING;
19034 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035}
19036
19037/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019038 * "strridx()" function
19039 */
19040 static void
19041f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019042 typval_T *argvars;
19043 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019044{
19045 char_u buf[NUMBUFLEN];
19046 char_u *needle;
19047 char_u *haystack;
19048 char_u *rest;
19049 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019050 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019052 needle = get_tv_string_chk(&argvars[1]);
19053 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019054
19055 rettv->vval.v_number = -1;
19056 if (needle == NULL || haystack == NULL)
19057 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019058
19059 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019060 if (argvars[2].v_type != VAR_UNKNOWN)
19061 {
19062 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019063 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019064 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019065 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019066 }
19067 else
19068 end_idx = haystack_len;
19069
Bram Moolenaar0d660222005-01-07 21:51:51 +000019070 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019071 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019072 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019073 lastmatch = haystack + end_idx;
19074 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019075 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019076 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019077 for (rest = haystack; *rest != '\0'; ++rest)
19078 {
19079 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019080 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019081 break;
19082 lastmatch = rest;
19083 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019084 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019085
19086 if (lastmatch == NULL)
19087 rettv->vval.v_number = -1;
19088 else
19089 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19090}
19091
19092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 * "strtrans()" function
19094 */
19095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019096f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019097 typval_T *argvars;
19098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019100 rettv->v_type = VAR_STRING;
19101 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102}
19103
19104/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019105 * "submatch()" function
19106 */
19107 static void
19108f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019109 typval_T *argvars;
19110 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019111{
Bram Moolenaar41571762014-04-02 19:00:58 +020019112 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019113 int no;
19114 int retList = 0;
19115
19116 no = (int)get_tv_number_chk(&argvars[0], &error);
19117 if (error)
19118 return;
19119 error = FALSE;
19120 if (argvars[1].v_type != VAR_UNKNOWN)
19121 retList = get_tv_number_chk(&argvars[1], &error);
19122 if (error)
19123 return;
19124
19125 if (retList == 0)
19126 {
19127 rettv->v_type = VAR_STRING;
19128 rettv->vval.v_string = reg_submatch(no);
19129 }
19130 else
19131 {
19132 rettv->v_type = VAR_LIST;
19133 rettv->vval.v_list = reg_submatch_list(no);
19134 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019135}
19136
19137/*
19138 * "substitute()" function
19139 */
19140 static void
19141f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019142 typval_T *argvars;
19143 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019144{
19145 char_u patbuf[NUMBUFLEN];
19146 char_u subbuf[NUMBUFLEN];
19147 char_u flagsbuf[NUMBUFLEN];
19148
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019149 char_u *str = get_tv_string_chk(&argvars[0]);
19150 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19151 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19152 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19153
Bram Moolenaar0d660222005-01-07 21:51:51 +000019154 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019155 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19156 rettv->vval.v_string = NULL;
19157 else
19158 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019159}
19160
19161/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019162 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019165f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019166 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168{
19169 int id = 0;
19170#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019171 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172 long col;
19173 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019174 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019176 lnum = get_tv_lnum(argvars); /* -1 on type error */
19177 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19178 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019180 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019181 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019182 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183#endif
19184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019185 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186}
19187
19188/*
19189 * "synIDattr(id, what [, mode])" function
19190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019192f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019193 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195{
19196 char_u *p = NULL;
19197#ifdef FEAT_SYN_HL
19198 int id;
19199 char_u *what;
19200 char_u *mode;
19201 char_u modebuf[NUMBUFLEN];
19202 int modec;
19203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019204 id = get_tv_number(&argvars[0]);
19205 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019206 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019208 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019210 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211 modec = 0; /* replace invalid with current */
19212 }
19213 else
19214 {
19215#ifdef FEAT_GUI
19216 if (gui.in_use)
19217 modec = 'g';
19218 else
19219#endif
19220 if (t_colors > 1)
19221 modec = 'c';
19222 else
19223 modec = 't';
19224 }
19225
19226
19227 switch (TOLOWER_ASC(what[0]))
19228 {
19229 case 'b':
19230 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19231 p = highlight_color(id, what, modec);
19232 else /* bold */
19233 p = highlight_has_attr(id, HL_BOLD, modec);
19234 break;
19235
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019236 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237 p = highlight_color(id, what, modec);
19238 break;
19239
19240 case 'i':
19241 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19242 p = highlight_has_attr(id, HL_INVERSE, modec);
19243 else /* italic */
19244 p = highlight_has_attr(id, HL_ITALIC, modec);
19245 break;
19246
19247 case 'n': /* name */
19248 p = get_highlight_name(NULL, id - 1);
19249 break;
19250
19251 case 'r': /* reverse */
19252 p = highlight_has_attr(id, HL_INVERSE, modec);
19253 break;
19254
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019255 case 's':
19256 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19257 p = highlight_color(id, what, modec);
19258 else /* standout */
19259 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260 break;
19261
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019262 case 'u':
19263 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19264 /* underline */
19265 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19266 else
19267 /* undercurl */
19268 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269 break;
19270 }
19271
19272 if (p != NULL)
19273 p = vim_strsave(p);
19274#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019275 rettv->v_type = VAR_STRING;
19276 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277}
19278
19279/*
19280 * "synIDtrans(id)" function
19281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019283f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019284 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286{
19287 int id;
19288
19289#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019290 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291
19292 if (id > 0)
19293 id = syn_get_final_id(id);
19294 else
19295#endif
19296 id = 0;
19297
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019298 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299}
19300
19301/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019302 * "synconcealed(lnum, col)" function
19303 */
19304 static void
19305f_synconcealed(argvars, rettv)
19306 typval_T *argvars UNUSED;
19307 typval_T *rettv;
19308{
19309#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19310 long lnum;
19311 long col;
19312 int syntax_flags = 0;
19313 int cchar;
19314 int matchid = 0;
19315 char_u str[NUMBUFLEN];
19316#endif
19317
19318 rettv->v_type = VAR_LIST;
19319 rettv->vval.v_list = NULL;
19320
19321#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19322 lnum = get_tv_lnum(argvars); /* -1 on type error */
19323 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19324
19325 vim_memset(str, NUL, sizeof(str));
19326
19327 if (rettv_list_alloc(rettv) != FAIL)
19328 {
19329 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19330 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19331 && curwin->w_p_cole > 0)
19332 {
19333 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19334 syntax_flags = get_syntax_info(&matchid);
19335
19336 /* get the conceal character */
19337 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19338 {
19339 cchar = syn_get_sub_char();
19340 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19341 cchar = lcs_conceal;
19342 if (cchar != NUL)
19343 {
19344# ifdef FEAT_MBYTE
19345 if (has_mbyte)
19346 (*mb_char2bytes)(cchar, str);
19347 else
19348# endif
19349 str[0] = cchar;
19350 }
19351 }
19352 }
19353
19354 list_append_number(rettv->vval.v_list,
19355 (syntax_flags & HL_CONCEAL) != 0);
19356 /* -1 to auto-determine strlen */
19357 list_append_string(rettv->vval.v_list, str, -1);
19358 list_append_number(rettv->vval.v_list, matchid);
19359 }
19360#endif
19361}
19362
19363/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019364 * "synstack(lnum, col)" function
19365 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019366 static void
19367f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019368 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019369 typval_T *rettv;
19370{
19371#ifdef FEAT_SYN_HL
19372 long lnum;
19373 long col;
19374 int i;
19375 int id;
19376#endif
19377
19378 rettv->v_type = VAR_LIST;
19379 rettv->vval.v_list = NULL;
19380
19381#ifdef FEAT_SYN_HL
19382 lnum = get_tv_lnum(argvars); /* -1 on type error */
19383 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19384
19385 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019386 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019387 && rettv_list_alloc(rettv) != FAIL)
19388 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019389 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019390 for (i = 0; ; ++i)
19391 {
19392 id = syn_get_stack_item(i);
19393 if (id < 0)
19394 break;
19395 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19396 break;
19397 }
19398 }
19399#endif
19400}
19401
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019403get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019404 typval_T *argvars;
19405 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019406 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019408 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019410 char_u *infile = NULL;
19411 char_u buf[NUMBUFLEN];
19412 int err = FALSE;
19413 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019414 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019415 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019417 rettv->v_type = VAR_STRING;
19418 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019419 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019420 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019421
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019422 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019423 {
19424 /*
19425 * Write the string to a temp file, to be used for input of the shell
19426 * command.
19427 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019428 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019429 {
19430 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019431 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019432 }
19433
19434 fd = mch_fopen((char *)infile, WRITEBIN);
19435 if (fd == NULL)
19436 {
19437 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019438 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019439 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019440 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019441 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019442 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19443 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019444 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019445 else
19446 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019447 size_t len;
19448
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019449 p = get_tv_string_buf_chk(&argvars[1], buf);
19450 if (p == NULL)
19451 {
19452 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019453 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019454 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019455 len = STRLEN(p);
19456 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019457 err = TRUE;
19458 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019459 if (fclose(fd) != 0)
19460 err = TRUE;
19461 if (err)
19462 {
19463 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019464 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019465 }
19466 }
19467
Bram Moolenaar52a72462014-08-29 15:53:52 +020019468 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19469 * echoes typeahead, that messes up the display. */
19470 if (!msg_silent)
19471 flags += SHELL_COOKED;
19472
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019473 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019475 int len;
19476 listitem_T *li;
19477 char_u *s = NULL;
19478 char_u *start;
19479 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019480 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481
Bram Moolenaar52a72462014-08-29 15:53:52 +020019482 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019483 if (res == NULL)
19484 goto errret;
19485
19486 list = list_alloc();
19487 if (list == NULL)
19488 goto errret;
19489
19490 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019492 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019493 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019494 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019495 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019496
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019497 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019498 if (s == NULL)
19499 goto errret;
19500
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019501 for (p = s; start < end; ++p, ++start)
19502 *p = *start == NUL ? NL : *start;
19503 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019504
19505 li = listitem_alloc();
19506 if (li == NULL)
19507 {
19508 vim_free(s);
19509 goto errret;
19510 }
19511 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019512 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019513 li->li_tv.vval.v_string = s;
19514 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019516
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019517 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019518 rettv->v_type = VAR_LIST;
19519 rettv->vval.v_list = list;
19520 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019522 else
19523 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019524 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019525#ifdef USE_CR
19526 /* translate <CR> into <NL> */
19527 if (res != NULL)
19528 {
19529 char_u *s;
19530
19531 for (s = res; *s; ++s)
19532 {
19533 if (*s == CAR)
19534 *s = NL;
19535 }
19536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537#else
19538# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019539 /* translate <CR><NL> into <NL> */
19540 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019542 char_u *s, *d;
19543
19544 d = res;
19545 for (s = res; *s; ++s)
19546 {
19547 if (s[0] == CAR && s[1] == NL)
19548 ++s;
19549 *d++ = *s;
19550 }
19551 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553# endif
19554#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019555 rettv->vval.v_string = res;
19556 res = NULL;
19557 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019558
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019559errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019560 if (infile != NULL)
19561 {
19562 mch_remove(infile);
19563 vim_free(infile);
19564 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019565 if (res != NULL)
19566 vim_free(res);
19567 if (list != NULL)
19568 list_free(list, TRUE);
19569}
19570
19571/*
19572 * "system()" function
19573 */
19574 static void
19575f_system(argvars, rettv)
19576 typval_T *argvars;
19577 typval_T *rettv;
19578{
19579 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19580}
19581
19582/*
19583 * "systemlist()" function
19584 */
19585 static void
19586f_systemlist(argvars, rettv)
19587 typval_T *argvars;
19588 typval_T *rettv;
19589{
19590 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591}
19592
19593/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019594 * "tabpagebuflist()" function
19595 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019596 static void
19597f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019598 typval_T *argvars UNUSED;
19599 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019600{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019601#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019602 tabpage_T *tp;
19603 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019604
19605 if (argvars[0].v_type == VAR_UNKNOWN)
19606 wp = firstwin;
19607 else
19608 {
19609 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19610 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019611 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019612 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019613 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019614 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019615 for (; wp != NULL; wp = wp->w_next)
19616 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019617 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019618 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019619 }
19620#endif
19621}
19622
19623
19624/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019625 * "tabpagenr()" function
19626 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019627 static void
19628f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019629 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019630 typval_T *rettv;
19631{
19632 int nr = 1;
19633#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019634 char_u *arg;
19635
19636 if (argvars[0].v_type != VAR_UNKNOWN)
19637 {
19638 arg = get_tv_string_chk(&argvars[0]);
19639 nr = 0;
19640 if (arg != NULL)
19641 {
19642 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019643 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019644 else
19645 EMSG2(_(e_invexpr2), arg);
19646 }
19647 }
19648 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019649 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019650#endif
19651 rettv->vval.v_number = nr;
19652}
19653
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019654
19655#ifdef FEAT_WINDOWS
19656static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19657
19658/*
19659 * Common code for tabpagewinnr() and winnr().
19660 */
19661 static int
19662get_winnr(tp, argvar)
19663 tabpage_T *tp;
19664 typval_T *argvar;
19665{
19666 win_T *twin;
19667 int nr = 1;
19668 win_T *wp;
19669 char_u *arg;
19670
19671 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19672 if (argvar->v_type != VAR_UNKNOWN)
19673 {
19674 arg = get_tv_string_chk(argvar);
19675 if (arg == NULL)
19676 nr = 0; /* type error; errmsg already given */
19677 else if (STRCMP(arg, "$") == 0)
19678 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19679 else if (STRCMP(arg, "#") == 0)
19680 {
19681 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19682 if (twin == NULL)
19683 nr = 0;
19684 }
19685 else
19686 {
19687 EMSG2(_(e_invexpr2), arg);
19688 nr = 0;
19689 }
19690 }
19691
19692 if (nr > 0)
19693 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19694 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019695 {
19696 if (wp == NULL)
19697 {
19698 /* didn't find it in this tabpage */
19699 nr = 0;
19700 break;
19701 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019702 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019703 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019704 return nr;
19705}
19706#endif
19707
19708/*
19709 * "tabpagewinnr()" function
19710 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019711 static void
19712f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019713 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019714 typval_T *rettv;
19715{
19716 int nr = 1;
19717#ifdef FEAT_WINDOWS
19718 tabpage_T *tp;
19719
19720 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19721 if (tp == NULL)
19722 nr = 0;
19723 else
19724 nr = get_winnr(tp, &argvars[1]);
19725#endif
19726 rettv->vval.v_number = nr;
19727}
19728
19729
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019730/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019731 * "tagfiles()" function
19732 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019733 static void
19734f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019735 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019736 typval_T *rettv;
19737{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019738 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019739 tagname_T tn;
19740 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019741
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019742 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019743 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019744 fname = alloc(MAXPATHL);
19745 if (fname == NULL)
19746 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019747
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019748 for (first = TRUE; ; first = FALSE)
19749 if (get_tagfname(&tn, first, fname) == FAIL
19750 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019751 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019752 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019753 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019754}
19755
19756/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019757 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019758 */
19759 static void
19760f_taglist(argvars, rettv)
19761 typval_T *argvars;
19762 typval_T *rettv;
19763{
19764 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019765
19766 tag_pattern = get_tv_string(&argvars[0]);
19767
19768 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019769 if (*tag_pattern == NUL)
19770 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019771
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019772 if (rettv_list_alloc(rettv) == OK)
19773 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019774}
19775
19776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 * "tempname()" function
19778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019780f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019781 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783{
19784 static int x = 'A';
19785
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019786 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019787 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788
19789 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19790 * names. Skip 'I' and 'O', they are used for shell redirection. */
19791 do
19792 {
19793 if (x == 'Z')
19794 x = '0';
19795 else if (x == '9')
19796 x = 'A';
19797 else
19798 {
19799#ifdef EBCDIC
19800 if (x == 'I')
19801 x = 'J';
19802 else if (x == 'R')
19803 x = 'S';
19804 else
19805#endif
19806 ++x;
19807 }
19808 } while (x == 'I' || x == 'O');
19809}
19810
19811/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019812 * "test(list)" function: Just checking the walls...
19813 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019814 static void
19815f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019816 typval_T *argvars UNUSED;
19817 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019818{
19819 /* Used for unit testing. Change the code below to your liking. */
19820#if 0
19821 listitem_T *li;
19822 list_T *l;
19823 char_u *bad, *good;
19824
19825 if (argvars[0].v_type != VAR_LIST)
19826 return;
19827 l = argvars[0].vval.v_list;
19828 if (l == NULL)
19829 return;
19830 li = l->lv_first;
19831 if (li == NULL)
19832 return;
19833 bad = get_tv_string(&li->li_tv);
19834 li = li->li_next;
19835 if (li == NULL)
19836 return;
19837 good = get_tv_string(&li->li_tv);
19838 rettv->vval.v_number = test_edit_score(bad, good);
19839#endif
19840}
19841
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019842#ifdef FEAT_FLOAT
19843/*
19844 * "tan()" function
19845 */
19846 static void
19847f_tan(argvars, rettv)
19848 typval_T *argvars;
19849 typval_T *rettv;
19850{
19851 float_T f;
19852
19853 rettv->v_type = VAR_FLOAT;
19854 if (get_float_arg(argvars, &f) == OK)
19855 rettv->vval.v_float = tan(f);
19856 else
19857 rettv->vval.v_float = 0.0;
19858}
19859
19860/*
19861 * "tanh()" function
19862 */
19863 static void
19864f_tanh(argvars, rettv)
19865 typval_T *argvars;
19866 typval_T *rettv;
19867{
19868 float_T f;
19869
19870 rettv->v_type = VAR_FLOAT;
19871 if (get_float_arg(argvars, &f) == OK)
19872 rettv->vval.v_float = tanh(f);
19873 else
19874 rettv->vval.v_float = 0.0;
19875}
19876#endif
19877
Bram Moolenaard52d9742005-08-21 22:20:28 +000019878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 * "tolower(string)" function
19880 */
19881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019882f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019883 typval_T *argvars;
19884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885{
19886 char_u *p;
19887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019888 p = vim_strsave(get_tv_string(&argvars[0]));
19889 rettv->v_type = VAR_STRING;
19890 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891
19892 if (p != NULL)
19893 while (*p != NUL)
19894 {
19895#ifdef FEAT_MBYTE
19896 int l;
19897
19898 if (enc_utf8)
19899 {
19900 int c, lc;
19901
19902 c = utf_ptr2char(p);
19903 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019904 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905 /* TODO: reallocate string when byte count changes. */
19906 if (utf_char2len(lc) == l)
19907 utf_char2bytes(lc, p);
19908 p += l;
19909 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019910 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911 p += l; /* skip multi-byte character */
19912 else
19913#endif
19914 {
19915 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19916 ++p;
19917 }
19918 }
19919}
19920
19921/*
19922 * "toupper(string)" function
19923 */
19924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019925f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019926 typval_T *argvars;
19927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019929 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019930 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931}
19932
19933/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019934 * "tr(string, fromstr, tostr)" function
19935 */
19936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019937f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019938 typval_T *argvars;
19939 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019940{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019941 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019942 char_u *fromstr;
19943 char_u *tostr;
19944 char_u *p;
19945#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019946 int inlen;
19947 int fromlen;
19948 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019949 int idx;
19950 char_u *cpstr;
19951 int cplen;
19952 int first = TRUE;
19953#endif
19954 char_u buf[NUMBUFLEN];
19955 char_u buf2[NUMBUFLEN];
19956 garray_T ga;
19957
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019958 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019959 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19960 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019961
19962 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019963 rettv->v_type = VAR_STRING;
19964 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019965 if (fromstr == NULL || tostr == NULL)
19966 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019967 ga_init2(&ga, (int)sizeof(char), 80);
19968
19969#ifdef FEAT_MBYTE
19970 if (!has_mbyte)
19971#endif
19972 /* not multi-byte: fromstr and tostr must be the same length */
19973 if (STRLEN(fromstr) != STRLEN(tostr))
19974 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019975#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019976error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019977#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019978 EMSG2(_(e_invarg2), fromstr);
19979 ga_clear(&ga);
19980 return;
19981 }
19982
19983 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019984 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019985 {
19986#ifdef FEAT_MBYTE
19987 if (has_mbyte)
19988 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019989 inlen = (*mb_ptr2len)(in_str);
19990 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019991 cplen = inlen;
19992 idx = 0;
19993 for (p = fromstr; *p != NUL; p += fromlen)
19994 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019995 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019996 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019997 {
19998 for (p = tostr; *p != NUL; p += tolen)
19999 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020000 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020001 if (idx-- == 0)
20002 {
20003 cplen = tolen;
20004 cpstr = p;
20005 break;
20006 }
20007 }
20008 if (*p == NUL) /* tostr is shorter than fromstr */
20009 goto error;
20010 break;
20011 }
20012 ++idx;
20013 }
20014
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020015 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020016 {
20017 /* Check that fromstr and tostr have the same number of
20018 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020019 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020020 first = FALSE;
20021 for (p = tostr; *p != NUL; p += tolen)
20022 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020023 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020024 --idx;
20025 }
20026 if (idx != 0)
20027 goto error;
20028 }
20029
Bram Moolenaarcde88542015-08-11 19:14:00 +020020030 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020031 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020032 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020033
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020034 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020035 }
20036 else
20037#endif
20038 {
20039 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020040 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020041 if (p != NULL)
20042 ga_append(&ga, tostr[p - fromstr]);
20043 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020044 ga_append(&ga, *in_str);
20045 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020046 }
20047 }
20048
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020049 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020050 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020051 ga_append(&ga, NUL);
20052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020053 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020054}
20055
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020056#ifdef FEAT_FLOAT
20057/*
20058 * "trunc({float})" function
20059 */
20060 static void
20061f_trunc(argvars, rettv)
20062 typval_T *argvars;
20063 typval_T *rettv;
20064{
20065 float_T f;
20066
20067 rettv->v_type = VAR_FLOAT;
20068 if (get_float_arg(argvars, &f) == OK)
20069 /* trunc() is not in C90, use floor() or ceil() instead. */
20070 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20071 else
20072 rettv->vval.v_float = 0.0;
20073}
20074#endif
20075
Bram Moolenaar8299df92004-07-10 09:47:34 +000020076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 * "type(expr)" function
20078 */
20079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020080f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020081 typval_T *argvars;
20082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020084 int n;
20085
20086 switch (argvars[0].v_type)
20087 {
20088 case VAR_NUMBER: n = 0; break;
20089 case VAR_STRING: n = 1; break;
20090 case VAR_FUNC: n = 2; break;
20091 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020092 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020093#ifdef FEAT_FLOAT
20094 case VAR_FLOAT: n = 5; break;
20095#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020096 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
20097 }
20098 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099}
20100
20101/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020102 * "undofile(name)" function
20103 */
20104 static void
20105f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010020106 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020107 typval_T *rettv;
20108{
20109 rettv->v_type = VAR_STRING;
20110#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020111 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020112 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020113
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020114 if (*fname == NUL)
20115 {
20116 /* If there is no file name there will be no undo file. */
20117 rettv->vval.v_string = NULL;
20118 }
20119 else
20120 {
20121 char_u *ffname = FullName_save(fname, FALSE);
20122
20123 if (ffname != NULL)
20124 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20125 vim_free(ffname);
20126 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020127 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020128#else
20129 rettv->vval.v_string = NULL;
20130#endif
20131}
20132
20133/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020134 * "undotree()" function
20135 */
20136 static void
20137f_undotree(argvars, rettv)
20138 typval_T *argvars UNUSED;
20139 typval_T *rettv;
20140{
20141 if (rettv_dict_alloc(rettv) == OK)
20142 {
20143 dict_T *dict = rettv->vval.v_dict;
20144 list_T *list;
20145
Bram Moolenaar730cde92010-06-27 05:18:54 +020020146 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020147 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020148 dict_add_nr_str(dict, "save_last",
20149 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020150 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20151 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020152 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020153
20154 list = list_alloc();
20155 if (list != NULL)
20156 {
20157 u_eval_tree(curbuf->b_u_oldhead, list);
20158 dict_add_list(dict, "entries", list);
20159 }
20160 }
20161}
20162
20163/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020164 * "values(dict)" function
20165 */
20166 static void
20167f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020168 typval_T *argvars;
20169 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020170{
20171 dict_list(argvars, rettv, 1);
20172}
20173
20174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175 * "virtcol(string)" function
20176 */
20177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020178f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020179 typval_T *argvars;
20180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181{
20182 colnr_T vcol = 0;
20183 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020184 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020186 fp = var2fpos(&argvars[0], FALSE, &fnum);
20187 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20188 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 {
20190 getvvcol(curwin, fp, NULL, NULL, &vcol);
20191 ++vcol;
20192 }
20193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020194 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195}
20196
20197/*
20198 * "visualmode()" function
20199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020201f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020202 typval_T *argvars UNUSED;
20203 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205 char_u str[2];
20206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020207 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 str[0] = curbuf->b_visual_mode_eval;
20209 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020210 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211
20212 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020213 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215}
20216
20217/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020218 * "wildmenumode()" function
20219 */
20220 static void
20221f_wildmenumode(argvars, rettv)
20222 typval_T *argvars UNUSED;
20223 typval_T *rettv UNUSED;
20224{
20225#ifdef FEAT_WILDMENU
20226 if (wild_menu_showing)
20227 rettv->vval.v_number = 1;
20228#endif
20229}
20230
20231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232 * "winbufnr(nr)" function
20233 */
20234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020235f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020236 typval_T *argvars;
20237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238{
20239 win_T *wp;
20240
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020241 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020243 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020245 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246}
20247
20248/*
20249 * "wincol()" function
20250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020252f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020253 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255{
20256 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020257 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258}
20259
20260/*
20261 * "winheight(nr)" function
20262 */
20263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020264f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020265 typval_T *argvars;
20266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267{
20268 win_T *wp;
20269
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020270 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020272 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020274 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275}
20276
20277/*
20278 * "winline()" function
20279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020281f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020282 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284{
20285 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020286 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287}
20288
20289/*
20290 * "winnr()" function
20291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020293f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020294 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296{
20297 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020298
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020300 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020302 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303}
20304
20305/*
20306 * "winrestcmd()" function
20307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020309f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020310 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312{
20313#ifdef FEAT_WINDOWS
20314 win_T *wp;
20315 int winnr = 1;
20316 garray_T ga;
20317 char_u buf[50];
20318
20319 ga_init2(&ga, (int)sizeof(char), 70);
20320 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20321 {
20322 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20323 ga_concat(&ga, buf);
20324# ifdef FEAT_VERTSPLIT
20325 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20326 ga_concat(&ga, buf);
20327# endif
20328 ++winnr;
20329 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020330 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020332 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020334 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020336 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337}
20338
20339/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020340 * "winrestview()" function
20341 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020342 static void
20343f_winrestview(argvars, rettv)
20344 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020345 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020346{
20347 dict_T *dict;
20348
20349 if (argvars[0].v_type != VAR_DICT
20350 || (dict = argvars[0].vval.v_dict) == NULL)
20351 EMSG(_(e_invarg));
20352 else
20353 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020354 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20355 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20356 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20357 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020358#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020359 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20360 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020361#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020362 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20363 {
20364 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20365 curwin->w_set_curswant = FALSE;
20366 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020367
Bram Moolenaar82c25852014-05-28 16:47:16 +020020368 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20369 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020370#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020371 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20372 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020373#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020374 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20375 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20376 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20377 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020378
20379 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020380 win_new_height(curwin, curwin->w_height);
20381# ifdef FEAT_VERTSPLIT
20382 win_new_width(curwin, W_WIDTH(curwin));
20383# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020384 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020385
Bram Moolenaarb851a962014-10-31 15:45:52 +010020386 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020387 curwin->w_topline = 1;
20388 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20389 curwin->w_topline = curbuf->b_ml.ml_line_count;
20390#ifdef FEAT_DIFF
20391 check_topfill(curwin, TRUE);
20392#endif
20393 }
20394}
20395
20396/*
20397 * "winsaveview()" function
20398 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020399 static void
20400f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020401 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020402 typval_T *rettv;
20403{
20404 dict_T *dict;
20405
Bram Moolenaara800b422010-06-27 01:15:55 +020020406 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020407 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020408 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020409
20410 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20411 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20412#ifdef FEAT_VIRTUALEDIT
20413 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20414#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020415 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020416 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20417
20418 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20419#ifdef FEAT_DIFF
20420 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20421#endif
20422 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20423 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20424}
20425
20426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427 * "winwidth(nr)" function
20428 */
20429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020430f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020431 typval_T *argvars;
20432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433{
20434 win_T *wp;
20435
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020436 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020438 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439 else
20440#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020441 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020443 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444#endif
20445}
20446
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020448 * "wordcount()" function
20449 */
20450 static void
20451f_wordcount(argvars, rettv)
20452 typval_T *argvars UNUSED;
20453 typval_T *rettv;
20454{
20455 if (rettv_dict_alloc(rettv) == FAIL)
20456 return;
20457 cursor_pos_info(rettv->vval.v_dict);
20458}
20459
20460/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020461 * Write list of strings to file
20462 */
20463 static int
20464write_list(fd, list, binary)
20465 FILE *fd;
20466 list_T *list;
20467 int binary;
20468{
20469 listitem_T *li;
20470 int c;
20471 int ret = OK;
20472 char_u *s;
20473
20474 for (li = list->lv_first; li != NULL; li = li->li_next)
20475 {
20476 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20477 {
20478 if (*s == '\n')
20479 c = putc(NUL, fd);
20480 else
20481 c = putc(*s, fd);
20482 if (c == EOF)
20483 {
20484 ret = FAIL;
20485 break;
20486 }
20487 }
20488 if (!binary || li->li_next != NULL)
20489 if (putc('\n', fd) == EOF)
20490 {
20491 ret = FAIL;
20492 break;
20493 }
20494 if (ret == FAIL)
20495 {
20496 EMSG(_(e_write));
20497 break;
20498 }
20499 }
20500 return ret;
20501}
20502
20503/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020504 * "writefile()" function
20505 */
20506 static void
20507f_writefile(argvars, rettv)
20508 typval_T *argvars;
20509 typval_T *rettv;
20510{
20511 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020512 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020513 char_u *fname;
20514 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020515 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020516
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020517 if (check_restricted() || check_secure())
20518 return;
20519
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020520 if (argvars[0].v_type != VAR_LIST)
20521 {
20522 EMSG2(_(e_listarg), "writefile()");
20523 return;
20524 }
20525 if (argvars[0].vval.v_list == NULL)
20526 return;
20527
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020528 if (argvars[2].v_type != VAR_UNKNOWN)
20529 {
20530 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20531 binary = TRUE;
20532 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20533 append = TRUE;
20534 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020535
20536 /* Always open the file in binary mode, library functions have a mind of
20537 * their own about CR-LF conversion. */
20538 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020539 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20540 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020541 {
20542 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20543 ret = -1;
20544 }
20545 else
20546 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020547 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20548 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020549 fclose(fd);
20550 }
20551
20552 rettv->vval.v_number = ret;
20553}
20554
20555/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020556 * "xor(expr, expr)" function
20557 */
20558 static void
20559f_xor(argvars, rettv)
20560 typval_T *argvars;
20561 typval_T *rettv;
20562{
20563 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20564 ^ get_tv_number_chk(&argvars[1], NULL);
20565}
20566
20567
20568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020570 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571 */
20572 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020573var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020574 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020575 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020576 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020578 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020580 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581
Bram Moolenaara5525202006-03-02 22:52:09 +000020582 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020583 if (varp->v_type == VAR_LIST)
20584 {
20585 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020586 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020587 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020588 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020589
20590 l = varp->vval.v_list;
20591 if (l == NULL)
20592 return NULL;
20593
20594 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020595 pos.lnum = list_find_nr(l, 0L, &error);
20596 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020597 return NULL; /* invalid line number */
20598
20599 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020600 pos.col = list_find_nr(l, 1L, &error);
20601 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020602 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020603 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020604
20605 /* We accept "$" for the column number: last column. */
20606 li = list_find(l, 1L);
20607 if (li != NULL && li->li_tv.v_type == VAR_STRING
20608 && li->li_tv.vval.v_string != NULL
20609 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20610 pos.col = len + 1;
20611
Bram Moolenaara5525202006-03-02 22:52:09 +000020612 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020613 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020614 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020615 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020616
Bram Moolenaara5525202006-03-02 22:52:09 +000020617#ifdef FEAT_VIRTUALEDIT
20618 /* Get the virtual offset. Defaults to zero. */
20619 pos.coladd = list_find_nr(l, 2L, &error);
20620 if (error)
20621 pos.coladd = 0;
20622#endif
20623
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020624 return &pos;
20625 }
20626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020627 name = get_tv_string_chk(varp);
20628 if (name == NULL)
20629 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020630 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020632 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20633 {
20634 if (VIsual_active)
20635 return &VIsual;
20636 return &curwin->w_cursor;
20637 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020638 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020640 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20642 return NULL;
20643 return pp;
20644 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020645
20646#ifdef FEAT_VIRTUALEDIT
20647 pos.coladd = 0;
20648#endif
20649
Bram Moolenaar477933c2007-07-17 14:32:23 +000020650 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020651 {
20652 pos.col = 0;
20653 if (name[1] == '0') /* "w0": first visible line */
20654 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020655 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020656 pos.lnum = curwin->w_topline;
20657 return &pos;
20658 }
20659 else if (name[1] == '$') /* "w$": last visible line */
20660 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020661 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020662 pos.lnum = curwin->w_botline - 1;
20663 return &pos;
20664 }
20665 }
20666 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020667 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020668 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669 {
20670 pos.lnum = curbuf->b_ml.ml_line_count;
20671 pos.col = 0;
20672 }
20673 else
20674 {
20675 pos.lnum = curwin->w_cursor.lnum;
20676 pos.col = (colnr_T)STRLEN(ml_get_curline());
20677 }
20678 return &pos;
20679 }
20680 return NULL;
20681}
20682
20683/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020684 * Convert list in "arg" into a position and optional file number.
20685 * When "fnump" is NULL there is no file number, only 3 items.
20686 * Note that the column is passed on as-is, the caller may want to decrement
20687 * it to use 1 for the first column.
20688 * Return FAIL when conversion is not possible, doesn't check the position for
20689 * validity.
20690 */
20691 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020692list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020693 typval_T *arg;
20694 pos_T *posp;
20695 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020696 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020697{
20698 list_T *l = arg->vval.v_list;
20699 long i = 0;
20700 long n;
20701
Bram Moolenaar493c1782014-05-28 14:34:46 +020020702 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20703 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020704 if (arg->v_type != VAR_LIST
20705 || l == NULL
20706 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020707 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020708 return FAIL;
20709
20710 if (fnump != NULL)
20711 {
20712 n = list_find_nr(l, i++, NULL); /* fnum */
20713 if (n < 0)
20714 return FAIL;
20715 if (n == 0)
20716 n = curbuf->b_fnum; /* current buffer */
20717 *fnump = n;
20718 }
20719
20720 n = list_find_nr(l, i++, NULL); /* lnum */
20721 if (n < 0)
20722 return FAIL;
20723 posp->lnum = n;
20724
20725 n = list_find_nr(l, i++, NULL); /* col */
20726 if (n < 0)
20727 return FAIL;
20728 posp->col = n;
20729
20730#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020731 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020732 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020733 posp->coladd = 0;
20734 else
20735 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020736#endif
20737
Bram Moolenaar493c1782014-05-28 14:34:46 +020020738 if (curswantp != NULL)
20739 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20740
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020741 return OK;
20742}
20743
20744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 * Get the length of an environment variable name.
20746 * Advance "arg" to the first character after the name.
20747 * Return 0 for error.
20748 */
20749 static int
20750get_env_len(arg)
20751 char_u **arg;
20752{
20753 char_u *p;
20754 int len;
20755
20756 for (p = *arg; vim_isIDc(*p); ++p)
20757 ;
20758 if (p == *arg) /* no name found */
20759 return 0;
20760
20761 len = (int)(p - *arg);
20762 *arg = p;
20763 return len;
20764}
20765
20766/*
20767 * Get the length of the name of a function or internal variable.
20768 * "arg" is advanced to the first non-white character after the name.
20769 * Return 0 if something is wrong.
20770 */
20771 static int
20772get_id_len(arg)
20773 char_u **arg;
20774{
20775 char_u *p;
20776 int len;
20777
20778 /* Find the end of the name. */
20779 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020780 {
20781 if (*p == ':')
20782 {
20783 /* "s:" is start of "s:var", but "n:" is not and can be used in
20784 * slice "[n:]". Also "xx:" is not a namespace. */
20785 len = (int)(p - *arg);
20786 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20787 || len > 1)
20788 break;
20789 }
20790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791 if (p == *arg) /* no name found */
20792 return 0;
20793
20794 len = (int)(p - *arg);
20795 *arg = skipwhite(p);
20796
20797 return len;
20798}
20799
20800/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020801 * Get the length of the name of a variable or function.
20802 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020804 * Return -1 if curly braces expansion failed.
20805 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 * If the name contains 'magic' {}'s, expand them and return the
20807 * expanded name in an allocated string via 'alias' - caller must free.
20808 */
20809 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020810get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 char_u **arg;
20812 char_u **alias;
20813 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020814 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815{
20816 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817 char_u *p;
20818 char_u *expr_start;
20819 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820
20821 *alias = NULL; /* default to no alias */
20822
20823 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20824 && (*arg)[2] == (int)KE_SNR)
20825 {
20826 /* hard coded <SNR>, already translated */
20827 *arg += 3;
20828 return get_id_len(arg) + 3;
20829 }
20830 len = eval_fname_script(*arg);
20831 if (len > 0)
20832 {
20833 /* literal "<SID>", "s:" or "<SNR>" */
20834 *arg += len;
20835 }
20836
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020838 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020840 p = find_name_end(*arg, &expr_start, &expr_end,
20841 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 if (expr_start != NULL)
20843 {
20844 char_u *temp_string;
20845
20846 if (!evaluate)
20847 {
20848 len += (int)(p - *arg);
20849 *arg = skipwhite(p);
20850 return len;
20851 }
20852
20853 /*
20854 * Include any <SID> etc in the expanded string:
20855 * Thus the -len here.
20856 */
20857 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20858 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020859 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 *alias = temp_string;
20861 *arg = skipwhite(p);
20862 return (int)STRLEN(temp_string);
20863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864
20865 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020866 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020867 EMSG2(_(e_invexpr2), *arg);
20868
20869 return len;
20870}
20871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020872/*
20873 * Find the end of a variable or function name, taking care of magic braces.
20874 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20875 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020876 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020877 * Return a pointer to just after the name. Equal to "arg" if there is no
20878 * valid name.
20879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020880 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020881find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882 char_u *arg;
20883 char_u **expr_start;
20884 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020885 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020887 int mb_nest = 0;
20888 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020890 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020892 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020894 *expr_start = NULL;
20895 *expr_end = NULL;
20896 }
20897
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020898 /* Quick check for valid starting character. */
20899 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20900 return arg;
20901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020902 for (p = arg; *p != NUL
20903 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020904 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020905 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020906 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020907 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020908 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020909 if (*p == '\'')
20910 {
20911 /* skip over 'string' to avoid counting [ and ] inside it. */
20912 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20913 ;
20914 if (*p == NUL)
20915 break;
20916 }
20917 else if (*p == '"')
20918 {
20919 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20920 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20921 if (*p == '\\' && p[1] != NUL)
20922 ++p;
20923 if (*p == NUL)
20924 break;
20925 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020926 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20927 {
20928 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020929 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020930 len = (int)(p - arg);
20931 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020932 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020933 break;
20934 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020936 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020938 if (*p == '[')
20939 ++br_nest;
20940 else if (*p == ']')
20941 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020944 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020946 if (*p == '{')
20947 {
20948 mb_nest++;
20949 if (expr_start != NULL && *expr_start == NULL)
20950 *expr_start = p;
20951 }
20952 else if (*p == '}')
20953 {
20954 mb_nest--;
20955 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20956 *expr_end = p;
20957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959 }
20960
20961 return p;
20962}
20963
20964/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020965 * Expands out the 'magic' {}'s in a variable/function name.
20966 * Note that this can call itself recursively, to deal with
20967 * constructs like foo{bar}{baz}{bam}
20968 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20969 * "in_start" ^
20970 * "expr_start" ^
20971 * "expr_end" ^
20972 * "in_end" ^
20973 *
20974 * Returns a new allocated string, which the caller must free.
20975 * Returns NULL for failure.
20976 */
20977 static char_u *
20978make_expanded_name(in_start, expr_start, expr_end, in_end)
20979 char_u *in_start;
20980 char_u *expr_start;
20981 char_u *expr_end;
20982 char_u *in_end;
20983{
20984 char_u c1;
20985 char_u *retval = NULL;
20986 char_u *temp_result;
20987 char_u *nextcmd = NULL;
20988
20989 if (expr_end == NULL || in_end == NULL)
20990 return NULL;
20991 *expr_start = NUL;
20992 *expr_end = NUL;
20993 c1 = *in_end;
20994 *in_end = NUL;
20995
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020996 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020997 if (temp_result != NULL && nextcmd == NULL)
20998 {
20999 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21000 + (in_end - expr_end) + 1));
21001 if (retval != NULL)
21002 {
21003 STRCPY(retval, in_start);
21004 STRCAT(retval, temp_result);
21005 STRCAT(retval, expr_end + 1);
21006 }
21007 }
21008 vim_free(temp_result);
21009
21010 *in_end = c1; /* put char back for error messages */
21011 *expr_start = '{';
21012 *expr_end = '}';
21013
21014 if (retval != NULL)
21015 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021016 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021017 if (expr_start != NULL)
21018 {
21019 /* Further expansion! */
21020 temp_result = make_expanded_name(retval, expr_start,
21021 expr_end, temp_result);
21022 vim_free(retval);
21023 retval = temp_result;
21024 }
21025 }
21026
21027 return retval;
21028}
21029
21030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021032 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 */
21034 static int
21035eval_isnamec(c)
21036 int c;
21037{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021038 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21039}
21040
21041/*
21042 * Return TRUE if character "c" can be used as the first character in a
21043 * variable or function name (excluding '{' and '}').
21044 */
21045 static int
21046eval_isnamec1(c)
21047 int c;
21048{
21049 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050}
21051
21052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053 * Set number v: variable to "val".
21054 */
21055 void
21056set_vim_var_nr(idx, val)
21057 int idx;
21058 long val;
21059{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021060 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061}
21062
21063/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021064 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065 */
21066 long
21067get_vim_var_nr(idx)
21068 int idx;
21069{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021070 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071}
21072
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021073/*
21074 * Get string v: variable value. Uses a static buffer, can only be used once.
21075 */
21076 char_u *
21077get_vim_var_str(idx)
21078 int idx;
21079{
21080 return get_tv_string(&vimvars[idx].vv_tv);
21081}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021082
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021084 * Get List v: variable value. Caller must take care of reference count when
21085 * needed.
21086 */
21087 list_T *
21088get_vim_var_list(idx)
21089 int idx;
21090{
21091 return vimvars[idx].vv_list;
21092}
21093
21094/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021095 * Set v:char to character "c".
21096 */
21097 void
21098set_vim_var_char(c)
21099 int c;
21100{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021101 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021102
21103#ifdef FEAT_MBYTE
21104 if (has_mbyte)
21105 buf[(*mb_char2bytes)(c, buf)] = NUL;
21106 else
21107#endif
21108 {
21109 buf[0] = c;
21110 buf[1] = NUL;
21111 }
21112 set_vim_var_string(VV_CHAR, buf, -1);
21113}
21114
21115/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021116 * Set v:count to "count" and v:count1 to "count1".
21117 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 */
21119 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021120set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 long count;
21122 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021123 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021125 if (set_prevcount)
21126 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021127 vimvars[VV_COUNT].vv_nr = count;
21128 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129}
21130
21131/*
21132 * Set string v: variable to a copy of "val".
21133 */
21134 void
21135set_vim_var_string(idx, val, len)
21136 int idx;
21137 char_u *val;
21138 int len; /* length of "val" to use or -1 (whole string) */
21139{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021140 /* Need to do this (at least) once, since we can't initialize a union.
21141 * Will always be invoked when "v:progname" is set. */
21142 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
21143
Bram Moolenaare9a41262005-01-15 22:18:47 +000021144 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021146 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021148 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021150 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151}
21152
21153/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021154 * Set List v: variable to "val".
21155 */
21156 void
21157set_vim_var_list(idx, val)
21158 int idx;
21159 list_T *val;
21160{
21161 list_unref(vimvars[idx].vv_list);
21162 vimvars[idx].vv_list = val;
21163 if (val != NULL)
21164 ++val->lv_refcount;
21165}
21166
21167/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021168 * Set Dictionary v: variable to "val".
21169 */
21170 void
21171set_vim_var_dict(idx, val)
21172 int idx;
21173 dict_T *val;
21174{
21175 int todo;
21176 hashitem_T *hi;
21177
21178 dict_unref(vimvars[idx].vv_dict);
21179 vimvars[idx].vv_dict = val;
21180 if (val != NULL)
21181 {
21182 ++val->dv_refcount;
21183
21184 /* Set readonly */
21185 todo = (int)val->dv_hashtab.ht_used;
21186 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21187 {
21188 if (HASHITEM_EMPTY(hi))
21189 continue;
21190 --todo;
21191 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21192 }
21193 }
21194}
21195
21196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 * Set v:register if needed.
21198 */
21199 void
21200set_reg_var(c)
21201 int c;
21202{
21203 char_u regname;
21204
21205 if (c == 0 || c == ' ')
21206 regname = '"';
21207 else
21208 regname = c;
21209 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021210 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211 set_vim_var_string(VV_REG, &regname, 1);
21212}
21213
21214/*
21215 * Get or set v:exception. If "oldval" == NULL, return the current value.
21216 * Otherwise, restore the value to "oldval" and return NULL.
21217 * Must always be called in pairs to save and restore v:exception! Does not
21218 * take care of memory allocations.
21219 */
21220 char_u *
21221v_exception(oldval)
21222 char_u *oldval;
21223{
21224 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021225 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226
Bram Moolenaare9a41262005-01-15 22:18:47 +000021227 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228 return NULL;
21229}
21230
21231/*
21232 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21233 * Otherwise, restore the value to "oldval" and return NULL.
21234 * Must always be called in pairs to save and restore v:throwpoint! Does not
21235 * take care of memory allocations.
21236 */
21237 char_u *
21238v_throwpoint(oldval)
21239 char_u *oldval;
21240{
21241 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021242 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243
Bram Moolenaare9a41262005-01-15 22:18:47 +000021244 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245 return NULL;
21246}
21247
21248#if defined(FEAT_AUTOCMD) || defined(PROTO)
21249/*
21250 * Set v:cmdarg.
21251 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21252 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21253 * Must always be called in pairs!
21254 */
21255 char_u *
21256set_cmdarg(eap, oldarg)
21257 exarg_T *eap;
21258 char_u *oldarg;
21259{
21260 char_u *oldval;
21261 char_u *newval;
21262 unsigned len;
21263
Bram Moolenaare9a41262005-01-15 22:18:47 +000021264 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021265 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021267 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021268 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021269 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 }
21271
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021272 if (eap->force_bin == FORCE_BIN)
21273 len = 6;
21274 else if (eap->force_bin == FORCE_NOBIN)
21275 len = 8;
21276 else
21277 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021278
21279 if (eap->read_edit)
21280 len += 7;
21281
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021282 if (eap->force_ff != 0)
21283 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21284# ifdef FEAT_MBYTE
21285 if (eap->force_enc != 0)
21286 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021287 if (eap->bad_char != 0)
21288 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021289# endif
21290
21291 newval = alloc(len + 1);
21292 if (newval == NULL)
21293 return NULL;
21294
21295 if (eap->force_bin == FORCE_BIN)
21296 sprintf((char *)newval, " ++bin");
21297 else if (eap->force_bin == FORCE_NOBIN)
21298 sprintf((char *)newval, " ++nobin");
21299 else
21300 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021301
21302 if (eap->read_edit)
21303 STRCAT(newval, " ++edit");
21304
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021305 if (eap->force_ff != 0)
21306 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21307 eap->cmd + eap->force_ff);
21308# ifdef FEAT_MBYTE
21309 if (eap->force_enc != 0)
21310 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21311 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021312 if (eap->bad_char == BAD_KEEP)
21313 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21314 else if (eap->bad_char == BAD_DROP)
21315 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21316 else if (eap->bad_char != 0)
21317 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021318# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021319 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021320 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321}
21322#endif
21323
21324/*
21325 * Get the value of internal variable "name".
21326 * Return OK or FAIL.
21327 */
21328 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021329get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021330 char_u *name;
21331 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021332 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021333 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021334 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021335 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336{
21337 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021338 typval_T *tv = NULL;
21339 typval_T atv;
21340 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342
21343 /* truncate the name, so that we can use strcmp() */
21344 cc = name[len];
21345 name[len] = NUL;
21346
21347 /*
21348 * Check for "b:changedtick".
21349 */
21350 if (STRCMP(name, "b:changedtick") == 0)
21351 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021352 atv.v_type = VAR_NUMBER;
21353 atv.vval.v_number = curbuf->b_changedtick;
21354 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 }
21356
21357 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 * Check for user-defined variables.
21359 */
21360 else
21361 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021362 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021364 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021365 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021366 if (dip != NULL)
21367 *dip = v;
21368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 }
21370
Bram Moolenaare9a41262005-01-15 22:18:47 +000021371 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021373 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021374 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375 ret = FAIL;
21376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021377 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021378 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379
21380 name[len] = cc;
21381
21382 return ret;
21383}
21384
21385/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021386 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21387 * Also handle function call with Funcref variable: func(expr)
21388 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21389 */
21390 static int
21391handle_subscript(arg, rettv, evaluate, verbose)
21392 char_u **arg;
21393 typval_T *rettv;
21394 int evaluate; /* do more than finding the end */
21395 int verbose; /* give error messages */
21396{
21397 int ret = OK;
21398 dict_T *selfdict = NULL;
21399 char_u *s;
21400 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021401 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021402
21403 while (ret == OK
21404 && (**arg == '['
21405 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021406 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021407 && !vim_iswhite(*(*arg - 1)))
21408 {
21409 if (**arg == '(')
21410 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021411 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021412 if (evaluate)
21413 {
21414 functv = *rettv;
21415 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021416
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021417 /* Invoke the function. Recursive! */
21418 s = functv.vval.v_string;
21419 }
21420 else
21421 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021422 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021423 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21424 &len, evaluate, selfdict);
21425
21426 /* Clear the funcref afterwards, so that deleting it while
21427 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021428 if (evaluate)
21429 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021430
21431 /* Stop the expression evaluation when immediately aborting on
21432 * error, or when an interrupt occurred or an exception was thrown
21433 * but not caught. */
21434 if (aborting())
21435 {
21436 if (ret == OK)
21437 clear_tv(rettv);
21438 ret = FAIL;
21439 }
21440 dict_unref(selfdict);
21441 selfdict = NULL;
21442 }
21443 else /* **arg == '[' || **arg == '.' */
21444 {
21445 dict_unref(selfdict);
21446 if (rettv->v_type == VAR_DICT)
21447 {
21448 selfdict = rettv->vval.v_dict;
21449 if (selfdict != NULL)
21450 ++selfdict->dv_refcount;
21451 }
21452 else
21453 selfdict = NULL;
21454 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21455 {
21456 clear_tv(rettv);
21457 ret = FAIL;
21458 }
21459 }
21460 }
21461 dict_unref(selfdict);
21462 return ret;
21463}
21464
21465/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021466 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021467 * value).
21468 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021469 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021470alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021471{
Bram Moolenaar33570922005-01-25 22:26:29 +000021472 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021473}
21474
21475/*
21476 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 * The string "s" must have been allocated, it is consumed.
21478 * Return NULL for out of memory, the variable otherwise.
21479 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021480 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021481alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 char_u *s;
21483{
Bram Moolenaar33570922005-01-25 22:26:29 +000021484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021486 rettv = alloc_tv();
21487 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021489 rettv->v_type = VAR_STRING;
21490 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491 }
21492 else
21493 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021494 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495}
21496
21497/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021498 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021500 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021501free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021502 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503{
21504 if (varp != NULL)
21505 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021506 switch (varp->v_type)
21507 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021508 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021509 func_unref(varp->vval.v_string);
21510 /*FALLTHROUGH*/
21511 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021512 vim_free(varp->vval.v_string);
21513 break;
21514 case VAR_LIST:
21515 list_unref(varp->vval.v_list);
21516 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021517 case VAR_DICT:
21518 dict_unref(varp->vval.v_dict);
21519 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021520 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021521#ifdef FEAT_FLOAT
21522 case VAR_FLOAT:
21523#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021524 case VAR_UNKNOWN:
21525 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021526 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021527 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021528 break;
21529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530 vim_free(varp);
21531 }
21532}
21533
21534/*
21535 * Free the memory for a variable value and set the value to NULL or 0.
21536 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021537 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021538clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021539 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540{
21541 if (varp != NULL)
21542 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021543 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021545 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021546 func_unref(varp->vval.v_string);
21547 /*FALLTHROUGH*/
21548 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021549 vim_free(varp->vval.v_string);
21550 varp->vval.v_string = NULL;
21551 break;
21552 case VAR_LIST:
21553 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021554 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021555 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021556 case VAR_DICT:
21557 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021558 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021559 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021560 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021561 varp->vval.v_number = 0;
21562 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021563#ifdef FEAT_FLOAT
21564 case VAR_FLOAT:
21565 varp->vval.v_float = 0.0;
21566 break;
21567#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021568 case VAR_UNKNOWN:
21569 break;
21570 default:
21571 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021573 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574 }
21575}
21576
21577/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021578 * Set the value of a variable to NULL without freeing items.
21579 */
21580 static void
21581init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021582 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021583{
21584 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021585 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021586}
21587
21588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 * Get the number value of a variable.
21590 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021591 * For incompatible types, return 0.
21592 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21593 * caller of incompatible types: it sets *denote to TRUE if "denote"
21594 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 */
21596 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021597get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021598 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021600 int error = FALSE;
21601
21602 return get_tv_number_chk(varp, &error); /* return 0L on error */
21603}
21604
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021605 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021606get_tv_number_chk(varp, denote)
21607 typval_T *varp;
21608 int *denote;
21609{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021610 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021612 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021614 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021615 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021616#ifdef FEAT_FLOAT
21617 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021618 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021619 break;
21620#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021621 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021622 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021623 break;
21624 case VAR_STRING:
21625 if (varp->vval.v_string != NULL)
21626 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021627 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021628 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021629 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021630 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021631 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021632 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021633 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021634 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021635 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021636 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021637 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021639 if (denote == NULL) /* useful for values that must be unsigned */
21640 n = -1;
21641 else
21642 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021643 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644}
21645
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021646#ifdef FEAT_FLOAT
21647 static float_T
21648get_tv_float(varp)
21649 typval_T *varp;
21650{
21651 switch (varp->v_type)
21652 {
21653 case VAR_NUMBER:
21654 return (float_T)(varp->vval.v_number);
21655#ifdef FEAT_FLOAT
21656 case VAR_FLOAT:
21657 return varp->vval.v_float;
21658 break;
21659#endif
21660 case VAR_FUNC:
21661 EMSG(_("E891: Using a Funcref as a Float"));
21662 break;
21663 case VAR_STRING:
21664 EMSG(_("E892: Using a String as a Float"));
21665 break;
21666 case VAR_LIST:
21667 EMSG(_("E893: Using a List as a Float"));
21668 break;
21669 case VAR_DICT:
21670 EMSG(_("E894: Using a Dictionary as a Float"));
21671 break;
21672 default:
21673 EMSG2(_(e_intern2), "get_tv_float()");
21674 break;
21675 }
21676 return 0;
21677}
21678#endif
21679
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021681 * Get the lnum from the first argument.
21682 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021683 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684 */
21685 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021686get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021687 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021688{
Bram Moolenaar33570922005-01-25 22:26:29 +000021689 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690 linenr_T lnum;
21691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021692 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 if (lnum == 0) /* no valid number, try using line() */
21694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021695 rettv.v_type = VAR_NUMBER;
21696 f_line(argvars, &rettv);
21697 lnum = rettv.vval.v_number;
21698 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021699 }
21700 return lnum;
21701}
21702
21703/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021704 * Get the lnum from the first argument.
21705 * Also accepts "$", then "buf" is used.
21706 * Returns 0 on error.
21707 */
21708 static linenr_T
21709get_tv_lnum_buf(argvars, buf)
21710 typval_T *argvars;
21711 buf_T *buf;
21712{
21713 if (argvars[0].v_type == VAR_STRING
21714 && argvars[0].vval.v_string != NULL
21715 && argvars[0].vval.v_string[0] == '$'
21716 && buf != NULL)
21717 return buf->b_ml.ml_line_count;
21718 return get_tv_number_chk(&argvars[0], NULL);
21719}
21720
21721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722 * Get the string value of a variable.
21723 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021724 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21725 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726 * If the String variable has never been set, return an empty string.
21727 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021728 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21729 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 */
21731 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021732get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021733 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734{
21735 static char_u mybuf[NUMBUFLEN];
21736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021737 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738}
21739
21740 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021741get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021742 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021743 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021745 char_u *res = get_tv_string_buf_chk(varp, buf);
21746
21747 return res != NULL ? res : (char_u *)"";
21748}
21749
Bram Moolenaar7d647822014-04-05 21:28:56 +020021750/*
21751 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21752 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021753 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021754get_tv_string_chk(varp)
21755 typval_T *varp;
21756{
21757 static char_u mybuf[NUMBUFLEN];
21758
21759 return get_tv_string_buf_chk(varp, mybuf);
21760}
21761
21762 static char_u *
21763get_tv_string_buf_chk(varp, buf)
21764 typval_T *varp;
21765 char_u *buf;
21766{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021767 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021769 case VAR_NUMBER:
21770 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21771 return buf;
21772 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021773 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021774 break;
21775 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021776 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021777 break;
21778 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021779 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021780 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021781#ifdef FEAT_FLOAT
21782 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021783 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021784 break;
21785#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021786 case VAR_STRING:
21787 if (varp->vval.v_string != NULL)
21788 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021789 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021790 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021791 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021792 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021794 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795}
21796
21797/*
21798 * Find variable "name" in the list of variables.
21799 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021800 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021801 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021802 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021804 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021805find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021807 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021808 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021811 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812
Bram Moolenaara7043832005-01-21 11:56:39 +000021813 ht = find_var_ht(name, &varname);
21814 if (htp != NULL)
21815 *htp = ht;
21816 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021818 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819}
21820
21821/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021822 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021823 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021825 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021826find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021827 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021828 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021829 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021830 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021831{
Bram Moolenaar33570922005-01-25 22:26:29 +000021832 hashitem_T *hi;
21833
21834 if (*varname == NUL)
21835 {
21836 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021837 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021838 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021839 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021840 case 'g': return &globvars_var;
21841 case 'v': return &vimvars_var;
21842 case 'b': return &curbuf->b_bufvar;
21843 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021844#ifdef FEAT_WINDOWS
21845 case 't': return &curtab->tp_winvar;
21846#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021847 case 'l': return current_funccal == NULL
21848 ? NULL : &current_funccal->l_vars_var;
21849 case 'a': return current_funccal == NULL
21850 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021851 }
21852 return NULL;
21853 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021854
21855 hi = hash_find(ht, varname);
21856 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021857 {
21858 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021859 * worked find the variable again. Don't auto-load a script if it was
21860 * loaded already, otherwise it would be loaded every time when
21861 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021862 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021863 {
21864 /* Note: script_autoload() may make "hi" invalid. It must either
21865 * be obtained again or not used. */
21866 if (!script_autoload(varname, FALSE) || aborting())
21867 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021868 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021869 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021870 if (HASHITEM_EMPTY(hi))
21871 return NULL;
21872 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021873 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021874}
21875
21876/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021877 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021878 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021879 * Set "varname" to the start of name without ':'.
21880 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021881 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021882find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 char_u *name;
21884 char_u **varname;
21885{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021886 hashitem_T *hi;
21887
Bram Moolenaar73627d02015-08-11 15:46:09 +020021888 if (name[0] == NUL)
21889 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890 if (name[1] != ':')
21891 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021892 /* The name must not start with a colon or #. */
21893 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894 return NULL;
21895 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021896
21897 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021898 hi = hash_find(&compat_hashtab, name);
21899 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021900 return &compat_hashtab;
21901
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021903 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021904 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905 }
21906 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021907 if (*name == 'g') /* global variable */
21908 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021909 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21910 */
21911 if (vim_strchr(name + 2, ':') != NULL
21912 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021913 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021914 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021915 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021917 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021918#ifdef FEAT_WINDOWS
21919 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021920 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021921#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021922 if (*name == 'v') /* v: variable */
21923 return &vimvarht;
21924 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021925 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021926 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021927 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928 if (*name == 's' /* script variable */
21929 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21930 return &SCRIPT_VARS(current_SID);
21931 return NULL;
21932}
21933
21934/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021935 * Get function call environment based on bactrace debug level
21936 */
21937 static funccall_T *
21938get_funccal()
21939{
21940 int i;
21941 funccall_T *funccal;
21942 funccall_T *temp_funccal;
21943
21944 funccal = current_funccal;
21945 if (debug_backtrace_level > 0)
21946 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021947 for (i = 0; i < debug_backtrace_level; i++)
21948 {
21949 temp_funccal = funccal->caller;
21950 if (temp_funccal)
21951 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021952 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021953 /* backtrace level overflow. reset to max */
21954 debug_backtrace_level = i;
21955 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021956 }
21957 return funccal;
21958}
21959
21960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021962 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963 * Returns NULL when it doesn't exist.
21964 */
21965 char_u *
21966get_var_value(name)
21967 char_u *name;
21968{
Bram Moolenaar33570922005-01-25 22:26:29 +000021969 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021971 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972 if (v == NULL)
21973 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021974 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975}
21976
21977/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021978 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979 * sourcing this script and when executing functions defined in the script.
21980 */
21981 void
21982new_script_vars(id)
21983 scid_T id;
21984{
Bram Moolenaara7043832005-01-21 11:56:39 +000021985 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021986 hashtab_T *ht;
21987 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021988
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21990 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021991 /* Re-allocating ga_data means that an ht_array pointing to
21992 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021993 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021994 for (i = 1; i <= ga_scripts.ga_len; ++i)
21995 {
21996 ht = &SCRIPT_VARS(i);
21997 if (ht->ht_mask == HT_INIT_SIZE - 1)
21998 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021999 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022000 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022001 }
22002
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 while (ga_scripts.ga_len < id)
22004 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022005 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022006 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022007 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009 }
22010 }
22011}
22012
22013/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022014 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22015 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016 */
22017 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022018init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000022019 dict_T *dict;
22020 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022021 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022{
Bram Moolenaar33570922005-01-25 22:26:29 +000022023 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022024 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022025 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022026 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022027 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022028 dict_var->di_tv.vval.v_dict = dict;
22029 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022030 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022031 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22032 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033}
22034
22035/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022036 * Unreference a dictionary initialized by init_var_dict().
22037 */
22038 void
22039unref_var_dict(dict)
22040 dict_T *dict;
22041{
22042 /* Now the dict needs to be freed if no one else is using it, go back to
22043 * normal reference counting. */
22044 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22045 dict_unref(dict);
22046}
22047
22048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022050 * Frees all allocated variables and the value they contain.
22051 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 */
22053 void
Bram Moolenaara7043832005-01-21 11:56:39 +000022054vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022055 hashtab_T *ht;
22056{
22057 vars_clear_ext(ht, TRUE);
22058}
22059
22060/*
22061 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22062 */
22063 static void
22064vars_clear_ext(ht, free_val)
22065 hashtab_T *ht;
22066 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067{
Bram Moolenaara7043832005-01-21 11:56:39 +000022068 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022069 hashitem_T *hi;
22070 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071
Bram Moolenaar33570922005-01-25 22:26:29 +000022072 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022073 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022074 for (hi = ht->ht_array; todo > 0; ++hi)
22075 {
22076 if (!HASHITEM_EMPTY(hi))
22077 {
22078 --todo;
22079
Bram Moolenaar33570922005-01-25 22:26:29 +000022080 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022081 * ht_array might change then. hash_clear() takes care of it
22082 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022083 v = HI2DI(hi);
22084 if (free_val)
22085 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022086 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022087 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022088 }
22089 }
22090 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022091 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092}
22093
Bram Moolenaara7043832005-01-21 11:56:39 +000022094/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022095 * Delete a variable from hashtab "ht" at item "hi".
22096 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000022099delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000022100 hashtab_T *ht;
22101 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102{
Bram Moolenaar33570922005-01-25 22:26:29 +000022103 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022104
22105 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022106 clear_tv(&di->di_tv);
22107 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108}
22109
22110/*
22111 * List the value of one internal variable.
22112 */
22113 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022114list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000022115 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022117 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022119 char_u *tofree;
22120 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022121 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022122
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022123 current_copyID += COPYID_INC;
22124 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000022125 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022126 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022127 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128}
22129
Bram Moolenaar071d4272004-06-13 20:20:40 +000022130 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022131list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 char_u *prefix;
22133 char_u *name;
22134 int type;
22135 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022136 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137{
Bram Moolenaar31859182007-08-14 20:41:13 +000022138 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22139 msg_start();
22140 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 if (name != NULL) /* "a:" vars don't have a name stored */
22142 msg_puts(name);
22143 msg_putchar(' ');
22144 msg_advance(22);
22145 if (type == VAR_NUMBER)
22146 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022147 else if (type == VAR_FUNC)
22148 msg_putchar('*');
22149 else if (type == VAR_LIST)
22150 {
22151 msg_putchar('[');
22152 if (*string == '[')
22153 ++string;
22154 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022155 else if (type == VAR_DICT)
22156 {
22157 msg_putchar('{');
22158 if (*string == '{')
22159 ++string;
22160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 else
22162 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022163
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022165
22166 if (type == VAR_FUNC)
22167 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022168 if (*first)
22169 {
22170 msg_clr_eos();
22171 *first = FALSE;
22172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173}
22174
22175/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022176 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177 * If the variable already exists, the value is updated.
22178 * Otherwise the variable is created.
22179 */
22180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022181set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022183 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022184 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185{
Bram Moolenaar33570922005-01-25 22:26:29 +000022186 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022188 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022190 ht = find_var_ht(name, &varname);
22191 if (ht == NULL || *varname == NUL)
22192 {
22193 EMSG2(_(e_illvar), name);
22194 return;
22195 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022196 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022197
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022198 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22199 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022200
Bram Moolenaar33570922005-01-25 22:26:29 +000022201 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022203 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022204 if (var_check_ro(v->di_flags, name, FALSE)
22205 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022206 return;
22207 if (v->di_tv.v_type != tv->v_type
22208 && !((v->di_tv.v_type == VAR_STRING
22209 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022210 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022211 || tv->v_type == VAR_NUMBER))
22212#ifdef FEAT_FLOAT
22213 && !((v->di_tv.v_type == VAR_NUMBER
22214 || v->di_tv.v_type == VAR_FLOAT)
22215 && (tv->v_type == VAR_NUMBER
22216 || tv->v_type == VAR_FLOAT))
22217#endif
22218 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022219 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022220 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022221 return;
22222 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022223
22224 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022225 * Handle setting internal v: variables separately where needed to
22226 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022227 */
22228 if (ht == &vimvarht)
22229 {
22230 if (v->di_tv.v_type == VAR_STRING)
22231 {
22232 vim_free(v->di_tv.vval.v_string);
22233 if (copy || tv->v_type != VAR_STRING)
22234 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22235 else
22236 {
22237 /* Take over the string to avoid an extra alloc/free. */
22238 v->di_tv.vval.v_string = tv->vval.v_string;
22239 tv->vval.v_string = NULL;
22240 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022241 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022242 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022243 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022244 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022245 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022246 if (STRCMP(varname, "searchforward") == 0)
22247 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022248#ifdef FEAT_SEARCH_EXTRA
22249 else if (STRCMP(varname, "hlsearch") == 0)
22250 {
22251 no_hlsearch = !v->di_tv.vval.v_number;
22252 redraw_all_later(SOME_VALID);
22253 }
22254#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022255 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022256 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022257 else if (v->di_tv.v_type != tv->v_type)
22258 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022259 }
22260
22261 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 }
22263 else /* add a new variable */
22264 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022265 /* Can't add "v:" variable. */
22266 if (ht == &vimvarht)
22267 {
22268 EMSG2(_(e_illvar), name);
22269 return;
22270 }
22271
Bram Moolenaar92124a32005-06-17 22:03:40 +000022272 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022273 if (!valid_varname(varname))
22274 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022275
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022276 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22277 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022278 if (v == NULL)
22279 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022280 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022281 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022283 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 return;
22285 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022286 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022288
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022289 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022290 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022291 else
22292 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022293 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022294 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022295 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297}
22298
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022299/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022300 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022301 * Also give an error message.
22302 */
22303 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022304var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022305 int flags;
22306 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022307 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022308{
22309 if (flags & DI_FLAGS_RO)
22310 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022311 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022312 return TRUE;
22313 }
22314 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22315 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022316 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022317 return TRUE;
22318 }
22319 return FALSE;
22320}
22321
22322/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022323 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22324 * Also give an error message.
22325 */
22326 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022327var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022328 int flags;
22329 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022330 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022331{
22332 if (flags & DI_FLAGS_FIX)
22333 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022334 EMSG2(_("E795: Cannot delete variable %s"),
22335 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022336 return TRUE;
22337 }
22338 return FALSE;
22339}
22340
22341/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022342 * Check if a funcref is assigned to a valid variable name.
22343 * Return TRUE and give an error if not.
22344 */
22345 static int
22346var_check_func_name(name, new_var)
22347 char_u *name; /* points to start of variable name */
22348 int new_var; /* TRUE when creating the variable */
22349{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022350 /* Allow for w: b: s: and t:. */
22351 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022352 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22353 ? name[2] : name[0]))
22354 {
22355 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22356 name);
22357 return TRUE;
22358 }
22359 /* Don't allow hiding a function. When "v" is not NULL we might be
22360 * assigning another function to the same var, the type is checked
22361 * below. */
22362 if (new_var && function_exists(name))
22363 {
22364 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22365 name);
22366 return TRUE;
22367 }
22368 return FALSE;
22369}
22370
22371/*
22372 * Check if a variable name is valid.
22373 * Return FALSE and give an error if not.
22374 */
22375 static int
22376valid_varname(varname)
22377 char_u *varname;
22378{
22379 char_u *p;
22380
22381 for (p = varname; *p != NUL; ++p)
22382 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22383 && *p != AUTOLOAD_CHAR)
22384 {
22385 EMSG2(_(e_illvar), varname);
22386 return FALSE;
22387 }
22388 return TRUE;
22389}
22390
22391/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022392 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022393 * Also give an error message, using "name" or _("name") when use_gettext is
22394 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022395 */
22396 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022397tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022398 int lock;
22399 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022400 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022401{
22402 if (lock & VAR_LOCKED)
22403 {
22404 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022405 name == NULL ? (char_u *)_("Unknown")
22406 : use_gettext ? (char_u *)_(name)
22407 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022408 return TRUE;
22409 }
22410 if (lock & VAR_FIXED)
22411 {
22412 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022413 name == NULL ? (char_u *)_("Unknown")
22414 : use_gettext ? (char_u *)_(name)
22415 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022416 return TRUE;
22417 }
22418 return FALSE;
22419}
22420
22421/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022422 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022423 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022424 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022425 * It is OK for "from" and "to" to point to the same item. This is used to
22426 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022427 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022428 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022429copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022430 typval_T *from;
22431 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022433 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022434 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022435 switch (from->v_type)
22436 {
22437 case VAR_NUMBER:
22438 to->vval.v_number = from->vval.v_number;
22439 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022440#ifdef FEAT_FLOAT
22441 case VAR_FLOAT:
22442 to->vval.v_float = from->vval.v_float;
22443 break;
22444#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022445 case VAR_STRING:
22446 case VAR_FUNC:
22447 if (from->vval.v_string == NULL)
22448 to->vval.v_string = NULL;
22449 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022450 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022451 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022452 if (from->v_type == VAR_FUNC)
22453 func_ref(to->vval.v_string);
22454 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022455 break;
22456 case VAR_LIST:
22457 if (from->vval.v_list == NULL)
22458 to->vval.v_list = NULL;
22459 else
22460 {
22461 to->vval.v_list = from->vval.v_list;
22462 ++to->vval.v_list->lv_refcount;
22463 }
22464 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022465 case VAR_DICT:
22466 if (from->vval.v_dict == NULL)
22467 to->vval.v_dict = NULL;
22468 else
22469 {
22470 to->vval.v_dict = from->vval.v_dict;
22471 ++to->vval.v_dict->dv_refcount;
22472 }
22473 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022474 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022475 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022476 break;
22477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478}
22479
22480/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022481 * Make a copy of an item.
22482 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022483 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22484 * reference to an already copied list/dict can be used.
22485 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022486 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022487 static int
22488item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022489 typval_T *from;
22490 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022491 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022492 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022493{
22494 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022495 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022496
Bram Moolenaar33570922005-01-25 22:26:29 +000022497 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022498 {
22499 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022500 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022501 }
22502 ++recurse;
22503
22504 switch (from->v_type)
22505 {
22506 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022507#ifdef FEAT_FLOAT
22508 case VAR_FLOAT:
22509#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022510 case VAR_STRING:
22511 case VAR_FUNC:
22512 copy_tv(from, to);
22513 break;
22514 case VAR_LIST:
22515 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022516 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022517 if (from->vval.v_list == NULL)
22518 to->vval.v_list = NULL;
22519 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22520 {
22521 /* use the copy made earlier */
22522 to->vval.v_list = from->vval.v_list->lv_copylist;
22523 ++to->vval.v_list->lv_refcount;
22524 }
22525 else
22526 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22527 if (to->vval.v_list == NULL)
22528 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022529 break;
22530 case VAR_DICT:
22531 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022532 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022533 if (from->vval.v_dict == NULL)
22534 to->vval.v_dict = NULL;
22535 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22536 {
22537 /* use the copy made earlier */
22538 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22539 ++to->vval.v_dict->dv_refcount;
22540 }
22541 else
22542 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22543 if (to->vval.v_dict == NULL)
22544 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022545 break;
22546 default:
22547 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022548 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022549 }
22550 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022551 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022552}
22553
22554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555 * ":echo expr1 ..." print each argument separated with a space, add a
22556 * newline at the end.
22557 * ":echon expr1 ..." print each argument plain.
22558 */
22559 void
22560ex_echo(eap)
22561 exarg_T *eap;
22562{
22563 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022564 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022565 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566 char_u *p;
22567 int needclr = TRUE;
22568 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022569 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570
22571 if (eap->skip)
22572 ++emsg_skip;
22573 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22574 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022575 /* If eval1() causes an error message the text from the command may
22576 * still need to be cleared. E.g., "echo 22,44". */
22577 need_clr_eos = needclr;
22578
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022580 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 {
22582 /*
22583 * Report the invalid expression unless the expression evaluation
22584 * has been cancelled due to an aborting error, an interrupt, or an
22585 * exception.
22586 */
22587 if (!aborting())
22588 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022589 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 break;
22591 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022592 need_clr_eos = FALSE;
22593
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 if (!eap->skip)
22595 {
22596 if (atstart)
22597 {
22598 atstart = FALSE;
22599 /* Call msg_start() after eval1(), evaluating the expression
22600 * may cause a message to appear. */
22601 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022602 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022603 /* Mark the saved text as finishing the line, so that what
22604 * follows is displayed on a new line when scrolling back
22605 * at the more prompt. */
22606 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 }
22610 else if (eap->cmdidx == CMD_echo)
22611 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022612 current_copyID += COPYID_INC;
22613 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022614 if (p != NULL)
22615 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022617 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022619 if (*p != TAB && needclr)
22620 {
22621 /* remove any text still there from the command */
22622 msg_clr_eos();
22623 needclr = FALSE;
22624 }
22625 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 }
22627 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022628 {
22629#ifdef FEAT_MBYTE
22630 if (has_mbyte)
22631 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022632 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022633
22634 (void)msg_outtrans_len_attr(p, i, echo_attr);
22635 p += i - 1;
22636 }
22637 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022639 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022642 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022644 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 arg = skipwhite(arg);
22646 }
22647 eap->nextcmd = check_nextcmd(arg);
22648
22649 if (eap->skip)
22650 --emsg_skip;
22651 else
22652 {
22653 /* remove text that may still be there from the command */
22654 if (needclr)
22655 msg_clr_eos();
22656 if (eap->cmdidx == CMD_echo)
22657 msg_end();
22658 }
22659}
22660
22661/*
22662 * ":echohl {name}".
22663 */
22664 void
22665ex_echohl(eap)
22666 exarg_T *eap;
22667{
22668 int id;
22669
22670 id = syn_name2id(eap->arg);
22671 if (id == 0)
22672 echo_attr = 0;
22673 else
22674 echo_attr = syn_id2attr(id);
22675}
22676
22677/*
22678 * ":execute expr1 ..." execute the result of an expression.
22679 * ":echomsg expr1 ..." Print a message
22680 * ":echoerr expr1 ..." Print an error
22681 * Each gets spaces around each argument and a newline at the end for
22682 * echo commands
22683 */
22684 void
22685ex_execute(eap)
22686 exarg_T *eap;
22687{
22688 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022689 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 int ret = OK;
22691 char_u *p;
22692 garray_T ga;
22693 int len;
22694 int save_did_emsg;
22695
22696 ga_init2(&ga, 1, 80);
22697
22698 if (eap->skip)
22699 ++emsg_skip;
22700 while (*arg != NUL && *arg != '|' && *arg != '\n')
22701 {
22702 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022703 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 {
22705 /*
22706 * Report the invalid expression unless the expression evaluation
22707 * has been cancelled due to an aborting error, an interrupt, or an
22708 * exception.
22709 */
22710 if (!aborting())
22711 EMSG2(_(e_invexpr2), p);
22712 ret = FAIL;
22713 break;
22714 }
22715
22716 if (!eap->skip)
22717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022718 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719 len = (int)STRLEN(p);
22720 if (ga_grow(&ga, len + 2) == FAIL)
22721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022722 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 ret = FAIL;
22724 break;
22725 }
22726 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 ga.ga_len += len;
22730 }
22731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022732 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 arg = skipwhite(arg);
22734 }
22735
22736 if (ret != FAIL && ga.ga_data != NULL)
22737 {
22738 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022741 out_flush();
22742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743 else if (eap->cmdidx == CMD_echoerr)
22744 {
22745 /* We don't want to abort following commands, restore did_emsg. */
22746 save_did_emsg = did_emsg;
22747 EMSG((char_u *)ga.ga_data);
22748 if (!force_abort)
22749 did_emsg = save_did_emsg;
22750 }
22751 else if (eap->cmdidx == CMD_execute)
22752 do_cmdline((char_u *)ga.ga_data,
22753 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22754 }
22755
22756 ga_clear(&ga);
22757
22758 if (eap->skip)
22759 --emsg_skip;
22760
22761 eap->nextcmd = check_nextcmd(arg);
22762}
22763
22764/*
22765 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22766 * "arg" points to the "&" or '+' when called, to "option" when returning.
22767 * Returns NULL when no option name found. Otherwise pointer to the char
22768 * after the option name.
22769 */
22770 static char_u *
22771find_option_end(arg, opt_flags)
22772 char_u **arg;
22773 int *opt_flags;
22774{
22775 char_u *p = *arg;
22776
22777 ++p;
22778 if (*p == 'g' && p[1] == ':')
22779 {
22780 *opt_flags = OPT_GLOBAL;
22781 p += 2;
22782 }
22783 else if (*p == 'l' && p[1] == ':')
22784 {
22785 *opt_flags = OPT_LOCAL;
22786 p += 2;
22787 }
22788 else
22789 *opt_flags = 0;
22790
22791 if (!ASCII_ISALPHA(*p))
22792 return NULL;
22793 *arg = p;
22794
22795 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22796 p += 4; /* termcap option */
22797 else
22798 while (ASCII_ISALPHA(*p))
22799 ++p;
22800 return p;
22801}
22802
22803/*
22804 * ":function"
22805 */
22806 void
22807ex_function(eap)
22808 exarg_T *eap;
22809{
22810 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022811 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 int j;
22813 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022815 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816 char_u *name = NULL;
22817 char_u *p;
22818 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022819 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820 garray_T newargs;
22821 garray_T newlines;
22822 int varargs = FALSE;
22823 int mustend = FALSE;
22824 int flags = 0;
22825 ufunc_T *fp;
22826 int indent;
22827 int nesting;
22828 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022829 dictitem_T *v;
22830 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022831 static int func_nr = 0; /* number for nameless function */
22832 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022833 hashtab_T *ht;
22834 int todo;
22835 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022836 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837
22838 /*
22839 * ":function" without argument: list functions.
22840 */
22841 if (ends_excmd(*eap->arg))
22842 {
22843 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022844 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022845 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022846 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022847 {
22848 if (!HASHITEM_EMPTY(hi))
22849 {
22850 --todo;
22851 fp = HI2UF(hi);
22852 if (!isdigit(*fp->uf_name))
22853 list_func_head(fp, FALSE);
22854 }
22855 }
22856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857 eap->nextcmd = check_nextcmd(eap->arg);
22858 return;
22859 }
22860
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022861 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022862 * ":function /pat": list functions matching pattern.
22863 */
22864 if (*eap->arg == '/')
22865 {
22866 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22867 if (!eap->skip)
22868 {
22869 regmatch_T regmatch;
22870
22871 c = *p;
22872 *p = NUL;
22873 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22874 *p = c;
22875 if (regmatch.regprog != NULL)
22876 {
22877 regmatch.rm_ic = p_ic;
22878
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022879 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022880 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22881 {
22882 if (!HASHITEM_EMPTY(hi))
22883 {
22884 --todo;
22885 fp = HI2UF(hi);
22886 if (!isdigit(*fp->uf_name)
22887 && vim_regexec(&regmatch, fp->uf_name, 0))
22888 list_func_head(fp, FALSE);
22889 }
22890 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022891 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022892 }
22893 }
22894 if (*p == '/')
22895 ++p;
22896 eap->nextcmd = check_nextcmd(p);
22897 return;
22898 }
22899
22900 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022901 * Get the function name. There are these situations:
22902 * func normal function name
22903 * "name" == func, "fudi.fd_dict" == NULL
22904 * dict.func new dictionary entry
22905 * "name" == NULL, "fudi.fd_dict" set,
22906 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22907 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022908 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022909 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22910 * dict.func existing dict entry that's not a Funcref
22911 * "name" == NULL, "fudi.fd_dict" set,
22912 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022913 * s:func script-local function name
22914 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022917 name = trans_function_name(&p, eap->skip, 0, &fudi);
22918 paren = (vim_strchr(p, '(') != NULL);
22919 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 {
22921 /*
22922 * Return on an invalid expression in braces, unless the expression
22923 * evaluation has been cancelled due to an aborting error, an
22924 * interrupt, or an exception.
22925 */
22926 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022927 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022928 if (!eap->skip && fudi.fd_newkey != NULL)
22929 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022930 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 else
22934 eap->skip = TRUE;
22935 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022936
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 /* An error in a function call during evaluation of an expression in magic
22938 * braces should not cause the function not to be defined. */
22939 saved_did_emsg = did_emsg;
22940 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941
22942 /*
22943 * ":function func" with only function name: list function.
22944 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022945 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 {
22947 if (!ends_excmd(*skipwhite(p)))
22948 {
22949 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022950 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 }
22952 eap->nextcmd = check_nextcmd(p);
22953 if (eap->nextcmd != NULL)
22954 *p = NUL;
22955 if (!eap->skip && !got_int)
22956 {
22957 fp = find_func(name);
22958 if (fp != NULL)
22959 {
22960 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022961 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022963 if (FUNCLINE(fp, j) == NULL)
22964 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 msg_putchar('\n');
22966 msg_outnum((long)(j + 1));
22967 if (j < 9)
22968 msg_putchar(' ');
22969 if (j < 99)
22970 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022971 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972 out_flush(); /* show a line at a time */
22973 ui_breakcheck();
22974 }
22975 if (!got_int)
22976 {
22977 msg_putchar('\n');
22978 msg_puts((char_u *)" endfunction");
22979 }
22980 }
22981 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022982 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022984 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 }
22986
22987 /*
22988 * ":function name(arg1, arg2)" Define function.
22989 */
22990 p = skipwhite(p);
22991 if (*p != '(')
22992 {
22993 if (!eap->skip)
22994 {
22995 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022996 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997 }
22998 /* attempt to continue by skipping some text */
22999 if (vim_strchr(p, '(') != NULL)
23000 p = vim_strchr(p, '(');
23001 }
23002 p = skipwhite(p + 1);
23003
23004 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23005 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23006
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023007 if (!eap->skip)
23008 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023009 /* Check the name of the function. Unless it's a dictionary function
23010 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023011 if (name != NULL)
23012 arg = name;
23013 else
23014 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023015 if (arg != NULL && (fudi.fd_di == NULL
23016 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023017 {
23018 if (*arg == K_SPECIAL)
23019 j = 3;
23020 else
23021 j = 0;
23022 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23023 : eval_isnamec(arg[j])))
23024 ++j;
23025 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023026 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023027 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023028 /* Disallow using the g: dict. */
23029 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23030 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023031 }
23032
Bram Moolenaar071d4272004-06-13 20:20:40 +000023033 /*
23034 * Isolate the arguments: "arg1, arg2, ...)"
23035 */
23036 while (*p != ')')
23037 {
23038 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23039 {
23040 varargs = TRUE;
23041 p += 3;
23042 mustend = TRUE;
23043 }
23044 else
23045 {
23046 arg = p;
23047 while (ASCII_ISALNUM(*p) || *p == '_')
23048 ++p;
23049 if (arg == p || isdigit(*arg)
23050 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23051 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23052 {
23053 if (!eap->skip)
23054 EMSG2(_("E125: Illegal argument: %s"), arg);
23055 break;
23056 }
23057 if (ga_grow(&newargs, 1) == FAIL)
23058 goto erret;
23059 c = *p;
23060 *p = NUL;
23061 arg = vim_strsave(arg);
23062 if (arg == NULL)
23063 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023064
23065 /* Check for duplicate argument name. */
23066 for (i = 0; i < newargs.ga_len; ++i)
23067 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23068 {
23069 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023070 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023071 goto erret;
23072 }
23073
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23075 *p = c;
23076 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077 if (*p == ',')
23078 ++p;
23079 else
23080 mustend = TRUE;
23081 }
23082 p = skipwhite(p);
23083 if (mustend && *p != ')')
23084 {
23085 if (!eap->skip)
23086 EMSG2(_(e_invarg2), eap->arg);
23087 break;
23088 }
23089 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023090 if (*p != ')')
23091 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023092 ++p; /* skip the ')' */
23093
Bram Moolenaare9a41262005-01-15 22:18:47 +000023094 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023095 for (;;)
23096 {
23097 p = skipwhite(p);
23098 if (STRNCMP(p, "range", 5) == 0)
23099 {
23100 flags |= FC_RANGE;
23101 p += 5;
23102 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023103 else if (STRNCMP(p, "dict", 4) == 0)
23104 {
23105 flags |= FC_DICT;
23106 p += 4;
23107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 else if (STRNCMP(p, "abort", 5) == 0)
23109 {
23110 flags |= FC_ABORT;
23111 p += 5;
23112 }
23113 else
23114 break;
23115 }
23116
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023117 /* When there is a line break use what follows for the function body.
23118 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23119 if (*p == '\n')
23120 line_arg = p + 1;
23121 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122 EMSG(_(e_trailing));
23123
23124 /*
23125 * Read the body of the function, until ":endfunction" is found.
23126 */
23127 if (KeyTyped)
23128 {
23129 /* Check if the function already exists, don't let the user type the
23130 * whole function before telling him it doesn't work! For a script we
23131 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023132 if (!eap->skip && !eap->forceit)
23133 {
23134 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23135 EMSG(_(e_funcdict));
23136 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023137 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023140 if (!eap->skip && did_emsg)
23141 goto erret;
23142
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143 msg_putchar('\n'); /* don't overwrite the function name */
23144 cmdline_row = msg_row;
23145 }
23146
23147 indent = 2;
23148 nesting = 0;
23149 for (;;)
23150 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023151 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023152 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023153 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023154 saved_wait_return = FALSE;
23155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023157 sourcing_lnum_off = sourcing_lnum;
23158
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023159 if (line_arg != NULL)
23160 {
23161 /* Use eap->arg, split up in parts by line breaks. */
23162 theline = line_arg;
23163 p = vim_strchr(theline, '\n');
23164 if (p == NULL)
23165 line_arg += STRLEN(line_arg);
23166 else
23167 {
23168 *p = NUL;
23169 line_arg = p + 1;
23170 }
23171 }
23172 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173 theline = getcmdline(':', 0L, indent);
23174 else
23175 theline = eap->getline(':', eap->cookie, indent);
23176 if (KeyTyped)
23177 lines_left = Rows - 1;
23178 if (theline == NULL)
23179 {
23180 EMSG(_("E126: Missing :endfunction"));
23181 goto erret;
23182 }
23183
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023184 /* Detect line continuation: sourcing_lnum increased more than one. */
23185 if (sourcing_lnum > sourcing_lnum_off + 1)
23186 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23187 else
23188 sourcing_lnum_off = 0;
23189
Bram Moolenaar071d4272004-06-13 20:20:40 +000023190 if (skip_until != NULL)
23191 {
23192 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23193 * don't check for ":endfunc". */
23194 if (STRCMP(theline, skip_until) == 0)
23195 {
23196 vim_free(skip_until);
23197 skip_until = NULL;
23198 }
23199 }
23200 else
23201 {
23202 /* skip ':' and blanks*/
23203 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23204 ;
23205
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023206 /* Check for "endfunction". */
23207 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023209 if (line_arg == NULL)
23210 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211 break;
23212 }
23213
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023214 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215 * at "end". */
23216 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23217 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023218 else if (STRNCMP(p, "if", 2) == 0
23219 || STRNCMP(p, "wh", 2) == 0
23220 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221 || STRNCMP(p, "try", 3) == 0)
23222 indent += 2;
23223
23224 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023225 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023227 if (*p == '!')
23228 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023229 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023230 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23231 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023232 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023233 ++nesting;
23234 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023235 }
23236 }
23237
23238 /* Check for ":append" or ":insert". */
23239 p = skip_range(p, NULL);
23240 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23241 || (p[0] == 'i'
23242 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23243 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23244 skip_until = vim_strsave((char_u *)".");
23245
23246 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23247 arg = skipwhite(skiptowhite(p));
23248 if (arg[0] == '<' && arg[1] =='<'
23249 && ((p[0] == 'p' && p[1] == 'y'
23250 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23251 || (p[0] == 'p' && p[1] == 'e'
23252 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23253 || (p[0] == 't' && p[1] == 'c'
23254 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023255 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23256 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23258 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023259 || (p[0] == 'm' && p[1] == 'z'
23260 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261 ))
23262 {
23263 /* ":python <<" continues until a dot, like ":append" */
23264 p = skipwhite(arg + 2);
23265 if (*p == NUL)
23266 skip_until = vim_strsave((char_u *)".");
23267 else
23268 skip_until = vim_strsave(p);
23269 }
23270 }
23271
23272 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023273 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023274 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023275 if (line_arg == NULL)
23276 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023278 }
23279
23280 /* Copy the line to newly allocated memory. get_one_sourceline()
23281 * allocates 250 bytes per line, this saves 80% on average. The cost
23282 * is an extra alloc/free. */
23283 p = vim_strsave(theline);
23284 if (p != NULL)
23285 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023286 if (line_arg == NULL)
23287 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023288 theline = p;
23289 }
23290
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023291 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23292
23293 /* Add NULL lines for continuation lines, so that the line count is
23294 * equal to the index in the growarray. */
23295 while (sourcing_lnum_off-- > 0)
23296 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023297
23298 /* Check for end of eap->arg. */
23299 if (line_arg != NULL && *line_arg == NUL)
23300 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301 }
23302
23303 /* Don't define the function when skipping commands or when an error was
23304 * detected. */
23305 if (eap->skip || did_emsg)
23306 goto erret;
23307
23308 /*
23309 * If there are no errors, add the function
23310 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023311 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023312 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023313 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023314 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023315 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023316 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023317 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023318 goto erret;
23319 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023320
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023321 fp = find_func(name);
23322 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023324 if (!eap->forceit)
23325 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023326 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023327 goto erret;
23328 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023329 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023330 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023331 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023332 name);
23333 goto erret;
23334 }
23335 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023336 ga_clear_strings(&(fp->uf_args));
23337 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023338 vim_free(name);
23339 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341 }
23342 else
23343 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023344 char numbuf[20];
23345
23346 fp = NULL;
23347 if (fudi.fd_newkey == NULL && !eap->forceit)
23348 {
23349 EMSG(_(e_funcdict));
23350 goto erret;
23351 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023352 if (fudi.fd_di == NULL)
23353 {
23354 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023355 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023356 goto erret;
23357 }
23358 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023359 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023360 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023361
23362 /* Give the function a sequential number. Can only be used with a
23363 * Funcref! */
23364 vim_free(name);
23365 sprintf(numbuf, "%d", ++func_nr);
23366 name = vim_strsave((char_u *)numbuf);
23367 if (name == NULL)
23368 goto erret;
23369 }
23370
23371 if (fp == NULL)
23372 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023373 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023374 {
23375 int slen, plen;
23376 char_u *scriptname;
23377
23378 /* Check that the autoload name matches the script name. */
23379 j = FAIL;
23380 if (sourcing_name != NULL)
23381 {
23382 scriptname = autoload_name(name);
23383 if (scriptname != NULL)
23384 {
23385 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023386 plen = (int)STRLEN(p);
23387 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023388 if (slen > plen && fnamecmp(p,
23389 sourcing_name + slen - plen) == 0)
23390 j = OK;
23391 vim_free(scriptname);
23392 }
23393 }
23394 if (j == FAIL)
23395 {
23396 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23397 goto erret;
23398 }
23399 }
23400
23401 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 if (fp == NULL)
23403 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023404
23405 if (fudi.fd_dict != NULL)
23406 {
23407 if (fudi.fd_di == NULL)
23408 {
23409 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023410 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023411 if (fudi.fd_di == NULL)
23412 {
23413 vim_free(fp);
23414 goto erret;
23415 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023416 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23417 {
23418 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023419 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023420 goto erret;
23421 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023422 }
23423 else
23424 /* overwrite existing dict entry */
23425 clear_tv(&fudi.fd_di->di_tv);
23426 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023427 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023428 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023429 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023430
23431 /* behave like "dict" was used */
23432 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023433 }
23434
Bram Moolenaar071d4272004-06-13 20:20:40 +000023435 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023436 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023437 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23438 {
23439 vim_free(fp);
23440 goto erret;
23441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023442 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023443 fp->uf_args = newargs;
23444 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023445#ifdef FEAT_PROFILE
23446 fp->uf_tml_count = NULL;
23447 fp->uf_tml_total = NULL;
23448 fp->uf_tml_self = NULL;
23449 fp->uf_profiling = FALSE;
23450 if (prof_def_func())
23451 func_do_profile(fp);
23452#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023453 fp->uf_varargs = varargs;
23454 fp->uf_flags = flags;
23455 fp->uf_calls = 0;
23456 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023457 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458
23459erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023460 ga_clear_strings(&newargs);
23461 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023462ret_free:
23463 vim_free(skip_until);
23464 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023465 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023466 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023467 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023468}
23469
23470/*
23471 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023472 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023474 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023475 * TFN_INT: internal function name OK
23476 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023477 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478 * Advances "pp" to just after the function name (if no error).
23479 */
23480 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023481trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482 char_u **pp;
23483 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023484 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023485 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023487 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023488 char_u *start;
23489 char_u *end;
23490 int lead;
23491 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023493 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023494
23495 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023496 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023498
23499 /* Check for hard coded <SNR>: already translated function ID (from a user
23500 * command). */
23501 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23502 && (*pp)[2] == (int)KE_SNR)
23503 {
23504 *pp += 3;
23505 len = get_id_len(pp) + 3;
23506 return vim_strnsave(start, len);
23507 }
23508
23509 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23510 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023511 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023512 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023513 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023514
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023515 /* Note that TFN_ flags use the same values as GLV_ flags. */
23516 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023517 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023518 if (end == start)
23519 {
23520 if (!skip)
23521 EMSG(_("E129: Function name required"));
23522 goto theend;
23523 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023524 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023525 {
23526 /*
23527 * Report an invalid expression in braces, unless the expression
23528 * evaluation has been cancelled due to an aborting error, an
23529 * interrupt, or an exception.
23530 */
23531 if (!aborting())
23532 {
23533 if (end != NULL)
23534 EMSG2(_(e_invarg2), start);
23535 }
23536 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023537 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023538 goto theend;
23539 }
23540
23541 if (lv.ll_tv != NULL)
23542 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023543 if (fdp != NULL)
23544 {
23545 fdp->fd_dict = lv.ll_dict;
23546 fdp->fd_newkey = lv.ll_newkey;
23547 lv.ll_newkey = NULL;
23548 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023549 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023550 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23551 {
23552 name = vim_strsave(lv.ll_tv->vval.v_string);
23553 *pp = end;
23554 }
23555 else
23556 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023557 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23558 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023559 EMSG(_(e_funcref));
23560 else
23561 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023562 name = NULL;
23563 }
23564 goto theend;
23565 }
23566
23567 if (lv.ll_name == NULL)
23568 {
23569 /* Error found, but continue after the function name. */
23570 *pp = end;
23571 goto theend;
23572 }
23573
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023574 /* Check if the name is a Funcref. If so, use the value. */
23575 if (lv.ll_exp_name != NULL)
23576 {
23577 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023578 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023579 if (name == lv.ll_exp_name)
23580 name = NULL;
23581 }
23582 else
23583 {
23584 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023585 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023586 if (name == *pp)
23587 name = NULL;
23588 }
23589 if (name != NULL)
23590 {
23591 name = vim_strsave(name);
23592 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023593 if (STRNCMP(name, "<SNR>", 5) == 0)
23594 {
23595 /* Change "<SNR>" to the byte sequence. */
23596 name[0] = K_SPECIAL;
23597 name[1] = KS_EXTRA;
23598 name[2] = (int)KE_SNR;
23599 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23600 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023601 goto theend;
23602 }
23603
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023604 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023605 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023606 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023607 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23608 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23609 {
23610 /* When there was "s:" already or the name expanded to get a
23611 * leading "s:" then remove it. */
23612 lv.ll_name += 2;
23613 len -= 2;
23614 lead = 2;
23615 }
23616 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023617 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023618 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023619 /* skip over "s:" and "g:" */
23620 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023621 lv.ll_name += 2;
23622 len = (int)(end - lv.ll_name);
23623 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023624
23625 /*
23626 * Copy the function name to allocated memory.
23627 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23628 * Accept <SNR>123_name() outside a script.
23629 */
23630 if (skip)
23631 lead = 0; /* do nothing */
23632 else if (lead > 0)
23633 {
23634 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023635 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23636 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023637 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023638 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023639 if (current_SID <= 0)
23640 {
23641 EMSG(_(e_usingsid));
23642 goto theend;
23643 }
23644 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23645 lead += (int)STRLEN(sid_buf);
23646 }
23647 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023648 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023649 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023650 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023651 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023652 goto theend;
23653 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023654 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023655 {
23656 char_u *cp = vim_strchr(lv.ll_name, ':');
23657
23658 if (cp != NULL && cp < end)
23659 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023660 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023661 goto theend;
23662 }
23663 }
23664
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023665 name = alloc((unsigned)(len + lead + 1));
23666 if (name != NULL)
23667 {
23668 if (lead > 0)
23669 {
23670 name[0] = K_SPECIAL;
23671 name[1] = KS_EXTRA;
23672 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023673 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023674 STRCPY(name + 3, sid_buf);
23675 }
23676 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023677 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023678 }
23679 *pp = end;
23680
23681theend:
23682 clear_lval(&lv);
23683 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684}
23685
23686/*
23687 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23688 * Return 2 if "p" starts with "s:".
23689 * Return 0 otherwise.
23690 */
23691 static int
23692eval_fname_script(p)
23693 char_u *p;
23694{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023695 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23696 * the standard library function. */
23697 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23698 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699 return 5;
23700 if (p[0] == 's' && p[1] == ':')
23701 return 2;
23702 return 0;
23703}
23704
23705/*
23706 * Return TRUE if "p" starts with "<SID>" or "s:".
23707 * Only works if eval_fname_script() returned non-zero for "p"!
23708 */
23709 static int
23710eval_fname_sid(p)
23711 char_u *p;
23712{
23713 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23714}
23715
23716/*
23717 * List the head of the function: "name(arg1, arg2)".
23718 */
23719 static void
23720list_func_head(fp, indent)
23721 ufunc_T *fp;
23722 int indent;
23723{
23724 int j;
23725
23726 msg_start();
23727 if (indent)
23728 MSG_PUTS(" ");
23729 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023730 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023731 {
23732 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023733 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734 }
23735 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023736 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023737 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023738 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023739 {
23740 if (j)
23741 MSG_PUTS(", ");
23742 msg_puts(FUNCARG(fp, j));
23743 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023744 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 {
23746 if (j)
23747 MSG_PUTS(", ");
23748 MSG_PUTS("...");
23749 }
23750 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023751 if (fp->uf_flags & FC_ABORT)
23752 MSG_PUTS(" abort");
23753 if (fp->uf_flags & FC_RANGE)
23754 MSG_PUTS(" range");
23755 if (fp->uf_flags & FC_DICT)
23756 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023757 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023758 if (p_verbose > 0)
23759 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023760}
23761
23762/*
23763 * Find a function by name, return pointer to it in ufuncs.
23764 * Return NULL for unknown function.
23765 */
23766 static ufunc_T *
23767find_func(name)
23768 char_u *name;
23769{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023770 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023771
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023772 hi = hash_find(&func_hashtab, name);
23773 if (!HASHITEM_EMPTY(hi))
23774 return HI2UF(hi);
23775 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023776}
23777
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023778#if defined(EXITFREE) || defined(PROTO)
23779 void
23780free_all_functions()
23781{
23782 hashitem_T *hi;
23783
23784 /* Need to start all over every time, because func_free() may change the
23785 * hash table. */
23786 while (func_hashtab.ht_used > 0)
23787 for (hi = func_hashtab.ht_array; ; ++hi)
23788 if (!HASHITEM_EMPTY(hi))
23789 {
23790 func_free(HI2UF(hi));
23791 break;
23792 }
23793}
23794#endif
23795
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023796 int
23797translated_function_exists(name)
23798 char_u *name;
23799{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023800 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023801 return find_internal_func(name) >= 0;
23802 return find_func(name) != NULL;
23803}
23804
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023805/*
23806 * Return TRUE if a function "name" exists.
23807 */
23808 static int
23809function_exists(name)
23810 char_u *name;
23811{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023812 char_u *nm = name;
23813 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023814 int n = FALSE;
23815
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023816 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23817 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023818 nm = skipwhite(nm);
23819
23820 /* Only accept "funcname", "funcname ", "funcname (..." and
23821 * "funcname(...", not "funcname!...". */
23822 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023823 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023824 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023825 return n;
23826}
23827
Bram Moolenaara1544c02013-05-30 12:35:52 +020023828 char_u *
23829get_expanded_name(name, check)
23830 char_u *name;
23831 int check;
23832{
23833 char_u *nm = name;
23834 char_u *p;
23835
23836 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23837
23838 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023839 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023840 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023841
Bram Moolenaara1544c02013-05-30 12:35:52 +020023842 vim_free(p);
23843 return NULL;
23844}
23845
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023846/*
23847 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023848 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23849 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023850 */
23851 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023852builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023853 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023854 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023855{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023856 char_u *p;
23857
23858 if (!ASCII_ISLOWER(name[0]))
23859 return FALSE;
23860 p = vim_strchr(name, AUTOLOAD_CHAR);
23861 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023862}
23863
Bram Moolenaar05159a02005-02-26 23:04:13 +000023864#if defined(FEAT_PROFILE) || defined(PROTO)
23865/*
23866 * Start profiling function "fp".
23867 */
23868 static void
23869func_do_profile(fp)
23870 ufunc_T *fp;
23871{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023872 int len = fp->uf_lines.ga_len;
23873
23874 if (len == 0)
23875 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023876 fp->uf_tm_count = 0;
23877 profile_zero(&fp->uf_tm_self);
23878 profile_zero(&fp->uf_tm_total);
23879 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023880 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023881 if (fp->uf_tml_total == NULL)
23882 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023883 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023884 if (fp->uf_tml_self == NULL)
23885 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023886 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023887 fp->uf_tml_idx = -1;
23888 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23889 || fp->uf_tml_self == NULL)
23890 return; /* out of memory */
23891
23892 fp->uf_profiling = TRUE;
23893}
23894
23895/*
23896 * Dump the profiling results for all functions in file "fd".
23897 */
23898 void
23899func_dump_profile(fd)
23900 FILE *fd;
23901{
23902 hashitem_T *hi;
23903 int todo;
23904 ufunc_T *fp;
23905 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023906 ufunc_T **sorttab;
23907 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023908
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023909 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023910 if (todo == 0)
23911 return; /* nothing to dump */
23912
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023913 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023914
Bram Moolenaar05159a02005-02-26 23:04:13 +000023915 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23916 {
23917 if (!HASHITEM_EMPTY(hi))
23918 {
23919 --todo;
23920 fp = HI2UF(hi);
23921 if (fp->uf_profiling)
23922 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023923 if (sorttab != NULL)
23924 sorttab[st_len++] = fp;
23925
Bram Moolenaar05159a02005-02-26 23:04:13 +000023926 if (fp->uf_name[0] == K_SPECIAL)
23927 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23928 else
23929 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23930 if (fp->uf_tm_count == 1)
23931 fprintf(fd, "Called 1 time\n");
23932 else
23933 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23934 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23935 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23936 fprintf(fd, "\n");
23937 fprintf(fd, "count total (s) self (s)\n");
23938
23939 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23940 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023941 if (FUNCLINE(fp, i) == NULL)
23942 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023943 prof_func_line(fd, fp->uf_tml_count[i],
23944 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023945 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23946 }
23947 fprintf(fd, "\n");
23948 }
23949 }
23950 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023951
23952 if (sorttab != NULL && st_len > 0)
23953 {
23954 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23955 prof_total_cmp);
23956 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23957 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23958 prof_self_cmp);
23959 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23960 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023961
23962 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023963}
Bram Moolenaar73830342005-02-28 22:48:19 +000023964
23965 static void
23966prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23967 FILE *fd;
23968 ufunc_T **sorttab;
23969 int st_len;
23970 char *title;
23971 int prefer_self; /* when equal print only self time */
23972{
23973 int i;
23974 ufunc_T *fp;
23975
23976 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23977 fprintf(fd, "count total (s) self (s) function\n");
23978 for (i = 0; i < 20 && i < st_len; ++i)
23979 {
23980 fp = sorttab[i];
23981 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23982 prefer_self);
23983 if (fp->uf_name[0] == K_SPECIAL)
23984 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23985 else
23986 fprintf(fd, " %s()\n", fp->uf_name);
23987 }
23988 fprintf(fd, "\n");
23989}
23990
23991/*
23992 * Print the count and times for one function or function line.
23993 */
23994 static void
23995prof_func_line(fd, count, total, self, prefer_self)
23996 FILE *fd;
23997 int count;
23998 proftime_T *total;
23999 proftime_T *self;
24000 int prefer_self; /* when equal print only self time */
24001{
24002 if (count > 0)
24003 {
24004 fprintf(fd, "%5d ", count);
24005 if (prefer_self && profile_equal(total, self))
24006 fprintf(fd, " ");
24007 else
24008 fprintf(fd, "%s ", profile_msg(total));
24009 if (!prefer_self && profile_equal(total, self))
24010 fprintf(fd, " ");
24011 else
24012 fprintf(fd, "%s ", profile_msg(self));
24013 }
24014 else
24015 fprintf(fd, " ");
24016}
24017
24018/*
24019 * Compare function for total time sorting.
24020 */
24021 static int
24022#ifdef __BORLANDC__
24023_RTLENTRYF
24024#endif
24025prof_total_cmp(s1, s2)
24026 const void *s1;
24027 const void *s2;
24028{
24029 ufunc_T *p1, *p2;
24030
24031 p1 = *(ufunc_T **)s1;
24032 p2 = *(ufunc_T **)s2;
24033 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24034}
24035
24036/*
24037 * Compare function for self time sorting.
24038 */
24039 static int
24040#ifdef __BORLANDC__
24041_RTLENTRYF
24042#endif
24043prof_self_cmp(s1, s2)
24044 const void *s1;
24045 const void *s2;
24046{
24047 ufunc_T *p1, *p2;
24048
24049 p1 = *(ufunc_T **)s1;
24050 p2 = *(ufunc_T **)s2;
24051 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24052}
24053
Bram Moolenaar05159a02005-02-26 23:04:13 +000024054#endif
24055
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024056/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024057 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024058 * Return TRUE if a package was loaded.
24059 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024060 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024061script_autoload(name, reload)
24062 char_u *name;
24063 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024064{
24065 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024066 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024067 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024068 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024069
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024070 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024071 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024072 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024073 return FALSE;
24074
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024075 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024076
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024077 /* Find the name in the list of previously loaded package names. Skip
24078 * "autoload/", it's always the same. */
24079 for (i = 0; i < ga_loaded.ga_len; ++i)
24080 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24081 break;
24082 if (!reload && i < ga_loaded.ga_len)
24083 ret = FALSE; /* was loaded already */
24084 else
24085 {
24086 /* Remember the name if it wasn't loaded already. */
24087 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24088 {
24089 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24090 tofree = NULL;
24091 }
24092
24093 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024094 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024095 ret = TRUE;
24096 }
24097
24098 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024099 return ret;
24100}
24101
24102/*
24103 * Return the autoload script name for a function or variable name.
24104 * Returns NULL when out of memory.
24105 */
24106 static char_u *
24107autoload_name(name)
24108 char_u *name;
24109{
24110 char_u *p;
24111 char_u *scriptname;
24112
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024113 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024114 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24115 if (scriptname == NULL)
24116 return FALSE;
24117 STRCPY(scriptname, "autoload/");
24118 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024119 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024120 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024121 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024122 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024123 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024124}
24125
Bram Moolenaar071d4272004-06-13 20:20:40 +000024126#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24127
24128/*
24129 * Function given to ExpandGeneric() to obtain the list of user defined
24130 * function names.
24131 */
24132 char_u *
24133get_user_func_name(xp, idx)
24134 expand_T *xp;
24135 int idx;
24136{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024137 static long_u done;
24138 static hashitem_T *hi;
24139 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140
24141 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024143 done = 0;
24144 hi = func_hashtab.ht_array;
24145 }
24146 if (done < func_hashtab.ht_used)
24147 {
24148 if (done++ > 0)
24149 ++hi;
24150 while (HASHITEM_EMPTY(hi))
24151 ++hi;
24152 fp = HI2UF(hi);
24153
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024154 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024155 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024157 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24158 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024159
24160 cat_func_name(IObuff, fp);
24161 if (xp->xp_context != EXPAND_USER_FUNC)
24162 {
24163 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024164 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165 STRCAT(IObuff, ")");
24166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167 return IObuff;
24168 }
24169 return NULL;
24170}
24171
24172#endif /* FEAT_CMDL_COMPL */
24173
24174/*
24175 * Copy the function name of "fp" to buffer "buf".
24176 * "buf" must be able to hold the function name plus three bytes.
24177 * Takes care of script-local function names.
24178 */
24179 static void
24180cat_func_name(buf, fp)
24181 char_u *buf;
24182 ufunc_T *fp;
24183{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024184 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185 {
24186 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024187 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188 }
24189 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024190 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191}
24192
24193/*
24194 * ":delfunction {name}"
24195 */
24196 void
24197ex_delfunction(eap)
24198 exarg_T *eap;
24199{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024200 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024201 char_u *p;
24202 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024203 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024204
24205 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024206 name = trans_function_name(&p, eap->skip, 0, &fudi);
24207 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024208 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024209 {
24210 if (fudi.fd_dict != NULL && !eap->skip)
24211 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 if (!ends_excmd(*skipwhite(p)))
24215 {
24216 vim_free(name);
24217 EMSG(_(e_trailing));
24218 return;
24219 }
24220 eap->nextcmd = check_nextcmd(p);
24221 if (eap->nextcmd != NULL)
24222 *p = NUL;
24223
24224 if (!eap->skip)
24225 fp = find_func(name);
24226 vim_free(name);
24227
24228 if (!eap->skip)
24229 {
24230 if (fp == NULL)
24231 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024232 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024233 return;
24234 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024235 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236 {
24237 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24238 return;
24239 }
24240
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024241 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024243 /* Delete the dict item that refers to the function, it will
24244 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024245 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024247 else
24248 func_free(fp);
24249 }
24250}
24251
24252/*
24253 * Free a function and remove it from the list of functions.
24254 */
24255 static void
24256func_free(fp)
24257 ufunc_T *fp;
24258{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024259 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024260
24261 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024262 ga_clear_strings(&(fp->uf_args));
24263 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024264#ifdef FEAT_PROFILE
24265 vim_free(fp->uf_tml_count);
24266 vim_free(fp->uf_tml_total);
24267 vim_free(fp->uf_tml_self);
24268#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024269
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024270 /* remove the function from the function hashtable */
24271 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24272 if (HASHITEM_EMPTY(hi))
24273 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024274 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024275 hash_remove(&func_hashtab, hi);
24276
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024277 vim_free(fp);
24278}
24279
24280/*
24281 * Unreference a Function: decrement the reference count and free it when it
24282 * becomes zero. Only for numbered functions.
24283 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024284 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024285func_unref(name)
24286 char_u *name;
24287{
24288 ufunc_T *fp;
24289
24290 if (name != NULL && isdigit(*name))
24291 {
24292 fp = find_func(name);
24293 if (fp == NULL)
24294 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024295 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024296 {
24297 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024298 * when "uf_calls" becomes zero. */
24299 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024300 func_free(fp);
24301 }
24302 }
24303}
24304
24305/*
24306 * Count a reference to a Function.
24307 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024308 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024309func_ref(name)
24310 char_u *name;
24311{
24312 ufunc_T *fp;
24313
24314 if (name != NULL && isdigit(*name))
24315 {
24316 fp = find_func(name);
24317 if (fp == NULL)
24318 EMSG2(_(e_intern2), "func_ref()");
24319 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024320 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321 }
24322}
24323
24324/*
24325 * Call a user function.
24326 */
24327 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024328call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024329 ufunc_T *fp; /* pointer to function */
24330 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024331 typval_T *argvars; /* arguments */
24332 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024333 linenr_T firstline; /* first line of range */
24334 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024335 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024336{
Bram Moolenaar33570922005-01-25 22:26:29 +000024337 char_u *save_sourcing_name;
24338 linenr_T save_sourcing_lnum;
24339 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024340 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024341 int save_did_emsg;
24342 static int depth = 0;
24343 dictitem_T *v;
24344 int fixvar_idx = 0; /* index in fixvar[] */
24345 int i;
24346 int ai;
24347 char_u numbuf[NUMBUFLEN];
24348 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024349 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024350#ifdef FEAT_PROFILE
24351 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024352 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354
24355 /* If depth of calling is getting too high, don't execute the function */
24356 if (depth >= p_mfd)
24357 {
24358 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024359 rettv->v_type = VAR_NUMBER;
24360 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361 return;
24362 }
24363 ++depth;
24364
24365 line_breakcheck(); /* check for CTRL-C hit */
24366
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024367 fc = (funccall_T *)alloc(sizeof(funccall_T));
24368 fc->caller = current_funccal;
24369 current_funccal = fc;
24370 fc->func = fp;
24371 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024372 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024373 fc->linenr = 0;
24374 fc->returned = FALSE;
24375 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024376 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024377 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24378 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379
Bram Moolenaar33570922005-01-25 22:26:29 +000024380 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024381 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024382 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24383 * each argument variable and saves a lot of time.
24384 */
24385 /*
24386 * Init l: variables.
24387 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024388 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024389 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024390 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024391 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24392 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024393 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024394 name = v->di_key;
24395 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024396 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024397 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024398 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024399 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024400 v->di_tv.vval.v_dict = selfdict;
24401 ++selfdict->dv_refcount;
24402 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024403
Bram Moolenaar33570922005-01-25 22:26:29 +000024404 /*
24405 * Init a: variables.
24406 * Set a:0 to "argcount".
24407 * Set a:000 to a list with room for the "..." arguments.
24408 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024409 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024410 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024411 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024412 /* Use "name" to avoid a warning from some compiler that checks the
24413 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024414 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024415 name = v->di_key;
24416 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024417 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024418 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024419 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024420 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024421 v->di_tv.vval.v_list = &fc->l_varlist;
24422 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24423 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24424 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024425
24426 /*
24427 * Set a:firstline to "firstline" and a:lastline to "lastline".
24428 * Set a:name to named arguments.
24429 * Set a:N to the "..." arguments.
24430 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024431 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024432 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024433 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024434 (varnumber_T)lastline);
24435 for (i = 0; i < argcount; ++i)
24436 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024437 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024438 if (ai < 0)
24439 /* named argument a:name */
24440 name = FUNCARG(fp, i);
24441 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024442 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024443 /* "..." argument a:1, a:2, etc. */
24444 sprintf((char *)numbuf, "%d", ai + 1);
24445 name = numbuf;
24446 }
24447 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24448 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024449 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024450 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24451 }
24452 else
24453 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024454 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24455 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024456 if (v == NULL)
24457 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024458 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024459 }
24460 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024461 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024462
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024463 /* Note: the values are copied directly to avoid alloc/free.
24464 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024465 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024466 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024467
24468 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24469 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024470 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24471 fc->l_listitems[ai].li_tv = argvars[i];
24472 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024473 }
24474 }
24475
Bram Moolenaar071d4272004-06-13 20:20:40 +000024476 /* Don't redraw while executing the function. */
24477 ++RedrawingDisabled;
24478 save_sourcing_name = sourcing_name;
24479 save_sourcing_lnum = sourcing_lnum;
24480 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024481 /* need space for function name + ("function " + 3) or "[number]" */
24482 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24483 + STRLEN(fp->uf_name) + 20;
24484 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024485 if (sourcing_name != NULL)
24486 {
24487 if (save_sourcing_name != NULL
24488 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024489 sprintf((char *)sourcing_name, "%s[%d]..",
24490 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024491 else
24492 STRCPY(sourcing_name, "function ");
24493 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24494
24495 if (p_verbose >= 12)
24496 {
24497 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024498 verbose_enter_scroll();
24499
Bram Moolenaar555b2802005-05-19 21:08:39 +000024500 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024501 if (p_verbose >= 14)
24502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024504 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024505 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024506 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024507
24508 msg_puts((char_u *)"(");
24509 for (i = 0; i < argcount; ++i)
24510 {
24511 if (i > 0)
24512 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024513 if (argvars[i].v_type == VAR_NUMBER)
24514 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515 else
24516 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024517 /* Do not want errors such as E724 here. */
24518 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024519 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024520 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024521 if (s != NULL)
24522 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024523 if (vim_strsize(s) > MSG_BUF_CLEN)
24524 {
24525 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24526 s = buf;
24527 }
24528 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024529 vim_free(tofree);
24530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531 }
24532 }
24533 msg_puts((char_u *)")");
24534 }
24535 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024536
24537 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 --no_wait_return;
24539 }
24540 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024541#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024542 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024543 {
24544 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24545 func_do_profile(fp);
24546 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024547 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024548 {
24549 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024550 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024551 profile_zero(&fp->uf_tm_children);
24552 }
24553 script_prof_save(&wait_start);
24554 }
24555#endif
24556
Bram Moolenaar071d4272004-06-13 20:20:40 +000024557 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024558 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024559 save_did_emsg = did_emsg;
24560 did_emsg = FALSE;
24561
24562 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024563 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024564 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24565
24566 --RedrawingDisabled;
24567
24568 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024569 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024571 clear_tv(rettv);
24572 rettv->v_type = VAR_NUMBER;
24573 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574 }
24575
Bram Moolenaar05159a02005-02-26 23:04:13 +000024576#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024577 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024578 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024579 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024580 profile_end(&call_start);
24581 profile_sub_wait(&wait_start, &call_start);
24582 profile_add(&fp->uf_tm_total, &call_start);
24583 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024584 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024585 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024586 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24587 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024588 }
24589 }
24590#endif
24591
Bram Moolenaar071d4272004-06-13 20:20:40 +000024592 /* when being verbose, mention the return value */
24593 if (p_verbose >= 12)
24594 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024595 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024596 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024597
Bram Moolenaar071d4272004-06-13 20:20:40 +000024598 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024599 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024600 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024601 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024602 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024603 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024604 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024605 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024606 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024607 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024608 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024609
Bram Moolenaar555b2802005-05-19 21:08:39 +000024610 /* The value may be very long. Skip the middle part, so that we
24611 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024612 * truncate it at the end. Don't want errors such as E724 here. */
24613 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024614 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024615 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024616 if (s != NULL)
24617 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024618 if (vim_strsize(s) > MSG_BUF_CLEN)
24619 {
24620 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24621 s = buf;
24622 }
24623 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024624 vim_free(tofree);
24625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 }
24627 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024628
24629 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630 --no_wait_return;
24631 }
24632
24633 vim_free(sourcing_name);
24634 sourcing_name = save_sourcing_name;
24635 sourcing_lnum = save_sourcing_lnum;
24636 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024637#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024638 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024639 script_prof_restore(&wait_start);
24640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024641
24642 if (p_verbose >= 12 && sourcing_name != NULL)
24643 {
24644 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024645 verbose_enter_scroll();
24646
Bram Moolenaar555b2802005-05-19 21:08:39 +000024647 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024648 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024649
24650 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024651 --no_wait_return;
24652 }
24653
24654 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024655 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024656 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024657
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024658 /* If the a:000 list and the l: and a: dicts are not referenced we can
24659 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024660 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24661 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24662 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24663 {
24664 free_funccal(fc, FALSE);
24665 }
24666 else
24667 {
24668 hashitem_T *hi;
24669 listitem_T *li;
24670 int todo;
24671
24672 /* "fc" is still in use. This can happen when returning "a:000" or
24673 * assigning "l:" to a global variable.
24674 * Link "fc" in the list for garbage collection later. */
24675 fc->caller = previous_funccal;
24676 previous_funccal = fc;
24677
24678 /* Make a copy of the a: variables, since we didn't do that above. */
24679 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24680 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24681 {
24682 if (!HASHITEM_EMPTY(hi))
24683 {
24684 --todo;
24685 v = HI2DI(hi);
24686 copy_tv(&v->di_tv, &v->di_tv);
24687 }
24688 }
24689
24690 /* Make a copy of the a:000 items, since we didn't do that above. */
24691 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24692 copy_tv(&li->li_tv, &li->li_tv);
24693 }
24694}
24695
24696/*
24697 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024698 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024699 */
24700 static int
24701can_free_funccal(fc, copyID)
24702 funccall_T *fc;
24703 int copyID;
24704{
24705 return (fc->l_varlist.lv_copyID != copyID
24706 && fc->l_vars.dv_copyID != copyID
24707 && fc->l_avars.dv_copyID != copyID);
24708}
24709
24710/*
24711 * Free "fc" and what it contains.
24712 */
24713 static void
24714free_funccal(fc, free_val)
24715 funccall_T *fc;
24716 int free_val; /* a: vars were allocated */
24717{
24718 listitem_T *li;
24719
24720 /* The a: variables typevals may not have been allocated, only free the
24721 * allocated variables. */
24722 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24723
24724 /* free all l: variables */
24725 vars_clear(&fc->l_vars.dv_hashtab);
24726
24727 /* Free the a:000 variables if they were allocated. */
24728 if (free_val)
24729 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24730 clear_tv(&li->li_tv);
24731
24732 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024733}
24734
24735/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024736 * Add a number variable "name" to dict "dp" with value "nr".
24737 */
24738 static void
24739add_nr_var(dp, v, name, nr)
24740 dict_T *dp;
24741 dictitem_T *v;
24742 char *name;
24743 varnumber_T nr;
24744{
24745 STRCPY(v->di_key, name);
24746 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24747 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24748 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024749 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024750 v->di_tv.vval.v_number = nr;
24751}
24752
24753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024754 * ":return [expr]"
24755 */
24756 void
24757ex_return(eap)
24758 exarg_T *eap;
24759{
24760 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024761 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762 int returning = FALSE;
24763
24764 if (current_funccal == NULL)
24765 {
24766 EMSG(_("E133: :return not inside a function"));
24767 return;
24768 }
24769
24770 if (eap->skip)
24771 ++emsg_skip;
24772
24773 eap->nextcmd = NULL;
24774 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024775 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024776 {
24777 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024778 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024780 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781 }
24782 /* It's safer to return also on error. */
24783 else if (!eap->skip)
24784 {
24785 /*
24786 * Return unless the expression evaluation has been cancelled due to an
24787 * aborting error, an interrupt, or an exception.
24788 */
24789 if (!aborting())
24790 returning = do_return(eap, FALSE, TRUE, NULL);
24791 }
24792
24793 /* When skipping or the return gets pending, advance to the next command
24794 * in this line (!returning). Otherwise, ignore the rest of the line.
24795 * Following lines will be ignored by get_func_line(). */
24796 if (returning)
24797 eap->nextcmd = NULL;
24798 else if (eap->nextcmd == NULL) /* no argument */
24799 eap->nextcmd = check_nextcmd(arg);
24800
24801 if (eap->skip)
24802 --emsg_skip;
24803}
24804
24805/*
24806 * Return from a function. Possibly makes the return pending. Also called
24807 * for a pending return at the ":endtry" or after returning from an extra
24808 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024809 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024810 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024811 * FALSE when the return gets pending.
24812 */
24813 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024814do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024815 exarg_T *eap;
24816 int reanimate;
24817 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024818 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024819{
24820 int idx;
24821 struct condstack *cstack = eap->cstack;
24822
24823 if (reanimate)
24824 /* Undo the return. */
24825 current_funccal->returned = FALSE;
24826
24827 /*
24828 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24829 * not in its finally clause (which then is to be executed next) is found.
24830 * In this case, make the ":return" pending for execution at the ":endtry".
24831 * Otherwise, return normally.
24832 */
24833 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24834 if (idx >= 0)
24835 {
24836 cstack->cs_pending[idx] = CSTP_RETURN;
24837
24838 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024839 /* A pending return again gets pending. "rettv" points to an
24840 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024842 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024843 else
24844 {
24845 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024846 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024848 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024849
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024850 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851 {
24852 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024853 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024854 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024855 else
24856 EMSG(_(e_outofmem));
24857 }
24858 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024859 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024860
24861 if (reanimate)
24862 {
24863 /* The pending return value could be overwritten by a ":return"
24864 * without argument in a finally clause; reset the default
24865 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024866 current_funccal->rettv->v_type = VAR_NUMBER;
24867 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024868 }
24869 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024870 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871 }
24872 else
24873 {
24874 current_funccal->returned = TRUE;
24875
24876 /* If the return is carried out now, store the return value. For
24877 * a return immediately after reanimation, the value is already
24878 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024879 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024881 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024882 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024883 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024884 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024885 }
24886 }
24887
24888 return idx < 0;
24889}
24890
24891/*
24892 * Free the variable with a pending return value.
24893 */
24894 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024895discard_pending_return(rettv)
24896 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024897{
Bram Moolenaar33570922005-01-25 22:26:29 +000024898 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899}
24900
24901/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024902 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903 * is an allocated string. Used by report_pending() for verbose messages.
24904 */
24905 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024906get_return_cmd(rettv)
24907 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024909 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024910 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024911 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024912
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024913 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024914 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024915 if (s == NULL)
24916 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024917
24918 STRCPY(IObuff, ":return ");
24919 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24920 if (STRLEN(s) + 8 >= IOSIZE)
24921 STRCPY(IObuff + IOSIZE - 4, "...");
24922 vim_free(tofree);
24923 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924}
24925
24926/*
24927 * Get next function line.
24928 * Called by do_cmdline() to get the next line.
24929 * Returns allocated string, or NULL for end of function.
24930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 char_u *
24932get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024933 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024934 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024935 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024936{
Bram Moolenaar33570922005-01-25 22:26:29 +000024937 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024938 ufunc_T *fp = fcp->func;
24939 char_u *retval;
24940 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941
24942 /* If breakpoints have been added/deleted need to check for it. */
24943 if (fcp->dbg_tick != debug_tick)
24944 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024945 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024946 sourcing_lnum);
24947 fcp->dbg_tick = debug_tick;
24948 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024949#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024950 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024951 func_line_end(cookie);
24952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024953
Bram Moolenaar05159a02005-02-26 23:04:13 +000024954 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024955 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24956 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024957 retval = NULL;
24958 else
24959 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024960 /* Skip NULL lines (continuation lines). */
24961 while (fcp->linenr < gap->ga_len
24962 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24963 ++fcp->linenr;
24964 if (fcp->linenr >= gap->ga_len)
24965 retval = NULL;
24966 else
24967 {
24968 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24969 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024970#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024971 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024972 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024973#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024975 }
24976
24977 /* Did we encounter a breakpoint? */
24978 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24979 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024980 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024981 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024982 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024983 sourcing_lnum);
24984 fcp->dbg_tick = debug_tick;
24985 }
24986
24987 return retval;
24988}
24989
Bram Moolenaar05159a02005-02-26 23:04:13 +000024990#if defined(FEAT_PROFILE) || defined(PROTO)
24991/*
24992 * Called when starting to read a function line.
24993 * "sourcing_lnum" must be correct!
24994 * When skipping lines it may not actually be executed, but we won't find out
24995 * until later and we need to store the time now.
24996 */
24997 void
24998func_line_start(cookie)
24999 void *cookie;
25000{
25001 funccall_T *fcp = (funccall_T *)cookie;
25002 ufunc_T *fp = fcp->func;
25003
25004 if (fp->uf_profiling && sourcing_lnum >= 1
25005 && sourcing_lnum <= fp->uf_lines.ga_len)
25006 {
25007 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025008 /* Skip continuation lines. */
25009 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25010 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025011 fp->uf_tml_execed = FALSE;
25012 profile_start(&fp->uf_tml_start);
25013 profile_zero(&fp->uf_tml_children);
25014 profile_get_wait(&fp->uf_tml_wait);
25015 }
25016}
25017
25018/*
25019 * Called when actually executing a function line.
25020 */
25021 void
25022func_line_exec(cookie)
25023 void *cookie;
25024{
25025 funccall_T *fcp = (funccall_T *)cookie;
25026 ufunc_T *fp = fcp->func;
25027
25028 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25029 fp->uf_tml_execed = TRUE;
25030}
25031
25032/*
25033 * Called when done with a function line.
25034 */
25035 void
25036func_line_end(cookie)
25037 void *cookie;
25038{
25039 funccall_T *fcp = (funccall_T *)cookie;
25040 ufunc_T *fp = fcp->func;
25041
25042 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25043 {
25044 if (fp->uf_tml_execed)
25045 {
25046 ++fp->uf_tml_count[fp->uf_tml_idx];
25047 profile_end(&fp->uf_tml_start);
25048 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025049 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025050 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25051 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025052 }
25053 fp->uf_tml_idx = -1;
25054 }
25055}
25056#endif
25057
Bram Moolenaar071d4272004-06-13 20:20:40 +000025058/*
25059 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025060 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025061 */
25062 int
25063func_has_ended(cookie)
25064 void *cookie;
25065{
Bram Moolenaar33570922005-01-25 22:26:29 +000025066 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025067
25068 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25069 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025070 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071 || fcp->returned);
25072}
25073
25074/*
25075 * return TRUE if cookie indicates a function which "abort"s on errors.
25076 */
25077 int
25078func_has_abort(cookie)
25079 void *cookie;
25080{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025081 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025082}
25083
25084#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25085typedef enum
25086{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025087 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25088 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25089 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025090} var_flavour_T;
25091
25092static var_flavour_T var_flavour __ARGS((char_u *varname));
25093
25094 static var_flavour_T
25095var_flavour(varname)
25096 char_u *varname;
25097{
25098 char_u *p = varname;
25099
25100 if (ASCII_ISUPPER(*p))
25101 {
25102 while (*(++p))
25103 if (ASCII_ISLOWER(*p))
25104 return VAR_FLAVOUR_SESSION;
25105 return VAR_FLAVOUR_VIMINFO;
25106 }
25107 else
25108 return VAR_FLAVOUR_DEFAULT;
25109}
25110#endif
25111
25112#if defined(FEAT_VIMINFO) || defined(PROTO)
25113/*
25114 * Restore global vars that start with a capital from the viminfo file
25115 */
25116 int
25117read_viminfo_varlist(virp, writing)
25118 vir_T *virp;
25119 int writing;
25120{
25121 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025122 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025123 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025124 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025125
25126 if (!writing && (find_viminfo_parameter('!') != NULL))
25127 {
25128 tab = vim_strchr(virp->vir_line + 1, '\t');
25129 if (tab != NULL)
25130 {
25131 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025132 switch (*tab)
25133 {
25134 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025135#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025136 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025137#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025138 case 'D': type = VAR_DICT; break;
25139 case 'L': type = VAR_LIST; break;
25140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025141
25142 tab = vim_strchr(tab, '\t');
25143 if (tab != NULL)
25144 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025145 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025146 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025147 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025148 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025149#ifdef FEAT_FLOAT
25150 else if (type == VAR_FLOAT)
25151 (void)string2float(tab + 1, &tv.vval.v_float);
25152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025153 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025154 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025155 if (type == VAR_DICT || type == VAR_LIST)
25156 {
25157 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25158
25159 if (etv == NULL)
25160 /* Failed to parse back the dict or list, use it as a
25161 * string. */
25162 tv.v_type = VAR_STRING;
25163 else
25164 {
25165 vim_free(tv.vval.v_string);
25166 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025167 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025168 }
25169 }
25170
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025171 /* when in a function use global variables */
25172 save_funccal = current_funccal;
25173 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025174 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025175 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025176
25177 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025178 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025179 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181 }
25182 }
25183 }
25184
25185 return viminfo_readline(virp);
25186}
25187
25188/*
25189 * Write global vars that start with a capital to the viminfo file
25190 */
25191 void
25192write_viminfo_varlist(fp)
25193 FILE *fp;
25194{
Bram Moolenaar33570922005-01-25 22:26:29 +000025195 hashitem_T *hi;
25196 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025197 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025198 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025199 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025200 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025201 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025202
25203 if (find_viminfo_parameter('!') == NULL)
25204 return;
25205
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025206 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025207
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025208 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025209 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025210 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025211 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025213 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025214 this_var = HI2DI(hi);
25215 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025216 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025217 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025218 {
25219 case VAR_STRING: s = "STR"; break;
25220 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025221#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025222 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025223#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025224 case VAR_DICT: s = "DIC"; break;
25225 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000025226 default: continue;
25227 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025228 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025229 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025230 if (p != NULL)
25231 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025232 vim_free(tofree);
25233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025234 }
25235 }
25236}
25237#endif
25238
25239#if defined(FEAT_SESSION) || defined(PROTO)
25240 int
25241store_session_globals(fd)
25242 FILE *fd;
25243{
Bram Moolenaar33570922005-01-25 22:26:29 +000025244 hashitem_T *hi;
25245 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025246 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025247 char_u *p, *t;
25248
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025249 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025250 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025251 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025252 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025253 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025254 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025255 this_var = HI2DI(hi);
25256 if ((this_var->di_tv.v_type == VAR_NUMBER
25257 || this_var->di_tv.v_type == VAR_STRING)
25258 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025259 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025260 /* Escape special characters with a backslash. Turn a LF and
25261 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025262 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025263 (char_u *)"\\\"\n\r");
25264 if (p == NULL) /* out of memory */
25265 break;
25266 for (t = p; *t != NUL; ++t)
25267 if (*t == '\n')
25268 *t = 'n';
25269 else if (*t == '\r')
25270 *t = 'r';
25271 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025272 this_var->di_key,
25273 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25274 : ' ',
25275 p,
25276 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25277 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025278 || put_eol(fd) == FAIL)
25279 {
25280 vim_free(p);
25281 return FAIL;
25282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025283 vim_free(p);
25284 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025285#ifdef FEAT_FLOAT
25286 else if (this_var->di_tv.v_type == VAR_FLOAT
25287 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25288 {
25289 float_T f = this_var->di_tv.vval.v_float;
25290 int sign = ' ';
25291
25292 if (f < 0)
25293 {
25294 f = -f;
25295 sign = '-';
25296 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025297 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025298 this_var->di_key, sign, f) < 0)
25299 || put_eol(fd) == FAIL)
25300 return FAIL;
25301 }
25302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025303 }
25304 }
25305 return OK;
25306}
25307#endif
25308
Bram Moolenaar661b1822005-07-28 22:36:45 +000025309/*
25310 * Display script name where an item was last set.
25311 * Should only be invoked when 'verbose' is non-zero.
25312 */
25313 void
25314last_set_msg(scriptID)
25315 scid_T scriptID;
25316{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025317 char_u *p;
25318
Bram Moolenaar661b1822005-07-28 22:36:45 +000025319 if (scriptID != 0)
25320 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025321 p = home_replace_save(NULL, get_scriptname(scriptID));
25322 if (p != NULL)
25323 {
25324 verbose_enter();
25325 MSG_PUTS(_("\n\tLast set from "));
25326 MSG_PUTS(p);
25327 vim_free(p);
25328 verbose_leave();
25329 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025330 }
25331}
25332
Bram Moolenaard812df62008-11-09 12:46:09 +000025333/*
25334 * List v:oldfiles in a nice way.
25335 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025336 void
25337ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025338 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025339{
25340 list_T *l = vimvars[VV_OLDFILES].vv_list;
25341 listitem_T *li;
25342 int nr = 0;
25343
25344 if (l == NULL)
25345 msg((char_u *)_("No old files"));
25346 else
25347 {
25348 msg_start();
25349 msg_scroll = TRUE;
25350 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25351 {
25352 msg_outnum((long)++nr);
25353 MSG_PUTS(": ");
25354 msg_outtrans(get_tv_string(&li->li_tv));
25355 msg_putchar('\n');
25356 out_flush(); /* output one line at a time */
25357 ui_breakcheck();
25358 }
25359 /* Assume "got_int" was set to truncate the listing. */
25360 got_int = FALSE;
25361
25362#ifdef FEAT_BROWSE_CMD
25363 if (cmdmod.browse)
25364 {
25365 quit_more = FALSE;
25366 nr = prompt_for_number(FALSE);
25367 msg_starthere();
25368 if (nr > 0)
25369 {
25370 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25371 (long)nr);
25372
25373 if (p != NULL)
25374 {
25375 p = expand_env_save(p);
25376 eap->arg = p;
25377 eap->cmdidx = CMD_edit;
25378 cmdmod.browse = FALSE;
25379 do_exedit(eap, NULL);
25380 vim_free(p);
25381 }
25382 }
25383 }
25384#endif
25385 }
25386}
25387
Bram Moolenaar53744302015-07-17 17:38:22 +020025388/* reset v:option_new, v:option_old and v:option_type */
25389 void
25390reset_v_option_vars()
25391{
25392 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25393 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25394 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25395}
25396
25397
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398#endif /* FEAT_EVAL */
25399
Bram Moolenaar071d4272004-06-13 20:20:40 +000025400
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025401#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025402
25403#ifdef WIN3264
25404/*
25405 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25406 */
25407static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25408static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25409static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25410
25411/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025412 * Get the short path (8.3) for the filename in "fnamep".
25413 * Only works for a valid file name.
25414 * When the path gets longer "fnamep" is changed and the allocated buffer
25415 * is put in "bufp".
25416 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25417 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025418 */
25419 static int
25420get_short_pathname(fnamep, bufp, fnamelen)
25421 char_u **fnamep;
25422 char_u **bufp;
25423 int *fnamelen;
25424{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025425 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025426 char_u *newbuf;
25427
25428 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025429 l = GetShortPathName(*fnamep, *fnamep, len);
25430 if (l > len - 1)
25431 {
25432 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025433 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025434 newbuf = vim_strnsave(*fnamep, l);
25435 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025436 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025437
25438 vim_free(*bufp);
25439 *fnamep = *bufp = newbuf;
25440
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025441 /* Really should always succeed, as the buffer is big enough. */
25442 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025443 }
25444
25445 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025446 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447}
25448
25449/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025450 * Get the short path (8.3) for the filename in "fname". The converted
25451 * path is returned in "bufp".
25452 *
25453 * Some of the directories specified in "fname" may not exist. This function
25454 * will shorten the existing directories at the beginning of the path and then
25455 * append the remaining non-existing path.
25456 *
25457 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025458 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025459 * bufp - Pointer to an allocated buffer for the filename.
25460 * fnamelen - Length of the filename pointed to by fname
25461 *
25462 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025463 */
25464 static int
25465shortpath_for_invalid_fname(fname, bufp, fnamelen)
25466 char_u **fname;
25467 char_u **bufp;
25468 int *fnamelen;
25469{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025470 char_u *short_fname, *save_fname, *pbuf_unused;
25471 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025472 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025473 int old_len, len;
25474 int new_len, sfx_len;
25475 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476
25477 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025478 old_len = *fnamelen;
25479 save_fname = vim_strnsave(*fname, old_len);
25480 pbuf_unused = NULL;
25481 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025483 endp = save_fname + old_len - 1; /* Find the end of the copy */
25484 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025485
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025486 /*
25487 * Try shortening the supplied path till it succeeds by removing one
25488 * directory at a time from the tail of the path.
25489 */
25490 len = 0;
25491 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025492 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025493 /* go back one path-separator */
25494 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25495 --endp;
25496 if (endp <= save_fname)
25497 break; /* processed the complete path */
25498
25499 /*
25500 * Replace the path separator with a NUL and try to shorten the
25501 * resulting path.
25502 */
25503 ch = *endp;
25504 *endp = 0;
25505 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025506 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025507 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25508 {
25509 retval = FAIL;
25510 goto theend;
25511 }
25512 *endp = ch; /* preserve the string */
25513
25514 if (len > 0)
25515 break; /* successfully shortened the path */
25516
25517 /* failed to shorten the path. Skip the path separator */
25518 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025519 }
25520
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025521 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025523 /*
25524 * Succeeded in shortening the path. Now concatenate the shortened
25525 * path with the remaining path at the tail.
25526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025527
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025528 /* Compute the length of the new path. */
25529 sfx_len = (int)(save_endp - endp) + 1;
25530 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025531
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025532 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025533 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025534 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025535 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025536 /* There is not enough space in the currently allocated string,
25537 * copy it to a buffer big enough. */
25538 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025539 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025540 {
25541 retval = FAIL;
25542 goto theend;
25543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025544 }
25545 else
25546 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025547 /* Transfer short_fname to the main buffer (it's big enough),
25548 * unless get_short_pathname() did its work in-place. */
25549 *fname = *bufp = save_fname;
25550 if (short_fname != save_fname)
25551 vim_strncpy(save_fname, short_fname, len);
25552 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025554
25555 /* concat the not-shortened part of the path */
25556 vim_strncpy(*fname + len, endp, sfx_len);
25557 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025558 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025559
25560theend:
25561 vim_free(pbuf_unused);
25562 vim_free(save_fname);
25563
25564 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565}
25566
25567/*
25568 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025569 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025570 */
25571 static int
25572shortpath_for_partial(fnamep, bufp, fnamelen)
25573 char_u **fnamep;
25574 char_u **bufp;
25575 int *fnamelen;
25576{
25577 int sepcount, len, tflen;
25578 char_u *p;
25579 char_u *pbuf, *tfname;
25580 int hasTilde;
25581
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025582 /* Count up the path separators from the RHS.. so we know which part
25583 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025584 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025585 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025586 if (vim_ispathsep(*p))
25587 ++sepcount;
25588
25589 /* Need full path first (use expand_env() to remove a "~/") */
25590 hasTilde = (**fnamep == '~');
25591 if (hasTilde)
25592 pbuf = tfname = expand_env_save(*fnamep);
25593 else
25594 pbuf = tfname = FullName_save(*fnamep, FALSE);
25595
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025596 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025597
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025598 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25599 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025600
25601 if (len == 0)
25602 {
25603 /* Don't have a valid filename, so shorten the rest of the
25604 * path if we can. This CAN give us invalid 8.3 filenames, but
25605 * there's not a lot of point in guessing what it might be.
25606 */
25607 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025608 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25609 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025610 }
25611
25612 /* Count the paths backward to find the beginning of the desired string. */
25613 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025614 {
25615#ifdef FEAT_MBYTE
25616 if (has_mbyte)
25617 p -= mb_head_off(tfname, p);
25618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025619 if (vim_ispathsep(*p))
25620 {
25621 if (sepcount == 0 || (hasTilde && sepcount == 1))
25622 break;
25623 else
25624 sepcount --;
25625 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025627 if (hasTilde)
25628 {
25629 --p;
25630 if (p >= tfname)
25631 *p = '~';
25632 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025633 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025634 }
25635 else
25636 ++p;
25637
25638 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25639 vim_free(*bufp);
25640 *fnamelen = (int)STRLEN(p);
25641 *bufp = pbuf;
25642 *fnamep = p;
25643
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025644 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025645}
25646#endif /* WIN3264 */
25647
25648/*
25649 * Adjust a filename, according to a string of modifiers.
25650 * *fnamep must be NUL terminated when called. When returning, the length is
25651 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025652 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025653 * When there is an error, *fnamep is set to NULL.
25654 */
25655 int
25656modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25657 char_u *src; /* string with modifiers */
25658 int *usedlen; /* characters after src that are used */
25659 char_u **fnamep; /* file name so far */
25660 char_u **bufp; /* buffer for allocated file name or NULL */
25661 int *fnamelen; /* length of fnamep */
25662{
25663 int valid = 0;
25664 char_u *tail;
25665 char_u *s, *p, *pbuf;
25666 char_u dirname[MAXPATHL];
25667 int c;
25668 int has_fullname = 0;
25669#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025670 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025671 int has_shortname = 0;
25672#endif
25673
25674repeat:
25675 /* ":p" - full path/file_name */
25676 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25677 {
25678 has_fullname = 1;
25679
25680 valid |= VALID_PATH;
25681 *usedlen += 2;
25682
25683 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25684 if ((*fnamep)[0] == '~'
25685#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25686 && ((*fnamep)[1] == '/'
25687# ifdef BACKSLASH_IN_FILENAME
25688 || (*fnamep)[1] == '\\'
25689# endif
25690 || (*fnamep)[1] == NUL)
25691
25692#endif
25693 )
25694 {
25695 *fnamep = expand_env_save(*fnamep);
25696 vim_free(*bufp); /* free any allocated file name */
25697 *bufp = *fnamep;
25698 if (*fnamep == NULL)
25699 return -1;
25700 }
25701
25702 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025703 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025704 {
25705 if (vim_ispathsep(*p)
25706 && p[1] == '.'
25707 && (p[2] == NUL
25708 || vim_ispathsep(p[2])
25709 || (p[2] == '.'
25710 && (p[3] == NUL || vim_ispathsep(p[3])))))
25711 break;
25712 }
25713
25714 /* FullName_save() is slow, don't use it when not needed. */
25715 if (*p != NUL || !vim_isAbsName(*fnamep))
25716 {
25717 *fnamep = FullName_save(*fnamep, *p != NUL);
25718 vim_free(*bufp); /* free any allocated file name */
25719 *bufp = *fnamep;
25720 if (*fnamep == NULL)
25721 return -1;
25722 }
25723
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025724#ifdef WIN3264
25725# if _WIN32_WINNT >= 0x0500
25726 if (vim_strchr(*fnamep, '~') != NULL)
25727 {
25728 /* Expand 8.3 filename to full path. Needed to make sure the same
25729 * file does not have two different names.
25730 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25731 p = alloc(_MAX_PATH + 1);
25732 if (p != NULL)
25733 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025734 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025735 {
25736 vim_free(*bufp);
25737 *bufp = *fnamep = p;
25738 }
25739 else
25740 vim_free(p);
25741 }
25742 }
25743# endif
25744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025745 /* Append a path separator to a directory. */
25746 if (mch_isdir(*fnamep))
25747 {
25748 /* Make room for one or two extra characters. */
25749 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25750 vim_free(*bufp); /* free any allocated file name */
25751 *bufp = *fnamep;
25752 if (*fnamep == NULL)
25753 return -1;
25754 add_pathsep(*fnamep);
25755 }
25756 }
25757
25758 /* ":." - path relative to the current directory */
25759 /* ":~" - path relative to the home directory */
25760 /* ":8" - shortname path - postponed till after */
25761 while (src[*usedlen] == ':'
25762 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25763 {
25764 *usedlen += 2;
25765 if (c == '8')
25766 {
25767#ifdef WIN3264
25768 has_shortname = 1; /* Postpone this. */
25769#endif
25770 continue;
25771 }
25772 pbuf = NULL;
25773 /* Need full path first (use expand_env() to remove a "~/") */
25774 if (!has_fullname)
25775 {
25776 if (c == '.' && **fnamep == '~')
25777 p = pbuf = expand_env_save(*fnamep);
25778 else
25779 p = pbuf = FullName_save(*fnamep, FALSE);
25780 }
25781 else
25782 p = *fnamep;
25783
25784 has_fullname = 0;
25785
25786 if (p != NULL)
25787 {
25788 if (c == '.')
25789 {
25790 mch_dirname(dirname, MAXPATHL);
25791 s = shorten_fname(p, dirname);
25792 if (s != NULL)
25793 {
25794 *fnamep = s;
25795 if (pbuf != NULL)
25796 {
25797 vim_free(*bufp); /* free any allocated file name */
25798 *bufp = pbuf;
25799 pbuf = NULL;
25800 }
25801 }
25802 }
25803 else
25804 {
25805 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25806 /* Only replace it when it starts with '~' */
25807 if (*dirname == '~')
25808 {
25809 s = vim_strsave(dirname);
25810 if (s != NULL)
25811 {
25812 *fnamep = s;
25813 vim_free(*bufp);
25814 *bufp = s;
25815 }
25816 }
25817 }
25818 vim_free(pbuf);
25819 }
25820 }
25821
25822 tail = gettail(*fnamep);
25823 *fnamelen = (int)STRLEN(*fnamep);
25824
25825 /* ":h" - head, remove "/file_name", can be repeated */
25826 /* Don't remove the first "/" or "c:\" */
25827 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25828 {
25829 valid |= VALID_HEAD;
25830 *usedlen += 2;
25831 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025832 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025833 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834 *fnamelen = (int)(tail - *fnamep);
25835#ifdef VMS
25836 if (*fnamelen > 0)
25837 *fnamelen += 1; /* the path separator is part of the path */
25838#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025839 if (*fnamelen == 0)
25840 {
25841 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25842 p = vim_strsave((char_u *)".");
25843 if (p == NULL)
25844 return -1;
25845 vim_free(*bufp);
25846 *bufp = *fnamep = tail = p;
25847 *fnamelen = 1;
25848 }
25849 else
25850 {
25851 while (tail > s && !after_pathsep(s, tail))
25852 mb_ptr_back(*fnamep, tail);
25853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025854 }
25855
25856 /* ":8" - shortname */
25857 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25858 {
25859 *usedlen += 2;
25860#ifdef WIN3264
25861 has_shortname = 1;
25862#endif
25863 }
25864
25865#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025866 /*
25867 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025868 */
25869 if (has_shortname)
25870 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025871 /* Copy the string if it is shortened by :h and when it wasn't copied
25872 * yet, because we are going to change it in place. Avoids changing
25873 * the buffer name for "%:8". */
25874 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025875 {
25876 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025877 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025878 return -1;
25879 vim_free(*bufp);
25880 *bufp = *fnamep = p;
25881 }
25882
25883 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025884 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025885 if (!has_fullname && !vim_isAbsName(*fnamep))
25886 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025887 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025888 return -1;
25889 }
25890 else
25891 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025892 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025893
Bram Moolenaardc935552011-08-17 15:23:23 +020025894 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025895 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025896 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025897 return -1;
25898
25899 if (l == 0)
25900 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025901 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025902 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025903 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025904 return -1;
25905 }
25906 *fnamelen = l;
25907 }
25908 }
25909#endif /* WIN3264 */
25910
25911 /* ":t" - tail, just the basename */
25912 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25913 {
25914 *usedlen += 2;
25915 *fnamelen -= (int)(tail - *fnamep);
25916 *fnamep = tail;
25917 }
25918
25919 /* ":e" - extension, can be repeated */
25920 /* ":r" - root, without extension, can be repeated */
25921 while (src[*usedlen] == ':'
25922 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25923 {
25924 /* find a '.' in the tail:
25925 * - for second :e: before the current fname
25926 * - otherwise: The last '.'
25927 */
25928 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25929 s = *fnamep - 2;
25930 else
25931 s = *fnamep + *fnamelen - 1;
25932 for ( ; s > tail; --s)
25933 if (s[0] == '.')
25934 break;
25935 if (src[*usedlen + 1] == 'e') /* :e */
25936 {
25937 if (s > tail)
25938 {
25939 *fnamelen += (int)(*fnamep - (s + 1));
25940 *fnamep = s + 1;
25941#ifdef VMS
25942 /* cut version from the extension */
25943 s = *fnamep + *fnamelen - 1;
25944 for ( ; s > *fnamep; --s)
25945 if (s[0] == ';')
25946 break;
25947 if (s > *fnamep)
25948 *fnamelen = s - *fnamep;
25949#endif
25950 }
25951 else if (*fnamep <= tail)
25952 *fnamelen = 0;
25953 }
25954 else /* :r */
25955 {
25956 if (s > tail) /* remove one extension */
25957 *fnamelen = (int)(s - *fnamep);
25958 }
25959 *usedlen += 2;
25960 }
25961
25962 /* ":s?pat?foo?" - substitute */
25963 /* ":gs?pat?foo?" - global substitute */
25964 if (src[*usedlen] == ':'
25965 && (src[*usedlen + 1] == 's'
25966 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25967 {
25968 char_u *str;
25969 char_u *pat;
25970 char_u *sub;
25971 int sep;
25972 char_u *flags;
25973 int didit = FALSE;
25974
25975 flags = (char_u *)"";
25976 s = src + *usedlen + 2;
25977 if (src[*usedlen + 1] == 'g')
25978 {
25979 flags = (char_u *)"g";
25980 ++s;
25981 }
25982
25983 sep = *s++;
25984 if (sep)
25985 {
25986 /* find end of pattern */
25987 p = vim_strchr(s, sep);
25988 if (p != NULL)
25989 {
25990 pat = vim_strnsave(s, (int)(p - s));
25991 if (pat != NULL)
25992 {
25993 s = p + 1;
25994 /* find end of substitution */
25995 p = vim_strchr(s, sep);
25996 if (p != NULL)
25997 {
25998 sub = vim_strnsave(s, (int)(p - s));
25999 str = vim_strnsave(*fnamep, *fnamelen);
26000 if (sub != NULL && str != NULL)
26001 {
26002 *usedlen = (int)(p + 1 - src);
26003 s = do_string_sub(str, pat, sub, flags);
26004 if (s != NULL)
26005 {
26006 *fnamep = s;
26007 *fnamelen = (int)STRLEN(s);
26008 vim_free(*bufp);
26009 *bufp = s;
26010 didit = TRUE;
26011 }
26012 }
26013 vim_free(sub);
26014 vim_free(str);
26015 }
26016 vim_free(pat);
26017 }
26018 }
26019 /* after using ":s", repeat all the modifiers */
26020 if (didit)
26021 goto repeat;
26022 }
26023 }
26024
Bram Moolenaar26df0922014-02-23 23:39:13 +010026025 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26026 {
26027 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26028 if (p == NULL)
26029 return -1;
26030 vim_free(*bufp);
26031 *bufp = *fnamep = p;
26032 *fnamelen = (int)STRLEN(p);
26033 *usedlen += 2;
26034 }
26035
Bram Moolenaar071d4272004-06-13 20:20:40 +000026036 return valid;
26037}
26038
26039/*
26040 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26041 * "flags" can be "g" to do a global substitute.
26042 * Returns an allocated string, NULL for error.
26043 */
26044 char_u *
26045do_string_sub(str, pat, sub, flags)
26046 char_u *str;
26047 char_u *pat;
26048 char_u *sub;
26049 char_u *flags;
26050{
26051 int sublen;
26052 regmatch_T regmatch;
26053 int i;
26054 int do_all;
26055 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026056 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026057 garray_T ga;
26058 char_u *ret;
26059 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026060 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026061
26062 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26063 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026064 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026065
26066 ga_init2(&ga, 1, 200);
26067
26068 do_all = (flags[0] == 'g');
26069
26070 regmatch.rm_ic = p_ic;
26071 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26072 if (regmatch.regprog != NULL)
26073 {
26074 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026075 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026076 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26077 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026078 /* Skip empty match except for first match. */
26079 if (regmatch.startp[0] == regmatch.endp[0])
26080 {
26081 if (zero_width == regmatch.startp[0])
26082 {
26083 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026084 i = MB_PTR2LEN(tail);
26085 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26086 (size_t)i);
26087 ga.ga_len += i;
26088 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026089 continue;
26090 }
26091 zero_width = regmatch.startp[0];
26092 }
26093
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094 /*
26095 * Get some space for a temporary buffer to do the substitution
26096 * into. It will contain:
26097 * - The text up to where the match is.
26098 * - The substituted text.
26099 * - The text after the match.
26100 */
26101 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026102 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26104 {
26105 ga_clear(&ga);
26106 break;
26107 }
26108
26109 /* copy the text up to where the match is */
26110 i = (int)(regmatch.startp[0] - tail);
26111 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26112 /* add the substituted text */
26113 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26114 + ga.ga_len + i, TRUE, TRUE, FALSE);
26115 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026116 tail = regmatch.endp[0];
26117 if (*tail == NUL)
26118 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026119 if (!do_all)
26120 break;
26121 }
26122
26123 if (ga.ga_data != NULL)
26124 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26125
Bram Moolenaar473de612013-06-08 18:19:48 +020026126 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026127 }
26128
26129 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26130 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026131 if (p_cpo == empty_option)
26132 p_cpo = save_cpo;
26133 else
26134 /* Darn, evaluating {sub} expression changed the value. */
26135 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026136
26137 return ret;
26138}
26139
26140#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */