blob: de30f712ee495c9193b2c442e2dd04b8e6667a18 [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 Moolenaarf193fff2006-04-27 00:02:13 +000010963 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010964
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010965 /* This is not allowed in the sandbox. If the commands would still be
10966 * executed in the sandbox it would be OK, but it probably happens later,
10967 * when "sandbox" is no longer set. */
10968 if (check_secure())
10969 return;
10970
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010971 keys = get_tv_string(&argvars[0]);
10972 if (*keys != NUL)
10973 {
10974 if (argvars[1].v_type != VAR_UNKNOWN)
10975 {
10976 flags = get_tv_string_buf(&argvars[1], nbuf);
10977 for ( ; *flags != NUL; ++flags)
10978 {
10979 switch (*flags)
10980 {
10981 case 'n': remap = FALSE; break;
10982 case 'm': remap = TRUE; break;
10983 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010984 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010985 }
10986 }
10987 }
10988
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010989 /* Need to escape K_SPECIAL and CSI before putting the string in the
10990 * typeahead buffer. */
10991 keys_esc = vim_strsave_escape_csi(keys);
10992 if (keys_esc != NULL)
10993 {
10994 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010995 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010996 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010997 if (vgetc_busy)
10998 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010999 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011000 }
11001}
11002
11003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 * "filereadable()" function
11005 */
11006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011008 typval_T *argvars;
11009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011011 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 char_u *p;
11013 int n;
11014
Bram Moolenaarc236c162008-07-13 17:41:49 +000011015#ifndef O_NONBLOCK
11016# define O_NONBLOCK 0
11017#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011018 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011019 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11020 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 {
11022 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011023 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 }
11025 else
11026 n = FALSE;
11027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011028 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029}
11030
11031/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011032 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 * rights to write into.
11034 */
11035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011037 typval_T *argvars;
11038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011040 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041}
11042
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011043static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011044
11045 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011046findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011047 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011048 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011049 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011050{
11051#ifdef FEAT_SEARCHPATH
11052 char_u *fname;
11053 char_u *fresult = NULL;
11054 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11055 char_u *p;
11056 char_u pathbuf[NUMBUFLEN];
11057 int count = 1;
11058 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011059 int error = FALSE;
11060#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011061
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011062 rettv->vval.v_string = NULL;
11063 rettv->v_type = VAR_STRING;
11064
11065#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011066 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011067
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011068 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011070 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11071 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011072 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011073 else
11074 {
11075 if (*p != NUL)
11076 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011077
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011079 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011080 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011081 }
11082
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011083 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11084 error = TRUE;
11085
11086 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011088 do
11089 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011090 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011091 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011092 fresult = find_file_in_path_option(first ? fname : NULL,
11093 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011094 0, first, path,
11095 find_what,
11096 curbuf->b_ffname,
11097 find_what == FINDFILE_DIR
11098 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011099 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011100
11101 if (fresult != NULL && rettv->v_type == VAR_LIST)
11102 list_append_string(rettv->vval.v_list, fresult, -1);
11103
11104 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011105 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011106
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011107 if (rettv->v_type == VAR_STRING)
11108 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011109#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011110}
11111
Bram Moolenaar33570922005-01-25 22:26:29 +000011112static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
11113static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011114
11115/*
11116 * Implementation of map() and filter().
11117 */
11118 static void
11119filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011120 typval_T *argvars;
11121 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011122 int map;
11123{
11124 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011125 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011126 listitem_T *li, *nli;
11127 list_T *l = NULL;
11128 dictitem_T *di;
11129 hashtab_T *ht;
11130 hashitem_T *hi;
11131 dict_T *d = NULL;
11132 typval_T save_val;
11133 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011134 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011135 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011136 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011137 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011138 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011139 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011140 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011141
Bram Moolenaare9a41262005-01-15 22:18:47 +000011142 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011143 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011144 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011145 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011146 return;
11147 }
11148 else if (argvars[0].v_type == VAR_DICT)
11149 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011150 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011151 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011152 return;
11153 }
11154 else
11155 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011156 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011157 return;
11158 }
11159
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011160 expr = get_tv_string_buf_chk(&argvars[1], buf);
11161 /* On type errors, the preceding call has already displayed an error
11162 * message. Avoid a misleading error message for an empty string that
11163 * was not passed as argument. */
11164 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011165 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 prepare_vimvar(VV_VAL, &save_val);
11167 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011168
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011169 /* We reset "did_emsg" to be able to detect whether an error
11170 * occurred during evaluation of the expression. */
11171 save_did_emsg = did_emsg;
11172 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011173
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011174 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011175 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011177 vimvars[VV_KEY].vv_type = VAR_STRING;
11178
11179 ht = &d->dv_hashtab;
11180 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011181 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011182 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011184 if (!HASHITEM_EMPTY(hi))
11185 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011186 int r;
11187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011188 --todo;
11189 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011190 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011191 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11192 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011193 break;
11194 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011195 r = filter_map_one(&di->di_tv, expr, map, &rem);
11196 clear_tv(&vimvars[VV_KEY].vv_tv);
11197 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011198 break;
11199 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011200 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011201 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11202 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011203 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011204 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011205 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011206 }
11207 }
11208 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011209 }
11210 else
11211 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011212 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011214 for (li = l->lv_first; li != NULL; li = nli)
11215 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011216 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011217 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011218 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011219 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011220 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011221 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011222 break;
11223 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011224 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011225 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011226 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011227 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011228
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011229 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011231
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011232 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011233 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011234
11235 copy_tv(&argvars[0], rettv);
11236}
11237
11238 static int
11239filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011240 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011241 char_u *expr;
11242 int map;
11243 int *remp;
11244{
Bram Moolenaar33570922005-01-25 22:26:29 +000011245 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011246 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011247 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011248
Bram Moolenaar33570922005-01-25 22:26:29 +000011249 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011250 s = expr;
11251 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011252 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011253 if (*s != NUL) /* check for trailing chars after expr */
11254 {
11255 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011256 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011257 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011258 }
11259 if (map)
11260 {
11261 /* map(): replace the list item value */
11262 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011263 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011264 *tv = rettv;
11265 }
11266 else
11267 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011268 int error = FALSE;
11269
Bram Moolenaare9a41262005-01-15 22:18:47 +000011270 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011271 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011272 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011273 /* On type error, nothing has been removed; return FAIL to stop the
11274 * loop. The error message was given by get_tv_number_chk(). */
11275 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011276 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011277 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011278 retval = OK;
11279theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011280 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011281 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011282}
11283
11284/*
11285 * "filter()" function
11286 */
11287 static void
11288f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011289 typval_T *argvars;
11290 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011291{
11292 filter_map(argvars, rettv, FALSE);
11293}
11294
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011295/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011296 * "finddir({fname}[, {path}[, {count}]])" function
11297 */
11298 static void
11299f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011300 typval_T *argvars;
11301 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011302{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011303 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011304}
11305
11306/*
11307 * "findfile({fname}[, {path}[, {count}]])" function
11308 */
11309 static void
11310f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011311 typval_T *argvars;
11312 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011313{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011314 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011315}
11316
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011317#ifdef FEAT_FLOAT
11318/*
11319 * "float2nr({float})" function
11320 */
11321 static void
11322f_float2nr(argvars, rettv)
11323 typval_T *argvars;
11324 typval_T *rettv;
11325{
11326 float_T f;
11327
11328 if (get_float_arg(argvars, &f) == OK)
11329 {
11330 if (f < -0x7fffffff)
11331 rettv->vval.v_number = -0x7fffffff;
11332 else if (f > 0x7fffffff)
11333 rettv->vval.v_number = 0x7fffffff;
11334 else
11335 rettv->vval.v_number = (varnumber_T)f;
11336 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011337}
11338
11339/*
11340 * "floor({float})" function
11341 */
11342 static void
11343f_floor(argvars, rettv)
11344 typval_T *argvars;
11345 typval_T *rettv;
11346{
11347 float_T f;
11348
11349 rettv->v_type = VAR_FLOAT;
11350 if (get_float_arg(argvars, &f) == OK)
11351 rettv->vval.v_float = floor(f);
11352 else
11353 rettv->vval.v_float = 0.0;
11354}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011355
11356/*
11357 * "fmod()" function
11358 */
11359 static void
11360f_fmod(argvars, rettv)
11361 typval_T *argvars;
11362 typval_T *rettv;
11363{
11364 float_T fx, fy;
11365
11366 rettv->v_type = VAR_FLOAT;
11367 if (get_float_arg(argvars, &fx) == OK
11368 && get_float_arg(&argvars[1], &fy) == OK)
11369 rettv->vval.v_float = fmod(fx, fy);
11370 else
11371 rettv->vval.v_float = 0.0;
11372}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011373#endif
11374
Bram Moolenaar0d660222005-01-07 21:51:51 +000011375/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011376 * "fnameescape({string})" function
11377 */
11378 static void
11379f_fnameescape(argvars, rettv)
11380 typval_T *argvars;
11381 typval_T *rettv;
11382{
11383 rettv->vval.v_string = vim_strsave_fnameescape(
11384 get_tv_string(&argvars[0]), FALSE);
11385 rettv->v_type = VAR_STRING;
11386}
11387
11388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 * "fnamemodify({fname}, {mods})" function
11390 */
11391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011392f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011393 typval_T *argvars;
11394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395{
11396 char_u *fname;
11397 char_u *mods;
11398 int usedlen = 0;
11399 int len;
11400 char_u *fbuf = NULL;
11401 char_u buf[NUMBUFLEN];
11402
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011403 fname = get_tv_string_chk(&argvars[0]);
11404 mods = get_tv_string_buf_chk(&argvars[1], buf);
11405 if (fname == NULL || mods == NULL)
11406 fname = NULL;
11407 else
11408 {
11409 len = (int)STRLEN(fname);
11410 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011415 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 vim_free(fbuf);
11419}
11420
Bram Moolenaar33570922005-01-25 22:26:29 +000011421static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422
11423/*
11424 * "foldclosed()" function
11425 */
11426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011427foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011428 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011429 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011430 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431{
11432#ifdef FEAT_FOLDING
11433 linenr_T lnum;
11434 linenr_T first, last;
11435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11438 {
11439 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11440 {
11441 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 return;
11446 }
11447 }
11448#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450}
11451
11452/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011453 * "foldclosed()" function
11454 */
11455 static void
11456f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011457 typval_T *argvars;
11458 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011459{
11460 foldclosed_both(argvars, rettv, FALSE);
11461}
11462
11463/*
11464 * "foldclosedend()" function
11465 */
11466 static void
11467f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011468 typval_T *argvars;
11469 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011470{
11471 foldclosed_both(argvars, rettv, TRUE);
11472}
11473
11474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 * "foldlevel()" function
11476 */
11477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011479 typval_T *argvars UNUSED;
11480 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481{
11482#ifdef FEAT_FOLDING
11483 linenr_T lnum;
11484
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489}
11490
11491/*
11492 * "foldtext()" function
11493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011496 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498{
11499#ifdef FEAT_FOLDING
11500 linenr_T lnum;
11501 char_u *s;
11502 char_u *r;
11503 int len;
11504 char *txt;
11505#endif
11506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011507 rettv->v_type = VAR_STRING;
11508 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011510 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11511 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11512 <= curbuf->b_ml.ml_line_count
11513 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514 {
11515 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011516 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11517 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 {
11519 if (!linewhite(lnum))
11520 break;
11521 ++lnum;
11522 }
11523
11524 /* Find interesting text in this line. */
11525 s = skipwhite(ml_get(lnum));
11526 /* skip C comment-start */
11527 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011530 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011531 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011532 {
11533 s = skipwhite(ml_get(lnum + 1));
11534 if (*s == '*')
11535 s = skipwhite(s + 1);
11536 }
11537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538 txt = _("+-%s%3ld lines: ");
11539 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011540 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 + 20 /* for %3ld */
11542 + STRLEN(s))); /* concatenated */
11543 if (r != NULL)
11544 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011545 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11546 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11547 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 len = (int)STRLEN(r);
11549 STRCAT(r, s);
11550 /* remove 'foldmarker' and 'commentstring' */
11551 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 }
11554 }
11555#endif
11556}
11557
11558/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011559 * "foldtextresult(lnum)" function
11560 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011563 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011564 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011565{
11566#ifdef FEAT_FOLDING
11567 linenr_T lnum;
11568 char_u *text;
11569 char_u buf[51];
11570 foldinfo_T foldinfo;
11571 int fold_count;
11572#endif
11573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011574 rettv->v_type = VAR_STRING;
11575 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011576#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011578 /* treat illegal types and illegal string values for {lnum} the same */
11579 if (lnum < 0)
11580 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011581 fold_count = foldedCount(curwin, lnum, &foldinfo);
11582 if (fold_count > 0)
11583 {
11584 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11585 &foldinfo, buf);
11586 if (text == buf)
11587 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011588 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011589 }
11590#endif
11591}
11592
11593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594 * "foreground()" function
11595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011597f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011598 typval_T *argvars UNUSED;
11599 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601#ifdef FEAT_GUI
11602 if (gui.in_use)
11603 gui_mch_set_foreground();
11604#else
11605# ifdef WIN32
11606 win32_set_foreground();
11607# endif
11608#endif
11609}
11610
11611/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011612 * "function()" function
11613 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011615f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011616 typval_T *argvars;
11617 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011618{
11619 char_u *s;
11620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011622 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011623 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011624 /* Don't check an autoload name for existence here. */
11625 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011626 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011627 else
11628 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011629 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011630 {
11631 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011632 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011633
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011634 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11635 * also be called from another script. Using trans_function_name()
11636 * would also work, but some plugins depend on the name being
11637 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011638 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011639 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011640 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011641 if (rettv->vval.v_string != NULL)
11642 {
11643 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011644 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011645 }
11646 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011647 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011648 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011649 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011650 }
11651}
11652
11653/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011654 * "garbagecollect()" function
11655 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011656 static void
11657f_garbagecollect(argvars, rettv)
11658 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011659 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011660{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011661 /* This is postponed until we are back at the toplevel, because we may be
11662 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11663 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011664
11665 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11666 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011667}
11668
11669/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011670 * "get()" function
11671 */
11672 static void
11673f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011674 typval_T *argvars;
11675 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011676{
Bram Moolenaar33570922005-01-25 22:26:29 +000011677 listitem_T *li;
11678 list_T *l;
11679 dictitem_T *di;
11680 dict_T *d;
11681 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011682
Bram Moolenaare9a41262005-01-15 22:18:47 +000011683 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011684 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011685 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011686 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011687 int error = FALSE;
11688
11689 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11690 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011691 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011692 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011693 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011694 else if (argvars[0].v_type == VAR_DICT)
11695 {
11696 if ((d = argvars[0].vval.v_dict) != NULL)
11697 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011698 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011699 if (di != NULL)
11700 tv = &di->di_tv;
11701 }
11702 }
11703 else
11704 EMSG2(_(e_listdictarg), "get()");
11705
11706 if (tv == NULL)
11707 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011708 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011709 copy_tv(&argvars[2], rettv);
11710 }
11711 else
11712 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011713}
11714
Bram Moolenaar342337a2005-07-21 21:11:17 +000011715static 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 +000011716
11717/*
11718 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011719 * Return a range (from start to end) of lines in rettv from the specified
11720 * buffer.
11721 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011722 */
11723 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011724get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011725 buf_T *buf;
11726 linenr_T start;
11727 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011728 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011729 typval_T *rettv;
11730{
11731 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011732
Bram Moolenaar959a1432013-12-14 12:17:38 +010011733 rettv->v_type = VAR_STRING;
11734 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011735 if (retlist && rettv_list_alloc(rettv) == FAIL)
11736 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011737
11738 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11739 return;
11740
11741 if (!retlist)
11742 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011743 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11744 p = ml_get_buf(buf, start, FALSE);
11745 else
11746 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011747 rettv->vval.v_string = vim_strsave(p);
11748 }
11749 else
11750 {
11751 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011752 return;
11753
11754 if (start < 1)
11755 start = 1;
11756 if (end > buf->b_ml.ml_line_count)
11757 end = buf->b_ml.ml_line_count;
11758 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011759 if (list_append_string(rettv->vval.v_list,
11760 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011761 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011762 }
11763}
11764
11765/*
11766 * "getbufline()" function
11767 */
11768 static void
11769f_getbufline(argvars, rettv)
11770 typval_T *argvars;
11771 typval_T *rettv;
11772{
11773 linenr_T lnum;
11774 linenr_T end;
11775 buf_T *buf;
11776
11777 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11778 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011779 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011780 --emsg_off;
11781
Bram Moolenaar661b1822005-07-28 22:36:45 +000011782 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011783 if (argvars[2].v_type == VAR_UNKNOWN)
11784 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011785 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011786 end = get_tv_lnum_buf(&argvars[2], buf);
11787
Bram Moolenaar342337a2005-07-21 21:11:17 +000011788 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011789}
11790
Bram Moolenaar0d660222005-01-07 21:51:51 +000011791/*
11792 * "getbufvar()" function
11793 */
11794 static void
11795f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011796 typval_T *argvars;
11797 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011798{
11799 buf_T *buf;
11800 buf_T *save_curbuf;
11801 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011802 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011803 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011805 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11806 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011807 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011808 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011809
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011810 rettv->v_type = VAR_STRING;
11811 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011812
11813 if (buf != NULL && varname != NULL)
11814 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011815 /* set curbuf to be our buf, temporarily */
11816 save_curbuf = curbuf;
11817 curbuf = buf;
11818
Bram Moolenaar0d660222005-01-07 21:51:51 +000011819 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011820 {
11821 if (get_option_tv(&varname, rettv, TRUE) == OK)
11822 done = TRUE;
11823 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011824 else if (STRCMP(varname, "changedtick") == 0)
11825 {
11826 rettv->v_type = VAR_NUMBER;
11827 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011828 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011829 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011830 else
11831 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011832 /* Look up the variable. */
11833 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11834 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11835 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011836 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011837 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011838 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011839 done = TRUE;
11840 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011841 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011842
11843 /* restore previous notion of curbuf */
11844 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011845 }
11846
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011847 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11848 /* use the default value */
11849 copy_tv(&argvars[2], rettv);
11850
Bram Moolenaar0d660222005-01-07 21:51:51 +000011851 --emsg_off;
11852}
11853
11854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 * "getchar()" function
11856 */
11857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011858f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011859 typval_T *argvars;
11860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861{
11862 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011863 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011865 /* Position the cursor. Needed after a message that ends in a space. */
11866 windgoto(msg_row, msg_col);
11867
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 ++no_mapping;
11869 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011870 for (;;)
11871 {
11872 if (argvars[0].v_type == VAR_UNKNOWN)
11873 /* getchar(): blocking wait. */
11874 n = safe_vgetc();
11875 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11876 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011877 n = vpeekc_any();
11878 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011879 /* illegal argument or getchar(0) and no char avail: return zero */
11880 n = 0;
11881 else
11882 /* getchar(0) and char avail: return char */
11883 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011884
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011885 if (n == K_IGNORE)
11886 continue;
11887 break;
11888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889 --no_mapping;
11890 --allow_keys;
11891
Bram Moolenaar219b8702006-11-01 14:32:36 +000011892 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11893 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11894 vimvars[VV_MOUSE_COL].vv_nr = 0;
11895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011896 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 if (IS_SPECIAL(n) || mod_mask != 0)
11898 {
11899 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11900 int i = 0;
11901
11902 /* Turn a special key into three bytes, plus modifier. */
11903 if (mod_mask != 0)
11904 {
11905 temp[i++] = K_SPECIAL;
11906 temp[i++] = KS_MODIFIER;
11907 temp[i++] = mod_mask;
11908 }
11909 if (IS_SPECIAL(n))
11910 {
11911 temp[i++] = K_SPECIAL;
11912 temp[i++] = K_SECOND(n);
11913 temp[i++] = K_THIRD(n);
11914 }
11915#ifdef FEAT_MBYTE
11916 else if (has_mbyte)
11917 i += (*mb_char2bytes)(n, temp + i);
11918#endif
11919 else
11920 temp[i++] = n;
11921 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011922 rettv->v_type = VAR_STRING;
11923 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011924
11925#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011926 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011927 {
11928 int row = mouse_row;
11929 int col = mouse_col;
11930 win_T *win;
11931 linenr_T lnum;
11932# ifdef FEAT_WINDOWS
11933 win_T *wp;
11934# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011935 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011936
11937 if (row >= 0 && col >= 0)
11938 {
11939 /* Find the window at the mouse coordinates and compute the
11940 * text position. */
11941 win = mouse_find_win(&row, &col);
11942 (void)mouse_comp_pos(win, &row, &col, &lnum);
11943# ifdef FEAT_WINDOWS
11944 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011945 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011946# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011947 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011948 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11949 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11950 }
11951 }
11952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953 }
11954}
11955
11956/*
11957 * "getcharmod()" function
11958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011960f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011961 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011964 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965}
11966
11967/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011968 * "getcharsearch()" function
11969 */
11970 static void
11971f_getcharsearch(argvars, rettv)
11972 typval_T *argvars UNUSED;
11973 typval_T *rettv;
11974{
11975 if (rettv_dict_alloc(rettv) != FAIL)
11976 {
11977 dict_T *dict = rettv->vval.v_dict;
11978
11979 dict_add_nr_str(dict, "char", 0L, last_csearch());
11980 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11981 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11982 }
11983}
11984
11985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986 * "getcmdline()" function
11987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011990 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993 rettv->v_type = VAR_STRING;
11994 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995}
11996
11997/*
11998 * "getcmdpos()" function
11999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012002 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006}
12007
12008/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012009 * "getcmdtype()" function
12010 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012011 static void
12012f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012013 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012014 typval_T *rettv;
12015{
12016 rettv->v_type = VAR_STRING;
12017 rettv->vval.v_string = alloc(2);
12018 if (rettv->vval.v_string != NULL)
12019 {
12020 rettv->vval.v_string[0] = get_cmdline_type();
12021 rettv->vval.v_string[1] = NUL;
12022 }
12023}
12024
12025/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012026 * "getcmdwintype()" function
12027 */
12028 static void
12029f_getcmdwintype(argvars, rettv)
12030 typval_T *argvars UNUSED;
12031 typval_T *rettv;
12032{
12033 rettv->v_type = VAR_STRING;
12034 rettv->vval.v_string = NULL;
12035#ifdef FEAT_CMDWIN
12036 rettv->vval.v_string = alloc(2);
12037 if (rettv->vval.v_string != NULL)
12038 {
12039 rettv->vval.v_string[0] = cmdwin_type;
12040 rettv->vval.v_string[1] = NUL;
12041 }
12042#endif
12043}
12044
12045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046 * "getcwd()" function
12047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012049f_getcwd(argvars, rettv)
Bram Moolenaarc9703302016-01-17 21:49:33 +010012050 typval_T *argvars;
Bram Moolenaar33570922005-01-25 22:26:29 +000012051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012053 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012054 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012056 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012057 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012058
12059 wp = find_tabwin(&argvars[0], &argvars[1]);
12060 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012062 if (wp->w_localdir != NULL)
12063 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12064 else if(globaldir != NULL)
12065 rettv->vval.v_string = vim_strsave(globaldir);
12066 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012067 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012068 cwd = alloc(MAXPATHL);
12069 if (cwd != NULL)
12070 {
12071 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12072 rettv->vval.v_string = vim_strsave(cwd);
12073 vim_free(cwd);
12074 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012075 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012076#ifdef BACKSLASH_IN_FILENAME
12077 if (rettv->vval.v_string != NULL)
12078 slash_adjust(rettv->vval.v_string);
12079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 }
12081}
12082
12083/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012084 * "getfontname()" function
12085 */
12086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012088 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012089 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012090{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012091 rettv->v_type = VAR_STRING;
12092 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012093#ifdef FEAT_GUI
12094 if (gui.in_use)
12095 {
12096 GuiFont font;
12097 char_u *name = NULL;
12098
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012099 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012100 {
12101 /* Get the "Normal" font. Either the name saved by
12102 * hl_set_font_name() or from the font ID. */
12103 font = gui.norm_font;
12104 name = hl_get_font_name();
12105 }
12106 else
12107 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012109 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12110 return;
12111 font = gui_mch_get_font(name, FALSE);
12112 if (font == NOFONT)
12113 return; /* Invalid font name, return empty string. */
12114 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012115 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012116 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012117 gui_mch_free_font(font);
12118 }
12119#endif
12120}
12121
12122/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012123 * "getfperm({fname})" function
12124 */
12125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012126f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012127 typval_T *argvars;
12128 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012129{
12130 char_u *fname;
12131 struct stat st;
12132 char_u *perm = NULL;
12133 char_u flags[] = "rwx";
12134 int i;
12135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012136 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012139 if (mch_stat((char *)fname, &st) >= 0)
12140 {
12141 perm = vim_strsave((char_u *)"---------");
12142 if (perm != NULL)
12143 {
12144 for (i = 0; i < 9; i++)
12145 {
12146 if (st.st_mode & (1 << (8 - i)))
12147 perm[i] = flags[i % 3];
12148 }
12149 }
12150 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012151 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012152}
12153
12154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155 * "getfsize({fname})" function
12156 */
12157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012159 typval_T *argvars;
12160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161{
12162 char_u *fname;
12163 struct stat st;
12164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012165 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012167 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168
12169 if (mch_stat((char *)fname, &st) >= 0)
12170 {
12171 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012175 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012176
12177 /* non-perfect check for overflow */
12178 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12179 rettv->vval.v_number = -2;
12180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181 }
12182 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184}
12185
12186/*
12187 * "getftime({fname})" function
12188 */
12189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012190f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012191 typval_T *argvars;
12192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193{
12194 char_u *fname;
12195 struct stat st;
12196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012197 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198
12199 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012200 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012202 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203}
12204
12205/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012206 * "getftype({fname})" function
12207 */
12208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012209f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012210 typval_T *argvars;
12211 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012212{
12213 char_u *fname;
12214 struct stat st;
12215 char_u *type = NULL;
12216 char *t;
12217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012218 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012220 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012221 if (mch_lstat((char *)fname, &st) >= 0)
12222 {
12223#ifdef S_ISREG
12224 if (S_ISREG(st.st_mode))
12225 t = "file";
12226 else if (S_ISDIR(st.st_mode))
12227 t = "dir";
12228# ifdef S_ISLNK
12229 else if (S_ISLNK(st.st_mode))
12230 t = "link";
12231# endif
12232# ifdef S_ISBLK
12233 else if (S_ISBLK(st.st_mode))
12234 t = "bdev";
12235# endif
12236# ifdef S_ISCHR
12237 else if (S_ISCHR(st.st_mode))
12238 t = "cdev";
12239# endif
12240# ifdef S_ISFIFO
12241 else if (S_ISFIFO(st.st_mode))
12242 t = "fifo";
12243# endif
12244# ifdef S_ISSOCK
12245 else if (S_ISSOCK(st.st_mode))
12246 t = "fifo";
12247# endif
12248 else
12249 t = "other";
12250#else
12251# ifdef S_IFMT
12252 switch (st.st_mode & S_IFMT)
12253 {
12254 case S_IFREG: t = "file"; break;
12255 case S_IFDIR: t = "dir"; break;
12256# ifdef S_IFLNK
12257 case S_IFLNK: t = "link"; break;
12258# endif
12259# ifdef S_IFBLK
12260 case S_IFBLK: t = "bdev"; break;
12261# endif
12262# ifdef S_IFCHR
12263 case S_IFCHR: t = "cdev"; break;
12264# endif
12265# ifdef S_IFIFO
12266 case S_IFIFO: t = "fifo"; break;
12267# endif
12268# ifdef S_IFSOCK
12269 case S_IFSOCK: t = "socket"; break;
12270# endif
12271 default: t = "other";
12272 }
12273# else
12274 if (mch_isdir(fname))
12275 t = "dir";
12276 else
12277 t = "file";
12278# endif
12279#endif
12280 type = vim_strsave((char_u *)t);
12281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012282 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012283}
12284
12285/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012286 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012287 */
12288 static void
12289f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012290 typval_T *argvars;
12291 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012292{
12293 linenr_T lnum;
12294 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012295 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012296
12297 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012298 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012299 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012300 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012301 retlist = FALSE;
12302 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012303 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012304 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012305 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012306 retlist = TRUE;
12307 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012308
Bram Moolenaar342337a2005-07-21 21:11:17 +000012309 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012310}
12311
12312/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012313 * "getmatches()" function
12314 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012315 static void
12316f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012317 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012318 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012319{
12320#ifdef FEAT_SEARCH_EXTRA
12321 dict_T *dict;
12322 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012323 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012324
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012325 if (rettv_list_alloc(rettv) == OK)
12326 {
12327 while (cur != NULL)
12328 {
12329 dict = dict_alloc();
12330 if (dict == NULL)
12331 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012332 if (cur->match.regprog == NULL)
12333 {
12334 /* match added with matchaddpos() */
12335 for (i = 0; i < MAXPOSMATCH; ++i)
12336 {
12337 llpos_T *llpos;
12338 char buf[6];
12339 list_T *l;
12340
12341 llpos = &cur->pos.pos[i];
12342 if (llpos->lnum == 0)
12343 break;
12344 l = list_alloc();
12345 if (l == NULL)
12346 break;
12347 list_append_number(l, (varnumber_T)llpos->lnum);
12348 if (llpos->col > 0)
12349 {
12350 list_append_number(l, (varnumber_T)llpos->col);
12351 list_append_number(l, (varnumber_T)llpos->len);
12352 }
12353 sprintf(buf, "pos%d", i + 1);
12354 dict_add_list(dict, buf, l);
12355 }
12356 }
12357 else
12358 {
12359 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12360 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012361 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012362 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12363 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012364# ifdef FEAT_CONCEAL
12365 if (cur->conceal_char)
12366 {
12367 char_u buf[MB_MAXBYTES + 1];
12368
12369 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12370 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12371 }
12372# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012373 list_append_dict(rettv->vval.v_list, dict);
12374 cur = cur->next;
12375 }
12376 }
12377#endif
12378}
12379
12380/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012381 * "getpid()" function
12382 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012383 static void
12384f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012385 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012386 typval_T *rettv;
12387{
12388 rettv->vval.v_number = mch_get_pid();
12389}
12390
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012391static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12392
12393/*
12394 * "getcurpos()" function
12395 */
12396 static void
12397f_getcurpos(argvars, rettv)
12398 typval_T *argvars;
12399 typval_T *rettv;
12400{
12401 getpos_both(argvars, rettv, TRUE);
12402}
12403
Bram Moolenaar18081e32008-02-20 19:11:07 +000012404/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012405 * "getpos(string)" function
12406 */
12407 static void
12408f_getpos(argvars, rettv)
12409 typval_T *argvars;
12410 typval_T *rettv;
12411{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012412 getpos_both(argvars, rettv, FALSE);
12413}
12414
12415 static void
12416getpos_both(argvars, rettv, getcurpos)
12417 typval_T *argvars;
12418 typval_T *rettv;
12419 int getcurpos;
12420{
Bram Moolenaara5525202006-03-02 22:52:09 +000012421 pos_T *fp;
12422 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012423 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012424
12425 if (rettv_list_alloc(rettv) == OK)
12426 {
12427 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012428 if (getcurpos)
12429 fp = &curwin->w_cursor;
12430 else
12431 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012432 if (fnum != -1)
12433 list_append_number(l, (varnumber_T)fnum);
12434 else
12435 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012436 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12437 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012438 list_append_number(l, (fp != NULL)
12439 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012440 : (varnumber_T)0);
12441 list_append_number(l,
12442#ifdef FEAT_VIRTUALEDIT
12443 (fp != NULL) ? (varnumber_T)fp->coladd :
12444#endif
12445 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012446 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012447 list_append_number(l, curwin->w_curswant == MAXCOL ?
12448 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012449 }
12450 else
12451 rettv->vval.v_number = FALSE;
12452}
12453
12454/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012455 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012456 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012457 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012458f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012459 typval_T *argvars UNUSED;
12460 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012461{
12462#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012463 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012464#endif
12465
Bram Moolenaar2641f772005-03-25 21:58:17 +000012466#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012467 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012468 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012469 wp = NULL;
12470 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12471 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012472 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012473 if (wp == NULL)
12474 return;
12475 }
12476
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012477 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012478 }
12479#endif
12480}
12481
12482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483 * "getreg()" function
12484 */
12485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012486f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012487 typval_T *argvars;
12488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489{
12490 char_u *strregname;
12491 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012492 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012493 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012494 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012496 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012498 strregname = get_tv_string_chk(&argvars[0]);
12499 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012500 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012502 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012503 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12504 return_list = get_tv_number_chk(&argvars[2], &error);
12505 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012508 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012509
12510 if (error)
12511 return;
12512
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 regname = (strregname == NULL ? '"' : *strregname);
12514 if (regname == 0)
12515 regname = '"';
12516
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012517 if (return_list)
12518 {
12519 rettv->v_type = VAR_LIST;
12520 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12521 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012522 if (rettv->vval.v_list != NULL)
12523 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012524 }
12525 else
12526 {
12527 rettv->v_type = VAR_STRING;
12528 rettv->vval.v_string = get_reg_contents(regname,
12529 arg2 ? GREG_EXPR_SRC : 0);
12530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531}
12532
12533/*
12534 * "getregtype()" function
12535 */
12536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012537f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012538 typval_T *argvars;
12539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540{
12541 char_u *strregname;
12542 int regname;
12543 char_u buf[NUMBUFLEN + 2];
12544 long reglen = 0;
12545
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012546 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012547 {
12548 strregname = get_tv_string_chk(&argvars[0]);
12549 if (strregname == NULL) /* type error; errmsg already given */
12550 {
12551 rettv->v_type = VAR_STRING;
12552 rettv->vval.v_string = NULL;
12553 return;
12554 }
12555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 else
12557 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012558 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559
12560 regname = (strregname == NULL ? '"' : *strregname);
12561 if (regname == 0)
12562 regname = '"';
12563
12564 buf[0] = NUL;
12565 buf[1] = NUL;
12566 switch (get_reg_type(regname, &reglen))
12567 {
12568 case MLINE: buf[0] = 'V'; break;
12569 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570 case MBLOCK:
12571 buf[0] = Ctrl_V;
12572 sprintf((char *)buf + 1, "%ld", reglen + 1);
12573 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012575 rettv->v_type = VAR_STRING;
12576 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577}
12578
12579/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012580 * "gettabvar()" function
12581 */
12582 static void
12583f_gettabvar(argvars, rettv)
12584 typval_T *argvars;
12585 typval_T *rettv;
12586{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012587 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012588 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012589 dictitem_T *v;
12590 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012591 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012592
12593 rettv->v_type = VAR_STRING;
12594 rettv->vval.v_string = NULL;
12595
12596 varname = get_tv_string_chk(&argvars[1]);
12597 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12598 if (tp != NULL && varname != NULL)
12599 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012600 /* Set tp to be our tabpage, temporarily. Also set the window to the
12601 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012602 if (switch_win(&oldcurwin, &oldtabpage,
12603 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012604 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012605 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012606 /* look up the variable */
12607 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12608 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12609 if (v != NULL)
12610 {
12611 copy_tv(&v->di_tv, rettv);
12612 done = TRUE;
12613 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012614 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012615
12616 /* restore previous notion of curwin */
12617 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012618 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012619
12620 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012621 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012622}
12623
12624/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012625 * "gettabwinvar()" function
12626 */
12627 static void
12628f_gettabwinvar(argvars, rettv)
12629 typval_T *argvars;
12630 typval_T *rettv;
12631{
12632 getwinvar(argvars, rettv, 1);
12633}
12634
12635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636 * "getwinposx()" function
12637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012639f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012640 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644#ifdef FEAT_GUI
12645 if (gui.in_use)
12646 {
12647 int x, y;
12648
12649 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012650 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651 }
12652#endif
12653}
12654
12655/*
12656 * "getwinposy()" function
12657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012659f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012660 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012663 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664#ifdef FEAT_GUI
12665 if (gui.in_use)
12666 {
12667 int x, y;
12668
12669 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012670 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671 }
12672#endif
12673}
12674
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012675/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012676 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012677 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012678 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012679find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012680 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012681 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012682{
12683#ifdef FEAT_WINDOWS
12684 win_T *wp;
12685#endif
12686 int nr;
12687
12688 nr = get_tv_number_chk(vp, NULL);
12689
12690#ifdef FEAT_WINDOWS
12691 if (nr < 0)
12692 return NULL;
12693 if (nr == 0)
12694 return curwin;
12695
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012696 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12697 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012698 if (--nr <= 0)
12699 break;
12700 return wp;
12701#else
12702 if (nr == 0 || nr == 1)
12703 return curwin;
12704 return NULL;
12705#endif
12706}
12707
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012709 * Find window specified by "wvp" in tabpage "tvp".
12710 */
12711 static win_T *
12712find_tabwin(wvp, tvp)
12713 typval_T *wvp; /* VAR_UNKNOWN for current window */
12714 typval_T *tvp; /* VAR_UNKNOWN for current tab page */
12715{
12716 win_T *wp = NULL;
12717 tabpage_T *tp = NULL;
12718 long n;
12719
12720 if (wvp->v_type != VAR_UNKNOWN)
12721 {
12722 if (tvp->v_type != VAR_UNKNOWN)
12723 {
12724 n = get_tv_number(tvp);
12725 if (n >= 0)
12726 tp = find_tabpage(n);
12727 }
12728 else
12729 tp = curtab;
12730
12731 if (tp != NULL)
12732 wp = find_win_by_nr(wvp, tp);
12733 }
12734 else
12735 wp = curwin;
12736
12737 return wp;
12738}
12739
12740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741 * "getwinvar()" function
12742 */
12743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012744f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012745 typval_T *argvars;
12746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012748 getwinvar(argvars, rettv, 0);
12749}
12750
12751/*
12752 * getwinvar() and gettabwinvar()
12753 */
12754 static void
12755getwinvar(argvars, rettv, off)
12756 typval_T *argvars;
12757 typval_T *rettv;
12758 int off; /* 1 for gettabwinvar() */
12759{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012760 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012762 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012763 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012764 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012765#ifdef FEAT_WINDOWS
12766 win_T *oldcurwin;
12767 tabpage_T *oldtabpage;
12768 int need_switch_win;
12769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012771#ifdef FEAT_WINDOWS
12772 if (off == 1)
12773 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12774 else
12775 tp = curtab;
12776#endif
12777 win = find_win_by_nr(&argvars[off], tp);
12778 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012779 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012781 rettv->v_type = VAR_STRING;
12782 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783
12784 if (win != NULL && varname != NULL)
12785 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012786#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012787 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012788 * otherwise the window is not valid. Only do this when needed,
12789 * autocommands get blocked. */
12790 need_switch_win = !(tp == curtab && win == curwin);
12791 if (!need_switch_win
12792 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12793#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012794 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012795 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012796 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012797 if (get_option_tv(&varname, rettv, 1) == OK)
12798 done = TRUE;
12799 }
12800 else
12801 {
12802 /* Look up the variable. */
12803 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12804 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12805 varname, FALSE);
12806 if (v != NULL)
12807 {
12808 copy_tv(&v->di_tv, rettv);
12809 done = TRUE;
12810 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012813
Bram Moolenaarba117c22015-09-29 16:53:22 +020012814#ifdef FEAT_WINDOWS
12815 if (need_switch_win)
12816 /* restore previous notion of curwin */
12817 restore_win(oldcurwin, oldtabpage, TRUE);
12818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 }
12820
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012821 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12822 /* use the default return value */
12823 copy_tv(&argvars[off + 2], rettv);
12824
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825 --emsg_off;
12826}
12827
12828/*
12829 * "glob()" function
12830 */
12831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012832f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012833 typval_T *argvars;
12834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012836 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012838 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012840 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012841 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012843 if (argvars[1].v_type != VAR_UNKNOWN)
12844 {
12845 if (get_tv_number_chk(&argvars[1], &error))
12846 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012847 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012848 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012849 if (get_tv_number_chk(&argvars[2], &error))
12850 {
12851 rettv->v_type = VAR_LIST;
12852 rettv->vval.v_list = NULL;
12853 }
12854 if (argvars[3].v_type != VAR_UNKNOWN
12855 && get_tv_number_chk(&argvars[3], &error))
12856 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012857 }
12858 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012859 if (!error)
12860 {
12861 ExpandInit(&xpc);
12862 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012863 if (p_wic)
12864 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012865 if (rettv->v_type == VAR_STRING)
12866 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012867 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012868 else if (rettv_list_alloc(rettv) != FAIL)
12869 {
12870 int i;
12871
12872 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12873 NULL, options, WILD_ALL_KEEP);
12874 for (i = 0; i < xpc.xp_numfiles; i++)
12875 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12876
12877 ExpandCleanup(&xpc);
12878 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012879 }
12880 else
12881 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882}
12883
12884/*
12885 * "globpath()" function
12886 */
12887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012889 typval_T *argvars;
12890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012892 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012894 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012895 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012896 garray_T ga;
12897 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012899 /* When the optional second argument is non-zero, don't remove matches
12900 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012902 if (argvars[2].v_type != VAR_UNKNOWN)
12903 {
12904 if (get_tv_number_chk(&argvars[2], &error))
12905 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012906 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012907 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012908 if (get_tv_number_chk(&argvars[3], &error))
12909 {
12910 rettv->v_type = VAR_LIST;
12911 rettv->vval.v_list = NULL;
12912 }
12913 if (argvars[4].v_type != VAR_UNKNOWN
12914 && get_tv_number_chk(&argvars[4], &error))
12915 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012916 }
12917 }
12918 if (file != NULL && !error)
12919 {
12920 ga_init2(&ga, (int)sizeof(char_u *), 10);
12921 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12922 if (rettv->v_type == VAR_STRING)
12923 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12924 else if (rettv_list_alloc(rettv) != FAIL)
12925 for (i = 0; i < ga.ga_len; ++i)
12926 list_append_string(rettv->vval.v_list,
12927 ((char_u **)(ga.ga_data))[i], -1);
12928 ga_clear_strings(&ga);
12929 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012930 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012931 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932}
12933
12934/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012935 * "glob2regpat()" function
12936 */
12937 static void
12938f_glob2regpat(argvars, rettv)
12939 typval_T *argvars;
12940 typval_T *rettv;
12941{
12942 char_u *pat = get_tv_string_chk(&argvars[0]);
12943
12944 rettv->v_type = VAR_STRING;
12945 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12946}
12947
12948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949 * "has()" function
12950 */
12951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012952f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012953 typval_T *argvars;
12954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955{
12956 int i;
12957 char_u *name;
12958 int n = FALSE;
12959 static char *(has_list[]) =
12960 {
12961#ifdef AMIGA
12962 "amiga",
12963# ifdef FEAT_ARP
12964 "arp",
12965# endif
12966#endif
12967#ifdef __BEOS__
12968 "beos",
12969#endif
12970#ifdef MSDOS
12971# ifdef DJGPP
12972 "dos32",
12973# else
12974 "dos16",
12975# endif
12976#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012977#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978 "mac",
12979#endif
12980#if defined(MACOS_X_UNIX)
12981 "macunix",
12982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983#ifdef __QNX__
12984 "qnx",
12985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986#ifdef UNIX
12987 "unix",
12988#endif
12989#ifdef VMS
12990 "vms",
12991#endif
12992#ifdef WIN16
12993 "win16",
12994#endif
12995#ifdef WIN32
12996 "win32",
12997#endif
12998#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12999 "win32unix",
13000#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013001#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002 "win64",
13003#endif
13004#ifdef EBCDIC
13005 "ebcdic",
13006#endif
13007#ifndef CASE_INSENSITIVE_FILENAME
13008 "fname_case",
13009#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013010#ifdef HAVE_ACL
13011 "acl",
13012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013#ifdef FEAT_ARABIC
13014 "arabic",
13015#endif
13016#ifdef FEAT_AUTOCMD
13017 "autocmd",
13018#endif
13019#ifdef FEAT_BEVAL
13020 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013021# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13022 "balloon_multiline",
13023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024#endif
13025#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13026 "builtin_terms",
13027# ifdef ALL_BUILTIN_TCAPS
13028 "all_builtin_terms",
13029# endif
13030#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013031#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13032 || defined(FEAT_GUI_W32) \
13033 || defined(FEAT_GUI_MOTIF))
13034 "browsefilter",
13035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036#ifdef FEAT_BYTEOFF
13037 "byte_offset",
13038#endif
13039#ifdef FEAT_CINDENT
13040 "cindent",
13041#endif
13042#ifdef FEAT_CLIENTSERVER
13043 "clientserver",
13044#endif
13045#ifdef FEAT_CLIPBOARD
13046 "clipboard",
13047#endif
13048#ifdef FEAT_CMDL_COMPL
13049 "cmdline_compl",
13050#endif
13051#ifdef FEAT_CMDHIST
13052 "cmdline_hist",
13053#endif
13054#ifdef FEAT_COMMENTS
13055 "comments",
13056#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013057#ifdef FEAT_CONCEAL
13058 "conceal",
13059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060#ifdef FEAT_CRYPT
13061 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013062 "crypt-blowfish",
13063 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064#endif
13065#ifdef FEAT_CSCOPE
13066 "cscope",
13067#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013068#ifdef FEAT_CURSORBIND
13069 "cursorbind",
13070#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013071#ifdef CURSOR_SHAPE
13072 "cursorshape",
13073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074#ifdef DEBUG
13075 "debug",
13076#endif
13077#ifdef FEAT_CON_DIALOG
13078 "dialog_con",
13079#endif
13080#ifdef FEAT_GUI_DIALOG
13081 "dialog_gui",
13082#endif
13083#ifdef FEAT_DIFF
13084 "diff",
13085#endif
13086#ifdef FEAT_DIGRAPHS
13087 "digraphs",
13088#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013089#ifdef FEAT_DIRECTX
13090 "directx",
13091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092#ifdef FEAT_DND
13093 "dnd",
13094#endif
13095#ifdef FEAT_EMACS_TAGS
13096 "emacs_tags",
13097#endif
13098 "eval", /* always present, of course! */
13099#ifdef FEAT_EX_EXTRA
13100 "ex_extra",
13101#endif
13102#ifdef FEAT_SEARCH_EXTRA
13103 "extra_search",
13104#endif
13105#ifdef FEAT_FKMAP
13106 "farsi",
13107#endif
13108#ifdef FEAT_SEARCHPATH
13109 "file_in_path",
13110#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013111#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013112 "filterpipe",
13113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114#ifdef FEAT_FIND_ID
13115 "find_in_path",
13116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013117#ifdef FEAT_FLOAT
13118 "float",
13119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120#ifdef FEAT_FOLDING
13121 "folding",
13122#endif
13123#ifdef FEAT_FOOTER
13124 "footer",
13125#endif
13126#if !defined(USE_SYSTEM) && defined(UNIX)
13127 "fork",
13128#endif
13129#ifdef FEAT_GETTEXT
13130 "gettext",
13131#endif
13132#ifdef FEAT_GUI
13133 "gui",
13134#endif
13135#ifdef FEAT_GUI_ATHENA
13136# ifdef FEAT_GUI_NEXTAW
13137 "gui_neXtaw",
13138# else
13139 "gui_athena",
13140# endif
13141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142#ifdef FEAT_GUI_GTK
13143 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013146#ifdef FEAT_GUI_GNOME
13147 "gui_gnome",
13148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149#ifdef FEAT_GUI_MAC
13150 "gui_mac",
13151#endif
13152#ifdef FEAT_GUI_MOTIF
13153 "gui_motif",
13154#endif
13155#ifdef FEAT_GUI_PHOTON
13156 "gui_photon",
13157#endif
13158#ifdef FEAT_GUI_W16
13159 "gui_win16",
13160#endif
13161#ifdef FEAT_GUI_W32
13162 "gui_win32",
13163#endif
13164#ifdef FEAT_HANGULIN
13165 "hangul_input",
13166#endif
13167#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13168 "iconv",
13169#endif
13170#ifdef FEAT_INS_EXPAND
13171 "insert_expand",
13172#endif
13173#ifdef FEAT_JUMPLIST
13174 "jumplist",
13175#endif
13176#ifdef FEAT_KEYMAP
13177 "keymap",
13178#endif
13179#ifdef FEAT_LANGMAP
13180 "langmap",
13181#endif
13182#ifdef FEAT_LIBCALL
13183 "libcall",
13184#endif
13185#ifdef FEAT_LINEBREAK
13186 "linebreak",
13187#endif
13188#ifdef FEAT_LISP
13189 "lispindent",
13190#endif
13191#ifdef FEAT_LISTCMDS
13192 "listcmds",
13193#endif
13194#ifdef FEAT_LOCALMAP
13195 "localmap",
13196#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013197#ifdef FEAT_LUA
13198# ifndef DYNAMIC_LUA
13199 "lua",
13200# endif
13201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202#ifdef FEAT_MENU
13203 "menu",
13204#endif
13205#ifdef FEAT_SESSION
13206 "mksession",
13207#endif
13208#ifdef FEAT_MODIFY_FNAME
13209 "modify_fname",
13210#endif
13211#ifdef FEAT_MOUSE
13212 "mouse",
13213#endif
13214#ifdef FEAT_MOUSESHAPE
13215 "mouseshape",
13216#endif
13217#if defined(UNIX) || defined(VMS)
13218# ifdef FEAT_MOUSE_DEC
13219 "mouse_dec",
13220# endif
13221# ifdef FEAT_MOUSE_GPM
13222 "mouse_gpm",
13223# endif
13224# ifdef FEAT_MOUSE_JSB
13225 "mouse_jsbterm",
13226# endif
13227# ifdef FEAT_MOUSE_NET
13228 "mouse_netterm",
13229# endif
13230# ifdef FEAT_MOUSE_PTERM
13231 "mouse_pterm",
13232# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013233# ifdef FEAT_MOUSE_SGR
13234 "mouse_sgr",
13235# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013236# ifdef FEAT_SYSMOUSE
13237 "mouse_sysmouse",
13238# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013239# ifdef FEAT_MOUSE_URXVT
13240 "mouse_urxvt",
13241# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242# ifdef FEAT_MOUSE_XTERM
13243 "mouse_xterm",
13244# endif
13245#endif
13246#ifdef FEAT_MBYTE
13247 "multi_byte",
13248#endif
13249#ifdef FEAT_MBYTE_IME
13250 "multi_byte_ime",
13251#endif
13252#ifdef FEAT_MULTI_LANG
13253 "multi_lang",
13254#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013255#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013256#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013257 "mzscheme",
13258#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260#ifdef FEAT_OLE
13261 "ole",
13262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263#ifdef FEAT_PATH_EXTRA
13264 "path_extra",
13265#endif
13266#ifdef FEAT_PERL
13267#ifndef DYNAMIC_PERL
13268 "perl",
13269#endif
13270#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013271#ifdef FEAT_PERSISTENT_UNDO
13272 "persistent_undo",
13273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274#ifdef FEAT_PYTHON
13275#ifndef DYNAMIC_PYTHON
13276 "python",
13277#endif
13278#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013279#ifdef FEAT_PYTHON3
13280#ifndef DYNAMIC_PYTHON3
13281 "python3",
13282#endif
13283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284#ifdef FEAT_POSTSCRIPT
13285 "postscript",
13286#endif
13287#ifdef FEAT_PRINTER
13288 "printer",
13289#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013290#ifdef FEAT_PROFILE
13291 "profile",
13292#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013293#ifdef FEAT_RELTIME
13294 "reltime",
13295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296#ifdef FEAT_QUICKFIX
13297 "quickfix",
13298#endif
13299#ifdef FEAT_RIGHTLEFT
13300 "rightleft",
13301#endif
13302#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13303 "ruby",
13304#endif
13305#ifdef FEAT_SCROLLBIND
13306 "scrollbind",
13307#endif
13308#ifdef FEAT_CMDL_INFO
13309 "showcmd",
13310 "cmdline_info",
13311#endif
13312#ifdef FEAT_SIGNS
13313 "signs",
13314#endif
13315#ifdef FEAT_SMARTINDENT
13316 "smartindent",
13317#endif
13318#ifdef FEAT_SNIFF
13319 "sniff",
13320#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013321#ifdef STARTUPTIME
13322 "startuptime",
13323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324#ifdef FEAT_STL_OPT
13325 "statusline",
13326#endif
13327#ifdef FEAT_SUN_WORKSHOP
13328 "sun_workshop",
13329#endif
13330#ifdef FEAT_NETBEANS_INTG
13331 "netbeans_intg",
13332#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013333#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013334 "spell",
13335#endif
13336#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337 "syntax",
13338#endif
13339#if defined(USE_SYSTEM) || !defined(UNIX)
13340 "system",
13341#endif
13342#ifdef FEAT_TAG_BINS
13343 "tag_binary",
13344#endif
13345#ifdef FEAT_TAG_OLDSTATIC
13346 "tag_old_static",
13347#endif
13348#ifdef FEAT_TAG_ANYWHITE
13349 "tag_any_white",
13350#endif
13351#ifdef FEAT_TCL
13352# ifndef DYNAMIC_TCL
13353 "tcl",
13354# endif
13355#endif
13356#ifdef TERMINFO
13357 "terminfo",
13358#endif
13359#ifdef FEAT_TERMRESPONSE
13360 "termresponse",
13361#endif
13362#ifdef FEAT_TEXTOBJ
13363 "textobjects",
13364#endif
13365#ifdef HAVE_TGETENT
13366 "tgetent",
13367#endif
13368#ifdef FEAT_TITLE
13369 "title",
13370#endif
13371#ifdef FEAT_TOOLBAR
13372 "toolbar",
13373#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013374#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13375 "unnamedplus",
13376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377#ifdef FEAT_USR_CMDS
13378 "user-commands", /* was accidentally included in 5.4 */
13379 "user_commands",
13380#endif
13381#ifdef FEAT_VIMINFO
13382 "viminfo",
13383#endif
13384#ifdef FEAT_VERTSPLIT
13385 "vertsplit",
13386#endif
13387#ifdef FEAT_VIRTUALEDIT
13388 "virtualedit",
13389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391#ifdef FEAT_VISUALEXTRA
13392 "visualextra",
13393#endif
13394#ifdef FEAT_VREPLACE
13395 "vreplace",
13396#endif
13397#ifdef FEAT_WILDIGN
13398 "wildignore",
13399#endif
13400#ifdef FEAT_WILDMENU
13401 "wildmenu",
13402#endif
13403#ifdef FEAT_WINDOWS
13404 "windows",
13405#endif
13406#ifdef FEAT_WAK
13407 "winaltkeys",
13408#endif
13409#ifdef FEAT_WRITEBACKUP
13410 "writebackup",
13411#endif
13412#ifdef FEAT_XIM
13413 "xim",
13414#endif
13415#ifdef FEAT_XFONTSET
13416 "xfontset",
13417#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013418#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013419 "xpm",
13420 "xpm_w32", /* for backward compatibility */
13421#else
13422# if defined(HAVE_XPM)
13423 "xpm",
13424# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426#ifdef USE_XSMP
13427 "xsmp",
13428#endif
13429#ifdef USE_XSMP_INTERACT
13430 "xsmp_interact",
13431#endif
13432#ifdef FEAT_XCLIPBOARD
13433 "xterm_clipboard",
13434#endif
13435#ifdef FEAT_XTERM_SAVE
13436 "xterm_save",
13437#endif
13438#if defined(UNIX) && defined(FEAT_X11)
13439 "X11",
13440#endif
13441 NULL
13442 };
13443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013444 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445 for (i = 0; has_list[i] != NULL; ++i)
13446 if (STRICMP(name, has_list[i]) == 0)
13447 {
13448 n = TRUE;
13449 break;
13450 }
13451
13452 if (n == FALSE)
13453 {
13454 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013455 {
13456 if (name[5] == '-'
13457 && STRLEN(name) > 11
13458 && vim_isdigit(name[6])
13459 && vim_isdigit(name[8])
13460 && vim_isdigit(name[10]))
13461 {
13462 int major = atoi((char *)name + 6);
13463 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013464
13465 /* Expect "patch-9.9.01234". */
13466 n = (major < VIM_VERSION_MAJOR
13467 || (major == VIM_VERSION_MAJOR
13468 && (minor < VIM_VERSION_MINOR
13469 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013470 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013471 }
13472 else
13473 n = has_patch(atoi((char *)name + 5));
13474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475 else if (STRICMP(name, "vim_starting") == 0)
13476 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013477#ifdef FEAT_MBYTE
13478 else if (STRICMP(name, "multi_byte_encoding") == 0)
13479 n = has_mbyte;
13480#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013481#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13482 else if (STRICMP(name, "balloon_multiline") == 0)
13483 n = multiline_balloon_available();
13484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485#ifdef DYNAMIC_TCL
13486 else if (STRICMP(name, "tcl") == 0)
13487 n = tcl_enabled(FALSE);
13488#endif
13489#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13490 else if (STRICMP(name, "iconv") == 0)
13491 n = iconv_enabled(FALSE);
13492#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013493#ifdef DYNAMIC_LUA
13494 else if (STRICMP(name, "lua") == 0)
13495 n = lua_enabled(FALSE);
13496#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013497#ifdef DYNAMIC_MZSCHEME
13498 else if (STRICMP(name, "mzscheme") == 0)
13499 n = mzscheme_enabled(FALSE);
13500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501#ifdef DYNAMIC_RUBY
13502 else if (STRICMP(name, "ruby") == 0)
13503 n = ruby_enabled(FALSE);
13504#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013505#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506#ifdef DYNAMIC_PYTHON
13507 else if (STRICMP(name, "python") == 0)
13508 n = python_enabled(FALSE);
13509#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013510#endif
13511#ifdef FEAT_PYTHON3
13512#ifdef DYNAMIC_PYTHON3
13513 else if (STRICMP(name, "python3") == 0)
13514 n = python3_enabled(FALSE);
13515#endif
13516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517#ifdef DYNAMIC_PERL
13518 else if (STRICMP(name, "perl") == 0)
13519 n = perl_enabled(FALSE);
13520#endif
13521#ifdef FEAT_GUI
13522 else if (STRICMP(name, "gui_running") == 0)
13523 n = (gui.in_use || gui.starting);
13524# ifdef FEAT_GUI_W32
13525 else if (STRICMP(name, "gui_win32s") == 0)
13526 n = gui_is_win32s();
13527# endif
13528# ifdef FEAT_BROWSE
13529 else if (STRICMP(name, "browse") == 0)
13530 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13531# endif
13532#endif
13533#ifdef FEAT_SYN_HL
13534 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013535 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536#endif
13537#if defined(WIN3264)
13538 else if (STRICMP(name, "win95") == 0)
13539 n = mch_windows95();
13540#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013541#ifdef FEAT_NETBEANS_INTG
13542 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013543 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 }
13546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013547 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548}
13549
13550/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013551 * "has_key()" function
13552 */
13553 static void
13554f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013555 typval_T *argvars;
13556 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013557{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013558 if (argvars[0].v_type != VAR_DICT)
13559 {
13560 EMSG(_(e_dictreq));
13561 return;
13562 }
13563 if (argvars[0].vval.v_dict == NULL)
13564 return;
13565
13566 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013567 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013568}
13569
13570/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013571 * "haslocaldir()" function
13572 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013573 static void
13574f_haslocaldir(argvars, rettv)
Bram Moolenaarc9703302016-01-17 21:49:33 +010013575 typval_T *argvars;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013576 typval_T *rettv;
13577{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013578 win_T *wp = NULL;
13579
13580 wp = find_tabwin(&argvars[0], &argvars[1]);
13581 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013582}
13583
13584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 * "hasmapto()" function
13586 */
13587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013588f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013589 typval_T *argvars;
13590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591{
13592 char_u *name;
13593 char_u *mode;
13594 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013595 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013598 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 mode = (char_u *)"nvo";
13600 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013602 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013603 if (argvars[2].v_type != VAR_UNKNOWN)
13604 abbr = get_tv_number(&argvars[2]);
13605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606
Bram Moolenaar2c932302006-03-18 21:42:09 +000013607 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013608 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013610 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611}
13612
13613/*
13614 * "histadd()" function
13615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013618 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620{
13621#ifdef FEAT_CMDHIST
13622 int histype;
13623 char_u *str;
13624 char_u buf[NUMBUFLEN];
13625#endif
13626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628 if (check_restricted() || check_secure())
13629 return;
13630#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013631 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13632 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 if (histype >= 0)
13634 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013635 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 if (*str != NUL)
13637 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013638 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 return;
13642 }
13643 }
13644#endif
13645}
13646
13647/*
13648 * "histdel()" function
13649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013652 typval_T *argvars UNUSED;
13653 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654{
13655#ifdef FEAT_CMDHIST
13656 int n;
13657 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013658 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013660 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13661 if (str == NULL)
13662 n = 0;
13663 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013665 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013666 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013668 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013669 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670 else
13671 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013672 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013673 get_tv_string_buf(&argvars[1], buf));
13674 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675#endif
13676}
13677
13678/*
13679 * "histget()" function
13680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013682f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013683 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685{
13686#ifdef FEAT_CMDHIST
13687 int type;
13688 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013689 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013691 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13692 if (str == NULL)
13693 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013695 {
13696 type = get_histtype(str);
13697 if (argvars[1].v_type == VAR_UNKNOWN)
13698 idx = get_history_idx(type);
13699 else
13700 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13701 /* -1 on type error */
13702 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013705 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013707 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708}
13709
13710/*
13711 * "histnr()" function
13712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013714f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013715 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717{
13718 int i;
13719
13720#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013721 char_u *history = get_tv_string_chk(&argvars[0]);
13722
13723 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 if (i >= HIST_CMD && i < HIST_COUNT)
13725 i = get_history_idx(i);
13726 else
13727#endif
13728 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730}
13731
13732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733 * "highlightID(name)" function
13734 */
13735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013736f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013737 typval_T *argvars;
13738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013740 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741}
13742
13743/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013744 * "highlight_exists()" function
13745 */
13746 static void
13747f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013748 typval_T *argvars;
13749 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013750{
13751 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13752}
13753
13754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 * "hostname()" function
13756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013758f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013759 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761{
13762 char_u hostname[256];
13763
13764 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013765 rettv->v_type = VAR_STRING;
13766 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767}
13768
13769/*
13770 * iconv() function
13771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013773f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013774 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776{
13777#ifdef FEAT_MBYTE
13778 char_u buf1[NUMBUFLEN];
13779 char_u buf2[NUMBUFLEN];
13780 char_u *from, *to, *str;
13781 vimconv_T vimconv;
13782#endif
13783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013784 rettv->v_type = VAR_STRING;
13785 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786
13787#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013788 str = get_tv_string(&argvars[0]);
13789 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13790 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791 vimconv.vc_type = CONV_NONE;
13792 convert_setup(&vimconv, from, to);
13793
13794 /* If the encodings are equal, no conversion needed. */
13795 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013796 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013798 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799
13800 convert_setup(&vimconv, NULL, NULL);
13801 vim_free(from);
13802 vim_free(to);
13803#endif
13804}
13805
13806/*
13807 * "indent()" function
13808 */
13809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013810f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013811 typval_T *argvars;
13812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813{
13814 linenr_T lnum;
13815
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013816 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013818 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013820 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821}
13822
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013823/*
13824 * "index()" function
13825 */
13826 static void
13827f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013828 typval_T *argvars;
13829 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013830{
Bram Moolenaar33570922005-01-25 22:26:29 +000013831 list_T *l;
13832 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013833 long idx = 0;
13834 int ic = FALSE;
13835
13836 rettv->vval.v_number = -1;
13837 if (argvars[0].v_type != VAR_LIST)
13838 {
13839 EMSG(_(e_listreq));
13840 return;
13841 }
13842 l = argvars[0].vval.v_list;
13843 if (l != NULL)
13844 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013845 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013846 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013848 int error = FALSE;
13849
Bram Moolenaar758711c2005-02-02 23:11:38 +000013850 /* Start at specified item. Use the cached index that list_find()
13851 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013852 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013853 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013854 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013855 ic = get_tv_number_chk(&argvars[3], &error);
13856 if (error)
13857 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013858 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013859
Bram Moolenaar758711c2005-02-02 23:11:38 +000013860 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013861 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013862 {
13863 rettv->vval.v_number = idx;
13864 break;
13865 }
13866 }
13867}
13868
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869static int inputsecret_flag = 0;
13870
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013871static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13872
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013874 * This function is used by f_input() and f_inputdialog() functions. The third
13875 * argument to f_input() specifies the type of completion to use at the
13876 * prompt. The third argument to f_inputdialog() specifies the value to return
13877 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878 */
13879 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013880get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013881 typval_T *argvars;
13882 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013883 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013885 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 char_u *p = NULL;
13887 int c;
13888 char_u buf[NUMBUFLEN];
13889 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013890 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013891 int xp_type = EXPAND_NOTHING;
13892 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013894 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013895 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896
13897#ifdef NO_CONSOLE_INPUT
13898 /* While starting up, there is no place to enter text. */
13899 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901#endif
13902
13903 cmd_silent = FALSE; /* Want to see the prompt. */
13904 if (prompt != NULL)
13905 {
13906 /* Only the part of the message after the last NL is considered as
13907 * prompt for the command line */
13908 p = vim_strrchr(prompt, '\n');
13909 if (p == NULL)
13910 p = prompt;
13911 else
13912 {
13913 ++p;
13914 c = *p;
13915 *p = NUL;
13916 msg_start();
13917 msg_clr_eos();
13918 msg_puts_attr(prompt, echo_attr);
13919 msg_didout = FALSE;
13920 msg_starthere();
13921 *p = c;
13922 }
13923 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013925 if (argvars[1].v_type != VAR_UNKNOWN)
13926 {
13927 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13928 if (defstr != NULL)
13929 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013931 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013932 {
13933 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013934 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013935 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013936
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013937 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013938 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013939
Bram Moolenaar4463f292005-09-25 22:20:24 +000013940 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13941 if (xp_name == NULL)
13942 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013943
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013944 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013945
Bram Moolenaar4463f292005-09-25 22:20:24 +000013946 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13947 &xp_arg) == FAIL)
13948 return;
13949 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013950 }
13951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013952 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013953 {
13954# ifdef FEAT_EX_EXTRA
13955 int save_ex_normal_busy = ex_normal_busy;
13956 ex_normal_busy = 0;
13957# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013958 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013959 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13960 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013961# ifdef FEAT_EX_EXTRA
13962 ex_normal_busy = save_ex_normal_busy;
13963# endif
13964 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013965 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013966 && argvars[1].v_type != VAR_UNKNOWN
13967 && argvars[2].v_type != VAR_UNKNOWN)
13968 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13969 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013970
13971 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013973 /* since the user typed this, no need to wait for return */
13974 need_wait_return = FALSE;
13975 msg_didout = FALSE;
13976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977 cmd_silent = cmd_silent_save;
13978}
13979
13980/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013981 * "input()" function
13982 * Also handles inputsecret() when inputsecret is set.
13983 */
13984 static void
13985f_input(argvars, rettv)
13986 typval_T *argvars;
13987 typval_T *rettv;
13988{
13989 get_user_input(argvars, rettv, FALSE);
13990}
13991
13992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 * "inputdialog()" function
13994 */
13995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013996f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013997 typval_T *argvars;
13998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999{
14000#if defined(FEAT_GUI_TEXTDIALOG)
14001 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14002 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14003 {
14004 char_u *message;
14005 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014006 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014008 message = get_tv_string_chk(&argvars[0]);
14009 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014010 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014011 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 else
14013 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014014 if (message != NULL && defstr != NULL
14015 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014016 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014017 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 else
14019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014020 if (message != NULL && defstr != NULL
14021 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014022 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023 rettv->vval.v_string = vim_strsave(
14024 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014026 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014028 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029 }
14030 else
14031#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014032 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033}
14034
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014035/*
14036 * "inputlist()" function
14037 */
14038 static void
14039f_inputlist(argvars, rettv)
14040 typval_T *argvars;
14041 typval_T *rettv;
14042{
14043 listitem_T *li;
14044 int selected;
14045 int mouse_used;
14046
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014047#ifdef NO_CONSOLE_INPUT
14048 /* While starting up, there is no place to enter text. */
14049 if (no_console_input())
14050 return;
14051#endif
14052 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14053 {
14054 EMSG2(_(e_listarg), "inputlist()");
14055 return;
14056 }
14057
14058 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014059 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014060 lines_left = Rows; /* avoid more prompt */
14061 msg_scroll = TRUE;
14062 msg_clr_eos();
14063
14064 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14065 {
14066 msg_puts(get_tv_string(&li->li_tv));
14067 msg_putchar('\n');
14068 }
14069
14070 /* Ask for choice. */
14071 selected = prompt_for_number(&mouse_used);
14072 if (mouse_used)
14073 selected -= lines_left;
14074
14075 rettv->vval.v_number = selected;
14076}
14077
14078
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14080
14081/*
14082 * "inputrestore()" function
14083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014085f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014086 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088{
14089 if (ga_userinput.ga_len > 0)
14090 {
14091 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14093 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014094 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095 }
14096 else if (p_verbose > 1)
14097 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014098 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014099 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100 }
14101}
14102
14103/*
14104 * "inputsave()" function
14105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014107f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014108 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014111 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112 if (ga_grow(&ga_userinput, 1) == OK)
14113 {
14114 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14115 + ga_userinput.ga_len);
14116 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014117 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118 }
14119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014120 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121}
14122
14123/*
14124 * "inputsecret()" function
14125 */
14126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014127f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014128 typval_T *argvars;
14129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130{
14131 ++cmdline_star;
14132 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014133 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 --cmdline_star;
14135 --inputsecret_flag;
14136}
14137
14138/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014139 * "insert()" function
14140 */
14141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014142f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014143 typval_T *argvars;
14144 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014145{
14146 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014147 listitem_T *item;
14148 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014150
14151 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014152 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014153 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014154 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014155 {
14156 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014157 before = get_tv_number_chk(&argvars[2], &error);
14158 if (error)
14159 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014160
Bram Moolenaar758711c2005-02-02 23:11:38 +000014161 if (before == l->lv_len)
14162 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014163 else
14164 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014165 item = list_find(l, before);
14166 if (item == NULL)
14167 {
14168 EMSGN(_(e_listidx), before);
14169 l = NULL;
14170 }
14171 }
14172 if (l != NULL)
14173 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014174 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014175 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014176 }
14177 }
14178}
14179
14180/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014181 * "invert(expr)" function
14182 */
14183 static void
14184f_invert(argvars, rettv)
14185 typval_T *argvars;
14186 typval_T *rettv;
14187{
14188 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14189}
14190
14191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192 * "isdirectory()" function
14193 */
14194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014195f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014196 typval_T *argvars;
14197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014199 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200}
14201
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014202/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014203 * "islocked()" function
14204 */
14205 static void
14206f_islocked(argvars, rettv)
14207 typval_T *argvars;
14208 typval_T *rettv;
14209{
14210 lval_T lv;
14211 char_u *end;
14212 dictitem_T *di;
14213
14214 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014215 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14216 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014217 if (end != NULL && lv.ll_name != NULL)
14218 {
14219 if (*end != NUL)
14220 EMSG(_(e_trailing));
14221 else
14222 {
14223 if (lv.ll_tv == NULL)
14224 {
14225 if (check_changedtick(lv.ll_name))
14226 rettv->vval.v_number = 1; /* always locked */
14227 else
14228 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014229 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014230 if (di != NULL)
14231 {
14232 /* Consider a variable locked when:
14233 * 1. the variable itself is locked
14234 * 2. the value of the variable is locked.
14235 * 3. the List or Dict value is locked.
14236 */
14237 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14238 || tv_islocked(&di->di_tv));
14239 }
14240 }
14241 }
14242 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014243 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014244 else if (lv.ll_newkey != NULL)
14245 EMSG2(_(e_dictkey), lv.ll_newkey);
14246 else if (lv.ll_list != NULL)
14247 /* List item. */
14248 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14249 else
14250 /* Dictionary item. */
14251 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14252 }
14253 }
14254
14255 clear_lval(&lv);
14256}
14257
Bram Moolenaar33570922005-01-25 22:26:29 +000014258static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014259
14260/*
14261 * Turn a dict into a list:
14262 * "what" == 0: list of keys
14263 * "what" == 1: list of values
14264 * "what" == 2: list of items
14265 */
14266 static void
14267dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014268 typval_T *argvars;
14269 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014270 int what;
14271{
Bram Moolenaar33570922005-01-25 22:26:29 +000014272 list_T *l2;
14273 dictitem_T *di;
14274 hashitem_T *hi;
14275 listitem_T *li;
14276 listitem_T *li2;
14277 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014278 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014279
Bram Moolenaar8c711452005-01-14 21:53:12 +000014280 if (argvars[0].v_type != VAR_DICT)
14281 {
14282 EMSG(_(e_dictreq));
14283 return;
14284 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014285 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014286 return;
14287
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014288 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014289 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014290
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014291 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014292 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014293 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014294 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014295 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014296 --todo;
14297 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014298
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014299 li = listitem_alloc();
14300 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014301 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014302 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014303
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014304 if (what == 0)
14305 {
14306 /* keys() */
14307 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014308 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014309 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14310 }
14311 else if (what == 1)
14312 {
14313 /* values() */
14314 copy_tv(&di->di_tv, &li->li_tv);
14315 }
14316 else
14317 {
14318 /* items() */
14319 l2 = list_alloc();
14320 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014321 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014322 li->li_tv.vval.v_list = l2;
14323 if (l2 == NULL)
14324 break;
14325 ++l2->lv_refcount;
14326
14327 li2 = listitem_alloc();
14328 if (li2 == NULL)
14329 break;
14330 list_append(l2, li2);
14331 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014332 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014333 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14334
14335 li2 = listitem_alloc();
14336 if (li2 == NULL)
14337 break;
14338 list_append(l2, li2);
14339 copy_tv(&di->di_tv, &li2->li_tv);
14340 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014341 }
14342 }
14343}
14344
14345/*
14346 * "items(dict)" function
14347 */
14348 static void
14349f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014350 typval_T *argvars;
14351 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014352{
14353 dict_list(argvars, rettv, 2);
14354}
14355
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014357 * "join()" function
14358 */
14359 static void
14360f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014361 typval_T *argvars;
14362 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014363{
14364 garray_T ga;
14365 char_u *sep;
14366
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014367 if (argvars[0].v_type != VAR_LIST)
14368 {
14369 EMSG(_(e_listreq));
14370 return;
14371 }
14372 if (argvars[0].vval.v_list == NULL)
14373 return;
14374 if (argvars[1].v_type == VAR_UNKNOWN)
14375 sep = (char_u *)" ";
14376 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014377 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014378
14379 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014380
14381 if (sep != NULL)
14382 {
14383 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014384 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014385 ga_append(&ga, NUL);
14386 rettv->vval.v_string = (char_u *)ga.ga_data;
14387 }
14388 else
14389 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014390}
14391
14392/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014393 * "keys()" function
14394 */
14395 static void
14396f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014397 typval_T *argvars;
14398 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014399{
14400 dict_list(argvars, rettv, 0);
14401}
14402
14403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404 * "last_buffer_nr()" function.
14405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014407f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014408 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410{
14411 int n = 0;
14412 buf_T *buf;
14413
14414 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14415 if (n < buf->b_fnum)
14416 n = buf->b_fnum;
14417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014418 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014419}
14420
14421/*
14422 * "len()" function
14423 */
14424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014425f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014426 typval_T *argvars;
14427 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014428{
14429 switch (argvars[0].v_type)
14430 {
14431 case VAR_STRING:
14432 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433 rettv->vval.v_number = (varnumber_T)STRLEN(
14434 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014435 break;
14436 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014437 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014438 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014439 case VAR_DICT:
14440 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14441 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014442 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014443 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014444 break;
14445 }
14446}
14447
Bram Moolenaar33570922005-01-25 22:26:29 +000014448static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014449
14450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014451libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014452 typval_T *argvars;
14453 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014454 int type;
14455{
14456#ifdef FEAT_LIBCALL
14457 char_u *string_in;
14458 char_u **string_result;
14459 int nr_result;
14460#endif
14461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014462 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014463 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014465
14466 if (check_restricted() || check_secure())
14467 return;
14468
14469#ifdef FEAT_LIBCALL
14470 /* The first two args must be strings, otherwise its meaningless */
14471 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14472 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014473 string_in = NULL;
14474 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014475 string_in = argvars[2].vval.v_string;
14476 if (type == VAR_NUMBER)
14477 string_result = NULL;
14478 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014480 if (mch_libcall(argvars[0].vval.v_string,
14481 argvars[1].vval.v_string,
14482 string_in,
14483 argvars[2].vval.v_number,
14484 string_result,
14485 &nr_result) == OK
14486 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014487 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014488 }
14489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490}
14491
14492/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014493 * "libcall()" function
14494 */
14495 static void
14496f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014497 typval_T *argvars;
14498 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014499{
14500 libcall_common(argvars, rettv, VAR_STRING);
14501}
14502
14503/*
14504 * "libcallnr()" function
14505 */
14506 static void
14507f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014508 typval_T *argvars;
14509 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014510{
14511 libcall_common(argvars, rettv, VAR_NUMBER);
14512}
14513
14514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515 * "line(string)" function
14516 */
14517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014518f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014519 typval_T *argvars;
14520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521{
14522 linenr_T lnum = 0;
14523 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014524 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014526 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527 if (fp != NULL)
14528 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014529 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530}
14531
14532/*
14533 * "line2byte(lnum)" function
14534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014536f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014537 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539{
14540#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014541 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542#else
14543 linenr_T lnum;
14544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014545 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014547 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014549 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14550 if (rettv->vval.v_number >= 0)
14551 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552#endif
14553}
14554
14555/*
14556 * "lispindent(lnum)" function
14557 */
14558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014559f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014560 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562{
14563#ifdef FEAT_LISP
14564 pos_T pos;
14565 linenr_T lnum;
14566
14567 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014568 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14570 {
14571 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014572 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 curwin->w_cursor = pos;
14574 }
14575 else
14576#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014577 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578}
14579
14580/*
14581 * "localtime()" function
14582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014584f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014585 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589}
14590
Bram Moolenaar33570922005-01-25 22:26:29 +000014591static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592
14593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014594get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014595 typval_T *argvars;
14596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597 int exact;
14598{
14599 char_u *keys;
14600 char_u *which;
14601 char_u buf[NUMBUFLEN];
14602 char_u *keys_buf = NULL;
14603 char_u *rhs;
14604 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014605 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014606 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014607 mapblock_T *mp;
14608 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609
14610 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014611 rettv->v_type = VAR_STRING;
14612 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014614 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615 if (*keys == NUL)
14616 return;
14617
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014618 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014620 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014621 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014622 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014623 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014624 if (argvars[3].v_type != VAR_UNKNOWN)
14625 get_dict = get_tv_number(&argvars[3]);
14626 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628 else
14629 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014630 if (which == NULL)
14631 return;
14632
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633 mode = get_map_mode(&which, 0);
14634
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014635 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014636 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014638
14639 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014641 /* Return a string. */
14642 if (rhs != NULL)
14643 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644
Bram Moolenaarbd743252010-10-20 21:23:33 +020014645 }
14646 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14647 {
14648 /* Return a dictionary. */
14649 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14650 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14651 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652
Bram Moolenaarbd743252010-10-20 21:23:33 +020014653 dict_add_nr_str(dict, "lhs", 0L, lhs);
14654 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14655 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14656 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14657 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14658 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14659 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014660 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014661 dict_add_nr_str(dict, "mode", 0L, mapmode);
14662
14663 vim_free(lhs);
14664 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665 }
14666}
14667
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014668#ifdef FEAT_FLOAT
14669/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014670 * "log()" function
14671 */
14672 static void
14673f_log(argvars, rettv)
14674 typval_T *argvars;
14675 typval_T *rettv;
14676{
14677 float_T f;
14678
14679 rettv->v_type = VAR_FLOAT;
14680 if (get_float_arg(argvars, &f) == OK)
14681 rettv->vval.v_float = log(f);
14682 else
14683 rettv->vval.v_float = 0.0;
14684}
14685
14686/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014687 * "log10()" function
14688 */
14689 static void
14690f_log10(argvars, rettv)
14691 typval_T *argvars;
14692 typval_T *rettv;
14693{
14694 float_T f;
14695
14696 rettv->v_type = VAR_FLOAT;
14697 if (get_float_arg(argvars, &f) == OK)
14698 rettv->vval.v_float = log10(f);
14699 else
14700 rettv->vval.v_float = 0.0;
14701}
14702#endif
14703
Bram Moolenaar1dced572012-04-05 16:54:08 +020014704#ifdef FEAT_LUA
14705/*
14706 * "luaeval()" function
14707 */
14708 static void
14709f_luaeval(argvars, rettv)
14710 typval_T *argvars;
14711 typval_T *rettv;
14712{
14713 char_u *str;
14714 char_u buf[NUMBUFLEN];
14715
14716 str = get_tv_string_buf(&argvars[0], buf);
14717 do_luaeval(str, argvars + 1, rettv);
14718}
14719#endif
14720
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014722 * "map()" function
14723 */
14724 static void
14725f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014726 typval_T *argvars;
14727 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014728{
14729 filter_map(argvars, rettv, TRUE);
14730}
14731
14732/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014733 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734 */
14735 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014736f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014737 typval_T *argvars;
14738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014740 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741}
14742
14743/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014744 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 */
14746 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014747f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014748 typval_T *argvars;
14749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014751 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752}
14753
Bram Moolenaar33570922005-01-25 22:26:29 +000014754static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755
14756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014757find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014758 typval_T *argvars;
14759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 int type;
14761{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014762 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014763 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014764 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 char_u *pat;
14766 regmatch_T regmatch;
14767 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014768 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 char_u *save_cpo;
14770 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014771 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014772 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014773 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014774 list_T *l = NULL;
14775 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014776 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014777 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778
14779 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14780 save_cpo = p_cpo;
14781 p_cpo = (char_u *)"";
14782
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014783 rettv->vval.v_number = -1;
14784 if (type == 3)
14785 {
14786 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014787 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014788 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014789 }
14790 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014792 rettv->v_type = VAR_STRING;
14793 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014796 if (argvars[0].v_type == VAR_LIST)
14797 {
14798 if ((l = argvars[0].vval.v_list) == NULL)
14799 goto theend;
14800 li = l->lv_first;
14801 }
14802 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014803 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014804 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014805 len = (long)STRLEN(str);
14806 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014807
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014808 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14809 if (pat == NULL)
14810 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014811
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014812 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014814 int error = FALSE;
14815
14816 start = get_tv_number_chk(&argvars[2], &error);
14817 if (error)
14818 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014819 if (l != NULL)
14820 {
14821 li = list_find(l, start);
14822 if (li == NULL)
14823 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014824 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014825 }
14826 else
14827 {
14828 if (start < 0)
14829 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014830 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014831 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014832 /* When "count" argument is there ignore matches before "start",
14833 * otherwise skip part of the string. Differs when pattern is "^"
14834 * or "\<". */
14835 if (argvars[3].v_type != VAR_UNKNOWN)
14836 startcol = start;
14837 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014838 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014839 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014840 len -= start;
14841 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014842 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014843
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014844 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014845 nth = get_tv_number_chk(&argvars[3], &error);
14846 if (error)
14847 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848 }
14849
14850 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14851 if (regmatch.regprog != NULL)
14852 {
14853 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014854
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014855 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014856 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014857 if (l != NULL)
14858 {
14859 if (li == NULL)
14860 {
14861 match = FALSE;
14862 break;
14863 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014864 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014865 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014866 if (str == NULL)
14867 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014868 }
14869
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014870 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014871
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014872 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014873 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014874 if (l == NULL && !match)
14875 break;
14876
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014877 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014878 if (l != NULL)
14879 {
14880 li = li->li_next;
14881 ++idx;
14882 }
14883 else
14884 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014885#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014886 startcol = (colnr_T)(regmatch.startp[0]
14887 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014888#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014889 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014890#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014891 if (startcol > (colnr_T)len
14892 || str + startcol <= regmatch.startp[0])
14893 {
14894 match = FALSE;
14895 break;
14896 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014897 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014898 }
14899
14900 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014902 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014903 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014904 int i;
14905
14906 /* return list with matched string and submatches */
14907 for (i = 0; i < NSUBEXP; ++i)
14908 {
14909 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014910 {
14911 if (list_append_string(rettv->vval.v_list,
14912 (char_u *)"", 0) == FAIL)
14913 break;
14914 }
14915 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014916 regmatch.startp[i],
14917 (int)(regmatch.endp[i] - regmatch.startp[i]))
14918 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014919 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014920 }
14921 }
14922 else if (type == 2)
14923 {
14924 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014925 if (l != NULL)
14926 copy_tv(&li->li_tv, rettv);
14927 else
14928 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014930 }
14931 else if (l != NULL)
14932 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933 else
14934 {
14935 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014936 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 (varnumber_T)(regmatch.startp[0] - str);
14938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014939 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014941 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 }
14943 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014944 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945 }
14946
14947theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014948 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 p_cpo = save_cpo;
14950}
14951
14952/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014953 * "match()" function
14954 */
14955 static void
14956f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014957 typval_T *argvars;
14958 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014959{
14960 find_some_match(argvars, rettv, 1);
14961}
14962
14963/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014964 * "matchadd()" function
14965 */
14966 static void
14967f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014968 typval_T *argvars UNUSED;
14969 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014970{
14971#ifdef FEAT_SEARCH_EXTRA
14972 char_u buf[NUMBUFLEN];
14973 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14974 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14975 int prio = 10; /* default priority */
14976 int id = -1;
14977 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014978 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014979
14980 rettv->vval.v_number = -1;
14981
14982 if (grp == NULL || pat == NULL)
14983 return;
14984 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014985 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014986 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014987 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014988 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014989 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014990 if (argvars[4].v_type != VAR_UNKNOWN)
14991 {
14992 if (argvars[4].v_type != VAR_DICT)
14993 {
14994 EMSG(_(e_dictreq));
14995 return;
14996 }
14997 if (dict_find(argvars[4].vval.v_dict,
14998 (char_u *)"conceal", -1) != NULL)
14999 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15000 (char_u *)"conceal", FALSE);
15001 }
15002 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015003 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015004 if (error == TRUE)
15005 return;
15006 if (id >= 1 && id <= 3)
15007 {
15008 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15009 return;
15010 }
15011
Bram Moolenaar6561d522015-07-21 15:48:27 +020015012 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15013 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015014#endif
15015}
15016
15017/*
15018 * "matchaddpos()" function
15019 */
15020 static void
15021f_matchaddpos(argvars, rettv)
15022 typval_T *argvars UNUSED;
15023 typval_T *rettv UNUSED;
15024{
15025#ifdef FEAT_SEARCH_EXTRA
15026 char_u buf[NUMBUFLEN];
15027 char_u *group;
15028 int prio = 10;
15029 int id = -1;
15030 int error = FALSE;
15031 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015032 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015033
15034 rettv->vval.v_number = -1;
15035
15036 group = get_tv_string_buf_chk(&argvars[0], buf);
15037 if (group == NULL)
15038 return;
15039
15040 if (argvars[1].v_type != VAR_LIST)
15041 {
15042 EMSG2(_(e_listarg), "matchaddpos()");
15043 return;
15044 }
15045 l = argvars[1].vval.v_list;
15046 if (l == NULL)
15047 return;
15048
15049 if (argvars[2].v_type != VAR_UNKNOWN)
15050 {
15051 prio = get_tv_number_chk(&argvars[2], &error);
15052 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015053 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015054 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015055 if (argvars[4].v_type != VAR_UNKNOWN)
15056 {
15057 if (argvars[4].v_type != VAR_DICT)
15058 {
15059 EMSG(_(e_dictreq));
15060 return;
15061 }
15062 if (dict_find(argvars[4].vval.v_dict,
15063 (char_u *)"conceal", -1) != NULL)
15064 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15065 (char_u *)"conceal", FALSE);
15066 }
15067 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015068 }
15069 if (error == TRUE)
15070 return;
15071
15072 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15073 if (id == 1 || id == 2)
15074 {
15075 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15076 return;
15077 }
15078
Bram Moolenaar6561d522015-07-21 15:48:27 +020015079 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15080 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015081#endif
15082}
15083
15084/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015085 * "matcharg()" function
15086 */
15087 static void
15088f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015089 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015090 typval_T *rettv;
15091{
15092 if (rettv_list_alloc(rettv) == OK)
15093 {
15094#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015095 int id = get_tv_number(&argvars[0]);
15096 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015097
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015098 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015099 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015100 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15101 {
15102 list_append_string(rettv->vval.v_list,
15103 syn_id2name(m->hlg_id), -1);
15104 list_append_string(rettv->vval.v_list, m->pattern, -1);
15105 }
15106 else
15107 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015108 list_append_string(rettv->vval.v_list, NULL, -1);
15109 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015110 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015111 }
15112#endif
15113 }
15114}
15115
15116/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015117 * "matchdelete()" function
15118 */
15119 static void
15120f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015121 typval_T *argvars UNUSED;
15122 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015123{
15124#ifdef FEAT_SEARCH_EXTRA
15125 rettv->vval.v_number = match_delete(curwin,
15126 (int)get_tv_number(&argvars[0]), TRUE);
15127#endif
15128}
15129
15130/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015131 * "matchend()" function
15132 */
15133 static void
15134f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015135 typval_T *argvars;
15136 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015137{
15138 find_some_match(argvars, rettv, 0);
15139}
15140
15141/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015142 * "matchlist()" function
15143 */
15144 static void
15145f_matchlist(argvars, rettv)
15146 typval_T *argvars;
15147 typval_T *rettv;
15148{
15149 find_some_match(argvars, rettv, 3);
15150}
15151
15152/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015153 * "matchstr()" function
15154 */
15155 static void
15156f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015157 typval_T *argvars;
15158 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015159{
15160 find_some_match(argvars, rettv, 2);
15161}
15162
Bram Moolenaar33570922005-01-25 22:26:29 +000015163static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015164
15165 static void
15166max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015167 typval_T *argvars;
15168 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015169 int domax;
15170{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015171 long n = 0;
15172 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015173 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015174
15175 if (argvars[0].v_type == VAR_LIST)
15176 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015177 list_T *l;
15178 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015179
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015180 l = argvars[0].vval.v_list;
15181 if (l != NULL)
15182 {
15183 li = l->lv_first;
15184 if (li != NULL)
15185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015186 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015187 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015188 {
15189 li = li->li_next;
15190 if (li == NULL)
15191 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015192 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015193 if (domax ? i > n : i < n)
15194 n = i;
15195 }
15196 }
15197 }
15198 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015199 else if (argvars[0].v_type == VAR_DICT)
15200 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015201 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015202 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015203 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015204 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015205
15206 d = argvars[0].vval.v_dict;
15207 if (d != NULL)
15208 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015209 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015210 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015211 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015212 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015213 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015214 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015215 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015216 if (first)
15217 {
15218 n = i;
15219 first = FALSE;
15220 }
15221 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015222 n = i;
15223 }
15224 }
15225 }
15226 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015227 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015228 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015229 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015230}
15231
15232/*
15233 * "max()" function
15234 */
15235 static void
15236f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015237 typval_T *argvars;
15238 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015239{
15240 max_min(argvars, rettv, TRUE);
15241}
15242
15243/*
15244 * "min()" function
15245 */
15246 static void
15247f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015248 typval_T *argvars;
15249 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015250{
15251 max_min(argvars, rettv, FALSE);
15252}
15253
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015254static int mkdir_recurse __ARGS((char_u *dir, int prot));
15255
15256/*
15257 * Create the directory in which "dir" is located, and higher levels when
15258 * needed.
15259 */
15260 static int
15261mkdir_recurse(dir, prot)
15262 char_u *dir;
15263 int prot;
15264{
15265 char_u *p;
15266 char_u *updir;
15267 int r = FAIL;
15268
15269 /* Get end of directory name in "dir".
15270 * We're done when it's "/" or "c:/". */
15271 p = gettail_sep(dir);
15272 if (p <= get_past_head(dir))
15273 return OK;
15274
15275 /* If the directory exists we're done. Otherwise: create it.*/
15276 updir = vim_strnsave(dir, (int)(p - dir));
15277 if (updir == NULL)
15278 return FAIL;
15279 if (mch_isdir(updir))
15280 r = OK;
15281 else if (mkdir_recurse(updir, prot) == OK)
15282 r = vim_mkdir_emsg(updir, prot);
15283 vim_free(updir);
15284 return r;
15285}
15286
15287#ifdef vim_mkdir
15288/*
15289 * "mkdir()" function
15290 */
15291 static void
15292f_mkdir(argvars, rettv)
15293 typval_T *argvars;
15294 typval_T *rettv;
15295{
15296 char_u *dir;
15297 char_u buf[NUMBUFLEN];
15298 int prot = 0755;
15299
15300 rettv->vval.v_number = FAIL;
15301 if (check_restricted() || check_secure())
15302 return;
15303
15304 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015305 if (*dir == NUL)
15306 rettv->vval.v_number = FAIL;
15307 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015308 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015309 if (*gettail(dir) == NUL)
15310 /* remove trailing slashes */
15311 *gettail_sep(dir) = NUL;
15312
15313 if (argvars[1].v_type != VAR_UNKNOWN)
15314 {
15315 if (argvars[2].v_type != VAR_UNKNOWN)
15316 prot = get_tv_number_chk(&argvars[2], NULL);
15317 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15318 mkdir_recurse(dir, prot);
15319 }
15320 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015321 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015322}
15323#endif
15324
Bram Moolenaar0d660222005-01-07 21:51:51 +000015325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326 * "mode()" function
15327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015329f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015330 typval_T *argvars;
15331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015333 char_u buf[3];
15334
15335 buf[1] = NUL;
15336 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338 if (VIsual_active)
15339 {
15340 if (VIsual_select)
15341 buf[0] = VIsual_mode + 's' - 'v';
15342 else
15343 buf[0] = VIsual_mode;
15344 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015345 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015346 || State == CONFIRM)
15347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015349 if (State == ASKMORE)
15350 buf[1] = 'm';
15351 else if (State == CONFIRM)
15352 buf[1] = '?';
15353 }
15354 else if (State == EXTERNCMD)
15355 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 else if (State & INSERT)
15357 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015358#ifdef FEAT_VREPLACE
15359 if (State & VREPLACE_FLAG)
15360 {
15361 buf[0] = 'R';
15362 buf[1] = 'v';
15363 }
15364 else
15365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366 if (State & REPLACE_FLAG)
15367 buf[0] = 'R';
15368 else
15369 buf[0] = 'i';
15370 }
15371 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015374 if (exmode_active)
15375 buf[1] = 'v';
15376 }
15377 else if (exmode_active)
15378 {
15379 buf[0] = 'c';
15380 buf[1] = 'e';
15381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015385 if (finish_op)
15386 buf[1] = 'o';
15387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015389 /* Clear out the minor mode when the argument is not a non-zero number or
15390 * non-empty string. */
15391 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015392 buf[1] = NUL;
15393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015394 rettv->vval.v_string = vim_strsave(buf);
15395 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396}
15397
Bram Moolenaar429fa852013-04-15 12:27:36 +020015398#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015399/*
15400 * "mzeval()" function
15401 */
15402 static void
15403f_mzeval(argvars, rettv)
15404 typval_T *argvars;
15405 typval_T *rettv;
15406{
15407 char_u *str;
15408 char_u buf[NUMBUFLEN];
15409
15410 str = get_tv_string_buf(&argvars[0], buf);
15411 do_mzeval(str, rettv);
15412}
Bram Moolenaar75676462013-01-30 14:55:42 +010015413
15414 void
15415mzscheme_call_vim(name, args, rettv)
15416 char_u *name;
15417 typval_T *args;
15418 typval_T *rettv;
15419{
15420 typval_T argvars[3];
15421
15422 argvars[0].v_type = VAR_STRING;
15423 argvars[0].vval.v_string = name;
15424 copy_tv(args, &argvars[1]);
15425 argvars[2].v_type = VAR_UNKNOWN;
15426 f_call(argvars, rettv);
15427 clear_tv(&argvars[1]);
15428}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015429#endif
15430
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015432 * "nextnonblank()" function
15433 */
15434 static void
15435f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015436 typval_T *argvars;
15437 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015438{
15439 linenr_T lnum;
15440
15441 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015443 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015444 {
15445 lnum = 0;
15446 break;
15447 }
15448 if (*skipwhite(ml_get(lnum)) != NUL)
15449 break;
15450 }
15451 rettv->vval.v_number = lnum;
15452}
15453
15454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455 * "nr2char()" function
15456 */
15457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015458f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015459 typval_T *argvars;
15460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461{
15462 char_u buf[NUMBUFLEN];
15463
15464#ifdef FEAT_MBYTE
15465 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015466 {
15467 int utf8 = 0;
15468
15469 if (argvars[1].v_type != VAR_UNKNOWN)
15470 utf8 = get_tv_number_chk(&argvars[1], NULL);
15471 if (utf8)
15472 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15473 else
15474 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476 else
15477#endif
15478 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015479 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480 buf[1] = NUL;
15481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015482 rettv->v_type = VAR_STRING;
15483 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015484}
15485
15486/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015487 * "or(expr, expr)" function
15488 */
15489 static void
15490f_or(argvars, rettv)
15491 typval_T *argvars;
15492 typval_T *rettv;
15493{
15494 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15495 | get_tv_number_chk(&argvars[1], NULL);
15496}
15497
15498/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015499 * "pathshorten()" function
15500 */
15501 static void
15502f_pathshorten(argvars, rettv)
15503 typval_T *argvars;
15504 typval_T *rettv;
15505{
15506 char_u *p;
15507
15508 rettv->v_type = VAR_STRING;
15509 p = get_tv_string_chk(&argvars[0]);
15510 if (p == NULL)
15511 rettv->vval.v_string = NULL;
15512 else
15513 {
15514 p = vim_strsave(p);
15515 rettv->vval.v_string = p;
15516 if (p != NULL)
15517 shorten_dir(p);
15518 }
15519}
15520
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015521#ifdef FEAT_PERL
15522/*
15523 * "perleval()" function
15524 */
15525 static void
15526f_perleval(argvars, rettv)
15527 typval_T *argvars;
15528 typval_T *rettv;
15529{
15530 char_u *str;
15531 char_u buf[NUMBUFLEN];
15532
15533 str = get_tv_string_buf(&argvars[0], buf);
15534 do_perleval(str, rettv);
15535}
15536#endif
15537
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015538#ifdef FEAT_FLOAT
15539/*
15540 * "pow()" function
15541 */
15542 static void
15543f_pow(argvars, rettv)
15544 typval_T *argvars;
15545 typval_T *rettv;
15546{
15547 float_T fx, fy;
15548
15549 rettv->v_type = VAR_FLOAT;
15550 if (get_float_arg(argvars, &fx) == OK
15551 && get_float_arg(&argvars[1], &fy) == OK)
15552 rettv->vval.v_float = pow(fx, fy);
15553 else
15554 rettv->vval.v_float = 0.0;
15555}
15556#endif
15557
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015558/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015559 * "prevnonblank()" function
15560 */
15561 static void
15562f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015563 typval_T *argvars;
15564 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015565{
15566 linenr_T lnum;
15567
15568 lnum = get_tv_lnum(argvars);
15569 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15570 lnum = 0;
15571 else
15572 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15573 --lnum;
15574 rettv->vval.v_number = lnum;
15575}
15576
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015577#ifdef HAVE_STDARG_H
15578/* This dummy va_list is here because:
15579 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15580 * - locally in the function results in a "used before set" warning
15581 * - using va_start() to initialize it gives "function with fixed args" error */
15582static va_list ap;
15583#endif
15584
Bram Moolenaar8c711452005-01-14 21:53:12 +000015585/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015586 * "printf()" function
15587 */
15588 static void
15589f_printf(argvars, rettv)
15590 typval_T *argvars;
15591 typval_T *rettv;
15592{
15593 rettv->v_type = VAR_STRING;
15594 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015595#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015596 {
15597 char_u buf[NUMBUFLEN];
15598 int len;
15599 char_u *s;
15600 int saved_did_emsg = did_emsg;
15601 char *fmt;
15602
15603 /* Get the required length, allocate the buffer and do it for real. */
15604 did_emsg = FALSE;
15605 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015606 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015607 if (!did_emsg)
15608 {
15609 s = alloc(len + 1);
15610 if (s != NULL)
15611 {
15612 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015613 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015614 }
15615 }
15616 did_emsg |= saved_did_emsg;
15617 }
15618#endif
15619}
15620
15621/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015622 * "pumvisible()" function
15623 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015624 static void
15625f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015626 typval_T *argvars UNUSED;
15627 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015628{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015629#ifdef FEAT_INS_EXPAND
15630 if (pum_visible())
15631 rettv->vval.v_number = 1;
15632#endif
15633}
15634
Bram Moolenaardb913952012-06-29 12:54:53 +020015635#ifdef FEAT_PYTHON3
15636/*
15637 * "py3eval()" function
15638 */
15639 static void
15640f_py3eval(argvars, rettv)
15641 typval_T *argvars;
15642 typval_T *rettv;
15643{
15644 char_u *str;
15645 char_u buf[NUMBUFLEN];
15646
15647 str = get_tv_string_buf(&argvars[0], buf);
15648 do_py3eval(str, rettv);
15649}
15650#endif
15651
15652#ifdef FEAT_PYTHON
15653/*
15654 * "pyeval()" function
15655 */
15656 static void
15657f_pyeval(argvars, rettv)
15658 typval_T *argvars;
15659 typval_T *rettv;
15660{
15661 char_u *str;
15662 char_u buf[NUMBUFLEN];
15663
15664 str = get_tv_string_buf(&argvars[0], buf);
15665 do_pyeval(str, rettv);
15666}
15667#endif
15668
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015669/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015670 * "range()" function
15671 */
15672 static void
15673f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015674 typval_T *argvars;
15675 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015676{
15677 long start;
15678 long end;
15679 long stride = 1;
15680 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015681 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015682
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015683 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015684 if (argvars[1].v_type == VAR_UNKNOWN)
15685 {
15686 end = start - 1;
15687 start = 0;
15688 }
15689 else
15690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015691 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015692 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015693 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015694 }
15695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015696 if (error)
15697 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015698 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015699 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015700 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015701 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015702 else
15703 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015704 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015705 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015706 if (list_append_number(rettv->vval.v_list,
15707 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015708 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015709 }
15710}
15711
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015712/*
15713 * "readfile()" function
15714 */
15715 static void
15716f_readfile(argvars, rettv)
15717 typval_T *argvars;
15718 typval_T *rettv;
15719{
15720 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015721 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015722 char_u *fname;
15723 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015724 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15725 int io_size = sizeof(buf);
15726 int readlen; /* size of last fread() */
15727 char_u *prev = NULL; /* previously read bytes, if any */
15728 long prevlen = 0; /* length of data in prev */
15729 long prevsize = 0; /* size of prev buffer */
15730 long maxline = MAXLNUM;
15731 long cnt = 0;
15732 char_u *p; /* position in buf */
15733 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015734
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015735 if (argvars[1].v_type != VAR_UNKNOWN)
15736 {
15737 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15738 binary = TRUE;
15739 if (argvars[2].v_type != VAR_UNKNOWN)
15740 maxline = get_tv_number(&argvars[2]);
15741 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015742
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015743 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015744 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015745
15746 /* Always open the file in binary mode, library functions have a mind of
15747 * their own about CR-LF conversion. */
15748 fname = get_tv_string(&argvars[0]);
15749 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15750 {
15751 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15752 return;
15753 }
15754
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015755 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015756 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015757 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015758
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015759 /* This for loop processes what was read, but is also entered at end
15760 * of file so that either:
15761 * - an incomplete line gets written
15762 * - a "binary" file gets an empty line at the end if it ends in a
15763 * newline. */
15764 for (p = buf, start = buf;
15765 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15766 ++p)
15767 {
15768 if (*p == '\n' || readlen <= 0)
15769 {
15770 listitem_T *li;
15771 char_u *s = NULL;
15772 long_u len = p - start;
15773
15774 /* Finished a line. Remove CRs before NL. */
15775 if (readlen > 0 && !binary)
15776 {
15777 while (len > 0 && start[len - 1] == '\r')
15778 --len;
15779 /* removal may cross back to the "prev" string */
15780 if (len == 0)
15781 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15782 --prevlen;
15783 }
15784 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015785 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015786 else
15787 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015788 /* Change "prev" buffer to be the right size. This way
15789 * the bytes are only copied once, and very long lines are
15790 * allocated only once. */
15791 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015792 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015793 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015794 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015795 prev = NULL; /* the list will own the string */
15796 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015797 }
15798 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015799 if (s == NULL)
15800 {
15801 do_outofmem_msg((long_u) prevlen + len + 1);
15802 failed = TRUE;
15803 break;
15804 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015805
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015806 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015807 {
15808 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015809 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015810 break;
15811 }
15812 li->li_tv.v_type = VAR_STRING;
15813 li->li_tv.v_lock = 0;
15814 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015815 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015816
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015817 start = p + 1; /* step over newline */
15818 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015819 break;
15820 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015821 else if (*p == NUL)
15822 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015823#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015824 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15825 * when finding the BF and check the previous two bytes. */
15826 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015827 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015828 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15829 * + 1, these may be in the "prev" string. */
15830 char_u back1 = p >= buf + 1 ? p[-1]
15831 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15832 char_u back2 = p >= buf + 2 ? p[-2]
15833 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15834 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015835
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015836 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015837 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015838 char_u *dest = p - 2;
15839
15840 /* Usually a BOM is at the beginning of a file, and so at
15841 * the beginning of a line; then we can just step over it.
15842 */
15843 if (start == dest)
15844 start = p + 1;
15845 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015846 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015847 /* have to shuffle buf to close gap */
15848 int adjust_prevlen = 0;
15849
15850 if (dest < buf)
15851 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015852 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015853 dest = buf;
15854 }
15855 if (readlen > p - buf + 1)
15856 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15857 readlen -= 3 - adjust_prevlen;
15858 prevlen -= adjust_prevlen;
15859 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015860 }
15861 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015862 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015863#endif
15864 } /* for */
15865
15866 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15867 break;
15868 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015869 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015870 /* There's part of a line in buf, store it in "prev". */
15871 if (p - start + prevlen >= prevsize)
15872 {
15873 /* need bigger "prev" buffer */
15874 char_u *newprev;
15875
15876 /* A common use case is ordinary text files and "prev" gets a
15877 * fragment of a line, so the first allocation is made
15878 * small, to avoid repeatedly 'allocing' large and
15879 * 'reallocing' small. */
15880 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015881 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015882 else
15883 {
15884 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015885 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015886 prevsize = grow50pc > growmin ? grow50pc : growmin;
15887 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015888 newprev = prev == NULL ? alloc(prevsize)
15889 : vim_realloc(prev, prevsize);
15890 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015891 {
15892 do_outofmem_msg((long_u)prevsize);
15893 failed = TRUE;
15894 break;
15895 }
15896 prev = newprev;
15897 }
15898 /* Add the line part to end of "prev". */
15899 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015900 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015901 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015902 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015903
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015904 /*
15905 * For a negative line count use only the lines at the end of the file,
15906 * free the rest.
15907 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015908 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015909 while (cnt > -maxline)
15910 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015911 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015912 --cnt;
15913 }
15914
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015915 if (failed)
15916 {
15917 list_free(rettv->vval.v_list, TRUE);
15918 /* readfile doc says an empty list is returned on error */
15919 rettv->vval.v_list = list_alloc();
15920 }
15921
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015922 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015923 fclose(fd);
15924}
15925
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015926#if defined(FEAT_RELTIME)
15927static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15928
15929/*
15930 * Convert a List to proftime_T.
15931 * Return FAIL when there is something wrong.
15932 */
15933 static int
15934list2proftime(arg, tm)
15935 typval_T *arg;
15936 proftime_T *tm;
15937{
15938 long n1, n2;
15939 int error = FALSE;
15940
15941 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15942 || arg->vval.v_list->lv_len != 2)
15943 return FAIL;
15944 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15945 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15946# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015947 tm->HighPart = n1;
15948 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015949# else
15950 tm->tv_sec = n1;
15951 tm->tv_usec = n2;
15952# endif
15953 return error ? FAIL : OK;
15954}
15955#endif /* FEAT_RELTIME */
15956
15957/*
15958 * "reltime()" function
15959 */
15960 static void
15961f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015962 typval_T *argvars UNUSED;
15963 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015964{
15965#ifdef FEAT_RELTIME
15966 proftime_T res;
15967 proftime_T start;
15968
15969 if (argvars[0].v_type == VAR_UNKNOWN)
15970 {
15971 /* No arguments: get current time. */
15972 profile_start(&res);
15973 }
15974 else if (argvars[1].v_type == VAR_UNKNOWN)
15975 {
15976 if (list2proftime(&argvars[0], &res) == FAIL)
15977 return;
15978 profile_end(&res);
15979 }
15980 else
15981 {
15982 /* Two arguments: compute the difference. */
15983 if (list2proftime(&argvars[0], &start) == FAIL
15984 || list2proftime(&argvars[1], &res) == FAIL)
15985 return;
15986 profile_sub(&res, &start);
15987 }
15988
15989 if (rettv_list_alloc(rettv) == OK)
15990 {
15991 long n1, n2;
15992
15993# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015994 n1 = res.HighPart;
15995 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015996# else
15997 n1 = res.tv_sec;
15998 n2 = res.tv_usec;
15999# endif
16000 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16001 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16002 }
16003#endif
16004}
16005
16006/*
16007 * "reltimestr()" function
16008 */
16009 static void
16010f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016011 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016012 typval_T *rettv;
16013{
16014#ifdef FEAT_RELTIME
16015 proftime_T tm;
16016#endif
16017
16018 rettv->v_type = VAR_STRING;
16019 rettv->vval.v_string = NULL;
16020#ifdef FEAT_RELTIME
16021 if (list2proftime(&argvars[0], &tm) == OK)
16022 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16023#endif
16024}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016025
Bram Moolenaar0d660222005-01-07 21:51:51 +000016026#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
16027static void make_connection __ARGS((void));
16028static int check_connection __ARGS((void));
16029
16030 static void
16031make_connection()
16032{
16033 if (X_DISPLAY == NULL
16034# ifdef FEAT_GUI
16035 && !gui.in_use
16036# endif
16037 )
16038 {
16039 x_force_connect = TRUE;
16040 setup_term_clip();
16041 x_force_connect = FALSE;
16042 }
16043}
16044
16045 static int
16046check_connection()
16047{
16048 make_connection();
16049 if (X_DISPLAY == NULL)
16050 {
16051 EMSG(_("E240: No connection to Vim server"));
16052 return FAIL;
16053 }
16054 return OK;
16055}
16056#endif
16057
16058#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016059static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016060
16061 static void
16062remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000016063 typval_T *argvars;
16064 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016065 int expr;
16066{
16067 char_u *server_name;
16068 char_u *keys;
16069 char_u *r = NULL;
16070 char_u buf[NUMBUFLEN];
16071# ifdef WIN32
16072 HWND w;
16073# else
16074 Window w;
16075# endif
16076
16077 if (check_restricted() || check_secure())
16078 return;
16079
16080# ifdef FEAT_X11
16081 if (check_connection() == FAIL)
16082 return;
16083# endif
16084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016085 server_name = get_tv_string_chk(&argvars[0]);
16086 if (server_name == NULL)
16087 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016088 keys = get_tv_string_buf(&argvars[1], buf);
16089# ifdef WIN32
16090 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16091# else
16092 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16093 < 0)
16094# endif
16095 {
16096 if (r != NULL)
16097 EMSG(r); /* sending worked but evaluation failed */
16098 else
16099 EMSG2(_("E241: Unable to send to %s"), server_name);
16100 return;
16101 }
16102
16103 rettv->vval.v_string = r;
16104
16105 if (argvars[2].v_type != VAR_UNKNOWN)
16106 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016107 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016108 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016109 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016110
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016111 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016112 v.di_tv.v_type = VAR_STRING;
16113 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016114 idvar = get_tv_string_chk(&argvars[2]);
16115 if (idvar != NULL)
16116 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016117 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016118 }
16119}
16120#endif
16121
16122/*
16123 * "remote_expr()" function
16124 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016125 static void
16126f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016127 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016128 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016129{
16130 rettv->v_type = VAR_STRING;
16131 rettv->vval.v_string = NULL;
16132#ifdef FEAT_CLIENTSERVER
16133 remote_common(argvars, rettv, TRUE);
16134#endif
16135}
16136
16137/*
16138 * "remote_foreground()" function
16139 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016140 static void
16141f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016142 typval_T *argvars UNUSED;
16143 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016144{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016145#ifdef FEAT_CLIENTSERVER
16146# ifdef WIN32
16147 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016148 {
16149 char_u *server_name = get_tv_string_chk(&argvars[0]);
16150
16151 if (server_name != NULL)
16152 serverForeground(server_name);
16153 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016154# else
16155 /* Send a foreground() expression to the server. */
16156 argvars[1].v_type = VAR_STRING;
16157 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16158 argvars[2].v_type = VAR_UNKNOWN;
16159 remote_common(argvars, rettv, TRUE);
16160 vim_free(argvars[1].vval.v_string);
16161# endif
16162#endif
16163}
16164
Bram Moolenaar0d660222005-01-07 21:51:51 +000016165 static void
16166f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016167 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016168 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016169{
16170#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016171 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016172 char_u *s = NULL;
16173# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016174 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016175# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016176 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016177
16178 if (check_restricted() || check_secure())
16179 {
16180 rettv->vval.v_number = -1;
16181 return;
16182 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016183 serverid = get_tv_string_chk(&argvars[0]);
16184 if (serverid == NULL)
16185 {
16186 rettv->vval.v_number = -1;
16187 return; /* type error; errmsg already given */
16188 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016189# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016190 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016191 if (n == 0)
16192 rettv->vval.v_number = -1;
16193 else
16194 {
16195 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16196 rettv->vval.v_number = (s != NULL);
16197 }
16198# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016199 if (check_connection() == FAIL)
16200 return;
16201
16202 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016203 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016204# endif
16205
16206 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016208 char_u *retvar;
16209
Bram Moolenaar33570922005-01-25 22:26:29 +000016210 v.di_tv.v_type = VAR_STRING;
16211 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016212 retvar = get_tv_string_chk(&argvars[1]);
16213 if (retvar != NULL)
16214 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016215 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016216 }
16217#else
16218 rettv->vval.v_number = -1;
16219#endif
16220}
16221
Bram Moolenaar0d660222005-01-07 21:51:51 +000016222 static void
16223f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016224 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016225 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016226{
16227 char_u *r = NULL;
16228
16229#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016230 char_u *serverid = get_tv_string_chk(&argvars[0]);
16231
16232 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016233 {
16234# ifdef WIN32
16235 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016236 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016237
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016238 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016239 if (n != 0)
16240 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16241 if (r == NULL)
16242# else
16243 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016244 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016245# endif
16246 EMSG(_("E277: Unable to read a server reply"));
16247 }
16248#endif
16249 rettv->v_type = VAR_STRING;
16250 rettv->vval.v_string = r;
16251}
16252
16253/*
16254 * "remote_send()" function
16255 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016256 static void
16257f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016258 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016259 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016260{
16261 rettv->v_type = VAR_STRING;
16262 rettv->vval.v_string = NULL;
16263#ifdef FEAT_CLIENTSERVER
16264 remote_common(argvars, rettv, FALSE);
16265#endif
16266}
16267
16268/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016269 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016270 */
16271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016272f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016273 typval_T *argvars;
16274 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016275{
Bram Moolenaar33570922005-01-25 22:26:29 +000016276 list_T *l;
16277 listitem_T *item, *item2;
16278 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016279 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016280 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016281 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016282 dict_T *d;
16283 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016284 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016285
Bram Moolenaar8c711452005-01-14 21:53:12 +000016286 if (argvars[0].v_type == VAR_DICT)
16287 {
16288 if (argvars[2].v_type != VAR_UNKNOWN)
16289 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016290 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016291 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016293 key = get_tv_string_chk(&argvars[1]);
16294 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016296 di = dict_find(d, key, -1);
16297 if (di == NULL)
16298 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016299 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16300 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016301 {
16302 *rettv = di->di_tv;
16303 init_tv(&di->di_tv);
16304 dictitem_remove(d, di);
16305 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016306 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016307 }
16308 }
16309 else if (argvars[0].v_type != VAR_LIST)
16310 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016311 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016312 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016314 int error = FALSE;
16315
16316 idx = get_tv_number_chk(&argvars[1], &error);
16317 if (error)
16318 ; /* type error: do nothing, errmsg already given */
16319 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016320 EMSGN(_(e_listidx), idx);
16321 else
16322 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016323 if (argvars[2].v_type == VAR_UNKNOWN)
16324 {
16325 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016326 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016327 *rettv = item->li_tv;
16328 vim_free(item);
16329 }
16330 else
16331 {
16332 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016333 end = get_tv_number_chk(&argvars[2], &error);
16334 if (error)
16335 ; /* type error: do nothing */
16336 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016337 EMSGN(_(e_listidx), end);
16338 else
16339 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016340 int cnt = 0;
16341
16342 for (li = item; li != NULL; li = li->li_next)
16343 {
16344 ++cnt;
16345 if (li == item2)
16346 break;
16347 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016348 if (li == NULL) /* didn't find "item2" after "item" */
16349 EMSG(_(e_invrange));
16350 else
16351 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016352 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016353 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016354 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016355 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016356 l->lv_first = item;
16357 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016358 item->li_prev = NULL;
16359 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016360 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016361 }
16362 }
16363 }
16364 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016365 }
16366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367}
16368
16369/*
16370 * "rename({from}, {to})" function
16371 */
16372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016373f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016374 typval_T *argvars;
16375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376{
16377 char_u buf[NUMBUFLEN];
16378
16379 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016380 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016382 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16383 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384}
16385
16386/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016387 * "repeat()" function
16388 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016389 static void
16390f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016391 typval_T *argvars;
16392 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016393{
16394 char_u *p;
16395 int n;
16396 int slen;
16397 int len;
16398 char_u *r;
16399 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016400
16401 n = get_tv_number(&argvars[1]);
16402 if (argvars[0].v_type == VAR_LIST)
16403 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016404 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016405 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016406 if (list_extend(rettv->vval.v_list,
16407 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016408 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016409 }
16410 else
16411 {
16412 p = get_tv_string(&argvars[0]);
16413 rettv->v_type = VAR_STRING;
16414 rettv->vval.v_string = NULL;
16415
16416 slen = (int)STRLEN(p);
16417 len = slen * n;
16418 if (len <= 0)
16419 return;
16420
16421 r = alloc(len + 1);
16422 if (r != NULL)
16423 {
16424 for (i = 0; i < n; i++)
16425 mch_memmove(r + i * slen, p, (size_t)slen);
16426 r[len] = NUL;
16427 }
16428
16429 rettv->vval.v_string = r;
16430 }
16431}
16432
16433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434 * "resolve()" function
16435 */
16436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016437f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016438 typval_T *argvars;
16439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440{
16441 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016442#ifdef HAVE_READLINK
16443 char_u *buf = NULL;
16444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016446 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447#ifdef FEAT_SHORTCUT
16448 {
16449 char_u *v = NULL;
16450
16451 v = mch_resolve_shortcut(p);
16452 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016453 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016455 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456 }
16457#else
16458# ifdef HAVE_READLINK
16459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 char_u *cpy;
16461 int len;
16462 char_u *remain = NULL;
16463 char_u *q;
16464 int is_relative_to_current = FALSE;
16465 int has_trailing_pathsep = FALSE;
16466 int limit = 100;
16467
16468 p = vim_strsave(p);
16469
16470 if (p[0] == '.' && (vim_ispathsep(p[1])
16471 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16472 is_relative_to_current = TRUE;
16473
16474 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016475 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016476 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016478 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480
16481 q = getnextcomp(p);
16482 if (*q != NUL)
16483 {
16484 /* Separate the first path component in "p", and keep the
16485 * remainder (beginning with the path separator). */
16486 remain = vim_strsave(q - 1);
16487 q[-1] = NUL;
16488 }
16489
Bram Moolenaard9462e32011-04-11 21:35:11 +020016490 buf = alloc(MAXPATHL + 1);
16491 if (buf == NULL)
16492 goto fail;
16493
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 for (;;)
16495 {
16496 for (;;)
16497 {
16498 len = readlink((char *)p, (char *)buf, MAXPATHL);
16499 if (len <= 0)
16500 break;
16501 buf[len] = NUL;
16502
16503 if (limit-- == 0)
16504 {
16505 vim_free(p);
16506 vim_free(remain);
16507 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016508 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509 goto fail;
16510 }
16511
16512 /* Ensure that the result will have a trailing path separator
16513 * if the argument has one. */
16514 if (remain == NULL && has_trailing_pathsep)
16515 add_pathsep(buf);
16516
16517 /* Separate the first path component in the link value and
16518 * concatenate the remainders. */
16519 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16520 if (*q != NUL)
16521 {
16522 if (remain == NULL)
16523 remain = vim_strsave(q - 1);
16524 else
16525 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016526 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016527 if (cpy != NULL)
16528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 vim_free(remain);
16530 remain = cpy;
16531 }
16532 }
16533 q[-1] = NUL;
16534 }
16535
16536 q = gettail(p);
16537 if (q > p && *q == NUL)
16538 {
16539 /* Ignore trailing path separator. */
16540 q[-1] = NUL;
16541 q = gettail(p);
16542 }
16543 if (q > p && !mch_isFullName(buf))
16544 {
16545 /* symlink is relative to directory of argument */
16546 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16547 if (cpy != NULL)
16548 {
16549 STRCPY(cpy, p);
16550 STRCPY(gettail(cpy), buf);
16551 vim_free(p);
16552 p = cpy;
16553 }
16554 }
16555 else
16556 {
16557 vim_free(p);
16558 p = vim_strsave(buf);
16559 }
16560 }
16561
16562 if (remain == NULL)
16563 break;
16564
16565 /* Append the first path component of "remain" to "p". */
16566 q = getnextcomp(remain + 1);
16567 len = q - remain - (*q != NUL);
16568 cpy = vim_strnsave(p, STRLEN(p) + len);
16569 if (cpy != NULL)
16570 {
16571 STRNCAT(cpy, remain, len);
16572 vim_free(p);
16573 p = cpy;
16574 }
16575 /* Shorten "remain". */
16576 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016577 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578 else
16579 {
16580 vim_free(remain);
16581 remain = NULL;
16582 }
16583 }
16584
16585 /* If the result is a relative path name, make it explicitly relative to
16586 * the current directory if and only if the argument had this form. */
16587 if (!vim_ispathsep(*p))
16588 {
16589 if (is_relative_to_current
16590 && *p != NUL
16591 && !(p[0] == '.'
16592 && (p[1] == NUL
16593 || vim_ispathsep(p[1])
16594 || (p[1] == '.'
16595 && (p[2] == NUL
16596 || vim_ispathsep(p[2]))))))
16597 {
16598 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016599 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600 if (cpy != NULL)
16601 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602 vim_free(p);
16603 p = cpy;
16604 }
16605 }
16606 else if (!is_relative_to_current)
16607 {
16608 /* Strip leading "./". */
16609 q = p;
16610 while (q[0] == '.' && vim_ispathsep(q[1]))
16611 q += 2;
16612 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016613 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614 }
16615 }
16616
16617 /* Ensure that the result will have no trailing path separator
16618 * if the argument had none. But keep "/" or "//". */
16619 if (!has_trailing_pathsep)
16620 {
16621 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016622 if (after_pathsep(p, q))
16623 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624 }
16625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016626 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627 }
16628# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016629 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630# endif
16631#endif
16632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016633 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634
16635#ifdef HAVE_READLINK
16636fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016637 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016639 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640}
16641
16642/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016643 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 */
16645 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016646f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016647 typval_T *argvars;
16648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649{
Bram Moolenaar33570922005-01-25 22:26:29 +000016650 list_T *l;
16651 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652
Bram Moolenaar0d660222005-01-07 21:51:51 +000016653 if (argvars[0].v_type != VAR_LIST)
16654 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016655 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016656 && !tv_check_lock(l->lv_lock,
16657 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016658 {
16659 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016660 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016661 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016662 while (li != NULL)
16663 {
16664 ni = li->li_prev;
16665 list_append(l, li);
16666 li = ni;
16667 }
16668 rettv->vval.v_list = l;
16669 rettv->v_type = VAR_LIST;
16670 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016671 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673}
16674
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016675#define SP_NOMOVE 0x01 /* don't move cursor */
16676#define SP_REPEAT 0x02 /* repeat to find outer pair */
16677#define SP_RETCOUNT 0x04 /* return matchcount */
16678#define SP_SETPCMARK 0x08 /* set previous context mark */
16679#define SP_START 0x10 /* accept match at start position */
16680#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16681#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016682#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016683
Bram Moolenaar33570922005-01-25 22:26:29 +000016684static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016685
16686/*
16687 * Get flags for a search function.
16688 * Possibly sets "p_ws".
16689 * Returns BACKWARD, FORWARD or zero (for an error).
16690 */
16691 static int
16692get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016693 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016694 int *flagsp;
16695{
16696 int dir = FORWARD;
16697 char_u *flags;
16698 char_u nbuf[NUMBUFLEN];
16699 int mask;
16700
16701 if (varp->v_type != VAR_UNKNOWN)
16702 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016703 flags = get_tv_string_buf_chk(varp, nbuf);
16704 if (flags == NULL)
16705 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016706 while (*flags != NUL)
16707 {
16708 switch (*flags)
16709 {
16710 case 'b': dir = BACKWARD; break;
16711 case 'w': p_ws = TRUE; break;
16712 case 'W': p_ws = FALSE; break;
16713 default: mask = 0;
16714 if (flagsp != NULL)
16715 switch (*flags)
16716 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016717 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016718 case 'e': mask = SP_END; break;
16719 case 'm': mask = SP_RETCOUNT; break;
16720 case 'n': mask = SP_NOMOVE; break;
16721 case 'p': mask = SP_SUBPAT; break;
16722 case 'r': mask = SP_REPEAT; break;
16723 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016724 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016725 }
16726 if (mask == 0)
16727 {
16728 EMSG2(_(e_invarg2), flags);
16729 dir = 0;
16730 }
16731 else
16732 *flagsp |= mask;
16733 }
16734 if (dir == 0)
16735 break;
16736 ++flags;
16737 }
16738 }
16739 return dir;
16740}
16741
Bram Moolenaar071d4272004-06-13 20:20:40 +000016742/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016743 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016745 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016746search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016747 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016748 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016749 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016751 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752 char_u *pat;
16753 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016754 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755 int save_p_ws = p_ws;
16756 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016757 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016758 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016759 proftime_T tm;
16760#ifdef FEAT_RELTIME
16761 long time_limit = 0;
16762#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016763 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016764 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016766 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016767 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016768 if (dir == 0)
16769 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016770 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016771 if (flags & SP_START)
16772 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016773 if (flags & SP_END)
16774 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016775 if (flags & SP_COLUMN)
16776 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016777
Bram Moolenaar76929292008-01-06 19:07:36 +000016778 /* Optional arguments: line number to stop searching and timeout. */
16779 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016780 {
16781 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16782 if (lnum_stop < 0)
16783 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016784#ifdef FEAT_RELTIME
16785 if (argvars[3].v_type != VAR_UNKNOWN)
16786 {
16787 time_limit = get_tv_number_chk(&argvars[3], NULL);
16788 if (time_limit < 0)
16789 goto theend;
16790 }
16791#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016792 }
16793
Bram Moolenaar76929292008-01-06 19:07:36 +000016794#ifdef FEAT_RELTIME
16795 /* Set the time limit, if there is one. */
16796 profile_setlimit(time_limit, &tm);
16797#endif
16798
Bram Moolenaar231334e2005-07-25 20:46:57 +000016799 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016800 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016801 * Check to make sure only those flags are set.
16802 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16803 * flags cannot be set. Check for that condition also.
16804 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016805 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016806 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016807 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016808 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016809 goto theend;
16810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016812 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016813 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016814 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016815 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016817 if (flags & SP_SUBPAT)
16818 retval = subpatnum;
16819 else
16820 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016821 if (flags & SP_SETPCMARK)
16822 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016824 if (match_pos != NULL)
16825 {
16826 /* Store the match cursor position */
16827 match_pos->lnum = pos.lnum;
16828 match_pos->col = pos.col + 1;
16829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830 /* "/$" will put the cursor after the end of the line, may need to
16831 * correct that here */
16832 check_cursor();
16833 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016834
16835 /* If 'n' flag is used: restore cursor position. */
16836 if (flags & SP_NOMOVE)
16837 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016838 else
16839 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016840theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016842
16843 return retval;
16844}
16845
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016846#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016847
16848/*
16849 * round() is not in C90, use ceil() or floor() instead.
16850 */
16851 float_T
16852vim_round(f)
16853 float_T f;
16854{
16855 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16856}
16857
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016858/*
16859 * "round({float})" function
16860 */
16861 static void
16862f_round(argvars, rettv)
16863 typval_T *argvars;
16864 typval_T *rettv;
16865{
16866 float_T f;
16867
16868 rettv->v_type = VAR_FLOAT;
16869 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016870 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016871 else
16872 rettv->vval.v_float = 0.0;
16873}
16874#endif
16875
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016876/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016877 * "screenattr()" function
16878 */
16879 static void
16880f_screenattr(argvars, rettv)
16881 typval_T *argvars UNUSED;
16882 typval_T *rettv;
16883{
16884 int row;
16885 int col;
16886 int c;
16887
16888 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16889 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16890 if (row < 0 || row >= screen_Rows
16891 || col < 0 || col >= screen_Columns)
16892 c = -1;
16893 else
16894 c = ScreenAttrs[LineOffset[row] + col];
16895 rettv->vval.v_number = c;
16896}
16897
16898/*
16899 * "screenchar()" function
16900 */
16901 static void
16902f_screenchar(argvars, rettv)
16903 typval_T *argvars UNUSED;
16904 typval_T *rettv;
16905{
16906 int row;
16907 int col;
16908 int off;
16909 int c;
16910
16911 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16912 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16913 if (row < 0 || row >= screen_Rows
16914 || col < 0 || col >= screen_Columns)
16915 c = -1;
16916 else
16917 {
16918 off = LineOffset[row] + col;
16919#ifdef FEAT_MBYTE
16920 if (enc_utf8 && ScreenLinesUC[off] != 0)
16921 c = ScreenLinesUC[off];
16922 else
16923#endif
16924 c = ScreenLines[off];
16925 }
16926 rettv->vval.v_number = c;
16927}
16928
16929/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016930 * "screencol()" function
16931 *
16932 * First column is 1 to be consistent with virtcol().
16933 */
16934 static void
16935f_screencol(argvars, rettv)
16936 typval_T *argvars UNUSED;
16937 typval_T *rettv;
16938{
16939 rettv->vval.v_number = screen_screencol() + 1;
16940}
16941
16942/*
16943 * "screenrow()" function
16944 */
16945 static void
16946f_screenrow(argvars, rettv)
16947 typval_T *argvars UNUSED;
16948 typval_T *rettv;
16949{
16950 rettv->vval.v_number = screen_screenrow() + 1;
16951}
16952
16953/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016954 * "search()" function
16955 */
16956 static void
16957f_search(argvars, rettv)
16958 typval_T *argvars;
16959 typval_T *rettv;
16960{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016961 int flags = 0;
16962
16963 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964}
16965
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016967 * "searchdecl()" function
16968 */
16969 static void
16970f_searchdecl(argvars, rettv)
16971 typval_T *argvars;
16972 typval_T *rettv;
16973{
16974 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016975 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016976 int error = FALSE;
16977 char_u *name;
16978
16979 rettv->vval.v_number = 1; /* default: FAIL */
16980
16981 name = get_tv_string_chk(&argvars[0]);
16982 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016983 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016984 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016985 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16986 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16987 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016988 if (!error && name != NULL)
16989 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016990 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016991}
16992
16993/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016994 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016996 static int
16997searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016998 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016999 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000{
17001 char_u *spat, *mpat, *epat;
17002 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 int dir;
17005 int flags = 0;
17006 char_u nbuf1[NUMBUFLEN];
17007 char_u nbuf2[NUMBUFLEN];
17008 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017009 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017010 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017011 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017014 spat = get_tv_string_chk(&argvars[0]);
17015 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17016 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17017 if (spat == NULL || mpat == NULL || epat == NULL)
17018 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 /* Handle the optional fourth argument: flags */
17021 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017022 if (dir == 0)
17023 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017024
17025 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017026 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17027 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017028 if ((flags & (SP_END | SP_SUBPAT)) != 0
17029 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017030 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017031 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017032 goto theend;
17033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017035 /* Using 'r' implies 'W', otherwise it doesn't work. */
17036 if (flags & SP_REPEAT)
17037 p_ws = FALSE;
17038
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017039 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017040 if (argvars[3].v_type == VAR_UNKNOWN
17041 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 skip = (char_u *)"";
17043 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017045 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017046 if (argvars[5].v_type != VAR_UNKNOWN)
17047 {
17048 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17049 if (lnum_stop < 0)
17050 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017051#ifdef FEAT_RELTIME
17052 if (argvars[6].v_type != VAR_UNKNOWN)
17053 {
17054 time_limit = get_tv_number_chk(&argvars[6], NULL);
17055 if (time_limit < 0)
17056 goto theend;
17057 }
17058#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017059 }
17060 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017061 if (skip == NULL)
17062 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017064 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017065 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017066
17067theend:
17068 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017069
17070 return retval;
17071}
17072
17073/*
17074 * "searchpair()" function
17075 */
17076 static void
17077f_searchpair(argvars, rettv)
17078 typval_T *argvars;
17079 typval_T *rettv;
17080{
17081 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17082}
17083
17084/*
17085 * "searchpairpos()" function
17086 */
17087 static void
17088f_searchpairpos(argvars, rettv)
17089 typval_T *argvars;
17090 typval_T *rettv;
17091{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017092 pos_T match_pos;
17093 int lnum = 0;
17094 int col = 0;
17095
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017096 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017097 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017098
17099 if (searchpair_cmn(argvars, &match_pos) > 0)
17100 {
17101 lnum = match_pos.lnum;
17102 col = match_pos.col;
17103 }
17104
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017105 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17106 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017107}
17108
17109/*
17110 * Search for a start/middle/end thing.
17111 * Used by searchpair(), see its documentation for the details.
17112 * Returns 0 or -1 for no match,
17113 */
17114 long
Bram Moolenaar76929292008-01-06 19:07:36 +000017115do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
17116 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017117 char_u *spat; /* start pattern */
17118 char_u *mpat; /* middle pattern */
17119 char_u *epat; /* end pattern */
17120 int dir; /* BACKWARD or FORWARD */
17121 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017122 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017123 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017124 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017125 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017126{
17127 char_u *save_cpo;
17128 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17129 long retval = 0;
17130 pos_T pos;
17131 pos_T firstpos;
17132 pos_T foundpos;
17133 pos_T save_cursor;
17134 pos_T save_pos;
17135 int n;
17136 int r;
17137 int nest = 1;
17138 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017139 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017140 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017141
17142 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17143 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017144 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017145
Bram Moolenaar76929292008-01-06 19:07:36 +000017146#ifdef FEAT_RELTIME
17147 /* Set the time limit, if there is one. */
17148 profile_setlimit(time_limit, &tm);
17149#endif
17150
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017151 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17152 * start/middle/end (pat3, for the top pair). */
17153 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17154 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17155 if (pat2 == NULL || pat3 == NULL)
17156 goto theend;
17157 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17158 if (*mpat == NUL)
17159 STRCPY(pat3, pat2);
17160 else
17161 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17162 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017163 if (flags & SP_START)
17164 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017165
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 save_cursor = curwin->w_cursor;
17167 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017168 clearpos(&firstpos);
17169 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 pat = pat3;
17171 for (;;)
17172 {
17173 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017174 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17176 /* didn't find it or found the first match again: FAIL */
17177 break;
17178
17179 if (firstpos.lnum == 0)
17180 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017181 if (equalpos(pos, foundpos))
17182 {
17183 /* Found the same position again. Can happen with a pattern that
17184 * has "\zs" at the end and searching backwards. Advance one
17185 * character and try again. */
17186 if (dir == BACKWARD)
17187 decl(&pos);
17188 else
17189 incl(&pos);
17190 }
17191 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017193 /* clear the start flag to avoid getting stuck here */
17194 options &= ~SEARCH_START;
17195
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 /* If the skip pattern matches, ignore this match. */
17197 if (*skip != NUL)
17198 {
17199 save_pos = curwin->w_cursor;
17200 curwin->w_cursor = pos;
17201 r = eval_to_bool(skip, &err, NULL, FALSE);
17202 curwin->w_cursor = save_pos;
17203 if (err)
17204 {
17205 /* Evaluating {skip} caused an error, break here. */
17206 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017207 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 break;
17209 }
17210 if (r)
17211 continue;
17212 }
17213
17214 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17215 {
17216 /* Found end when searching backwards or start when searching
17217 * forward: nested pair. */
17218 ++nest;
17219 pat = pat2; /* nested, don't search for middle */
17220 }
17221 else
17222 {
17223 /* Found end when searching forward or start when searching
17224 * backward: end of (nested) pair; or found middle in outer pair. */
17225 if (--nest == 1)
17226 pat = pat3; /* outer level, search for middle */
17227 }
17228
17229 if (nest == 0)
17230 {
17231 /* Found the match: return matchcount or line number. */
17232 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017233 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017235 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017236 if (flags & SP_SETPCMARK)
17237 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238 curwin->w_cursor = pos;
17239 if (!(flags & SP_REPEAT))
17240 break;
17241 nest = 1; /* search for next unmatched */
17242 }
17243 }
17244
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017245 if (match_pos != NULL)
17246 {
17247 /* Store the match cursor position */
17248 match_pos->lnum = curwin->w_cursor.lnum;
17249 match_pos->col = curwin->w_cursor.col + 1;
17250 }
17251
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017253 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254 curwin->w_cursor = save_cursor;
17255
17256theend:
17257 vim_free(pat2);
17258 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017259 if (p_cpo == empty_option)
17260 p_cpo = save_cpo;
17261 else
17262 /* Darn, evaluating the {skip} expression changed the value. */
17263 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017264
17265 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266}
17267
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017268/*
17269 * "searchpos()" function
17270 */
17271 static void
17272f_searchpos(argvars, rettv)
17273 typval_T *argvars;
17274 typval_T *rettv;
17275{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017276 pos_T match_pos;
17277 int lnum = 0;
17278 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017279 int n;
17280 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017281
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017282 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017283 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017284
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017285 n = search_cmn(argvars, &match_pos, &flags);
17286 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017287 {
17288 lnum = match_pos.lnum;
17289 col = match_pos.col;
17290 }
17291
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017292 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17293 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017294 if (flags & SP_SUBPAT)
17295 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017296}
17297
17298
Bram Moolenaar0d660222005-01-07 21:51:51 +000017299 static void
17300f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017301 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017304#ifdef FEAT_CLIENTSERVER
17305 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017306 char_u *server = get_tv_string_chk(&argvars[0]);
17307 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308
Bram Moolenaar0d660222005-01-07 21:51:51 +000017309 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017310 if (server == NULL || reply == NULL)
17311 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017312 if (check_restricted() || check_secure())
17313 return;
17314# ifdef FEAT_X11
17315 if (check_connection() == FAIL)
17316 return;
17317# endif
17318
17319 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017321 EMSG(_("E258: Unable to send to client"));
17322 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017324 rettv->vval.v_number = 0;
17325#else
17326 rettv->vval.v_number = -1;
17327#endif
17328}
17329
Bram Moolenaar0d660222005-01-07 21:51:51 +000017330 static void
17331f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017332 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017333 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017334{
17335 char_u *r = NULL;
17336
17337#ifdef FEAT_CLIENTSERVER
17338# ifdef WIN32
17339 r = serverGetVimNames();
17340# else
17341 make_connection();
17342 if (X_DISPLAY != NULL)
17343 r = serverGetVimNames(X_DISPLAY);
17344# endif
17345#endif
17346 rettv->v_type = VAR_STRING;
17347 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348}
17349
17350/*
17351 * "setbufvar()" function
17352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017354f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017355 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017356 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357{
17358 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017361 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 char_u nbuf[NUMBUFLEN];
17363
17364 if (check_restricted() || check_secure())
17365 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017366 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17367 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017368 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 varp = &argvars[2];
17370
17371 if (buf != NULL && varname != NULL && varp != NULL)
17372 {
17373 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375
17376 if (*varname == '&')
17377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017378 long numval;
17379 char_u *strval;
17380 int error = FALSE;
17381
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017383 numval = get_tv_number_chk(varp, &error);
17384 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017385 if (!error && strval != NULL)
17386 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 }
17388 else
17389 {
17390 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17391 if (bufvarname != NULL)
17392 {
17393 STRCPY(bufvarname, "b:");
17394 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017395 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396 vim_free(bufvarname);
17397 }
17398 }
17399
17400 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403}
17404
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017405 static void
17406f_setcharsearch(argvars, rettv)
17407 typval_T *argvars;
17408 typval_T *rettv UNUSED;
17409{
17410 dict_T *d;
17411 dictitem_T *di;
17412 char_u *csearch;
17413
17414 if (argvars[0].v_type != VAR_DICT)
17415 {
17416 EMSG(_(e_dictreq));
17417 return;
17418 }
17419
17420 if ((d = argvars[0].vval.v_dict) != NULL)
17421 {
17422 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17423 if (csearch != NULL)
17424 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017425#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017426 if (enc_utf8)
17427 {
17428 int pcc[MAX_MCO];
17429 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017430
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017431 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17432 }
17433 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017434#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017435 set_last_csearch(PTR2CHAR(csearch),
17436 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017437 }
17438
17439 di = dict_find(d, (char_u *)"forward", -1);
17440 if (di != NULL)
17441 set_csearch_direction(get_tv_number(&di->di_tv)
17442 ? FORWARD : BACKWARD);
17443
17444 di = dict_find(d, (char_u *)"until", -1);
17445 if (di != NULL)
17446 set_csearch_until(!!get_tv_number(&di->di_tv));
17447 }
17448}
17449
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450/*
17451 * "setcmdpos()" function
17452 */
17453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017454f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017455 typval_T *argvars;
17456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017458 int pos = (int)get_tv_number(&argvars[0]) - 1;
17459
17460 if (pos >= 0)
17461 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462}
17463
17464/*
17465 * "setline()" function
17466 */
17467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017468f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017469 typval_T *argvars;
17470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471{
17472 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017473 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017474 list_T *l = NULL;
17475 listitem_T *li = NULL;
17476 long added = 0;
17477 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017479 lnum = get_tv_lnum(&argvars[0]);
17480 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017482 l = argvars[1].vval.v_list;
17483 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017485 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017486 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017487
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017488 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017489 for (;;)
17490 {
17491 if (l != NULL)
17492 {
17493 /* list argument, get next string */
17494 if (li == NULL)
17495 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017496 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017497 li = li->li_next;
17498 }
17499
17500 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017501 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017502 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017503
17504 /* When coming here from Insert mode, sync undo, so that this can be
17505 * undone separately from what was previously inserted. */
17506 if (u_sync_once == 2)
17507 {
17508 u_sync_once = 1; /* notify that u_sync() was called */
17509 u_sync(TRUE);
17510 }
17511
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017512 if (lnum <= curbuf->b_ml.ml_line_count)
17513 {
17514 /* existing line, replace it */
17515 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17516 {
17517 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017518 if (lnum == curwin->w_cursor.lnum)
17519 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017520 rettv->vval.v_number = 0; /* OK */
17521 }
17522 }
17523 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17524 {
17525 /* lnum is one past the last line, append the line */
17526 ++added;
17527 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17528 rettv->vval.v_number = 0; /* OK */
17529 }
17530
17531 if (l == NULL) /* only one string argument */
17532 break;
17533 ++lnum;
17534 }
17535
17536 if (added > 0)
17537 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538}
17539
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017540static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17541
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017543 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017544 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017545 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017546set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017547 win_T *wp UNUSED;
17548 typval_T *list_arg UNUSED;
17549 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017550 typval_T *rettv;
17551{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017552#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017553 char_u *act;
17554 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017555#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017556
Bram Moolenaar2641f772005-03-25 21:58:17 +000017557 rettv->vval.v_number = -1;
17558
17559#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017560 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017561 EMSG(_(e_listreq));
17562 else
17563 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017564 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017565
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017566 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017567 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017568 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017569 if (act == NULL)
17570 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017571 if (*act == 'a' || *act == 'r')
17572 action = *act;
17573 }
17574
Bram Moolenaar81484f42012-12-05 15:16:47 +010017575 if (l != NULL && set_errorlist(wp, l, action,
17576 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017577 rettv->vval.v_number = 0;
17578 }
17579#endif
17580}
17581
17582/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017583 * "setloclist()" function
17584 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017585 static void
17586f_setloclist(argvars, rettv)
17587 typval_T *argvars;
17588 typval_T *rettv;
17589{
17590 win_T *win;
17591
17592 rettv->vval.v_number = -1;
17593
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017594 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017595 if (win != NULL)
17596 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17597}
17598
17599/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017600 * "setmatches()" function
17601 */
17602 static void
17603f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017604 typval_T *argvars UNUSED;
17605 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017606{
17607#ifdef FEAT_SEARCH_EXTRA
17608 list_T *l;
17609 listitem_T *li;
17610 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017611 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017612
17613 rettv->vval.v_number = -1;
17614 if (argvars[0].v_type != VAR_LIST)
17615 {
17616 EMSG(_(e_listreq));
17617 return;
17618 }
17619 if ((l = argvars[0].vval.v_list) != NULL)
17620 {
17621
17622 /* To some extent make sure that we are dealing with a list from
17623 * "getmatches()". */
17624 li = l->lv_first;
17625 while (li != NULL)
17626 {
17627 if (li->li_tv.v_type != VAR_DICT
17628 || (d = li->li_tv.vval.v_dict) == NULL)
17629 {
17630 EMSG(_(e_invarg));
17631 return;
17632 }
17633 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017634 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17635 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017636 && dict_find(d, (char_u *)"priority", -1) != NULL
17637 && dict_find(d, (char_u *)"id", -1) != NULL))
17638 {
17639 EMSG(_(e_invarg));
17640 return;
17641 }
17642 li = li->li_next;
17643 }
17644
17645 clear_matches(curwin);
17646 li = l->lv_first;
17647 while (li != NULL)
17648 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017649 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017650 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017651 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017652 char_u *group;
17653 int priority;
17654 int id;
17655 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017656
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017657 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017658 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17659 {
17660 if (s == NULL)
17661 {
17662 s = list_alloc();
17663 if (s == NULL)
17664 return;
17665 }
17666
17667 /* match from matchaddpos() */
17668 for (i = 1; i < 9; i++)
17669 {
17670 sprintf((char *)buf, (char *)"pos%d", i);
17671 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17672 {
17673 if (di->di_tv.v_type != VAR_LIST)
17674 return;
17675
17676 list_append_tv(s, &di->di_tv);
17677 s->lv_refcount++;
17678 }
17679 else
17680 break;
17681 }
17682 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017683
17684 group = get_dict_string(d, (char_u *)"group", FALSE);
17685 priority = (int)get_dict_number(d, (char_u *)"priority");
17686 id = (int)get_dict_number(d, (char_u *)"id");
17687 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17688 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17689 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017690 if (i == 0)
17691 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017692 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017693 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017694 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017695 }
17696 else
17697 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017698 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017699 list_unref(s);
17700 s = NULL;
17701 }
17702
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017703 li = li->li_next;
17704 }
17705 rettv->vval.v_number = 0;
17706 }
17707#endif
17708}
17709
17710/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017711 * "setpos()" function
17712 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017713 static void
17714f_setpos(argvars, rettv)
17715 typval_T *argvars;
17716 typval_T *rettv;
17717{
17718 pos_T pos;
17719 int fnum;
17720 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017721 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017722
Bram Moolenaar08250432008-02-13 11:42:46 +000017723 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017724 name = get_tv_string_chk(argvars);
17725 if (name != NULL)
17726 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017727 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017728 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017729 if (--pos.col < 0)
17730 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017731 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017732 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017733 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017734 if (fnum == curbuf->b_fnum)
17735 {
17736 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017737 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017738 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017739 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017740 curwin->w_set_curswant = FALSE;
17741 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017742 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017743 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017744 }
17745 else
17746 EMSG(_(e_invarg));
17747 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017748 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17749 {
17750 /* set mark */
17751 if (setmark_pos(name[1], &pos, fnum) == OK)
17752 rettv->vval.v_number = 0;
17753 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017754 else
17755 EMSG(_(e_invarg));
17756 }
17757 }
17758}
17759
17760/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017761 * "setqflist()" function
17762 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017763 static void
17764f_setqflist(argvars, rettv)
17765 typval_T *argvars;
17766 typval_T *rettv;
17767{
17768 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17769}
17770
17771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772 * "setreg()" function
17773 */
17774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017775f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017776 typval_T *argvars;
17777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778{
17779 int regname;
17780 char_u *strregname;
17781 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017782 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783 int append;
17784 char_u yank_type;
17785 long block_len;
17786
17787 block_len = -1;
17788 yank_type = MAUTO;
17789 append = FALSE;
17790
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017791 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017792 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017794 if (strregname == NULL)
17795 return; /* type error; errmsg already given */
17796 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017797 if (regname == 0 || regname == '@')
17798 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017800 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017802 stropt = get_tv_string_chk(&argvars[2]);
17803 if (stropt == NULL)
17804 return; /* type error */
17805 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 switch (*stropt)
17807 {
17808 case 'a': case 'A': /* append */
17809 append = TRUE;
17810 break;
17811 case 'v': case 'c': /* character-wise selection */
17812 yank_type = MCHAR;
17813 break;
17814 case 'V': case 'l': /* line-wise selection */
17815 yank_type = MLINE;
17816 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817 case 'b': case Ctrl_V: /* block-wise selection */
17818 yank_type = MBLOCK;
17819 if (VIM_ISDIGIT(stropt[1]))
17820 {
17821 ++stropt;
17822 block_len = getdigits(&stropt) - 1;
17823 --stropt;
17824 }
17825 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017826 }
17827 }
17828
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017829 if (argvars[1].v_type == VAR_LIST)
17830 {
17831 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017832 char_u **allocval;
17833 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017834 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017835 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017836 int len = argvars[1].vval.v_list->lv_len;
17837 listitem_T *li;
17838
Bram Moolenaar7d647822014-04-05 21:28:56 +020017839 /* First half: use for pointers to result lines; second half: use for
17840 * pointers to allocated copies. */
17841 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017842 if (lstval == NULL)
17843 return;
17844 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017845 allocval = lstval + len + 2;
17846 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017847
17848 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17849 li = li->li_next)
17850 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017851 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017852 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017853 goto free_lstval;
17854 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017855 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017856 /* Need to make a copy, next get_tv_string_buf_chk() will
17857 * overwrite the string. */
17858 strval = vim_strsave(buf);
17859 if (strval == NULL)
17860 goto free_lstval;
17861 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017862 }
17863 *curval++ = strval;
17864 }
17865 *curval++ = NULL;
17866
17867 write_reg_contents_lst(regname, lstval, -1,
17868 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017869free_lstval:
17870 while (curallocval > allocval)
17871 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017872 vim_free(lstval);
17873 }
17874 else
17875 {
17876 strval = get_tv_string_chk(&argvars[1]);
17877 if (strval == NULL)
17878 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017879 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017881 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017882 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883}
17884
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017885/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017886 * "settabvar()" function
17887 */
17888 static void
17889f_settabvar(argvars, rettv)
17890 typval_T *argvars;
17891 typval_T *rettv;
17892{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017893#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017894 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017895 tabpage_T *tp;
17896#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017897 char_u *varname, *tabvarname;
17898 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017899
17900 rettv->vval.v_number = 0;
17901
17902 if (check_restricted() || check_secure())
17903 return;
17904
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017905#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017906 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017907#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017908 varname = get_tv_string_chk(&argvars[1]);
17909 varp = &argvars[2];
17910
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017911 if (varname != NULL && varp != NULL
17912#ifdef FEAT_WINDOWS
17913 && tp != NULL
17914#endif
17915 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017916 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017917#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017918 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017919 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017920#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017921
17922 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17923 if (tabvarname != NULL)
17924 {
17925 STRCPY(tabvarname, "t:");
17926 STRCPY(tabvarname + 2, varname);
17927 set_var(tabvarname, varp, TRUE);
17928 vim_free(tabvarname);
17929 }
17930
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017931#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017932 /* Restore current tabpage */
17933 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017934 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017935#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017936 }
17937}
17938
17939/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017940 * "settabwinvar()" function
17941 */
17942 static void
17943f_settabwinvar(argvars, rettv)
17944 typval_T *argvars;
17945 typval_T *rettv;
17946{
17947 setwinvar(argvars, rettv, 1);
17948}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949
17950/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017951 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017954f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017955 typval_T *argvars;
17956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017958 setwinvar(argvars, rettv, 0);
17959}
17960
17961/*
17962 * "setwinvar()" and "settabwinvar()" functions
17963 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017964
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017965 static void
17966setwinvar(argvars, rettv, off)
17967 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017968 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017969 int off;
17970{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971 win_T *win;
17972#ifdef FEAT_WINDOWS
17973 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017974 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017975 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976#endif
17977 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017978 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017980 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981
17982 if (check_restricted() || check_secure())
17983 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017984
17985#ifdef FEAT_WINDOWS
17986 if (off == 1)
17987 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17988 else
17989 tp = curtab;
17990#endif
17991 win = find_win_by_nr(&argvars[off], tp);
17992 varname = get_tv_string_chk(&argvars[off + 1]);
17993 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994
17995 if (win != NULL && varname != NULL && varp != NULL)
17996 {
17997#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017998 need_switch_win = !(tp == curtab && win == curwin);
17999 if (!need_switch_win
18000 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018003 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018005 long numval;
18006 char_u *strval;
18007 int error = FALSE;
18008
18009 ++varname;
18010 numval = get_tv_number_chk(varp, &error);
18011 strval = get_tv_string_buf_chk(varp, nbuf);
18012 if (!error && strval != NULL)
18013 set_option_value(varname, numval, strval, OPT_LOCAL);
18014 }
18015 else
18016 {
18017 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18018 if (winvarname != NULL)
18019 {
18020 STRCPY(winvarname, "w:");
18021 STRCPY(winvarname + 2, varname);
18022 set_var(winvarname, varp, TRUE);
18023 vim_free(winvarname);
18024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025 }
18026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018028 if (need_switch_win)
18029 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030#endif
18031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032}
18033
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018034#ifdef FEAT_CRYPT
18035/*
18036 * "sha256({string})" function
18037 */
18038 static void
18039f_sha256(argvars, rettv)
18040 typval_T *argvars;
18041 typval_T *rettv;
18042{
18043 char_u *p;
18044
18045 p = get_tv_string(&argvars[0]);
18046 rettv->vval.v_string = vim_strsave(
18047 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18048 rettv->v_type = VAR_STRING;
18049}
18050#endif /* FEAT_CRYPT */
18051
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018053 * "shellescape({string})" function
18054 */
18055 static void
18056f_shellescape(argvars, rettv)
18057 typval_T *argvars;
18058 typval_T *rettv;
18059{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018060 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018061 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018062 rettv->v_type = VAR_STRING;
18063}
18064
18065/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018066 * shiftwidth() function
18067 */
18068 static void
18069f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020018070 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018071 typval_T *rettv;
18072{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018073 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018074}
18075
18076/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018077 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078 */
18079 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000018080f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018081 typval_T *argvars;
18082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018084 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085
Bram Moolenaar0d660222005-01-07 21:51:51 +000018086 p = get_tv_string(&argvars[0]);
18087 rettv->vval.v_string = vim_strsave(p);
18088 simplify_filename(rettv->vval.v_string); /* simplify in place */
18089 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090}
18091
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018092#ifdef FEAT_FLOAT
18093/*
18094 * "sin()" function
18095 */
18096 static void
18097f_sin(argvars, rettv)
18098 typval_T *argvars;
18099 typval_T *rettv;
18100{
18101 float_T f;
18102
18103 rettv->v_type = VAR_FLOAT;
18104 if (get_float_arg(argvars, &f) == OK)
18105 rettv->vval.v_float = sin(f);
18106 else
18107 rettv->vval.v_float = 0.0;
18108}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018109
18110/*
18111 * "sinh()" function
18112 */
18113 static void
18114f_sinh(argvars, rettv)
18115 typval_T *argvars;
18116 typval_T *rettv;
18117{
18118 float_T f;
18119
18120 rettv->v_type = VAR_FLOAT;
18121 if (get_float_arg(argvars, &f) == OK)
18122 rettv->vval.v_float = sinh(f);
18123 else
18124 rettv->vval.v_float = 0.0;
18125}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018126#endif
18127
Bram Moolenaar0d660222005-01-07 21:51:51 +000018128static int
18129#ifdef __BORLANDC__
18130 _RTLENTRYF
18131#endif
18132 item_compare __ARGS((const void *s1, const void *s2));
18133static int
18134#ifdef __BORLANDC__
18135 _RTLENTRYF
18136#endif
18137 item_compare2 __ARGS((const void *s1, const void *s2));
18138
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018139/* struct used in the array that's given to qsort() */
18140typedef struct
18141{
18142 listitem_T *item;
18143 int idx;
18144} sortItem_T;
18145
Bram Moolenaar0d660222005-01-07 21:51:51 +000018146static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018147static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018148static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018149#ifdef FEAT_FLOAT
18150static int item_compare_float;
18151#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018152static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018153static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018154static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018155static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018156static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018157#define ITEM_COMPARE_FAIL 999
18158
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018160 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018162 static int
18163#ifdef __BORLANDC__
18164_RTLENTRYF
18165#endif
18166item_compare(s1, s2)
18167 const void *s1;
18168 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018170 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018171 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018172 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018173 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018174 int res;
18175 char_u numbuf1[NUMBUFLEN];
18176 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018178 si1 = (sortItem_T *)s1;
18179 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018180 tv1 = &si1->item->li_tv;
18181 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018182
18183 if (item_compare_numbers)
18184 {
18185 long v1 = get_tv_number(tv1);
18186 long v2 = get_tv_number(tv2);
18187
18188 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18189 }
18190
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018191#ifdef FEAT_FLOAT
18192 if (item_compare_float)
18193 {
18194 float_T v1 = get_tv_float(tv1);
18195 float_T v2 = get_tv_float(tv2);
18196
18197 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18198 }
18199#endif
18200
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018201 /* tv2string() puts quotes around a string and allocates memory. Don't do
18202 * that for string variables. Use a single quote when comparing with a
18203 * non-string to do what the docs promise. */
18204 if (tv1->v_type == VAR_STRING)
18205 {
18206 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18207 p1 = (char_u *)"'";
18208 else
18209 p1 = tv1->vval.v_string;
18210 }
18211 else
18212 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18213 if (tv2->v_type == VAR_STRING)
18214 {
18215 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18216 p2 = (char_u *)"'";
18217 else
18218 p2 = tv2->vval.v_string;
18219 }
18220 else
18221 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018222 if (p1 == NULL)
18223 p1 = (char_u *)"";
18224 if (p2 == NULL)
18225 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018226 if (!item_compare_numeric)
18227 {
18228 if (item_compare_ic)
18229 res = STRICMP(p1, p2);
18230 else
18231 res = STRCMP(p1, p2);
18232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018234 {
18235 double n1, n2;
18236 n1 = strtod((char *)p1, (char **)&p1);
18237 n2 = strtod((char *)p2, (char **)&p2);
18238 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18239 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018240
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018241 /* When the result would be zero, compare the item indexes. Makes the
18242 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018243 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018244 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018245
Bram Moolenaar0d660222005-01-07 21:51:51 +000018246 vim_free(tofree1);
18247 vim_free(tofree2);
18248 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249}
18250
18251 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018252#ifdef __BORLANDC__
18253_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018255item_compare2(s1, s2)
18256 const void *s1;
18257 const void *s2;
18258{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018259 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018260 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018261 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018262 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018263 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018265 /* shortcut after failure in previous call; compare all items equal */
18266 if (item_compare_func_err)
18267 return 0;
18268
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018269 si1 = (sortItem_T *)s1;
18270 si2 = (sortItem_T *)s2;
18271
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018272 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018273 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018274 copy_tv(&si1->item->li_tv, &argv[0]);
18275 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018276
18277 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018278 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018279 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18280 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018281 clear_tv(&argv[0]);
18282 clear_tv(&argv[1]);
18283
18284 if (res == FAIL)
18285 res = ITEM_COMPARE_FAIL;
18286 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018287 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18288 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018289 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018290 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018291
18292 /* When the result would be zero, compare the pointers themselves. Makes
18293 * the sort stable. */
18294 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018295 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018296
Bram Moolenaar0d660222005-01-07 21:51:51 +000018297 return res;
18298}
18299
18300/*
18301 * "sort({list})" function
18302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018304do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018305 typval_T *argvars;
18306 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018307 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308{
Bram Moolenaar33570922005-01-25 22:26:29 +000018309 list_T *l;
18310 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018311 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018312 long len;
18313 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314
Bram Moolenaar0d660222005-01-07 21:51:51 +000018315 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018316 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317 else
18318 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018319 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018320 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018321 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18322 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018323 return;
18324 rettv->vval.v_list = l;
18325 rettv->v_type = VAR_LIST;
18326 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327
Bram Moolenaar0d660222005-01-07 21:51:51 +000018328 len = list_len(l);
18329 if (len <= 1)
18330 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331
Bram Moolenaar0d660222005-01-07 21:51:51 +000018332 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018333 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018334 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018335#ifdef FEAT_FLOAT
18336 item_compare_float = FALSE;
18337#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018338 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018339 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018340 if (argvars[1].v_type != VAR_UNKNOWN)
18341 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018342 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018343 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018344 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018345 else
18346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018347 int error = FALSE;
18348
18349 i = get_tv_number_chk(&argvars[1], &error);
18350 if (error)
18351 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018352 if (i == 1)
18353 item_compare_ic = TRUE;
18354 else
18355 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018356 if (item_compare_func != NULL)
18357 {
18358 if (STRCMP(item_compare_func, "n") == 0)
18359 {
18360 item_compare_func = NULL;
18361 item_compare_numeric = TRUE;
18362 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018363 else if (STRCMP(item_compare_func, "N") == 0)
18364 {
18365 item_compare_func = NULL;
18366 item_compare_numbers = TRUE;
18367 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018368#ifdef FEAT_FLOAT
18369 else if (STRCMP(item_compare_func, "f") == 0)
18370 {
18371 item_compare_func = NULL;
18372 item_compare_float = TRUE;
18373 }
18374#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018375 else if (STRCMP(item_compare_func, "i") == 0)
18376 {
18377 item_compare_func = NULL;
18378 item_compare_ic = TRUE;
18379 }
18380 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018381 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018382
18383 if (argvars[2].v_type != VAR_UNKNOWN)
18384 {
18385 /* optional third argument: {dict} */
18386 if (argvars[2].v_type != VAR_DICT)
18387 {
18388 EMSG(_(e_dictreq));
18389 return;
18390 }
18391 item_compare_selfdict = argvars[2].vval.v_dict;
18392 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394
Bram Moolenaar0d660222005-01-07 21:51:51 +000018395 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018396 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018397 if (ptrs == NULL)
18398 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399
Bram Moolenaar327aa022014-03-25 18:24:23 +010018400 i = 0;
18401 if (sort)
18402 {
18403 /* sort(): ptrs will be the list to sort */
18404 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018405 {
18406 ptrs[i].item = li;
18407 ptrs[i].idx = i;
18408 ++i;
18409 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018410
18411 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018412 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018413 /* test the compare function */
18414 if (item_compare_func != NULL
18415 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018416 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018417 EMSG(_("E702: Sort compare function failed"));
18418 else
18419 {
18420 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018421 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018422 item_compare_func == NULL ? item_compare : item_compare2);
18423
18424 if (!item_compare_func_err)
18425 {
18426 /* Clear the List and append the items in sorted order. */
18427 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18428 l->lv_len = 0;
18429 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018430 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018431 }
18432 }
18433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018435 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018436 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18437
18438 /* f_uniq(): ptrs will be a stack of items to remove */
18439 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018440 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018441 item_compare_func_ptr = item_compare_func
18442 ? item_compare2 : item_compare;
18443
18444 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18445 li = li->li_next)
18446 {
18447 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18448 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018449 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018450 if (item_compare_func_err)
18451 {
18452 EMSG(_("E882: Uniq compare function failed"));
18453 break;
18454 }
18455 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018457 if (!item_compare_func_err)
18458 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018459 while (--i >= 0)
18460 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018461 li = ptrs[i].item->li_next;
18462 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018463 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018464 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018465 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018466 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018467 list_fix_watch(l, li);
18468 listitem_free(li);
18469 l->lv_len--;
18470 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018471 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018472 }
18473
18474 vim_free(ptrs);
18475 }
18476}
18477
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018478/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018479 * "sort({list})" function
18480 */
18481 static void
18482f_sort(argvars, rettv)
18483 typval_T *argvars;
18484 typval_T *rettv;
18485{
18486 do_sort_uniq(argvars, rettv, TRUE);
18487}
18488
18489/*
18490 * "uniq({list})" function
18491 */
18492 static void
18493f_uniq(argvars, rettv)
18494 typval_T *argvars;
18495 typval_T *rettv;
18496{
18497 do_sort_uniq(argvars, rettv, FALSE);
18498}
18499
18500/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018501 * "soundfold({word})" function
18502 */
18503 static void
18504f_soundfold(argvars, rettv)
18505 typval_T *argvars;
18506 typval_T *rettv;
18507{
18508 char_u *s;
18509
18510 rettv->v_type = VAR_STRING;
18511 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018512#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018513 rettv->vval.v_string = eval_soundfold(s);
18514#else
18515 rettv->vval.v_string = vim_strsave(s);
18516#endif
18517}
18518
18519/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018520 * "spellbadword()" function
18521 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018522 static void
18523f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018524 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018525 typval_T *rettv;
18526{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018527 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018528 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018529 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018530
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018531 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018532 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018533
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018534#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018535 if (argvars[0].v_type == VAR_UNKNOWN)
18536 {
18537 /* Find the start and length of the badly spelled word. */
18538 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18539 if (len != 0)
18540 word = ml_get_cursor();
18541 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018542 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018543 {
18544 char_u *str = get_tv_string_chk(&argvars[0]);
18545 int capcol = -1;
18546
18547 if (str != NULL)
18548 {
18549 /* Check the argument for spelling. */
18550 while (*str != NUL)
18551 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018552 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018553 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018554 {
18555 word = str;
18556 break;
18557 }
18558 str += len;
18559 }
18560 }
18561 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018562#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018563
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018564 list_append_string(rettv->vval.v_list, word, len);
18565 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018566 attr == HLF_SPB ? "bad" :
18567 attr == HLF_SPR ? "rare" :
18568 attr == HLF_SPL ? "local" :
18569 attr == HLF_SPC ? "caps" :
18570 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018571}
18572
18573/*
18574 * "spellsuggest()" function
18575 */
18576 static void
18577f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018578 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018579 typval_T *rettv;
18580{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018581#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018582 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018583 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018584 int maxcount;
18585 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018586 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018587 listitem_T *li;
18588 int need_capital = FALSE;
18589#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018590
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018591 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018592 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018593
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018594#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018595 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018596 {
18597 str = get_tv_string(&argvars[0]);
18598 if (argvars[1].v_type != VAR_UNKNOWN)
18599 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018600 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018601 if (maxcount <= 0)
18602 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018603 if (argvars[2].v_type != VAR_UNKNOWN)
18604 {
18605 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18606 if (typeerr)
18607 return;
18608 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018609 }
18610 else
18611 maxcount = 25;
18612
Bram Moolenaar4770d092006-01-12 23:22:24 +000018613 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018614
18615 for (i = 0; i < ga.ga_len; ++i)
18616 {
18617 str = ((char_u **)ga.ga_data)[i];
18618
18619 li = listitem_alloc();
18620 if (li == NULL)
18621 vim_free(str);
18622 else
18623 {
18624 li->li_tv.v_type = VAR_STRING;
18625 li->li_tv.v_lock = 0;
18626 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018627 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018628 }
18629 }
18630 ga_clear(&ga);
18631 }
18632#endif
18633}
18634
Bram Moolenaar0d660222005-01-07 21:51:51 +000018635 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018636f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018637 typval_T *argvars;
18638 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018639{
18640 char_u *str;
18641 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018642 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018643 regmatch_T regmatch;
18644 char_u patbuf[NUMBUFLEN];
18645 char_u *save_cpo;
18646 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018647 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018648 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018649 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018650
18651 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18652 save_cpo = p_cpo;
18653 p_cpo = (char_u *)"";
18654
18655 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018656 if (argvars[1].v_type != VAR_UNKNOWN)
18657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018658 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18659 if (pat == NULL)
18660 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018661 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018662 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018663 }
18664 if (pat == NULL || *pat == NUL)
18665 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018666
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018667 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018669 if (typeerr)
18670 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671
Bram Moolenaar0d660222005-01-07 21:51:51 +000018672 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18673 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018675 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018676 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018677 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018678 if (*str == NUL)
18679 match = FALSE; /* empty item at the end */
18680 else
18681 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018682 if (match)
18683 end = regmatch.startp[0];
18684 else
18685 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018686 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18687 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018688 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018689 if (list_append_string(rettv->vval.v_list, str,
18690 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018691 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018692 }
18693 if (!match)
18694 break;
18695 /* Advance to just after the match. */
18696 if (regmatch.endp[0] > str)
18697 col = 0;
18698 else
18699 {
18700 /* Don't get stuck at the same match. */
18701#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018702 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018703#else
18704 col = 1;
18705#endif
18706 }
18707 str = regmatch.endp[0];
18708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709
Bram Moolenaar473de612013-06-08 18:19:48 +020018710 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712
Bram Moolenaar0d660222005-01-07 21:51:51 +000018713 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714}
18715
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018716#ifdef FEAT_FLOAT
18717/*
18718 * "sqrt()" function
18719 */
18720 static void
18721f_sqrt(argvars, rettv)
18722 typval_T *argvars;
18723 typval_T *rettv;
18724{
18725 float_T f;
18726
18727 rettv->v_type = VAR_FLOAT;
18728 if (get_float_arg(argvars, &f) == OK)
18729 rettv->vval.v_float = sqrt(f);
18730 else
18731 rettv->vval.v_float = 0.0;
18732}
18733
18734/*
18735 * "str2float()" function
18736 */
18737 static void
18738f_str2float(argvars, rettv)
18739 typval_T *argvars;
18740 typval_T *rettv;
18741{
18742 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18743
18744 if (*p == '+')
18745 p = skipwhite(p + 1);
18746 (void)string2float(p, &rettv->vval.v_float);
18747 rettv->v_type = VAR_FLOAT;
18748}
18749#endif
18750
Bram Moolenaar2c932302006-03-18 21:42:09 +000018751/*
18752 * "str2nr()" function
18753 */
18754 static void
18755f_str2nr(argvars, rettv)
18756 typval_T *argvars;
18757 typval_T *rettv;
18758{
18759 int base = 10;
18760 char_u *p;
18761 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018762 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018763
18764 if (argvars[1].v_type != VAR_UNKNOWN)
18765 {
18766 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018767 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018768 {
18769 EMSG(_(e_invarg));
18770 return;
18771 }
18772 }
18773
18774 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018775 if (*p == '+')
18776 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018777 switch (base)
18778 {
18779 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18780 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18781 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18782 default: what = 0;
18783 }
18784 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018785 rettv->vval.v_number = n;
18786}
18787
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788#ifdef HAVE_STRFTIME
18789/*
18790 * "strftime({format}[, {time}])" function
18791 */
18792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018793f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018794 typval_T *argvars;
18795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796{
18797 char_u result_buf[256];
18798 struct tm *curtime;
18799 time_t seconds;
18800 char_u *p;
18801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018802 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018804 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018805 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 seconds = time(NULL);
18807 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018808 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 curtime = localtime(&seconds);
18810 /* MSVC returns NULL for an invalid value of seconds. */
18811 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018812 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813 else
18814 {
18815# ifdef FEAT_MBYTE
18816 vimconv_T conv;
18817 char_u *enc;
18818
18819 conv.vc_type = CONV_NONE;
18820 enc = enc_locale();
18821 convert_setup(&conv, p_enc, enc);
18822 if (conv.vc_type != CONV_NONE)
18823 p = string_convert(&conv, p, NULL);
18824# endif
18825 if (p != NULL)
18826 (void)strftime((char *)result_buf, sizeof(result_buf),
18827 (char *)p, curtime);
18828 else
18829 result_buf[0] = NUL;
18830
18831# ifdef FEAT_MBYTE
18832 if (conv.vc_type != CONV_NONE)
18833 vim_free(p);
18834 convert_setup(&conv, enc, p_enc);
18835 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018836 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 else
18838# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018839 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840
18841# ifdef FEAT_MBYTE
18842 /* Release conversion descriptors */
18843 convert_setup(&conv, NULL, NULL);
18844 vim_free(enc);
18845# endif
18846 }
18847}
18848#endif
18849
18850/*
18851 * "stridx()" function
18852 */
18853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018854f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018855 typval_T *argvars;
18856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857{
18858 char_u buf[NUMBUFLEN];
18859 char_u *needle;
18860 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018861 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018863 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018865 needle = get_tv_string_chk(&argvars[1]);
18866 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018867 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018868 if (needle == NULL || haystack == NULL)
18869 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870
Bram Moolenaar33570922005-01-25 22:26:29 +000018871 if (argvars[2].v_type != VAR_UNKNOWN)
18872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018873 int error = FALSE;
18874
18875 start_idx = get_tv_number_chk(&argvars[2], &error);
18876 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018877 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018878 if (start_idx >= 0)
18879 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018880 }
18881
18882 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18883 if (pos != NULL)
18884 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885}
18886
18887/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018888 * "string()" function
18889 */
18890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018891f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018892 typval_T *argvars;
18893 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018894{
18895 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018896 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018898 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018899 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018900 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018901 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018902 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903}
18904
18905/*
18906 * "strlen()" function
18907 */
18908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018909f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018910 typval_T *argvars;
18911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018913 rettv->vval.v_number = (varnumber_T)(STRLEN(
18914 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915}
18916
18917/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018918 * "strchars()" function
18919 */
18920 static void
18921f_strchars(argvars, rettv)
18922 typval_T *argvars;
18923 typval_T *rettv;
18924{
18925 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018926 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018927#ifdef FEAT_MBYTE
18928 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018929 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018930#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018931
18932 if (argvars[1].v_type != VAR_UNKNOWN)
18933 skipcc = get_tv_number_chk(&argvars[1], NULL);
18934 if (skipcc < 0 || skipcc > 1)
18935 EMSG(_(e_invarg));
18936 else
18937 {
18938#ifdef FEAT_MBYTE
18939 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18940 while (*s != NUL)
18941 {
18942 func_mb_ptr2char_adv(&s);
18943 ++len;
18944 }
18945 rettv->vval.v_number = len;
18946#else
18947 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18948#endif
18949 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018950}
18951
18952/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018953 * "strdisplaywidth()" function
18954 */
18955 static void
18956f_strdisplaywidth(argvars, rettv)
18957 typval_T *argvars;
18958 typval_T *rettv;
18959{
18960 char_u *s = get_tv_string(&argvars[0]);
18961 int col = 0;
18962
18963 if (argvars[1].v_type != VAR_UNKNOWN)
18964 col = get_tv_number(&argvars[1]);
18965
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018966 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018967}
18968
18969/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018970 * "strwidth()" function
18971 */
18972 static void
18973f_strwidth(argvars, rettv)
18974 typval_T *argvars;
18975 typval_T *rettv;
18976{
18977 char_u *s = get_tv_string(&argvars[0]);
18978
18979 rettv->vval.v_number = (varnumber_T)(
18980#ifdef FEAT_MBYTE
18981 mb_string2cells(s, -1)
18982#else
18983 STRLEN(s)
18984#endif
18985 );
18986}
18987
18988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 * "strpart()" function
18990 */
18991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018992f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018993 typval_T *argvars;
18994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995{
18996 char_u *p;
18997 int n;
18998 int len;
18999 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019000 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019002 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 slen = (int)STRLEN(p);
19004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019005 n = get_tv_number_chk(&argvars[1], &error);
19006 if (error)
19007 len = 0;
19008 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019009 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010 else
19011 len = slen - n; /* default len: all bytes that are available. */
19012
19013 /*
19014 * Only return the overlap between the specified part and the actual
19015 * string.
19016 */
19017 if (n < 0)
19018 {
19019 len += n;
19020 n = 0;
19021 }
19022 else if (n > slen)
19023 n = slen;
19024 if (len < 0)
19025 len = 0;
19026 else if (n + len > slen)
19027 len = slen - n;
19028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019029 rettv->v_type = VAR_STRING;
19030 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031}
19032
19033/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019034 * "strridx()" function
19035 */
19036 static void
19037f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019038 typval_T *argvars;
19039 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019040{
19041 char_u buf[NUMBUFLEN];
19042 char_u *needle;
19043 char_u *haystack;
19044 char_u *rest;
19045 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019046 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019048 needle = get_tv_string_chk(&argvars[1]);
19049 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019050
19051 rettv->vval.v_number = -1;
19052 if (needle == NULL || haystack == NULL)
19053 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019054
19055 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019056 if (argvars[2].v_type != VAR_UNKNOWN)
19057 {
19058 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019059 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019060 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019061 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019062 }
19063 else
19064 end_idx = haystack_len;
19065
Bram Moolenaar0d660222005-01-07 21:51:51 +000019066 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019067 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019068 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019069 lastmatch = haystack + end_idx;
19070 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019071 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019072 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019073 for (rest = haystack; *rest != '\0'; ++rest)
19074 {
19075 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019076 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019077 break;
19078 lastmatch = rest;
19079 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019080 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019081
19082 if (lastmatch == NULL)
19083 rettv->vval.v_number = -1;
19084 else
19085 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19086}
19087
19088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089 * "strtrans()" function
19090 */
19091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019092f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019093 typval_T *argvars;
19094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019096 rettv->v_type = VAR_STRING;
19097 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098}
19099
19100/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019101 * "submatch()" function
19102 */
19103 static void
19104f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019105 typval_T *argvars;
19106 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019107{
Bram Moolenaar41571762014-04-02 19:00:58 +020019108 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019109 int no;
19110 int retList = 0;
19111
19112 no = (int)get_tv_number_chk(&argvars[0], &error);
19113 if (error)
19114 return;
19115 error = FALSE;
19116 if (argvars[1].v_type != VAR_UNKNOWN)
19117 retList = get_tv_number_chk(&argvars[1], &error);
19118 if (error)
19119 return;
19120
19121 if (retList == 0)
19122 {
19123 rettv->v_type = VAR_STRING;
19124 rettv->vval.v_string = reg_submatch(no);
19125 }
19126 else
19127 {
19128 rettv->v_type = VAR_LIST;
19129 rettv->vval.v_list = reg_submatch_list(no);
19130 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019131}
19132
19133/*
19134 * "substitute()" function
19135 */
19136 static void
19137f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019138 typval_T *argvars;
19139 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019140{
19141 char_u patbuf[NUMBUFLEN];
19142 char_u subbuf[NUMBUFLEN];
19143 char_u flagsbuf[NUMBUFLEN];
19144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019145 char_u *str = get_tv_string_chk(&argvars[0]);
19146 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19147 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19148 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19149
Bram Moolenaar0d660222005-01-07 21:51:51 +000019150 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019151 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19152 rettv->vval.v_string = NULL;
19153 else
19154 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019155}
19156
19157/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019158 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019161f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019162 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164{
19165 int id = 0;
19166#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019167 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 long col;
19169 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019170 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019172 lnum = get_tv_lnum(argvars); /* -1 on type error */
19173 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19174 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019176 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019177 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019178 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179#endif
19180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019181 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182}
19183
19184/*
19185 * "synIDattr(id, what [, mode])" function
19186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019188f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019189 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191{
19192 char_u *p = NULL;
19193#ifdef FEAT_SYN_HL
19194 int id;
19195 char_u *what;
19196 char_u *mode;
19197 char_u modebuf[NUMBUFLEN];
19198 int modec;
19199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019200 id = get_tv_number(&argvars[0]);
19201 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019202 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019204 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019206 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 modec = 0; /* replace invalid with current */
19208 }
19209 else
19210 {
19211#ifdef FEAT_GUI
19212 if (gui.in_use)
19213 modec = 'g';
19214 else
19215#endif
19216 if (t_colors > 1)
19217 modec = 'c';
19218 else
19219 modec = 't';
19220 }
19221
19222
19223 switch (TOLOWER_ASC(what[0]))
19224 {
19225 case 'b':
19226 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19227 p = highlight_color(id, what, modec);
19228 else /* bold */
19229 p = highlight_has_attr(id, HL_BOLD, modec);
19230 break;
19231
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019232 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233 p = highlight_color(id, what, modec);
19234 break;
19235
19236 case 'i':
19237 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19238 p = highlight_has_attr(id, HL_INVERSE, modec);
19239 else /* italic */
19240 p = highlight_has_attr(id, HL_ITALIC, modec);
19241 break;
19242
19243 case 'n': /* name */
19244 p = get_highlight_name(NULL, id - 1);
19245 break;
19246
19247 case 'r': /* reverse */
19248 p = highlight_has_attr(id, HL_INVERSE, modec);
19249 break;
19250
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019251 case 's':
19252 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19253 p = highlight_color(id, what, modec);
19254 else /* standout */
19255 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256 break;
19257
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019258 case 'u':
19259 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19260 /* underline */
19261 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19262 else
19263 /* undercurl */
19264 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 break;
19266 }
19267
19268 if (p != NULL)
19269 p = vim_strsave(p);
19270#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019271 rettv->v_type = VAR_STRING;
19272 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273}
19274
19275/*
19276 * "synIDtrans(id)" function
19277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019279f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019280 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282{
19283 int id;
19284
19285#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019286 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287
19288 if (id > 0)
19289 id = syn_get_final_id(id);
19290 else
19291#endif
19292 id = 0;
19293
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019294 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295}
19296
19297/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019298 * "synconcealed(lnum, col)" function
19299 */
19300 static void
19301f_synconcealed(argvars, rettv)
19302 typval_T *argvars UNUSED;
19303 typval_T *rettv;
19304{
19305#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19306 long lnum;
19307 long col;
19308 int syntax_flags = 0;
19309 int cchar;
19310 int matchid = 0;
19311 char_u str[NUMBUFLEN];
19312#endif
19313
19314 rettv->v_type = VAR_LIST;
19315 rettv->vval.v_list = NULL;
19316
19317#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19318 lnum = get_tv_lnum(argvars); /* -1 on type error */
19319 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19320
19321 vim_memset(str, NUL, sizeof(str));
19322
19323 if (rettv_list_alloc(rettv) != FAIL)
19324 {
19325 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19326 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19327 && curwin->w_p_cole > 0)
19328 {
19329 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19330 syntax_flags = get_syntax_info(&matchid);
19331
19332 /* get the conceal character */
19333 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19334 {
19335 cchar = syn_get_sub_char();
19336 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19337 cchar = lcs_conceal;
19338 if (cchar != NUL)
19339 {
19340# ifdef FEAT_MBYTE
19341 if (has_mbyte)
19342 (*mb_char2bytes)(cchar, str);
19343 else
19344# endif
19345 str[0] = cchar;
19346 }
19347 }
19348 }
19349
19350 list_append_number(rettv->vval.v_list,
19351 (syntax_flags & HL_CONCEAL) != 0);
19352 /* -1 to auto-determine strlen */
19353 list_append_string(rettv->vval.v_list, str, -1);
19354 list_append_number(rettv->vval.v_list, matchid);
19355 }
19356#endif
19357}
19358
19359/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019360 * "synstack(lnum, col)" function
19361 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019362 static void
19363f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019364 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019365 typval_T *rettv;
19366{
19367#ifdef FEAT_SYN_HL
19368 long lnum;
19369 long col;
19370 int i;
19371 int id;
19372#endif
19373
19374 rettv->v_type = VAR_LIST;
19375 rettv->vval.v_list = NULL;
19376
19377#ifdef FEAT_SYN_HL
19378 lnum = get_tv_lnum(argvars); /* -1 on type error */
19379 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19380
19381 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019382 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019383 && rettv_list_alloc(rettv) != FAIL)
19384 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019385 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019386 for (i = 0; ; ++i)
19387 {
19388 id = syn_get_stack_item(i);
19389 if (id < 0)
19390 break;
19391 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19392 break;
19393 }
19394 }
19395#endif
19396}
19397
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019399get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019400 typval_T *argvars;
19401 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019402 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019404 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019406 char_u *infile = NULL;
19407 char_u buf[NUMBUFLEN];
19408 int err = FALSE;
19409 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019410 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019411 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019413 rettv->v_type = VAR_STRING;
19414 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019415 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019416 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019417
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019418 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019419 {
19420 /*
19421 * Write the string to a temp file, to be used for input of the shell
19422 * command.
19423 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019424 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019425 {
19426 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019427 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019428 }
19429
19430 fd = mch_fopen((char *)infile, WRITEBIN);
19431 if (fd == NULL)
19432 {
19433 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019434 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019435 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019436 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019437 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019438 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19439 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019440 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019441 else
19442 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019443 size_t len;
19444
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019445 p = get_tv_string_buf_chk(&argvars[1], buf);
19446 if (p == NULL)
19447 {
19448 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019449 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019450 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019451 len = STRLEN(p);
19452 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019453 err = TRUE;
19454 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019455 if (fclose(fd) != 0)
19456 err = TRUE;
19457 if (err)
19458 {
19459 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019460 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019461 }
19462 }
19463
Bram Moolenaar52a72462014-08-29 15:53:52 +020019464 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19465 * echoes typeahead, that messes up the display. */
19466 if (!msg_silent)
19467 flags += SHELL_COOKED;
19468
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019469 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019471 int len;
19472 listitem_T *li;
19473 char_u *s = NULL;
19474 char_u *start;
19475 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019476 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477
Bram Moolenaar52a72462014-08-29 15:53:52 +020019478 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019479 if (res == NULL)
19480 goto errret;
19481
19482 list = list_alloc();
19483 if (list == NULL)
19484 goto errret;
19485
19486 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019488 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019489 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019490 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019491 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019492
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019493 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019494 if (s == NULL)
19495 goto errret;
19496
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019497 for (p = s; start < end; ++p, ++start)
19498 *p = *start == NUL ? NL : *start;
19499 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019500
19501 li = listitem_alloc();
19502 if (li == NULL)
19503 {
19504 vim_free(s);
19505 goto errret;
19506 }
19507 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019508 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019509 li->li_tv.vval.v_string = s;
19510 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019512
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019513 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019514 rettv->v_type = VAR_LIST;
19515 rettv->vval.v_list = list;
19516 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019518 else
19519 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019520 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019521#ifdef USE_CR
19522 /* translate <CR> into <NL> */
19523 if (res != NULL)
19524 {
19525 char_u *s;
19526
19527 for (s = res; *s; ++s)
19528 {
19529 if (*s == CAR)
19530 *s = NL;
19531 }
19532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533#else
19534# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019535 /* translate <CR><NL> into <NL> */
19536 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019538 char_u *s, *d;
19539
19540 d = res;
19541 for (s = res; *s; ++s)
19542 {
19543 if (s[0] == CAR && s[1] == NL)
19544 ++s;
19545 *d++ = *s;
19546 }
19547 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019549# endif
19550#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019551 rettv->vval.v_string = res;
19552 res = NULL;
19553 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019554
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019555errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019556 if (infile != NULL)
19557 {
19558 mch_remove(infile);
19559 vim_free(infile);
19560 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019561 if (res != NULL)
19562 vim_free(res);
19563 if (list != NULL)
19564 list_free(list, TRUE);
19565}
19566
19567/*
19568 * "system()" function
19569 */
19570 static void
19571f_system(argvars, rettv)
19572 typval_T *argvars;
19573 typval_T *rettv;
19574{
19575 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19576}
19577
19578/*
19579 * "systemlist()" function
19580 */
19581 static void
19582f_systemlist(argvars, rettv)
19583 typval_T *argvars;
19584 typval_T *rettv;
19585{
19586 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587}
19588
19589/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019590 * "tabpagebuflist()" function
19591 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019592 static void
19593f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019594 typval_T *argvars UNUSED;
19595 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019596{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019597#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019598 tabpage_T *tp;
19599 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019600
19601 if (argvars[0].v_type == VAR_UNKNOWN)
19602 wp = firstwin;
19603 else
19604 {
19605 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19606 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019607 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019608 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019609 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019610 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019611 for (; wp != NULL; wp = wp->w_next)
19612 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019613 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019614 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019615 }
19616#endif
19617}
19618
19619
19620/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019621 * "tabpagenr()" function
19622 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019623 static void
19624f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019625 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019626 typval_T *rettv;
19627{
19628 int nr = 1;
19629#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019630 char_u *arg;
19631
19632 if (argvars[0].v_type != VAR_UNKNOWN)
19633 {
19634 arg = get_tv_string_chk(&argvars[0]);
19635 nr = 0;
19636 if (arg != NULL)
19637 {
19638 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019639 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019640 else
19641 EMSG2(_(e_invexpr2), arg);
19642 }
19643 }
19644 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019645 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019646#endif
19647 rettv->vval.v_number = nr;
19648}
19649
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019650
19651#ifdef FEAT_WINDOWS
19652static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19653
19654/*
19655 * Common code for tabpagewinnr() and winnr().
19656 */
19657 static int
19658get_winnr(tp, argvar)
19659 tabpage_T *tp;
19660 typval_T *argvar;
19661{
19662 win_T *twin;
19663 int nr = 1;
19664 win_T *wp;
19665 char_u *arg;
19666
19667 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19668 if (argvar->v_type != VAR_UNKNOWN)
19669 {
19670 arg = get_tv_string_chk(argvar);
19671 if (arg == NULL)
19672 nr = 0; /* type error; errmsg already given */
19673 else if (STRCMP(arg, "$") == 0)
19674 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19675 else if (STRCMP(arg, "#") == 0)
19676 {
19677 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19678 if (twin == NULL)
19679 nr = 0;
19680 }
19681 else
19682 {
19683 EMSG2(_(e_invexpr2), arg);
19684 nr = 0;
19685 }
19686 }
19687
19688 if (nr > 0)
19689 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19690 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019691 {
19692 if (wp == NULL)
19693 {
19694 /* didn't find it in this tabpage */
19695 nr = 0;
19696 break;
19697 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019698 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019699 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019700 return nr;
19701}
19702#endif
19703
19704/*
19705 * "tabpagewinnr()" function
19706 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019707 static void
19708f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019709 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019710 typval_T *rettv;
19711{
19712 int nr = 1;
19713#ifdef FEAT_WINDOWS
19714 tabpage_T *tp;
19715
19716 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19717 if (tp == NULL)
19718 nr = 0;
19719 else
19720 nr = get_winnr(tp, &argvars[1]);
19721#endif
19722 rettv->vval.v_number = nr;
19723}
19724
19725
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019726/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019727 * "tagfiles()" function
19728 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019729 static void
19730f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019731 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019732 typval_T *rettv;
19733{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019734 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019735 tagname_T tn;
19736 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019737
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019738 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019739 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019740 fname = alloc(MAXPATHL);
19741 if (fname == NULL)
19742 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019743
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019744 for (first = TRUE; ; first = FALSE)
19745 if (get_tagfname(&tn, first, fname) == FAIL
19746 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019747 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019748 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019749 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019750}
19751
19752/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019753 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019754 */
19755 static void
19756f_taglist(argvars, rettv)
19757 typval_T *argvars;
19758 typval_T *rettv;
19759{
19760 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019761
19762 tag_pattern = get_tv_string(&argvars[0]);
19763
19764 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019765 if (*tag_pattern == NUL)
19766 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019767
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019768 if (rettv_list_alloc(rettv) == OK)
19769 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019770}
19771
19772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 * "tempname()" function
19774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019776f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019777 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779{
19780 static int x = 'A';
19781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019782 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019783 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784
19785 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19786 * names. Skip 'I' and 'O', they are used for shell redirection. */
19787 do
19788 {
19789 if (x == 'Z')
19790 x = '0';
19791 else if (x == '9')
19792 x = 'A';
19793 else
19794 {
19795#ifdef EBCDIC
19796 if (x == 'I')
19797 x = 'J';
19798 else if (x == 'R')
19799 x = 'S';
19800 else
19801#endif
19802 ++x;
19803 }
19804 } while (x == 'I' || x == 'O');
19805}
19806
19807/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019808 * "test(list)" function: Just checking the walls...
19809 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019810 static void
19811f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019812 typval_T *argvars UNUSED;
19813 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019814{
19815 /* Used for unit testing. Change the code below to your liking. */
19816#if 0
19817 listitem_T *li;
19818 list_T *l;
19819 char_u *bad, *good;
19820
19821 if (argvars[0].v_type != VAR_LIST)
19822 return;
19823 l = argvars[0].vval.v_list;
19824 if (l == NULL)
19825 return;
19826 li = l->lv_first;
19827 if (li == NULL)
19828 return;
19829 bad = get_tv_string(&li->li_tv);
19830 li = li->li_next;
19831 if (li == NULL)
19832 return;
19833 good = get_tv_string(&li->li_tv);
19834 rettv->vval.v_number = test_edit_score(bad, good);
19835#endif
19836}
19837
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019838#ifdef FEAT_FLOAT
19839/*
19840 * "tan()" function
19841 */
19842 static void
19843f_tan(argvars, rettv)
19844 typval_T *argvars;
19845 typval_T *rettv;
19846{
19847 float_T f;
19848
19849 rettv->v_type = VAR_FLOAT;
19850 if (get_float_arg(argvars, &f) == OK)
19851 rettv->vval.v_float = tan(f);
19852 else
19853 rettv->vval.v_float = 0.0;
19854}
19855
19856/*
19857 * "tanh()" function
19858 */
19859 static void
19860f_tanh(argvars, rettv)
19861 typval_T *argvars;
19862 typval_T *rettv;
19863{
19864 float_T f;
19865
19866 rettv->v_type = VAR_FLOAT;
19867 if (get_float_arg(argvars, &f) == OK)
19868 rettv->vval.v_float = tanh(f);
19869 else
19870 rettv->vval.v_float = 0.0;
19871}
19872#endif
19873
Bram Moolenaard52d9742005-08-21 22:20:28 +000019874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 * "tolower(string)" function
19876 */
19877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019878f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019879 typval_T *argvars;
19880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881{
19882 char_u *p;
19883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019884 p = vim_strsave(get_tv_string(&argvars[0]));
19885 rettv->v_type = VAR_STRING;
19886 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887
19888 if (p != NULL)
19889 while (*p != NUL)
19890 {
19891#ifdef FEAT_MBYTE
19892 int l;
19893
19894 if (enc_utf8)
19895 {
19896 int c, lc;
19897
19898 c = utf_ptr2char(p);
19899 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019900 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 /* TODO: reallocate string when byte count changes. */
19902 if (utf_char2len(lc) == l)
19903 utf_char2bytes(lc, p);
19904 p += l;
19905 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019906 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 p += l; /* skip multi-byte character */
19908 else
19909#endif
19910 {
19911 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19912 ++p;
19913 }
19914 }
19915}
19916
19917/*
19918 * "toupper(string)" function
19919 */
19920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019921f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019922 typval_T *argvars;
19923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019925 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019926 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927}
19928
19929/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019930 * "tr(string, fromstr, tostr)" function
19931 */
19932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019933f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019934 typval_T *argvars;
19935 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019936{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019937 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019938 char_u *fromstr;
19939 char_u *tostr;
19940 char_u *p;
19941#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019942 int inlen;
19943 int fromlen;
19944 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019945 int idx;
19946 char_u *cpstr;
19947 int cplen;
19948 int first = TRUE;
19949#endif
19950 char_u buf[NUMBUFLEN];
19951 char_u buf2[NUMBUFLEN];
19952 garray_T ga;
19953
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019954 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019955 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19956 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019957
19958 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019959 rettv->v_type = VAR_STRING;
19960 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019961 if (fromstr == NULL || tostr == NULL)
19962 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019963 ga_init2(&ga, (int)sizeof(char), 80);
19964
19965#ifdef FEAT_MBYTE
19966 if (!has_mbyte)
19967#endif
19968 /* not multi-byte: fromstr and tostr must be the same length */
19969 if (STRLEN(fromstr) != STRLEN(tostr))
19970 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019971#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019972error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019973#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019974 EMSG2(_(e_invarg2), fromstr);
19975 ga_clear(&ga);
19976 return;
19977 }
19978
19979 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019980 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019981 {
19982#ifdef FEAT_MBYTE
19983 if (has_mbyte)
19984 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019985 inlen = (*mb_ptr2len)(in_str);
19986 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019987 cplen = inlen;
19988 idx = 0;
19989 for (p = fromstr; *p != NUL; p += fromlen)
19990 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019991 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019992 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019993 {
19994 for (p = tostr; *p != NUL; p += tolen)
19995 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019996 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019997 if (idx-- == 0)
19998 {
19999 cplen = tolen;
20000 cpstr = p;
20001 break;
20002 }
20003 }
20004 if (*p == NUL) /* tostr is shorter than fromstr */
20005 goto error;
20006 break;
20007 }
20008 ++idx;
20009 }
20010
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020011 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020012 {
20013 /* Check that fromstr and tostr have the same number of
20014 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020015 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020016 first = FALSE;
20017 for (p = tostr; *p != NUL; p += tolen)
20018 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020019 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020020 --idx;
20021 }
20022 if (idx != 0)
20023 goto error;
20024 }
20025
Bram Moolenaarcde88542015-08-11 19:14:00 +020020026 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020027 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020028 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020029
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020030 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020031 }
20032 else
20033#endif
20034 {
20035 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020036 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020037 if (p != NULL)
20038 ga_append(&ga, tostr[p - fromstr]);
20039 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020040 ga_append(&ga, *in_str);
20041 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020042 }
20043 }
20044
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020045 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020046 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020047 ga_append(&ga, NUL);
20048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020049 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020050}
20051
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020052#ifdef FEAT_FLOAT
20053/*
20054 * "trunc({float})" function
20055 */
20056 static void
20057f_trunc(argvars, rettv)
20058 typval_T *argvars;
20059 typval_T *rettv;
20060{
20061 float_T f;
20062
20063 rettv->v_type = VAR_FLOAT;
20064 if (get_float_arg(argvars, &f) == OK)
20065 /* trunc() is not in C90, use floor() or ceil() instead. */
20066 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20067 else
20068 rettv->vval.v_float = 0.0;
20069}
20070#endif
20071
Bram Moolenaar8299df92004-07-10 09:47:34 +000020072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073 * "type(expr)" function
20074 */
20075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020076f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020077 typval_T *argvars;
20078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020080 int n;
20081
20082 switch (argvars[0].v_type)
20083 {
20084 case VAR_NUMBER: n = 0; break;
20085 case VAR_STRING: n = 1; break;
20086 case VAR_FUNC: n = 2; break;
20087 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020088 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020089#ifdef FEAT_FLOAT
20090 case VAR_FLOAT: n = 5; break;
20091#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020092 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
20093 }
20094 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095}
20096
20097/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020098 * "undofile(name)" function
20099 */
20100 static void
20101f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010020102 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020103 typval_T *rettv;
20104{
20105 rettv->v_type = VAR_STRING;
20106#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020107 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020108 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020109
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020110 if (*fname == NUL)
20111 {
20112 /* If there is no file name there will be no undo file. */
20113 rettv->vval.v_string = NULL;
20114 }
20115 else
20116 {
20117 char_u *ffname = FullName_save(fname, FALSE);
20118
20119 if (ffname != NULL)
20120 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20121 vim_free(ffname);
20122 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020123 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020124#else
20125 rettv->vval.v_string = NULL;
20126#endif
20127}
20128
20129/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020130 * "undotree()" function
20131 */
20132 static void
20133f_undotree(argvars, rettv)
20134 typval_T *argvars UNUSED;
20135 typval_T *rettv;
20136{
20137 if (rettv_dict_alloc(rettv) == OK)
20138 {
20139 dict_T *dict = rettv->vval.v_dict;
20140 list_T *list;
20141
Bram Moolenaar730cde92010-06-27 05:18:54 +020020142 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020143 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020144 dict_add_nr_str(dict, "save_last",
20145 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020146 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20147 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020148 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020149
20150 list = list_alloc();
20151 if (list != NULL)
20152 {
20153 u_eval_tree(curbuf->b_u_oldhead, list);
20154 dict_add_list(dict, "entries", list);
20155 }
20156 }
20157}
20158
20159/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020160 * "values(dict)" function
20161 */
20162 static void
20163f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020164 typval_T *argvars;
20165 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020166{
20167 dict_list(argvars, rettv, 1);
20168}
20169
20170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 * "virtcol(string)" function
20172 */
20173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020174f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020175 typval_T *argvars;
20176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177{
20178 colnr_T vcol = 0;
20179 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020180 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020182 fp = var2fpos(&argvars[0], FALSE, &fnum);
20183 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20184 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 {
20186 getvvcol(curwin, fp, NULL, NULL, &vcol);
20187 ++vcol;
20188 }
20189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020190 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191}
20192
20193/*
20194 * "visualmode()" function
20195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020197f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020198 typval_T *argvars UNUSED;
20199 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 char_u str[2];
20202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020203 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 str[0] = curbuf->b_visual_mode_eval;
20205 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020206 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207
20208 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020209 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211}
20212
20213/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020214 * "wildmenumode()" function
20215 */
20216 static void
20217f_wildmenumode(argvars, rettv)
20218 typval_T *argvars UNUSED;
20219 typval_T *rettv UNUSED;
20220{
20221#ifdef FEAT_WILDMENU
20222 if (wild_menu_showing)
20223 rettv->vval.v_number = 1;
20224#endif
20225}
20226
20227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 * "winbufnr(nr)" function
20229 */
20230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020231f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020232 typval_T *argvars;
20233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234{
20235 win_T *wp;
20236
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020237 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020239 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020241 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242}
20243
20244/*
20245 * "wincol()" function
20246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020248f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020249 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251{
20252 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020253 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254}
20255
20256/*
20257 * "winheight(nr)" function
20258 */
20259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020260f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020261 typval_T *argvars;
20262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263{
20264 win_T *wp;
20265
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020266 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020268 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020270 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271}
20272
20273/*
20274 * "winline()" function
20275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020277f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020278 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280{
20281 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020282 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283}
20284
20285/*
20286 * "winnr()" function
20287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020289f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020290 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292{
20293 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020294
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020296 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020298 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299}
20300
20301/*
20302 * "winrestcmd()" function
20303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020305f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020306 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308{
20309#ifdef FEAT_WINDOWS
20310 win_T *wp;
20311 int winnr = 1;
20312 garray_T ga;
20313 char_u buf[50];
20314
20315 ga_init2(&ga, (int)sizeof(char), 70);
20316 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20317 {
20318 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20319 ga_concat(&ga, buf);
20320# ifdef FEAT_VERTSPLIT
20321 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20322 ga_concat(&ga, buf);
20323# endif
20324 ++winnr;
20325 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020326 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020328 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020330 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020332 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333}
20334
20335/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020336 * "winrestview()" function
20337 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020338 static void
20339f_winrestview(argvars, rettv)
20340 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020341 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020342{
20343 dict_T *dict;
20344
20345 if (argvars[0].v_type != VAR_DICT
20346 || (dict = argvars[0].vval.v_dict) == NULL)
20347 EMSG(_(e_invarg));
20348 else
20349 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020350 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20351 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20352 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20353 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020354#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020355 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20356 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020357#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020358 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20359 {
20360 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20361 curwin->w_set_curswant = FALSE;
20362 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020363
Bram Moolenaar82c25852014-05-28 16:47:16 +020020364 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20365 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020366#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020367 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20368 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020369#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020370 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20371 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20372 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20373 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020374
20375 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020376 win_new_height(curwin, curwin->w_height);
20377# ifdef FEAT_VERTSPLIT
20378 win_new_width(curwin, W_WIDTH(curwin));
20379# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020380 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020381
Bram Moolenaarb851a962014-10-31 15:45:52 +010020382 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020383 curwin->w_topline = 1;
20384 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20385 curwin->w_topline = curbuf->b_ml.ml_line_count;
20386#ifdef FEAT_DIFF
20387 check_topfill(curwin, TRUE);
20388#endif
20389 }
20390}
20391
20392/*
20393 * "winsaveview()" function
20394 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020395 static void
20396f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020397 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020398 typval_T *rettv;
20399{
20400 dict_T *dict;
20401
Bram Moolenaara800b422010-06-27 01:15:55 +020020402 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020403 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020404 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020405
20406 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20407 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20408#ifdef FEAT_VIRTUALEDIT
20409 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20410#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020411 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020412 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20413
20414 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20415#ifdef FEAT_DIFF
20416 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20417#endif
20418 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20419 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20420}
20421
20422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 * "winwidth(nr)" function
20424 */
20425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020426f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020427 typval_T *argvars;
20428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429{
20430 win_T *wp;
20431
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020432 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020434 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 else
20436#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020437 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020439 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440#endif
20441}
20442
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020444 * "wordcount()" function
20445 */
20446 static void
20447f_wordcount(argvars, rettv)
20448 typval_T *argvars UNUSED;
20449 typval_T *rettv;
20450{
20451 if (rettv_dict_alloc(rettv) == FAIL)
20452 return;
20453 cursor_pos_info(rettv->vval.v_dict);
20454}
20455
20456/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020457 * Write list of strings to file
20458 */
20459 static int
20460write_list(fd, list, binary)
20461 FILE *fd;
20462 list_T *list;
20463 int binary;
20464{
20465 listitem_T *li;
20466 int c;
20467 int ret = OK;
20468 char_u *s;
20469
20470 for (li = list->lv_first; li != NULL; li = li->li_next)
20471 {
20472 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20473 {
20474 if (*s == '\n')
20475 c = putc(NUL, fd);
20476 else
20477 c = putc(*s, fd);
20478 if (c == EOF)
20479 {
20480 ret = FAIL;
20481 break;
20482 }
20483 }
20484 if (!binary || li->li_next != NULL)
20485 if (putc('\n', fd) == EOF)
20486 {
20487 ret = FAIL;
20488 break;
20489 }
20490 if (ret == FAIL)
20491 {
20492 EMSG(_(e_write));
20493 break;
20494 }
20495 }
20496 return ret;
20497}
20498
20499/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020500 * "writefile()" function
20501 */
20502 static void
20503f_writefile(argvars, rettv)
20504 typval_T *argvars;
20505 typval_T *rettv;
20506{
20507 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020508 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020509 char_u *fname;
20510 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020511 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020512
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020513 if (check_restricted() || check_secure())
20514 return;
20515
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020516 if (argvars[0].v_type != VAR_LIST)
20517 {
20518 EMSG2(_(e_listarg), "writefile()");
20519 return;
20520 }
20521 if (argvars[0].vval.v_list == NULL)
20522 return;
20523
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020524 if (argvars[2].v_type != VAR_UNKNOWN)
20525 {
20526 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20527 binary = TRUE;
20528 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20529 append = TRUE;
20530 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020531
20532 /* Always open the file in binary mode, library functions have a mind of
20533 * their own about CR-LF conversion. */
20534 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020535 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20536 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020537 {
20538 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20539 ret = -1;
20540 }
20541 else
20542 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020543 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20544 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020545 fclose(fd);
20546 }
20547
20548 rettv->vval.v_number = ret;
20549}
20550
20551/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020552 * "xor(expr, expr)" function
20553 */
20554 static void
20555f_xor(argvars, rettv)
20556 typval_T *argvars;
20557 typval_T *rettv;
20558{
20559 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20560 ^ get_tv_number_chk(&argvars[1], NULL);
20561}
20562
20563
20564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020566 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 */
20568 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020569var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020570 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020571 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020572 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020574 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020576 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577
Bram Moolenaara5525202006-03-02 22:52:09 +000020578 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020579 if (varp->v_type == VAR_LIST)
20580 {
20581 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020582 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020583 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020584 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020585
20586 l = varp->vval.v_list;
20587 if (l == NULL)
20588 return NULL;
20589
20590 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020591 pos.lnum = list_find_nr(l, 0L, &error);
20592 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020593 return NULL; /* invalid line number */
20594
20595 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020596 pos.col = list_find_nr(l, 1L, &error);
20597 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020598 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020599 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020600
20601 /* We accept "$" for the column number: last column. */
20602 li = list_find(l, 1L);
20603 if (li != NULL && li->li_tv.v_type == VAR_STRING
20604 && li->li_tv.vval.v_string != NULL
20605 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20606 pos.col = len + 1;
20607
Bram Moolenaara5525202006-03-02 22:52:09 +000020608 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020609 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020610 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020611 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020612
Bram Moolenaara5525202006-03-02 22:52:09 +000020613#ifdef FEAT_VIRTUALEDIT
20614 /* Get the virtual offset. Defaults to zero. */
20615 pos.coladd = list_find_nr(l, 2L, &error);
20616 if (error)
20617 pos.coladd = 0;
20618#endif
20619
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020620 return &pos;
20621 }
20622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020623 name = get_tv_string_chk(varp);
20624 if (name == NULL)
20625 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020626 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020628 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20629 {
20630 if (VIsual_active)
20631 return &VIsual;
20632 return &curwin->w_cursor;
20633 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020634 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020636 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20638 return NULL;
20639 return pp;
20640 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020641
20642#ifdef FEAT_VIRTUALEDIT
20643 pos.coladd = 0;
20644#endif
20645
Bram Moolenaar477933c2007-07-17 14:32:23 +000020646 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020647 {
20648 pos.col = 0;
20649 if (name[1] == '0') /* "w0": first visible line */
20650 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020651 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020652 pos.lnum = curwin->w_topline;
20653 return &pos;
20654 }
20655 else if (name[1] == '$') /* "w$": last visible line */
20656 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020657 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020658 pos.lnum = curwin->w_botline - 1;
20659 return &pos;
20660 }
20661 }
20662 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020664 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020665 {
20666 pos.lnum = curbuf->b_ml.ml_line_count;
20667 pos.col = 0;
20668 }
20669 else
20670 {
20671 pos.lnum = curwin->w_cursor.lnum;
20672 pos.col = (colnr_T)STRLEN(ml_get_curline());
20673 }
20674 return &pos;
20675 }
20676 return NULL;
20677}
20678
20679/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020680 * Convert list in "arg" into a position and optional file number.
20681 * When "fnump" is NULL there is no file number, only 3 items.
20682 * Note that the column is passed on as-is, the caller may want to decrement
20683 * it to use 1 for the first column.
20684 * Return FAIL when conversion is not possible, doesn't check the position for
20685 * validity.
20686 */
20687 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020688list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020689 typval_T *arg;
20690 pos_T *posp;
20691 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020692 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020693{
20694 list_T *l = arg->vval.v_list;
20695 long i = 0;
20696 long n;
20697
Bram Moolenaar493c1782014-05-28 14:34:46 +020020698 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20699 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020700 if (arg->v_type != VAR_LIST
20701 || l == NULL
20702 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020703 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020704 return FAIL;
20705
20706 if (fnump != NULL)
20707 {
20708 n = list_find_nr(l, i++, NULL); /* fnum */
20709 if (n < 0)
20710 return FAIL;
20711 if (n == 0)
20712 n = curbuf->b_fnum; /* current buffer */
20713 *fnump = n;
20714 }
20715
20716 n = list_find_nr(l, i++, NULL); /* lnum */
20717 if (n < 0)
20718 return FAIL;
20719 posp->lnum = n;
20720
20721 n = list_find_nr(l, i++, NULL); /* col */
20722 if (n < 0)
20723 return FAIL;
20724 posp->col = n;
20725
20726#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020727 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020728 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020729 posp->coladd = 0;
20730 else
20731 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020732#endif
20733
Bram Moolenaar493c1782014-05-28 14:34:46 +020020734 if (curswantp != NULL)
20735 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20736
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020737 return OK;
20738}
20739
20740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741 * Get the length of an environment variable name.
20742 * Advance "arg" to the first character after the name.
20743 * Return 0 for error.
20744 */
20745 static int
20746get_env_len(arg)
20747 char_u **arg;
20748{
20749 char_u *p;
20750 int len;
20751
20752 for (p = *arg; vim_isIDc(*p); ++p)
20753 ;
20754 if (p == *arg) /* no name found */
20755 return 0;
20756
20757 len = (int)(p - *arg);
20758 *arg = p;
20759 return len;
20760}
20761
20762/*
20763 * Get the length of the name of a function or internal variable.
20764 * "arg" is advanced to the first non-white character after the name.
20765 * Return 0 if something is wrong.
20766 */
20767 static int
20768get_id_len(arg)
20769 char_u **arg;
20770{
20771 char_u *p;
20772 int len;
20773
20774 /* Find the end of the name. */
20775 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020776 {
20777 if (*p == ':')
20778 {
20779 /* "s:" is start of "s:var", but "n:" is not and can be used in
20780 * slice "[n:]". Also "xx:" is not a namespace. */
20781 len = (int)(p - *arg);
20782 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20783 || len > 1)
20784 break;
20785 }
20786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 if (p == *arg) /* no name found */
20788 return 0;
20789
20790 len = (int)(p - *arg);
20791 *arg = skipwhite(p);
20792
20793 return len;
20794}
20795
20796/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020797 * Get the length of the name of a variable or function.
20798 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020800 * Return -1 if curly braces expansion failed.
20801 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802 * If the name contains 'magic' {}'s, expand them and return the
20803 * expanded name in an allocated string via 'alias' - caller must free.
20804 */
20805 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020806get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 char_u **arg;
20808 char_u **alias;
20809 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020810 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811{
20812 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813 char_u *p;
20814 char_u *expr_start;
20815 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816
20817 *alias = NULL; /* default to no alias */
20818
20819 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20820 && (*arg)[2] == (int)KE_SNR)
20821 {
20822 /* hard coded <SNR>, already translated */
20823 *arg += 3;
20824 return get_id_len(arg) + 3;
20825 }
20826 len = eval_fname_script(*arg);
20827 if (len > 0)
20828 {
20829 /* literal "<SID>", "s:" or "<SNR>" */
20830 *arg += len;
20831 }
20832
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020834 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020836 p = find_name_end(*arg, &expr_start, &expr_end,
20837 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838 if (expr_start != NULL)
20839 {
20840 char_u *temp_string;
20841
20842 if (!evaluate)
20843 {
20844 len += (int)(p - *arg);
20845 *arg = skipwhite(p);
20846 return len;
20847 }
20848
20849 /*
20850 * Include any <SID> etc in the expanded string:
20851 * Thus the -len here.
20852 */
20853 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20854 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020855 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856 *alias = temp_string;
20857 *arg = skipwhite(p);
20858 return (int)STRLEN(temp_string);
20859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860
20861 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020862 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 EMSG2(_(e_invexpr2), *arg);
20864
20865 return len;
20866}
20867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020868/*
20869 * Find the end of a variable or function name, taking care of magic braces.
20870 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20871 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020872 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020873 * Return a pointer to just after the name. Equal to "arg" if there is no
20874 * valid name.
20875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020877find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878 char_u *arg;
20879 char_u **expr_start;
20880 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020881 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020883 int mb_nest = 0;
20884 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020886 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020888 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020890 *expr_start = NULL;
20891 *expr_end = NULL;
20892 }
20893
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020894 /* Quick check for valid starting character. */
20895 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20896 return arg;
20897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020898 for (p = arg; *p != NUL
20899 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020900 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020901 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020902 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020903 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020904 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020905 if (*p == '\'')
20906 {
20907 /* skip over 'string' to avoid counting [ and ] inside it. */
20908 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20909 ;
20910 if (*p == NUL)
20911 break;
20912 }
20913 else if (*p == '"')
20914 {
20915 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20916 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20917 if (*p == '\\' && p[1] != NUL)
20918 ++p;
20919 if (*p == NUL)
20920 break;
20921 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020922 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20923 {
20924 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020925 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020926 len = (int)(p - arg);
20927 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020928 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020929 break;
20930 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020932 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020934 if (*p == '[')
20935 ++br_nest;
20936 else if (*p == ']')
20937 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020940 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020942 if (*p == '{')
20943 {
20944 mb_nest++;
20945 if (expr_start != NULL && *expr_start == NULL)
20946 *expr_start = p;
20947 }
20948 else if (*p == '}')
20949 {
20950 mb_nest--;
20951 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20952 *expr_end = p;
20953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955 }
20956
20957 return p;
20958}
20959
20960/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020961 * Expands out the 'magic' {}'s in a variable/function name.
20962 * Note that this can call itself recursively, to deal with
20963 * constructs like foo{bar}{baz}{bam}
20964 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20965 * "in_start" ^
20966 * "expr_start" ^
20967 * "expr_end" ^
20968 * "in_end" ^
20969 *
20970 * Returns a new allocated string, which the caller must free.
20971 * Returns NULL for failure.
20972 */
20973 static char_u *
20974make_expanded_name(in_start, expr_start, expr_end, in_end)
20975 char_u *in_start;
20976 char_u *expr_start;
20977 char_u *expr_end;
20978 char_u *in_end;
20979{
20980 char_u c1;
20981 char_u *retval = NULL;
20982 char_u *temp_result;
20983 char_u *nextcmd = NULL;
20984
20985 if (expr_end == NULL || in_end == NULL)
20986 return NULL;
20987 *expr_start = NUL;
20988 *expr_end = NUL;
20989 c1 = *in_end;
20990 *in_end = NUL;
20991
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020992 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020993 if (temp_result != NULL && nextcmd == NULL)
20994 {
20995 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20996 + (in_end - expr_end) + 1));
20997 if (retval != NULL)
20998 {
20999 STRCPY(retval, in_start);
21000 STRCAT(retval, temp_result);
21001 STRCAT(retval, expr_end + 1);
21002 }
21003 }
21004 vim_free(temp_result);
21005
21006 *in_end = c1; /* put char back for error messages */
21007 *expr_start = '{';
21008 *expr_end = '}';
21009
21010 if (retval != NULL)
21011 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021012 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021013 if (expr_start != NULL)
21014 {
21015 /* Further expansion! */
21016 temp_result = make_expanded_name(retval, expr_start,
21017 expr_end, temp_result);
21018 vim_free(retval);
21019 retval = temp_result;
21020 }
21021 }
21022
21023 return retval;
21024}
21025
21026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021028 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029 */
21030 static int
21031eval_isnamec(c)
21032 int c;
21033{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021034 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21035}
21036
21037/*
21038 * Return TRUE if character "c" can be used as the first character in a
21039 * variable or function name (excluding '{' and '}').
21040 */
21041 static int
21042eval_isnamec1(c)
21043 int c;
21044{
21045 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046}
21047
21048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021049 * Set number v: variable to "val".
21050 */
21051 void
21052set_vim_var_nr(idx, val)
21053 int idx;
21054 long val;
21055{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021056 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021057}
21058
21059/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021060 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061 */
21062 long
21063get_vim_var_nr(idx)
21064 int idx;
21065{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021066 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067}
21068
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021069/*
21070 * Get string v: variable value. Uses a static buffer, can only be used once.
21071 */
21072 char_u *
21073get_vim_var_str(idx)
21074 int idx;
21075{
21076 return get_tv_string(&vimvars[idx].vv_tv);
21077}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021078
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021080 * Get List v: variable value. Caller must take care of reference count when
21081 * needed.
21082 */
21083 list_T *
21084get_vim_var_list(idx)
21085 int idx;
21086{
21087 return vimvars[idx].vv_list;
21088}
21089
21090/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021091 * Set v:char to character "c".
21092 */
21093 void
21094set_vim_var_char(c)
21095 int c;
21096{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021097 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021098
21099#ifdef FEAT_MBYTE
21100 if (has_mbyte)
21101 buf[(*mb_char2bytes)(c, buf)] = NUL;
21102 else
21103#endif
21104 {
21105 buf[0] = c;
21106 buf[1] = NUL;
21107 }
21108 set_vim_var_string(VV_CHAR, buf, -1);
21109}
21110
21111/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021112 * Set v:count to "count" and v:count1 to "count1".
21113 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 */
21115 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021116set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 long count;
21118 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021119 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021121 if (set_prevcount)
21122 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021123 vimvars[VV_COUNT].vv_nr = count;
21124 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125}
21126
21127/*
21128 * Set string v: variable to a copy of "val".
21129 */
21130 void
21131set_vim_var_string(idx, val, len)
21132 int idx;
21133 char_u *val;
21134 int len; /* length of "val" to use or -1 (whole string) */
21135{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021136 /* Need to do this (at least) once, since we can't initialize a union.
21137 * Will always be invoked when "v:progname" is set. */
21138 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
21139
Bram Moolenaare9a41262005-01-15 22:18:47 +000021140 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021142 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021144 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021146 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147}
21148
21149/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021150 * Set List v: variable to "val".
21151 */
21152 void
21153set_vim_var_list(idx, val)
21154 int idx;
21155 list_T *val;
21156{
21157 list_unref(vimvars[idx].vv_list);
21158 vimvars[idx].vv_list = val;
21159 if (val != NULL)
21160 ++val->lv_refcount;
21161}
21162
21163/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021164 * Set Dictionary v: variable to "val".
21165 */
21166 void
21167set_vim_var_dict(idx, val)
21168 int idx;
21169 dict_T *val;
21170{
21171 int todo;
21172 hashitem_T *hi;
21173
21174 dict_unref(vimvars[idx].vv_dict);
21175 vimvars[idx].vv_dict = val;
21176 if (val != NULL)
21177 {
21178 ++val->dv_refcount;
21179
21180 /* Set readonly */
21181 todo = (int)val->dv_hashtab.ht_used;
21182 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21183 {
21184 if (HASHITEM_EMPTY(hi))
21185 continue;
21186 --todo;
21187 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21188 }
21189 }
21190}
21191
21192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193 * Set v:register if needed.
21194 */
21195 void
21196set_reg_var(c)
21197 int c;
21198{
21199 char_u regname;
21200
21201 if (c == 0 || c == ' ')
21202 regname = '"';
21203 else
21204 regname = c;
21205 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021206 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207 set_vim_var_string(VV_REG, &regname, 1);
21208}
21209
21210/*
21211 * Get or set v:exception. If "oldval" == NULL, return the current value.
21212 * Otherwise, restore the value to "oldval" and return NULL.
21213 * Must always be called in pairs to save and restore v:exception! Does not
21214 * take care of memory allocations.
21215 */
21216 char_u *
21217v_exception(oldval)
21218 char_u *oldval;
21219{
21220 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021221 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222
Bram Moolenaare9a41262005-01-15 22:18:47 +000021223 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 return NULL;
21225}
21226
21227/*
21228 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21229 * Otherwise, restore the value to "oldval" and return NULL.
21230 * Must always be called in pairs to save and restore v:throwpoint! Does not
21231 * take care of memory allocations.
21232 */
21233 char_u *
21234v_throwpoint(oldval)
21235 char_u *oldval;
21236{
21237 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021238 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239
Bram Moolenaare9a41262005-01-15 22:18:47 +000021240 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241 return NULL;
21242}
21243
21244#if defined(FEAT_AUTOCMD) || defined(PROTO)
21245/*
21246 * Set v:cmdarg.
21247 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21248 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21249 * Must always be called in pairs!
21250 */
21251 char_u *
21252set_cmdarg(eap, oldarg)
21253 exarg_T *eap;
21254 char_u *oldarg;
21255{
21256 char_u *oldval;
21257 char_u *newval;
21258 unsigned len;
21259
Bram Moolenaare9a41262005-01-15 22:18:47 +000021260 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021261 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021263 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021264 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021265 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 }
21267
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021268 if (eap->force_bin == FORCE_BIN)
21269 len = 6;
21270 else if (eap->force_bin == FORCE_NOBIN)
21271 len = 8;
21272 else
21273 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021274
21275 if (eap->read_edit)
21276 len += 7;
21277
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021278 if (eap->force_ff != 0)
21279 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21280# ifdef FEAT_MBYTE
21281 if (eap->force_enc != 0)
21282 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021283 if (eap->bad_char != 0)
21284 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021285# endif
21286
21287 newval = alloc(len + 1);
21288 if (newval == NULL)
21289 return NULL;
21290
21291 if (eap->force_bin == FORCE_BIN)
21292 sprintf((char *)newval, " ++bin");
21293 else if (eap->force_bin == FORCE_NOBIN)
21294 sprintf((char *)newval, " ++nobin");
21295 else
21296 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021297
21298 if (eap->read_edit)
21299 STRCAT(newval, " ++edit");
21300
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021301 if (eap->force_ff != 0)
21302 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21303 eap->cmd + eap->force_ff);
21304# ifdef FEAT_MBYTE
21305 if (eap->force_enc != 0)
21306 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21307 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021308 if (eap->bad_char == BAD_KEEP)
21309 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21310 else if (eap->bad_char == BAD_DROP)
21311 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21312 else if (eap->bad_char != 0)
21313 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021314# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021315 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021316 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317}
21318#endif
21319
21320/*
21321 * Get the value of internal variable "name".
21322 * Return OK or FAIL.
21323 */
21324 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021325get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326 char_u *name;
21327 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021328 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021329 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021330 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021331 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332{
21333 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021334 typval_T *tv = NULL;
21335 typval_T atv;
21336 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338
21339 /* truncate the name, so that we can use strcmp() */
21340 cc = name[len];
21341 name[len] = NUL;
21342
21343 /*
21344 * Check for "b:changedtick".
21345 */
21346 if (STRCMP(name, "b:changedtick") == 0)
21347 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021348 atv.v_type = VAR_NUMBER;
21349 atv.vval.v_number = curbuf->b_changedtick;
21350 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351 }
21352
21353 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354 * Check for user-defined variables.
21355 */
21356 else
21357 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021358 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021360 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021361 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021362 if (dip != NULL)
21363 *dip = v;
21364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 }
21366
Bram Moolenaare9a41262005-01-15 22:18:47 +000021367 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021369 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021370 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 ret = FAIL;
21372 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021373 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021374 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375
21376 name[len] = cc;
21377
21378 return ret;
21379}
21380
21381/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021382 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21383 * Also handle function call with Funcref variable: func(expr)
21384 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21385 */
21386 static int
21387handle_subscript(arg, rettv, evaluate, verbose)
21388 char_u **arg;
21389 typval_T *rettv;
21390 int evaluate; /* do more than finding the end */
21391 int verbose; /* give error messages */
21392{
21393 int ret = OK;
21394 dict_T *selfdict = NULL;
21395 char_u *s;
21396 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021397 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021398
21399 while (ret == OK
21400 && (**arg == '['
21401 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021402 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021403 && !vim_iswhite(*(*arg - 1)))
21404 {
21405 if (**arg == '(')
21406 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021407 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021408 if (evaluate)
21409 {
21410 functv = *rettv;
21411 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021412
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021413 /* Invoke the function. Recursive! */
21414 s = functv.vval.v_string;
21415 }
21416 else
21417 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021418 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021419 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21420 &len, evaluate, selfdict);
21421
21422 /* Clear the funcref afterwards, so that deleting it while
21423 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021424 if (evaluate)
21425 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021426
21427 /* Stop the expression evaluation when immediately aborting on
21428 * error, or when an interrupt occurred or an exception was thrown
21429 * but not caught. */
21430 if (aborting())
21431 {
21432 if (ret == OK)
21433 clear_tv(rettv);
21434 ret = FAIL;
21435 }
21436 dict_unref(selfdict);
21437 selfdict = NULL;
21438 }
21439 else /* **arg == '[' || **arg == '.' */
21440 {
21441 dict_unref(selfdict);
21442 if (rettv->v_type == VAR_DICT)
21443 {
21444 selfdict = rettv->vval.v_dict;
21445 if (selfdict != NULL)
21446 ++selfdict->dv_refcount;
21447 }
21448 else
21449 selfdict = NULL;
21450 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21451 {
21452 clear_tv(rettv);
21453 ret = FAIL;
21454 }
21455 }
21456 }
21457 dict_unref(selfdict);
21458 return ret;
21459}
21460
21461/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021462 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021463 * value).
21464 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021465 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021466alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021467{
Bram Moolenaar33570922005-01-25 22:26:29 +000021468 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021469}
21470
21471/*
21472 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021473 * The string "s" must have been allocated, it is consumed.
21474 * Return NULL for out of memory, the variable otherwise.
21475 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021476 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021477alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478 char_u *s;
21479{
Bram Moolenaar33570922005-01-25 22:26:29 +000021480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021482 rettv = alloc_tv();
21483 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021485 rettv->v_type = VAR_STRING;
21486 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487 }
21488 else
21489 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021490 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491}
21492
21493/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021494 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021496 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021497free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021498 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499{
21500 if (varp != NULL)
21501 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021502 switch (varp->v_type)
21503 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021504 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021505 func_unref(varp->vval.v_string);
21506 /*FALLTHROUGH*/
21507 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021508 vim_free(varp->vval.v_string);
21509 break;
21510 case VAR_LIST:
21511 list_unref(varp->vval.v_list);
21512 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021513 case VAR_DICT:
21514 dict_unref(varp->vval.v_dict);
21515 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021516 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021517#ifdef FEAT_FLOAT
21518 case VAR_FLOAT:
21519#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021520 case VAR_UNKNOWN:
21521 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021522 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021523 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021524 break;
21525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526 vim_free(varp);
21527 }
21528}
21529
21530/*
21531 * Free the memory for a variable value and set the value to NULL or 0.
21532 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021533 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021534clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021535 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536{
21537 if (varp != NULL)
21538 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021539 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021541 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021542 func_unref(varp->vval.v_string);
21543 /*FALLTHROUGH*/
21544 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021545 vim_free(varp->vval.v_string);
21546 varp->vval.v_string = NULL;
21547 break;
21548 case VAR_LIST:
21549 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021550 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021551 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021552 case VAR_DICT:
21553 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021554 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021555 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021556 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021557 varp->vval.v_number = 0;
21558 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021559#ifdef FEAT_FLOAT
21560 case VAR_FLOAT:
21561 varp->vval.v_float = 0.0;
21562 break;
21563#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021564 case VAR_UNKNOWN:
21565 break;
21566 default:
21567 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021569 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 }
21571}
21572
21573/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021574 * Set the value of a variable to NULL without freeing items.
21575 */
21576 static void
21577init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021578 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021579{
21580 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021581 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021582}
21583
21584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 * Get the number value of a variable.
21586 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021587 * For incompatible types, return 0.
21588 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21589 * caller of incompatible types: it sets *denote to TRUE if "denote"
21590 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 */
21592 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021593get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021594 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021596 int error = FALSE;
21597
21598 return get_tv_number_chk(varp, &error); /* return 0L on error */
21599}
21600
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021601 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021602get_tv_number_chk(varp, denote)
21603 typval_T *varp;
21604 int *denote;
21605{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021606 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021608 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021610 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021611 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021612#ifdef FEAT_FLOAT
21613 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021614 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021615 break;
21616#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021617 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021618 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021619 break;
21620 case VAR_STRING:
21621 if (varp->vval.v_string != NULL)
21622 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021623 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021624 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021625 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021626 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021627 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021628 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021629 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021630 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021631 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021632 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021633 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021635 if (denote == NULL) /* useful for values that must be unsigned */
21636 n = -1;
21637 else
21638 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021639 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640}
21641
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021642#ifdef FEAT_FLOAT
21643 static float_T
21644get_tv_float(varp)
21645 typval_T *varp;
21646{
21647 switch (varp->v_type)
21648 {
21649 case VAR_NUMBER:
21650 return (float_T)(varp->vval.v_number);
21651#ifdef FEAT_FLOAT
21652 case VAR_FLOAT:
21653 return varp->vval.v_float;
21654 break;
21655#endif
21656 case VAR_FUNC:
21657 EMSG(_("E891: Using a Funcref as a Float"));
21658 break;
21659 case VAR_STRING:
21660 EMSG(_("E892: Using a String as a Float"));
21661 break;
21662 case VAR_LIST:
21663 EMSG(_("E893: Using a List as a Float"));
21664 break;
21665 case VAR_DICT:
21666 EMSG(_("E894: Using a Dictionary as a Float"));
21667 break;
21668 default:
21669 EMSG2(_(e_intern2), "get_tv_float()");
21670 break;
21671 }
21672 return 0;
21673}
21674#endif
21675
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021677 * Get the lnum from the first argument.
21678 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021679 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 */
21681 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021682get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021683 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684{
Bram Moolenaar33570922005-01-25 22:26:29 +000021685 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021686 linenr_T lnum;
21687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021688 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021689 if (lnum == 0) /* no valid number, try using line() */
21690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021691 rettv.v_type = VAR_NUMBER;
21692 f_line(argvars, &rettv);
21693 lnum = rettv.vval.v_number;
21694 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695 }
21696 return lnum;
21697}
21698
21699/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021700 * Get the lnum from the first argument.
21701 * Also accepts "$", then "buf" is used.
21702 * Returns 0 on error.
21703 */
21704 static linenr_T
21705get_tv_lnum_buf(argvars, buf)
21706 typval_T *argvars;
21707 buf_T *buf;
21708{
21709 if (argvars[0].v_type == VAR_STRING
21710 && argvars[0].vval.v_string != NULL
21711 && argvars[0].vval.v_string[0] == '$'
21712 && buf != NULL)
21713 return buf->b_ml.ml_line_count;
21714 return get_tv_number_chk(&argvars[0], NULL);
21715}
21716
21717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 * Get the string value of a variable.
21719 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021720 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21721 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722 * If the String variable has never been set, return an empty string.
21723 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021724 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21725 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726 */
21727 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021728get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021729 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730{
21731 static char_u mybuf[NUMBUFLEN];
21732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021733 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734}
21735
21736 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021737get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021738 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021739 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021741 char_u *res = get_tv_string_buf_chk(varp, buf);
21742
21743 return res != NULL ? res : (char_u *)"";
21744}
21745
Bram Moolenaar7d647822014-04-05 21:28:56 +020021746/*
21747 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21748 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021749 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021750get_tv_string_chk(varp)
21751 typval_T *varp;
21752{
21753 static char_u mybuf[NUMBUFLEN];
21754
21755 return get_tv_string_buf_chk(varp, mybuf);
21756}
21757
21758 static char_u *
21759get_tv_string_buf_chk(varp, buf)
21760 typval_T *varp;
21761 char_u *buf;
21762{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021763 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021765 case VAR_NUMBER:
21766 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21767 return buf;
21768 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021769 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021770 break;
21771 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021772 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021773 break;
21774 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021775 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021776 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021777#ifdef FEAT_FLOAT
21778 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021779 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021780 break;
21781#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021782 case VAR_STRING:
21783 if (varp->vval.v_string != NULL)
21784 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021785 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021786 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021787 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021788 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021790 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791}
21792
21793/*
21794 * Find variable "name" in the list of variables.
21795 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021796 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021797 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021798 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021799 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021800 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021801find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021803 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021804 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021807 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808
Bram Moolenaara7043832005-01-21 11:56:39 +000021809 ht = find_var_ht(name, &varname);
21810 if (htp != NULL)
21811 *htp = ht;
21812 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021814 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815}
21816
21817/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021818 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021819 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021821 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021822find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021823 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021824 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021825 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021826 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021827{
Bram Moolenaar33570922005-01-25 22:26:29 +000021828 hashitem_T *hi;
21829
21830 if (*varname == NUL)
21831 {
21832 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021833 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021834 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021835 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021836 case 'g': return &globvars_var;
21837 case 'v': return &vimvars_var;
21838 case 'b': return &curbuf->b_bufvar;
21839 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021840#ifdef FEAT_WINDOWS
21841 case 't': return &curtab->tp_winvar;
21842#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021843 case 'l': return current_funccal == NULL
21844 ? NULL : &current_funccal->l_vars_var;
21845 case 'a': return current_funccal == NULL
21846 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021847 }
21848 return NULL;
21849 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021850
21851 hi = hash_find(ht, varname);
21852 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021853 {
21854 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021855 * worked find the variable again. Don't auto-load a script if it was
21856 * loaded already, otherwise it would be loaded every time when
21857 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021858 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021859 {
21860 /* Note: script_autoload() may make "hi" invalid. It must either
21861 * be obtained again or not used. */
21862 if (!script_autoload(varname, FALSE) || aborting())
21863 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021864 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021865 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021866 if (HASHITEM_EMPTY(hi))
21867 return NULL;
21868 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021869 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021870}
21871
21872/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021873 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021874 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021875 * Set "varname" to the start of name without ':'.
21876 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021877 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021878find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879 char_u *name;
21880 char_u **varname;
21881{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021882 hashitem_T *hi;
21883
Bram Moolenaar73627d02015-08-11 15:46:09 +020021884 if (name[0] == NUL)
21885 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 if (name[1] != ':')
21887 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021888 /* The name must not start with a colon or #. */
21889 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890 return NULL;
21891 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021892
21893 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021894 hi = hash_find(&compat_hashtab, name);
21895 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021896 return &compat_hashtab;
21897
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021899 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021900 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 }
21902 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021903 if (*name == 'g') /* global variable */
21904 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021905 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21906 */
21907 if (vim_strchr(name + 2, ':') != NULL
21908 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021909 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021911 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021913 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021914#ifdef FEAT_WINDOWS
21915 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021916 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021917#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021918 if (*name == 'v') /* v: variable */
21919 return &vimvarht;
21920 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021921 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021922 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021923 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 if (*name == 's' /* script variable */
21925 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21926 return &SCRIPT_VARS(current_SID);
21927 return NULL;
21928}
21929
21930/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021931 * Get function call environment based on bactrace debug level
21932 */
21933 static funccall_T *
21934get_funccal()
21935{
21936 int i;
21937 funccall_T *funccal;
21938 funccall_T *temp_funccal;
21939
21940 funccal = current_funccal;
21941 if (debug_backtrace_level > 0)
21942 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021943 for (i = 0; i < debug_backtrace_level; i++)
21944 {
21945 temp_funccal = funccal->caller;
21946 if (temp_funccal)
21947 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021948 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021949 /* backtrace level overflow. reset to max */
21950 debug_backtrace_level = i;
21951 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021952 }
21953 return funccal;
21954}
21955
21956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021957 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021958 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959 * Returns NULL when it doesn't exist.
21960 */
21961 char_u *
21962get_var_value(name)
21963 char_u *name;
21964{
Bram Moolenaar33570922005-01-25 22:26:29 +000021965 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021967 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 if (v == NULL)
21969 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021970 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971}
21972
21973/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021974 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975 * sourcing this script and when executing functions defined in the script.
21976 */
21977 void
21978new_script_vars(id)
21979 scid_T id;
21980{
Bram Moolenaara7043832005-01-21 11:56:39 +000021981 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021982 hashtab_T *ht;
21983 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021984
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21986 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021987 /* Re-allocating ga_data means that an ht_array pointing to
21988 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021989 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021990 for (i = 1; i <= ga_scripts.ga_len; ++i)
21991 {
21992 ht = &SCRIPT_VARS(i);
21993 if (ht->ht_mask == HT_INIT_SIZE - 1)
21994 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021995 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021996 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021997 }
21998
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 while (ga_scripts.ga_len < id)
22000 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022001 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022002 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022003 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005 }
22006 }
22007}
22008
22009/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022010 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22011 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 */
22013 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022014init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000022015 dict_T *dict;
22016 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022017 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018{
Bram Moolenaar33570922005-01-25 22:26:29 +000022019 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022020 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022021 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022022 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022023 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022024 dict_var->di_tv.vval.v_dict = dict;
22025 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022026 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022027 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22028 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029}
22030
22031/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022032 * Unreference a dictionary initialized by init_var_dict().
22033 */
22034 void
22035unref_var_dict(dict)
22036 dict_T *dict;
22037{
22038 /* Now the dict needs to be freed if no one else is using it, go back to
22039 * normal reference counting. */
22040 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22041 dict_unref(dict);
22042}
22043
22044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022046 * Frees all allocated variables and the value they contain.
22047 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 */
22049 void
Bram Moolenaara7043832005-01-21 11:56:39 +000022050vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022051 hashtab_T *ht;
22052{
22053 vars_clear_ext(ht, TRUE);
22054}
22055
22056/*
22057 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22058 */
22059 static void
22060vars_clear_ext(ht, free_val)
22061 hashtab_T *ht;
22062 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063{
Bram Moolenaara7043832005-01-21 11:56:39 +000022064 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022065 hashitem_T *hi;
22066 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067
Bram Moolenaar33570922005-01-25 22:26:29 +000022068 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022069 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022070 for (hi = ht->ht_array; todo > 0; ++hi)
22071 {
22072 if (!HASHITEM_EMPTY(hi))
22073 {
22074 --todo;
22075
Bram Moolenaar33570922005-01-25 22:26:29 +000022076 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022077 * ht_array might change then. hash_clear() takes care of it
22078 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022079 v = HI2DI(hi);
22080 if (free_val)
22081 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022082 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022083 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022084 }
22085 }
22086 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022087 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088}
22089
Bram Moolenaara7043832005-01-21 11:56:39 +000022090/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022091 * Delete a variable from hashtab "ht" at item "hi".
22092 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000022095delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000022096 hashtab_T *ht;
22097 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098{
Bram Moolenaar33570922005-01-25 22:26:29 +000022099 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022100
22101 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022102 clear_tv(&di->di_tv);
22103 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104}
22105
22106/*
22107 * List the value of one internal variable.
22108 */
22109 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022110list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000022111 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022113 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022115 char_u *tofree;
22116 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022117 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022118
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022119 current_copyID += COPYID_INC;
22120 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000022121 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022122 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022123 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124}
22125
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022127list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128 char_u *prefix;
22129 char_u *name;
22130 int type;
22131 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022132 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022133{
Bram Moolenaar31859182007-08-14 20:41:13 +000022134 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22135 msg_start();
22136 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 if (name != NULL) /* "a:" vars don't have a name stored */
22138 msg_puts(name);
22139 msg_putchar(' ');
22140 msg_advance(22);
22141 if (type == VAR_NUMBER)
22142 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022143 else if (type == VAR_FUNC)
22144 msg_putchar('*');
22145 else if (type == VAR_LIST)
22146 {
22147 msg_putchar('[');
22148 if (*string == '[')
22149 ++string;
22150 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022151 else if (type == VAR_DICT)
22152 {
22153 msg_putchar('{');
22154 if (*string == '{')
22155 ++string;
22156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 else
22158 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022159
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022161
22162 if (type == VAR_FUNC)
22163 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022164 if (*first)
22165 {
22166 msg_clr_eos();
22167 *first = FALSE;
22168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169}
22170
22171/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022172 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 * If the variable already exists, the value is updated.
22174 * Otherwise the variable is created.
22175 */
22176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022177set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022179 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022180 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022181{
Bram Moolenaar33570922005-01-25 22:26:29 +000022182 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022183 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022184 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022186 ht = find_var_ht(name, &varname);
22187 if (ht == NULL || *varname == NUL)
22188 {
22189 EMSG2(_(e_illvar), name);
22190 return;
22191 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022192 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022193
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022194 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22195 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022196
Bram Moolenaar33570922005-01-25 22:26:29 +000022197 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022199 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022200 if (var_check_ro(v->di_flags, name, FALSE)
22201 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022202 return;
22203 if (v->di_tv.v_type != tv->v_type
22204 && !((v->di_tv.v_type == VAR_STRING
22205 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022206 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022207 || tv->v_type == VAR_NUMBER))
22208#ifdef FEAT_FLOAT
22209 && !((v->di_tv.v_type == VAR_NUMBER
22210 || v->di_tv.v_type == VAR_FLOAT)
22211 && (tv->v_type == VAR_NUMBER
22212 || tv->v_type == VAR_FLOAT))
22213#endif
22214 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022215 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022216 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022217 return;
22218 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022219
22220 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022221 * Handle setting internal v: variables separately where needed to
22222 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022223 */
22224 if (ht == &vimvarht)
22225 {
22226 if (v->di_tv.v_type == VAR_STRING)
22227 {
22228 vim_free(v->di_tv.vval.v_string);
22229 if (copy || tv->v_type != VAR_STRING)
22230 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22231 else
22232 {
22233 /* Take over the string to avoid an extra alloc/free. */
22234 v->di_tv.vval.v_string = tv->vval.v_string;
22235 tv->vval.v_string = NULL;
22236 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022237 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022238 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022239 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022240 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022241 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022242 if (STRCMP(varname, "searchforward") == 0)
22243 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022244#ifdef FEAT_SEARCH_EXTRA
22245 else if (STRCMP(varname, "hlsearch") == 0)
22246 {
22247 no_hlsearch = !v->di_tv.vval.v_number;
22248 redraw_all_later(SOME_VALID);
22249 }
22250#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022251 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022252 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022253 else if (v->di_tv.v_type != tv->v_type)
22254 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022255 }
22256
22257 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258 }
22259 else /* add a new variable */
22260 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022261 /* Can't add "v:" variable. */
22262 if (ht == &vimvarht)
22263 {
22264 EMSG2(_(e_illvar), name);
22265 return;
22266 }
22267
Bram Moolenaar92124a32005-06-17 22:03:40 +000022268 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022269 if (!valid_varname(varname))
22270 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022271
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022272 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22273 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022274 if (v == NULL)
22275 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022276 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022277 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022279 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 return;
22281 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022282 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022284
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022285 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022286 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022287 else
22288 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022289 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022290 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022291 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293}
22294
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022295/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022296 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022297 * Also give an error message.
22298 */
22299 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022300var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022301 int flags;
22302 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022303 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022304{
22305 if (flags & DI_FLAGS_RO)
22306 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022307 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022308 return TRUE;
22309 }
22310 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22311 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022312 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022313 return TRUE;
22314 }
22315 return FALSE;
22316}
22317
22318/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022319 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22320 * Also give an error message.
22321 */
22322 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022323var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022324 int flags;
22325 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022326 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022327{
22328 if (flags & DI_FLAGS_FIX)
22329 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022330 EMSG2(_("E795: Cannot delete variable %s"),
22331 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022332 return TRUE;
22333 }
22334 return FALSE;
22335}
22336
22337/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022338 * Check if a funcref is assigned to a valid variable name.
22339 * Return TRUE and give an error if not.
22340 */
22341 static int
22342var_check_func_name(name, new_var)
22343 char_u *name; /* points to start of variable name */
22344 int new_var; /* TRUE when creating the variable */
22345{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022346 /* Allow for w: b: s: and t:. */
22347 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022348 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22349 ? name[2] : name[0]))
22350 {
22351 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22352 name);
22353 return TRUE;
22354 }
22355 /* Don't allow hiding a function. When "v" is not NULL we might be
22356 * assigning another function to the same var, the type is checked
22357 * below. */
22358 if (new_var && function_exists(name))
22359 {
22360 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22361 name);
22362 return TRUE;
22363 }
22364 return FALSE;
22365}
22366
22367/*
22368 * Check if a variable name is valid.
22369 * Return FALSE and give an error if not.
22370 */
22371 static int
22372valid_varname(varname)
22373 char_u *varname;
22374{
22375 char_u *p;
22376
22377 for (p = varname; *p != NUL; ++p)
22378 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22379 && *p != AUTOLOAD_CHAR)
22380 {
22381 EMSG2(_(e_illvar), varname);
22382 return FALSE;
22383 }
22384 return TRUE;
22385}
22386
22387/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022388 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022389 * Also give an error message, using "name" or _("name") when use_gettext is
22390 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022391 */
22392 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022393tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022394 int lock;
22395 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022396 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022397{
22398 if (lock & VAR_LOCKED)
22399 {
22400 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022401 name == NULL ? (char_u *)_("Unknown")
22402 : use_gettext ? (char_u *)_(name)
22403 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022404 return TRUE;
22405 }
22406 if (lock & VAR_FIXED)
22407 {
22408 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022409 name == NULL ? (char_u *)_("Unknown")
22410 : use_gettext ? (char_u *)_(name)
22411 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022412 return TRUE;
22413 }
22414 return FALSE;
22415}
22416
22417/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022418 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022419 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022420 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022421 * It is OK for "from" and "to" to point to the same item. This is used to
22422 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022423 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022424 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022425copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022426 typval_T *from;
22427 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022429 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022430 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022431 switch (from->v_type)
22432 {
22433 case VAR_NUMBER:
22434 to->vval.v_number = from->vval.v_number;
22435 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022436#ifdef FEAT_FLOAT
22437 case VAR_FLOAT:
22438 to->vval.v_float = from->vval.v_float;
22439 break;
22440#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022441 case VAR_STRING:
22442 case VAR_FUNC:
22443 if (from->vval.v_string == NULL)
22444 to->vval.v_string = NULL;
22445 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022446 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022447 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022448 if (from->v_type == VAR_FUNC)
22449 func_ref(to->vval.v_string);
22450 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022451 break;
22452 case VAR_LIST:
22453 if (from->vval.v_list == NULL)
22454 to->vval.v_list = NULL;
22455 else
22456 {
22457 to->vval.v_list = from->vval.v_list;
22458 ++to->vval.v_list->lv_refcount;
22459 }
22460 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022461 case VAR_DICT:
22462 if (from->vval.v_dict == NULL)
22463 to->vval.v_dict = NULL;
22464 else
22465 {
22466 to->vval.v_dict = from->vval.v_dict;
22467 ++to->vval.v_dict->dv_refcount;
22468 }
22469 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022470 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022471 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022472 break;
22473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474}
22475
22476/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022477 * Make a copy of an item.
22478 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022479 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22480 * reference to an already copied list/dict can be used.
22481 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022482 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022483 static int
22484item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022485 typval_T *from;
22486 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022487 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022488 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022489{
22490 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022491 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022492
Bram Moolenaar33570922005-01-25 22:26:29 +000022493 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022494 {
22495 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022496 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022497 }
22498 ++recurse;
22499
22500 switch (from->v_type)
22501 {
22502 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022503#ifdef FEAT_FLOAT
22504 case VAR_FLOAT:
22505#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022506 case VAR_STRING:
22507 case VAR_FUNC:
22508 copy_tv(from, to);
22509 break;
22510 case VAR_LIST:
22511 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022512 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022513 if (from->vval.v_list == NULL)
22514 to->vval.v_list = NULL;
22515 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22516 {
22517 /* use the copy made earlier */
22518 to->vval.v_list = from->vval.v_list->lv_copylist;
22519 ++to->vval.v_list->lv_refcount;
22520 }
22521 else
22522 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22523 if (to->vval.v_list == NULL)
22524 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022525 break;
22526 case VAR_DICT:
22527 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022528 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022529 if (from->vval.v_dict == NULL)
22530 to->vval.v_dict = NULL;
22531 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22532 {
22533 /* use the copy made earlier */
22534 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22535 ++to->vval.v_dict->dv_refcount;
22536 }
22537 else
22538 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22539 if (to->vval.v_dict == NULL)
22540 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022541 break;
22542 default:
22543 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022544 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022545 }
22546 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022547 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022548}
22549
22550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 * ":echo expr1 ..." print each argument separated with a space, add a
22552 * newline at the end.
22553 * ":echon expr1 ..." print each argument plain.
22554 */
22555 void
22556ex_echo(eap)
22557 exarg_T *eap;
22558{
22559 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022560 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022561 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 char_u *p;
22563 int needclr = TRUE;
22564 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022565 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566
22567 if (eap->skip)
22568 ++emsg_skip;
22569 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22570 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022571 /* If eval1() causes an error message the text from the command may
22572 * still need to be cleared. E.g., "echo 22,44". */
22573 need_clr_eos = needclr;
22574
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022576 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 {
22578 /*
22579 * Report the invalid expression unless the expression evaluation
22580 * has been cancelled due to an aborting error, an interrupt, or an
22581 * exception.
22582 */
22583 if (!aborting())
22584 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022585 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586 break;
22587 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022588 need_clr_eos = FALSE;
22589
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 if (!eap->skip)
22591 {
22592 if (atstart)
22593 {
22594 atstart = FALSE;
22595 /* Call msg_start() after eval1(), evaluating the expression
22596 * may cause a message to appear. */
22597 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022598 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022599 /* Mark the saved text as finishing the line, so that what
22600 * follows is displayed on a new line when scrolling back
22601 * at the more prompt. */
22602 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 }
22606 else if (eap->cmdidx == CMD_echo)
22607 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022608 current_copyID += COPYID_INC;
22609 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022610 if (p != NULL)
22611 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022613 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022615 if (*p != TAB && needclr)
22616 {
22617 /* remove any text still there from the command */
22618 msg_clr_eos();
22619 needclr = FALSE;
22620 }
22621 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 }
22623 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022624 {
22625#ifdef FEAT_MBYTE
22626 if (has_mbyte)
22627 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022628 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022629
22630 (void)msg_outtrans_len_attr(p, i, echo_attr);
22631 p += i - 1;
22632 }
22633 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022635 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022638 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022640 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641 arg = skipwhite(arg);
22642 }
22643 eap->nextcmd = check_nextcmd(arg);
22644
22645 if (eap->skip)
22646 --emsg_skip;
22647 else
22648 {
22649 /* remove text that may still be there from the command */
22650 if (needclr)
22651 msg_clr_eos();
22652 if (eap->cmdidx == CMD_echo)
22653 msg_end();
22654 }
22655}
22656
22657/*
22658 * ":echohl {name}".
22659 */
22660 void
22661ex_echohl(eap)
22662 exarg_T *eap;
22663{
22664 int id;
22665
22666 id = syn_name2id(eap->arg);
22667 if (id == 0)
22668 echo_attr = 0;
22669 else
22670 echo_attr = syn_id2attr(id);
22671}
22672
22673/*
22674 * ":execute expr1 ..." execute the result of an expression.
22675 * ":echomsg expr1 ..." Print a message
22676 * ":echoerr expr1 ..." Print an error
22677 * Each gets spaces around each argument and a newline at the end for
22678 * echo commands
22679 */
22680 void
22681ex_execute(eap)
22682 exarg_T *eap;
22683{
22684 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022685 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 int ret = OK;
22687 char_u *p;
22688 garray_T ga;
22689 int len;
22690 int save_did_emsg;
22691
22692 ga_init2(&ga, 1, 80);
22693
22694 if (eap->skip)
22695 ++emsg_skip;
22696 while (*arg != NUL && *arg != '|' && *arg != '\n')
22697 {
22698 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022699 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700 {
22701 /*
22702 * Report the invalid expression unless the expression evaluation
22703 * has been cancelled due to an aborting error, an interrupt, or an
22704 * exception.
22705 */
22706 if (!aborting())
22707 EMSG2(_(e_invexpr2), p);
22708 ret = FAIL;
22709 break;
22710 }
22711
22712 if (!eap->skip)
22713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022714 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 len = (int)STRLEN(p);
22716 if (ga_grow(&ga, len + 2) == FAIL)
22717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022718 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719 ret = FAIL;
22720 break;
22721 }
22722 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725 ga.ga_len += len;
22726 }
22727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022728 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 arg = skipwhite(arg);
22730 }
22731
22732 if (ret != FAIL && ga.ga_data != NULL)
22733 {
22734 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022737 out_flush();
22738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 else if (eap->cmdidx == CMD_echoerr)
22740 {
22741 /* We don't want to abort following commands, restore did_emsg. */
22742 save_did_emsg = did_emsg;
22743 EMSG((char_u *)ga.ga_data);
22744 if (!force_abort)
22745 did_emsg = save_did_emsg;
22746 }
22747 else if (eap->cmdidx == CMD_execute)
22748 do_cmdline((char_u *)ga.ga_data,
22749 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22750 }
22751
22752 ga_clear(&ga);
22753
22754 if (eap->skip)
22755 --emsg_skip;
22756
22757 eap->nextcmd = check_nextcmd(arg);
22758}
22759
22760/*
22761 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22762 * "arg" points to the "&" or '+' when called, to "option" when returning.
22763 * Returns NULL when no option name found. Otherwise pointer to the char
22764 * after the option name.
22765 */
22766 static char_u *
22767find_option_end(arg, opt_flags)
22768 char_u **arg;
22769 int *opt_flags;
22770{
22771 char_u *p = *arg;
22772
22773 ++p;
22774 if (*p == 'g' && p[1] == ':')
22775 {
22776 *opt_flags = OPT_GLOBAL;
22777 p += 2;
22778 }
22779 else if (*p == 'l' && p[1] == ':')
22780 {
22781 *opt_flags = OPT_LOCAL;
22782 p += 2;
22783 }
22784 else
22785 *opt_flags = 0;
22786
22787 if (!ASCII_ISALPHA(*p))
22788 return NULL;
22789 *arg = p;
22790
22791 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22792 p += 4; /* termcap option */
22793 else
22794 while (ASCII_ISALPHA(*p))
22795 ++p;
22796 return p;
22797}
22798
22799/*
22800 * ":function"
22801 */
22802 void
22803ex_function(eap)
22804 exarg_T *eap;
22805{
22806 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022807 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808 int j;
22809 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022811 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 char_u *name = NULL;
22813 char_u *p;
22814 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022815 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816 garray_T newargs;
22817 garray_T newlines;
22818 int varargs = FALSE;
22819 int mustend = FALSE;
22820 int flags = 0;
22821 ufunc_T *fp;
22822 int indent;
22823 int nesting;
22824 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022825 dictitem_T *v;
22826 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022827 static int func_nr = 0; /* number for nameless function */
22828 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022829 hashtab_T *ht;
22830 int todo;
22831 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022832 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833
22834 /*
22835 * ":function" without argument: list functions.
22836 */
22837 if (ends_excmd(*eap->arg))
22838 {
22839 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022840 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022841 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022842 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022843 {
22844 if (!HASHITEM_EMPTY(hi))
22845 {
22846 --todo;
22847 fp = HI2UF(hi);
22848 if (!isdigit(*fp->uf_name))
22849 list_func_head(fp, FALSE);
22850 }
22851 }
22852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853 eap->nextcmd = check_nextcmd(eap->arg);
22854 return;
22855 }
22856
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022857 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022858 * ":function /pat": list functions matching pattern.
22859 */
22860 if (*eap->arg == '/')
22861 {
22862 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22863 if (!eap->skip)
22864 {
22865 regmatch_T regmatch;
22866
22867 c = *p;
22868 *p = NUL;
22869 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22870 *p = c;
22871 if (regmatch.regprog != NULL)
22872 {
22873 regmatch.rm_ic = p_ic;
22874
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022875 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022876 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22877 {
22878 if (!HASHITEM_EMPTY(hi))
22879 {
22880 --todo;
22881 fp = HI2UF(hi);
22882 if (!isdigit(*fp->uf_name)
22883 && vim_regexec(&regmatch, fp->uf_name, 0))
22884 list_func_head(fp, FALSE);
22885 }
22886 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022887 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022888 }
22889 }
22890 if (*p == '/')
22891 ++p;
22892 eap->nextcmd = check_nextcmd(p);
22893 return;
22894 }
22895
22896 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022897 * Get the function name. There are these situations:
22898 * func normal function name
22899 * "name" == func, "fudi.fd_dict" == NULL
22900 * dict.func new dictionary entry
22901 * "name" == NULL, "fudi.fd_dict" set,
22902 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22903 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022904 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022905 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22906 * dict.func existing dict entry that's not a Funcref
22907 * "name" == NULL, "fudi.fd_dict" set,
22908 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022909 * s:func script-local function name
22910 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022913 name = trans_function_name(&p, eap->skip, 0, &fudi);
22914 paren = (vim_strchr(p, '(') != NULL);
22915 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916 {
22917 /*
22918 * Return on an invalid expression in braces, unless the expression
22919 * evaluation has been cancelled due to an aborting error, an
22920 * interrupt, or an exception.
22921 */
22922 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022923 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022924 if (!eap->skip && fudi.fd_newkey != NULL)
22925 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022926 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 else
22930 eap->skip = TRUE;
22931 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022932
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 /* An error in a function call during evaluation of an expression in magic
22934 * braces should not cause the function not to be defined. */
22935 saved_did_emsg = did_emsg;
22936 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937
22938 /*
22939 * ":function func" with only function name: list function.
22940 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022941 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 {
22943 if (!ends_excmd(*skipwhite(p)))
22944 {
22945 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022946 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947 }
22948 eap->nextcmd = check_nextcmd(p);
22949 if (eap->nextcmd != NULL)
22950 *p = NUL;
22951 if (!eap->skip && !got_int)
22952 {
22953 fp = find_func(name);
22954 if (fp != NULL)
22955 {
22956 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022957 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022959 if (FUNCLINE(fp, j) == NULL)
22960 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 msg_putchar('\n');
22962 msg_outnum((long)(j + 1));
22963 if (j < 9)
22964 msg_putchar(' ');
22965 if (j < 99)
22966 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022967 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 out_flush(); /* show a line at a time */
22969 ui_breakcheck();
22970 }
22971 if (!got_int)
22972 {
22973 msg_putchar('\n');
22974 msg_puts((char_u *)" endfunction");
22975 }
22976 }
22977 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022978 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022980 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 }
22982
22983 /*
22984 * ":function name(arg1, arg2)" Define function.
22985 */
22986 p = skipwhite(p);
22987 if (*p != '(')
22988 {
22989 if (!eap->skip)
22990 {
22991 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022992 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 }
22994 /* attempt to continue by skipping some text */
22995 if (vim_strchr(p, '(') != NULL)
22996 p = vim_strchr(p, '(');
22997 }
22998 p = skipwhite(p + 1);
22999
23000 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23001 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23002
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023003 if (!eap->skip)
23004 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023005 /* Check the name of the function. Unless it's a dictionary function
23006 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023007 if (name != NULL)
23008 arg = name;
23009 else
23010 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023011 if (arg != NULL && (fudi.fd_di == NULL
23012 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023013 {
23014 if (*arg == K_SPECIAL)
23015 j = 3;
23016 else
23017 j = 0;
23018 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23019 : eval_isnamec(arg[j])))
23020 ++j;
23021 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023022 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023023 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023024 /* Disallow using the g: dict. */
23025 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23026 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023027 }
23028
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 /*
23030 * Isolate the arguments: "arg1, arg2, ...)"
23031 */
23032 while (*p != ')')
23033 {
23034 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23035 {
23036 varargs = TRUE;
23037 p += 3;
23038 mustend = TRUE;
23039 }
23040 else
23041 {
23042 arg = p;
23043 while (ASCII_ISALNUM(*p) || *p == '_')
23044 ++p;
23045 if (arg == p || isdigit(*arg)
23046 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23047 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23048 {
23049 if (!eap->skip)
23050 EMSG2(_("E125: Illegal argument: %s"), arg);
23051 break;
23052 }
23053 if (ga_grow(&newargs, 1) == FAIL)
23054 goto erret;
23055 c = *p;
23056 *p = NUL;
23057 arg = vim_strsave(arg);
23058 if (arg == NULL)
23059 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023060
23061 /* Check for duplicate argument name. */
23062 for (i = 0; i < newargs.ga_len; ++i)
23063 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23064 {
23065 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023066 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023067 goto erret;
23068 }
23069
Bram Moolenaar071d4272004-06-13 20:20:40 +000023070 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23071 *p = c;
23072 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073 if (*p == ',')
23074 ++p;
23075 else
23076 mustend = TRUE;
23077 }
23078 p = skipwhite(p);
23079 if (mustend && *p != ')')
23080 {
23081 if (!eap->skip)
23082 EMSG2(_(e_invarg2), eap->arg);
23083 break;
23084 }
23085 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023086 if (*p != ')')
23087 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023088 ++p; /* skip the ')' */
23089
Bram Moolenaare9a41262005-01-15 22:18:47 +000023090 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 for (;;)
23092 {
23093 p = skipwhite(p);
23094 if (STRNCMP(p, "range", 5) == 0)
23095 {
23096 flags |= FC_RANGE;
23097 p += 5;
23098 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023099 else if (STRNCMP(p, "dict", 4) == 0)
23100 {
23101 flags |= FC_DICT;
23102 p += 4;
23103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104 else if (STRNCMP(p, "abort", 5) == 0)
23105 {
23106 flags |= FC_ABORT;
23107 p += 5;
23108 }
23109 else
23110 break;
23111 }
23112
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023113 /* When there is a line break use what follows for the function body.
23114 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23115 if (*p == '\n')
23116 line_arg = p + 1;
23117 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118 EMSG(_(e_trailing));
23119
23120 /*
23121 * Read the body of the function, until ":endfunction" is found.
23122 */
23123 if (KeyTyped)
23124 {
23125 /* Check if the function already exists, don't let the user type the
23126 * whole function before telling him it doesn't work! For a script we
23127 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023128 if (!eap->skip && !eap->forceit)
23129 {
23130 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23131 EMSG(_(e_funcdict));
23132 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023133 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023136 if (!eap->skip && did_emsg)
23137 goto erret;
23138
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139 msg_putchar('\n'); /* don't overwrite the function name */
23140 cmdline_row = msg_row;
23141 }
23142
23143 indent = 2;
23144 nesting = 0;
23145 for (;;)
23146 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023147 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023148 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023149 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023150 saved_wait_return = FALSE;
23151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023153 sourcing_lnum_off = sourcing_lnum;
23154
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023155 if (line_arg != NULL)
23156 {
23157 /* Use eap->arg, split up in parts by line breaks. */
23158 theline = line_arg;
23159 p = vim_strchr(theline, '\n');
23160 if (p == NULL)
23161 line_arg += STRLEN(line_arg);
23162 else
23163 {
23164 *p = NUL;
23165 line_arg = p + 1;
23166 }
23167 }
23168 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169 theline = getcmdline(':', 0L, indent);
23170 else
23171 theline = eap->getline(':', eap->cookie, indent);
23172 if (KeyTyped)
23173 lines_left = Rows - 1;
23174 if (theline == NULL)
23175 {
23176 EMSG(_("E126: Missing :endfunction"));
23177 goto erret;
23178 }
23179
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023180 /* Detect line continuation: sourcing_lnum increased more than one. */
23181 if (sourcing_lnum > sourcing_lnum_off + 1)
23182 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23183 else
23184 sourcing_lnum_off = 0;
23185
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186 if (skip_until != NULL)
23187 {
23188 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23189 * don't check for ":endfunc". */
23190 if (STRCMP(theline, skip_until) == 0)
23191 {
23192 vim_free(skip_until);
23193 skip_until = NULL;
23194 }
23195 }
23196 else
23197 {
23198 /* skip ':' and blanks*/
23199 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23200 ;
23201
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023202 /* Check for "endfunction". */
23203 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023205 if (line_arg == NULL)
23206 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 break;
23208 }
23209
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023210 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211 * at "end". */
23212 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23213 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023214 else if (STRNCMP(p, "if", 2) == 0
23215 || STRNCMP(p, "wh", 2) == 0
23216 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217 || STRNCMP(p, "try", 3) == 0)
23218 indent += 2;
23219
23220 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023221 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023223 if (*p == '!')
23224 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023225 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023226 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23227 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023229 ++nesting;
23230 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231 }
23232 }
23233
23234 /* Check for ":append" or ":insert". */
23235 p = skip_range(p, NULL);
23236 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23237 || (p[0] == 'i'
23238 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23239 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23240 skip_until = vim_strsave((char_u *)".");
23241
23242 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23243 arg = skipwhite(skiptowhite(p));
23244 if (arg[0] == '<' && arg[1] =='<'
23245 && ((p[0] == 'p' && p[1] == 'y'
23246 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23247 || (p[0] == 'p' && p[1] == 'e'
23248 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23249 || (p[0] == 't' && p[1] == 'c'
23250 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023251 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23252 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023253 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23254 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023255 || (p[0] == 'm' && p[1] == 'z'
23256 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257 ))
23258 {
23259 /* ":python <<" continues until a dot, like ":append" */
23260 p = skipwhite(arg + 2);
23261 if (*p == NUL)
23262 skip_until = vim_strsave((char_u *)".");
23263 else
23264 skip_until = vim_strsave(p);
23265 }
23266 }
23267
23268 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023269 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023270 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023271 if (line_arg == NULL)
23272 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023274 }
23275
23276 /* Copy the line to newly allocated memory. get_one_sourceline()
23277 * allocates 250 bytes per line, this saves 80% on average. The cost
23278 * is an extra alloc/free. */
23279 p = vim_strsave(theline);
23280 if (p != NULL)
23281 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023282 if (line_arg == NULL)
23283 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023284 theline = p;
23285 }
23286
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023287 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23288
23289 /* Add NULL lines for continuation lines, so that the line count is
23290 * equal to the index in the growarray. */
23291 while (sourcing_lnum_off-- > 0)
23292 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023293
23294 /* Check for end of eap->arg. */
23295 if (line_arg != NULL && *line_arg == NUL)
23296 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023297 }
23298
23299 /* Don't define the function when skipping commands or when an error was
23300 * detected. */
23301 if (eap->skip || did_emsg)
23302 goto erret;
23303
23304 /*
23305 * If there are no errors, add the function
23306 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023307 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023308 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023309 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023310 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023311 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023312 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023313 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023314 goto erret;
23315 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023316
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023317 fp = find_func(name);
23318 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023320 if (!eap->forceit)
23321 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023322 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023323 goto erret;
23324 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023325 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023326 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023327 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023328 name);
23329 goto erret;
23330 }
23331 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023332 ga_clear_strings(&(fp->uf_args));
23333 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023334 vim_free(name);
23335 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023337 }
23338 else
23339 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023340 char numbuf[20];
23341
23342 fp = NULL;
23343 if (fudi.fd_newkey == NULL && !eap->forceit)
23344 {
23345 EMSG(_(e_funcdict));
23346 goto erret;
23347 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023348 if (fudi.fd_di == NULL)
23349 {
23350 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023351 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023352 goto erret;
23353 }
23354 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023355 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023356 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023357
23358 /* Give the function a sequential number. Can only be used with a
23359 * Funcref! */
23360 vim_free(name);
23361 sprintf(numbuf, "%d", ++func_nr);
23362 name = vim_strsave((char_u *)numbuf);
23363 if (name == NULL)
23364 goto erret;
23365 }
23366
23367 if (fp == NULL)
23368 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023369 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023370 {
23371 int slen, plen;
23372 char_u *scriptname;
23373
23374 /* Check that the autoload name matches the script name. */
23375 j = FAIL;
23376 if (sourcing_name != NULL)
23377 {
23378 scriptname = autoload_name(name);
23379 if (scriptname != NULL)
23380 {
23381 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023382 plen = (int)STRLEN(p);
23383 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023384 if (slen > plen && fnamecmp(p,
23385 sourcing_name + slen - plen) == 0)
23386 j = OK;
23387 vim_free(scriptname);
23388 }
23389 }
23390 if (j == FAIL)
23391 {
23392 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23393 goto erret;
23394 }
23395 }
23396
23397 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398 if (fp == NULL)
23399 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023400
23401 if (fudi.fd_dict != NULL)
23402 {
23403 if (fudi.fd_di == NULL)
23404 {
23405 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023406 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023407 if (fudi.fd_di == NULL)
23408 {
23409 vim_free(fp);
23410 goto erret;
23411 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023412 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23413 {
23414 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023415 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023416 goto erret;
23417 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023418 }
23419 else
23420 /* overwrite existing dict entry */
23421 clear_tv(&fudi.fd_di->di_tv);
23422 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023423 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023424 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023425 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023426
23427 /* behave like "dict" was used */
23428 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023429 }
23430
Bram Moolenaar071d4272004-06-13 20:20:40 +000023431 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023432 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023433 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23434 {
23435 vim_free(fp);
23436 goto erret;
23437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023439 fp->uf_args = newargs;
23440 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023441#ifdef FEAT_PROFILE
23442 fp->uf_tml_count = NULL;
23443 fp->uf_tml_total = NULL;
23444 fp->uf_tml_self = NULL;
23445 fp->uf_profiling = FALSE;
23446 if (prof_def_func())
23447 func_do_profile(fp);
23448#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023449 fp->uf_varargs = varargs;
23450 fp->uf_flags = flags;
23451 fp->uf_calls = 0;
23452 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023453 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023454
23455erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023456 ga_clear_strings(&newargs);
23457 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023458ret_free:
23459 vim_free(skip_until);
23460 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023462 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023463 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464}
23465
23466/*
23467 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023468 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023470 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023471 * TFN_INT: internal function name OK
23472 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023473 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 * Advances "pp" to just after the function name (if no error).
23475 */
23476 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023477trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478 char_u **pp;
23479 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023480 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023481 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023483 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023484 char_u *start;
23485 char_u *end;
23486 int lead;
23487 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023488 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023489 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023490
23491 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023492 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023494
23495 /* Check for hard coded <SNR>: already translated function ID (from a user
23496 * command). */
23497 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23498 && (*pp)[2] == (int)KE_SNR)
23499 {
23500 *pp += 3;
23501 len = get_id_len(pp) + 3;
23502 return vim_strnsave(start, len);
23503 }
23504
23505 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23506 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023507 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023508 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023510
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023511 /* Note that TFN_ flags use the same values as GLV_ flags. */
23512 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023513 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023514 if (end == start)
23515 {
23516 if (!skip)
23517 EMSG(_("E129: Function name required"));
23518 goto theend;
23519 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023520 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023521 {
23522 /*
23523 * Report an invalid expression in braces, unless the expression
23524 * evaluation has been cancelled due to an aborting error, an
23525 * interrupt, or an exception.
23526 */
23527 if (!aborting())
23528 {
23529 if (end != NULL)
23530 EMSG2(_(e_invarg2), start);
23531 }
23532 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023533 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023534 goto theend;
23535 }
23536
23537 if (lv.ll_tv != NULL)
23538 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023539 if (fdp != NULL)
23540 {
23541 fdp->fd_dict = lv.ll_dict;
23542 fdp->fd_newkey = lv.ll_newkey;
23543 lv.ll_newkey = NULL;
23544 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023545 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023546 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23547 {
23548 name = vim_strsave(lv.ll_tv->vval.v_string);
23549 *pp = end;
23550 }
23551 else
23552 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023553 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23554 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023555 EMSG(_(e_funcref));
23556 else
23557 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023558 name = NULL;
23559 }
23560 goto theend;
23561 }
23562
23563 if (lv.ll_name == NULL)
23564 {
23565 /* Error found, but continue after the function name. */
23566 *pp = end;
23567 goto theend;
23568 }
23569
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023570 /* Check if the name is a Funcref. If so, use the value. */
23571 if (lv.ll_exp_name != NULL)
23572 {
23573 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023574 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023575 if (name == lv.ll_exp_name)
23576 name = NULL;
23577 }
23578 else
23579 {
23580 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023581 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023582 if (name == *pp)
23583 name = NULL;
23584 }
23585 if (name != NULL)
23586 {
23587 name = vim_strsave(name);
23588 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023589 if (STRNCMP(name, "<SNR>", 5) == 0)
23590 {
23591 /* Change "<SNR>" to the byte sequence. */
23592 name[0] = K_SPECIAL;
23593 name[1] = KS_EXTRA;
23594 name[2] = (int)KE_SNR;
23595 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23596 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023597 goto theend;
23598 }
23599
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023600 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023601 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023602 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023603 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23604 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23605 {
23606 /* When there was "s:" already or the name expanded to get a
23607 * leading "s:" then remove it. */
23608 lv.ll_name += 2;
23609 len -= 2;
23610 lead = 2;
23611 }
23612 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023613 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023614 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023615 /* skip over "s:" and "g:" */
23616 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023617 lv.ll_name += 2;
23618 len = (int)(end - lv.ll_name);
23619 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023620
23621 /*
23622 * Copy the function name to allocated memory.
23623 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23624 * Accept <SNR>123_name() outside a script.
23625 */
23626 if (skip)
23627 lead = 0; /* do nothing */
23628 else if (lead > 0)
23629 {
23630 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023631 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23632 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023633 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023634 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023635 if (current_SID <= 0)
23636 {
23637 EMSG(_(e_usingsid));
23638 goto theend;
23639 }
23640 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23641 lead += (int)STRLEN(sid_buf);
23642 }
23643 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023644 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023645 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023646 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023647 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023648 goto theend;
23649 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023650 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023651 {
23652 char_u *cp = vim_strchr(lv.ll_name, ':');
23653
23654 if (cp != NULL && cp < end)
23655 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023656 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023657 goto theend;
23658 }
23659 }
23660
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023661 name = alloc((unsigned)(len + lead + 1));
23662 if (name != NULL)
23663 {
23664 if (lead > 0)
23665 {
23666 name[0] = K_SPECIAL;
23667 name[1] = KS_EXTRA;
23668 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023669 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023670 STRCPY(name + 3, sid_buf);
23671 }
23672 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023673 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023674 }
23675 *pp = end;
23676
23677theend:
23678 clear_lval(&lv);
23679 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680}
23681
23682/*
23683 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23684 * Return 2 if "p" starts with "s:".
23685 * Return 0 otherwise.
23686 */
23687 static int
23688eval_fname_script(p)
23689 char_u *p;
23690{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023691 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23692 * the standard library function. */
23693 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23694 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023695 return 5;
23696 if (p[0] == 's' && p[1] == ':')
23697 return 2;
23698 return 0;
23699}
23700
23701/*
23702 * Return TRUE if "p" starts with "<SID>" or "s:".
23703 * Only works if eval_fname_script() returned non-zero for "p"!
23704 */
23705 static int
23706eval_fname_sid(p)
23707 char_u *p;
23708{
23709 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23710}
23711
23712/*
23713 * List the head of the function: "name(arg1, arg2)".
23714 */
23715 static void
23716list_func_head(fp, indent)
23717 ufunc_T *fp;
23718 int indent;
23719{
23720 int j;
23721
23722 msg_start();
23723 if (indent)
23724 MSG_PUTS(" ");
23725 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023726 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023727 {
23728 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023729 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023730 }
23731 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023732 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023733 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023734 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023735 {
23736 if (j)
23737 MSG_PUTS(", ");
23738 msg_puts(FUNCARG(fp, j));
23739 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023740 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023741 {
23742 if (j)
23743 MSG_PUTS(", ");
23744 MSG_PUTS("...");
23745 }
23746 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023747 if (fp->uf_flags & FC_ABORT)
23748 MSG_PUTS(" abort");
23749 if (fp->uf_flags & FC_RANGE)
23750 MSG_PUTS(" range");
23751 if (fp->uf_flags & FC_DICT)
23752 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023753 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023754 if (p_verbose > 0)
23755 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023756}
23757
23758/*
23759 * Find a function by name, return pointer to it in ufuncs.
23760 * Return NULL for unknown function.
23761 */
23762 static ufunc_T *
23763find_func(name)
23764 char_u *name;
23765{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023766 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023768 hi = hash_find(&func_hashtab, name);
23769 if (!HASHITEM_EMPTY(hi))
23770 return HI2UF(hi);
23771 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023772}
23773
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023774#if defined(EXITFREE) || defined(PROTO)
23775 void
23776free_all_functions()
23777{
23778 hashitem_T *hi;
23779
23780 /* Need to start all over every time, because func_free() may change the
23781 * hash table. */
23782 while (func_hashtab.ht_used > 0)
23783 for (hi = func_hashtab.ht_array; ; ++hi)
23784 if (!HASHITEM_EMPTY(hi))
23785 {
23786 func_free(HI2UF(hi));
23787 break;
23788 }
23789}
23790#endif
23791
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023792 int
23793translated_function_exists(name)
23794 char_u *name;
23795{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023796 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023797 return find_internal_func(name) >= 0;
23798 return find_func(name) != NULL;
23799}
23800
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023801/*
23802 * Return TRUE if a function "name" exists.
23803 */
23804 static int
23805function_exists(name)
23806 char_u *name;
23807{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023808 char_u *nm = name;
23809 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023810 int n = FALSE;
23811
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023812 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23813 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023814 nm = skipwhite(nm);
23815
23816 /* Only accept "funcname", "funcname ", "funcname (..." and
23817 * "funcname(...", not "funcname!...". */
23818 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023819 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023820 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023821 return n;
23822}
23823
Bram Moolenaara1544c02013-05-30 12:35:52 +020023824 char_u *
23825get_expanded_name(name, check)
23826 char_u *name;
23827 int check;
23828{
23829 char_u *nm = name;
23830 char_u *p;
23831
23832 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23833
23834 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023835 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023836 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023837
Bram Moolenaara1544c02013-05-30 12:35:52 +020023838 vim_free(p);
23839 return NULL;
23840}
23841
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023842/*
23843 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023844 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23845 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023846 */
23847 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023848builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023849 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023850 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023851{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023852 char_u *p;
23853
23854 if (!ASCII_ISLOWER(name[0]))
23855 return FALSE;
23856 p = vim_strchr(name, AUTOLOAD_CHAR);
23857 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023858}
23859
Bram Moolenaar05159a02005-02-26 23:04:13 +000023860#if defined(FEAT_PROFILE) || defined(PROTO)
23861/*
23862 * Start profiling function "fp".
23863 */
23864 static void
23865func_do_profile(fp)
23866 ufunc_T *fp;
23867{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023868 int len = fp->uf_lines.ga_len;
23869
23870 if (len == 0)
23871 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023872 fp->uf_tm_count = 0;
23873 profile_zero(&fp->uf_tm_self);
23874 profile_zero(&fp->uf_tm_total);
23875 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023876 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023877 if (fp->uf_tml_total == NULL)
23878 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023879 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023880 if (fp->uf_tml_self == NULL)
23881 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023882 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023883 fp->uf_tml_idx = -1;
23884 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23885 || fp->uf_tml_self == NULL)
23886 return; /* out of memory */
23887
23888 fp->uf_profiling = TRUE;
23889}
23890
23891/*
23892 * Dump the profiling results for all functions in file "fd".
23893 */
23894 void
23895func_dump_profile(fd)
23896 FILE *fd;
23897{
23898 hashitem_T *hi;
23899 int todo;
23900 ufunc_T *fp;
23901 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023902 ufunc_T **sorttab;
23903 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023904
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023905 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023906 if (todo == 0)
23907 return; /* nothing to dump */
23908
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023909 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023910
Bram Moolenaar05159a02005-02-26 23:04:13 +000023911 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23912 {
23913 if (!HASHITEM_EMPTY(hi))
23914 {
23915 --todo;
23916 fp = HI2UF(hi);
23917 if (fp->uf_profiling)
23918 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023919 if (sorttab != NULL)
23920 sorttab[st_len++] = fp;
23921
Bram Moolenaar05159a02005-02-26 23:04:13 +000023922 if (fp->uf_name[0] == K_SPECIAL)
23923 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23924 else
23925 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23926 if (fp->uf_tm_count == 1)
23927 fprintf(fd, "Called 1 time\n");
23928 else
23929 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23930 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23931 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23932 fprintf(fd, "\n");
23933 fprintf(fd, "count total (s) self (s)\n");
23934
23935 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23936 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023937 if (FUNCLINE(fp, i) == NULL)
23938 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023939 prof_func_line(fd, fp->uf_tml_count[i],
23940 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023941 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23942 }
23943 fprintf(fd, "\n");
23944 }
23945 }
23946 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023947
23948 if (sorttab != NULL && st_len > 0)
23949 {
23950 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23951 prof_total_cmp);
23952 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23953 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23954 prof_self_cmp);
23955 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23956 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023957
23958 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023959}
Bram Moolenaar73830342005-02-28 22:48:19 +000023960
23961 static void
23962prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23963 FILE *fd;
23964 ufunc_T **sorttab;
23965 int st_len;
23966 char *title;
23967 int prefer_self; /* when equal print only self time */
23968{
23969 int i;
23970 ufunc_T *fp;
23971
23972 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23973 fprintf(fd, "count total (s) self (s) function\n");
23974 for (i = 0; i < 20 && i < st_len; ++i)
23975 {
23976 fp = sorttab[i];
23977 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23978 prefer_self);
23979 if (fp->uf_name[0] == K_SPECIAL)
23980 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23981 else
23982 fprintf(fd, " %s()\n", fp->uf_name);
23983 }
23984 fprintf(fd, "\n");
23985}
23986
23987/*
23988 * Print the count and times for one function or function line.
23989 */
23990 static void
23991prof_func_line(fd, count, total, self, prefer_self)
23992 FILE *fd;
23993 int count;
23994 proftime_T *total;
23995 proftime_T *self;
23996 int prefer_self; /* when equal print only self time */
23997{
23998 if (count > 0)
23999 {
24000 fprintf(fd, "%5d ", count);
24001 if (prefer_self && profile_equal(total, self))
24002 fprintf(fd, " ");
24003 else
24004 fprintf(fd, "%s ", profile_msg(total));
24005 if (!prefer_self && profile_equal(total, self))
24006 fprintf(fd, " ");
24007 else
24008 fprintf(fd, "%s ", profile_msg(self));
24009 }
24010 else
24011 fprintf(fd, " ");
24012}
24013
24014/*
24015 * Compare function for total time sorting.
24016 */
24017 static int
24018#ifdef __BORLANDC__
24019_RTLENTRYF
24020#endif
24021prof_total_cmp(s1, s2)
24022 const void *s1;
24023 const void *s2;
24024{
24025 ufunc_T *p1, *p2;
24026
24027 p1 = *(ufunc_T **)s1;
24028 p2 = *(ufunc_T **)s2;
24029 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24030}
24031
24032/*
24033 * Compare function for self time sorting.
24034 */
24035 static int
24036#ifdef __BORLANDC__
24037_RTLENTRYF
24038#endif
24039prof_self_cmp(s1, s2)
24040 const void *s1;
24041 const void *s2;
24042{
24043 ufunc_T *p1, *p2;
24044
24045 p1 = *(ufunc_T **)s1;
24046 p2 = *(ufunc_T **)s2;
24047 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24048}
24049
Bram Moolenaar05159a02005-02-26 23:04:13 +000024050#endif
24051
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024052/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024053 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024054 * Return TRUE if a package was loaded.
24055 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024056 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024057script_autoload(name, reload)
24058 char_u *name;
24059 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024060{
24061 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024062 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024063 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024064 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024065
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024066 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024067 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024068 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024069 return FALSE;
24070
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024071 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024072
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024073 /* Find the name in the list of previously loaded package names. Skip
24074 * "autoload/", it's always the same. */
24075 for (i = 0; i < ga_loaded.ga_len; ++i)
24076 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24077 break;
24078 if (!reload && i < ga_loaded.ga_len)
24079 ret = FALSE; /* was loaded already */
24080 else
24081 {
24082 /* Remember the name if it wasn't loaded already. */
24083 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24084 {
24085 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24086 tofree = NULL;
24087 }
24088
24089 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024090 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024091 ret = TRUE;
24092 }
24093
24094 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024095 return ret;
24096}
24097
24098/*
24099 * Return the autoload script name for a function or variable name.
24100 * Returns NULL when out of memory.
24101 */
24102 static char_u *
24103autoload_name(name)
24104 char_u *name;
24105{
24106 char_u *p;
24107 char_u *scriptname;
24108
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024109 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024110 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24111 if (scriptname == NULL)
24112 return FALSE;
24113 STRCPY(scriptname, "autoload/");
24114 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024115 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024116 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024117 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024118 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024119 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024120}
24121
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24123
24124/*
24125 * Function given to ExpandGeneric() to obtain the list of user defined
24126 * function names.
24127 */
24128 char_u *
24129get_user_func_name(xp, idx)
24130 expand_T *xp;
24131 int idx;
24132{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024133 static long_u done;
24134 static hashitem_T *hi;
24135 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136
24137 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024139 done = 0;
24140 hi = func_hashtab.ht_array;
24141 }
24142 if (done < func_hashtab.ht_used)
24143 {
24144 if (done++ > 0)
24145 ++hi;
24146 while (HASHITEM_EMPTY(hi))
24147 ++hi;
24148 fp = HI2UF(hi);
24149
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024150 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024151 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024152
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024153 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24154 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024155
24156 cat_func_name(IObuff, fp);
24157 if (xp->xp_context != EXPAND_USER_FUNC)
24158 {
24159 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024160 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024161 STRCAT(IObuff, ")");
24162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024163 return IObuff;
24164 }
24165 return NULL;
24166}
24167
24168#endif /* FEAT_CMDL_COMPL */
24169
24170/*
24171 * Copy the function name of "fp" to buffer "buf".
24172 * "buf" must be able to hold the function name plus three bytes.
24173 * Takes care of script-local function names.
24174 */
24175 static void
24176cat_func_name(buf, fp)
24177 char_u *buf;
24178 ufunc_T *fp;
24179{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024180 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181 {
24182 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024183 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184 }
24185 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024186 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187}
24188
24189/*
24190 * ":delfunction {name}"
24191 */
24192 void
24193ex_delfunction(eap)
24194 exarg_T *eap;
24195{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024196 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024197 char_u *p;
24198 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024199 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024200
24201 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024202 name = trans_function_name(&p, eap->skip, 0, &fudi);
24203 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024204 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024205 {
24206 if (fudi.fd_dict != NULL && !eap->skip)
24207 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024208 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024210 if (!ends_excmd(*skipwhite(p)))
24211 {
24212 vim_free(name);
24213 EMSG(_(e_trailing));
24214 return;
24215 }
24216 eap->nextcmd = check_nextcmd(p);
24217 if (eap->nextcmd != NULL)
24218 *p = NUL;
24219
24220 if (!eap->skip)
24221 fp = find_func(name);
24222 vim_free(name);
24223
24224 if (!eap->skip)
24225 {
24226 if (fp == NULL)
24227 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024228 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229 return;
24230 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024231 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024232 {
24233 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24234 return;
24235 }
24236
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024237 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024238 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024239 /* Delete the dict item that refers to the function, it will
24240 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024241 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024243 else
24244 func_free(fp);
24245 }
24246}
24247
24248/*
24249 * Free a function and remove it from the list of functions.
24250 */
24251 static void
24252func_free(fp)
24253 ufunc_T *fp;
24254{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024255 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024256
24257 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024258 ga_clear_strings(&(fp->uf_args));
24259 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024260#ifdef FEAT_PROFILE
24261 vim_free(fp->uf_tml_count);
24262 vim_free(fp->uf_tml_total);
24263 vim_free(fp->uf_tml_self);
24264#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024265
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024266 /* remove the function from the function hashtable */
24267 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24268 if (HASHITEM_EMPTY(hi))
24269 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024270 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024271 hash_remove(&func_hashtab, hi);
24272
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024273 vim_free(fp);
24274}
24275
24276/*
24277 * Unreference a Function: decrement the reference count and free it when it
24278 * becomes zero. Only for numbered functions.
24279 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024280 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024281func_unref(name)
24282 char_u *name;
24283{
24284 ufunc_T *fp;
24285
24286 if (name != NULL && isdigit(*name))
24287 {
24288 fp = find_func(name);
24289 if (fp == NULL)
24290 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024291 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024292 {
24293 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024294 * when "uf_calls" becomes zero. */
24295 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024296 func_free(fp);
24297 }
24298 }
24299}
24300
24301/*
24302 * Count a reference to a Function.
24303 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024304 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024305func_ref(name)
24306 char_u *name;
24307{
24308 ufunc_T *fp;
24309
24310 if (name != NULL && isdigit(*name))
24311 {
24312 fp = find_func(name);
24313 if (fp == NULL)
24314 EMSG2(_(e_intern2), "func_ref()");
24315 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024316 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024317 }
24318}
24319
24320/*
24321 * Call a user function.
24322 */
24323 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024324call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 ufunc_T *fp; /* pointer to function */
24326 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024327 typval_T *argvars; /* arguments */
24328 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024329 linenr_T firstline; /* first line of range */
24330 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024331 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024332{
Bram Moolenaar33570922005-01-25 22:26:29 +000024333 char_u *save_sourcing_name;
24334 linenr_T save_sourcing_lnum;
24335 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024336 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024337 int save_did_emsg;
24338 static int depth = 0;
24339 dictitem_T *v;
24340 int fixvar_idx = 0; /* index in fixvar[] */
24341 int i;
24342 int ai;
24343 char_u numbuf[NUMBUFLEN];
24344 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024345 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024346#ifdef FEAT_PROFILE
24347 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024348 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350
24351 /* If depth of calling is getting too high, don't execute the function */
24352 if (depth >= p_mfd)
24353 {
24354 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024355 rettv->v_type = VAR_NUMBER;
24356 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 return;
24358 }
24359 ++depth;
24360
24361 line_breakcheck(); /* check for CTRL-C hit */
24362
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024363 fc = (funccall_T *)alloc(sizeof(funccall_T));
24364 fc->caller = current_funccal;
24365 current_funccal = fc;
24366 fc->func = fp;
24367 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024368 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024369 fc->linenr = 0;
24370 fc->returned = FALSE;
24371 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024373 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24374 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375
Bram Moolenaar33570922005-01-25 22:26:29 +000024376 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024377 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024378 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24379 * each argument variable and saves a lot of time.
24380 */
24381 /*
24382 * Init l: variables.
24383 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024384 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024385 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024386 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024387 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24388 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024389 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024390 name = v->di_key;
24391 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024392 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024393 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024394 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024395 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024396 v->di_tv.vval.v_dict = selfdict;
24397 ++selfdict->dv_refcount;
24398 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024399
Bram Moolenaar33570922005-01-25 22:26:29 +000024400 /*
24401 * Init a: variables.
24402 * Set a:0 to "argcount".
24403 * Set a:000 to a list with room for the "..." arguments.
24404 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024405 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024406 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024407 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024408 /* Use "name" to avoid a warning from some compiler that checks the
24409 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024410 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024411 name = v->di_key;
24412 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024413 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024414 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024415 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024416 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024417 v->di_tv.vval.v_list = &fc->l_varlist;
24418 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24419 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24420 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024421
24422 /*
24423 * Set a:firstline to "firstline" and a:lastline to "lastline".
24424 * Set a:name to named arguments.
24425 * Set a:N to the "..." arguments.
24426 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024427 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024428 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024429 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024430 (varnumber_T)lastline);
24431 for (i = 0; i < argcount; ++i)
24432 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024433 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024434 if (ai < 0)
24435 /* named argument a:name */
24436 name = FUNCARG(fp, i);
24437 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024438 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024439 /* "..." argument a:1, a:2, etc. */
24440 sprintf((char *)numbuf, "%d", ai + 1);
24441 name = numbuf;
24442 }
24443 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24444 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024445 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024446 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24447 }
24448 else
24449 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024450 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24451 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024452 if (v == NULL)
24453 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024454 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024455 }
24456 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024457 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024458
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024459 /* Note: the values are copied directly to avoid alloc/free.
24460 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024461 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024462 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024463
24464 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24465 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024466 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24467 fc->l_listitems[ai].li_tv = argvars[i];
24468 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024469 }
24470 }
24471
Bram Moolenaar071d4272004-06-13 20:20:40 +000024472 /* Don't redraw while executing the function. */
24473 ++RedrawingDisabled;
24474 save_sourcing_name = sourcing_name;
24475 save_sourcing_lnum = sourcing_lnum;
24476 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024477 /* need space for function name + ("function " + 3) or "[number]" */
24478 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24479 + STRLEN(fp->uf_name) + 20;
24480 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481 if (sourcing_name != NULL)
24482 {
24483 if (save_sourcing_name != NULL
24484 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024485 sprintf((char *)sourcing_name, "%s[%d]..",
24486 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024487 else
24488 STRCPY(sourcing_name, "function ");
24489 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24490
24491 if (p_verbose >= 12)
24492 {
24493 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024494 verbose_enter_scroll();
24495
Bram Moolenaar555b2802005-05-19 21:08:39 +000024496 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024497 if (p_verbose >= 14)
24498 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024499 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024500 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024501 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024502 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503
24504 msg_puts((char_u *)"(");
24505 for (i = 0; i < argcount; ++i)
24506 {
24507 if (i > 0)
24508 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024509 if (argvars[i].v_type == VAR_NUMBER)
24510 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024511 else
24512 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024513 /* Do not want errors such as E724 here. */
24514 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024515 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024516 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024517 if (s != NULL)
24518 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024519 if (vim_strsize(s) > MSG_BUF_CLEN)
24520 {
24521 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24522 s = buf;
24523 }
24524 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024525 vim_free(tofree);
24526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527 }
24528 }
24529 msg_puts((char_u *)")");
24530 }
24531 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024532
24533 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 --no_wait_return;
24535 }
24536 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024537#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024538 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024539 {
24540 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24541 func_do_profile(fp);
24542 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024543 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024544 {
24545 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024546 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024547 profile_zero(&fp->uf_tm_children);
24548 }
24549 script_prof_save(&wait_start);
24550 }
24551#endif
24552
Bram Moolenaar071d4272004-06-13 20:20:40 +000024553 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024554 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024555 save_did_emsg = did_emsg;
24556 did_emsg = FALSE;
24557
24558 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024559 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024560 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24561
24562 --RedrawingDisabled;
24563
24564 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024565 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024567 clear_tv(rettv);
24568 rettv->v_type = VAR_NUMBER;
24569 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570 }
24571
Bram Moolenaar05159a02005-02-26 23:04:13 +000024572#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024573 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024574 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024575 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024576 profile_end(&call_start);
24577 profile_sub_wait(&wait_start, &call_start);
24578 profile_add(&fp->uf_tm_total, &call_start);
24579 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024580 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024581 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024582 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24583 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024584 }
24585 }
24586#endif
24587
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588 /* when being verbose, mention the return value */
24589 if (p_verbose >= 12)
24590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024591 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024592 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024593
Bram Moolenaar071d4272004-06-13 20:20:40 +000024594 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024595 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024596 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024597 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024598 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024599 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024600 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024601 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024602 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024603 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024604 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024605
Bram Moolenaar555b2802005-05-19 21:08:39 +000024606 /* The value may be very long. Skip the middle part, so that we
24607 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024608 * truncate it at the end. Don't want errors such as E724 here. */
24609 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024610 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024611 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024612 if (s != NULL)
24613 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024614 if (vim_strsize(s) > MSG_BUF_CLEN)
24615 {
24616 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24617 s = buf;
24618 }
24619 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024620 vim_free(tofree);
24621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622 }
24623 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024624
24625 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 --no_wait_return;
24627 }
24628
24629 vim_free(sourcing_name);
24630 sourcing_name = save_sourcing_name;
24631 sourcing_lnum = save_sourcing_lnum;
24632 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024633#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024634 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024635 script_prof_restore(&wait_start);
24636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637
24638 if (p_verbose >= 12 && sourcing_name != NULL)
24639 {
24640 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024641 verbose_enter_scroll();
24642
Bram Moolenaar555b2802005-05-19 21:08:39 +000024643 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024644 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024645
24646 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024647 --no_wait_return;
24648 }
24649
24650 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024651 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024652 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024653
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024654 /* If the a:000 list and the l: and a: dicts are not referenced we can
24655 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024656 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24657 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24658 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24659 {
24660 free_funccal(fc, FALSE);
24661 }
24662 else
24663 {
24664 hashitem_T *hi;
24665 listitem_T *li;
24666 int todo;
24667
24668 /* "fc" is still in use. This can happen when returning "a:000" or
24669 * assigning "l:" to a global variable.
24670 * Link "fc" in the list for garbage collection later. */
24671 fc->caller = previous_funccal;
24672 previous_funccal = fc;
24673
24674 /* Make a copy of the a: variables, since we didn't do that above. */
24675 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24676 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24677 {
24678 if (!HASHITEM_EMPTY(hi))
24679 {
24680 --todo;
24681 v = HI2DI(hi);
24682 copy_tv(&v->di_tv, &v->di_tv);
24683 }
24684 }
24685
24686 /* Make a copy of the a:000 items, since we didn't do that above. */
24687 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24688 copy_tv(&li->li_tv, &li->li_tv);
24689 }
24690}
24691
24692/*
24693 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024694 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024695 */
24696 static int
24697can_free_funccal(fc, copyID)
24698 funccall_T *fc;
24699 int copyID;
24700{
24701 return (fc->l_varlist.lv_copyID != copyID
24702 && fc->l_vars.dv_copyID != copyID
24703 && fc->l_avars.dv_copyID != copyID);
24704}
24705
24706/*
24707 * Free "fc" and what it contains.
24708 */
24709 static void
24710free_funccal(fc, free_val)
24711 funccall_T *fc;
24712 int free_val; /* a: vars were allocated */
24713{
24714 listitem_T *li;
24715
24716 /* The a: variables typevals may not have been allocated, only free the
24717 * allocated variables. */
24718 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24719
24720 /* free all l: variables */
24721 vars_clear(&fc->l_vars.dv_hashtab);
24722
24723 /* Free the a:000 variables if they were allocated. */
24724 if (free_val)
24725 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24726 clear_tv(&li->li_tv);
24727
24728 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729}
24730
24731/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024732 * Add a number variable "name" to dict "dp" with value "nr".
24733 */
24734 static void
24735add_nr_var(dp, v, name, nr)
24736 dict_T *dp;
24737 dictitem_T *v;
24738 char *name;
24739 varnumber_T nr;
24740{
24741 STRCPY(v->di_key, name);
24742 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24743 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24744 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024745 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024746 v->di_tv.vval.v_number = nr;
24747}
24748
24749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024750 * ":return [expr]"
24751 */
24752 void
24753ex_return(eap)
24754 exarg_T *eap;
24755{
24756 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024757 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024758 int returning = FALSE;
24759
24760 if (current_funccal == NULL)
24761 {
24762 EMSG(_("E133: :return not inside a function"));
24763 return;
24764 }
24765
24766 if (eap->skip)
24767 ++emsg_skip;
24768
24769 eap->nextcmd = NULL;
24770 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024771 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024772 {
24773 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024774 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024775 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024776 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024777 }
24778 /* It's safer to return also on error. */
24779 else if (!eap->skip)
24780 {
24781 /*
24782 * Return unless the expression evaluation has been cancelled due to an
24783 * aborting error, an interrupt, or an exception.
24784 */
24785 if (!aborting())
24786 returning = do_return(eap, FALSE, TRUE, NULL);
24787 }
24788
24789 /* When skipping or the return gets pending, advance to the next command
24790 * in this line (!returning). Otherwise, ignore the rest of the line.
24791 * Following lines will be ignored by get_func_line(). */
24792 if (returning)
24793 eap->nextcmd = NULL;
24794 else if (eap->nextcmd == NULL) /* no argument */
24795 eap->nextcmd = check_nextcmd(arg);
24796
24797 if (eap->skip)
24798 --emsg_skip;
24799}
24800
24801/*
24802 * Return from a function. Possibly makes the return pending. Also called
24803 * for a pending return at the ":endtry" or after returning from an extra
24804 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024805 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024806 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807 * FALSE when the return gets pending.
24808 */
24809 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024810do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024811 exarg_T *eap;
24812 int reanimate;
24813 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024814 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024815{
24816 int idx;
24817 struct condstack *cstack = eap->cstack;
24818
24819 if (reanimate)
24820 /* Undo the return. */
24821 current_funccal->returned = FALSE;
24822
24823 /*
24824 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24825 * not in its finally clause (which then is to be executed next) is found.
24826 * In this case, make the ":return" pending for execution at the ":endtry".
24827 * Otherwise, return normally.
24828 */
24829 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24830 if (idx >= 0)
24831 {
24832 cstack->cs_pending[idx] = CSTP_RETURN;
24833
24834 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024835 /* A pending return again gets pending. "rettv" points to an
24836 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024837 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024838 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839 else
24840 {
24841 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024842 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024843 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024844 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024846 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847 {
24848 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024849 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024850 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851 else
24852 EMSG(_(e_outofmem));
24853 }
24854 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024855 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024856
24857 if (reanimate)
24858 {
24859 /* The pending return value could be overwritten by a ":return"
24860 * without argument in a finally clause; reset the default
24861 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024862 current_funccal->rettv->v_type = VAR_NUMBER;
24863 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864 }
24865 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024866 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024867 }
24868 else
24869 {
24870 current_funccal->returned = TRUE;
24871
24872 /* If the return is carried out now, store the return value. For
24873 * a return immediately after reanimation, the value is already
24874 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024875 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024877 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024878 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024879 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024880 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024881 }
24882 }
24883
24884 return idx < 0;
24885}
24886
24887/*
24888 * Free the variable with a pending return value.
24889 */
24890 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024891discard_pending_return(rettv)
24892 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024893{
Bram Moolenaar33570922005-01-25 22:26:29 +000024894 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895}
24896
24897/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024898 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899 * is an allocated string. Used by report_pending() for verbose messages.
24900 */
24901 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024902get_return_cmd(rettv)
24903 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024904{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024905 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024906 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024907 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024909 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024910 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024911 if (s == NULL)
24912 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024913
24914 STRCPY(IObuff, ":return ");
24915 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24916 if (STRLEN(s) + 8 >= IOSIZE)
24917 STRCPY(IObuff + IOSIZE - 4, "...");
24918 vim_free(tofree);
24919 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920}
24921
24922/*
24923 * Get next function line.
24924 * Called by do_cmdline() to get the next line.
24925 * Returns allocated string, or NULL for end of function.
24926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024927 char_u *
24928get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024929 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024930 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024931 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024932{
Bram Moolenaar33570922005-01-25 22:26:29 +000024933 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024934 ufunc_T *fp = fcp->func;
24935 char_u *retval;
24936 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937
24938 /* If breakpoints have been added/deleted need to check for it. */
24939 if (fcp->dbg_tick != debug_tick)
24940 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024941 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024942 sourcing_lnum);
24943 fcp->dbg_tick = debug_tick;
24944 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024945#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024946 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024947 func_line_end(cookie);
24948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024949
Bram Moolenaar05159a02005-02-26 23:04:13 +000024950 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024951 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24952 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024953 retval = NULL;
24954 else
24955 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024956 /* Skip NULL lines (continuation lines). */
24957 while (fcp->linenr < gap->ga_len
24958 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24959 ++fcp->linenr;
24960 if (fcp->linenr >= gap->ga_len)
24961 retval = NULL;
24962 else
24963 {
24964 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24965 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024966#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024967 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024968 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024969#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024971 }
24972
24973 /* Did we encounter a breakpoint? */
24974 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24975 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024976 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024977 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024978 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024979 sourcing_lnum);
24980 fcp->dbg_tick = debug_tick;
24981 }
24982
24983 return retval;
24984}
24985
Bram Moolenaar05159a02005-02-26 23:04:13 +000024986#if defined(FEAT_PROFILE) || defined(PROTO)
24987/*
24988 * Called when starting to read a function line.
24989 * "sourcing_lnum" must be correct!
24990 * When skipping lines it may not actually be executed, but we won't find out
24991 * until later and we need to store the time now.
24992 */
24993 void
24994func_line_start(cookie)
24995 void *cookie;
24996{
24997 funccall_T *fcp = (funccall_T *)cookie;
24998 ufunc_T *fp = fcp->func;
24999
25000 if (fp->uf_profiling && sourcing_lnum >= 1
25001 && sourcing_lnum <= fp->uf_lines.ga_len)
25002 {
25003 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025004 /* Skip continuation lines. */
25005 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25006 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025007 fp->uf_tml_execed = FALSE;
25008 profile_start(&fp->uf_tml_start);
25009 profile_zero(&fp->uf_tml_children);
25010 profile_get_wait(&fp->uf_tml_wait);
25011 }
25012}
25013
25014/*
25015 * Called when actually executing a function line.
25016 */
25017 void
25018func_line_exec(cookie)
25019 void *cookie;
25020{
25021 funccall_T *fcp = (funccall_T *)cookie;
25022 ufunc_T *fp = fcp->func;
25023
25024 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25025 fp->uf_tml_execed = TRUE;
25026}
25027
25028/*
25029 * Called when done with a function line.
25030 */
25031 void
25032func_line_end(cookie)
25033 void *cookie;
25034{
25035 funccall_T *fcp = (funccall_T *)cookie;
25036 ufunc_T *fp = fcp->func;
25037
25038 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25039 {
25040 if (fp->uf_tml_execed)
25041 {
25042 ++fp->uf_tml_count[fp->uf_tml_idx];
25043 profile_end(&fp->uf_tml_start);
25044 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025045 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025046 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25047 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025048 }
25049 fp->uf_tml_idx = -1;
25050 }
25051}
25052#endif
25053
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054/*
25055 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025056 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025057 */
25058 int
25059func_has_ended(cookie)
25060 void *cookie;
25061{
Bram Moolenaar33570922005-01-25 22:26:29 +000025062 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025063
25064 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25065 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025066 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025067 || fcp->returned);
25068}
25069
25070/*
25071 * return TRUE if cookie indicates a function which "abort"s on errors.
25072 */
25073 int
25074func_has_abort(cookie)
25075 void *cookie;
25076{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025077 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025078}
25079
25080#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25081typedef enum
25082{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025083 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25084 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25085 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025086} var_flavour_T;
25087
25088static var_flavour_T var_flavour __ARGS((char_u *varname));
25089
25090 static var_flavour_T
25091var_flavour(varname)
25092 char_u *varname;
25093{
25094 char_u *p = varname;
25095
25096 if (ASCII_ISUPPER(*p))
25097 {
25098 while (*(++p))
25099 if (ASCII_ISLOWER(*p))
25100 return VAR_FLAVOUR_SESSION;
25101 return VAR_FLAVOUR_VIMINFO;
25102 }
25103 else
25104 return VAR_FLAVOUR_DEFAULT;
25105}
25106#endif
25107
25108#if defined(FEAT_VIMINFO) || defined(PROTO)
25109/*
25110 * Restore global vars that start with a capital from the viminfo file
25111 */
25112 int
25113read_viminfo_varlist(virp, writing)
25114 vir_T *virp;
25115 int writing;
25116{
25117 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025118 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025119 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025120 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025121
25122 if (!writing && (find_viminfo_parameter('!') != NULL))
25123 {
25124 tab = vim_strchr(virp->vir_line + 1, '\t');
25125 if (tab != NULL)
25126 {
25127 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025128 switch (*tab)
25129 {
25130 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025131#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025132 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025133#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025134 case 'D': type = VAR_DICT; break;
25135 case 'L': type = VAR_LIST; break;
25136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025137
25138 tab = vim_strchr(tab, '\t');
25139 if (tab != NULL)
25140 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025141 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025142 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025143 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025144 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025145#ifdef FEAT_FLOAT
25146 else if (type == VAR_FLOAT)
25147 (void)string2float(tab + 1, &tv.vval.v_float);
25148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025149 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025150 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025151 if (type == VAR_DICT || type == VAR_LIST)
25152 {
25153 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25154
25155 if (etv == NULL)
25156 /* Failed to parse back the dict or list, use it as a
25157 * string. */
25158 tv.v_type = VAR_STRING;
25159 else
25160 {
25161 vim_free(tv.vval.v_string);
25162 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025163 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025164 }
25165 }
25166
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025167 /* when in a function use global variables */
25168 save_funccal = current_funccal;
25169 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025170 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025171 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025172
25173 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025174 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025175 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25176 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025177 }
25178 }
25179 }
25180
25181 return viminfo_readline(virp);
25182}
25183
25184/*
25185 * Write global vars that start with a capital to the viminfo file
25186 */
25187 void
25188write_viminfo_varlist(fp)
25189 FILE *fp;
25190{
Bram Moolenaar33570922005-01-25 22:26:29 +000025191 hashitem_T *hi;
25192 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025193 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025194 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025195 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025196 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025197 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025198
25199 if (find_viminfo_parameter('!') == NULL)
25200 return;
25201
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025202 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025203
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025204 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025205 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025207 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025208 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025209 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025210 this_var = HI2DI(hi);
25211 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025212 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025213 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025214 {
25215 case VAR_STRING: s = "STR"; break;
25216 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025217#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025218 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025219#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025220 case VAR_DICT: s = "DIC"; break;
25221 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000025222 default: continue;
25223 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025224 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025225 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025226 if (p != NULL)
25227 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025228 vim_free(tofree);
25229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025230 }
25231 }
25232}
25233#endif
25234
25235#if defined(FEAT_SESSION) || defined(PROTO)
25236 int
25237store_session_globals(fd)
25238 FILE *fd;
25239{
Bram Moolenaar33570922005-01-25 22:26:29 +000025240 hashitem_T *hi;
25241 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025242 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025243 char_u *p, *t;
25244
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025245 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025246 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025247 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025248 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025250 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025251 this_var = HI2DI(hi);
25252 if ((this_var->di_tv.v_type == VAR_NUMBER
25253 || this_var->di_tv.v_type == VAR_STRING)
25254 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025255 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025256 /* Escape special characters with a backslash. Turn a LF and
25257 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025258 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025259 (char_u *)"\\\"\n\r");
25260 if (p == NULL) /* out of memory */
25261 break;
25262 for (t = p; *t != NUL; ++t)
25263 if (*t == '\n')
25264 *t = 'n';
25265 else if (*t == '\r')
25266 *t = 'r';
25267 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025268 this_var->di_key,
25269 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25270 : ' ',
25271 p,
25272 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25273 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025274 || put_eol(fd) == FAIL)
25275 {
25276 vim_free(p);
25277 return FAIL;
25278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279 vim_free(p);
25280 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025281#ifdef FEAT_FLOAT
25282 else if (this_var->di_tv.v_type == VAR_FLOAT
25283 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25284 {
25285 float_T f = this_var->di_tv.vval.v_float;
25286 int sign = ' ';
25287
25288 if (f < 0)
25289 {
25290 f = -f;
25291 sign = '-';
25292 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025293 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025294 this_var->di_key, sign, f) < 0)
25295 || put_eol(fd) == FAIL)
25296 return FAIL;
25297 }
25298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025299 }
25300 }
25301 return OK;
25302}
25303#endif
25304
Bram Moolenaar661b1822005-07-28 22:36:45 +000025305/*
25306 * Display script name where an item was last set.
25307 * Should only be invoked when 'verbose' is non-zero.
25308 */
25309 void
25310last_set_msg(scriptID)
25311 scid_T scriptID;
25312{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025313 char_u *p;
25314
Bram Moolenaar661b1822005-07-28 22:36:45 +000025315 if (scriptID != 0)
25316 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025317 p = home_replace_save(NULL, get_scriptname(scriptID));
25318 if (p != NULL)
25319 {
25320 verbose_enter();
25321 MSG_PUTS(_("\n\tLast set from "));
25322 MSG_PUTS(p);
25323 vim_free(p);
25324 verbose_leave();
25325 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025326 }
25327}
25328
Bram Moolenaard812df62008-11-09 12:46:09 +000025329/*
25330 * List v:oldfiles in a nice way.
25331 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025332 void
25333ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025334 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025335{
25336 list_T *l = vimvars[VV_OLDFILES].vv_list;
25337 listitem_T *li;
25338 int nr = 0;
25339
25340 if (l == NULL)
25341 msg((char_u *)_("No old files"));
25342 else
25343 {
25344 msg_start();
25345 msg_scroll = TRUE;
25346 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25347 {
25348 msg_outnum((long)++nr);
25349 MSG_PUTS(": ");
25350 msg_outtrans(get_tv_string(&li->li_tv));
25351 msg_putchar('\n');
25352 out_flush(); /* output one line at a time */
25353 ui_breakcheck();
25354 }
25355 /* Assume "got_int" was set to truncate the listing. */
25356 got_int = FALSE;
25357
25358#ifdef FEAT_BROWSE_CMD
25359 if (cmdmod.browse)
25360 {
25361 quit_more = FALSE;
25362 nr = prompt_for_number(FALSE);
25363 msg_starthere();
25364 if (nr > 0)
25365 {
25366 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25367 (long)nr);
25368
25369 if (p != NULL)
25370 {
25371 p = expand_env_save(p);
25372 eap->arg = p;
25373 eap->cmdidx = CMD_edit;
25374 cmdmod.browse = FALSE;
25375 do_exedit(eap, NULL);
25376 vim_free(p);
25377 }
25378 }
25379 }
25380#endif
25381 }
25382}
25383
Bram Moolenaar53744302015-07-17 17:38:22 +020025384/* reset v:option_new, v:option_old and v:option_type */
25385 void
25386reset_v_option_vars()
25387{
25388 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25389 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25390 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25391}
25392
25393
Bram Moolenaar071d4272004-06-13 20:20:40 +000025394#endif /* FEAT_EVAL */
25395
Bram Moolenaar071d4272004-06-13 20:20:40 +000025396
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025397#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398
25399#ifdef WIN3264
25400/*
25401 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25402 */
25403static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25404static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25405static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25406
25407/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025408 * Get the short path (8.3) for the filename in "fnamep".
25409 * Only works for a valid file name.
25410 * When the path gets longer "fnamep" is changed and the allocated buffer
25411 * is put in "bufp".
25412 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25413 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025414 */
25415 static int
25416get_short_pathname(fnamep, bufp, fnamelen)
25417 char_u **fnamep;
25418 char_u **bufp;
25419 int *fnamelen;
25420{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025421 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025422 char_u *newbuf;
25423
25424 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025425 l = GetShortPathName(*fnamep, *fnamep, len);
25426 if (l > len - 1)
25427 {
25428 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025429 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025430 newbuf = vim_strnsave(*fnamep, l);
25431 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025432 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025433
25434 vim_free(*bufp);
25435 *fnamep = *bufp = newbuf;
25436
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025437 /* Really should always succeed, as the buffer is big enough. */
25438 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025439 }
25440
25441 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025442 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025443}
25444
25445/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025446 * Get the short path (8.3) for the filename in "fname". The converted
25447 * path is returned in "bufp".
25448 *
25449 * Some of the directories specified in "fname" may not exist. This function
25450 * will shorten the existing directories at the beginning of the path and then
25451 * append the remaining non-existing path.
25452 *
25453 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025454 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025455 * bufp - Pointer to an allocated buffer for the filename.
25456 * fnamelen - Length of the filename pointed to by fname
25457 *
25458 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025459 */
25460 static int
25461shortpath_for_invalid_fname(fname, bufp, fnamelen)
25462 char_u **fname;
25463 char_u **bufp;
25464 int *fnamelen;
25465{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025466 char_u *short_fname, *save_fname, *pbuf_unused;
25467 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025468 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025469 int old_len, len;
25470 int new_len, sfx_len;
25471 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025472
25473 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025474 old_len = *fnamelen;
25475 save_fname = vim_strnsave(*fname, old_len);
25476 pbuf_unused = NULL;
25477 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025478
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025479 endp = save_fname + old_len - 1; /* Find the end of the copy */
25480 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025482 /*
25483 * Try shortening the supplied path till it succeeds by removing one
25484 * directory at a time from the tail of the path.
25485 */
25486 len = 0;
25487 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025488 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025489 /* go back one path-separator */
25490 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25491 --endp;
25492 if (endp <= save_fname)
25493 break; /* processed the complete path */
25494
25495 /*
25496 * Replace the path separator with a NUL and try to shorten the
25497 * resulting path.
25498 */
25499 ch = *endp;
25500 *endp = 0;
25501 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025502 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025503 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25504 {
25505 retval = FAIL;
25506 goto theend;
25507 }
25508 *endp = ch; /* preserve the string */
25509
25510 if (len > 0)
25511 break; /* successfully shortened the path */
25512
25513 /* failed to shorten the path. Skip the path separator */
25514 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025515 }
25516
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025517 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025518 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025519 /*
25520 * Succeeded in shortening the path. Now concatenate the shortened
25521 * path with the remaining path at the tail.
25522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025523
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025524 /* Compute the length of the new path. */
25525 sfx_len = (int)(save_endp - endp) + 1;
25526 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025527
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025528 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025529 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025530 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025531 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025532 /* There is not enough space in the currently allocated string,
25533 * copy it to a buffer big enough. */
25534 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025535 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025536 {
25537 retval = FAIL;
25538 goto theend;
25539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025540 }
25541 else
25542 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025543 /* Transfer short_fname to the main buffer (it's big enough),
25544 * unless get_short_pathname() did its work in-place. */
25545 *fname = *bufp = save_fname;
25546 if (short_fname != save_fname)
25547 vim_strncpy(save_fname, short_fname, len);
25548 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025549 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025550
25551 /* concat the not-shortened part of the path */
25552 vim_strncpy(*fname + len, endp, sfx_len);
25553 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025554 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025555
25556theend:
25557 vim_free(pbuf_unused);
25558 vim_free(save_fname);
25559
25560 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025561}
25562
25563/*
25564 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025565 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025566 */
25567 static int
25568shortpath_for_partial(fnamep, bufp, fnamelen)
25569 char_u **fnamep;
25570 char_u **bufp;
25571 int *fnamelen;
25572{
25573 int sepcount, len, tflen;
25574 char_u *p;
25575 char_u *pbuf, *tfname;
25576 int hasTilde;
25577
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025578 /* Count up the path separators from the RHS.. so we know which part
25579 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025580 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025581 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025582 if (vim_ispathsep(*p))
25583 ++sepcount;
25584
25585 /* Need full path first (use expand_env() to remove a "~/") */
25586 hasTilde = (**fnamep == '~');
25587 if (hasTilde)
25588 pbuf = tfname = expand_env_save(*fnamep);
25589 else
25590 pbuf = tfname = FullName_save(*fnamep, FALSE);
25591
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025592 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025593
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025594 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25595 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025596
25597 if (len == 0)
25598 {
25599 /* Don't have a valid filename, so shorten the rest of the
25600 * path if we can. This CAN give us invalid 8.3 filenames, but
25601 * there's not a lot of point in guessing what it might be.
25602 */
25603 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025604 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25605 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025606 }
25607
25608 /* Count the paths backward to find the beginning of the desired string. */
25609 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025610 {
25611#ifdef FEAT_MBYTE
25612 if (has_mbyte)
25613 p -= mb_head_off(tfname, p);
25614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025615 if (vim_ispathsep(*p))
25616 {
25617 if (sepcount == 0 || (hasTilde && sepcount == 1))
25618 break;
25619 else
25620 sepcount --;
25621 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025623 if (hasTilde)
25624 {
25625 --p;
25626 if (p >= tfname)
25627 *p = '~';
25628 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025629 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025630 }
25631 else
25632 ++p;
25633
25634 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25635 vim_free(*bufp);
25636 *fnamelen = (int)STRLEN(p);
25637 *bufp = pbuf;
25638 *fnamep = p;
25639
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025640 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641}
25642#endif /* WIN3264 */
25643
25644/*
25645 * Adjust a filename, according to a string of modifiers.
25646 * *fnamep must be NUL terminated when called. When returning, the length is
25647 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025648 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025649 * When there is an error, *fnamep is set to NULL.
25650 */
25651 int
25652modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25653 char_u *src; /* string with modifiers */
25654 int *usedlen; /* characters after src that are used */
25655 char_u **fnamep; /* file name so far */
25656 char_u **bufp; /* buffer for allocated file name or NULL */
25657 int *fnamelen; /* length of fnamep */
25658{
25659 int valid = 0;
25660 char_u *tail;
25661 char_u *s, *p, *pbuf;
25662 char_u dirname[MAXPATHL];
25663 int c;
25664 int has_fullname = 0;
25665#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025666 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025667 int has_shortname = 0;
25668#endif
25669
25670repeat:
25671 /* ":p" - full path/file_name */
25672 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25673 {
25674 has_fullname = 1;
25675
25676 valid |= VALID_PATH;
25677 *usedlen += 2;
25678
25679 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25680 if ((*fnamep)[0] == '~'
25681#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25682 && ((*fnamep)[1] == '/'
25683# ifdef BACKSLASH_IN_FILENAME
25684 || (*fnamep)[1] == '\\'
25685# endif
25686 || (*fnamep)[1] == NUL)
25687
25688#endif
25689 )
25690 {
25691 *fnamep = expand_env_save(*fnamep);
25692 vim_free(*bufp); /* free any allocated file name */
25693 *bufp = *fnamep;
25694 if (*fnamep == NULL)
25695 return -1;
25696 }
25697
25698 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025699 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025700 {
25701 if (vim_ispathsep(*p)
25702 && p[1] == '.'
25703 && (p[2] == NUL
25704 || vim_ispathsep(p[2])
25705 || (p[2] == '.'
25706 && (p[3] == NUL || vim_ispathsep(p[3])))))
25707 break;
25708 }
25709
25710 /* FullName_save() is slow, don't use it when not needed. */
25711 if (*p != NUL || !vim_isAbsName(*fnamep))
25712 {
25713 *fnamep = FullName_save(*fnamep, *p != NUL);
25714 vim_free(*bufp); /* free any allocated file name */
25715 *bufp = *fnamep;
25716 if (*fnamep == NULL)
25717 return -1;
25718 }
25719
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025720#ifdef WIN3264
25721# if _WIN32_WINNT >= 0x0500
25722 if (vim_strchr(*fnamep, '~') != NULL)
25723 {
25724 /* Expand 8.3 filename to full path. Needed to make sure the same
25725 * file does not have two different names.
25726 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25727 p = alloc(_MAX_PATH + 1);
25728 if (p != NULL)
25729 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025730 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025731 {
25732 vim_free(*bufp);
25733 *bufp = *fnamep = p;
25734 }
25735 else
25736 vim_free(p);
25737 }
25738 }
25739# endif
25740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025741 /* Append a path separator to a directory. */
25742 if (mch_isdir(*fnamep))
25743 {
25744 /* Make room for one or two extra characters. */
25745 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25746 vim_free(*bufp); /* free any allocated file name */
25747 *bufp = *fnamep;
25748 if (*fnamep == NULL)
25749 return -1;
25750 add_pathsep(*fnamep);
25751 }
25752 }
25753
25754 /* ":." - path relative to the current directory */
25755 /* ":~" - path relative to the home directory */
25756 /* ":8" - shortname path - postponed till after */
25757 while (src[*usedlen] == ':'
25758 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25759 {
25760 *usedlen += 2;
25761 if (c == '8')
25762 {
25763#ifdef WIN3264
25764 has_shortname = 1; /* Postpone this. */
25765#endif
25766 continue;
25767 }
25768 pbuf = NULL;
25769 /* Need full path first (use expand_env() to remove a "~/") */
25770 if (!has_fullname)
25771 {
25772 if (c == '.' && **fnamep == '~')
25773 p = pbuf = expand_env_save(*fnamep);
25774 else
25775 p = pbuf = FullName_save(*fnamep, FALSE);
25776 }
25777 else
25778 p = *fnamep;
25779
25780 has_fullname = 0;
25781
25782 if (p != NULL)
25783 {
25784 if (c == '.')
25785 {
25786 mch_dirname(dirname, MAXPATHL);
25787 s = shorten_fname(p, dirname);
25788 if (s != NULL)
25789 {
25790 *fnamep = s;
25791 if (pbuf != NULL)
25792 {
25793 vim_free(*bufp); /* free any allocated file name */
25794 *bufp = pbuf;
25795 pbuf = NULL;
25796 }
25797 }
25798 }
25799 else
25800 {
25801 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25802 /* Only replace it when it starts with '~' */
25803 if (*dirname == '~')
25804 {
25805 s = vim_strsave(dirname);
25806 if (s != NULL)
25807 {
25808 *fnamep = s;
25809 vim_free(*bufp);
25810 *bufp = s;
25811 }
25812 }
25813 }
25814 vim_free(pbuf);
25815 }
25816 }
25817
25818 tail = gettail(*fnamep);
25819 *fnamelen = (int)STRLEN(*fnamep);
25820
25821 /* ":h" - head, remove "/file_name", can be repeated */
25822 /* Don't remove the first "/" or "c:\" */
25823 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25824 {
25825 valid |= VALID_HEAD;
25826 *usedlen += 2;
25827 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025828 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025829 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830 *fnamelen = (int)(tail - *fnamep);
25831#ifdef VMS
25832 if (*fnamelen > 0)
25833 *fnamelen += 1; /* the path separator is part of the path */
25834#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025835 if (*fnamelen == 0)
25836 {
25837 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25838 p = vim_strsave((char_u *)".");
25839 if (p == NULL)
25840 return -1;
25841 vim_free(*bufp);
25842 *bufp = *fnamep = tail = p;
25843 *fnamelen = 1;
25844 }
25845 else
25846 {
25847 while (tail > s && !after_pathsep(s, tail))
25848 mb_ptr_back(*fnamep, tail);
25849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025850 }
25851
25852 /* ":8" - shortname */
25853 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25854 {
25855 *usedlen += 2;
25856#ifdef WIN3264
25857 has_shortname = 1;
25858#endif
25859 }
25860
25861#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025862 /*
25863 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864 */
25865 if (has_shortname)
25866 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025867 /* Copy the string if it is shortened by :h and when it wasn't copied
25868 * yet, because we are going to change it in place. Avoids changing
25869 * the buffer name for "%:8". */
25870 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025871 {
25872 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025873 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025874 return -1;
25875 vim_free(*bufp);
25876 *bufp = *fnamep = p;
25877 }
25878
25879 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025880 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025881 if (!has_fullname && !vim_isAbsName(*fnamep))
25882 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025883 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025884 return -1;
25885 }
25886 else
25887 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025888 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025889
Bram Moolenaardc935552011-08-17 15:23:23 +020025890 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025891 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025892 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025893 return -1;
25894
25895 if (l == 0)
25896 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025897 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025898 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025899 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025900 return -1;
25901 }
25902 *fnamelen = l;
25903 }
25904 }
25905#endif /* WIN3264 */
25906
25907 /* ":t" - tail, just the basename */
25908 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25909 {
25910 *usedlen += 2;
25911 *fnamelen -= (int)(tail - *fnamep);
25912 *fnamep = tail;
25913 }
25914
25915 /* ":e" - extension, can be repeated */
25916 /* ":r" - root, without extension, can be repeated */
25917 while (src[*usedlen] == ':'
25918 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25919 {
25920 /* find a '.' in the tail:
25921 * - for second :e: before the current fname
25922 * - otherwise: The last '.'
25923 */
25924 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25925 s = *fnamep - 2;
25926 else
25927 s = *fnamep + *fnamelen - 1;
25928 for ( ; s > tail; --s)
25929 if (s[0] == '.')
25930 break;
25931 if (src[*usedlen + 1] == 'e') /* :e */
25932 {
25933 if (s > tail)
25934 {
25935 *fnamelen += (int)(*fnamep - (s + 1));
25936 *fnamep = s + 1;
25937#ifdef VMS
25938 /* cut version from the extension */
25939 s = *fnamep + *fnamelen - 1;
25940 for ( ; s > *fnamep; --s)
25941 if (s[0] == ';')
25942 break;
25943 if (s > *fnamep)
25944 *fnamelen = s - *fnamep;
25945#endif
25946 }
25947 else if (*fnamep <= tail)
25948 *fnamelen = 0;
25949 }
25950 else /* :r */
25951 {
25952 if (s > tail) /* remove one extension */
25953 *fnamelen = (int)(s - *fnamep);
25954 }
25955 *usedlen += 2;
25956 }
25957
25958 /* ":s?pat?foo?" - substitute */
25959 /* ":gs?pat?foo?" - global substitute */
25960 if (src[*usedlen] == ':'
25961 && (src[*usedlen + 1] == 's'
25962 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25963 {
25964 char_u *str;
25965 char_u *pat;
25966 char_u *sub;
25967 int sep;
25968 char_u *flags;
25969 int didit = FALSE;
25970
25971 flags = (char_u *)"";
25972 s = src + *usedlen + 2;
25973 if (src[*usedlen + 1] == 'g')
25974 {
25975 flags = (char_u *)"g";
25976 ++s;
25977 }
25978
25979 sep = *s++;
25980 if (sep)
25981 {
25982 /* find end of pattern */
25983 p = vim_strchr(s, sep);
25984 if (p != NULL)
25985 {
25986 pat = vim_strnsave(s, (int)(p - s));
25987 if (pat != NULL)
25988 {
25989 s = p + 1;
25990 /* find end of substitution */
25991 p = vim_strchr(s, sep);
25992 if (p != NULL)
25993 {
25994 sub = vim_strnsave(s, (int)(p - s));
25995 str = vim_strnsave(*fnamep, *fnamelen);
25996 if (sub != NULL && str != NULL)
25997 {
25998 *usedlen = (int)(p + 1 - src);
25999 s = do_string_sub(str, pat, sub, flags);
26000 if (s != NULL)
26001 {
26002 *fnamep = s;
26003 *fnamelen = (int)STRLEN(s);
26004 vim_free(*bufp);
26005 *bufp = s;
26006 didit = TRUE;
26007 }
26008 }
26009 vim_free(sub);
26010 vim_free(str);
26011 }
26012 vim_free(pat);
26013 }
26014 }
26015 /* after using ":s", repeat all the modifiers */
26016 if (didit)
26017 goto repeat;
26018 }
26019 }
26020
Bram Moolenaar26df0922014-02-23 23:39:13 +010026021 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26022 {
26023 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26024 if (p == NULL)
26025 return -1;
26026 vim_free(*bufp);
26027 *bufp = *fnamep = p;
26028 *fnamelen = (int)STRLEN(p);
26029 *usedlen += 2;
26030 }
26031
Bram Moolenaar071d4272004-06-13 20:20:40 +000026032 return valid;
26033}
26034
26035/*
26036 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26037 * "flags" can be "g" to do a global substitute.
26038 * Returns an allocated string, NULL for error.
26039 */
26040 char_u *
26041do_string_sub(str, pat, sub, flags)
26042 char_u *str;
26043 char_u *pat;
26044 char_u *sub;
26045 char_u *flags;
26046{
26047 int sublen;
26048 regmatch_T regmatch;
26049 int i;
26050 int do_all;
26051 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026052 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026053 garray_T ga;
26054 char_u *ret;
26055 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026056 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026057
26058 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26059 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026060 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026061
26062 ga_init2(&ga, 1, 200);
26063
26064 do_all = (flags[0] == 'g');
26065
26066 regmatch.rm_ic = p_ic;
26067 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26068 if (regmatch.regprog != NULL)
26069 {
26070 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026071 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026072 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26073 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026074 /* Skip empty match except for first match. */
26075 if (regmatch.startp[0] == regmatch.endp[0])
26076 {
26077 if (zero_width == regmatch.startp[0])
26078 {
26079 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026080 i = MB_PTR2LEN(tail);
26081 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26082 (size_t)i);
26083 ga.ga_len += i;
26084 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026085 continue;
26086 }
26087 zero_width = regmatch.startp[0];
26088 }
26089
Bram Moolenaar071d4272004-06-13 20:20:40 +000026090 /*
26091 * Get some space for a temporary buffer to do the substitution
26092 * into. It will contain:
26093 * - The text up to where the match is.
26094 * - The substituted text.
26095 * - The text after the match.
26096 */
26097 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026098 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026099 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26100 {
26101 ga_clear(&ga);
26102 break;
26103 }
26104
26105 /* copy the text up to where the match is */
26106 i = (int)(regmatch.startp[0] - tail);
26107 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26108 /* add the substituted text */
26109 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26110 + ga.ga_len + i, TRUE, TRUE, FALSE);
26111 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026112 tail = regmatch.endp[0];
26113 if (*tail == NUL)
26114 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026115 if (!do_all)
26116 break;
26117 }
26118
26119 if (ga.ga_data != NULL)
26120 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26121
Bram Moolenaar473de612013-06-08 18:19:48 +020026122 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026123 }
26124
26125 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26126 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026127 if (p_cpo == empty_option)
26128 p_cpo = save_cpo;
26129 else
26130 /* Darn, evaluating {sub} expression changed the value. */
26131 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026132
26133 return ret;
26134}
26135
26136#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */