blob: fb2cbe79e50d4422b76f42742d186e4dcb0a5a0e [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 Moolenaar8c8de832008-06-24 22:58:06 +0000660#ifdef FEAT_FLOAT
661static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
662#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000664static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000665static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200666#ifdef FEAT_PYTHON3
667static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
668#endif
669#ifdef FEAT_PYTHON
670static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
671#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000673static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000674static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000686#ifdef FEAT_FLOAT
687static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
688#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200689static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100691static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000694static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000696static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
699static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200701static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
703static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000704static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000705static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000706static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000707static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200709static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000710static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000711static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100712#ifdef FEAT_CRYPT
713static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
714#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000715static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200716static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#ifdef FEAT_FLOAT
719static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200720static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000721#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000722static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000723static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000724static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000727#ifdef FEAT_FLOAT
728static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
730#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000731static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200732static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000733#ifdef HAVE_STRFTIME
734static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
736static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200742static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200743static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000749static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200750static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200752static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000753static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000754static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000755static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000756static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000757static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000759static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200760#ifdef FEAT_FLOAT
761static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
763#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000764static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000767#ifdef FEAT_FLOAT
768static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
769#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200771static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200772static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100773static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
775static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
776static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100777static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
779static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
780static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
781static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
782static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
783static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000784static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
785static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000787static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaared767a22016-01-03 22:49:16 +0100788static void f_wordcount __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100789static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790
Bram Moolenaar493c1782014-05-28 14:34:46 +0200791static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000792static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000793static int get_env_len __ARGS((char_u **arg));
794static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000795static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000796static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
797#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
798#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
799 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000800static 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 +0000801static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000802static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200803static 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 +0000804static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static typval_T *alloc_tv __ARGS((void));
806static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000807static void init_tv __ARGS((typval_T *varp));
808static long get_tv_number __ARGS((typval_T *varp));
809static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000810static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000811static char_u *get_tv_string __ARGS((typval_T *varp));
812static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000813static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100814static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
815static 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 +0000816static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
Bram Moolenaarf1f60f82016-01-16 15:40:53 +0100817static funccall_T *get_funccal __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000818static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
819static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000820static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
821static 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 +0000822static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200823static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
824static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200825static int var_check_func_name __ARGS((char_u *name, int new_var));
826static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200827static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000828static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000829static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
830static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
831static int eval_fname_script __ARGS((char_u *p));
832static int eval_fname_sid __ARGS((char_u *p));
833static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834static ufunc_T *find_func __ARGS((char_u *name));
835static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200836static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000837#ifdef FEAT_PROFILE
838static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000839static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
840static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
841static int
842# ifdef __BORLANDC__
843 _RTLENTRYF
844# endif
845 prof_total_cmp __ARGS((const void *s1, const void *s2));
846static int
847# ifdef __BORLANDC__
848 _RTLENTRYF
849# endif
850 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200852static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000853static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000854static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000855static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000856static 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 +0000857static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
858static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000859static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000860static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
861static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000862static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000863static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000864static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200865static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200866static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000867
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200868
869#ifdef EBCDIC
870static int compare_func_name __ARGS((const void *s1, const void *s2));
871static void sortFunctions __ARGS(());
872#endif
873
Bram Moolenaar33570922005-01-25 22:26:29 +0000874/*
875 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000876 */
877 void
878eval_init()
879{
Bram Moolenaar33570922005-01-25 22:26:29 +0000880 int i;
881 struct vimvar *p;
882
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200883 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
884 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200885 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000886 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000887 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000888
889 for (i = 0; i < VV_LEN; ++i)
890 {
891 p = &vimvars[i];
892 STRCPY(p->vv_di.di_key, p->vv_name);
893 if (p->vv_flags & VV_RO)
894 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
895 else if (p->vv_flags & VV_RO_SBX)
896 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
897 else
898 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000899
900 /* add to v: scope dict, unless the value is not always available */
901 if (p->vv_type != VAR_UNKNOWN)
902 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000903 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000904 /* add to compat scope dict */
905 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000906 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000907 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100908 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200909 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100910 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200911 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200912
913#ifdef EBCDIC
914 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100915 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200916 */
917 sortFunctions();
918#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000919}
920
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000921#if defined(EXITFREE) || defined(PROTO)
922 void
923eval_clear()
924{
925 int i;
926 struct vimvar *p;
927
928 for (i = 0; i < VV_LEN; ++i)
929 {
930 p = &vimvars[i];
931 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000932 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000933 vim_free(p->vv_str);
934 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000935 }
936 else if (p->vv_di.di_tv.v_type == VAR_LIST)
937 {
938 list_unref(p->vv_list);
939 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000940 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000941 }
942 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000943 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000944 hash_clear(&compat_hashtab);
945
Bram Moolenaard9fba312005-06-26 22:34:35 +0000946 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100947# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200948 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100949# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000950
951 /* global variables */
952 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000953
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000954 /* autoloaded script names */
955 ga_clear_strings(&ga_loaded);
956
Bram Moolenaarcca74132013-09-25 21:00:28 +0200957 /* Script-local variables. First clear all the variables and in a second
958 * loop free the scriptvar_T, because a variable in one script might hold
959 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200960 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200961 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200962 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200963 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200964 ga_clear(&ga_scripts);
965
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000966 /* unreferenced lists and dicts */
967 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000968
969 /* functions */
970 free_all_functions();
971 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000972}
973#endif
974
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 * Return the name of the executed function.
977 */
978 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100979func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000981 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982}
983
984/*
985 * Return the address holding the next breakpoint line for a funccall cookie.
986 */
987 linenr_T *
988func_breakpoint(cookie)
989 void *cookie;
990{
Bram Moolenaar33570922005-01-25 22:26:29 +0000991 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992}
993
994/*
995 * Return the address holding the debug tick for a funccall cookie.
996 */
997 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100998func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999{
Bram Moolenaar33570922005-01-25 22:26:29 +00001000 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001}
1002
1003/*
1004 * Return the nesting level for a funccall cookie.
1005 */
1006 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001007func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
Bram Moolenaar33570922005-01-25 22:26:29 +00001009 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010}
1011
1012/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001013funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001015/* pointer to list of previously used funccal, still around because some
1016 * item in it is still being used. */
1017funccall_T *previous_funccal = NULL;
1018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019/*
1020 * Return TRUE when a function was ended by a ":return" command.
1021 */
1022 int
1023current_func_returned()
1024{
1025 return current_funccal->returned;
1026}
1027
1028
1029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 * Set an internal variable to a string value. Creates the variable if it does
1031 * not already exist.
1032 */
1033 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001034set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001036 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001037 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
1039 val = vim_strsave(value);
1040 if (val != NULL)
1041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001042 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001043 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001045 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001046 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 }
1048 }
1049}
1050
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001051static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001052static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001053static char_u *redir_endp = NULL;
1054static char_u *redir_varname = NULL;
1055
1056/*
1057 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001058 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 * Returns OK if successfully completed the setup. FAIL otherwise.
1060 */
1061 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001062var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063{
1064 int save_emsg;
1065 int err;
1066 typval_T tv;
1067
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001068 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001069 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 {
1071 EMSG(_(e_invarg));
1072 return FAIL;
1073 }
1074
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001075 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 redir_varname = vim_strsave(name);
1077 if (redir_varname == NULL)
1078 return FAIL;
1079
1080 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1081 if (redir_lval == NULL)
1082 {
1083 var_redir_stop();
1084 return FAIL;
1085 }
1086
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001087 /* The output is stored in growarray "redir_ga" until redirection ends. */
1088 ga_init2(&redir_ga, (int)sizeof(char), 500);
1089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001091 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001092 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1094 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001095 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 if (redir_endp != NULL && *redir_endp != NUL)
1097 /* Trailing characters are present after the variable name */
1098 EMSG(_(e_trailing));
1099 else
1100 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001101 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102 var_redir_stop();
1103 return FAIL;
1104 }
1105
1106 /* check if we can write to the variable: set it to or append an empty
1107 * string */
1108 save_emsg = did_emsg;
1109 did_emsg = FALSE;
1110 tv.v_type = VAR_STRING;
1111 tv.vval.v_string = (char_u *)"";
1112 if (append)
1113 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1114 else
1115 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001116 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001118 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 if (err)
1120 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001121 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122 var_redir_stop();
1123 return FAIL;
1124 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125
1126 return OK;
1127}
1128
1129/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 * Append "value[value_len]" to the variable set by var_redir_start().
1131 * The actual appending is postponed until redirection ends, because the value
1132 * appended may in fact be the string we write to, changing it may cause freed
1133 * memory to be used:
1134 * :redir => foo
1135 * :let foo
1136 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 */
1138 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001139var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001141 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142
1143 if (redir_lval == NULL)
1144 return;
1145
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001146 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001147 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001148 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001149 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001151 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001152 {
1153 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001154 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155 }
1156 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158}
1159
1160/*
1161 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001162 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 */
1164 void
1165var_redir_stop()
1166{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167 typval_T tv;
1168
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 if (redir_lval != NULL)
1170 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001171 /* If there was no error: assign the text to the variable. */
1172 if (redir_endp != NULL)
1173 {
1174 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1175 tv.v_type = VAR_STRING;
1176 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001177 /* Call get_lval() again, if it's inside a Dict or List it may
1178 * have changed. */
1179 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001180 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001181 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1182 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1183 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001184 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001185
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001186 /* free the collected output */
1187 vim_free(redir_ga.ga_data);
1188 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001190 vim_free(redir_lval);
1191 redir_lval = NULL;
1192 }
1193 vim_free(redir_varname);
1194 redir_varname = NULL;
1195}
1196
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197# if defined(FEAT_MBYTE) || defined(PROTO)
1198 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001199eval_charconvert(
1200 char_u *enc_from,
1201 char_u *enc_to,
1202 char_u *fname_from,
1203 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
1205 int err = FALSE;
1206
1207 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1208 set_vim_var_string(VV_CC_TO, enc_to, -1);
1209 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1210 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1211 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1212 err = TRUE;
1213 set_vim_var_string(VV_CC_FROM, NULL, -1);
1214 set_vim_var_string(VV_CC_TO, NULL, -1);
1215 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1216 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1217
1218 if (err)
1219 return FAIL;
1220 return OK;
1221}
1222# endif
1223
1224# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1225 int
1226eval_printexpr(fname, args)
1227 char_u *fname;
1228 char_u *args;
1229{
1230 int err = FALSE;
1231
1232 set_vim_var_string(VV_FNAME_IN, fname, -1);
1233 set_vim_var_string(VV_CMDARG, args, -1);
1234 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1235 err = TRUE;
1236 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1237 set_vim_var_string(VV_CMDARG, NULL, -1);
1238
1239 if (err)
1240 {
1241 mch_remove(fname);
1242 return FAIL;
1243 }
1244 return OK;
1245}
1246# endif
1247
1248# if defined(FEAT_DIFF) || defined(PROTO)
1249 void
1250eval_diff(origfile, newfile, outfile)
1251 char_u *origfile;
1252 char_u *newfile;
1253 char_u *outfile;
1254{
1255 int err = FALSE;
1256
1257 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1258 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1259 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1260 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1261 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1262 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1263 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1264}
1265
1266 void
1267eval_patch(origfile, difffile, outfile)
1268 char_u *origfile;
1269 char_u *difffile;
1270 char_u *outfile;
1271{
1272 int err;
1273
1274 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1275 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1276 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1277 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1278 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1279 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1280 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1281}
1282# endif
1283
1284/*
1285 * Top level evaluation function, returning a boolean.
1286 * Sets "error" to TRUE if there was an error.
1287 * Return TRUE or FALSE.
1288 */
1289 int
1290eval_to_bool(arg, error, nextcmd, skip)
1291 char_u *arg;
1292 int *error;
1293 char_u **nextcmd;
1294 int skip; /* only parse, don't execute */
1295{
Bram Moolenaar33570922005-01-25 22:26:29 +00001296 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 int retval = FALSE;
1298
1299 if (skip)
1300 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001301 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 else
1304 {
1305 *error = FALSE;
1306 if (!skip)
1307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001308 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 }
1312 if (skip)
1313 --emsg_skip;
1314
1315 return retval;
1316}
1317
1318/*
1319 * Top level evaluation function, returning a string. If "skip" is TRUE,
1320 * only parsing to "nextcmd" is done, without reporting errors. Return
1321 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1322 */
1323 char_u *
1324eval_to_string_skip(arg, nextcmd, skip)
1325 char_u *arg;
1326 char_u **nextcmd;
1327 int skip; /* only parse, don't execute */
1328{
Bram Moolenaar33570922005-01-25 22:26:29 +00001329 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 char_u *retval;
1331
1332 if (skip)
1333 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 retval = NULL;
1336 else
1337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001338 retval = vim_strsave(get_tv_string(&tv));
1339 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 }
1341 if (skip)
1342 --emsg_skip;
1343
1344 return retval;
1345}
1346
1347/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001348 * Skip over an expression at "*pp".
1349 * Return FAIL for an error, OK otherwise.
1350 */
1351 int
1352skip_expr(pp)
1353 char_u **pp;
1354{
Bram Moolenaar33570922005-01-25 22:26:29 +00001355 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001356
1357 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001358 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001359}
1360
1361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001363 * When "convert" is TRUE convert a List into a sequence of lines and convert
1364 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 * Return pointer to allocated memory, or NULL for failure.
1366 */
1367 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001368eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 char_u *arg;
1370 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001371 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
Bram Moolenaar33570922005-01-25 22:26:29 +00001373 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001375 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001376#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 retval = NULL;
1382 else
1383 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001384 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001385 {
1386 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001387 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001388 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001389 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001390 if (tv.vval.v_list->lv_len > 0)
1391 ga_append(&ga, NL);
1392 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001393 ga_append(&ga, NUL);
1394 retval = (char_u *)ga.ga_data;
1395 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001396#ifdef FEAT_FLOAT
1397 else if (convert && tv.v_type == VAR_FLOAT)
1398 {
1399 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1400 retval = vim_strsave(numbuf);
1401 }
1402#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001403 else
1404 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001405 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 }
1407
1408 return retval;
1409}
1410
1411/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001412 * Call eval_to_string() without using current local variables and using
1413 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 */
1415 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001416eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 char_u *arg;
1418 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001419 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
1421 char_u *retval;
1422 void *save_funccalp;
1423
1424 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001425 if (use_sandbox)
1426 ++sandbox;
1427 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001428 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001429 if (use_sandbox)
1430 --sandbox;
1431 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 restore_funccal(save_funccalp);
1433 return retval;
1434}
1435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436/*
1437 * Top level evaluation function, returning a number.
1438 * Evaluates "expr" silently.
1439 * Returns -1 for an error.
1440 */
1441 int
1442eval_to_number(expr)
1443 char_u *expr;
1444{
Bram Moolenaar33570922005-01-25 22:26:29 +00001445 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001447 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
1449 ++emsg_off;
1450
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001451 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 retval = -1;
1453 else
1454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001455 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001456 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 }
1458 --emsg_off;
1459
1460 return retval;
1461}
1462
Bram Moolenaara40058a2005-07-11 22:42:07 +00001463/*
1464 * Prepare v: variable "idx" to be used.
1465 * Save the current typeval in "save_tv".
1466 * When not used yet add the variable to the v: hashtable.
1467 */
1468 static void
1469prepare_vimvar(idx, save_tv)
1470 int idx;
1471 typval_T *save_tv;
1472{
1473 *save_tv = vimvars[idx].vv_tv;
1474 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1475 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1476}
1477
1478/*
1479 * Restore v: variable "idx" to typeval "save_tv".
1480 * When no longer defined, remove the variable from the v: hashtable.
1481 */
1482 static void
1483restore_vimvar(idx, save_tv)
1484 int idx;
1485 typval_T *save_tv;
1486{
1487 hashitem_T *hi;
1488
Bram Moolenaara40058a2005-07-11 22:42:07 +00001489 vimvars[idx].vv_tv = *save_tv;
1490 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1491 {
1492 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1493 if (HASHITEM_EMPTY(hi))
1494 EMSG2(_(e_intern2), "restore_vimvar()");
1495 else
1496 hash_remove(&vimvarht, hi);
1497 }
1498}
1499
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001500#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001501/*
1502 * Evaluate an expression to a list with suggestions.
1503 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001504 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001505 */
1506 list_T *
1507eval_spell_expr(badword, expr)
1508 char_u *badword;
1509 char_u *expr;
1510{
1511 typval_T save_val;
1512 typval_T rettv;
1513 list_T *list = NULL;
1514 char_u *p = skipwhite(expr);
1515
1516 /* Set "v:val" to the bad word. */
1517 prepare_vimvar(VV_VAL, &save_val);
1518 vimvars[VV_VAL].vv_type = VAR_STRING;
1519 vimvars[VV_VAL].vv_str = badword;
1520 if (p_verbose == 0)
1521 ++emsg_off;
1522
1523 if (eval1(&p, &rettv, TRUE) == OK)
1524 {
1525 if (rettv.v_type != VAR_LIST)
1526 clear_tv(&rettv);
1527 else
1528 list = rettv.vval.v_list;
1529 }
1530
1531 if (p_verbose == 0)
1532 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001533 restore_vimvar(VV_VAL, &save_val);
1534
1535 return list;
1536}
1537
1538/*
1539 * "list" is supposed to contain two items: a word and a number. Return the
1540 * word in "pp" and the number as the return value.
1541 * Return -1 if anything isn't right.
1542 * Used to get the good word and score from the eval_spell_expr() result.
1543 */
1544 int
1545get_spellword(list, pp)
1546 list_T *list;
1547 char_u **pp;
1548{
1549 listitem_T *li;
1550
1551 li = list->lv_first;
1552 if (li == NULL)
1553 return -1;
1554 *pp = get_tv_string(&li->li_tv);
1555
1556 li = li->li_next;
1557 if (li == NULL)
1558 return -1;
1559 return get_tv_number(&li->li_tv);
1560}
1561#endif
1562
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001563/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001564 * Top level evaluation function.
1565 * Returns an allocated typval_T with the result.
1566 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001567 */
1568 typval_T *
1569eval_expr(arg, nextcmd)
1570 char_u *arg;
1571 char_u **nextcmd;
1572{
1573 typval_T *tv;
1574
1575 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001576 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001577 {
1578 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001579 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001580 }
1581
1582 return tv;
1583}
1584
1585
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001587 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001588 * Uses argv[argc] for the function arguments. Only Number and String
1589 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001590 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001592 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001593call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 char_u *func;
1595 int argc;
1596 char_u **argv;
1597 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001598 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600{
Bram Moolenaar33570922005-01-25 22:26:29 +00001601 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 long n;
1603 int len;
1604 int i;
1605 int doesrange;
1606 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001607 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001609 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
1613 for (i = 0; i < argc; i++)
1614 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001615 /* Pass a NULL or empty argument as an empty string */
1616 if (argv[i] == NULL || *argv[i] == NUL)
1617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001618 argvars[i].v_type = VAR_STRING;
1619 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001620 continue;
1621 }
1622
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001623 if (str_arg_only)
1624 len = 0;
1625 else
1626 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001627 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if (len != 0 && len == (int)STRLEN(argv[i]))
1629 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001630 argvars[i].v_type = VAR_NUMBER;
1631 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 }
1633 else
1634 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001635 argvars[i].v_type = VAR_STRING;
1636 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 }
1638 }
1639
1640 if (safe)
1641 {
1642 save_funccalp = save_funccal();
1643 ++sandbox;
1644 }
1645
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001646 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1647 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 if (safe)
1651 {
1652 --sandbox;
1653 restore_funccal(save_funccalp);
1654 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655 vim_free(argvars);
1656
1657 if (ret == FAIL)
1658 clear_tv(rettv);
1659
1660 return ret;
1661}
1662
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001663/*
1664 * Call vimL function "func" and return the result as a number.
1665 * Returns -1 when calling the function fails.
1666 * Uses argv[argc] for the function arguments.
1667 */
1668 long
1669call_func_retnr(func, argc, argv, safe)
1670 char_u *func;
1671 int argc;
1672 char_u **argv;
1673 int safe; /* use the sandbox */
1674{
1675 typval_T rettv;
1676 long retval;
1677
1678 /* All arguments are passed as strings, no conversion to number. */
1679 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1680 return -1;
1681
1682 retval = get_tv_number_chk(&rettv, NULL);
1683 clear_tv(&rettv);
1684 return retval;
1685}
1686
1687#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1688 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1689
Bram Moolenaar4f688582007-07-24 12:34:30 +00001690# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001691/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001692 * Call vimL function "func" and return the result as a string.
1693 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001694 * Uses argv[argc] for the function arguments.
1695 */
1696 void *
1697call_func_retstr(func, argc, argv, safe)
1698 char_u *func;
1699 int argc;
1700 char_u **argv;
1701 int safe; /* use the sandbox */
1702{
1703 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001704 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001706 /* All arguments are passed as strings, no conversion to number. */
1707 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708 return NULL;
1709
1710 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 return retval;
1713}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001714# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001715
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001716/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001717 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001719 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 */
1721 void *
1722call_func_retlist(func, argc, argv, safe)
1723 char_u *func;
1724 int argc;
1725 char_u **argv;
1726 int safe; /* use the sandbox */
1727{
1728 typval_T rettv;
1729
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001730 /* All arguments are passed as strings, no conversion to number. */
1731 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732 return NULL;
1733
1734 if (rettv.v_type != VAR_LIST)
1735 {
1736 clear_tv(&rettv);
1737 return NULL;
1738 }
1739
1740 return rettv.vval.v_list;
1741}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742#endif
1743
1744/*
1745 * Save the current function call pointer, and set it to NULL.
1746 * Used when executing autocommands and for ":source".
1747 */
1748 void *
1749save_funccal()
1750{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001751 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 current_funccal = NULL;
1754 return (void *)fc;
1755}
1756
1757 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001758restore_funccal(vfc)
1759 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001761 funccall_T *fc = (funccall_T *)vfc;
1762
1763 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764}
1765
Bram Moolenaar05159a02005-02-26 23:04:13 +00001766#if defined(FEAT_PROFILE) || defined(PROTO)
1767/*
1768 * Prepare profiling for entering a child or something else that is not
1769 * counted for the script/function itself.
1770 * Should always be called in pair with prof_child_exit().
1771 */
1772 void
1773prof_child_enter(tm)
1774 proftime_T *tm; /* place to store waittime */
1775{
1776 funccall_T *fc = current_funccal;
1777
1778 if (fc != NULL && fc->func->uf_profiling)
1779 profile_start(&fc->prof_child);
1780 script_prof_save(tm);
1781}
1782
1783/*
1784 * Take care of time spent in a child.
1785 * Should always be called after prof_child_enter().
1786 */
1787 void
1788prof_child_exit(tm)
1789 proftime_T *tm; /* where waittime was stored */
1790{
1791 funccall_T *fc = current_funccal;
1792
1793 if (fc != NULL && fc->func->uf_profiling)
1794 {
1795 profile_end(&fc->prof_child);
1796 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1797 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1798 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1799 }
1800 script_prof_restore(tm);
1801}
1802#endif
1803
1804
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805#ifdef FEAT_FOLDING
1806/*
1807 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1808 * it in "*cp". Doesn't give error messages.
1809 */
1810 int
1811eval_foldexpr(arg, cp)
1812 char_u *arg;
1813 int *cp;
1814{
Bram Moolenaar33570922005-01-25 22:26:29 +00001815 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 int retval;
1817 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001818 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1819 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
1821 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001822 if (use_sandbox)
1823 ++sandbox;
1824 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 retval = 0;
1828 else
1829 {
1830 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 if (tv.v_type == VAR_NUMBER)
1832 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001833 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 retval = 0;
1835 else
1836 {
1837 /* If the result is a string, check if there is a non-digit before
1838 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if (!VIM_ISDIGIT(*s) && *s != '-')
1841 *cp = *s++;
1842 retval = atol((char *)s);
1843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001847 if (use_sandbox)
1848 --sandbox;
1849 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851 return retval;
1852}
1853#endif
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 * ":let" list all variable values
1857 * ":let var1 var2" list variable values
1858 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001859 * ":let var += expr" assignment command.
1860 * ":let var -= expr" assignment command.
1861 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 */
1864 void
1865ex_let(eap)
1866 exarg_T *eap;
1867{
1868 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001870 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 int var_count = 0;
1873 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001874 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001875 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001876 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
Bram Moolenaardb552d602006-03-23 22:59:57 +00001878 argend = skip_var_list(arg, &var_count, &semicolon);
1879 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001881 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1882 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001883 expr = skipwhite(argend);
1884 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1885 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001887 /*
1888 * ":let" without "=": list variables
1889 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 if (*arg == '[')
1891 EMSG(_(e_invarg));
1892 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001894 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001898 list_glob_vars(&first);
1899 list_buf_vars(&first);
1900 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001901#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001902 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001903#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001904 list_script_vars(&first);
1905 list_func_vars(&first);
1906 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 eap->nextcmd = check_nextcmd(arg);
1909 }
1910 else
1911 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 op[0] = '=';
1913 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001914 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001916 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1917 op[0] = *expr; /* +=, -= or .= */
1918 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001919 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001920 else
1921 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 if (eap->skip)
1924 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if (eap->skip)
1927 {
1928 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 --emsg_skip;
1931 }
1932 else if (i != FAIL)
1933 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 }
1938 }
1939}
1940
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941/*
1942 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1943 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 * When "nextchars" is not NULL it points to a string with characters that
1945 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1946 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 * Returns OK or FAIL;
1948 */
1949 static int
1950ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1951 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001952 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 int copy; /* copy values from "tv", don't move */
1954 int semicolon; /* from skip_var_list() */
1955 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957{
1958 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001959 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001961 listitem_T *item;
1962 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963
1964 if (*arg != '[')
1965 {
1966 /*
1967 * ":let var = expr" or ":for var in list"
1968 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 return FAIL;
1971 return OK;
1972 }
1973
1974 /*
1975 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1976 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001977 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 {
1979 EMSG(_(e_listreq));
1980 return FAIL;
1981 }
1982
1983 i = list_len(l);
1984 if (semicolon == 0 && var_count < i)
1985 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001986 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 return FAIL;
1988 }
1989 if (var_count - semicolon > i)
1990 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001991 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 return FAIL;
1993 }
1994
1995 item = l->lv_first;
1996 while (*arg != ']')
1997 {
1998 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001999 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002000 item = item->li_next;
2001 if (arg == NULL)
2002 return FAIL;
2003
2004 arg = skipwhite(arg);
2005 if (*arg == ';')
2006 {
2007 /* Put the rest of the list (may be empty) in the var after ';'.
2008 * Create a new list for this. */
2009 l = list_alloc();
2010 if (l == NULL)
2011 return FAIL;
2012 while (item != NULL)
2013 {
2014 list_append_tv(l, &item->li_tv);
2015 item = item->li_next;
2016 }
2017
2018 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002019 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 ltv.vval.v_list = l;
2021 l->lv_refcount = 1;
2022
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002023 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2024 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 clear_tv(&ltv);
2026 if (arg == NULL)
2027 return FAIL;
2028 break;
2029 }
2030 else if (*arg != ',' && *arg != ']')
2031 {
2032 EMSG2(_(e_intern2), "ex_let_vars()");
2033 return FAIL;
2034 }
2035 }
2036
2037 return OK;
2038}
2039
2040/*
2041 * Skip over assignable variable "var" or list of variables "[var, var]".
2042 * Used for ":let varvar = expr" and ":for varvar in expr".
2043 * For "[var, var]" increment "*var_count" for each variable.
2044 * for "[var, var; var]" set "semicolon".
2045 * Return NULL for an error.
2046 */
2047 static char_u *
2048skip_var_list(arg, var_count, semicolon)
2049 char_u *arg;
2050 int *var_count;
2051 int *semicolon;
2052{
2053 char_u *p, *s;
2054
2055 if (*arg == '[')
2056 {
2057 /* "[var, var]": find the matching ']'. */
2058 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002059 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002060 {
2061 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2062 s = skip_var_one(p);
2063 if (s == p)
2064 {
2065 EMSG2(_(e_invarg2), p);
2066 return NULL;
2067 }
2068 ++*var_count;
2069
2070 p = skipwhite(s);
2071 if (*p == ']')
2072 break;
2073 else if (*p == ';')
2074 {
2075 if (*semicolon == 1)
2076 {
2077 EMSG(_("Double ; in list of variables"));
2078 return NULL;
2079 }
2080 *semicolon = 1;
2081 }
2082 else if (*p != ',')
2083 {
2084 EMSG2(_(e_invarg2), p);
2085 return NULL;
2086 }
2087 }
2088 return p + 1;
2089 }
2090 else
2091 return skip_var_one(arg);
2092}
2093
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002094/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002095 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002096 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002097 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002098 static char_u *
2099skip_var_one(arg)
2100 char_u *arg;
2101{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002102 if (*arg == '@' && arg[1] != NUL)
2103 return arg + 2;
2104 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2105 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002106}
2107
Bram Moolenaara7043832005-01-21 11:56:39 +00002108/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002109 * List variables for hashtab "ht" with prefix "prefix".
2110 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002112 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002114 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002116 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002118{
Bram Moolenaar33570922005-01-25 22:26:29 +00002119 hashitem_T *hi;
2120 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 int todo;
2122
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002123 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002124 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2125 {
2126 if (!HASHITEM_EMPTY(hi))
2127 {
2128 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002129 di = HI2DI(hi);
2130 if (empty || di->di_tv.v_type != VAR_STRING
2131 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133 }
2134 }
2135}
2136
2137/*
2138 * List global variables.
2139 */
2140 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141list_glob_vars(first)
2142 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002143{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002145}
2146
2147/*
2148 * List buffer variables.
2149 */
2150 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151list_buf_vars(first)
2152 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002153{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002154 char_u numbuf[NUMBUFLEN];
2155
Bram Moolenaar429fa852013-04-15 12:27:36 +02002156 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158
2159 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2161 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002162}
2163
2164/*
2165 * List window variables.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_win_vars(first)
2169 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002170{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002171 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002173}
2174
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002175#ifdef FEAT_WINDOWS
2176/*
2177 * List tab page variables.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_tab_vars(first)
2181 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002182{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002183 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002185}
2186#endif
2187
Bram Moolenaara7043832005-01-21 11:56:39 +00002188/*
2189 * List Vim variables.
2190 */
2191 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192list_vim_vars(first)
2193 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196}
2197
2198/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002199 * List script-local variables, if there is a script.
2200 */
2201 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202list_script_vars(first)
2203 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204{
2205 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2207 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208}
2209
2210/*
2211 * List function variables, if there is a function.
2212 */
2213 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214list_func_vars(first)
2215 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216{
2217 if (current_funccal != NULL)
2218 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002219 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220}
2221
2222/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 * List variables in "arg".
2224 */
2225 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002226list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 exarg_T *eap;
2228 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002229 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230{
2231 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 char_u *name_start;
2235 char_u *arg_subsc;
2236 char_u *tofree;
2237 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238
2239 while (!ends_excmd(*arg) && !got_int)
2240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002243 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2245 {
2246 emsg_severe = TRUE;
2247 EMSG(_(e_trailing));
2248 break;
2249 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 /* get_name_len() takes care of expanding curly braces */
2254 name_start = name = arg;
2255 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2256 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 /* This is mainly to keep test 49 working: when expanding
2259 * curly braces fails overrule the exception error message. */
2260 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 emsg_severe = TRUE;
2263 EMSG2(_(e_invarg2), arg);
2264 break;
2265 }
2266 error = TRUE;
2267 }
2268 else
2269 {
2270 if (tofree != NULL)
2271 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002272 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 else
2275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 /* handle d.key, l[idx], f(expr) */
2277 arg_subsc = arg;
2278 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002281 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002286 case 'g': list_glob_vars(first); break;
2287 case 'b': list_buf_vars(first); break;
2288 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002289#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002290 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002291#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002292 case 'v': list_vim_vars(first); break;
2293 case 's': list_script_vars(first); break;
2294 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 default:
2296 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 }
2299 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300 {
2301 char_u numbuf[NUMBUFLEN];
2302 char_u *tf;
2303 int c;
2304 char_u *s;
2305
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002306 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 c = *arg;
2308 *arg = NUL;
2309 list_one_var_a((char_u *)"",
2310 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002311 tv.v_type,
2312 s == NULL ? (char_u *)"" : s,
2313 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314 *arg = c;
2315 vim_free(tf);
2316 }
2317 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002318 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 }
2320 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321
2322 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002324
2325 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 }
2327
2328 return arg;
2329}
2330
2331/*
2332 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2333 * Returns a pointer to the char just after the var name.
2334 * Returns NULL if there is an error.
2335 */
2336 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002339 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002341 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343{
2344 int c1;
2345 char_u *name;
2346 char_u *p;
2347 char_u *arg_end = NULL;
2348 int len;
2349 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002350 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351
2352 /*
2353 * ":let $VAR = expr": Set environment variable.
2354 */
2355 if (*arg == '$')
2356 {
2357 /* Find the end of the name. */
2358 ++arg;
2359 name = arg;
2360 len = get_env_len(&arg);
2361 if (len == 0)
2362 EMSG2(_(e_invarg2), name - 1);
2363 else
2364 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 if (op != NULL && (*op == '+' || *op == '-'))
2366 EMSG2(_(e_letwrong), op);
2367 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002368 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002370 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 {
2372 c1 = name[len];
2373 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002374 p = get_tv_string_chk(tv);
2375 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002376 {
2377 int mustfree = FALSE;
2378 char_u *s = vim_getenv(name, &mustfree);
2379
2380 if (s != NULL)
2381 {
2382 p = tofree = concat_str(s, p);
2383 if (mustfree)
2384 vim_free(s);
2385 }
2386 }
2387 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002389 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002390 if (STRICMP(name, "HOME") == 0)
2391 init_homedir();
2392 else if (didset_vim && STRICMP(name, "VIM") == 0)
2393 didset_vim = FALSE;
2394 else if (didset_vimruntime
2395 && STRICMP(name, "VIMRUNTIME") == 0)
2396 didset_vimruntime = FALSE;
2397 arg_end = arg;
2398 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002399 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002401 }
2402 }
2403 }
2404
2405 /*
2406 * ":let &option = expr": Set option value.
2407 * ":let &l:option = expr": Set local option value.
2408 * ":let &g:option = expr": Set global option value.
2409 */
2410 else if (*arg == '&')
2411 {
2412 /* Find the end of the name. */
2413 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002414 if (p == NULL || (endchars != NULL
2415 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 EMSG(_(e_letunexp));
2417 else
2418 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 long n;
2420 int opt_type;
2421 long numval;
2422 char_u *stringval = NULL;
2423 char_u *s;
2424
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 c1 = *p;
2426 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427
2428 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002429 s = get_tv_string_chk(tv); /* != NULL if number or string */
2430 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 {
2432 opt_type = get_option_value(arg, &numval,
2433 &stringval, opt_flags);
2434 if ((opt_type == 1 && *op == '.')
2435 || (opt_type == 0 && *op != '.'))
2436 EMSG2(_(e_letwrong), op);
2437 else
2438 {
2439 if (opt_type == 1) /* number */
2440 {
2441 if (*op == '+')
2442 n = numval + n;
2443 else
2444 n = numval - n;
2445 }
2446 else if (opt_type == 0 && stringval != NULL) /* string */
2447 {
2448 s = concat_str(stringval, s);
2449 vim_free(stringval);
2450 stringval = s;
2451 }
2452 }
2453 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002454 if (s != NULL)
2455 {
2456 set_option_value(arg, n, s, opt_flags);
2457 arg_end = p;
2458 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002459 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 }
2462 }
2463
2464 /*
2465 * ":let @r = expr": Set register contents.
2466 */
2467 else if (*arg == '@')
2468 {
2469 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 if (op != NULL && (*op == '+' || *op == '-'))
2471 EMSG2(_(e_letwrong), op);
2472 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002473 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 EMSG(_(e_letunexp));
2475 else
2476 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002477 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 char_u *s;
2479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002480 p = get_tv_string_chk(tv);
2481 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002483 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002484 if (s != NULL)
2485 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002486 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002487 vim_free(s);
2488 }
2489 }
2490 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002493 arg_end = arg + 1;
2494 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002495 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 }
2497 }
2498
2499 /*
2500 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002503 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002505 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002507 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2511 EMSG(_(e_letunexp));
2512 else
2513 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002514 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 arg_end = p;
2516 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 }
2520
2521 else
2522 EMSG2(_(e_invarg2), arg);
2523
2524 return arg_end;
2525}
2526
2527/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2529 */
2530 static int
2531check_changedtick(arg)
2532 char_u *arg;
2533{
2534 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2535 {
2536 EMSG2(_(e_readonlyvar), arg);
2537 return TRUE;
2538 }
2539 return FALSE;
2540}
2541
2542/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 * Get an lval: variable, Dict item or List item that can be assigned a value
2544 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2545 * "name.key", "name.key[expr]" etc.
2546 * Indexing only works if "name" is an existing List or Dictionary.
2547 * "name" points to the start of the name.
2548 * If "rettv" is not NULL it points to the value to be assigned.
2549 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2550 * wrong; must end in space or cmd separator.
2551 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002552 * flags:
2553 * GLV_QUIET: do not give error messages
2554 * GLV_NO_AUTOLOAD: do not use script autoloading
2555 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002557 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 */
2560 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002561get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002562 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002563 typval_T *rettv;
2564 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 int unlet;
2566 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002567 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002568 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 char_u *expr_start, *expr_end;
2572 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002573 dictitem_T *v;
2574 typval_T var1;
2575 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002581 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585
2586 if (skip)
2587 {
2588 /* When skipping just find the end of the name. */
2589 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002590 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 }
2592
2593 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002594 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 if (expr_start != NULL)
2596 {
2597 /* Don't expand the name when we already know there is an error. */
2598 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2599 && *p != '[' && *p != '.')
2600 {
2601 EMSG(_(e_trailing));
2602 return NULL;
2603 }
2604
2605 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2606 if (lp->ll_exp_name == NULL)
2607 {
2608 /* Report an invalid expression in braces, unless the
2609 * expression evaluation has been cancelled due to an
2610 * aborting error, an interrupt, or an exception. */
2611 if (!aborting() && !quiet)
2612 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002613 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 EMSG2(_(e_invarg2), name);
2615 return NULL;
2616 }
2617 }
2618 lp->ll_name = lp->ll_exp_name;
2619 }
2620 else
2621 lp->ll_name = name;
2622
2623 /* Without [idx] or .key we are done. */
2624 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2625 return p;
2626
2627 cc = *p;
2628 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002629 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (v == NULL && !quiet)
2631 EMSG2(_(e_undefvar), lp->ll_name);
2632 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 if (v == NULL)
2634 return NULL;
2635
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 /*
2637 * Loop until no more [idx] or .key is following.
2638 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002639 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2643 && !(lp->ll_tv->v_type == VAR_DICT
2644 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!quiet)
2647 EMSG(_("E689: Can only index a List or Dictionary"));
2648 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!quiet)
2653 EMSG(_("E708: [:] must come last"));
2654 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002656
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 len = -1;
2658 if (*p == '.')
2659 {
2660 key = p + 1;
2661 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2662 ;
2663 if (len == 0)
2664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (!quiet)
2666 EMSG(_(e_emptykey));
2667 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 }
2669 p = key + len;
2670 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002671 else
2672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002674 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (*p == ':')
2676 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002677 else
2678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 empty1 = FALSE;
2680 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002682 if (get_tv_string_chk(&var1) == NULL)
2683 {
2684 /* not a number or string */
2685 clear_tv(&var1);
2686 return NULL;
2687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
2689
2690 /* Optionally get the second index [ :expr]. */
2691 if (*p == ':')
2692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002696 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 if (!empty1)
2698 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (rettv != NULL && (rettv->v_type != VAR_LIST
2702 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (!quiet)
2705 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
2710 p = skipwhite(p + 1);
2711 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 else
2714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2717 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (!empty1)
2719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002722 if (get_tv_string_chk(&var2) == NULL)
2723 {
2724 /* not a number or string */
2725 if (!empty1)
2726 clear_tv(&var1);
2727 clear_tv(&var2);
2728 return NULL;
2729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002735
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (!quiet)
2739 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 if (!empty1)
2741 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 }
2746
2747 /* Skip to past ']'. */
2748 ++p;
2749 }
2750
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
2753 if (len == -1)
2754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002756 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 if (*key == NUL)
2758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (!quiet)
2760 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 }
2764 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002765 lp->ll_list = NULL;
2766 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002767 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002768
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002769 /* When assigning to a scope dictionary check that a function and
2770 * variable name is valid (only variable name unless it is l: or
2771 * g: dictionary). Disallow overwriting a builtin function. */
2772 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002773 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002774 int prevval;
2775 int wrong;
2776
2777 if (len != -1)
2778 {
2779 prevval = key[len];
2780 key[len] = NUL;
2781 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002782 else
2783 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002784 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2785 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002786 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002787 || !valid_varname(key);
2788 if (len != -1)
2789 key[len] = prevval;
2790 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002791 return NULL;
2792 }
2793
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002796 /* Can't add "v:" variable. */
2797 if (lp->ll_dict == &vimvardict)
2798 {
2799 EMSG2(_(e_illvar), name);
2800 return NULL;
2801 }
2802
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002803 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002807 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 if (len == -1)
2809 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 }
2812 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 if (len == -1)
2817 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 p = NULL;
2820 break;
2821 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002822 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002823 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002824 return NULL;
2825
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 if (len == -1)
2827 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 }
2830 else
2831 {
2832 /*
2833 * Get the number and item for the only or first index of the List.
2834 */
2835 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 else
2838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002839 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 clear_tv(&var1);
2841 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002842 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 lp->ll_list = lp->ll_tv->vval.v_list;
2844 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2845 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002847 if (lp->ll_n1 < 0)
2848 {
2849 lp->ll_n1 = 0;
2850 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2851 }
2852 }
2853 if (lp->ll_li == NULL)
2854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002857 if (!quiet)
2858 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 }
2861
2862 /*
2863 * May need to find the item or absolute index for the second
2864 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 * When no index given: "lp->ll_empty2" is TRUE.
2866 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002870 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 {
2877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 }
2883
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2885 if (lp->ll_n1 < 0)
2886 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2887 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002888 {
2889 if (!quiet)
2890 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002892 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002893 }
2894
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002897 }
2898
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 return p;
2900}
2901
2902/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 */
2905 static void
2906clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002907 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908{
2909 vim_free(lp->ll_exp_name);
2910 vim_free(lp->ll_newkey);
2911}
2912
2913/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002914 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002916 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 */
2918 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002919set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002920 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002922 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925{
2926 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002927 listitem_T *ri;
2928 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929
2930 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002931 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 cc = *endp;
2935 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936 if (op != NULL && *op != '=')
2937 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002938 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002939
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002940 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002941 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002942 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002943 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002945 if ((di == NULL
2946 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2947 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2948 FALSE)))
2949 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 set_var(lp->ll_name, &tv, FALSE);
2951 clear_tv(&tv);
2952 }
2953 }
2954 else
2955 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002957 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002959 else if (tv_check_lock(lp->ll_newkey == NULL
2960 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002961 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002962 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 else if (lp->ll_range)
2964 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002965 listitem_T *ll_li = lp->ll_li;
2966 int ll_n1 = lp->ll_n1;
2967
2968 /*
2969 * Check whether any of the list items is locked
2970 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002971 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002972 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002973 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002974 return;
2975 ri = ri->li_next;
2976 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2977 break;
2978 ll_li = ll_li->li_next;
2979 ++ll_n1;
2980 }
2981
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 /*
2983 * Assign the List values to the list items.
2984 */
2985 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002986 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002987 if (op != NULL && *op != '=')
2988 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2989 else
2990 {
2991 clear_tv(&lp->ll_li->li_tv);
2992 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2993 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 ri = ri->li_next;
2995 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2996 break;
2997 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003000 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003001 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 ri = NULL;
3003 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003004 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 lp->ll_li = lp->ll_li->li_next;
3007 ++lp->ll_n1;
3008 }
3009 if (ri != NULL)
3010 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003011 else if (lp->ll_empty2
3012 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 : lp->ll_n1 != lp->ll_n2)
3014 EMSG(_("E711: List value has not enough items"));
3015 }
3016 else
3017 {
3018 /*
3019 * Assign to a List or Dictionary item.
3020 */
3021 if (lp->ll_newkey != NULL)
3022 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003023 if (op != NULL && *op != '=')
3024 {
3025 EMSG2(_(e_letwrong), op);
3026 return;
3027 }
3028
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003030 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 if (di == NULL)
3032 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003033 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3034 {
3035 vim_free(di);
3036 return;
3037 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003038 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003039 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003040 else if (op != NULL && *op != '=')
3041 {
3042 tv_op(lp->ll_tv, rettv, op);
3043 return;
3044 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003045 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003047
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 /*
3049 * Assign the value to the variable or list item.
3050 */
3051 if (copy)
3052 copy_tv(rettv, lp->ll_tv);
3053 else
3054 {
3055 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003056 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003058 }
3059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003060}
3061
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003062/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003063 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3064 * Returns OK or FAIL.
3065 */
3066 static int
3067tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003068 typval_T *tv1;
3069 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 char_u *op;
3071{
3072 long n;
3073 char_u numbuf[NUMBUFLEN];
3074 char_u *s;
3075
3076 /* Can't do anything with a Funcref or a Dict on the right. */
3077 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3078 {
3079 switch (tv1->v_type)
3080 {
3081 case VAR_DICT:
3082 case VAR_FUNC:
3083 break;
3084
3085 case VAR_LIST:
3086 if (*op != '+' || tv2->v_type != VAR_LIST)
3087 break;
3088 /* List += List */
3089 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3090 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3091 return OK;
3092
3093 case VAR_NUMBER:
3094 case VAR_STRING:
3095 if (tv2->v_type == VAR_LIST)
3096 break;
3097 if (*op == '+' || *op == '-')
3098 {
3099 /* nr += nr or nr -= nr*/
3100 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003101#ifdef FEAT_FLOAT
3102 if (tv2->v_type == VAR_FLOAT)
3103 {
3104 float_T f = n;
3105
3106 if (*op == '+')
3107 f += tv2->vval.v_float;
3108 else
3109 f -= tv2->vval.v_float;
3110 clear_tv(tv1);
3111 tv1->v_type = VAR_FLOAT;
3112 tv1->vval.v_float = f;
3113 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003115#endif
3116 {
3117 if (*op == '+')
3118 n += get_tv_number(tv2);
3119 else
3120 n -= get_tv_number(tv2);
3121 clear_tv(tv1);
3122 tv1->v_type = VAR_NUMBER;
3123 tv1->vval.v_number = n;
3124 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003125 }
3126 else
3127 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003128 if (tv2->v_type == VAR_FLOAT)
3129 break;
3130
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003131 /* str .= str */
3132 s = get_tv_string(tv1);
3133 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3134 clear_tv(tv1);
3135 tv1->v_type = VAR_STRING;
3136 tv1->vval.v_string = s;
3137 }
3138 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003139
3140#ifdef FEAT_FLOAT
3141 case VAR_FLOAT:
3142 {
3143 float_T f;
3144
3145 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3146 && tv2->v_type != VAR_NUMBER
3147 && tv2->v_type != VAR_STRING))
3148 break;
3149 if (tv2->v_type == VAR_FLOAT)
3150 f = tv2->vval.v_float;
3151 else
3152 f = get_tv_number(tv2);
3153 if (*op == '+')
3154 tv1->vval.v_float += f;
3155 else
3156 tv1->vval.v_float -= f;
3157 }
3158 return OK;
3159#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003160 }
3161 }
3162
3163 EMSG2(_(e_letwrong), op);
3164 return FAIL;
3165}
3166
3167/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168 * Add a watcher to a list.
3169 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003170 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 list_T *l;
3173 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174{
3175 lw->lw_next = l->lv_watch;
3176 l->lv_watch = lw;
3177}
3178
3179/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003180 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181 * No warning when it isn't found...
3182 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003183 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003185 list_T *l;
3186 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187{
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189
3190 lwp = &l->lv_watch;
3191 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3192 {
3193 if (lw == lwrem)
3194 {
3195 *lwp = lw->lw_next;
3196 break;
3197 }
3198 lwp = &lw->lw_next;
3199 }
3200}
3201
3202/*
3203 * Just before removing an item from a list: advance watchers to the next
3204 * item.
3205 */
3206 static void
3207list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003208 list_T *l;
3209 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003210{
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212
3213 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3214 if (lw->lw_item == item)
3215 lw->lw_item = item->li_next;
3216}
3217
3218/*
3219 * Evaluate the expression used in a ":for var in expr" command.
3220 * "arg" points to "var".
3221 * Set "*errp" to TRUE for an error, FALSE otherwise;
3222 * Return a pointer that holds the info. Null when there is an error.
3223 */
3224 void *
3225eval_for_line(arg, errp, nextcmdp, skip)
3226 char_u *arg;
3227 int *errp;
3228 char_u **nextcmdp;
3229 int skip;
3230{
Bram Moolenaar33570922005-01-25 22:26:29 +00003231 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 typval_T tv;
3234 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
3236 *errp = TRUE; /* default: there is an error */
3237
Bram Moolenaar33570922005-01-25 22:26:29 +00003238 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239 if (fi == NULL)
3240 return NULL;
3241
3242 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3243 if (expr == NULL)
3244 return fi;
3245
3246 expr = skipwhite(expr);
3247 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3248 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003249 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250 return fi;
3251 }
3252
3253 if (skip)
3254 ++emsg_skip;
3255 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3256 {
3257 *errp = FALSE;
3258 if (!skip)
3259 {
3260 l = tv.vval.v_list;
3261 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003262 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003264 clear_tv(&tv);
3265 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 else
3267 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003268 /* No need to increment the refcount, it's already set for the
3269 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 fi->fi_list = l;
3271 list_add_watch(l, &fi->fi_lw);
3272 fi->fi_lw.lw_item = l->lv_first;
3273 }
3274 }
3275 }
3276 if (skip)
3277 --emsg_skip;
3278
3279 return fi;
3280}
3281
3282/*
3283 * Use the first item in a ":for" list. Advance to the next.
3284 * Assign the values to the variable (list). "arg" points to the first one.
3285 * Return TRUE when a valid item was found, FALSE when at end of list or
3286 * something wrong.
3287 */
3288 int
3289next_for_item(fi_void, arg)
3290 void *fi_void;
3291 char_u *arg;
3292{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003293 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296
3297 item = fi->fi_lw.lw_item;
3298 if (item == NULL)
3299 result = FALSE;
3300 else
3301 {
3302 fi->fi_lw.lw_item = item->li_next;
3303 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3304 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3305 }
3306 return result;
3307}
3308
3309/*
3310 * Free the structure used to store info used by ":for".
3311 */
3312 void
3313free_for_info(fi_void)
3314 void *fi_void;
3315{
Bram Moolenaar33570922005-01-25 22:26:29 +00003316 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003318 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003319 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003320 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003321 list_unref(fi->fi_list);
3322 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323 vim_free(fi);
3324}
3325
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3327
3328 void
3329set_context_for_expression(xp, arg, cmdidx)
3330 expand_T *xp;
3331 char_u *arg;
3332 cmdidx_T cmdidx;
3333{
3334 int got_eq = FALSE;
3335 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003336 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003338 if (cmdidx == CMD_let)
3339 {
3340 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003341 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 {
3343 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003344 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 {
3346 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003347 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 if (vim_iswhite(*p))
3349 break;
3350 }
3351 return;
3352 }
3353 }
3354 else
3355 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3356 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 while ((xp->xp_pattern = vim_strpbrk(arg,
3358 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3359 {
3360 c = *xp->xp_pattern;
3361 if (c == '&')
3362 {
3363 c = xp->xp_pattern[1];
3364 if (c == '&')
3365 {
3366 ++xp->xp_pattern;
3367 xp->xp_context = cmdidx != CMD_let || got_eq
3368 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3369 }
3370 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003371 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003373 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3374 xp->xp_pattern += 2;
3375
3376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
3378 else if (c == '$')
3379 {
3380 /* environment variable */
3381 xp->xp_context = EXPAND_ENV_VARS;
3382 }
3383 else if (c == '=')
3384 {
3385 got_eq = TRUE;
3386 xp->xp_context = EXPAND_EXPRESSION;
3387 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003388 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 && xp->xp_context == EXPAND_FUNCTIONS
3390 && vim_strchr(xp->xp_pattern, '(') == NULL)
3391 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003392 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 break;
3394 }
3395 else if (cmdidx != CMD_let || got_eq)
3396 {
3397 if (c == '"') /* string */
3398 {
3399 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3400 if (c == '\\' && xp->xp_pattern[1] != NUL)
3401 ++xp->xp_pattern;
3402 xp->xp_context = EXPAND_NOTHING;
3403 }
3404 else if (c == '\'') /* literal string */
3405 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003406 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3408 /* skip */ ;
3409 xp->xp_context = EXPAND_NOTHING;
3410 }
3411 else if (c == '|')
3412 {
3413 if (xp->xp_pattern[1] == '|')
3414 {
3415 ++xp->xp_pattern;
3416 xp->xp_context = EXPAND_EXPRESSION;
3417 }
3418 else
3419 xp->xp_context = EXPAND_COMMANDS;
3420 }
3421 else
3422 xp->xp_context = EXPAND_EXPRESSION;
3423 }
3424 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003425 /* Doesn't look like something valid, expand as an expression
3426 * anyway. */
3427 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 arg = xp->xp_pattern;
3429 if (*arg != NUL)
3430 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3431 /* skip */ ;
3432 }
3433 xp->xp_pattern = arg;
3434}
3435
3436#endif /* FEAT_CMDL_COMPL */
3437
3438/*
3439 * ":1,25call func(arg1, arg2)" function call.
3440 */
3441 void
3442ex_call(eap)
3443 exarg_T *eap;
3444{
3445 char_u *arg = eap->arg;
3446 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003448 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003450 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 linenr_T lnum;
3452 int doesrange;
3453 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003454 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003456 if (eap->skip)
3457 {
3458 /* trans_function_name() doesn't work well when skipping, use eval0()
3459 * instead to skip to any following command, e.g. for:
3460 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003461 ++emsg_skip;
3462 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3463 clear_tv(&rettv);
3464 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003465 return;
3466 }
3467
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003468 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003469 if (fudi.fd_newkey != NULL)
3470 {
3471 /* Still need to give an error message for missing key. */
3472 EMSG2(_(e_dictkey), fudi.fd_newkey);
3473 vim_free(fudi.fd_newkey);
3474 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003475 if (tofree == NULL)
3476 return;
3477
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003478 /* Increase refcount on dictionary, it could get deleted when evaluating
3479 * the arguments. */
3480 if (fudi.fd_dict != NULL)
3481 ++fudi.fd_dict->dv_refcount;
3482
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003483 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003484 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003485 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar532c7802005-01-27 14:44:31 +00003487 /* Skip white space to allow ":call func ()". Not good, but required for
3488 * backward compatibility. */
3489 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003490 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
3492 if (*startarg != '(')
3493 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003494 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 goto end;
3496 }
3497
3498 /*
3499 * When skipping, evaluate the function once, to find the end of the
3500 * arguments.
3501 * When the function takes a range, this is discovered after the first
3502 * call, and the loop is broken.
3503 */
3504 if (eap->skip)
3505 {
3506 ++emsg_skip;
3507 lnum = eap->line2; /* do it once, also with an invalid range */
3508 }
3509 else
3510 lnum = eap->line1;
3511 for ( ; lnum <= eap->line2; ++lnum)
3512 {
3513 if (!eap->skip && eap->addr_count > 0)
3514 {
3515 curwin->w_cursor.lnum = lnum;
3516 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003517#ifdef FEAT_VIRTUALEDIT
3518 curwin->w_cursor.coladd = 0;
3519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003522 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003523 eap->line1, eap->line2, &doesrange,
3524 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 failed = TRUE;
3527 break;
3528 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003529
3530 /* Handle a function returning a Funcref, Dictionary or List. */
3531 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3532 {
3533 failed = TRUE;
3534 break;
3535 }
3536
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003537 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (doesrange || eap->skip)
3539 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003540
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003542 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003543 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003544 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (aborting())
3546 break;
3547 }
3548 if (eap->skip)
3549 --emsg_skip;
3550
3551 if (!failed)
3552 {
3553 /* Check for trailing illegal characters and a following command. */
3554 if (!ends_excmd(*arg))
3555 {
3556 emsg_severe = TRUE;
3557 EMSG(_(e_trailing));
3558 }
3559 else
3560 eap->nextcmd = check_nextcmd(arg);
3561 }
3562
3563end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003564 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003565 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566}
3567
3568/*
3569 * ":unlet[!] var1 ... " command.
3570 */
3571 void
3572ex_unlet(eap)
3573 exarg_T *eap;
3574{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003575 ex_unletlock(eap, eap->arg, 0);
3576}
3577
3578/*
3579 * ":lockvar" and ":unlockvar" commands
3580 */
3581 void
3582ex_lockvar(eap)
3583 exarg_T *eap;
3584{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003586 int deep = 2;
3587
3588 if (eap->forceit)
3589 deep = -1;
3590 else if (vim_isdigit(*arg))
3591 {
3592 deep = getdigits(&arg);
3593 arg = skipwhite(arg);
3594 }
3595
3596 ex_unletlock(eap, arg, deep);
3597}
3598
3599/*
3600 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3601 */
3602 static void
3603ex_unletlock(eap, argstart, deep)
3604 exarg_T *eap;
3605 char_u *argstart;
3606 int deep;
3607{
3608 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
3613 do
3614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003616 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003617 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 if (lv.ll_name == NULL)
3619 error = TRUE; /* error but continue parsing */
3620 if (name_end == NULL || (!vim_iswhite(*name_end)
3621 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 if (name_end != NULL)
3624 {
3625 emsg_severe = TRUE;
3626 EMSG(_(e_trailing));
3627 }
3628 if (!(eap->skip || error))
3629 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 break;
3631 }
3632
3633 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634 {
3635 if (eap->cmdidx == CMD_unlet)
3636 {
3637 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3638 error = TRUE;
3639 }
3640 else
3641 {
3642 if (do_lock_var(&lv, name_end, deep,
3643 eap->cmdidx == CMD_lockvar) == FAIL)
3644 error = TRUE;
3645 }
3646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 if (!eap->skip)
3649 clear_lval(&lv);
3650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 arg = skipwhite(name_end);
3652 } while (!ends_excmd(*arg));
3653
3654 eap->nextcmd = check_nextcmd(arg);
3655}
3656
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003659 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 int forceit;
3662{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663 int ret = OK;
3664 int cc;
3665
3666 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003668 cc = *name_end;
3669 *name_end = NUL;
3670
3671 /* Normal name or expanded name. */
3672 if (check_changedtick(lp->ll_name))
3673 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003674 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003676 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003677 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003678 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003679 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003680 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003681 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003682 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003683 else if (lp->ll_range)
3684 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003685 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003686 listitem_T *ll_li = lp->ll_li;
3687 int ll_n1 = lp->ll_n1;
3688
3689 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3690 {
3691 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003692 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003693 return FAIL;
3694 ll_li = li;
3695 ++ll_n1;
3696 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697
3698 /* Delete a range of List items. */
3699 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3700 {
3701 li = lp->ll_li->li_next;
3702 listitem_remove(lp->ll_list, lp->ll_li);
3703 lp->ll_li = li;
3704 ++lp->ll_n1;
3705 }
3706 }
3707 else
3708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 /* unlet a List item. */
3711 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003714 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 }
3716
3717 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003718}
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720/*
3721 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003722 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 */
3724 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003725do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003727 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728{
Bram Moolenaar33570922005-01-25 22:26:29 +00003729 hashtab_T *ht;
3730 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003731 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003732 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003733 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
Bram Moolenaar33570922005-01-25 22:26:29 +00003735 ht = find_var_ht(name, &varname);
3736 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003738 if (ht == &globvarht)
3739 d = &globvardict;
3740 else if (current_funccal != NULL
3741 && ht == &current_funccal->l_vars.dv_hashtab)
3742 d = &current_funccal->l_vars;
3743 else if (ht == &compat_hashtab)
3744 d = &vimvardict;
3745 else
3746 {
3747 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3748 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3749 }
3750 if (d == NULL)
3751 {
3752 EMSG2(_(e_intern2), "do_unlet()");
3753 return FAIL;
3754 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003755 hi = hash_find(ht, varname);
3756 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003757 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003758 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003759 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003760 || var_check_ro(di->di_flags, name, FALSE)
3761 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003762 return FAIL;
3763
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003764 delete_var(ht, hi);
3765 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003768 if (forceit)
3769 return OK;
3770 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 return FAIL;
3772}
3773
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003774/*
3775 * Lock or unlock variable indicated by "lp".
3776 * "deep" is the levels to go (-1 for unlimited);
3777 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3778 */
3779 static int
3780do_lock_var(lp, name_end, deep, lock)
3781 lval_T *lp;
3782 char_u *name_end;
3783 int deep;
3784 int lock;
3785{
3786 int ret = OK;
3787 int cc;
3788 dictitem_T *di;
3789
3790 if (deep == 0) /* nothing to do */
3791 return OK;
3792
3793 if (lp->ll_tv == NULL)
3794 {
3795 cc = *name_end;
3796 *name_end = NUL;
3797
3798 /* Normal name or expanded name. */
3799 if (check_changedtick(lp->ll_name))
3800 ret = FAIL;
3801 else
3802 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003803 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003804 if (di == NULL)
3805 ret = FAIL;
3806 else
3807 {
3808 if (lock)
3809 di->di_flags |= DI_FLAGS_LOCK;
3810 else
3811 di->di_flags &= ~DI_FLAGS_LOCK;
3812 item_lock(&di->di_tv, deep, lock);
3813 }
3814 }
3815 *name_end = cc;
3816 }
3817 else if (lp->ll_range)
3818 {
3819 listitem_T *li = lp->ll_li;
3820
3821 /* (un)lock a range of List items. */
3822 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3823 {
3824 item_lock(&li->li_tv, deep, lock);
3825 li = li->li_next;
3826 ++lp->ll_n1;
3827 }
3828 }
3829 else if (lp->ll_list != NULL)
3830 /* (un)lock a List item. */
3831 item_lock(&lp->ll_li->li_tv, deep, lock);
3832 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003833 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003834 item_lock(&lp->ll_di->di_tv, deep, lock);
3835
3836 return ret;
3837}
3838
3839/*
3840 * Lock or unlock an item. "deep" is nr of levels to go.
3841 */
3842 static void
3843item_lock(tv, deep, lock)
3844 typval_T *tv;
3845 int deep;
3846 int lock;
3847{
3848 static int recurse = 0;
3849 list_T *l;
3850 listitem_T *li;
3851 dict_T *d;
3852 hashitem_T *hi;
3853 int todo;
3854
3855 if (recurse >= DICT_MAXNEST)
3856 {
3857 EMSG(_("E743: variable nested too deep for (un)lock"));
3858 return;
3859 }
3860 if (deep == 0)
3861 return;
3862 ++recurse;
3863
3864 /* lock/unlock the item itself */
3865 if (lock)
3866 tv->v_lock |= VAR_LOCKED;
3867 else
3868 tv->v_lock &= ~VAR_LOCKED;
3869
3870 switch (tv->v_type)
3871 {
3872 case VAR_LIST:
3873 if ((l = tv->vval.v_list) != NULL)
3874 {
3875 if (lock)
3876 l->lv_lock |= VAR_LOCKED;
3877 else
3878 l->lv_lock &= ~VAR_LOCKED;
3879 if (deep < 0 || deep > 1)
3880 /* recursive: lock/unlock the items the List contains */
3881 for (li = l->lv_first; li != NULL; li = li->li_next)
3882 item_lock(&li->li_tv, deep - 1, lock);
3883 }
3884 break;
3885 case VAR_DICT:
3886 if ((d = tv->vval.v_dict) != NULL)
3887 {
3888 if (lock)
3889 d->dv_lock |= VAR_LOCKED;
3890 else
3891 d->dv_lock &= ~VAR_LOCKED;
3892 if (deep < 0 || deep > 1)
3893 {
3894 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003895 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003896 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3897 {
3898 if (!HASHITEM_EMPTY(hi))
3899 {
3900 --todo;
3901 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3902 }
3903 }
3904 }
3905 }
3906 }
3907 --recurse;
3908}
3909
Bram Moolenaara40058a2005-07-11 22:42:07 +00003910/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003911 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3912 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003913 */
3914 static int
3915tv_islocked(tv)
3916 typval_T *tv;
3917{
3918 return (tv->v_lock & VAR_LOCKED)
3919 || (tv->v_type == VAR_LIST
3920 && tv->vval.v_list != NULL
3921 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3922 || (tv->v_type == VAR_DICT
3923 && tv->vval.v_dict != NULL
3924 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3925}
3926
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3928/*
3929 * Delete all "menutrans_" variables.
3930 */
3931 void
3932del_menutrans_vars()
3933{
Bram Moolenaar33570922005-01-25 22:26:29 +00003934 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003935 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003938 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 {
3941 if (!HASHITEM_EMPTY(hi))
3942 {
3943 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3945 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003946 }
3947 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949}
3950#endif
3951
3952#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3953
3954/*
3955 * Local string buffer for the next two functions to store a variable name
3956 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3957 * get_user_var_name().
3958 */
3959
3960static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3961
3962static char_u *varnamebuf = NULL;
3963static int varnamebuflen = 0;
3964
3965/*
3966 * Function to concatenate a prefix and a variable name.
3967 */
3968 static char_u *
3969cat_prefix_varname(prefix, name)
3970 int prefix;
3971 char_u *name;
3972{
3973 int len;
3974
3975 len = (int)STRLEN(name) + 3;
3976 if (len > varnamebuflen)
3977 {
3978 vim_free(varnamebuf);
3979 len += 10; /* some additional space */
3980 varnamebuf = alloc(len);
3981 if (varnamebuf == NULL)
3982 {
3983 varnamebuflen = 0;
3984 return NULL;
3985 }
3986 varnamebuflen = len;
3987 }
3988 *varnamebuf = prefix;
3989 varnamebuf[1] = ':';
3990 STRCPY(varnamebuf + 2, name);
3991 return varnamebuf;
3992}
3993
3994/*
3995 * Function given to ExpandGeneric() to obtain the list of user defined
3996 * (global/buffer/window/built-in) variable names.
3997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 char_u *
3999get_user_var_name(xp, idx)
4000 expand_T *xp;
4001 int idx;
4002{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004003 static long_u gdone;
4004 static long_u bdone;
4005 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004006#ifdef FEAT_WINDOWS
4007 static long_u tdone;
4008#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004009 static int vidx;
4010 static hashitem_T *hi;
4011 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
4013 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004014 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004015 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004016#ifdef FEAT_WINDOWS
4017 tdone = 0;
4018#endif
4019 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004020
4021 /* Global variables */
4022 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004024 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004025 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004026 else
4027 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004028 while (HASHITEM_EMPTY(hi))
4029 ++hi;
4030 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4031 return cat_prefix_varname('g', hi->hi_key);
4032 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004034
4035 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004036 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004037 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004039 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004041 else
4042 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 while (HASHITEM_EMPTY(hi))
4044 ++hi;
4045 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004049 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 return (char_u *)"b:changedtick";
4051 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004052
4053 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004054 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004057 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004058 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004059 else
4060 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004061 while (HASHITEM_EMPTY(hi))
4062 ++hi;
4063 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004065
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004066#ifdef FEAT_WINDOWS
4067 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004068 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069 if (tdone < ht->ht_used)
4070 {
4071 if (tdone++ == 0)
4072 hi = ht->ht_array;
4073 else
4074 ++hi;
4075 while (HASHITEM_EMPTY(hi))
4076 ++hi;
4077 return cat_prefix_varname('t', hi->hi_key);
4078 }
4079#endif
4080
Bram Moolenaar33570922005-01-25 22:26:29 +00004081 /* v: variables */
4082 if (vidx < VV_LEN)
4083 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 vim_free(varnamebuf);
4086 varnamebuf = NULL;
4087 varnamebuflen = 0;
4088 return NULL;
4089}
4090
4091#endif /* FEAT_CMDL_COMPL */
4092
4093/*
4094 * types for expressions.
4095 */
4096typedef enum
4097{
4098 TYPE_UNKNOWN = 0
4099 , TYPE_EQUAL /* == */
4100 , TYPE_NEQUAL /* != */
4101 , TYPE_GREATER /* > */
4102 , TYPE_GEQUAL /* >= */
4103 , TYPE_SMALLER /* < */
4104 , TYPE_SEQUAL /* <= */
4105 , TYPE_MATCH /* =~ */
4106 , TYPE_NOMATCH /* !~ */
4107} exptype_T;
4108
4109/*
4110 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4113 */
4114
4115/*
4116 * Handle zero level expression.
4117 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004119 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 * Return OK or FAIL.
4121 */
4122 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 char_u **nextcmd;
4127 int evaluate;
4128{
4129 int ret;
4130 char_u *p;
4131
4132 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 if (ret == FAIL || !ends_excmd(*p))
4135 {
4136 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 /*
4139 * Report the invalid expression unless the expression evaluation has
4140 * been cancelled due to an aborting error, an interrupt, or an
4141 * exception.
4142 */
4143 if (!aborting())
4144 EMSG2(_(e_invexpr2), arg);
4145 ret = FAIL;
4146 }
4147 if (nextcmd != NULL)
4148 *nextcmd = check_nextcmd(p);
4149
4150 return ret;
4151}
4152
4153/*
4154 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004155 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 *
4157 * "arg" must point to the first non-white of the expression.
4158 * "arg" is advanced to the next non-white after the recognized expression.
4159 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004160 * Note: "rettv.v_lock" is not set.
4161 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 * Return OK or FAIL.
4163 */
4164 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004165eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 int evaluate;
4169{
4170 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004171 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 /*
4174 * Get the first variable.
4175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178
4179 if ((*arg)[0] == '?')
4180 {
4181 result = FALSE;
4182 if (evaluate)
4183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 int error = FALSE;
4185
4186 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004189 if (error)
4190 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192
4193 /*
4194 * Get the second variable.
4195 */
4196 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 return FAIL;
4199
4200 /*
4201 * Check for the ":".
4202 */
4203 if ((*arg)[0] != ':')
4204 {
4205 EMSG(_("E109: Missing ':' after '?'"));
4206 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 return FAIL;
4209 }
4210
4211 /*
4212 * Get the third variable.
4213 */
4214 *arg = skipwhite(*arg + 1);
4215 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4216 {
4217 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 return FAIL;
4220 }
4221 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224
4225 return OK;
4226}
4227
4228/*
4229 * Handle first level expression:
4230 * expr2 || expr2 || expr2 logical OR
4231 *
4232 * "arg" must point to the first non-white of the expression.
4233 * "arg" is advanced to the next non-white after the recognized expression.
4234 *
4235 * Return OK or FAIL.
4236 */
4237 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 int evaluate;
4242{
Bram Moolenaar33570922005-01-25 22:26:29 +00004243 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 long result;
4245 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
4248 /*
4249 * Get the first variable.
4250 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 return FAIL;
4253
4254 /*
4255 * Repeat until there is no following "||".
4256 */
4257 first = TRUE;
4258 result = FALSE;
4259 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4260 {
4261 if (evaluate && first)
4262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 if (error)
4267 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 first = FALSE;
4269 }
4270
4271 /*
4272 * Get the second variable.
4273 */
4274 *arg = skipwhite(*arg + 2);
4275 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4276 return FAIL;
4277
4278 /*
4279 * Compute the result.
4280 */
4281 if (evaluate && !result)
4282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004285 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004286 if (error)
4287 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
4289 if (evaluate)
4290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 rettv->v_type = VAR_NUMBER;
4292 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
4294 }
4295
4296 return OK;
4297}
4298
4299/*
4300 * Handle second level expression:
4301 * expr3 && expr3 && expr3 logical AND
4302 *
4303 * "arg" must point to the first non-white of the expression.
4304 * "arg" is advanced to the next non-white after the recognized expression.
4305 *
4306 * Return OK or FAIL.
4307 */
4308 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 int evaluate;
4313{
Bram Moolenaar33570922005-01-25 22:26:29 +00004314 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 long result;
4316 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
4319 /*
4320 * Get the first variable.
4321 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 return FAIL;
4324
4325 /*
4326 * Repeat until there is no following "&&".
4327 */
4328 first = TRUE;
4329 result = TRUE;
4330 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4331 {
4332 if (evaluate && first)
4333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004334 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 if (error)
4338 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 first = FALSE;
4340 }
4341
4342 /*
4343 * Get the second variable.
4344 */
4345 *arg = skipwhite(*arg + 2);
4346 if (eval4(arg, &var2, evaluate && result) == FAIL)
4347 return FAIL;
4348
4349 /*
4350 * Compute the result.
4351 */
4352 if (evaluate && result)
4353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004354 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 if (error)
4358 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360 if (evaluate)
4361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004362 rettv->v_type = VAR_NUMBER;
4363 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365 }
4366
4367 return OK;
4368}
4369
4370/*
4371 * Handle third level expression:
4372 * var1 == var2
4373 * var1 =~ var2
4374 * var1 != var2
4375 * var1 !~ var2
4376 * var1 > var2
4377 * var1 >= var2
4378 * var1 < var2
4379 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004380 * var1 is var2
4381 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 *
4383 * "arg" must point to the first non-white of the expression.
4384 * "arg" is advanced to the next non-white after the recognized expression.
4385 *
4386 * Return OK or FAIL.
4387 */
4388 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004389eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 int evaluate;
4393{
Bram Moolenaar33570922005-01-25 22:26:29 +00004394 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 char_u *p;
4396 int i;
4397 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004398 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 int len = 2;
4400 long n1, n2;
4401 char_u *s1, *s2;
4402 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4403 regmatch_T regmatch;
4404 int ic;
4405 char_u *save_cpo;
4406
4407 /*
4408 * Get the first variable.
4409 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004410 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 return FAIL;
4412
4413 p = *arg;
4414 switch (p[0])
4415 {
4416 case '=': if (p[1] == '=')
4417 type = TYPE_EQUAL;
4418 else if (p[1] == '~')
4419 type = TYPE_MATCH;
4420 break;
4421 case '!': if (p[1] == '=')
4422 type = TYPE_NEQUAL;
4423 else if (p[1] == '~')
4424 type = TYPE_NOMATCH;
4425 break;
4426 case '>': if (p[1] != '=')
4427 {
4428 type = TYPE_GREATER;
4429 len = 1;
4430 }
4431 else
4432 type = TYPE_GEQUAL;
4433 break;
4434 case '<': if (p[1] != '=')
4435 {
4436 type = TYPE_SMALLER;
4437 len = 1;
4438 }
4439 else
4440 type = TYPE_SEQUAL;
4441 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004442 case 'i': if (p[1] == 's')
4443 {
4444 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4445 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004446 i = p[len];
4447 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004448 {
4449 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4450 type_is = TRUE;
4451 }
4452 }
4453 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
4455
4456 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004457 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 */
4459 if (type != TYPE_UNKNOWN)
4460 {
4461 /* extra question mark appended: ignore case */
4462 if (p[len] == '?')
4463 {
4464 ic = TRUE;
4465 ++len;
4466 }
4467 /* extra '#' appended: match case */
4468 else if (p[len] == '#')
4469 {
4470 ic = FALSE;
4471 ++len;
4472 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004473 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 else
4475 ic = p_ic;
4476
4477 /*
4478 * Get the second variable.
4479 */
4480 *arg = skipwhite(p + len);
4481 if (eval5(arg, &var2, evaluate) == FAIL)
4482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004483 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 return FAIL;
4485 }
4486
4487 if (evaluate)
4488 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004489 if (type_is && rettv->v_type != var2.v_type)
4490 {
4491 /* For "is" a different type always means FALSE, for "notis"
4492 * it means TRUE. */
4493 n1 = (type == TYPE_NEQUAL);
4494 }
4495 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4496 {
4497 if (type_is)
4498 {
4499 n1 = (rettv->v_type == var2.v_type
4500 && rettv->vval.v_list == var2.vval.v_list);
4501 if (type == TYPE_NEQUAL)
4502 n1 = !n1;
4503 }
4504 else if (rettv->v_type != var2.v_type
4505 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4506 {
4507 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004508 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004509 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004510 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004511 clear_tv(rettv);
4512 clear_tv(&var2);
4513 return FAIL;
4514 }
4515 else
4516 {
4517 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004518 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4519 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004520 if (type == TYPE_NEQUAL)
4521 n1 = !n1;
4522 }
4523 }
4524
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004525 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4526 {
4527 if (type_is)
4528 {
4529 n1 = (rettv->v_type == var2.v_type
4530 && rettv->vval.v_dict == var2.vval.v_dict);
4531 if (type == TYPE_NEQUAL)
4532 n1 = !n1;
4533 }
4534 else if (rettv->v_type != var2.v_type
4535 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4536 {
4537 if (rettv->v_type != var2.v_type)
4538 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4539 else
4540 EMSG(_("E736: Invalid operation for Dictionary"));
4541 clear_tv(rettv);
4542 clear_tv(&var2);
4543 return FAIL;
4544 }
4545 else
4546 {
4547 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004548 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4549 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004550 if (type == TYPE_NEQUAL)
4551 n1 = !n1;
4552 }
4553 }
4554
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004555 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4556 {
4557 if (rettv->v_type != var2.v_type
4558 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4559 {
4560 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004561 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004562 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004563 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004564 clear_tv(rettv);
4565 clear_tv(&var2);
4566 return FAIL;
4567 }
4568 else
4569 {
4570 /* Compare two Funcrefs for being equal or unequal. */
4571 if (rettv->vval.v_string == NULL
4572 || var2.vval.v_string == NULL)
4573 n1 = FALSE;
4574 else
4575 n1 = STRCMP(rettv->vval.v_string,
4576 var2.vval.v_string) == 0;
4577 if (type == TYPE_NEQUAL)
4578 n1 = !n1;
4579 }
4580 }
4581
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004582#ifdef FEAT_FLOAT
4583 /*
4584 * If one of the two variables is a float, compare as a float.
4585 * When using "=~" or "!~", always compare as string.
4586 */
4587 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4588 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4589 {
4590 float_T f1, f2;
4591
4592 if (rettv->v_type == VAR_FLOAT)
4593 f1 = rettv->vval.v_float;
4594 else
4595 f1 = get_tv_number(rettv);
4596 if (var2.v_type == VAR_FLOAT)
4597 f2 = var2.vval.v_float;
4598 else
4599 f2 = get_tv_number(&var2);
4600 n1 = FALSE;
4601 switch (type)
4602 {
4603 case TYPE_EQUAL: n1 = (f1 == f2); break;
4604 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4605 case TYPE_GREATER: n1 = (f1 > f2); break;
4606 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4607 case TYPE_SMALLER: n1 = (f1 < f2); break;
4608 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4609 case TYPE_UNKNOWN:
4610 case TYPE_MATCH:
4611 case TYPE_NOMATCH: break; /* avoid gcc warning */
4612 }
4613 }
4614#endif
4615
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 /*
4617 * If one of the two variables is a number, compare as a number.
4618 * When using "=~" or "!~", always compare as string.
4619 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004620 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4622 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004623 n1 = get_tv_number(rettv);
4624 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 switch (type)
4626 {
4627 case TYPE_EQUAL: n1 = (n1 == n2); break;
4628 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4629 case TYPE_GREATER: n1 = (n1 > n2); break;
4630 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4631 case TYPE_SMALLER: n1 = (n1 < n2); break;
4632 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4633 case TYPE_UNKNOWN:
4634 case TYPE_MATCH:
4635 case TYPE_NOMATCH: break; /* avoid gcc warning */
4636 }
4637 }
4638 else
4639 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004640 s1 = get_tv_string_buf(rettv, buf1);
4641 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4643 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4644 else
4645 i = 0;
4646 n1 = FALSE;
4647 switch (type)
4648 {
4649 case TYPE_EQUAL: n1 = (i == 0); break;
4650 case TYPE_NEQUAL: n1 = (i != 0); break;
4651 case TYPE_GREATER: n1 = (i > 0); break;
4652 case TYPE_GEQUAL: n1 = (i >= 0); break;
4653 case TYPE_SMALLER: n1 = (i < 0); break;
4654 case TYPE_SEQUAL: n1 = (i <= 0); break;
4655
4656 case TYPE_MATCH:
4657 case TYPE_NOMATCH:
4658 /* avoid 'l' flag in 'cpoptions' */
4659 save_cpo = p_cpo;
4660 p_cpo = (char_u *)"";
4661 regmatch.regprog = vim_regcomp(s2,
4662 RE_MAGIC + RE_STRING);
4663 regmatch.rm_ic = ic;
4664 if (regmatch.regprog != NULL)
4665 {
4666 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004667 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 if (type == TYPE_NOMATCH)
4669 n1 = !n1;
4670 }
4671 p_cpo = save_cpo;
4672 break;
4673
4674 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4675 }
4676 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004677 clear_tv(rettv);
4678 clear_tv(&var2);
4679 rettv->v_type = VAR_NUMBER;
4680 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
4682 }
4683
4684 return OK;
4685}
4686
4687/*
4688 * Handle fourth level expression:
4689 * + number addition
4690 * - number subtraction
4691 * . string concatenation
4692 *
4693 * "arg" must point to the first non-white of the expression.
4694 * "arg" is advanced to the next non-white after the recognized expression.
4695 *
4696 * Return OK or FAIL.
4697 */
4698 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004699eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 int evaluate;
4703{
Bram Moolenaar33570922005-01-25 22:26:29 +00004704 typval_T var2;
4705 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 int op;
4707 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004708#ifdef FEAT_FLOAT
4709 float_T f1 = 0, f2 = 0;
4710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 char_u *s1, *s2;
4712 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4713 char_u *p;
4714
4715 /*
4716 * Get the first variable.
4717 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004718 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 return FAIL;
4720
4721 /*
4722 * Repeat computing, until no '+', '-' or '.' is following.
4723 */
4724 for (;;)
4725 {
4726 op = **arg;
4727 if (op != '+' && op != '-' && op != '.')
4728 break;
4729
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004730 if ((op != '+' || rettv->v_type != VAR_LIST)
4731#ifdef FEAT_FLOAT
4732 && (op == '.' || rettv->v_type != VAR_FLOAT)
4733#endif
4734 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004735 {
4736 /* For "list + ...", an illegal use of the first operand as
4737 * a number cannot be determined before evaluating the 2nd
4738 * operand: if this is also a list, all is ok.
4739 * For "something . ...", "something - ..." or "non-list + ...",
4740 * we know that the first operand needs to be a string or number
4741 * without evaluating the 2nd operand. So check before to avoid
4742 * side effects after an error. */
4743 if (evaluate && get_tv_string_chk(rettv) == NULL)
4744 {
4745 clear_tv(rettv);
4746 return FAIL;
4747 }
4748 }
4749
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 /*
4751 * Get the second variable.
4752 */
4753 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004754 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 return FAIL;
4758 }
4759
4760 if (evaluate)
4761 {
4762 /*
4763 * Compute the result.
4764 */
4765 if (op == '.')
4766 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004767 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4768 s2 = get_tv_string_buf_chk(&var2, buf2);
4769 if (s2 == NULL) /* type error ? */
4770 {
4771 clear_tv(rettv);
4772 clear_tv(&var2);
4773 return FAIL;
4774 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004775 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004776 clear_tv(rettv);
4777 rettv->v_type = VAR_STRING;
4778 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004780 else if (op == '+' && rettv->v_type == VAR_LIST
4781 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004782 {
4783 /* concatenate Lists */
4784 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4785 &var3) == FAIL)
4786 {
4787 clear_tv(rettv);
4788 clear_tv(&var2);
4789 return FAIL;
4790 }
4791 clear_tv(rettv);
4792 *rettv = var3;
4793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 else
4795 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004796 int error = FALSE;
4797
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004798#ifdef FEAT_FLOAT
4799 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801 f1 = rettv->vval.v_float;
4802 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004803 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004804 else
4805#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004806 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004807 n1 = get_tv_number_chk(rettv, &error);
4808 if (error)
4809 {
4810 /* This can only happen for "list + non-list". For
4811 * "non-list + ..." or "something - ...", we returned
4812 * before evaluating the 2nd operand. */
4813 clear_tv(rettv);
4814 return FAIL;
4815 }
4816#ifdef FEAT_FLOAT
4817 if (var2.v_type == VAR_FLOAT)
4818 f1 = n1;
4819#endif
4820 }
4821#ifdef FEAT_FLOAT
4822 if (var2.v_type == VAR_FLOAT)
4823 {
4824 f2 = var2.vval.v_float;
4825 n2 = 0;
4826 }
4827 else
4828#endif
4829 {
4830 n2 = get_tv_number_chk(&var2, &error);
4831 if (error)
4832 {
4833 clear_tv(rettv);
4834 clear_tv(&var2);
4835 return FAIL;
4836 }
4837#ifdef FEAT_FLOAT
4838 if (rettv->v_type == VAR_FLOAT)
4839 f2 = n2;
4840#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004842 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004843
4844#ifdef FEAT_FLOAT
4845 /* If there is a float on either side the result is a float. */
4846 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4847 {
4848 if (op == '+')
4849 f1 = f1 + f2;
4850 else
4851 f1 = f1 - f2;
4852 rettv->v_type = VAR_FLOAT;
4853 rettv->vval.v_float = f1;
4854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004856#endif
4857 {
4858 if (op == '+')
4859 n1 = n1 + n2;
4860 else
4861 n1 = n1 - n2;
4862 rettv->v_type = VAR_NUMBER;
4863 rettv->vval.v_number = n1;
4864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004866 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868 }
4869 return OK;
4870}
4871
4872/*
4873 * Handle fifth level expression:
4874 * * number multiplication
4875 * / number division
4876 * % number modulo
4877 *
4878 * "arg" must point to the first non-white of the expression.
4879 * "arg" is advanced to the next non-white after the recognized expression.
4880 *
4881 * Return OK or FAIL.
4882 */
4883 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004884eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004888 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889{
Bram Moolenaar33570922005-01-25 22:26:29 +00004890 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 int op;
4892 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004893#ifdef FEAT_FLOAT
4894 int use_float = FALSE;
4895 float_T f1 = 0, f2;
4896#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004897 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898
4899 /*
4900 * Get the first variable.
4901 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004902 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 return FAIL;
4904
4905 /*
4906 * Repeat computing, until no '*', '/' or '%' is following.
4907 */
4908 for (;;)
4909 {
4910 op = **arg;
4911 if (op != '*' && op != '/' && op != '%')
4912 break;
4913
4914 if (evaluate)
4915 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916#ifdef FEAT_FLOAT
4917 if (rettv->v_type == VAR_FLOAT)
4918 {
4919 f1 = rettv->vval.v_float;
4920 use_float = TRUE;
4921 n1 = 0;
4922 }
4923 else
4924#endif
4925 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004926 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004927 if (error)
4928 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
4930 else
4931 n1 = 0;
4932
4933 /*
4934 * Get the second variable.
4935 */
4936 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004937 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 return FAIL;
4939
4940 if (evaluate)
4941 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004942#ifdef FEAT_FLOAT
4943 if (var2.v_type == VAR_FLOAT)
4944 {
4945 if (!use_float)
4946 {
4947 f1 = n1;
4948 use_float = TRUE;
4949 }
4950 f2 = var2.vval.v_float;
4951 n2 = 0;
4952 }
4953 else
4954#endif
4955 {
4956 n2 = get_tv_number_chk(&var2, &error);
4957 clear_tv(&var2);
4958 if (error)
4959 return FAIL;
4960#ifdef FEAT_FLOAT
4961 if (use_float)
4962 f2 = n2;
4963#endif
4964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965
4966 /*
4967 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004968 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004970#ifdef FEAT_FLOAT
4971 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004973 if (op == '*')
4974 f1 = f1 * f2;
4975 else if (op == '/')
4976 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004977# ifdef VMS
4978 /* VMS crashes on divide by zero, work around it */
4979 if (f2 == 0.0)
4980 {
4981 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004982 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004983 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004984 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004985 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004986 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004987 }
4988 else
4989 f1 = f1 / f2;
4990# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991 /* We rely on the floating point library to handle divide
4992 * by zero to result in "inf" and not a crash. */
4993 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004994# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004998 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999 return FAIL;
5000 }
5001 rettv->v_type = VAR_FLOAT;
5002 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 }
5004 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005007 if (op == '*')
5008 n1 = n1 * n2;
5009 else if (op == '/')
5010 {
5011 if (n2 == 0) /* give an error message? */
5012 {
5013 if (n1 == 0)
5014 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5015 else if (n1 < 0)
5016 n1 = -0x7fffffffL;
5017 else
5018 n1 = 0x7fffffffL;
5019 }
5020 else
5021 n1 = n1 / n2;
5022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005024 {
5025 if (n2 == 0) /* give an error message? */
5026 n1 = 0;
5027 else
5028 n1 = n1 % n2;
5029 }
5030 rettv->v_type = VAR_NUMBER;
5031 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 }
5034 }
5035
5036 return OK;
5037}
5038
5039/*
5040 * Handle sixth level expression:
5041 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005042 * "string" string constant
5043 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 * &option-name option value
5045 * @r register contents
5046 * identifier variable value
5047 * function() function call
5048 * $VAR environment variable
5049 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005050 * [expr, expr] List
5051 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 *
5053 * Also handle:
5054 * ! in front logical NOT
5055 * - in front unary minus
5056 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 * trailing [] subscript in String or List
5058 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 *
5060 * "arg" must point to the first non-white of the expression.
5061 * "arg" is advanced to the next non-white after the recognized expression.
5062 *
5063 * Return OK or FAIL.
5064 */
5065 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005066eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005070 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 long n;
5073 int len;
5074 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 char_u *start_leader, *end_leader;
5076 int ret = OK;
5077 char_u *alias;
5078
5079 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005080 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005081 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
5085 /*
5086 * Skip '!' and '-' characters. They are handled later.
5087 */
5088 start_leader = *arg;
5089 while (**arg == '!' || **arg == '-' || **arg == '+')
5090 *arg = skipwhite(*arg + 1);
5091 end_leader = *arg;
5092
5093 switch (**arg)
5094 {
5095 /*
5096 * Number constant.
5097 */
5098 case '0':
5099 case '1':
5100 case '2':
5101 case '3':
5102 case '4':
5103 case '5':
5104 case '6':
5105 case '7':
5106 case '8':
5107 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005108 {
5109#ifdef FEAT_FLOAT
5110 char_u *p = skipdigits(*arg + 1);
5111 int get_float = FALSE;
5112
5113 /* We accept a float when the format matches
5114 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005115 * strict to avoid backwards compatibility problems.
5116 * Don't look for a float after the "." operator, so that
5117 * ":let vers = 1.2.3" doesn't fail. */
5118 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005120 get_float = TRUE;
5121 p = skipdigits(p + 2);
5122 if (*p == 'e' || *p == 'E')
5123 {
5124 ++p;
5125 if (*p == '-' || *p == '+')
5126 ++p;
5127 if (!vim_isdigit(*p))
5128 get_float = FALSE;
5129 else
5130 p = skipdigits(p + 1);
5131 }
5132 if (ASCII_ISALPHA(*p) || *p == '.')
5133 get_float = FALSE;
5134 }
5135 if (get_float)
5136 {
5137 float_T f;
5138
5139 *arg += string2float(*arg, &f);
5140 if (evaluate)
5141 {
5142 rettv->v_type = VAR_FLOAT;
5143 rettv->vval.v_float = f;
5144 }
5145 }
5146 else
5147#endif
5148 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005149 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005150 *arg += len;
5151 if (evaluate)
5152 {
5153 rettv->v_type = VAR_NUMBER;
5154 rettv->vval.v_number = n;
5155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
5157 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159
5160 /*
5161 * String constant: "string".
5162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005163 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 break;
5165
5166 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005169 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005170 break;
5171
5172 /*
5173 * List: [expr, expr]
5174 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005175 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 break;
5177
5178 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 * Dictionary: {key: val, key: val}
5180 */
5181 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5182 break;
5183
5184 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005185 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005187 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 break;
5189
5190 /*
5191 * Environment variable: $VAR.
5192 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005193 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 break;
5195
5196 /*
5197 * Register contents: @r.
5198 */
5199 case '@': ++*arg;
5200 if (evaluate)
5201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005203 rettv->vval.v_string = get_reg_contents(**arg,
5204 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
5206 if (**arg != NUL)
5207 ++*arg;
5208 break;
5209
5210 /*
5211 * nested expression: (expression).
5212 */
5213 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005214 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (**arg == ')')
5216 ++*arg;
5217 else if (ret == OK)
5218 {
5219 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005220 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 ret = FAIL;
5222 }
5223 break;
5224
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 break;
5227 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228
5229 if (ret == NOTDONE)
5230 {
5231 /*
5232 * Must be a variable or function name.
5233 * Can also be a curly-braces kind of name: {expr}.
5234 */
5235 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005236 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 if (alias != NULL)
5238 s = alias;
5239
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005240 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 ret = FAIL;
5242 else
5243 {
5244 if (**arg == '(') /* recursive! */
5245 {
5246 /* If "s" is the name of a variable of type VAR_FUNC
5247 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005248 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249
5250 /* Invoke the function. */
5251 ret = get_func_tv(s, len, rettv, arg,
5252 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005253 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005254
5255 /* If evaluate is FALSE rettv->v_type was not set in
5256 * get_func_tv, but it's needed in handle_subscript() to parse
5257 * what follows. So set it here. */
5258 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5259 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005260 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005261 rettv->v_type = VAR_FUNC;
5262 }
5263
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 /* Stop the expression evaluation when immediately
5265 * aborting on error, or when an interrupt occurred or
5266 * an exception was thrown but not caught. */
5267 if (aborting())
5268 {
5269 if (ret == OK)
5270 clear_tv(rettv);
5271 ret = FAIL;
5272 }
5273 }
5274 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005275 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005276 else
5277 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005279 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 }
5281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 *arg = skipwhite(*arg);
5283
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005284 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5285 * expr(expr). */
5286 if (ret == OK)
5287 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
5289 /*
5290 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5291 */
5292 if (ret == OK && evaluate && end_leader > start_leader)
5293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005295 int val = 0;
5296#ifdef FEAT_FLOAT
5297 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005299 if (rettv->v_type == VAR_FLOAT)
5300 f = rettv->vval.v_float;
5301 else
5302#endif
5303 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005304 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 clear_tv(rettv);
5307 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005309 else
5310 {
5311 while (end_leader > start_leader)
5312 {
5313 --end_leader;
5314 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005315 {
5316#ifdef FEAT_FLOAT
5317 if (rettv->v_type == VAR_FLOAT)
5318 f = !f;
5319 else
5320#endif
5321 val = !val;
5322 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005323 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005324 {
5325#ifdef FEAT_FLOAT
5326 if (rettv->v_type == VAR_FLOAT)
5327 f = -f;
5328 else
5329#endif
5330 val = -val;
5331 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005332 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005333#ifdef FEAT_FLOAT
5334 if (rettv->v_type == VAR_FLOAT)
5335 {
5336 clear_tv(rettv);
5337 rettv->vval.v_float = f;
5338 }
5339 else
5340#endif
5341 {
5342 clear_tv(rettv);
5343 rettv->v_type = VAR_NUMBER;
5344 rettv->vval.v_number = val;
5345 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
5348
5349 return ret;
5350}
5351
5352/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005353 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5354 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5356 */
5357 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005358eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005360 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005362 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363{
5364 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005365 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005367 long len = -1;
5368 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005370 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005372 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005374 if (verbose)
5375 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376 return FAIL;
5377 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005378#ifdef FEAT_FLOAT
5379 else if (rettv->v_type == VAR_FLOAT)
5380 {
5381 if (verbose)
5382 EMSG(_(e_float_as_string));
5383 return FAIL;
5384 }
5385#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005387 init_tv(&var1);
5388 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 /*
5392 * dict.name
5393 */
5394 key = *arg + 1;
5395 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5396 ;
5397 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005399 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 }
5401 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005403 /*
5404 * something[idx]
5405 *
5406 * Get the (first) variable from inside the [].
5407 */
5408 *arg = skipwhite(*arg + 1);
5409 if (**arg == ':')
5410 empty1 = TRUE;
5411 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5412 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005413 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5414 {
5415 /* not a number or string */
5416 clear_tv(&var1);
5417 return FAIL;
5418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005419
5420 /*
5421 * Get the second variable from inside the [:].
5422 */
5423 if (**arg == ':')
5424 {
5425 range = TRUE;
5426 *arg = skipwhite(*arg + 1);
5427 if (**arg == ']')
5428 empty2 = TRUE;
5429 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005431 if (!empty1)
5432 clear_tv(&var1);
5433 return FAIL;
5434 }
5435 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5436 {
5437 /* not a number or string */
5438 if (!empty1)
5439 clear_tv(&var1);
5440 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441 return FAIL;
5442 }
5443 }
5444
5445 /* Check for the ']'. */
5446 if (**arg != ']')
5447 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005448 if (verbose)
5449 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 clear_tv(&var1);
5451 if (range)
5452 clear_tv(&var2);
5453 return FAIL;
5454 }
5455 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 }
5457
5458 if (evaluate)
5459 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460 n1 = 0;
5461 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005463 n1 = get_tv_number(&var1);
5464 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 }
5466 if (range)
5467 {
5468 if (empty2)
5469 n2 = -1;
5470 else
5471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005472 n2 = get_tv_number(&var2);
5473 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 }
5475 }
5476
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005477 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 {
5479 case VAR_NUMBER:
5480 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 len = (long)STRLEN(s);
5483 if (range)
5484 {
5485 /* The resulting variable is a substring. If the indexes
5486 * are out of range the result is empty. */
5487 if (n1 < 0)
5488 {
5489 n1 = len + n1;
5490 if (n1 < 0)
5491 n1 = 0;
5492 }
5493 if (n2 < 0)
5494 n2 = len + n2;
5495 else if (n2 >= len)
5496 n2 = len;
5497 if (n1 >= len || n2 < 0 || n1 > n2)
5498 s = NULL;
5499 else
5500 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5501 }
5502 else
5503 {
5504 /* The resulting variable is a string of a single
5505 * character. If the index is too big or negative the
5506 * result is empty. */
5507 if (n1 >= len || n1 < 0)
5508 s = NULL;
5509 else
5510 s = vim_strnsave(s + n1, 1);
5511 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005512 clear_tv(rettv);
5513 rettv->v_type = VAR_STRING;
5514 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005515 break;
5516
5517 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005518 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 if (n1 < 0)
5520 n1 = len + n1;
5521 if (!empty1 && (n1 < 0 || n1 >= len))
5522 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005523 /* For a range we allow invalid values and return an empty
5524 * list. A list index out of range is an error. */
5525 if (!range)
5526 {
5527 if (verbose)
5528 EMSGN(_(e_listidx), n1);
5529 return FAIL;
5530 }
5531 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005532 }
5533 if (range)
5534 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005535 list_T *l;
5536 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005537
5538 if (n2 < 0)
5539 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005540 else if (n2 >= len)
5541 n2 = len - 1;
5542 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005543 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 l = list_alloc();
5545 if (l == NULL)
5546 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005547 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 n1 <= n2; ++n1)
5549 {
5550 if (list_append_tv(l, &item->li_tv) == FAIL)
5551 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005552 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 return FAIL;
5554 }
5555 item = item->li_next;
5556 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 clear_tv(rettv);
5558 rettv->v_type = VAR_LIST;
5559 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005560 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 }
5562 else
5563 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005564 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 clear_tv(rettv);
5566 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 }
5568 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005569
5570 case VAR_DICT:
5571 if (range)
5572 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005573 if (verbose)
5574 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575 if (len == -1)
5576 clear_tv(&var1);
5577 return FAIL;
5578 }
5579 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005580 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581
5582 if (len == -1)
5583 {
5584 key = get_tv_string(&var1);
5585 if (*key == NUL)
5586 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005587 if (verbose)
5588 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589 clear_tv(&var1);
5590 return FAIL;
5591 }
5592 }
5593
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005594 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005596 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005597 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005598 if (len == -1)
5599 clear_tv(&var1);
5600 if (item == NULL)
5601 return FAIL;
5602
5603 copy_tv(&item->di_tv, &var1);
5604 clear_tv(rettv);
5605 *rettv = var1;
5606 }
5607 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 }
5609 }
5610
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005611 return OK;
5612}
5613
5614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 * Get an option value.
5616 * "arg" points to the '&' or '+' before the option name.
5617 * "arg" is advanced to character after the option name.
5618 * Return OK or FAIL.
5619 */
5620 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005621get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005623 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 int evaluate;
5625{
5626 char_u *option_end;
5627 long numval;
5628 char_u *stringval;
5629 int opt_type;
5630 int c;
5631 int working = (**arg == '+'); /* has("+option") */
5632 int ret = OK;
5633 int opt_flags;
5634
5635 /*
5636 * Isolate the option name and find its value.
5637 */
5638 option_end = find_option_end(arg, &opt_flags);
5639 if (option_end == NULL)
5640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005641 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 EMSG2(_("E112: Option name missing: %s"), *arg);
5643 return FAIL;
5644 }
5645
5646 if (!evaluate)
5647 {
5648 *arg = option_end;
5649 return OK;
5650 }
5651
5652 c = *option_end;
5653 *option_end = NUL;
5654 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
5657 if (opt_type == -3) /* invalid name */
5658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 EMSG2(_("E113: Unknown option: %s"), *arg);
5661 ret = FAIL;
5662 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 {
5665 if (opt_type == -2) /* hidden string option */
5666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 rettv->v_type = VAR_STRING;
5668 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
5670 else if (opt_type == -1) /* hidden number option */
5671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672 rettv->v_type = VAR_NUMBER;
5673 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675 else if (opt_type == 1) /* number option */
5676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677 rettv->v_type = VAR_NUMBER;
5678 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
5680 else /* string option */
5681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005682 rettv->v_type = VAR_STRING;
5683 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
5685 }
5686 else if (working && (opt_type == -2 || opt_type == -1))
5687 ret = FAIL;
5688
5689 *option_end = c; /* put back for error messages */
5690 *arg = option_end;
5691
5692 return ret;
5693}
5694
5695/*
5696 * Allocate a variable for a string constant.
5697 * Return OK or FAIL.
5698 */
5699 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005700get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 int evaluate;
5704{
5705 char_u *p;
5706 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 int extra = 0;
5708
5709 /*
5710 * Find the end of the string, skipping backslashed characters.
5711 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005712 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
5714 if (*p == '\\' && p[1] != NUL)
5715 {
5716 ++p;
5717 /* A "\<x>" form occupies at least 4 characters, and produces up
5718 * to 6 characters: reserve space for 2 extra */
5719 if (*p == '<')
5720 extra += 2;
5721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
5723
5724 if (*p != '"')
5725 {
5726 EMSG2(_("E114: Missing quote: %s"), *arg);
5727 return FAIL;
5728 }
5729
5730 /* If only parsing, set *arg and return here */
5731 if (!evaluate)
5732 {
5733 *arg = p + 1;
5734 return OK;
5735 }
5736
5737 /*
5738 * Copy the string into allocated memory, handling backslashed
5739 * characters.
5740 */
5741 name = alloc((unsigned)(p - *arg + extra));
5742 if (name == NULL)
5743 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 rettv->v_type = VAR_STRING;
5745 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 {
5749 if (*p == '\\')
5750 {
5751 switch (*++p)
5752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 case 'b': *name++ = BS; ++p; break;
5754 case 'e': *name++ = ESC; ++p; break;
5755 case 'f': *name++ = FF; ++p; break;
5756 case 'n': *name++ = NL; ++p; break;
5757 case 'r': *name++ = CAR; ++p; break;
5758 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759
5760 case 'X': /* hex: "\x1", "\x12" */
5761 case 'x':
5762 case 'u': /* Unicode: "\u0023" */
5763 case 'U':
5764 if (vim_isxdigit(p[1]))
5765 {
5766 int n, nr;
5767 int c = toupper(*p);
5768
5769 if (c == 'X')
5770 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005771 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005773 else
5774 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 nr = 0;
5776 while (--n >= 0 && vim_isxdigit(p[1]))
5777 {
5778 ++p;
5779 nr = (nr << 4) + hex2nr(*p);
5780 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782#ifdef FEAT_MBYTE
5783 /* For "\u" store the number according to
5784 * 'encoding'. */
5785 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 else
5788#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 break;
5792
5793 /* octal: "\1", "\12", "\123" */
5794 case '0':
5795 case '1':
5796 case '2':
5797 case '3':
5798 case '4':
5799 case '5':
5800 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 case '7': *name = *p++ - '0';
5802 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 *name = (*name << 3) + *p++ - '0';
5805 if (*p >= '0' && *p <= '7')
5806 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 break;
5810
5811 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 if (extra != 0)
5814 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 break;
5817 }
5818 /* FALLTHROUGH */
5819
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 break;
5822 }
5823 }
5824 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 *arg = p + 1;
5830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 return OK;
5832}
5833
5834/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 * Return OK or FAIL.
5837 */
5838 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005839get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 int evaluate;
5843{
5844 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005845 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005846 int reduce = 0;
5847
5848 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 */
5851 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5852 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 break;
5857 ++reduce;
5858 ++p;
5859 }
5860 }
5861
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 return FAIL;
5866 }
5867
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 if (!evaluate)
5870 {
5871 *arg = p + 1;
5872 return OK;
5873 }
5874
5875 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 */
5878 str = alloc((unsigned)((p - *arg) - reduce));
5879 if (str == NULL)
5880 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 rettv->v_type = VAR_STRING;
5882 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 break;
5890 ++p;
5891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 *arg = p + 1;
5896
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 return OK;
5898}
5899
5900/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 * Allocate a variable for a List and fill it from "*arg".
5902 * Return OK or FAIL.
5903 */
5904 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005905get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005907 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908 int evaluate;
5909{
Bram Moolenaar33570922005-01-25 22:26:29 +00005910 list_T *l = NULL;
5911 typval_T tv;
5912 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913
5914 if (evaluate)
5915 {
5916 l = list_alloc();
5917 if (l == NULL)
5918 return FAIL;
5919 }
5920
5921 *arg = skipwhite(*arg + 1);
5922 while (**arg != ']' && **arg != NUL)
5923 {
5924 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5925 goto failret;
5926 if (evaluate)
5927 {
5928 item = listitem_alloc();
5929 if (item != NULL)
5930 {
5931 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005932 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 list_append(l, item);
5934 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005935 else
5936 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 }
5938
5939 if (**arg == ']')
5940 break;
5941 if (**arg != ',')
5942 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 goto failret;
5945 }
5946 *arg = skipwhite(*arg + 1);
5947 }
5948
5949 if (**arg != ']')
5950 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005951 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952failret:
5953 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005954 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 return FAIL;
5956 }
5957
5958 *arg = skipwhite(*arg + 1);
5959 if (evaluate)
5960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005961 rettv->v_type = VAR_LIST;
5962 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 ++l->lv_refcount;
5964 }
5965
5966 return OK;
5967}
5968
5969/*
5970 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005971 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005973 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974list_alloc()
5975{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005976 list_T *l;
5977
5978 l = (list_T *)alloc_clear(sizeof(list_T));
5979 if (l != NULL)
5980 {
5981 /* Prepend the list to the list of lists for garbage collection. */
5982 if (first_list != NULL)
5983 first_list->lv_used_prev = l;
5984 l->lv_used_prev = NULL;
5985 l->lv_used_next = first_list;
5986 first_list = l;
5987 }
5988 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989}
5990
5991/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005992 * Allocate an empty list for a return value.
5993 * Returns OK or FAIL.
5994 */
5995 static int
5996rettv_list_alloc(rettv)
5997 typval_T *rettv;
5998{
5999 list_T *l = list_alloc();
6000
6001 if (l == NULL)
6002 return FAIL;
6003
6004 rettv->vval.v_list = l;
6005 rettv->v_type = VAR_LIST;
6006 ++l->lv_refcount;
6007 return OK;
6008}
6009
6010/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 * Unreference a list: decrement the reference count and free it when it
6012 * becomes zero.
6013 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006014 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006016 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006018 if (l != NULL && --l->lv_refcount <= 0)
6019 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020}
6021
6022/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006023 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024 * Ignores the reference count.
6025 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006026 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006027list_free(l, recurse)
6028 list_T *l;
6029 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030{
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006033 /* Remove the list from the list of lists for garbage collection. */
6034 if (l->lv_used_prev == NULL)
6035 first_list = l->lv_used_next;
6036 else
6037 l->lv_used_prev->lv_used_next = l->lv_used_next;
6038 if (l->lv_used_next != NULL)
6039 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6040
Bram Moolenaard9fba312005-06-26 22:34:35 +00006041 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006043 /* Remove the item before deleting it. */
6044 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006045 if (recurse || (item->li_tv.v_type != VAR_LIST
6046 && item->li_tv.v_type != VAR_DICT))
6047 clear_tv(&item->li_tv);
6048 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 }
6050 vim_free(l);
6051}
6052
6053/*
6054 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006055 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006057 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058listitem_alloc()
6059{
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061}
6062
6063/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006064 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006066 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006070 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071 vim_free(item);
6072}
6073
6074/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006075 * Remove a list item from a List and free it. Also clears the value.
6076 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006077 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006078listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006079 list_T *l;
6080 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006081{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006082 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006083 listitem_free(item);
6084}
6085
6086/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087 * Get the number of items in a list.
6088 */
6089 static long
6090list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093 if (l == NULL)
6094 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006095 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096}
6097
6098/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006099 * Return TRUE when two lists have exactly the same values.
6100 */
6101 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006103 list_T *l1;
6104 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006105 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006106 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107{
Bram Moolenaar33570922005-01-25 22:26:29 +00006108 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006110 if (l1 == NULL || l2 == NULL)
6111 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006112 if (l1 == l2)
6113 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006114 if (list_len(l1) != list_len(l2))
6115 return FALSE;
6116
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006117 for (item1 = l1->lv_first, item2 = l2->lv_first;
6118 item1 != NULL && item2 != NULL;
6119 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006120 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 return FALSE;
6122 return item1 == NULL && item2 == NULL;
6123}
6124
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006125#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6126 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006127/*
6128 * Return the dictitem that an entry in a hashtable points to.
6129 */
6130 dictitem_T *
6131dict_lookup(hi)
6132 hashitem_T *hi;
6133{
6134 return HI2DI(hi);
6135}
6136#endif
6137
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006138/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006139 * Return TRUE when two dictionaries have exactly the same key/values.
6140 */
6141 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006142dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 dict_T *d1;
6144 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006145 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006146 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006147{
Bram Moolenaar33570922005-01-25 22:26:29 +00006148 hashitem_T *hi;
6149 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006150 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006151
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006152 if (d1 == NULL || d2 == NULL)
6153 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006154 if (d1 == d2)
6155 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006156 if (dict_len(d1) != dict_len(d2))
6157 return FALSE;
6158
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006159 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006160 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006161 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006162 if (!HASHITEM_EMPTY(hi))
6163 {
6164 item2 = dict_find(d2, hi->hi_key, -1);
6165 if (item2 == NULL)
6166 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006167 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006168 return FALSE;
6169 --todo;
6170 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006171 }
6172 return TRUE;
6173}
6174
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006175static int tv_equal_recurse_limit;
6176
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006177/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006179 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006180 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181 */
6182 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006184 typval_T *tv1;
6185 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006186 int ic; /* ignore case */
6187 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006188{
6189 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006190 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006192 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006193
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006194 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006195 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006196
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006197 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006198 * recursiveness to a limit. We guess they are equal then.
6199 * A fixed limit has the problem of still taking an awful long time.
6200 * Reduce the limit every time running into it. That should work fine for
6201 * deeply linked structures that are not recursively linked and catch
6202 * recursiveness quickly. */
6203 if (!recursive)
6204 tv_equal_recurse_limit = 1000;
6205 if (recursive_cnt >= tv_equal_recurse_limit)
6206 {
6207 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006208 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006209 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006210
6211 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006212 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006213 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006214 ++recursive_cnt;
6215 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6216 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006217 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006218
6219 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006220 ++recursive_cnt;
6221 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6222 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006223 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006224
6225 case VAR_FUNC:
6226 return (tv1->vval.v_string != NULL
6227 && tv2->vval.v_string != NULL
6228 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6229
6230 case VAR_NUMBER:
6231 return tv1->vval.v_number == tv2->vval.v_number;
6232
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006233#ifdef FEAT_FLOAT
6234 case VAR_FLOAT:
6235 return tv1->vval.v_float == tv2->vval.v_float;
6236#endif
6237
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006238 case VAR_STRING:
6239 s1 = get_tv_string_buf(tv1, buf1);
6240 s2 = get_tv_string_buf(tv2, buf2);
6241 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006242 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006243
6244 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245 return TRUE;
6246}
6247
6248/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006249 * Locate item with index "n" in list "l" and return it.
6250 * A negative index is counted from the end; -1 is the last item.
6251 * Returns NULL when "n" is out of range.
6252 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006253 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006254list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006255 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 long n;
6257{
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006259 long idx;
6260
6261 if (l == NULL)
6262 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006263
6264 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006265 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006266 n = l->lv_len + n;
6267
6268 /* Check for index out of range. */
6269 if (n < 0 || n >= l->lv_len)
6270 return NULL;
6271
6272 /* When there is a cached index may start search from there. */
6273 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006274 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006275 if (n < l->lv_idx / 2)
6276 {
6277 /* closest to the start of the list */
6278 item = l->lv_first;
6279 idx = 0;
6280 }
6281 else if (n > (l->lv_idx + l->lv_len) / 2)
6282 {
6283 /* closest to the end of the list */
6284 item = l->lv_last;
6285 idx = l->lv_len - 1;
6286 }
6287 else
6288 {
6289 /* closest to the cached index */
6290 item = l->lv_idx_item;
6291 idx = l->lv_idx;
6292 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293 }
6294 else
6295 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006296 if (n < l->lv_len / 2)
6297 {
6298 /* closest to the start of the list */
6299 item = l->lv_first;
6300 idx = 0;
6301 }
6302 else
6303 {
6304 /* closest to the end of the list */
6305 item = l->lv_last;
6306 idx = l->lv_len - 1;
6307 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006309
6310 while (n > idx)
6311 {
6312 /* search forward */
6313 item = item->li_next;
6314 ++idx;
6315 }
6316 while (n < idx)
6317 {
6318 /* search backward */
6319 item = item->li_prev;
6320 --idx;
6321 }
6322
6323 /* cache the used index */
6324 l->lv_idx = idx;
6325 l->lv_idx_item = item;
6326
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006327 return item;
6328}
6329
6330/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006331 * Get list item "l[idx]" as a number.
6332 */
6333 static long
6334list_find_nr(l, idx, errorp)
6335 list_T *l;
6336 long idx;
6337 int *errorp; /* set to TRUE when something wrong */
6338{
6339 listitem_T *li;
6340
6341 li = list_find(l, idx);
6342 if (li == NULL)
6343 {
6344 if (errorp != NULL)
6345 *errorp = TRUE;
6346 return -1L;
6347 }
6348 return get_tv_number_chk(&li->li_tv, errorp);
6349}
6350
6351/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006352 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6353 */
6354 char_u *
6355list_find_str(l, idx)
6356 list_T *l;
6357 long idx;
6358{
6359 listitem_T *li;
6360
6361 li = list_find(l, idx - 1);
6362 if (li == NULL)
6363 {
6364 EMSGN(_(e_listidx), idx);
6365 return NULL;
6366 }
6367 return get_tv_string(&li->li_tv);
6368}
6369
6370/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006371 * Locate "item" list "l" and return its index.
6372 * Returns -1 when "item" is not in the list.
6373 */
6374 static long
6375list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006376 list_T *l;
6377 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006378{
6379 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006381
6382 if (l == NULL)
6383 return -1;
6384 idx = 0;
6385 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6386 ++idx;
6387 if (li == NULL)
6388 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006389 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006390}
6391
6392/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393 * Append item "item" to the end of list "l".
6394 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006395 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006396list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 list_T *l;
6398 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399{
6400 if (l->lv_last == NULL)
6401 {
6402 /* empty list */
6403 l->lv_first = item;
6404 l->lv_last = item;
6405 item->li_prev = NULL;
6406 }
6407 else
6408 {
6409 l->lv_last->li_next = item;
6410 item->li_prev = l->lv_last;
6411 l->lv_last = item;
6412 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006413 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 item->li_next = NULL;
6415}
6416
6417/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006419 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006421 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 list_T *l;
6424 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006426 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427
Bram Moolenaar05159a02005-02-26 23:04:13 +00006428 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430 copy_tv(tv, &li->li_tv);
6431 list_append(l, li);
6432 return OK;
6433}
6434
6435/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006436 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006437 * Return FAIL when out of memory.
6438 */
6439 int
6440list_append_dict(list, dict)
6441 list_T *list;
6442 dict_T *dict;
6443{
6444 listitem_T *li = listitem_alloc();
6445
6446 if (li == NULL)
6447 return FAIL;
6448 li->li_tv.v_type = VAR_DICT;
6449 li->li_tv.v_lock = 0;
6450 li->li_tv.vval.v_dict = dict;
6451 list_append(list, li);
6452 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006453 return OK;
6454}
6455
6456/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006457 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006458 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006459 * Returns FAIL when out of memory.
6460 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006461 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006462list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006463 list_T *l;
6464 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006465 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006466{
6467 listitem_T *li = listitem_alloc();
6468
6469 if (li == NULL)
6470 return FAIL;
6471 list_append(l, li);
6472 li->li_tv.v_type = VAR_STRING;
6473 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006474 if (str == NULL)
6475 li->li_tv.vval.v_string = NULL;
6476 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006477 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006478 return FAIL;
6479 return OK;
6480}
6481
6482/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006483 * Append "n" to list "l".
6484 * Returns FAIL when out of memory.
6485 */
6486 static int
6487list_append_number(l, n)
6488 list_T *l;
6489 varnumber_T n;
6490{
6491 listitem_T *li;
6492
6493 li = listitem_alloc();
6494 if (li == NULL)
6495 return FAIL;
6496 li->li_tv.v_type = VAR_NUMBER;
6497 li->li_tv.v_lock = 0;
6498 li->li_tv.vval.v_number = n;
6499 list_append(l, li);
6500 return OK;
6501}
6502
6503/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006504 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505 * If "item" is NULL append at the end.
6506 * Return FAIL when out of memory.
6507 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006508 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006510 list_T *l;
6511 typval_T *tv;
6512 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006513{
Bram Moolenaar33570922005-01-25 22:26:29 +00006514 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515
6516 if (ni == NULL)
6517 return FAIL;
6518 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006519 list_insert(l, ni, item);
6520 return OK;
6521}
6522
6523 void
6524list_insert(l, ni, item)
6525 list_T *l;
6526 listitem_T *ni;
6527 listitem_T *item;
6528{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006529 if (item == NULL)
6530 /* Append new item at end of list. */
6531 list_append(l, ni);
6532 else
6533 {
6534 /* Insert new item before existing item. */
6535 ni->li_prev = item->li_prev;
6536 ni->li_next = item;
6537 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006538 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006540 ++l->lv_idx;
6541 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006543 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006545 l->lv_idx_item = NULL;
6546 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006548 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550}
6551
6552/*
6553 * Extend "l1" with "l2".
6554 * If "bef" is NULL append at the end, otherwise insert before this item.
6555 * Returns FAIL when out of memory.
6556 */
6557 static int
6558list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006559 list_T *l1;
6560 list_T *l2;
6561 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562{
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006564 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006566 /* We also quit the loop when we have inserted the original item count of
6567 * the list, avoid a hang when we extend a list with itself. */
6568 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6570 return FAIL;
6571 return OK;
6572}
6573
6574/*
6575 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6576 * Return FAIL when out of memory.
6577 */
6578 static int
6579list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006580 list_T *l1;
6581 list_T *l2;
6582 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583{
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006585
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006586 if (l1 == NULL || l2 == NULL)
6587 return FAIL;
6588
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006590 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006591 if (l == NULL)
6592 return FAIL;
6593 tv->v_type = VAR_LIST;
6594 tv->vval.v_list = l;
6595
6596 /* append all items from the second list */
6597 return list_extend(l, l2, NULL);
6598}
6599
6600/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006601 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006603 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006604 * Returns NULL when out of memory.
6605 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006606 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006608 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611{
Bram Moolenaar33570922005-01-25 22:26:29 +00006612 list_T *copy;
6613 listitem_T *item;
6614 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615
6616 if (orig == NULL)
6617 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618
6619 copy = list_alloc();
6620 if (copy != NULL)
6621 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006622 if (copyID != 0)
6623 {
6624 /* Do this before adding the items, because one of the items may
6625 * refer back to this list. */
6626 orig->lv_copyID = copyID;
6627 orig->lv_copylist = copy;
6628 }
6629 for (item = orig->lv_first; item != NULL && !got_int;
6630 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006631 {
6632 ni = listitem_alloc();
6633 if (ni == NULL)
6634 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006635 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006636 {
6637 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6638 {
6639 vim_free(ni);
6640 break;
6641 }
6642 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006644 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 list_append(copy, ni);
6646 }
6647 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006648 if (item != NULL)
6649 {
6650 list_unref(copy);
6651 copy = NULL;
6652 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006653 }
6654
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006655 return copy;
6656}
6657
6658/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006659 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006660 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006661 * This used to be called list_remove, but that conflicts with a Sun header
6662 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006663 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006664 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006665vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006666 list_T *l;
6667 listitem_T *item;
6668 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006669{
Bram Moolenaar33570922005-01-25 22:26:29 +00006670 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006672 /* notify watchers */
6673 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006674 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006675 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006676 list_fix_watch(l, ip);
6677 if (ip == item2)
6678 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006680
6681 if (item2->li_next == NULL)
6682 l->lv_last = item->li_prev;
6683 else
6684 item2->li_next->li_prev = item->li_prev;
6685 if (item->li_prev == NULL)
6686 l->lv_first = item2->li_next;
6687 else
6688 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006689 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006690}
6691
6692/*
6693 * Return an allocated string with the string representation of a list.
6694 * May return NULL.
6695 */
6696 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006697list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006698 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006699 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700{
6701 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006702
6703 if (tv->vval.v_list == NULL)
6704 return NULL;
6705 ga_init2(&ga, (int)sizeof(char), 80);
6706 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006707 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006708 {
6709 vim_free(ga.ga_data);
6710 return NULL;
6711 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006712 ga_append(&ga, ']');
6713 ga_append(&ga, NUL);
6714 return (char_u *)ga.ga_data;
6715}
6716
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006717typedef struct join_S {
6718 char_u *s;
6719 char_u *tofree;
6720} join_T;
6721
6722 static int
6723list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6724 garray_T *gap; /* to store the result in */
6725 list_T *l;
6726 char_u *sep;
6727 int echo_style;
6728 int copyID;
6729 garray_T *join_gap; /* to keep each list item string */
6730{
6731 int i;
6732 join_T *p;
6733 int len;
6734 int sumlen = 0;
6735 int first = TRUE;
6736 char_u *tofree;
6737 char_u numbuf[NUMBUFLEN];
6738 listitem_T *item;
6739 char_u *s;
6740
6741 /* Stringify each item in the list. */
6742 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6743 {
6744 if (echo_style)
6745 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6746 else
6747 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6748 if (s == NULL)
6749 return FAIL;
6750
6751 len = (int)STRLEN(s);
6752 sumlen += len;
6753
Bram Moolenaarcde88542015-08-11 19:14:00 +02006754 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006755 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6756 if (tofree != NULL || s != numbuf)
6757 {
6758 p->s = s;
6759 p->tofree = tofree;
6760 }
6761 else
6762 {
6763 p->s = vim_strnsave(s, len);
6764 p->tofree = p->s;
6765 }
6766
6767 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006768 if (did_echo_string_emsg) /* recursion error, bail out */
6769 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006770 }
6771
6772 /* Allocate result buffer with its total size, avoid re-allocation and
6773 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6774 if (join_gap->ga_len >= 2)
6775 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6776 if (ga_grow(gap, sumlen + 2) == FAIL)
6777 return FAIL;
6778
6779 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6780 {
6781 if (first)
6782 first = FALSE;
6783 else
6784 ga_concat(gap, sep);
6785 p = ((join_T *)join_gap->ga_data) + i;
6786
6787 if (p->s != NULL)
6788 ga_concat(gap, p->s);
6789 line_breakcheck();
6790 }
6791
6792 return OK;
6793}
6794
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006795/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006796 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006797 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006798 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006799 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006800 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006801list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006802 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006803 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006804 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006805 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006806 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006807{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006808 garray_T join_ga;
6809 int retval;
6810 join_T *p;
6811 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006812
Bram Moolenaard39a7512015-04-16 22:51:22 +02006813 if (l->lv_len < 1)
6814 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006815 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6816 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6817
6818 /* Dispose each item in join_ga. */
6819 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006820 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006821 p = (join_T *)join_ga.ga_data;
6822 for (i = 0; i < join_ga.ga_len; ++i)
6823 {
6824 vim_free(p->tofree);
6825 ++p;
6826 }
6827 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006828 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006829
6830 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006831}
6832
6833/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 * Garbage collection for lists and dictionaries.
6835 *
6836 * We use reference counts to be able to free most items right away when they
6837 * are no longer used. But for composite items it's possible that it becomes
6838 * unused while the reference count is > 0: When there is a recursive
6839 * reference. Example:
6840 * :let l = [1, 2, 3]
6841 * :let d = {9: l}
6842 * :let l[1] = d
6843 *
6844 * Since this is quite unusual we handle this with garbage collection: every
6845 * once in a while find out which lists and dicts are not referenced from any
6846 * variable.
6847 *
6848 * Here is a good reference text about garbage collection (refers to Python
6849 * but it applies to all reference-counting mechanisms):
6850 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006851 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006852
6853/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854 * Do garbage collection for lists and dicts.
6855 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006856 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006857 int
6858garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006859{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006861 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862 buf_T *buf;
6863 win_T *wp;
6864 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006865 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006866 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006867 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006868#ifdef FEAT_WINDOWS
6869 tabpage_T *tp;
6870#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006871
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006872 /* Only do this once. */
6873 want_garbage_collect = FALSE;
6874 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006875 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006876
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006877 /* We advance by two because we add one for items referenced through
6878 * previous_funccal. */
6879 current_copyID += COPYID_INC;
6880 copyID = current_copyID;
6881
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006882 /*
6883 * 1. Go through all accessible variables and mark all lists and dicts
6884 * with copyID.
6885 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006886
6887 /* Don't free variables in the previous_funccal list unless they are only
6888 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006889 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006890 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6891 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6893 NULL);
6894 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6895 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006896 }
6897
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 /* script-local variables */
6899 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006900 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901
6902 /* buffer-local variables */
6903 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6905 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006906
6907 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006908 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006909 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6910 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006911#ifdef FEAT_AUTOCMD
6912 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6914 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006915#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006917#ifdef FEAT_WINDOWS
6918 /* tabpage-local variables */
6919 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006920 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6921 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006922#endif
6923
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006926
6927 /* function-local variables */
6928 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6929 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6931 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006932 }
6933
Bram Moolenaard812df62008-11-09 12:46:09 +00006934 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006936
Bram Moolenaar1dced572012-04-05 16:54:08 +02006937#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006938 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006939#endif
6940
Bram Moolenaardb913952012-06-29 12:54:53 +02006941#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006943#endif
6944
6945#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006947#endif
6948
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006950 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006951 /*
6952 * 2. Free lists and dictionaries that are not referenced.
6953 */
6954 did_free = free_unref_items(copyID);
6955
6956 /*
6957 * 3. Check if any funccal can be freed now.
6958 */
6959 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006960 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006961 if (can_free_funccal(*pfc, copyID))
6962 {
6963 fc = *pfc;
6964 *pfc = fc->caller;
6965 free_funccal(fc, TRUE);
6966 did_free = TRUE;
6967 did_free_funccal = TRUE;
6968 }
6969 else
6970 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006972 if (did_free_funccal)
6973 /* When a funccal was freed some more items might be garbage
6974 * collected, so run again. */
6975 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006977 else if (p_verbose > 0)
6978 {
6979 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6980 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981
6982 return did_free;
6983}
6984
6985/*
6986 * Free lists and dictionaries that are no longer referenced.
6987 */
6988 static int
6989free_unref_items(copyID)
6990 int copyID;
6991{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006992 dict_T *dd, *dd_next;
6993 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006994 int did_free = FALSE;
6995
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 */
6999 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007000 {
7001 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007002 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007004 /* Free the Dictionary and ordinary items it contains, but don't
7005 * recurse into Lists and Dictionaries, they will be in the list
7006 * of dicts or list of lists. */
7007 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007010 dd = dd_next;
7011 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012
7013 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007014 * Go through the list of lists and free items without the copyID.
7015 * But don't free a list that has a watcher (used in a for loop), these
7016 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 */
7018 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007019 {
7020 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007021 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7022 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007024 /* Free the List and ordinary items it contains, but don't recurse
7025 * into Lists and Dictionaries, they will be in the list of dicts
7026 * or list of lists. */
7027 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007030 ll = ll_next;
7031 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 return did_free;
7033}
7034
7035/*
7036 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007037 * "list_stack" is used to add lists to be marked. Can be NULL.
7038 *
7039 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041 int
7042set_ref_in_ht(ht, copyID, list_stack)
7043 hashtab_T *ht;
7044 int copyID;
7045 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046{
7047 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007049 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007050 hashtab_T *cur_ht;
7051 ht_stack_T *ht_stack = NULL;
7052 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054 cur_ht = ht;
7055 for (;;)
7056 {
7057 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007059 /* Mark each item in the hashtab. If the item contains a hashtab
7060 * it is added to ht_stack, if it contains a list it is added to
7061 * list_stack. */
7062 todo = (int)cur_ht->ht_used;
7063 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7064 if (!HASHITEM_EMPTY(hi))
7065 {
7066 --todo;
7067 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7068 &ht_stack, list_stack);
7069 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007071
7072 if (ht_stack == NULL)
7073 break;
7074
7075 /* take an item from the stack */
7076 cur_ht = ht_stack->ht;
7077 tempitem = ht_stack;
7078 ht_stack = ht_stack->prev;
7079 free(tempitem);
7080 }
7081
7082 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007083}
7084
7085/*
7086 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007087 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7088 *
7089 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091 int
7092set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007093 list_T *l;
7094 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007095 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007096{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007097 listitem_T *li;
7098 int abort = FALSE;
7099 list_T *cur_l;
7100 list_stack_T *list_stack = NULL;
7101 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007103 cur_l = l;
7104 for (;;)
7105 {
7106 if (!abort)
7107 /* Mark each item in the list. If the item contains a hashtab
7108 * it is added to ht_stack, if it contains a list it is added to
7109 * list_stack. */
7110 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7111 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7112 ht_stack, &list_stack);
7113 if (list_stack == NULL)
7114 break;
7115
7116 /* take an item from the stack */
7117 cur_l = list_stack->list;
7118 tempitem = list_stack;
7119 list_stack = list_stack->prev;
7120 free(tempitem);
7121 }
7122
7123 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007124}
7125
7126/*
7127 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007128 * "list_stack" is used to add lists to be marked. Can be NULL.
7129 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7130 *
7131 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007132 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007133 int
7134set_ref_in_item(tv, copyID, ht_stack, list_stack)
7135 typval_T *tv;
7136 int copyID;
7137 ht_stack_T **ht_stack;
7138 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007139{
7140 dict_T *dd;
7141 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007142 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007143
7144 switch (tv->v_type)
7145 {
7146 case VAR_DICT:
7147 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007148 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007149 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007150 /* Didn't see this dict yet. */
7151 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007152 if (ht_stack == NULL)
7153 {
7154 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7155 }
7156 else
7157 {
7158 ht_stack_T *newitem = (ht_stack_T*)malloc(
7159 sizeof(ht_stack_T));
7160 if (newitem == NULL)
7161 abort = TRUE;
7162 else
7163 {
7164 newitem->ht = &dd->dv_hashtab;
7165 newitem->prev = *ht_stack;
7166 *ht_stack = newitem;
7167 }
7168 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007169 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007170 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007171
7172 case VAR_LIST:
7173 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007174 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007175 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007176 /* Didn't see this list yet. */
7177 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007178 if (list_stack == NULL)
7179 {
7180 abort = set_ref_in_list(ll, copyID, ht_stack);
7181 }
7182 else
7183 {
7184 list_stack_T *newitem = (list_stack_T*)malloc(
7185 sizeof(list_stack_T));
7186 if (newitem == NULL)
7187 abort = TRUE;
7188 else
7189 {
7190 newitem->list = ll;
7191 newitem->prev = *list_stack;
7192 *list_stack = newitem;
7193 }
7194 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007195 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007196 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007197 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007198 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007199}
7200
7201/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007202 * Allocate an empty header for a dictionary.
7203 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007204 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205dict_alloc()
7206{
Bram Moolenaar33570922005-01-25 22:26:29 +00007207 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208
Bram Moolenaar33570922005-01-25 22:26:29 +00007209 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007210 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007211 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007212 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007213 if (first_dict != NULL)
7214 first_dict->dv_used_prev = d;
7215 d->dv_used_next = first_dict;
7216 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007217 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007218
Bram Moolenaar33570922005-01-25 22:26:29 +00007219 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007220 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007221 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007222 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007223 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007224 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007225 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007226}
7227
7228/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007229 * Allocate an empty dict for a return value.
7230 * Returns OK or FAIL.
7231 */
7232 static int
7233rettv_dict_alloc(rettv)
7234 typval_T *rettv;
7235{
7236 dict_T *d = dict_alloc();
7237
7238 if (d == NULL)
7239 return FAIL;
7240
7241 rettv->vval.v_dict = d;
7242 rettv->v_type = VAR_DICT;
7243 ++d->dv_refcount;
7244 return OK;
7245}
7246
7247
7248/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007249 * Unreference a Dictionary: decrement the reference count and free it when it
7250 * becomes zero.
7251 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007252 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007256 if (d != NULL && --d->dv_refcount <= 0)
7257 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258}
7259
7260/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007261 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262 * Ignores the reference count.
7263 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007264 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007265dict_free(d, recurse)
7266 dict_T *d;
7267 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007270 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007271 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007273 /* Remove the dict from the list of dicts for garbage collection. */
7274 if (d->dv_used_prev == NULL)
7275 first_dict = d->dv_used_next;
7276 else
7277 d->dv_used_prev->dv_used_next = d->dv_used_next;
7278 if (d->dv_used_next != NULL)
7279 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7280
7281 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007282 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007283 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007285 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 if (!HASHITEM_EMPTY(hi))
7287 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007288 /* Remove the item before deleting it, just in case there is
7289 * something recursive causing trouble. */
7290 di = HI2DI(hi);
7291 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007292 if (recurse || (di->di_tv.v_type != VAR_LIST
7293 && di->di_tv.v_type != VAR_DICT))
7294 clear_tv(&di->di_tv);
7295 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007296 --todo;
7297 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300 vim_free(d);
7301}
7302
7303/*
7304 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007305 * The "key" is copied to the new item.
7306 * Note that the value of the item "di_tv" still needs to be initialized!
7307 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007309 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310dictitem_alloc(key)
7311 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312{
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007315 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007317 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007319 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007320 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322}
7323
7324/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325 * Make a copy of a Dictionary item.
7326 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007328dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007330{
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007333 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7334 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335 if (di != NULL)
7336 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007337 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007338 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 copy_tv(&org->di_tv, &di->di_tv);
7340 }
7341 return di;
7342}
7343
7344/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 * Remove item "item" from Dictionary "dict" and free it.
7346 */
7347 static void
7348dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 dict_T *dict;
7350 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351{
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355 if (HASHITEM_EMPTY(hi))
7356 EMSG2(_(e_intern2), "dictitem_remove()");
7357 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 dictitem_free(item);
7360}
7361
7362/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007363 * Free a dict item. Also clears the value.
7364 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007365 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007367 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007368{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007370 if (item->di_flags & DI_FLAGS_ALLOC)
7371 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007372}
7373
7374/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7376 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007377 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378 * Returns NULL when out of memory.
7379 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007381dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007382 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007384 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007385{
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 dict_T *copy;
7387 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007388 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390
7391 if (orig == NULL)
7392 return NULL;
7393
7394 copy = dict_alloc();
7395 if (copy != NULL)
7396 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007397 if (copyID != 0)
7398 {
7399 orig->dv_copyID = copyID;
7400 orig->dv_copydict = copy;
7401 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007402 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007403 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007404 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007405 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007406 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007407 --todo;
7408
7409 di = dictitem_alloc(hi->hi_key);
7410 if (di == NULL)
7411 break;
7412 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007413 {
7414 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7415 copyID) == FAIL)
7416 {
7417 vim_free(di);
7418 break;
7419 }
7420 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007421 else
7422 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7423 if (dict_add(copy, di) == FAIL)
7424 {
7425 dictitem_free(di);
7426 break;
7427 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007428 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007429 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007430
Bram Moolenaare9a41262005-01-15 22:18:47 +00007431 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007432 if (todo > 0)
7433 {
7434 dict_unref(copy);
7435 copy = NULL;
7436 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007437 }
7438
7439 return copy;
7440}
7441
7442/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007443 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007444 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007445 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007446 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 dict_T *d;
7449 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007450{
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452}
7453
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007455 * Add a number or string entry to dictionary "d".
7456 * When "str" is NULL use number "nr", otherwise use "str".
7457 * Returns FAIL when out of memory and when key already exists.
7458 */
7459 int
7460dict_add_nr_str(d, key, nr, str)
7461 dict_T *d;
7462 char *key;
7463 long nr;
7464 char_u *str;
7465{
7466 dictitem_T *item;
7467
7468 item = dictitem_alloc((char_u *)key);
7469 if (item == NULL)
7470 return FAIL;
7471 item->di_tv.v_lock = 0;
7472 if (str == NULL)
7473 {
7474 item->di_tv.v_type = VAR_NUMBER;
7475 item->di_tv.vval.v_number = nr;
7476 }
7477 else
7478 {
7479 item->di_tv.v_type = VAR_STRING;
7480 item->di_tv.vval.v_string = vim_strsave(str);
7481 }
7482 if (dict_add(d, item) == FAIL)
7483 {
7484 dictitem_free(item);
7485 return FAIL;
7486 }
7487 return OK;
7488}
7489
7490/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007491 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007492 * Returns FAIL when out of memory and when key already exists.
7493 */
7494 int
7495dict_add_list(d, key, list)
7496 dict_T *d;
7497 char *key;
7498 list_T *list;
7499{
7500 dictitem_T *item;
7501
7502 item = dictitem_alloc((char_u *)key);
7503 if (item == NULL)
7504 return FAIL;
7505 item->di_tv.v_lock = 0;
7506 item->di_tv.v_type = VAR_LIST;
7507 item->di_tv.vval.v_list = list;
7508 if (dict_add(d, item) == FAIL)
7509 {
7510 dictitem_free(item);
7511 return FAIL;
7512 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007513 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007514 return OK;
7515}
7516
7517/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007518 * Get the number of items in a Dictionary.
7519 */
7520 static long
7521dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007523{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007524 if (d == NULL)
7525 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007526 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007527}
7528
7529/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 * Find item "key[len]" in Dictionary "d".
7531 * If "len" is negative use strlen(key).
7532 * Returns NULL when not found.
7533 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007534 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007536 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 char_u *key;
7538 int len;
7539{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007540#define AKEYLEN 200
7541 char_u buf[AKEYLEN];
7542 char_u *akey;
7543 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007546 if (len < 0)
7547 akey = key;
7548 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007549 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550 tofree = akey = vim_strnsave(key, len);
7551 if (akey == NULL)
7552 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007553 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 else
7555 {
7556 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007557 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007558 akey = buf;
7559 }
7560
Bram Moolenaar33570922005-01-25 22:26:29 +00007561 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007562 vim_free(tofree);
7563 if (HASHITEM_EMPTY(hi))
7564 return NULL;
7565 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007566}
7567
7568/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007569 * Get a string item from a dictionary.
7570 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007571 * Returns NULL if the entry doesn't exist or out of memory.
7572 */
7573 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007574get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007575 dict_T *d;
7576 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007577 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007578{
7579 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007580 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007581
7582 di = dict_find(d, key, -1);
7583 if (di == NULL)
7584 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007585 s = get_tv_string(&di->di_tv);
7586 if (save && s != NULL)
7587 s = vim_strsave(s);
7588 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007589}
7590
7591/*
7592 * Get a number item from a dictionary.
7593 * Returns 0 if the entry doesn't exist or out of memory.
7594 */
7595 long
7596get_dict_number(d, key)
7597 dict_T *d;
7598 char_u *key;
7599{
7600 dictitem_T *di;
7601
7602 di = dict_find(d, key, -1);
7603 if (di == NULL)
7604 return 0;
7605 return get_tv_number(&di->di_tv);
7606}
7607
7608/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609 * Return an allocated string with the string representation of a Dictionary.
7610 * May return NULL.
7611 */
7612 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007613dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007614 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007615 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007616{
7617 garray_T ga;
7618 int first = TRUE;
7619 char_u *tofree;
7620 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007623 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007624 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007626 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007627 return NULL;
7628 ga_init2(&ga, (int)sizeof(char), 80);
7629 ga_append(&ga, '{');
7630
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007631 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007632 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007634 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007636 --todo;
7637
7638 if (first)
7639 first = FALSE;
7640 else
7641 ga_concat(&ga, (char_u *)", ");
7642
7643 tofree = string_quote(hi->hi_key, FALSE);
7644 if (tofree != NULL)
7645 {
7646 ga_concat(&ga, tofree);
7647 vim_free(tofree);
7648 }
7649 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007650 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007651 if (s != NULL)
7652 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007654 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007655 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007656 line_breakcheck();
7657
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007660 if (todo > 0)
7661 {
7662 vim_free(ga.ga_data);
7663 return NULL;
7664 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665
7666 ga_append(&ga, '}');
7667 ga_append(&ga, NUL);
7668 return (char_u *)ga.ga_data;
7669}
7670
7671/*
7672 * Allocate a variable for a Dictionary and fill it from "*arg".
7673 * Return OK or FAIL. Returns NOTDONE for {expr}.
7674 */
7675 static int
7676get_dict_tv(arg, rettv, evaluate)
7677 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679 int evaluate;
7680{
Bram Moolenaar33570922005-01-25 22:26:29 +00007681 dict_T *d = NULL;
7682 typval_T tvkey;
7683 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007684 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007687 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688
7689 /*
7690 * First check if it's not a curly-braces thing: {expr}.
7691 * Must do this without evaluating, otherwise a function may be called
7692 * twice. Unfortunately this means we need to call eval1() twice for the
7693 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007694 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007696 if (*start != '}')
7697 {
7698 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7699 return FAIL;
7700 if (*start == '}')
7701 return NOTDONE;
7702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703
7704 if (evaluate)
7705 {
7706 d = dict_alloc();
7707 if (d == NULL)
7708 return FAIL;
7709 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007710 tvkey.v_type = VAR_UNKNOWN;
7711 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712
7713 *arg = skipwhite(*arg + 1);
7714 while (**arg != '}' && **arg != NUL)
7715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007716 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 goto failret;
7718 if (**arg != ':')
7719 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007720 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007721 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 goto failret;
7723 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007724 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007726 key = get_tv_string_buf_chk(&tvkey, buf);
7727 if (key == NULL || *key == NUL)
7728 {
7729 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7730 if (key != NULL)
7731 EMSG(_(e_emptykey));
7732 clear_tv(&tvkey);
7733 goto failret;
7734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007736
7737 *arg = skipwhite(*arg + 1);
7738 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7739 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007740 if (evaluate)
7741 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007742 goto failret;
7743 }
7744 if (evaluate)
7745 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007746 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007747 if (item != NULL)
7748 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007749 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007750 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007751 clear_tv(&tv);
7752 goto failret;
7753 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007754 item = dictitem_alloc(key);
7755 clear_tv(&tvkey);
7756 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007759 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007760 if (dict_add(d, item) == FAIL)
7761 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 }
7763 }
7764
7765 if (**arg == '}')
7766 break;
7767 if (**arg != ',')
7768 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007769 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007770 goto failret;
7771 }
7772 *arg = skipwhite(*arg + 1);
7773 }
7774
7775 if (**arg != '}')
7776 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007777 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007778failret:
7779 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007780 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007781 return FAIL;
7782 }
7783
7784 *arg = skipwhite(*arg + 1);
7785 if (evaluate)
7786 {
7787 rettv->v_type = VAR_DICT;
7788 rettv->vval.v_dict = d;
7789 ++d->dv_refcount;
7790 }
7791
7792 return OK;
7793}
7794
Bram Moolenaar8c711452005-01-14 21:53:12 +00007795/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007796 * Return a string with the string representation of a variable.
7797 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007798 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007799 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007800 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007801 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007802 */
7803 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007804echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007805 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007806 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007807 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007808 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007809{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007810 static int recurse = 0;
7811 char_u *r = NULL;
7812
Bram Moolenaar33570922005-01-25 22:26:29 +00007813 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007814 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007815 if (!did_echo_string_emsg)
7816 {
7817 /* Only give this message once for a recursive call to avoid
7818 * flooding the user with errors. And stop iterating over lists
7819 * and dicts. */
7820 did_echo_string_emsg = TRUE;
7821 EMSG(_("E724: variable nested too deep for displaying"));
7822 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007823 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007824 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007825 }
7826 ++recurse;
7827
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007828 switch (tv->v_type)
7829 {
7830 case VAR_FUNC:
7831 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007832 r = tv->vval.v_string;
7833 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007834
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007835 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007836 if (tv->vval.v_list == NULL)
7837 {
7838 *tofree = NULL;
7839 r = NULL;
7840 }
7841 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7842 {
7843 *tofree = NULL;
7844 r = (char_u *)"[...]";
7845 }
7846 else
7847 {
7848 tv->vval.v_list->lv_copyID = copyID;
7849 *tofree = list2string(tv, copyID);
7850 r = *tofree;
7851 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007852 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007853
Bram Moolenaar8c711452005-01-14 21:53:12 +00007854 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007855 if (tv->vval.v_dict == NULL)
7856 {
7857 *tofree = NULL;
7858 r = NULL;
7859 }
7860 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7861 {
7862 *tofree = NULL;
7863 r = (char_u *)"{...}";
7864 }
7865 else
7866 {
7867 tv->vval.v_dict->dv_copyID = copyID;
7868 *tofree = dict2string(tv, copyID);
7869 r = *tofree;
7870 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007871 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007872
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007873 case VAR_STRING:
7874 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007875 *tofree = NULL;
7876 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007877 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007878
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007879#ifdef FEAT_FLOAT
7880 case VAR_FLOAT:
7881 *tofree = NULL;
7882 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7883 r = numbuf;
7884 break;
7885#endif
7886
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007887 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007888 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007889 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007890 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007891
Bram Moolenaar8502c702014-06-17 12:51:16 +02007892 if (--recurse == 0)
7893 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007894 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007895}
7896
7897/*
7898 * Return a string with the string representation of a variable.
7899 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7900 * "numbuf" is used for a number.
7901 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007902 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007903 */
7904 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007905tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007906 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007907 char_u **tofree;
7908 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007909 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007910{
7911 switch (tv->v_type)
7912 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007913 case VAR_FUNC:
7914 *tofree = string_quote(tv->vval.v_string, TRUE);
7915 return *tofree;
7916 case VAR_STRING:
7917 *tofree = string_quote(tv->vval.v_string, FALSE);
7918 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007919#ifdef FEAT_FLOAT
7920 case VAR_FLOAT:
7921 *tofree = NULL;
7922 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7923 return numbuf;
7924#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007925 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007926 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007927 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007928 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007929 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007931 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007932 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007933}
7934
7935/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007936 * Return string "str" in ' quotes, doubling ' characters.
7937 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007938 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007939 */
7940 static char_u *
7941string_quote(str, function)
7942 char_u *str;
7943 int function;
7944{
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007946 char_u *p, *r, *s;
7947
Bram Moolenaar33570922005-01-25 22:26:29 +00007948 len = (function ? 13 : 3);
7949 if (str != NULL)
7950 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007951 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 for (p = str; *p != NUL; mb_ptr_adv(p))
7953 if (*p == '\'')
7954 ++len;
7955 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007956 s = r = alloc(len);
7957 if (r != NULL)
7958 {
7959 if (function)
7960 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007961 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007962 r += 10;
7963 }
7964 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007965 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 if (str != NULL)
7967 for (p = str; *p != NUL; )
7968 {
7969 if (*p == '\'')
7970 *r++ = '\'';
7971 MB_COPY_CHAR(p, r);
7972 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007973 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007974 if (function)
7975 *r++ = ')';
7976 *r++ = NUL;
7977 }
7978 return s;
7979}
7980
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007981#ifdef FEAT_FLOAT
7982/*
7983 * Convert the string "text" to a floating point number.
7984 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7985 * this always uses a decimal point.
7986 * Returns the length of the text that was consumed.
7987 */
7988 static int
7989string2float(text, value)
7990 char_u *text;
7991 float_T *value; /* result stored here */
7992{
7993 char *s = (char *)text;
7994 float_T f;
7995
7996 f = strtod(s, &s);
7997 *value = f;
7998 return (int)((char_u *)s - text);
7999}
8000#endif
8001
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 * Get the value of an environment variable.
8004 * "arg" is pointing to the '$'. It is advanced to after the name.
8005 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008006 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 */
8008 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008009get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 int evaluate;
8013{
8014 char_u *string = NULL;
8015 int len;
8016 int cc;
8017 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008018 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
8020 ++*arg;
8021 name = *arg;
8022 len = get_env_len(arg);
8023 if (evaluate)
8024 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008025 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008026 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008027
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008028 cc = name[len];
8029 name[len] = NUL;
8030 /* first try vim_getenv(), fast for normal environment vars */
8031 string = vim_getenv(name, &mustfree);
8032 if (string != NULL && *string != NUL)
8033 {
8034 if (!mustfree)
8035 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008037 else
8038 {
8039 if (mustfree)
8040 vim_free(string);
8041
8042 /* next try expanding things like $VIM and ${HOME} */
8043 string = expand_env_save(name - 1);
8044 if (string != NULL && *string == '$')
8045 {
8046 vim_free(string);
8047 string = NULL;
8048 }
8049 }
8050 name[len] = cc;
8051
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008052 rettv->v_type = VAR_STRING;
8053 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 }
8055
8056 return OK;
8057}
8058
8059/*
8060 * Array with names and number of arguments of all internal functions
8061 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8062 */
8063static struct fst
8064{
8065 char *f_name; /* function name */
8066 char f_min_argc; /* minimal number of arguments */
8067 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008068 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008069 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070} functions[] =
8071{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008072#ifdef FEAT_FLOAT
8073 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008074 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008076 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008077 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008078 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {"append", 2, 2, f_append},
8080 {"argc", 0, 0, f_argc},
8081 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008082 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008083 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008084#ifdef FEAT_FLOAT
8085 {"asin", 1, 1, f_asin}, /* WJMc */
8086#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008087 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008088 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008089 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008090 {"assert_false", 1, 2, f_assert_false},
8091 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008092#ifdef FEAT_FLOAT
8093 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008094 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008097 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"bufexists", 1, 1, f_bufexists},
8099 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8100 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8101 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8102 {"buflisted", 1, 1, f_buflisted},
8103 {"bufloaded", 1, 1, f_bufloaded},
8104 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008105 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"bufwinnr", 1, 1, f_bufwinnr},
8107 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008108 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008109 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008110 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008111#ifdef FEAT_FLOAT
8112 {"ceil", 1, 1, f_ceil},
8113#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008114 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008115 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008117 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008119#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008120 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008121 {"complete_add", 1, 1, f_complete_add},
8122 {"complete_check", 0, 0, f_complete_check},
8123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008125 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008126#ifdef FEAT_FLOAT
8127 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008128 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008129#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008130 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008132 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008133 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008134 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008136 {"diff_filler", 1, 1, f_diff_filler},
8137 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008138 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008140 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"eventhandler", 0, 0, f_eventhandler},
8142 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008143 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008145#ifdef FEAT_FLOAT
8146 {"exp", 1, 1, f_exp},
8147#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008148 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008149 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008150 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8152 {"filereadable", 1, 1, f_filereadable},
8153 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008154 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008155 {"finddir", 1, 3, f_finddir},
8156 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008157#ifdef FEAT_FLOAT
8158 {"float2nr", 1, 1, f_float2nr},
8159 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008160 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008161#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008162 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"fnamemodify", 2, 2, f_fnamemodify},
8164 {"foldclosed", 1, 1, f_foldclosed},
8165 {"foldclosedend", 1, 1, f_foldclosedend},
8166 {"foldlevel", 1, 1, f_foldlevel},
8167 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008168 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008170 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008171 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008172 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008173 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008174 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"getchar", 0, 1, f_getchar},
8176 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008177 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"getcmdline", 0, 0, f_getcmdline},
8179 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008180 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008181 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008182 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008184 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008185 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"getfsize", 1, 1, f_getfsize},
8187 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008188 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008189 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008190 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008191 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008192 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008193 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008194 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008195 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008197 {"gettabvar", 2, 3, f_gettabvar},
8198 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"getwinposx", 0, 0, f_getwinposx},
8200 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008201 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008202 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008203 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008204 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008206 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008207 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008208 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8210 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8211 {"histadd", 2, 2, f_histadd},
8212 {"histdel", 1, 2, f_histdel},
8213 {"histget", 1, 2, f_histget},
8214 {"histnr", 1, 1, f_histnr},
8215 {"hlID", 1, 1, f_hlID},
8216 {"hlexists", 1, 1, f_hlexists},
8217 {"hostname", 0, 0, f_hostname},
8218 {"iconv", 3, 3, f_iconv},
8219 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008220 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008221 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008223 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {"inputrestore", 0, 0, f_inputrestore},
8225 {"inputsave", 0, 0, f_inputsave},
8226 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008227 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008228 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008230 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008231 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008232 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008233 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008235 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {"libcall", 3, 3, f_libcall},
8237 {"libcallnr", 3, 3, f_libcallnr},
8238 {"line", 1, 1, f_line},
8239 {"line2byte", 1, 1, f_line2byte},
8240 {"lispindent", 1, 1, f_lispindent},
8241 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008242#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008243 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008244 {"log10", 1, 1, f_log10},
8245#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008246#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008247 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008248#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008249 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008250 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008251 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008252 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008253 {"matchadd", 2, 5, f_matchadd},
8254 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008255 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008256 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008257 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008258 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008259 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008260 {"max", 1, 1, f_max},
8261 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008262#ifdef vim_mkdir
8263 {"mkdir", 1, 3, f_mkdir},
8264#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008265 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008266#ifdef FEAT_MZSCHEME
8267 {"mzeval", 1, 1, f_mzeval},
8268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008270 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008271 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008272 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008273#ifdef FEAT_FLOAT
8274 {"pow", 2, 2, f_pow},
8275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008277 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008278 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008279#ifdef FEAT_PYTHON3
8280 {"py3eval", 1, 1, f_py3eval},
8281#endif
8282#ifdef FEAT_PYTHON
8283 {"pyeval", 1, 1, f_pyeval},
8284#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008285 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008286 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008287 {"reltime", 0, 2, f_reltime},
8288 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {"remote_expr", 2, 3, f_remote_expr},
8290 {"remote_foreground", 1, 1, f_remote_foreground},
8291 {"remote_peek", 1, 2, f_remote_peek},
8292 {"remote_read", 1, 1, f_remote_read},
8293 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008294 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008296 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008298 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008299#ifdef FEAT_FLOAT
8300 {"round", 1, 1, f_round},
8301#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008302 {"screenattr", 2, 2, f_screenattr},
8303 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008304 {"screencol", 0, 0, f_screencol},
8305 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008306 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008307 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008308 {"searchpair", 3, 7, f_searchpair},
8309 {"searchpairpos", 3, 7, f_searchpairpos},
8310 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"server2client", 2, 2, f_server2client},
8312 {"serverlist", 0, 0, f_serverlist},
8313 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008314 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {"setcmdpos", 1, 1, f_setcmdpos},
8316 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008317 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008318 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008319 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008320 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008322 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008323 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008325#ifdef FEAT_CRYPT
8326 {"sha256", 1, 1, f_sha256},
8327#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008328 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008329 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008331#ifdef FEAT_FLOAT
8332 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008333 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008334#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008335 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008336 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008337 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008338 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008339 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008340#ifdef FEAT_FLOAT
8341 {"sqrt", 1, 1, f_sqrt},
8342 {"str2float", 1, 1, f_str2float},
8343#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008344 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008345 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008346 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347#ifdef HAVE_STRFTIME
8348 {"strftime", 1, 2, f_strftime},
8349#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008350 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008351 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"strlen", 1, 1, f_strlen},
8353 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008354 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008356 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008357 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"substitute", 4, 4, f_substitute},
8359 {"synID", 3, 3, f_synID},
8360 {"synIDattr", 2, 3, f_synIDattr},
8361 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008362 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008363 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008364 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008365 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008366 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008367 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008368 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008369 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008370 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008371#ifdef FEAT_FLOAT
8372 {"tan", 1, 1, f_tan},
8373 {"tanh", 1, 1, f_tanh},
8374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008376 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {"tolower", 1, 1, f_tolower},
8378 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008379 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008380#ifdef FEAT_FLOAT
8381 {"trunc", 1, 1, f_trunc},
8382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008384 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008385 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008386 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008387 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {"virtcol", 1, 1, f_virtcol},
8389 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008390 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 {"winbufnr", 1, 1, f_winbufnr},
8392 {"wincol", 0, 0, f_wincol},
8393 {"winheight", 1, 1, f_winheight},
8394 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008395 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008397 {"winrestview", 1, 1, f_winrestview},
8398 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008400 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008401 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008402 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403};
8404
8405#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8406
8407/*
8408 * Function given to ExpandGeneric() to obtain the list of internal
8409 * or user defined function names.
8410 */
8411 char_u *
8412get_function_name(xp, idx)
8413 expand_T *xp;
8414 int idx;
8415{
8416 static int intidx = -1;
8417 char_u *name;
8418
8419 if (idx == 0)
8420 intidx = -1;
8421 if (intidx < 0)
8422 {
8423 name = get_user_func_name(xp, idx);
8424 if (name != NULL)
8425 return name;
8426 }
8427 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8428 {
8429 STRCPY(IObuff, functions[intidx].f_name);
8430 STRCAT(IObuff, "(");
8431 if (functions[intidx].f_max_argc == 0)
8432 STRCAT(IObuff, ")");
8433 return IObuff;
8434 }
8435
8436 return NULL;
8437}
8438
8439/*
8440 * Function given to ExpandGeneric() to obtain the list of internal or
8441 * user defined variable or function names.
8442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 char_u *
8444get_expr_name(xp, idx)
8445 expand_T *xp;
8446 int idx;
8447{
8448 static int intidx = -1;
8449 char_u *name;
8450
8451 if (idx == 0)
8452 intidx = -1;
8453 if (intidx < 0)
8454 {
8455 name = get_function_name(xp, idx);
8456 if (name != NULL)
8457 return name;
8458 }
8459 return get_user_var_name(xp, ++intidx);
8460}
8461
8462#endif /* FEAT_CMDL_COMPL */
8463
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008464#if defined(EBCDIC) || defined(PROTO)
8465/*
8466 * Compare struct fst by function name.
8467 */
8468 static int
8469compare_func_name(s1, s2)
8470 const void *s1;
8471 const void *s2;
8472{
8473 struct fst *p1 = (struct fst *)s1;
8474 struct fst *p2 = (struct fst *)s2;
8475
8476 return STRCMP(p1->f_name, p2->f_name);
8477}
8478
8479/*
8480 * Sort the function table by function name.
8481 * The sorting of the table above is ASCII dependant.
8482 * On machines using EBCDIC we have to sort it.
8483 */
8484 static void
8485sortFunctions()
8486{
8487 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8488
8489 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8490}
8491#endif
8492
8493
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494/*
8495 * Find internal function in table above.
8496 * Return index, or -1 if not found
8497 */
8498 static int
8499find_internal_func(name)
8500 char_u *name; /* name of the function */
8501{
8502 int first = 0;
8503 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8504 int cmp;
8505 int x;
8506
8507 /*
8508 * Find the function name in the table. Binary search.
8509 */
8510 while (first <= last)
8511 {
8512 x = first + ((unsigned)(last - first) >> 1);
8513 cmp = STRCMP(name, functions[x].f_name);
8514 if (cmp < 0)
8515 last = x - 1;
8516 else if (cmp > 0)
8517 first = x + 1;
8518 else
8519 return x;
8520 }
8521 return -1;
8522}
8523
8524/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008525 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8526 * name it contains, otherwise return "name".
8527 */
8528 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008529deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 char_u *name;
8531 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008532 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008533{
Bram Moolenaar33570922005-01-25 22:26:29 +00008534 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008535 int cc;
8536
8537 cc = name[*lenp];
8538 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008539 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008540 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008541 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008542 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008543 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008544 {
8545 *lenp = 0;
8546 return (char_u *)""; /* just in case */
8547 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008548 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008549 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008550 }
8551
8552 return name;
8553}
8554
8555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 * Allocate a variable for the result of a function.
8557 * Return OK or FAIL.
8558 */
8559 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008560get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8561 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 char_u *name; /* name of the function */
8563 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 char_u **arg; /* argument, pointing to the '(' */
8566 linenr_T firstline; /* first line of range */
8567 linenr_T lastline; /* last line of range */
8568 int *doesrange; /* return: function handled range */
8569 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008570 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
8572 char_u *argp;
8573 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008574 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 int argcount = 0; /* number of arguments found */
8576
8577 /*
8578 * Get the arguments.
8579 */
8580 argp = *arg;
8581 while (argcount < MAX_FUNC_ARGS)
8582 {
8583 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8584 if (*argp == ')' || *argp == ',' || *argp == NUL)
8585 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8587 {
8588 ret = FAIL;
8589 break;
8590 }
8591 ++argcount;
8592 if (*argp != ',')
8593 break;
8594 }
8595 if (*argp == ')')
8596 ++argp;
8597 else
8598 ret = FAIL;
8599
8600 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008601 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008602 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008604 {
8605 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008606 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008607 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008608 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610
8611 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008612 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613
8614 *arg = skipwhite(argp);
8615 return ret;
8616}
8617
8618
8619/*
8620 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008621 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008622 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 */
8624 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008625call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008626 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008627 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008629 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008631 typval_T *argvars; /* vars for arguments, must have "argcount"
8632 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 linenr_T firstline; /* first line of range */
8634 linenr_T lastline; /* last line of range */
8635 int *doesrange; /* return: function handled range */
8636 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008637 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638{
8639 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640#define ERROR_UNKNOWN 0
8641#define ERROR_TOOMANY 1
8642#define ERROR_TOOFEW 2
8643#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008644#define ERROR_DICT 4
8645#define ERROR_NONE 5
8646#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 int error = ERROR_NONE;
8648 int i;
8649 int llen;
8650 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651#define FLEN_FIXED 40
8652 char_u fname_buf[FLEN_FIXED + 1];
8653 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008654 char_u *name;
8655
8656 /* Make a copy of the name, if it comes from a funcref variable it could
8657 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008658 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008659 if (name == NULL)
8660 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661
8662 /*
8663 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8664 * Change <SNR>123_name() to K_SNR 123_name().
8665 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 llen = eval_fname_script(name);
8668 if (llen > 0)
8669 {
8670 fname_buf[0] = K_SPECIAL;
8671 fname_buf[1] = KS_EXTRA;
8672 fname_buf[2] = (int)KE_SNR;
8673 i = 3;
8674 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8675 {
8676 if (current_SID <= 0)
8677 error = ERROR_SCRIPT;
8678 else
8679 {
8680 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8681 i = (int)STRLEN(fname_buf);
8682 }
8683 }
8684 if (i + STRLEN(name + llen) < FLEN_FIXED)
8685 {
8686 STRCPY(fname_buf + i, name + llen);
8687 fname = fname_buf;
8688 }
8689 else
8690 {
8691 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8692 if (fname == NULL)
8693 error = ERROR_OTHER;
8694 else
8695 {
8696 mch_memmove(fname, fname_buf, (size_t)i);
8697 STRCPY(fname + i, name + llen);
8698 }
8699 }
8700 }
8701 else
8702 fname = name;
8703
8704 *doesrange = FALSE;
8705
8706
8707 /* execute the function if no errors detected and executing */
8708 if (evaluate && error == ERROR_NONE)
8709 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008710 char_u *rfname = fname;
8711
8712 /* Ignore "g:" before a function name. */
8713 if (fname[0] == 'g' && fname[1] == ':')
8714 rfname = fname + 2;
8715
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008716 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8717 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 error = ERROR_UNKNOWN;
8719
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008720 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 {
8722 /*
8723 * User defined function.
8724 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008725 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008726
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008728 /* Trigger FuncUndefined event, may load the function. */
8729 if (fp == NULL
8730 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008731 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008732 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008734 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008735 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 }
8737#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008738 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008739 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008740 {
8741 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008742 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008743 }
8744
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 if (fp != NULL)
8746 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008747 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008749 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008751 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008753 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008754 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 else
8756 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008757 int did_save_redo = FALSE;
8758
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 /*
8760 * Call the user function.
8761 * Save and restore search patterns, script variables and
8762 * redo buffer.
8763 */
8764 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008765#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008766 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008767#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008768 {
8769 saveRedobuff();
8770 did_save_redo = TRUE;
8771 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008772 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008773 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008774 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008775 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8776 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8777 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008778 /* Function was unreferenced while being used, free it
8779 * now. */
8780 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008781 if (did_save_redo)
8782 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 restore_search_patterns();
8784 error = ERROR_NONE;
8785 }
8786 }
8787 }
8788 else
8789 {
8790 /*
8791 * Find the function name in the table, call its implementation.
8792 */
8793 i = find_internal_func(fname);
8794 if (i >= 0)
8795 {
8796 if (argcount < functions[i].f_min_argc)
8797 error = ERROR_TOOFEW;
8798 else if (argcount > functions[i].f_max_argc)
8799 error = ERROR_TOOMANY;
8800 else
8801 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008802 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008803 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 error = ERROR_NONE;
8805 }
8806 }
8807 }
8808 /*
8809 * The function call (or "FuncUndefined" autocommand sequence) might
8810 * have been aborted by an error, an interrupt, or an explicitly thrown
8811 * exception that has not been caught so far. This situation can be
8812 * tested for by calling aborting(). For an error in an internal
8813 * function or for the "E132" error in call_user_func(), however, the
8814 * throw point at which the "force_abort" flag (temporarily reset by
8815 * emsg()) is normally updated has not been reached yet. We need to
8816 * update that flag first to make aborting() reliable.
8817 */
8818 update_force_abort();
8819 }
8820 if (error == ERROR_NONE)
8821 ret = OK;
8822
8823 /*
8824 * Report an error unless the argument evaluation or function call has been
8825 * cancelled due to an aborting error, an interrupt, or an exception.
8826 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008827 if (!aborting())
8828 {
8829 switch (error)
8830 {
8831 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008832 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008833 break;
8834 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008835 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008836 break;
8837 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008838 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008839 name);
8840 break;
8841 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008842 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008843 name);
8844 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008845 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008846 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008847 name);
8848 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008849 }
8850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 if (fname != name && fname != fname_buf)
8853 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008854 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855
8856 return ret;
8857}
8858
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008859/*
8860 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008861 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008862 */
8863 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008864emsg_funcname(ermsg, name)
8865 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008866 char_u *name;
8867{
8868 char_u *p;
8869
8870 if (*name == K_SPECIAL)
8871 p = concat_str((char_u *)"<SNR>", name + 3);
8872 else
8873 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008874 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008875 if (p != name)
8876 vim_free(p);
8877}
8878
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008879/*
8880 * Return TRUE for a non-zero Number and a non-empty String.
8881 */
8882 static int
8883non_zero_arg(argvars)
8884 typval_T *argvars;
8885{
8886 return ((argvars[0].v_type == VAR_NUMBER
8887 && argvars[0].vval.v_number != 0)
8888 || (argvars[0].v_type == VAR_STRING
8889 && argvars[0].vval.v_string != NULL
8890 && *argvars[0].vval.v_string != NUL));
8891}
8892
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893/*********************************************
8894 * Implementation of the built-in functions
8895 */
8896
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008897#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008898static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8899
8900/*
8901 * Get the float value of "argvars[0]" into "f".
8902 * Returns FAIL when the argument is not a Number or Float.
8903 */
8904 static int
8905get_float_arg(argvars, f)
8906 typval_T *argvars;
8907 float_T *f;
8908{
8909 if (argvars[0].v_type == VAR_FLOAT)
8910 {
8911 *f = argvars[0].vval.v_float;
8912 return OK;
8913 }
8914 if (argvars[0].v_type == VAR_NUMBER)
8915 {
8916 *f = (float_T)argvars[0].vval.v_number;
8917 return OK;
8918 }
8919 EMSG(_("E808: Number or Float required"));
8920 return FAIL;
8921}
8922
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008923/*
8924 * "abs(expr)" function
8925 */
8926 static void
8927f_abs(argvars, rettv)
8928 typval_T *argvars;
8929 typval_T *rettv;
8930{
8931 if (argvars[0].v_type == VAR_FLOAT)
8932 {
8933 rettv->v_type = VAR_FLOAT;
8934 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8935 }
8936 else
8937 {
8938 varnumber_T n;
8939 int error = FALSE;
8940
8941 n = get_tv_number_chk(&argvars[0], &error);
8942 if (error)
8943 rettv->vval.v_number = -1;
8944 else if (n > 0)
8945 rettv->vval.v_number = n;
8946 else
8947 rettv->vval.v_number = -n;
8948 }
8949}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008950
8951/*
8952 * "acos()" function
8953 */
8954 static void
8955f_acos(argvars, rettv)
8956 typval_T *argvars;
8957 typval_T *rettv;
8958{
8959 float_T f;
8960
8961 rettv->v_type = VAR_FLOAT;
8962 if (get_float_arg(argvars, &f) == OK)
8963 rettv->vval.v_float = acos(f);
8964 else
8965 rettv->vval.v_float = 0.0;
8966}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008967#endif
8968
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008970 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 */
8972 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008973f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008974 typval_T *argvars;
8975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
Bram Moolenaar33570922005-01-25 22:26:29 +00008977 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008980 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008982 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008983 && !tv_check_lock(l->lv_lock,
8984 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008985 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008987 }
8988 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008989 EMSG(_(e_listreq));
8990}
8991
8992/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008993 * "alloc_fail(id, countdown, repeat)" function
8994 */
8995 static void
8996f_alloc_fail(argvars, rettv)
8997 typval_T *argvars;
8998 typval_T *rettv UNUSED;
8999{
9000 if (argvars[0].v_type != VAR_NUMBER
9001 || argvars[0].vval.v_number <= 0
9002 || argvars[1].v_type != VAR_NUMBER
9003 || argvars[1].vval.v_number < 0
9004 || argvars[2].v_type != VAR_NUMBER)
9005 EMSG(_(e_invarg));
9006 else
9007 {
9008 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009009 if (alloc_fail_id >= aid_last)
9010 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009011 alloc_fail_countdown = argvars[1].vval.v_number;
9012 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009013 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009014 }
9015}
9016
9017/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009018 * "and(expr, expr)" function
9019 */
9020 static void
9021f_and(argvars, rettv)
9022 typval_T *argvars;
9023 typval_T *rettv;
9024{
9025 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9026 & get_tv_number_chk(&argvars[1], NULL);
9027}
9028
9029/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009030 * "append(lnum, string/list)" function
9031 */
9032 static void
9033f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009034 typval_T *argvars;
9035 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009036{
9037 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009038 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009039 list_T *l = NULL;
9040 listitem_T *li = NULL;
9041 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009042 long added = 0;
9043
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009044 /* When coming here from Insert mode, sync undo, so that this can be
9045 * undone separately from what was previously inserted. */
9046 if (u_sync_once == 2)
9047 {
9048 u_sync_once = 1; /* notify that u_sync() was called */
9049 u_sync(TRUE);
9050 }
9051
Bram Moolenaar0d660222005-01-07 21:51:51 +00009052 lnum = get_tv_lnum(argvars);
9053 if (lnum >= 0
9054 && lnum <= curbuf->b_ml.ml_line_count
9055 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009056 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009057 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009058 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009059 l = argvars[1].vval.v_list;
9060 if (l == NULL)
9061 return;
9062 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009063 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009064 for (;;)
9065 {
9066 if (l == NULL)
9067 tv = &argvars[1]; /* append a string */
9068 else if (li == NULL)
9069 break; /* end of list */
9070 else
9071 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009072 line = get_tv_string_chk(tv);
9073 if (line == NULL) /* type error */
9074 {
9075 rettv->vval.v_number = 1; /* Failed */
9076 break;
9077 }
9078 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009079 ++added;
9080 if (l == NULL)
9081 break;
9082 li = li->li_next;
9083 }
9084
9085 appended_lines_mark(lnum, added);
9086 if (curwin->w_cursor.lnum > lnum)
9087 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009089 else
9090 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091}
9092
9093/*
9094 * "argc()" function
9095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009098 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102}
9103
9104/*
9105 * "argidx()" function
9106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009109 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113}
9114
9115/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009116 * "arglistid()" function
9117 */
9118 static void
9119f_arglistid(argvars, rettv)
9120 typval_T *argvars UNUSED;
9121 typval_T *rettv;
9122{
9123 win_T *wp;
9124 tabpage_T *tp = NULL;
9125 long n;
9126
9127 rettv->vval.v_number = -1;
9128 if (argvars[0].v_type != VAR_UNKNOWN)
9129 {
9130 if (argvars[1].v_type != VAR_UNKNOWN)
9131 {
9132 n = get_tv_number(&argvars[1]);
9133 if (n >= 0)
9134 tp = find_tabpage(n);
9135 }
9136 else
9137 tp = curtab;
9138
9139 if (tp != NULL)
9140 {
9141 wp = find_win_by_nr(&argvars[0], tp);
9142 if (wp != NULL)
9143 rettv->vval.v_number = wp->w_alist->id;
9144 }
9145 }
9146 else
9147 rettv->vval.v_number = curwin->w_alist->id;
9148}
9149
9150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 * "argv(nr)" function
9152 */
9153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009155 typval_T *argvars;
9156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157{
9158 int idx;
9159
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009160 if (argvars[0].v_type != VAR_UNKNOWN)
9161 {
9162 idx = get_tv_number_chk(&argvars[0], NULL);
9163 if (idx >= 0 && idx < ARGCOUNT)
9164 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9165 else
9166 rettv->vval.v_string = NULL;
9167 rettv->v_type = VAR_STRING;
9168 }
9169 else if (rettv_list_alloc(rettv) == OK)
9170 for (idx = 0; idx < ARGCOUNT; ++idx)
9171 list_append_string(rettv->vval.v_list,
9172 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173}
9174
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009175static void prepare_assert_error __ARGS((garray_T*gap));
9176static 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));
9177static void assert_error __ARGS((garray_T *gap));
9178static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9179
9180/*
9181 * Prepare "gap" for an assert error and add the sourcing position.
9182 */
9183 static void
9184prepare_assert_error(gap)
9185 garray_T *gap;
9186{
9187 char buf[NUMBUFLEN];
9188
9189 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009190 if (sourcing_name != NULL)
9191 {
9192 ga_concat(gap, sourcing_name);
9193 if (sourcing_lnum > 0)
9194 ga_concat(gap, (char_u *)" ");
9195 }
9196 if (sourcing_lnum > 0)
9197 {
9198 sprintf(buf, "line %ld", (long)sourcing_lnum);
9199 ga_concat(gap, (char_u *)buf);
9200 }
9201 if (sourcing_name != NULL || sourcing_lnum > 0)
9202 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009203}
9204
9205/*
9206 * Fill "gap" with information about an assert error.
9207 */
9208 static void
9209fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9210 garray_T *gap;
9211 typval_T *opt_msg_tv;
9212 char_u *exp_str;
9213 typval_T *exp_tv;
9214 typval_T *got_tv;
9215{
9216 char_u numbuf[NUMBUFLEN];
9217 char_u *tofree;
9218
9219 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9220 {
9221 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9222 vim_free(tofree);
9223 }
9224 else
9225 {
9226 ga_concat(gap, (char_u *)"Expected ");
9227 if (exp_str == NULL)
9228 {
9229 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9230 vim_free(tofree);
9231 }
9232 else
9233 ga_concat(gap, exp_str);
9234 ga_concat(gap, (char_u *)" but got ");
9235 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9236 vim_free(tofree);
9237 }
9238}
Bram Moolenaar43345542015-11-29 17:35:35 +01009239
9240/*
9241 * Add an assert error to v:errors.
9242 */
9243 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009244assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009245 garray_T *gap;
9246{
9247 struct vimvar *vp = &vimvars[VV_ERRORS];
9248
9249 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9250 /* Make sure v:errors is a list. */
9251 set_vim_var_list(VV_ERRORS, list_alloc());
9252 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9253}
9254
Bram Moolenaar43345542015-11-29 17:35:35 +01009255/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009256 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009257 */
9258 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009259f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009260 typval_T *argvars;
9261 typval_T *rettv UNUSED;
9262{
9263 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009264
9265 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9266 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009267 prepare_assert_error(&ga);
9268 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9269 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009270 ga_clear(&ga);
9271 }
9272}
9273
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009274/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009275 * "assert_exception(string[, msg])" function
9276 */
9277 static void
9278f_assert_exception(argvars, rettv)
9279 typval_T *argvars;
9280 typval_T *rettv UNUSED;
9281{
9282 garray_T ga;
9283 char *error;
9284
9285 error = (char *)get_tv_string_chk(&argvars[0]);
9286 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9287 {
9288 prepare_assert_error(&ga);
9289 ga_concat(&ga, (char_u *)"v:exception is not set");
9290 assert_error(&ga);
9291 ga_clear(&ga);
9292 }
9293 else if (strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
9294 {
9295 prepare_assert_error(&ga);
9296 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9297 &vimvars[VV_EXCEPTION].vv_tv);
9298 assert_error(&ga);
9299 ga_clear(&ga);
9300 }
9301}
9302
9303/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009304 * "assert_fails(cmd [, error])" function
9305 */
9306 static void
9307f_assert_fails(argvars, rettv)
9308 typval_T *argvars;
9309 typval_T *rettv UNUSED;
9310{
9311 char_u *cmd = get_tv_string_chk(&argvars[0]);
9312 garray_T ga;
9313
9314 called_emsg = FALSE;
9315 suppress_errthrow = TRUE;
9316 emsg_silent = TRUE;
9317 do_cmdline_cmd(cmd);
9318 if (!called_emsg)
9319 {
9320 prepare_assert_error(&ga);
9321 ga_concat(&ga, (char_u *)"command did not fail: ");
9322 ga_concat(&ga, cmd);
9323 assert_error(&ga);
9324 ga_clear(&ga);
9325 }
9326 else if (argvars[1].v_type != VAR_UNKNOWN)
9327 {
9328 char_u buf[NUMBUFLEN];
9329 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9330
9331 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9332 {
9333 prepare_assert_error(&ga);
9334 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9335 &vimvars[VV_ERRMSG].vv_tv);
9336 assert_error(&ga);
9337 ga_clear(&ga);
9338 }
9339 }
9340
9341 called_emsg = FALSE;
9342 suppress_errthrow = FALSE;
9343 emsg_silent = FALSE;
9344 emsg_on_display = FALSE;
9345 set_vim_var_string(VV_ERRMSG, NULL, 0);
9346}
9347
9348/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009349 * Common for assert_true() and assert_false().
9350 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009351 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009352assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009353 typval_T *argvars;
9354 int isTrue;
9355{
9356 int error = FALSE;
9357 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009358
9359 if (argvars[0].v_type != VAR_NUMBER
9360 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9361 || error)
9362 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009363 prepare_assert_error(&ga);
9364 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009365 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009366 NULL, &argvars[0]);
9367 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009368 ga_clear(&ga);
9369 }
9370}
9371
9372/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009373 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009374 */
9375 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009376f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009377 typval_T *argvars;
9378 typval_T *rettv UNUSED;
9379{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009380 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009381}
9382
9383/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009384 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009385 */
9386 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009387f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009388 typval_T *argvars;
9389 typval_T *rettv UNUSED;
9390{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009391 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009392}
9393
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009394#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009395/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009396 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009397 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009398 static void
9399f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009400 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009401 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009402{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009403 float_T f;
9404
9405 rettv->v_type = VAR_FLOAT;
9406 if (get_float_arg(argvars, &f) == OK)
9407 rettv->vval.v_float = asin(f);
9408 else
9409 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009410}
9411
9412/*
9413 * "atan()" function
9414 */
9415 static void
9416f_atan(argvars, rettv)
9417 typval_T *argvars;
9418 typval_T *rettv;
9419{
9420 float_T f;
9421
9422 rettv->v_type = VAR_FLOAT;
9423 if (get_float_arg(argvars, &f) == OK)
9424 rettv->vval.v_float = atan(f);
9425 else
9426 rettv->vval.v_float = 0.0;
9427}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009428
9429/*
9430 * "atan2()" function
9431 */
9432 static void
9433f_atan2(argvars, rettv)
9434 typval_T *argvars;
9435 typval_T *rettv;
9436{
9437 float_T fx, fy;
9438
9439 rettv->v_type = VAR_FLOAT;
9440 if (get_float_arg(argvars, &fx) == OK
9441 && get_float_arg(&argvars[1], &fy) == OK)
9442 rettv->vval.v_float = atan2(fx, fy);
9443 else
9444 rettv->vval.v_float = 0.0;
9445}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009446#endif
9447
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448/*
9449 * "browse(save, title, initdir, default)" function
9450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009453 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455{
9456#ifdef FEAT_BROWSE
9457 int save;
9458 char_u *title;
9459 char_u *initdir;
9460 char_u *defname;
9461 char_u buf[NUMBUFLEN];
9462 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009463 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009465 save = get_tv_number_chk(&argvars[0], &error);
9466 title = get_tv_string_chk(&argvars[1]);
9467 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9468 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 if (error || title == NULL || initdir == NULL || defname == NULL)
9471 rettv->vval.v_string = NULL;
9472 else
9473 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009474 do_browse(save ? BROWSE_SAVE : 0,
9475 title, defname, NULL, initdir, NULL, curbuf);
9476#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009478#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009480}
9481
9482/*
9483 * "browsedir(title, initdir)" function
9484 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009487 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009488 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009489{
9490#ifdef FEAT_BROWSE
9491 char_u *title;
9492 char_u *initdir;
9493 char_u buf[NUMBUFLEN];
9494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009495 title = get_tv_string_chk(&argvars[0]);
9496 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009498 if (title == NULL || initdir == NULL)
9499 rettv->vval.v_string = NULL;
9500 else
9501 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009502 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507}
9508
Bram Moolenaar33570922005-01-25 22:26:29 +00009509static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009510
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511/*
9512 * Find a buffer by number or exact name.
9513 */
9514 static buf_T *
9515find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009516 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517{
9518 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009520 if (avar->v_type == VAR_NUMBER)
9521 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009522 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009524 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009525 if (buf == NULL)
9526 {
9527 /* No full path name match, try a match with a URL or a "nofile"
9528 * buffer, these don't use the full path. */
9529 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9530 if (buf->b_fname != NULL
9531 && (path_with_url(buf->b_fname)
9532#ifdef FEAT_QUICKFIX
9533 || bt_nofile(buf)
9534#endif
9535 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009536 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009537 break;
9538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 }
9540 return buf;
9541}
9542
9543/*
9544 * "bufexists(expr)" function
9545 */
9546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009548 typval_T *argvars;
9549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009551 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552}
9553
9554/*
9555 * "buflisted(expr)" function
9556 */
9557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009559 typval_T *argvars;
9560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
9562 buf_T *buf;
9563
9564 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566}
9567
9568/*
9569 * "bufloaded(expr)" function
9570 */
9571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009573 typval_T *argvars;
9574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575{
9576 buf_T *buf;
9577
9578 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580}
9581
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009582static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009583
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584/*
9585 * Get buffer by number or pattern.
9586 */
9587 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009588get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009589 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009590 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009592 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 int save_magic;
9594 char_u *save_cpo;
9595 buf_T *buf;
9596
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009597 if (tv->v_type == VAR_NUMBER)
9598 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009599 if (tv->v_type != VAR_STRING)
9600 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 if (name == NULL || *name == NUL)
9602 return curbuf;
9603 if (name[0] == '$' && name[1] == NUL)
9604 return lastbuf;
9605
9606 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9607 save_magic = p_magic;
9608 p_magic = TRUE;
9609 save_cpo = p_cpo;
9610 p_cpo = (char_u *)"";
9611
9612 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009613 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614
9615 p_magic = save_magic;
9616 p_cpo = save_cpo;
9617
9618 /* If not found, try expanding the name, like done for bufexists(). */
9619 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621
9622 return buf;
9623}
9624
9625/*
9626 * "bufname(expr)" function
9627 */
9628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009630 typval_T *argvars;
9631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632{
9633 buf_T *buf;
9634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009635 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009637 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 --emsg_off;
9644}
9645
9646/*
9647 * "bufnr(expr)" function
9648 */
9649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009651 typval_T *argvars;
9652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653{
9654 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009655 int error = FALSE;
9656 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009658 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009660 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009661 --emsg_off;
9662
9663 /* If the buffer isn't found and the second argument is not zero create a
9664 * new buffer. */
9665 if (buf == NULL
9666 && argvars[1].v_type != VAR_UNKNOWN
9667 && get_tv_number_chk(&argvars[1], &error) != 0
9668 && !error
9669 && (name = get_tv_string_chk(&argvars[0])) != NULL
9670 && !error)
9671 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9672
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677}
9678
9679/*
9680 * "bufwinnr(nr)" function
9681 */
9682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009684 typval_T *argvars;
9685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686{
9687#ifdef FEAT_WINDOWS
9688 win_T *wp;
9689 int winnr = 0;
9690#endif
9691 buf_T *buf;
9692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009693 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009695 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696#ifdef FEAT_WINDOWS
9697 for (wp = firstwin; wp; wp = wp->w_next)
9698 {
9699 ++winnr;
9700 if (wp->w_buffer == buf)
9701 break;
9702 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706#endif
9707 --emsg_off;
9708}
9709
9710/*
9711 * "byte2line(byte)" function
9712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009715 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717{
9718#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720#else
9721 long boff = 0;
9722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009723 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009725 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 (linenr_T)0, &boff);
9729#endif
9730}
9731
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009732 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009733byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009734 typval_T *argvars;
9735 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009736 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009737{
9738#ifdef FEAT_MBYTE
9739 char_u *t;
9740#endif
9741 char_u *str;
9742 long idx;
9743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009744 str = get_tv_string_chk(&argvars[0]);
9745 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009746 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009747 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009748 return;
9749
9750#ifdef FEAT_MBYTE
9751 t = str;
9752 for ( ; idx > 0; idx--)
9753 {
9754 if (*t == NUL) /* EOL reached */
9755 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009756 if (enc_utf8 && comp)
9757 t += utf_ptr2len(t);
9758 else
9759 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009760 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009761 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009762#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009763 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009764 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009765#endif
9766}
9767
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009768/*
9769 * "byteidx()" function
9770 */
9771 static void
9772f_byteidx(argvars, rettv)
9773 typval_T *argvars;
9774 typval_T *rettv;
9775{
9776 byteidx(argvars, rettv, FALSE);
9777}
9778
9779/*
9780 * "byteidxcomp()" function
9781 */
9782 static void
9783f_byteidxcomp(argvars, rettv)
9784 typval_T *argvars;
9785 typval_T *rettv;
9786{
9787 byteidx(argvars, rettv, TRUE);
9788}
9789
Bram Moolenaardb913952012-06-29 12:54:53 +02009790 int
9791func_call(name, args, selfdict, rettv)
9792 char_u *name;
9793 typval_T *args;
9794 dict_T *selfdict;
9795 typval_T *rettv;
9796{
9797 listitem_T *item;
9798 typval_T argv[MAX_FUNC_ARGS + 1];
9799 int argc = 0;
9800 int dummy;
9801 int r = 0;
9802
9803 for (item = args->vval.v_list->lv_first; item != NULL;
9804 item = item->li_next)
9805 {
9806 if (argc == MAX_FUNC_ARGS)
9807 {
9808 EMSG(_("E699: Too many arguments"));
9809 break;
9810 }
9811 /* Make a copy of each argument. This is needed to be able to set
9812 * v_lock to VAR_FIXED in the copy without changing the original list.
9813 */
9814 copy_tv(&item->li_tv, &argv[argc++]);
9815 }
9816
9817 if (item == NULL)
9818 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9819 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9820 &dummy, TRUE, selfdict);
9821
9822 /* Free the arguments. */
9823 while (argc > 0)
9824 clear_tv(&argv[--argc]);
9825
9826 return r;
9827}
9828
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009829/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009830 * "call(func, arglist)" function
9831 */
9832 static void
9833f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009834 typval_T *argvars;
9835 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009836{
9837 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009838 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009839
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009840 if (argvars[1].v_type != VAR_LIST)
9841 {
9842 EMSG(_(e_listreq));
9843 return;
9844 }
9845 if (argvars[1].vval.v_list == NULL)
9846 return;
9847
9848 if (argvars[0].v_type == VAR_FUNC)
9849 func = argvars[0].vval.v_string;
9850 else
9851 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009852 if (*func == NUL)
9853 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009854
Bram Moolenaare9a41262005-01-15 22:18:47 +00009855 if (argvars[2].v_type != VAR_UNKNOWN)
9856 {
9857 if (argvars[2].v_type != VAR_DICT)
9858 {
9859 EMSG(_(e_dictreq));
9860 return;
9861 }
9862 selfdict = argvars[2].vval.v_dict;
9863 }
9864
Bram Moolenaardb913952012-06-29 12:54:53 +02009865 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009866}
9867
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009868#ifdef FEAT_FLOAT
9869/*
9870 * "ceil({float})" function
9871 */
9872 static void
9873f_ceil(argvars, rettv)
9874 typval_T *argvars;
9875 typval_T *rettv;
9876{
9877 float_T f;
9878
9879 rettv->v_type = VAR_FLOAT;
9880 if (get_float_arg(argvars, &f) == OK)
9881 rettv->vval.v_float = ceil(f);
9882 else
9883 rettv->vval.v_float = 0.0;
9884}
9885#endif
9886
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009887/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009888 * "changenr()" function
9889 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009890 static void
9891f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009892 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009893 typval_T *rettv;
9894{
9895 rettv->vval.v_number = curbuf->b_u_seq_cur;
9896}
9897
9898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 * "char2nr(string)" function
9900 */
9901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009902f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009903 typval_T *argvars;
9904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905{
9906#ifdef FEAT_MBYTE
9907 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009908 {
9909 int utf8 = 0;
9910
9911 if (argvars[1].v_type != VAR_UNKNOWN)
9912 utf8 = get_tv_number_chk(&argvars[1], NULL);
9913
9914 if (utf8)
9915 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9916 else
9917 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 else
9920#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009921 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922}
9923
9924/*
9925 * "cindent(lnum)" function
9926 */
9927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009928f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009929 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931{
9932#ifdef FEAT_CINDENT
9933 pos_T pos;
9934 linenr_T lnum;
9935
9936 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9939 {
9940 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009941 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 curwin->w_cursor = pos;
9943 }
9944 else
9945#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009946 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947}
9948
9949/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009950 * "clearmatches()" function
9951 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009952 static void
9953f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009954 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009955 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009956{
9957#ifdef FEAT_SEARCH_EXTRA
9958 clear_matches(curwin);
9959#endif
9960}
9961
9962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 * "col(string)" function
9964 */
9965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009967 typval_T *argvars;
9968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969{
9970 colnr_T col = 0;
9971 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009972 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009974 fp = var2fpos(&argvars[0], FALSE, &fnum);
9975 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 {
9977 if (fp->col == MAXCOL)
9978 {
9979 /* '> can be MAXCOL, get the length of the line then */
9980 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009981 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 else
9983 col = MAXCOL;
9984 }
9985 else
9986 {
9987 col = fp->col + 1;
9988#ifdef FEAT_VIRTUALEDIT
9989 /* col(".") when the cursor is on the NUL at the end of the line
9990 * because of "coladd" can be seen as an extra column. */
9991 if (virtual_active() && fp == &curwin->w_cursor)
9992 {
9993 char_u *p = ml_get_cursor();
9994
9995 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9996 curwin->w_virtcol - curwin->w_cursor.coladd))
9997 {
9998# ifdef FEAT_MBYTE
9999 int l;
10000
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010001 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 col += l;
10003# else
10004 if (*p != NUL && p[1] == NUL)
10005 ++col;
10006# endif
10007 }
10008 }
10009#endif
10010 }
10011 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010012 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013}
10014
Bram Moolenaar572cb562005-08-05 21:35:02 +000010015#if defined(FEAT_INS_EXPAND)
10016/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010017 * "complete()" function
10018 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010019 static void
10020f_complete(argvars, rettv)
10021 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010022 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +000010023{
10024 int startcol;
10025
10026 if ((State & INSERT) == 0)
10027 {
10028 EMSG(_("E785: complete() can only be used in Insert mode"));
10029 return;
10030 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010031
10032 /* Check for undo allowed here, because if something was already inserted
10033 * the line was already saved for undo and this check isn't done. */
10034 if (!undo_allowed())
10035 return;
10036
Bram Moolenaarade00832006-03-10 21:46:58 +000010037 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10038 {
10039 EMSG(_(e_invarg));
10040 return;
10041 }
10042
10043 startcol = get_tv_number_chk(&argvars[0], NULL);
10044 if (startcol <= 0)
10045 return;
10046
10047 set_completion(startcol - 1, argvars[1].vval.v_list);
10048}
10049
10050/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010051 * "complete_add()" function
10052 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010053 static void
10054f_complete_add(argvars, rettv)
10055 typval_T *argvars;
10056 typval_T *rettv;
10057{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010058 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010059}
10060
10061/*
10062 * "complete_check()" function
10063 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010064 static void
10065f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010066 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +000010067 typval_T *rettv;
10068{
10069 int saved = RedrawingDisabled;
10070
10071 RedrawingDisabled = 0;
10072 ins_compl_check_keys(0);
10073 rettv->vval.v_number = compl_interrupted;
10074 RedrawingDisabled = saved;
10075}
10076#endif
10077
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078/*
10079 * "confirm(message, buttons[, default [, type]])" function
10080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010082f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010083 typval_T *argvars UNUSED;
10084 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085{
10086#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10087 char_u *message;
10088 char_u *buttons = NULL;
10089 char_u buf[NUMBUFLEN];
10090 char_u buf2[NUMBUFLEN];
10091 int def = 1;
10092 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010093 char_u *typestr;
10094 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010096 message = get_tv_string_chk(&argvars[0]);
10097 if (message == NULL)
10098 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010099 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010101 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10102 if (buttons == NULL)
10103 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010104 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010106 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010107 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010109 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10110 if (typestr == NULL)
10111 error = TRUE;
10112 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010114 switch (TOUPPER_ASC(*typestr))
10115 {
10116 case 'E': type = VIM_ERROR; break;
10117 case 'Q': type = VIM_QUESTION; break;
10118 case 'I': type = VIM_INFO; break;
10119 case 'W': type = VIM_WARNING; break;
10120 case 'G': type = VIM_GENERIC; break;
10121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 }
10123 }
10124 }
10125 }
10126
10127 if (buttons == NULL || *buttons == NUL)
10128 buttons = (char_u *)_("&Ok");
10129
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010130 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010131 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010132 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133#endif
10134}
10135
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010136/*
10137 * "copy()" function
10138 */
10139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010141 typval_T *argvars;
10142 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010143{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010144 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010145}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010147#ifdef FEAT_FLOAT
10148/*
10149 * "cos()" function
10150 */
10151 static void
10152f_cos(argvars, rettv)
10153 typval_T *argvars;
10154 typval_T *rettv;
10155{
10156 float_T f;
10157
10158 rettv->v_type = VAR_FLOAT;
10159 if (get_float_arg(argvars, &f) == OK)
10160 rettv->vval.v_float = cos(f);
10161 else
10162 rettv->vval.v_float = 0.0;
10163}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010164
10165/*
10166 * "cosh()" function
10167 */
10168 static void
10169f_cosh(argvars, rettv)
10170 typval_T *argvars;
10171 typval_T *rettv;
10172{
10173 float_T f;
10174
10175 rettv->v_type = VAR_FLOAT;
10176 if (get_float_arg(argvars, &f) == OK)
10177 rettv->vval.v_float = cosh(f);
10178 else
10179 rettv->vval.v_float = 0.0;
10180}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010181#endif
10182
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010184 * "count()" function
10185 */
10186 static void
10187f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010188 typval_T *argvars;
10189 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010190{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010191 long n = 0;
10192 int ic = FALSE;
10193
Bram Moolenaare9a41262005-01-15 22:18:47 +000010194 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010195 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010196 listitem_T *li;
10197 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010198 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010199
Bram Moolenaare9a41262005-01-15 22:18:47 +000010200 if ((l = argvars[0].vval.v_list) != NULL)
10201 {
10202 li = l->lv_first;
10203 if (argvars[2].v_type != VAR_UNKNOWN)
10204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010205 int error = FALSE;
10206
10207 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010208 if (argvars[3].v_type != VAR_UNKNOWN)
10209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010210 idx = get_tv_number_chk(&argvars[3], &error);
10211 if (!error)
10212 {
10213 li = list_find(l, idx);
10214 if (li == NULL)
10215 EMSGN(_(e_listidx), idx);
10216 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010217 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010218 if (error)
10219 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010220 }
10221
10222 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010223 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010224 ++n;
10225 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010226 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010227 else if (argvars[0].v_type == VAR_DICT)
10228 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010229 int todo;
10230 dict_T *d;
10231 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010232
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010233 if ((d = argvars[0].vval.v_dict) != NULL)
10234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010235 int error = FALSE;
10236
Bram Moolenaare9a41262005-01-15 22:18:47 +000010237 if (argvars[2].v_type != VAR_UNKNOWN)
10238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010239 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 if (argvars[3].v_type != VAR_UNKNOWN)
10241 EMSG(_(e_invarg));
10242 }
10243
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010244 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010245 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010246 {
10247 if (!HASHITEM_EMPTY(hi))
10248 {
10249 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010250 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010251 ++n;
10252 }
10253 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010254 }
10255 }
10256 else
10257 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010258 rettv->vval.v_number = n;
10259}
10260
10261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10263 *
10264 * Checks the existence of a cscope connection.
10265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010267f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010268 typval_T *argvars UNUSED;
10269 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270{
10271#ifdef FEAT_CSCOPE
10272 int num = 0;
10273 char_u *dbpath = NULL;
10274 char_u *prepend = NULL;
10275 char_u buf[NUMBUFLEN];
10276
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010277 if (argvars[0].v_type != VAR_UNKNOWN
10278 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010280 num = (int)get_tv_number(&argvars[0]);
10281 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010282 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010283 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 }
10285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010286 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287#endif
10288}
10289
10290/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010291 * "cursor(lnum, col)" function, or
10292 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010294 * Moves the cursor to the specified line and column.
10295 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010298f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010299 typval_T *argvars;
10300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301{
10302 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010303#ifdef FEAT_VIRTUALEDIT
10304 long coladd = 0;
10305#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010306 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010308 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010309 if (argvars[1].v_type == VAR_UNKNOWN)
10310 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010311 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010312 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010313
Bram Moolenaar493c1782014-05-28 14:34:46 +020010314 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010315 {
10316 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010317 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010318 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010319 line = pos.lnum;
10320 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010321#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010322 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010323#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010324 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010325 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010326 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010327 set_curswant = FALSE;
10328 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010329 }
10330 else
10331 {
10332 line = get_tv_lnum(argvars);
10333 col = get_tv_number_chk(&argvars[1], NULL);
10334#ifdef FEAT_VIRTUALEDIT
10335 if (argvars[2].v_type != VAR_UNKNOWN)
10336 coladd = get_tv_number_chk(&argvars[2], NULL);
10337#endif
10338 }
10339 if (line < 0 || col < 0
10340#ifdef FEAT_VIRTUALEDIT
10341 || coladd < 0
10342#endif
10343 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010344 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 if (line > 0)
10346 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 if (col > 0)
10348 curwin->w_cursor.col = col - 1;
10349#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010350 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351#endif
10352
10353 /* Make sure the cursor is in a valid position. */
10354 check_cursor();
10355#ifdef FEAT_MBYTE
10356 /* Correct cursor for multi-byte character. */
10357 if (has_mbyte)
10358 mb_adjust_cursor();
10359#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010360
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010361 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010362 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363}
10364
10365/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010366 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 */
10368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010369f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010370 typval_T *argvars;
10371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010373 int noref = 0;
10374
10375 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010376 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010377 if (noref < 0 || noref > 1)
10378 EMSG(_(e_invarg));
10379 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010380 {
10381 current_copyID += COPYID_INC;
10382 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384}
10385
10386/*
10387 * "delete()" function
10388 */
10389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010390f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010391 typval_T *argvars;
10392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010394 char_u nbuf[NUMBUFLEN];
10395 char_u *name;
10396 char_u *flags;
10397
10398 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010400 return;
10401
10402 name = get_tv_string(&argvars[0]);
10403 if (name == NULL || *name == NUL)
10404 {
10405 EMSG(_(e_invarg));
10406 return;
10407 }
10408
10409 if (argvars[1].v_type != VAR_UNKNOWN)
10410 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010412 flags = (char_u *)"";
10413
10414 if (*flags == NUL)
10415 /* delete a file */
10416 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10417 else if (STRCMP(flags, "d") == 0)
10418 /* delete an empty directory */
10419 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10420 else if (STRCMP(flags, "rf") == 0)
10421 /* delete an directory recursively */
10422 rettv->vval.v_number = delete_recursive(name);
10423 else
10424 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425}
10426
10427/*
10428 * "did_filetype()" function
10429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010431f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010432 typval_T *argvars UNUSED;
10433 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434{
10435#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437#endif
10438}
10439
10440/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010441 * "diff_filler()" function
10442 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010444f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010445 typval_T *argvars UNUSED;
10446 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010447{
10448#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010449 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010450#endif
10451}
10452
10453/*
10454 * "diff_hlID()" function
10455 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010457f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010458 typval_T *argvars UNUSED;
10459 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010460{
10461#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010462 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010463 static linenr_T prev_lnum = 0;
10464 static int changedtick = 0;
10465 static int fnum = 0;
10466 static int change_start = 0;
10467 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010468 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010469 int filler_lines;
10470 int col;
10471
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010472 if (lnum < 0) /* ignore type error in {lnum} arg */
10473 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010474 if (lnum != prev_lnum
10475 || changedtick != curbuf->b_changedtick
10476 || fnum != curbuf->b_fnum)
10477 {
10478 /* New line, buffer, change: need to get the values. */
10479 filler_lines = diff_check(curwin, lnum);
10480 if (filler_lines < 0)
10481 {
10482 if (filler_lines == -1)
10483 {
10484 change_start = MAXCOL;
10485 change_end = -1;
10486 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10487 hlID = HLF_ADD; /* added line */
10488 else
10489 hlID = HLF_CHD; /* changed line */
10490 }
10491 else
10492 hlID = HLF_ADD; /* added line */
10493 }
10494 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010495 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010496 prev_lnum = lnum;
10497 changedtick = curbuf->b_changedtick;
10498 fnum = curbuf->b_fnum;
10499 }
10500
10501 if (hlID == HLF_CHD || hlID == HLF_TXD)
10502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010503 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010504 if (col >= change_start && col <= change_end)
10505 hlID = HLF_TXD; /* changed text */
10506 else
10507 hlID = HLF_CHD; /* changed line */
10508 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010509 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010510#endif
10511}
10512
10513/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010514 * "empty({expr})" function
10515 */
10516 static void
10517f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010518 typval_T *argvars;
10519 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010520{
10521 int n;
10522
10523 switch (argvars[0].v_type)
10524 {
10525 case VAR_STRING:
10526 case VAR_FUNC:
10527 n = argvars[0].vval.v_string == NULL
10528 || *argvars[0].vval.v_string == NUL;
10529 break;
10530 case VAR_NUMBER:
10531 n = argvars[0].vval.v_number == 0;
10532 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010533#ifdef FEAT_FLOAT
10534 case VAR_FLOAT:
10535 n = argvars[0].vval.v_float == 0.0;
10536 break;
10537#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010538 case VAR_LIST:
10539 n = argvars[0].vval.v_list == NULL
10540 || argvars[0].vval.v_list->lv_first == NULL;
10541 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010542 case VAR_DICT:
10543 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010544 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010545 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010546 default:
10547 EMSG2(_(e_intern2), "f_empty()");
10548 n = 0;
10549 }
10550
10551 rettv->vval.v_number = n;
10552}
10553
10554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 * "escape({string}, {chars})" function
10556 */
10557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010558f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010559 typval_T *argvars;
10560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561{
10562 char_u buf[NUMBUFLEN];
10563
Bram Moolenaar758711c2005-02-02 23:11:38 +000010564 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10565 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010566 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567}
10568
10569/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010570 * "eval()" function
10571 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010572 static void
10573f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010574 typval_T *argvars;
10575 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010576{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010577 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010579 s = get_tv_string_chk(&argvars[0]);
10580 if (s != NULL)
10581 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010582
Bram Moolenaar615b9972015-01-14 17:15:05 +010010583 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010584 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10585 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010586 if (p != NULL && !aborting())
10587 EMSG2(_(e_invexpr2), p);
10588 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010589 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010590 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010591 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010592 else if (*s != NUL)
10593 EMSG(_(e_trailing));
10594}
10595
10596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 * "eventhandler()" function
10598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010600f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010601 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010604 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605}
10606
10607/*
10608 * "executable()" function
10609 */
10610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010611f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010612 typval_T *argvars;
10613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010615 char_u *name = get_tv_string(&argvars[0]);
10616
10617 /* Check in $PATH and also check directly if there is a directory name. */
10618 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10619 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010620}
10621
10622/*
10623 * "exepath()" function
10624 */
10625 static void
10626f_exepath(argvars, rettv)
10627 typval_T *argvars;
10628 typval_T *rettv;
10629{
10630 char_u *p = NULL;
10631
Bram Moolenaarb5971142015-03-21 17:32:19 +010010632 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010633 rettv->v_type = VAR_STRING;
10634 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635}
10636
10637/*
10638 * "exists()" function
10639 */
10640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010641f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010642 typval_T *argvars;
10643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644{
10645 char_u *p;
10646 char_u *name;
10647 int n = FALSE;
10648 int len = 0;
10649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010650 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 if (*p == '$') /* environment variable */
10652 {
10653 /* first try "normal" environment variables (fast) */
10654 if (mch_getenv(p + 1) != NULL)
10655 n = TRUE;
10656 else
10657 {
10658 /* try expanding things like $VIM and ${HOME} */
10659 p = expand_env_save(p);
10660 if (p != NULL && *p != '$')
10661 n = TRUE;
10662 vim_free(p);
10663 }
10664 }
10665 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010667 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010668 if (*skipwhite(p) != NUL)
10669 n = FALSE; /* trailing garbage */
10670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671 else if (*p == '*') /* internal or user defined function */
10672 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010673 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 }
10675 else if (*p == ':')
10676 {
10677 n = cmd_exists(p + 1);
10678 }
10679 else if (*p == '#')
10680 {
10681#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010682 if (p[1] == '#')
10683 n = autocmd_supported(p + 2);
10684 else
10685 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686#endif
10687 }
10688 else /* internal variable */
10689 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010690 char_u *tofree;
10691 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010693 /* get_name_len() takes care of expanding curly braces */
10694 name = p;
10695 len = get_name_len(&p, &tofree, TRUE, FALSE);
10696 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010698 if (tofree != NULL)
10699 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010700 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010701 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010703 /* handle d.key, l[idx], f(expr) */
10704 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10705 if (n)
10706 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 }
10708 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010709 if (*p != NUL)
10710 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010712 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 }
10714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010715 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716}
10717
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010718#ifdef FEAT_FLOAT
10719/*
10720 * "exp()" function
10721 */
10722 static void
10723f_exp(argvars, rettv)
10724 typval_T *argvars;
10725 typval_T *rettv;
10726{
10727 float_T f;
10728
10729 rettv->v_type = VAR_FLOAT;
10730 if (get_float_arg(argvars, &f) == OK)
10731 rettv->vval.v_float = exp(f);
10732 else
10733 rettv->vval.v_float = 0.0;
10734}
10735#endif
10736
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737/*
10738 * "expand()" function
10739 */
10740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010742 typval_T *argvars;
10743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744{
10745 char_u *s;
10746 int len;
10747 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010748 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010750 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010751 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010753 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010754 if (argvars[1].v_type != VAR_UNKNOWN
10755 && argvars[2].v_type != VAR_UNKNOWN
10756 && get_tv_number_chk(&argvars[2], &error)
10757 && !error)
10758 {
10759 rettv->v_type = VAR_LIST;
10760 rettv->vval.v_list = NULL;
10761 }
10762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010763 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 if (*s == '%' || *s == '#' || *s == '<')
10765 {
10766 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010767 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010769 if (rettv->v_type == VAR_LIST)
10770 {
10771 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10772 list_append_string(rettv->vval.v_list, result, -1);
10773 else
10774 vim_free(result);
10775 }
10776 else
10777 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 }
10779 else
10780 {
10781 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010782 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010783 if (argvars[1].v_type != VAR_UNKNOWN
10784 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010785 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010786 if (!error)
10787 {
10788 ExpandInit(&xpc);
10789 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010790 if (p_wic)
10791 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010792 if (rettv->v_type == VAR_STRING)
10793 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10794 options, WILD_ALL);
10795 else if (rettv_list_alloc(rettv) != FAIL)
10796 {
10797 int i;
10798
10799 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10800 for (i = 0; i < xpc.xp_numfiles; i++)
10801 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10802 ExpandCleanup(&xpc);
10803 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010804 }
10805 else
10806 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 }
10808}
10809
10810/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010811 * Go over all entries in "d2" and add them to "d1".
10812 * When "action" is "error" then a duplicate key is an error.
10813 * When "action" is "force" then a duplicate key is overwritten.
10814 * Otherwise duplicate keys are ignored ("action" is "keep").
10815 */
10816 void
10817dict_extend(d1, d2, action)
10818 dict_T *d1;
10819 dict_T *d2;
10820 char_u *action;
10821{
10822 dictitem_T *di1;
10823 hashitem_T *hi2;
10824 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010825 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010826
10827 todo = (int)d2->dv_hashtab.ht_used;
10828 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10829 {
10830 if (!HASHITEM_EMPTY(hi2))
10831 {
10832 --todo;
10833 di1 = dict_find(d1, hi2->hi_key, -1);
10834 if (d1->dv_scope != 0)
10835 {
10836 /* Disallow replacing a builtin function in l: and g:.
10837 * Check the key to be valid when adding to any
10838 * scope. */
10839 if (d1->dv_scope == VAR_DEF_SCOPE
10840 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10841 && var_check_func_name(hi2->hi_key,
10842 di1 == NULL))
10843 break;
10844 if (!valid_varname(hi2->hi_key))
10845 break;
10846 }
10847 if (di1 == NULL)
10848 {
10849 di1 = dictitem_copy(HI2DI(hi2));
10850 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10851 dictitem_free(di1);
10852 }
10853 else if (*action == 'e')
10854 {
10855 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10856 break;
10857 }
10858 else if (*action == 'f' && HI2DI(hi2) != di1)
10859 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010860 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10861 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010862 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010863 clear_tv(&di1->di_tv);
10864 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10865 }
10866 }
10867 }
10868}
10869
10870/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010871 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010872 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010873 */
10874 static void
10875f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010876 typval_T *argvars;
10877 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010878{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010879 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010880
Bram Moolenaare9a41262005-01-15 22:18:47 +000010881 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010882 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010883 list_T *l1, *l2;
10884 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010885 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010886 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010887
Bram Moolenaare9a41262005-01-15 22:18:47 +000010888 l1 = argvars[0].vval.v_list;
10889 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010890 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010891 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010892 {
10893 if (argvars[2].v_type != VAR_UNKNOWN)
10894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010895 before = get_tv_number_chk(&argvars[2], &error);
10896 if (error)
10897 return; /* type error; errmsg already given */
10898
Bram Moolenaar758711c2005-02-02 23:11:38 +000010899 if (before == l1->lv_len)
10900 item = NULL;
10901 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010902 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010903 item = list_find(l1, before);
10904 if (item == NULL)
10905 {
10906 EMSGN(_(e_listidx), before);
10907 return;
10908 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010909 }
10910 }
10911 else
10912 item = NULL;
10913 list_extend(l1, l2, item);
10914
Bram Moolenaare9a41262005-01-15 22:18:47 +000010915 copy_tv(&argvars[0], rettv);
10916 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010917 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010918 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10919 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010920 dict_T *d1, *d2;
10921 char_u *action;
10922 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010923
10924 d1 = argvars[0].vval.v_dict;
10925 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010926 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010927 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010928 {
10929 /* Check the third argument. */
10930 if (argvars[2].v_type != VAR_UNKNOWN)
10931 {
10932 static char *(av[]) = {"keep", "force", "error"};
10933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010934 action = get_tv_string_chk(&argvars[2]);
10935 if (action == NULL)
10936 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010937 for (i = 0; i < 3; ++i)
10938 if (STRCMP(action, av[i]) == 0)
10939 break;
10940 if (i == 3)
10941 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010942 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010943 return;
10944 }
10945 }
10946 else
10947 action = (char_u *)"force";
10948
Bram Moolenaara9922d62013-05-30 13:01:18 +020010949 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010950
Bram Moolenaare9a41262005-01-15 22:18:47 +000010951 copy_tv(&argvars[0], rettv);
10952 }
10953 }
10954 else
10955 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010956}
10957
10958/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010959 * "feedkeys()" function
10960 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010961 static void
10962f_feedkeys(argvars, rettv)
10963 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010964 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010965{
10966 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010967 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010968 char_u *keys, *flags;
10969 char_u nbuf[NUMBUFLEN];
10970 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010971 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010972
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010973 /* This is not allowed in the sandbox. If the commands would still be
10974 * executed in the sandbox it would be OK, but it probably happens later,
10975 * when "sandbox" is no longer set. */
10976 if (check_secure())
10977 return;
10978
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010979 keys = get_tv_string(&argvars[0]);
10980 if (*keys != NUL)
10981 {
10982 if (argvars[1].v_type != VAR_UNKNOWN)
10983 {
10984 flags = get_tv_string_buf(&argvars[1], nbuf);
10985 for ( ; *flags != NUL; ++flags)
10986 {
10987 switch (*flags)
10988 {
10989 case 'n': remap = FALSE; break;
10990 case 'm': remap = TRUE; break;
10991 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010992 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010993 }
10994 }
10995 }
10996
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010997 /* Need to escape K_SPECIAL and CSI before putting the string in the
10998 * typeahead buffer. */
10999 keys_esc = vim_strsave_escape_csi(keys);
11000 if (keys_esc != NULL)
11001 {
11002 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011003 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011004 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011005 if (vgetc_busy)
11006 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011007 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011008 }
11009}
11010
11011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 * "filereadable()" function
11013 */
11014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011015f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011016 typval_T *argvars;
11017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011019 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 char_u *p;
11021 int n;
11022
Bram Moolenaarc236c162008-07-13 17:41:49 +000011023#ifndef O_NONBLOCK
11024# define O_NONBLOCK 0
11025#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011026 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011027 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11028 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 {
11030 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011031 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 }
11033 else
11034 n = FALSE;
11035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037}
11038
11039/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011040 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 * rights to write into.
11042 */
11043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011045 typval_T *argvars;
11046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011048 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049}
11050
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011051static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011052
11053 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011054findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011055 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011056 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011057 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011058{
11059#ifdef FEAT_SEARCHPATH
11060 char_u *fname;
11061 char_u *fresult = NULL;
11062 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11063 char_u *p;
11064 char_u pathbuf[NUMBUFLEN];
11065 int count = 1;
11066 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011067 int error = FALSE;
11068#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011069
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011070 rettv->vval.v_string = NULL;
11071 rettv->v_type = VAR_STRING;
11072
11073#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011075
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011076 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011077 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11079 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011080 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 else
11082 {
11083 if (*p != NUL)
11084 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011086 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011087 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011088 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011089 }
11090
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011091 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11092 error = TRUE;
11093
11094 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011095 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011096 do
11097 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011098 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011099 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011100 fresult = find_file_in_path_option(first ? fname : NULL,
11101 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011102 0, first, path,
11103 find_what,
11104 curbuf->b_ffname,
11105 find_what == FINDFILE_DIR
11106 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011107 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011108
11109 if (fresult != NULL && rettv->v_type == VAR_LIST)
11110 list_append_string(rettv->vval.v_list, fresult, -1);
11111
11112 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011114
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011115 if (rettv->v_type == VAR_STRING)
11116 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011117#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011118}
11119
Bram Moolenaar33570922005-01-25 22:26:29 +000011120static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
11121static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011122
11123/*
11124 * Implementation of map() and filter().
11125 */
11126 static void
11127filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011128 typval_T *argvars;
11129 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011130 int map;
11131{
11132 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011133 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011134 listitem_T *li, *nli;
11135 list_T *l = NULL;
11136 dictitem_T *di;
11137 hashtab_T *ht;
11138 hashitem_T *hi;
11139 dict_T *d = NULL;
11140 typval_T save_val;
11141 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011142 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011143 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011144 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011145 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011146 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011147 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011148 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011149
Bram Moolenaare9a41262005-01-15 22:18:47 +000011150 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011151 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011152 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011153 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011154 return;
11155 }
11156 else if (argvars[0].v_type == VAR_DICT)
11157 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011158 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011159 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011160 return;
11161 }
11162 else
11163 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011164 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011165 return;
11166 }
11167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011168 expr = get_tv_string_buf_chk(&argvars[1], buf);
11169 /* On type errors, the preceding call has already displayed an error
11170 * message. Avoid a misleading error message for an empty string that
11171 * was not passed as argument. */
11172 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011174 prepare_vimvar(VV_VAL, &save_val);
11175 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011176
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011177 /* We reset "did_emsg" to be able to detect whether an error
11178 * occurred during evaluation of the expression. */
11179 save_did_emsg = did_emsg;
11180 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011181
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011182 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011183 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011185 vimvars[VV_KEY].vv_type = VAR_STRING;
11186
11187 ht = &d->dv_hashtab;
11188 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011189 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011190 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011192 if (!HASHITEM_EMPTY(hi))
11193 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011194 int r;
11195
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011196 --todo;
11197 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011198 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011199 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11200 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011201 break;
11202 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011203 r = filter_map_one(&di->di_tv, expr, map, &rem);
11204 clear_tv(&vimvars[VV_KEY].vv_tv);
11205 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011206 break;
11207 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011208 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011209 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11210 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011211 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011212 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011213 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011214 }
11215 }
11216 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011217 }
11218 else
11219 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011220 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11221
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222 for (li = l->lv_first; li != NULL; li = nli)
11223 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011224 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011225 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011226 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011227 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011228 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011229 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011230 break;
11231 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011232 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011233 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011234 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011235 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011236
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011237 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011238 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011239
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011240 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011241 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011242
11243 copy_tv(&argvars[0], rettv);
11244}
11245
11246 static int
11247filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011248 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011249 char_u *expr;
11250 int map;
11251 int *remp;
11252{
Bram Moolenaar33570922005-01-25 22:26:29 +000011253 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011254 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011255 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011256
Bram Moolenaar33570922005-01-25 22:26:29 +000011257 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011258 s = expr;
11259 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011260 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011261 if (*s != NUL) /* check for trailing chars after expr */
11262 {
11263 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011264 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011265 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011266 }
11267 if (map)
11268 {
11269 /* map(): replace the list item value */
11270 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011271 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011272 *tv = rettv;
11273 }
11274 else
11275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011276 int error = FALSE;
11277
Bram Moolenaare9a41262005-01-15 22:18:47 +000011278 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011279 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011280 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011281 /* On type error, nothing has been removed; return FAIL to stop the
11282 * loop. The error message was given by get_tv_number_chk(). */
11283 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011284 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011285 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011286 retval = OK;
11287theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011288 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011289 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011290}
11291
11292/*
11293 * "filter()" function
11294 */
11295 static void
11296f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011297 typval_T *argvars;
11298 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011299{
11300 filter_map(argvars, rettv, FALSE);
11301}
11302
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011303/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011304 * "finddir({fname}[, {path}[, {count}]])" function
11305 */
11306 static void
11307f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011308 typval_T *argvars;
11309 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011310{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011311 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011312}
11313
11314/*
11315 * "findfile({fname}[, {path}[, {count}]])" function
11316 */
11317 static void
11318f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 typval_T *argvars;
11320 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011321{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011322 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011323}
11324
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011325#ifdef FEAT_FLOAT
11326/*
11327 * "float2nr({float})" function
11328 */
11329 static void
11330f_float2nr(argvars, rettv)
11331 typval_T *argvars;
11332 typval_T *rettv;
11333{
11334 float_T f;
11335
11336 if (get_float_arg(argvars, &f) == OK)
11337 {
11338 if (f < -0x7fffffff)
11339 rettv->vval.v_number = -0x7fffffff;
11340 else if (f > 0x7fffffff)
11341 rettv->vval.v_number = 0x7fffffff;
11342 else
11343 rettv->vval.v_number = (varnumber_T)f;
11344 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011345}
11346
11347/*
11348 * "floor({float})" function
11349 */
11350 static void
11351f_floor(argvars, rettv)
11352 typval_T *argvars;
11353 typval_T *rettv;
11354{
11355 float_T f;
11356
11357 rettv->v_type = VAR_FLOAT;
11358 if (get_float_arg(argvars, &f) == OK)
11359 rettv->vval.v_float = floor(f);
11360 else
11361 rettv->vval.v_float = 0.0;
11362}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011363
11364/*
11365 * "fmod()" function
11366 */
11367 static void
11368f_fmod(argvars, rettv)
11369 typval_T *argvars;
11370 typval_T *rettv;
11371{
11372 float_T fx, fy;
11373
11374 rettv->v_type = VAR_FLOAT;
11375 if (get_float_arg(argvars, &fx) == OK
11376 && get_float_arg(&argvars[1], &fy) == OK)
11377 rettv->vval.v_float = fmod(fx, fy);
11378 else
11379 rettv->vval.v_float = 0.0;
11380}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011381#endif
11382
Bram Moolenaar0d660222005-01-07 21:51:51 +000011383/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011384 * "fnameescape({string})" function
11385 */
11386 static void
11387f_fnameescape(argvars, rettv)
11388 typval_T *argvars;
11389 typval_T *rettv;
11390{
11391 rettv->vval.v_string = vim_strsave_fnameescape(
11392 get_tv_string(&argvars[0]), FALSE);
11393 rettv->v_type = VAR_STRING;
11394}
11395
11396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 * "fnamemodify({fname}, {mods})" function
11398 */
11399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011401 typval_T *argvars;
11402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403{
11404 char_u *fname;
11405 char_u *mods;
11406 int usedlen = 0;
11407 int len;
11408 char_u *fbuf = NULL;
11409 char_u buf[NUMBUFLEN];
11410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011411 fname = get_tv_string_chk(&argvars[0]);
11412 mods = get_tv_string_buf_chk(&argvars[1], buf);
11413 if (fname == NULL || mods == NULL)
11414 fname = NULL;
11415 else
11416 {
11417 len = (int)STRLEN(fname);
11418 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011423 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 vim_free(fbuf);
11427}
11428
Bram Moolenaar33570922005-01-25 22:26:29 +000011429static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430
11431/*
11432 * "foldclosed()" function
11433 */
11434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011436 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011437 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011438 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439{
11440#ifdef FEAT_FOLDING
11441 linenr_T lnum;
11442 linenr_T first, last;
11443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11446 {
11447 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11448 {
11449 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011450 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011452 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453 return;
11454 }
11455 }
11456#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458}
11459
11460/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011461 * "foldclosed()" function
11462 */
11463 static void
11464f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011465 typval_T *argvars;
11466 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011467{
11468 foldclosed_both(argvars, rettv, FALSE);
11469}
11470
11471/*
11472 * "foldclosedend()" function
11473 */
11474 static void
11475f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011476 typval_T *argvars;
11477 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011478{
11479 foldclosed_both(argvars, rettv, TRUE);
11480}
11481
11482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 * "foldlevel()" function
11484 */
11485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011487 typval_T *argvars UNUSED;
11488 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489{
11490#ifdef FEAT_FOLDING
11491 linenr_T lnum;
11492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011493 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497}
11498
11499/*
11500 * "foldtext()" function
11501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011504 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506{
11507#ifdef FEAT_FOLDING
11508 linenr_T lnum;
11509 char_u *s;
11510 char_u *r;
11511 int len;
11512 char *txt;
11513#endif
11514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011515 rettv->v_type = VAR_STRING;
11516 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011518 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11519 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11520 <= curbuf->b_ml.ml_line_count
11521 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 {
11523 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011524 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11525 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 {
11527 if (!linewhite(lnum))
11528 break;
11529 ++lnum;
11530 }
11531
11532 /* Find interesting text in this line. */
11533 s = skipwhite(ml_get(lnum));
11534 /* skip C comment-start */
11535 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011538 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011539 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011540 {
11541 s = skipwhite(ml_get(lnum + 1));
11542 if (*s == '*')
11543 s = skipwhite(s + 1);
11544 }
11545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 txt = _("+-%s%3ld lines: ");
11547 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011548 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 + 20 /* for %3ld */
11550 + STRLEN(s))); /* concatenated */
11551 if (r != NULL)
11552 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011553 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11554 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11555 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556 len = (int)STRLEN(r);
11557 STRCAT(r, s);
11558 /* remove 'foldmarker' and 'commentstring' */
11559 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 }
11562 }
11563#endif
11564}
11565
11566/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011567 * "foldtextresult(lnum)" function
11568 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011571 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011572 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011573{
11574#ifdef FEAT_FOLDING
11575 linenr_T lnum;
11576 char_u *text;
11577 char_u buf[51];
11578 foldinfo_T foldinfo;
11579 int fold_count;
11580#endif
11581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011582 rettv->v_type = VAR_STRING;
11583 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011584#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011586 /* treat illegal types and illegal string values for {lnum} the same */
11587 if (lnum < 0)
11588 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011589 fold_count = foldedCount(curwin, lnum, &foldinfo);
11590 if (fold_count > 0)
11591 {
11592 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11593 &foldinfo, buf);
11594 if (text == buf)
11595 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011597 }
11598#endif
11599}
11600
11601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602 * "foreground()" function
11603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011606 typval_T *argvars UNUSED;
11607 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609#ifdef FEAT_GUI
11610 if (gui.in_use)
11611 gui_mch_set_foreground();
11612#else
11613# ifdef WIN32
11614 win32_set_foreground();
11615# endif
11616#endif
11617}
11618
11619/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011620 * "function()" function
11621 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011623f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011624 typval_T *argvars;
11625 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011626{
11627 char_u *s;
11628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011629 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011630 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011631 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011632 /* Don't check an autoload name for existence here. */
11633 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011634 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011635 else
11636 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011637 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011638 {
11639 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011640 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011641
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011642 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11643 * also be called from another script. Using trans_function_name()
11644 * would also work, but some plugins depend on the name being
11645 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011646 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011647 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011648 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011649 if (rettv->vval.v_string != NULL)
11650 {
11651 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011652 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011653 }
11654 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011655 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011656 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011657 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011658 }
11659}
11660
11661/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011662 * "garbagecollect()" function
11663 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011664 static void
11665f_garbagecollect(argvars, rettv)
11666 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011667 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011668{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011669 /* This is postponed until we are back at the toplevel, because we may be
11670 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11671 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011672
11673 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11674 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011675}
11676
11677/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011678 * "get()" function
11679 */
11680 static void
11681f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011682 typval_T *argvars;
11683 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011684{
Bram Moolenaar33570922005-01-25 22:26:29 +000011685 listitem_T *li;
11686 list_T *l;
11687 dictitem_T *di;
11688 dict_T *d;
11689 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011690
Bram Moolenaare9a41262005-01-15 22:18:47 +000011691 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011692 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011693 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011695 int error = FALSE;
11696
11697 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11698 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011699 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011700 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011701 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011702 else if (argvars[0].v_type == VAR_DICT)
11703 {
11704 if ((d = argvars[0].vval.v_dict) != NULL)
11705 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011706 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011707 if (di != NULL)
11708 tv = &di->di_tv;
11709 }
11710 }
11711 else
11712 EMSG2(_(e_listdictarg), "get()");
11713
11714 if (tv == NULL)
11715 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011716 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011717 copy_tv(&argvars[2], rettv);
11718 }
11719 else
11720 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011721}
11722
Bram Moolenaar342337a2005-07-21 21:11:17 +000011723static 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 +000011724
11725/*
11726 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011727 * Return a range (from start to end) of lines in rettv from the specified
11728 * buffer.
11729 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011730 */
11731 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011732get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011733 buf_T *buf;
11734 linenr_T start;
11735 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011736 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011737 typval_T *rettv;
11738{
11739 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011740
Bram Moolenaar959a1432013-12-14 12:17:38 +010011741 rettv->v_type = VAR_STRING;
11742 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011743 if (retlist && rettv_list_alloc(rettv) == FAIL)
11744 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011745
11746 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11747 return;
11748
11749 if (!retlist)
11750 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011751 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11752 p = ml_get_buf(buf, start, FALSE);
11753 else
11754 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011755 rettv->vval.v_string = vim_strsave(p);
11756 }
11757 else
11758 {
11759 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011760 return;
11761
11762 if (start < 1)
11763 start = 1;
11764 if (end > buf->b_ml.ml_line_count)
11765 end = buf->b_ml.ml_line_count;
11766 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011767 if (list_append_string(rettv->vval.v_list,
11768 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011769 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011770 }
11771}
11772
11773/*
11774 * "getbufline()" function
11775 */
11776 static void
11777f_getbufline(argvars, rettv)
11778 typval_T *argvars;
11779 typval_T *rettv;
11780{
11781 linenr_T lnum;
11782 linenr_T end;
11783 buf_T *buf;
11784
11785 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11786 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011787 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011788 --emsg_off;
11789
Bram Moolenaar661b1822005-07-28 22:36:45 +000011790 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011791 if (argvars[2].v_type == VAR_UNKNOWN)
11792 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011793 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011794 end = get_tv_lnum_buf(&argvars[2], buf);
11795
Bram Moolenaar342337a2005-07-21 21:11:17 +000011796 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011797}
11798
Bram Moolenaar0d660222005-01-07 21:51:51 +000011799/*
11800 * "getbufvar()" function
11801 */
11802 static void
11803f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011804 typval_T *argvars;
11805 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011806{
11807 buf_T *buf;
11808 buf_T *save_curbuf;
11809 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011810 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011811 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011813 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11814 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011815 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011816 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011817
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011818 rettv->v_type = VAR_STRING;
11819 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011820
11821 if (buf != NULL && varname != NULL)
11822 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011823 /* set curbuf to be our buf, temporarily */
11824 save_curbuf = curbuf;
11825 curbuf = buf;
11826
Bram Moolenaar0d660222005-01-07 21:51:51 +000011827 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011828 {
11829 if (get_option_tv(&varname, rettv, TRUE) == OK)
11830 done = TRUE;
11831 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011832 else if (STRCMP(varname, "changedtick") == 0)
11833 {
11834 rettv->v_type = VAR_NUMBER;
11835 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011836 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011837 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011838 else
11839 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011840 /* Look up the variable. */
11841 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11842 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11843 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011844 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011845 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011846 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011847 done = TRUE;
11848 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011849 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011850
11851 /* restore previous notion of curbuf */
11852 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011853 }
11854
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011855 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11856 /* use the default value */
11857 copy_tv(&argvars[2], rettv);
11858
Bram Moolenaar0d660222005-01-07 21:51:51 +000011859 --emsg_off;
11860}
11861
11862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 * "getchar()" function
11864 */
11865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011866f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011867 typval_T *argvars;
11868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869{
11870 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011871 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011873 /* Position the cursor. Needed after a message that ends in a space. */
11874 windgoto(msg_row, msg_col);
11875
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876 ++no_mapping;
11877 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011878 for (;;)
11879 {
11880 if (argvars[0].v_type == VAR_UNKNOWN)
11881 /* getchar(): blocking wait. */
11882 n = safe_vgetc();
11883 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11884 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011885 n = vpeekc_any();
11886 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011887 /* illegal argument or getchar(0) and no char avail: return zero */
11888 n = 0;
11889 else
11890 /* getchar(0) and char avail: return char */
11891 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011892
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011893 if (n == K_IGNORE)
11894 continue;
11895 break;
11896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 --no_mapping;
11898 --allow_keys;
11899
Bram Moolenaar219b8702006-11-01 14:32:36 +000011900 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11901 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11902 vimvars[VV_MOUSE_COL].vv_nr = 0;
11903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 if (IS_SPECIAL(n) || mod_mask != 0)
11906 {
11907 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11908 int i = 0;
11909
11910 /* Turn a special key into three bytes, plus modifier. */
11911 if (mod_mask != 0)
11912 {
11913 temp[i++] = K_SPECIAL;
11914 temp[i++] = KS_MODIFIER;
11915 temp[i++] = mod_mask;
11916 }
11917 if (IS_SPECIAL(n))
11918 {
11919 temp[i++] = K_SPECIAL;
11920 temp[i++] = K_SECOND(n);
11921 temp[i++] = K_THIRD(n);
11922 }
11923#ifdef FEAT_MBYTE
11924 else if (has_mbyte)
11925 i += (*mb_char2bytes)(n, temp + i);
11926#endif
11927 else
11928 temp[i++] = n;
11929 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011930 rettv->v_type = VAR_STRING;
11931 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011932
11933#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011934 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011935 {
11936 int row = mouse_row;
11937 int col = mouse_col;
11938 win_T *win;
11939 linenr_T lnum;
11940# ifdef FEAT_WINDOWS
11941 win_T *wp;
11942# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011943 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011944
11945 if (row >= 0 && col >= 0)
11946 {
11947 /* Find the window at the mouse coordinates and compute the
11948 * text position. */
11949 win = mouse_find_win(&row, &col);
11950 (void)mouse_comp_pos(win, &row, &col, &lnum);
11951# ifdef FEAT_WINDOWS
11952 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011953 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011954# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011955 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011956 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11957 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11958 }
11959 }
11960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961 }
11962}
11963
11964/*
11965 * "getcharmod()" function
11966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011969 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011972 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973}
11974
11975/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011976 * "getcharsearch()" function
11977 */
11978 static void
11979f_getcharsearch(argvars, rettv)
11980 typval_T *argvars UNUSED;
11981 typval_T *rettv;
11982{
11983 if (rettv_dict_alloc(rettv) != FAIL)
11984 {
11985 dict_T *dict = rettv->vval.v_dict;
11986
11987 dict_add_nr_str(dict, "char", 0L, last_csearch());
11988 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11989 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11990 }
11991}
11992
11993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 * "getcmdline()" function
11995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011998 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001 rettv->v_type = VAR_STRING;
12002 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003}
12004
12005/*
12006 * "getcmdpos()" function
12007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012009f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012010 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012013 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014}
12015
12016/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012017 * "getcmdtype()" function
12018 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012019 static void
12020f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012021 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012022 typval_T *rettv;
12023{
12024 rettv->v_type = VAR_STRING;
12025 rettv->vval.v_string = alloc(2);
12026 if (rettv->vval.v_string != NULL)
12027 {
12028 rettv->vval.v_string[0] = get_cmdline_type();
12029 rettv->vval.v_string[1] = NUL;
12030 }
12031}
12032
12033/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012034 * "getcmdwintype()" function
12035 */
12036 static void
12037f_getcmdwintype(argvars, rettv)
12038 typval_T *argvars UNUSED;
12039 typval_T *rettv;
12040{
12041 rettv->v_type = VAR_STRING;
12042 rettv->vval.v_string = NULL;
12043#ifdef FEAT_CMDWIN
12044 rettv->vval.v_string = alloc(2);
12045 if (rettv->vval.v_string != NULL)
12046 {
12047 rettv->vval.v_string[0] = cmdwin_type;
12048 rettv->vval.v_string[1] = NUL;
12049 }
12050#endif
12051}
12052
12053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054 * "getcwd()" function
12055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012058 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060{
Bram Moolenaard9462e32011-04-11 21:35:11 +020012061 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012064 rettv->vval.v_string = NULL;
12065 cwd = alloc(MAXPATHL);
12066 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020012068 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12069 {
12070 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020012072 if (rettv->vval.v_string != NULL)
12073 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020012075 }
12076 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 }
12078}
12079
12080/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012081 * "getfontname()" function
12082 */
12083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012084f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012085 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012086 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012087{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012088 rettv->v_type = VAR_STRING;
12089 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012090#ifdef FEAT_GUI
12091 if (gui.in_use)
12092 {
12093 GuiFont font;
12094 char_u *name = NULL;
12095
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012096 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012097 {
12098 /* Get the "Normal" font. Either the name saved by
12099 * hl_set_font_name() or from the font ID. */
12100 font = gui.norm_font;
12101 name = hl_get_font_name();
12102 }
12103 else
12104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012106 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12107 return;
12108 font = gui_mch_get_font(name, FALSE);
12109 if (font == NOFONT)
12110 return; /* Invalid font name, return empty string. */
12111 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012112 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012113 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012114 gui_mch_free_font(font);
12115 }
12116#endif
12117}
12118
12119/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012120 * "getfperm({fname})" function
12121 */
12122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012124 typval_T *argvars;
12125 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012126{
12127 char_u *fname;
12128 struct stat st;
12129 char_u *perm = NULL;
12130 char_u flags[] = "rwx";
12131 int i;
12132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012135 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012136 if (mch_stat((char *)fname, &st) >= 0)
12137 {
12138 perm = vim_strsave((char_u *)"---------");
12139 if (perm != NULL)
12140 {
12141 for (i = 0; i < 9; i++)
12142 {
12143 if (st.st_mode & (1 << (8 - i)))
12144 perm[i] = flags[i % 3];
12145 }
12146 }
12147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012148 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012149}
12150
12151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 * "getfsize({fname})" function
12153 */
12154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012156 typval_T *argvars;
12157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158{
12159 char_u *fname;
12160 struct stat st;
12161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012162 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012164 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165
12166 if (mch_stat((char *)fname, &st) >= 0)
12167 {
12168 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012173
12174 /* non-perfect check for overflow */
12175 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12176 rettv->vval.v_number = -2;
12177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178 }
12179 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181}
12182
12183/*
12184 * "getftime({fname})" function
12185 */
12186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012187f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012188 typval_T *argvars;
12189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190{
12191 char_u *fname;
12192 struct stat st;
12193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195
12196 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012197 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012199 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200}
12201
12202/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012203 * "getftype({fname})" function
12204 */
12205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012206f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012207 typval_T *argvars;
12208 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012209{
12210 char_u *fname;
12211 struct stat st;
12212 char_u *type = NULL;
12213 char *t;
12214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012215 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012217 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012218 if (mch_lstat((char *)fname, &st) >= 0)
12219 {
12220#ifdef S_ISREG
12221 if (S_ISREG(st.st_mode))
12222 t = "file";
12223 else if (S_ISDIR(st.st_mode))
12224 t = "dir";
12225# ifdef S_ISLNK
12226 else if (S_ISLNK(st.st_mode))
12227 t = "link";
12228# endif
12229# ifdef S_ISBLK
12230 else if (S_ISBLK(st.st_mode))
12231 t = "bdev";
12232# endif
12233# ifdef S_ISCHR
12234 else if (S_ISCHR(st.st_mode))
12235 t = "cdev";
12236# endif
12237# ifdef S_ISFIFO
12238 else if (S_ISFIFO(st.st_mode))
12239 t = "fifo";
12240# endif
12241# ifdef S_ISSOCK
12242 else if (S_ISSOCK(st.st_mode))
12243 t = "fifo";
12244# endif
12245 else
12246 t = "other";
12247#else
12248# ifdef S_IFMT
12249 switch (st.st_mode & S_IFMT)
12250 {
12251 case S_IFREG: t = "file"; break;
12252 case S_IFDIR: t = "dir"; break;
12253# ifdef S_IFLNK
12254 case S_IFLNK: t = "link"; break;
12255# endif
12256# ifdef S_IFBLK
12257 case S_IFBLK: t = "bdev"; break;
12258# endif
12259# ifdef S_IFCHR
12260 case S_IFCHR: t = "cdev"; break;
12261# endif
12262# ifdef S_IFIFO
12263 case S_IFIFO: t = "fifo"; break;
12264# endif
12265# ifdef S_IFSOCK
12266 case S_IFSOCK: t = "socket"; break;
12267# endif
12268 default: t = "other";
12269 }
12270# else
12271 if (mch_isdir(fname))
12272 t = "dir";
12273 else
12274 t = "file";
12275# endif
12276#endif
12277 type = vim_strsave((char_u *)t);
12278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012279 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012280}
12281
12282/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012283 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012284 */
12285 static void
12286f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012287 typval_T *argvars;
12288 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012289{
12290 linenr_T lnum;
12291 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012292 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012293
12294 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012295 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012296 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012297 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012298 retlist = FALSE;
12299 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012300 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012301 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012302 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012303 retlist = TRUE;
12304 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012305
Bram Moolenaar342337a2005-07-21 21:11:17 +000012306 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012307}
12308
12309/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012310 * "getmatches()" function
12311 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012312 static void
12313f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012314 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012315 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012316{
12317#ifdef FEAT_SEARCH_EXTRA
12318 dict_T *dict;
12319 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012320 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012321
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012322 if (rettv_list_alloc(rettv) == OK)
12323 {
12324 while (cur != NULL)
12325 {
12326 dict = dict_alloc();
12327 if (dict == NULL)
12328 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012329 if (cur->match.regprog == NULL)
12330 {
12331 /* match added with matchaddpos() */
12332 for (i = 0; i < MAXPOSMATCH; ++i)
12333 {
12334 llpos_T *llpos;
12335 char buf[6];
12336 list_T *l;
12337
12338 llpos = &cur->pos.pos[i];
12339 if (llpos->lnum == 0)
12340 break;
12341 l = list_alloc();
12342 if (l == NULL)
12343 break;
12344 list_append_number(l, (varnumber_T)llpos->lnum);
12345 if (llpos->col > 0)
12346 {
12347 list_append_number(l, (varnumber_T)llpos->col);
12348 list_append_number(l, (varnumber_T)llpos->len);
12349 }
12350 sprintf(buf, "pos%d", i + 1);
12351 dict_add_list(dict, buf, l);
12352 }
12353 }
12354 else
12355 {
12356 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12357 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012358 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012359 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12360 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012361# ifdef FEAT_CONCEAL
12362 if (cur->conceal_char)
12363 {
12364 char_u buf[MB_MAXBYTES + 1];
12365
12366 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12367 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12368 }
12369# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012370 list_append_dict(rettv->vval.v_list, dict);
12371 cur = cur->next;
12372 }
12373 }
12374#endif
12375}
12376
12377/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012378 * "getpid()" function
12379 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012380 static void
12381f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012382 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012383 typval_T *rettv;
12384{
12385 rettv->vval.v_number = mch_get_pid();
12386}
12387
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012388static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12389
12390/*
12391 * "getcurpos()" function
12392 */
12393 static void
12394f_getcurpos(argvars, rettv)
12395 typval_T *argvars;
12396 typval_T *rettv;
12397{
12398 getpos_both(argvars, rettv, TRUE);
12399}
12400
Bram Moolenaar18081e32008-02-20 19:11:07 +000012401/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012402 * "getpos(string)" function
12403 */
12404 static void
12405f_getpos(argvars, rettv)
12406 typval_T *argvars;
12407 typval_T *rettv;
12408{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012409 getpos_both(argvars, rettv, FALSE);
12410}
12411
12412 static void
12413getpos_both(argvars, rettv, getcurpos)
12414 typval_T *argvars;
12415 typval_T *rettv;
12416 int getcurpos;
12417{
Bram Moolenaara5525202006-03-02 22:52:09 +000012418 pos_T *fp;
12419 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012420 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012421
12422 if (rettv_list_alloc(rettv) == OK)
12423 {
12424 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012425 if (getcurpos)
12426 fp = &curwin->w_cursor;
12427 else
12428 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012429 if (fnum != -1)
12430 list_append_number(l, (varnumber_T)fnum);
12431 else
12432 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012433 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12434 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012435 list_append_number(l, (fp != NULL)
12436 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012437 : (varnumber_T)0);
12438 list_append_number(l,
12439#ifdef FEAT_VIRTUALEDIT
12440 (fp != NULL) ? (varnumber_T)fp->coladd :
12441#endif
12442 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012443 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012444 list_append_number(l, curwin->w_curswant == MAXCOL ?
12445 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012446 }
12447 else
12448 rettv->vval.v_number = FALSE;
12449}
12450
12451/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012452 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012453 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012454 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012455f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012456 typval_T *argvars UNUSED;
12457 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012458{
12459#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012460 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012461#endif
12462
Bram Moolenaar2641f772005-03-25 21:58:17 +000012463#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012464 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012465 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012466 wp = NULL;
12467 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12468 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012469 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012470 if (wp == NULL)
12471 return;
12472 }
12473
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012474 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012475 }
12476#endif
12477}
12478
12479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480 * "getreg()" function
12481 */
12482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012483f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012484 typval_T *argvars;
12485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486{
12487 char_u *strregname;
12488 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012489 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012490 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012491 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012493 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012495 strregname = get_tv_string_chk(&argvars[0]);
12496 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012497 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012499 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012500 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12501 return_list = get_tv_number_chk(&argvars[2], &error);
12502 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012505 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012506
12507 if (error)
12508 return;
12509
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510 regname = (strregname == NULL ? '"' : *strregname);
12511 if (regname == 0)
12512 regname = '"';
12513
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012514 if (return_list)
12515 {
12516 rettv->v_type = VAR_LIST;
12517 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12518 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012519 if (rettv->vval.v_list != NULL)
12520 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012521 }
12522 else
12523 {
12524 rettv->v_type = VAR_STRING;
12525 rettv->vval.v_string = get_reg_contents(regname,
12526 arg2 ? GREG_EXPR_SRC : 0);
12527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528}
12529
12530/*
12531 * "getregtype()" function
12532 */
12533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012534f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012535 typval_T *argvars;
12536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537{
12538 char_u *strregname;
12539 int regname;
12540 char_u buf[NUMBUFLEN + 2];
12541 long reglen = 0;
12542
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012543 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012544 {
12545 strregname = get_tv_string_chk(&argvars[0]);
12546 if (strregname == NULL) /* type error; errmsg already given */
12547 {
12548 rettv->v_type = VAR_STRING;
12549 rettv->vval.v_string = NULL;
12550 return;
12551 }
12552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553 else
12554 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012555 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556
12557 regname = (strregname == NULL ? '"' : *strregname);
12558 if (regname == 0)
12559 regname = '"';
12560
12561 buf[0] = NUL;
12562 buf[1] = NUL;
12563 switch (get_reg_type(regname, &reglen))
12564 {
12565 case MLINE: buf[0] = 'V'; break;
12566 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567 case MBLOCK:
12568 buf[0] = Ctrl_V;
12569 sprintf((char *)buf + 1, "%ld", reglen + 1);
12570 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012572 rettv->v_type = VAR_STRING;
12573 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574}
12575
12576/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012577 * "gettabvar()" function
12578 */
12579 static void
12580f_gettabvar(argvars, rettv)
12581 typval_T *argvars;
12582 typval_T *rettv;
12583{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012584 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012585 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012586 dictitem_T *v;
12587 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012588 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012589
12590 rettv->v_type = VAR_STRING;
12591 rettv->vval.v_string = NULL;
12592
12593 varname = get_tv_string_chk(&argvars[1]);
12594 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12595 if (tp != NULL && varname != NULL)
12596 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012597 /* Set tp to be our tabpage, temporarily. Also set the window to the
12598 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012599 if (switch_win(&oldcurwin, &oldtabpage,
12600 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012601 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012602 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012603 /* look up the variable */
12604 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12605 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12606 if (v != NULL)
12607 {
12608 copy_tv(&v->di_tv, rettv);
12609 done = TRUE;
12610 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012611 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012612
12613 /* restore previous notion of curwin */
12614 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012615 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012616
12617 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012618 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012619}
12620
12621/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012622 * "gettabwinvar()" function
12623 */
12624 static void
12625f_gettabwinvar(argvars, rettv)
12626 typval_T *argvars;
12627 typval_T *rettv;
12628{
12629 getwinvar(argvars, rettv, 1);
12630}
12631
12632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633 * "getwinposx()" function
12634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012636f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012637 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012640 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641#ifdef FEAT_GUI
12642 if (gui.in_use)
12643 {
12644 int x, y;
12645
12646 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012647 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648 }
12649#endif
12650}
12651
12652/*
12653 * "getwinposy()" function
12654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012656f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012657 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012660 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661#ifdef FEAT_GUI
12662 if (gui.in_use)
12663 {
12664 int x, y;
12665
12666 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012667 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668 }
12669#endif
12670}
12671
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012672/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012673 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012674 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012675 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012676find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012677 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012678 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012679{
12680#ifdef FEAT_WINDOWS
12681 win_T *wp;
12682#endif
12683 int nr;
12684
12685 nr = get_tv_number_chk(vp, NULL);
12686
12687#ifdef FEAT_WINDOWS
12688 if (nr < 0)
12689 return NULL;
12690 if (nr == 0)
12691 return curwin;
12692
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012693 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12694 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012695 if (--nr <= 0)
12696 break;
12697 return wp;
12698#else
12699 if (nr == 0 || nr == 1)
12700 return curwin;
12701 return NULL;
12702#endif
12703}
12704
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705/*
12706 * "getwinvar()" function
12707 */
12708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012709f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012710 typval_T *argvars;
12711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012713 getwinvar(argvars, rettv, 0);
12714}
12715
12716/*
12717 * getwinvar() and gettabwinvar()
12718 */
12719 static void
12720getwinvar(argvars, rettv, off)
12721 typval_T *argvars;
12722 typval_T *rettv;
12723 int off; /* 1 for gettabwinvar() */
12724{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012725 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012727 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012728 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012729 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012730#ifdef FEAT_WINDOWS
12731 win_T *oldcurwin;
12732 tabpage_T *oldtabpage;
12733 int need_switch_win;
12734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012736#ifdef FEAT_WINDOWS
12737 if (off == 1)
12738 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12739 else
12740 tp = curtab;
12741#endif
12742 win = find_win_by_nr(&argvars[off], tp);
12743 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012744 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012746 rettv->v_type = VAR_STRING;
12747 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748
12749 if (win != NULL && varname != NULL)
12750 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012751#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012752 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012753 * otherwise the window is not valid. Only do this when needed,
12754 * autocommands get blocked. */
12755 need_switch_win = !(tp == curtab && win == curwin);
12756 if (!need_switch_win
12757 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12758#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012759 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012760 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012761 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012762 if (get_option_tv(&varname, rettv, 1) == OK)
12763 done = TRUE;
12764 }
12765 else
12766 {
12767 /* Look up the variable. */
12768 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12769 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12770 varname, FALSE);
12771 if (v != NULL)
12772 {
12773 copy_tv(&v->di_tv, rettv);
12774 done = TRUE;
12775 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012778
Bram Moolenaarba117c22015-09-29 16:53:22 +020012779#ifdef FEAT_WINDOWS
12780 if (need_switch_win)
12781 /* restore previous notion of curwin */
12782 restore_win(oldcurwin, oldtabpage, TRUE);
12783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784 }
12785
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012786 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12787 /* use the default return value */
12788 copy_tv(&argvars[off + 2], rettv);
12789
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790 --emsg_off;
12791}
12792
12793/*
12794 * "glob()" function
12795 */
12796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012798 typval_T *argvars;
12799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012801 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012803 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012805 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012806 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012808 if (argvars[1].v_type != VAR_UNKNOWN)
12809 {
12810 if (get_tv_number_chk(&argvars[1], &error))
12811 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012812 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012813 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012814 if (get_tv_number_chk(&argvars[2], &error))
12815 {
12816 rettv->v_type = VAR_LIST;
12817 rettv->vval.v_list = NULL;
12818 }
12819 if (argvars[3].v_type != VAR_UNKNOWN
12820 && get_tv_number_chk(&argvars[3], &error))
12821 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012822 }
12823 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012824 if (!error)
12825 {
12826 ExpandInit(&xpc);
12827 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012828 if (p_wic)
12829 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012830 if (rettv->v_type == VAR_STRING)
12831 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012832 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012833 else if (rettv_list_alloc(rettv) != FAIL)
12834 {
12835 int i;
12836
12837 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12838 NULL, options, WILD_ALL_KEEP);
12839 for (i = 0; i < xpc.xp_numfiles; i++)
12840 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12841
12842 ExpandCleanup(&xpc);
12843 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012844 }
12845 else
12846 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847}
12848
12849/*
12850 * "globpath()" function
12851 */
12852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012854 typval_T *argvars;
12855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012857 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012859 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012860 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012861 garray_T ga;
12862 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012864 /* When the optional second argument is non-zero, don't remove matches
12865 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012866 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012867 if (argvars[2].v_type != VAR_UNKNOWN)
12868 {
12869 if (get_tv_number_chk(&argvars[2], &error))
12870 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012871 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012872 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012873 if (get_tv_number_chk(&argvars[3], &error))
12874 {
12875 rettv->v_type = VAR_LIST;
12876 rettv->vval.v_list = NULL;
12877 }
12878 if (argvars[4].v_type != VAR_UNKNOWN
12879 && get_tv_number_chk(&argvars[4], &error))
12880 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012881 }
12882 }
12883 if (file != NULL && !error)
12884 {
12885 ga_init2(&ga, (int)sizeof(char_u *), 10);
12886 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12887 if (rettv->v_type == VAR_STRING)
12888 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12889 else if (rettv_list_alloc(rettv) != FAIL)
12890 for (i = 0; i < ga.ga_len; ++i)
12891 list_append_string(rettv->vval.v_list,
12892 ((char_u **)(ga.ga_data))[i], -1);
12893 ga_clear_strings(&ga);
12894 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012895 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012896 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897}
12898
12899/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012900 * "glob2regpat()" function
12901 */
12902 static void
12903f_glob2regpat(argvars, rettv)
12904 typval_T *argvars;
12905 typval_T *rettv;
12906{
12907 char_u *pat = get_tv_string_chk(&argvars[0]);
12908
12909 rettv->v_type = VAR_STRING;
12910 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12911}
12912
12913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914 * "has()" function
12915 */
12916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012917f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012918 typval_T *argvars;
12919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920{
12921 int i;
12922 char_u *name;
12923 int n = FALSE;
12924 static char *(has_list[]) =
12925 {
12926#ifdef AMIGA
12927 "amiga",
12928# ifdef FEAT_ARP
12929 "arp",
12930# endif
12931#endif
12932#ifdef __BEOS__
12933 "beos",
12934#endif
12935#ifdef MSDOS
12936# ifdef DJGPP
12937 "dos32",
12938# else
12939 "dos16",
12940# endif
12941#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012942#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943 "mac",
12944#endif
12945#if defined(MACOS_X_UNIX)
12946 "macunix",
12947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948#ifdef __QNX__
12949 "qnx",
12950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951#ifdef UNIX
12952 "unix",
12953#endif
12954#ifdef VMS
12955 "vms",
12956#endif
12957#ifdef WIN16
12958 "win16",
12959#endif
12960#ifdef WIN32
12961 "win32",
12962#endif
12963#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12964 "win32unix",
12965#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012966#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967 "win64",
12968#endif
12969#ifdef EBCDIC
12970 "ebcdic",
12971#endif
12972#ifndef CASE_INSENSITIVE_FILENAME
12973 "fname_case",
12974#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012975#ifdef HAVE_ACL
12976 "acl",
12977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978#ifdef FEAT_ARABIC
12979 "arabic",
12980#endif
12981#ifdef FEAT_AUTOCMD
12982 "autocmd",
12983#endif
12984#ifdef FEAT_BEVAL
12985 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012986# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12987 "balloon_multiline",
12988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989#endif
12990#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12991 "builtin_terms",
12992# ifdef ALL_BUILTIN_TCAPS
12993 "all_builtin_terms",
12994# endif
12995#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012996#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12997 || defined(FEAT_GUI_W32) \
12998 || defined(FEAT_GUI_MOTIF))
12999 "browsefilter",
13000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001#ifdef FEAT_BYTEOFF
13002 "byte_offset",
13003#endif
13004#ifdef FEAT_CINDENT
13005 "cindent",
13006#endif
13007#ifdef FEAT_CLIENTSERVER
13008 "clientserver",
13009#endif
13010#ifdef FEAT_CLIPBOARD
13011 "clipboard",
13012#endif
13013#ifdef FEAT_CMDL_COMPL
13014 "cmdline_compl",
13015#endif
13016#ifdef FEAT_CMDHIST
13017 "cmdline_hist",
13018#endif
13019#ifdef FEAT_COMMENTS
13020 "comments",
13021#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013022#ifdef FEAT_CONCEAL
13023 "conceal",
13024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025#ifdef FEAT_CRYPT
13026 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013027 "crypt-blowfish",
13028 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029#endif
13030#ifdef FEAT_CSCOPE
13031 "cscope",
13032#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013033#ifdef FEAT_CURSORBIND
13034 "cursorbind",
13035#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013036#ifdef CURSOR_SHAPE
13037 "cursorshape",
13038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039#ifdef DEBUG
13040 "debug",
13041#endif
13042#ifdef FEAT_CON_DIALOG
13043 "dialog_con",
13044#endif
13045#ifdef FEAT_GUI_DIALOG
13046 "dialog_gui",
13047#endif
13048#ifdef FEAT_DIFF
13049 "diff",
13050#endif
13051#ifdef FEAT_DIGRAPHS
13052 "digraphs",
13053#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013054#ifdef FEAT_DIRECTX
13055 "directx",
13056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057#ifdef FEAT_DND
13058 "dnd",
13059#endif
13060#ifdef FEAT_EMACS_TAGS
13061 "emacs_tags",
13062#endif
13063 "eval", /* always present, of course! */
13064#ifdef FEAT_EX_EXTRA
13065 "ex_extra",
13066#endif
13067#ifdef FEAT_SEARCH_EXTRA
13068 "extra_search",
13069#endif
13070#ifdef FEAT_FKMAP
13071 "farsi",
13072#endif
13073#ifdef FEAT_SEARCHPATH
13074 "file_in_path",
13075#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013076#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013077 "filterpipe",
13078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079#ifdef FEAT_FIND_ID
13080 "find_in_path",
13081#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013082#ifdef FEAT_FLOAT
13083 "float",
13084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085#ifdef FEAT_FOLDING
13086 "folding",
13087#endif
13088#ifdef FEAT_FOOTER
13089 "footer",
13090#endif
13091#if !defined(USE_SYSTEM) && defined(UNIX)
13092 "fork",
13093#endif
13094#ifdef FEAT_GETTEXT
13095 "gettext",
13096#endif
13097#ifdef FEAT_GUI
13098 "gui",
13099#endif
13100#ifdef FEAT_GUI_ATHENA
13101# ifdef FEAT_GUI_NEXTAW
13102 "gui_neXtaw",
13103# else
13104 "gui_athena",
13105# endif
13106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107#ifdef FEAT_GUI_GTK
13108 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013111#ifdef FEAT_GUI_GNOME
13112 "gui_gnome",
13113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114#ifdef FEAT_GUI_MAC
13115 "gui_mac",
13116#endif
13117#ifdef FEAT_GUI_MOTIF
13118 "gui_motif",
13119#endif
13120#ifdef FEAT_GUI_PHOTON
13121 "gui_photon",
13122#endif
13123#ifdef FEAT_GUI_W16
13124 "gui_win16",
13125#endif
13126#ifdef FEAT_GUI_W32
13127 "gui_win32",
13128#endif
13129#ifdef FEAT_HANGULIN
13130 "hangul_input",
13131#endif
13132#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13133 "iconv",
13134#endif
13135#ifdef FEAT_INS_EXPAND
13136 "insert_expand",
13137#endif
13138#ifdef FEAT_JUMPLIST
13139 "jumplist",
13140#endif
13141#ifdef FEAT_KEYMAP
13142 "keymap",
13143#endif
13144#ifdef FEAT_LANGMAP
13145 "langmap",
13146#endif
13147#ifdef FEAT_LIBCALL
13148 "libcall",
13149#endif
13150#ifdef FEAT_LINEBREAK
13151 "linebreak",
13152#endif
13153#ifdef FEAT_LISP
13154 "lispindent",
13155#endif
13156#ifdef FEAT_LISTCMDS
13157 "listcmds",
13158#endif
13159#ifdef FEAT_LOCALMAP
13160 "localmap",
13161#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013162#ifdef FEAT_LUA
13163# ifndef DYNAMIC_LUA
13164 "lua",
13165# endif
13166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167#ifdef FEAT_MENU
13168 "menu",
13169#endif
13170#ifdef FEAT_SESSION
13171 "mksession",
13172#endif
13173#ifdef FEAT_MODIFY_FNAME
13174 "modify_fname",
13175#endif
13176#ifdef FEAT_MOUSE
13177 "mouse",
13178#endif
13179#ifdef FEAT_MOUSESHAPE
13180 "mouseshape",
13181#endif
13182#if defined(UNIX) || defined(VMS)
13183# ifdef FEAT_MOUSE_DEC
13184 "mouse_dec",
13185# endif
13186# ifdef FEAT_MOUSE_GPM
13187 "mouse_gpm",
13188# endif
13189# ifdef FEAT_MOUSE_JSB
13190 "mouse_jsbterm",
13191# endif
13192# ifdef FEAT_MOUSE_NET
13193 "mouse_netterm",
13194# endif
13195# ifdef FEAT_MOUSE_PTERM
13196 "mouse_pterm",
13197# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013198# ifdef FEAT_MOUSE_SGR
13199 "mouse_sgr",
13200# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013201# ifdef FEAT_SYSMOUSE
13202 "mouse_sysmouse",
13203# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013204# ifdef FEAT_MOUSE_URXVT
13205 "mouse_urxvt",
13206# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207# ifdef FEAT_MOUSE_XTERM
13208 "mouse_xterm",
13209# endif
13210#endif
13211#ifdef FEAT_MBYTE
13212 "multi_byte",
13213#endif
13214#ifdef FEAT_MBYTE_IME
13215 "multi_byte_ime",
13216#endif
13217#ifdef FEAT_MULTI_LANG
13218 "multi_lang",
13219#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013220#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013221#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013222 "mzscheme",
13223#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225#ifdef FEAT_OLE
13226 "ole",
13227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228#ifdef FEAT_PATH_EXTRA
13229 "path_extra",
13230#endif
13231#ifdef FEAT_PERL
13232#ifndef DYNAMIC_PERL
13233 "perl",
13234#endif
13235#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013236#ifdef FEAT_PERSISTENT_UNDO
13237 "persistent_undo",
13238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239#ifdef FEAT_PYTHON
13240#ifndef DYNAMIC_PYTHON
13241 "python",
13242#endif
13243#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013244#ifdef FEAT_PYTHON3
13245#ifndef DYNAMIC_PYTHON3
13246 "python3",
13247#endif
13248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249#ifdef FEAT_POSTSCRIPT
13250 "postscript",
13251#endif
13252#ifdef FEAT_PRINTER
13253 "printer",
13254#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013255#ifdef FEAT_PROFILE
13256 "profile",
13257#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013258#ifdef FEAT_RELTIME
13259 "reltime",
13260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261#ifdef FEAT_QUICKFIX
13262 "quickfix",
13263#endif
13264#ifdef FEAT_RIGHTLEFT
13265 "rightleft",
13266#endif
13267#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13268 "ruby",
13269#endif
13270#ifdef FEAT_SCROLLBIND
13271 "scrollbind",
13272#endif
13273#ifdef FEAT_CMDL_INFO
13274 "showcmd",
13275 "cmdline_info",
13276#endif
13277#ifdef FEAT_SIGNS
13278 "signs",
13279#endif
13280#ifdef FEAT_SMARTINDENT
13281 "smartindent",
13282#endif
13283#ifdef FEAT_SNIFF
13284 "sniff",
13285#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013286#ifdef STARTUPTIME
13287 "startuptime",
13288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289#ifdef FEAT_STL_OPT
13290 "statusline",
13291#endif
13292#ifdef FEAT_SUN_WORKSHOP
13293 "sun_workshop",
13294#endif
13295#ifdef FEAT_NETBEANS_INTG
13296 "netbeans_intg",
13297#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013298#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013299 "spell",
13300#endif
13301#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 "syntax",
13303#endif
13304#if defined(USE_SYSTEM) || !defined(UNIX)
13305 "system",
13306#endif
13307#ifdef FEAT_TAG_BINS
13308 "tag_binary",
13309#endif
13310#ifdef FEAT_TAG_OLDSTATIC
13311 "tag_old_static",
13312#endif
13313#ifdef FEAT_TAG_ANYWHITE
13314 "tag_any_white",
13315#endif
13316#ifdef FEAT_TCL
13317# ifndef DYNAMIC_TCL
13318 "tcl",
13319# endif
13320#endif
13321#ifdef TERMINFO
13322 "terminfo",
13323#endif
13324#ifdef FEAT_TERMRESPONSE
13325 "termresponse",
13326#endif
13327#ifdef FEAT_TEXTOBJ
13328 "textobjects",
13329#endif
13330#ifdef HAVE_TGETENT
13331 "tgetent",
13332#endif
13333#ifdef FEAT_TITLE
13334 "title",
13335#endif
13336#ifdef FEAT_TOOLBAR
13337 "toolbar",
13338#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013339#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13340 "unnamedplus",
13341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342#ifdef FEAT_USR_CMDS
13343 "user-commands", /* was accidentally included in 5.4 */
13344 "user_commands",
13345#endif
13346#ifdef FEAT_VIMINFO
13347 "viminfo",
13348#endif
13349#ifdef FEAT_VERTSPLIT
13350 "vertsplit",
13351#endif
13352#ifdef FEAT_VIRTUALEDIT
13353 "virtualedit",
13354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356#ifdef FEAT_VISUALEXTRA
13357 "visualextra",
13358#endif
13359#ifdef FEAT_VREPLACE
13360 "vreplace",
13361#endif
13362#ifdef FEAT_WILDIGN
13363 "wildignore",
13364#endif
13365#ifdef FEAT_WILDMENU
13366 "wildmenu",
13367#endif
13368#ifdef FEAT_WINDOWS
13369 "windows",
13370#endif
13371#ifdef FEAT_WAK
13372 "winaltkeys",
13373#endif
13374#ifdef FEAT_WRITEBACKUP
13375 "writebackup",
13376#endif
13377#ifdef FEAT_XIM
13378 "xim",
13379#endif
13380#ifdef FEAT_XFONTSET
13381 "xfontset",
13382#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013383#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013384 "xpm",
13385 "xpm_w32", /* for backward compatibility */
13386#else
13387# if defined(HAVE_XPM)
13388 "xpm",
13389# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391#ifdef USE_XSMP
13392 "xsmp",
13393#endif
13394#ifdef USE_XSMP_INTERACT
13395 "xsmp_interact",
13396#endif
13397#ifdef FEAT_XCLIPBOARD
13398 "xterm_clipboard",
13399#endif
13400#ifdef FEAT_XTERM_SAVE
13401 "xterm_save",
13402#endif
13403#if defined(UNIX) && defined(FEAT_X11)
13404 "X11",
13405#endif
13406 NULL
13407 };
13408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013409 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410 for (i = 0; has_list[i] != NULL; ++i)
13411 if (STRICMP(name, has_list[i]) == 0)
13412 {
13413 n = TRUE;
13414 break;
13415 }
13416
13417 if (n == FALSE)
13418 {
13419 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013420 {
13421 if (name[5] == '-'
13422 && STRLEN(name) > 11
13423 && vim_isdigit(name[6])
13424 && vim_isdigit(name[8])
13425 && vim_isdigit(name[10]))
13426 {
13427 int major = atoi((char *)name + 6);
13428 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013429
13430 /* Expect "patch-9.9.01234". */
13431 n = (major < VIM_VERSION_MAJOR
13432 || (major == VIM_VERSION_MAJOR
13433 && (minor < VIM_VERSION_MINOR
13434 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013435 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013436 }
13437 else
13438 n = has_patch(atoi((char *)name + 5));
13439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 else if (STRICMP(name, "vim_starting") == 0)
13441 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013442#ifdef FEAT_MBYTE
13443 else if (STRICMP(name, "multi_byte_encoding") == 0)
13444 n = has_mbyte;
13445#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013446#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13447 else if (STRICMP(name, "balloon_multiline") == 0)
13448 n = multiline_balloon_available();
13449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450#ifdef DYNAMIC_TCL
13451 else if (STRICMP(name, "tcl") == 0)
13452 n = tcl_enabled(FALSE);
13453#endif
13454#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13455 else if (STRICMP(name, "iconv") == 0)
13456 n = iconv_enabled(FALSE);
13457#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013458#ifdef DYNAMIC_LUA
13459 else if (STRICMP(name, "lua") == 0)
13460 n = lua_enabled(FALSE);
13461#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013462#ifdef DYNAMIC_MZSCHEME
13463 else if (STRICMP(name, "mzscheme") == 0)
13464 n = mzscheme_enabled(FALSE);
13465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466#ifdef DYNAMIC_RUBY
13467 else if (STRICMP(name, "ruby") == 0)
13468 n = ruby_enabled(FALSE);
13469#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013470#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471#ifdef DYNAMIC_PYTHON
13472 else if (STRICMP(name, "python") == 0)
13473 n = python_enabled(FALSE);
13474#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013475#endif
13476#ifdef FEAT_PYTHON3
13477#ifdef DYNAMIC_PYTHON3
13478 else if (STRICMP(name, "python3") == 0)
13479 n = python3_enabled(FALSE);
13480#endif
13481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482#ifdef DYNAMIC_PERL
13483 else if (STRICMP(name, "perl") == 0)
13484 n = perl_enabled(FALSE);
13485#endif
13486#ifdef FEAT_GUI
13487 else if (STRICMP(name, "gui_running") == 0)
13488 n = (gui.in_use || gui.starting);
13489# ifdef FEAT_GUI_W32
13490 else if (STRICMP(name, "gui_win32s") == 0)
13491 n = gui_is_win32s();
13492# endif
13493# ifdef FEAT_BROWSE
13494 else if (STRICMP(name, "browse") == 0)
13495 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13496# endif
13497#endif
13498#ifdef FEAT_SYN_HL
13499 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013500 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501#endif
13502#if defined(WIN3264)
13503 else if (STRICMP(name, "win95") == 0)
13504 n = mch_windows95();
13505#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013506#ifdef FEAT_NETBEANS_INTG
13507 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013508 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 }
13511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013512 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513}
13514
13515/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013516 * "has_key()" function
13517 */
13518 static void
13519f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013520 typval_T *argvars;
13521 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013522{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013523 if (argvars[0].v_type != VAR_DICT)
13524 {
13525 EMSG(_(e_dictreq));
13526 return;
13527 }
13528 if (argvars[0].vval.v_dict == NULL)
13529 return;
13530
13531 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013532 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013533}
13534
13535/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013536 * "haslocaldir()" function
13537 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013538 static void
13539f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013540 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013541 typval_T *rettv;
13542{
13543 rettv->vval.v_number = (curwin->w_localdir != NULL);
13544}
13545
13546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 * "hasmapto()" function
13548 */
13549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013550f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013551 typval_T *argvars;
13552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553{
13554 char_u *name;
13555 char_u *mode;
13556 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013557 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013559 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013560 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 mode = (char_u *)"nvo";
13562 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013564 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013565 if (argvars[2].v_type != VAR_UNKNOWN)
13566 abbr = get_tv_number(&argvars[2]);
13567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568
Bram Moolenaar2c932302006-03-18 21:42:09 +000013569 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013570 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013572 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573}
13574
13575/*
13576 * "histadd()" function
13577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013579f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013580 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582{
13583#ifdef FEAT_CMDHIST
13584 int histype;
13585 char_u *str;
13586 char_u buf[NUMBUFLEN];
13587#endif
13588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590 if (check_restricted() || check_secure())
13591 return;
13592#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013593 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13594 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 if (histype >= 0)
13596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 if (*str != NUL)
13599 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013600 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013602 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603 return;
13604 }
13605 }
13606#endif
13607}
13608
13609/*
13610 * "histdel()" function
13611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013614 typval_T *argvars UNUSED;
13615 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616{
13617#ifdef FEAT_CMDHIST
13618 int n;
13619 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013620 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013622 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13623 if (str == NULL)
13624 n = 0;
13625 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013627 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013628 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013630 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 else
13633 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013634 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013635 get_tv_string_buf(&argvars[1], buf));
13636 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637#endif
13638}
13639
13640/*
13641 * "histget()" function
13642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013645 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647{
13648#ifdef FEAT_CMDHIST
13649 int type;
13650 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013651 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013653 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13654 if (str == NULL)
13655 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013657 {
13658 type = get_histtype(str);
13659 if (argvars[1].v_type == VAR_UNKNOWN)
13660 idx = get_history_idx(type);
13661 else
13662 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13663 /* -1 on type error */
13664 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013667 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013669 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670}
13671
13672/*
13673 * "histnr()" function
13674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013676f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013677 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679{
13680 int i;
13681
13682#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013683 char_u *history = get_tv_string_chk(&argvars[0]);
13684
13685 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686 if (i >= HIST_CMD && i < HIST_COUNT)
13687 i = get_history_idx(i);
13688 else
13689#endif
13690 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013691 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692}
13693
13694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 * "highlightID(name)" function
13696 */
13697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013698f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013699 typval_T *argvars;
13700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013702 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703}
13704
13705/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013706 * "highlight_exists()" function
13707 */
13708 static void
13709f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013710 typval_T *argvars;
13711 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013712{
13713 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13714}
13715
13716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717 * "hostname()" function
13718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013720f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013721 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723{
13724 char_u hostname[256];
13725
13726 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013727 rettv->v_type = VAR_STRING;
13728 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729}
13730
13731/*
13732 * iconv() function
13733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013735f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013736 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738{
13739#ifdef FEAT_MBYTE
13740 char_u buf1[NUMBUFLEN];
13741 char_u buf2[NUMBUFLEN];
13742 char_u *from, *to, *str;
13743 vimconv_T vimconv;
13744#endif
13745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013746 rettv->v_type = VAR_STRING;
13747 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748
13749#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013750 str = get_tv_string(&argvars[0]);
13751 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13752 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753 vimconv.vc_type = CONV_NONE;
13754 convert_setup(&vimconv, from, to);
13755
13756 /* If the encodings are equal, no conversion needed. */
13757 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013758 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013760 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761
13762 convert_setup(&vimconv, NULL, NULL);
13763 vim_free(from);
13764 vim_free(to);
13765#endif
13766}
13767
13768/*
13769 * "indent()" function
13770 */
13771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013772f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013773 typval_T *argvars;
13774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775{
13776 linenr_T lnum;
13777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013778 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013780 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013782 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783}
13784
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013785/*
13786 * "index()" function
13787 */
13788 static void
13789f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013790 typval_T *argvars;
13791 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013792{
Bram Moolenaar33570922005-01-25 22:26:29 +000013793 list_T *l;
13794 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013795 long idx = 0;
13796 int ic = FALSE;
13797
13798 rettv->vval.v_number = -1;
13799 if (argvars[0].v_type != VAR_LIST)
13800 {
13801 EMSG(_(e_listreq));
13802 return;
13803 }
13804 l = argvars[0].vval.v_list;
13805 if (l != NULL)
13806 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013807 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013808 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013810 int error = FALSE;
13811
Bram Moolenaar758711c2005-02-02 23:11:38 +000013812 /* Start at specified item. Use the cached index that list_find()
13813 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013814 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013815 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013816 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013817 ic = get_tv_number_chk(&argvars[3], &error);
13818 if (error)
13819 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013820 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013821
Bram Moolenaar758711c2005-02-02 23:11:38 +000013822 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013823 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013824 {
13825 rettv->vval.v_number = idx;
13826 break;
13827 }
13828 }
13829}
13830
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831static int inputsecret_flag = 0;
13832
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013833static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13834
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013836 * This function is used by f_input() and f_inputdialog() functions. The third
13837 * argument to f_input() specifies the type of completion to use at the
13838 * prompt. The third argument to f_inputdialog() specifies the value to return
13839 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 */
13841 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013842get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013843 typval_T *argvars;
13844 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013845 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013847 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 char_u *p = NULL;
13849 int c;
13850 char_u buf[NUMBUFLEN];
13851 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013852 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013853 int xp_type = EXPAND_NOTHING;
13854 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013856 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013857 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858
13859#ifdef NO_CONSOLE_INPUT
13860 /* While starting up, there is no place to enter text. */
13861 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863#endif
13864
13865 cmd_silent = FALSE; /* Want to see the prompt. */
13866 if (prompt != NULL)
13867 {
13868 /* Only the part of the message after the last NL is considered as
13869 * prompt for the command line */
13870 p = vim_strrchr(prompt, '\n');
13871 if (p == NULL)
13872 p = prompt;
13873 else
13874 {
13875 ++p;
13876 c = *p;
13877 *p = NUL;
13878 msg_start();
13879 msg_clr_eos();
13880 msg_puts_attr(prompt, echo_attr);
13881 msg_didout = FALSE;
13882 msg_starthere();
13883 *p = c;
13884 }
13885 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013887 if (argvars[1].v_type != VAR_UNKNOWN)
13888 {
13889 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13890 if (defstr != NULL)
13891 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013893 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013894 {
13895 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013896 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013897 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013898
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013899 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013900 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013901
Bram Moolenaar4463f292005-09-25 22:20:24 +000013902 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13903 if (xp_name == NULL)
13904 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013905
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013906 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013907
Bram Moolenaar4463f292005-09-25 22:20:24 +000013908 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13909 &xp_arg) == FAIL)
13910 return;
13911 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013912 }
13913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013914 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013915 {
13916# ifdef FEAT_EX_EXTRA
13917 int save_ex_normal_busy = ex_normal_busy;
13918 ex_normal_busy = 0;
13919# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013920 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013921 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13922 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013923# ifdef FEAT_EX_EXTRA
13924 ex_normal_busy = save_ex_normal_busy;
13925# endif
13926 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013927 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013928 && argvars[1].v_type != VAR_UNKNOWN
13929 && argvars[2].v_type != VAR_UNKNOWN)
13930 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13931 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013932
13933 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013935 /* since the user typed this, no need to wait for return */
13936 need_wait_return = FALSE;
13937 msg_didout = FALSE;
13938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 cmd_silent = cmd_silent_save;
13940}
13941
13942/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013943 * "input()" function
13944 * Also handles inputsecret() when inputsecret is set.
13945 */
13946 static void
13947f_input(argvars, rettv)
13948 typval_T *argvars;
13949 typval_T *rettv;
13950{
13951 get_user_input(argvars, rettv, FALSE);
13952}
13953
13954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955 * "inputdialog()" function
13956 */
13957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013958f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013959 typval_T *argvars;
13960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961{
13962#if defined(FEAT_GUI_TEXTDIALOG)
13963 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13964 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13965 {
13966 char_u *message;
13967 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013968 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013970 message = get_tv_string_chk(&argvars[0]);
13971 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013972 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013973 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 else
13975 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013976 if (message != NULL && defstr != NULL
13977 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013978 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013979 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980 else
13981 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013982 if (message != NULL && defstr != NULL
13983 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013984 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013985 rettv->vval.v_string = vim_strsave(
13986 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013988 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013990 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991 }
13992 else
13993#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013994 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995}
13996
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013997/*
13998 * "inputlist()" function
13999 */
14000 static void
14001f_inputlist(argvars, rettv)
14002 typval_T *argvars;
14003 typval_T *rettv;
14004{
14005 listitem_T *li;
14006 int selected;
14007 int mouse_used;
14008
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014009#ifdef NO_CONSOLE_INPUT
14010 /* While starting up, there is no place to enter text. */
14011 if (no_console_input())
14012 return;
14013#endif
14014 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14015 {
14016 EMSG2(_(e_listarg), "inputlist()");
14017 return;
14018 }
14019
14020 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014021 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014022 lines_left = Rows; /* avoid more prompt */
14023 msg_scroll = TRUE;
14024 msg_clr_eos();
14025
14026 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14027 {
14028 msg_puts(get_tv_string(&li->li_tv));
14029 msg_putchar('\n');
14030 }
14031
14032 /* Ask for choice. */
14033 selected = prompt_for_number(&mouse_used);
14034 if (mouse_used)
14035 selected -= lines_left;
14036
14037 rettv->vval.v_number = selected;
14038}
14039
14040
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14042
14043/*
14044 * "inputrestore()" function
14045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014048 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050{
14051 if (ga_userinput.ga_len > 0)
14052 {
14053 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14055 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014056 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057 }
14058 else if (p_verbose > 1)
14059 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014060 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014061 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062 }
14063}
14064
14065/*
14066 * "inputsave()" function
14067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014069f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014070 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014073 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074 if (ga_grow(&ga_userinput, 1) == OK)
14075 {
14076 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14077 + ga_userinput.ga_len);
14078 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014079 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 }
14081 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083}
14084
14085/*
14086 * "inputsecret()" function
14087 */
14088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014089f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014090 typval_T *argvars;
14091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092{
14093 ++cmdline_star;
14094 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014095 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096 --cmdline_star;
14097 --inputsecret_flag;
14098}
14099
14100/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014101 * "insert()" function
14102 */
14103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014104f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014105 typval_T *argvars;
14106 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014107{
14108 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014109 listitem_T *item;
14110 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014111 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014112
14113 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014114 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014115 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014116 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014117 {
14118 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014119 before = get_tv_number_chk(&argvars[2], &error);
14120 if (error)
14121 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014122
Bram Moolenaar758711c2005-02-02 23:11:38 +000014123 if (before == l->lv_len)
14124 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014125 else
14126 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014127 item = list_find(l, before);
14128 if (item == NULL)
14129 {
14130 EMSGN(_(e_listidx), before);
14131 l = NULL;
14132 }
14133 }
14134 if (l != NULL)
14135 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014136 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014137 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014138 }
14139 }
14140}
14141
14142/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014143 * "invert(expr)" function
14144 */
14145 static void
14146f_invert(argvars, rettv)
14147 typval_T *argvars;
14148 typval_T *rettv;
14149{
14150 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14151}
14152
14153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154 * "isdirectory()" function
14155 */
14156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014158 typval_T *argvars;
14159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014161 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162}
14163
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014164/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014165 * "islocked()" function
14166 */
14167 static void
14168f_islocked(argvars, rettv)
14169 typval_T *argvars;
14170 typval_T *rettv;
14171{
14172 lval_T lv;
14173 char_u *end;
14174 dictitem_T *di;
14175
14176 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014177 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14178 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014179 if (end != NULL && lv.ll_name != NULL)
14180 {
14181 if (*end != NUL)
14182 EMSG(_(e_trailing));
14183 else
14184 {
14185 if (lv.ll_tv == NULL)
14186 {
14187 if (check_changedtick(lv.ll_name))
14188 rettv->vval.v_number = 1; /* always locked */
14189 else
14190 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014191 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014192 if (di != NULL)
14193 {
14194 /* Consider a variable locked when:
14195 * 1. the variable itself is locked
14196 * 2. the value of the variable is locked.
14197 * 3. the List or Dict value is locked.
14198 */
14199 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14200 || tv_islocked(&di->di_tv));
14201 }
14202 }
14203 }
14204 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014205 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014206 else if (lv.ll_newkey != NULL)
14207 EMSG2(_(e_dictkey), lv.ll_newkey);
14208 else if (lv.ll_list != NULL)
14209 /* List item. */
14210 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14211 else
14212 /* Dictionary item. */
14213 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14214 }
14215 }
14216
14217 clear_lval(&lv);
14218}
14219
Bram Moolenaar33570922005-01-25 22:26:29 +000014220static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014221
14222/*
14223 * Turn a dict into a list:
14224 * "what" == 0: list of keys
14225 * "what" == 1: list of values
14226 * "what" == 2: list of items
14227 */
14228 static void
14229dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014230 typval_T *argvars;
14231 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014232 int what;
14233{
Bram Moolenaar33570922005-01-25 22:26:29 +000014234 list_T *l2;
14235 dictitem_T *di;
14236 hashitem_T *hi;
14237 listitem_T *li;
14238 listitem_T *li2;
14239 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014240 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014241
Bram Moolenaar8c711452005-01-14 21:53:12 +000014242 if (argvars[0].v_type != VAR_DICT)
14243 {
14244 EMSG(_(e_dictreq));
14245 return;
14246 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014247 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014248 return;
14249
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014250 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014251 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014252
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014253 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014254 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014255 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014256 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014257 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014258 --todo;
14259 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014260
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014261 li = listitem_alloc();
14262 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014263 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014264 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014265
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014266 if (what == 0)
14267 {
14268 /* keys() */
14269 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014270 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014271 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14272 }
14273 else if (what == 1)
14274 {
14275 /* values() */
14276 copy_tv(&di->di_tv, &li->li_tv);
14277 }
14278 else
14279 {
14280 /* items() */
14281 l2 = list_alloc();
14282 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014283 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014284 li->li_tv.vval.v_list = l2;
14285 if (l2 == NULL)
14286 break;
14287 ++l2->lv_refcount;
14288
14289 li2 = listitem_alloc();
14290 if (li2 == NULL)
14291 break;
14292 list_append(l2, li2);
14293 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014294 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014295 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14296
14297 li2 = listitem_alloc();
14298 if (li2 == NULL)
14299 break;
14300 list_append(l2, li2);
14301 copy_tv(&di->di_tv, &li2->li_tv);
14302 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014303 }
14304 }
14305}
14306
14307/*
14308 * "items(dict)" function
14309 */
14310 static void
14311f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014312 typval_T *argvars;
14313 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014314{
14315 dict_list(argvars, rettv, 2);
14316}
14317
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014319 * "join()" function
14320 */
14321 static void
14322f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014323 typval_T *argvars;
14324 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014325{
14326 garray_T ga;
14327 char_u *sep;
14328
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014329 if (argvars[0].v_type != VAR_LIST)
14330 {
14331 EMSG(_(e_listreq));
14332 return;
14333 }
14334 if (argvars[0].vval.v_list == NULL)
14335 return;
14336 if (argvars[1].v_type == VAR_UNKNOWN)
14337 sep = (char_u *)" ";
14338 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014339 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014340
14341 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342
14343 if (sep != NULL)
14344 {
14345 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014346 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014347 ga_append(&ga, NUL);
14348 rettv->vval.v_string = (char_u *)ga.ga_data;
14349 }
14350 else
14351 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014352}
14353
14354/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014355 * "keys()" function
14356 */
14357 static void
14358f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014359 typval_T *argvars;
14360 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014361{
14362 dict_list(argvars, rettv, 0);
14363}
14364
14365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 * "last_buffer_nr()" function.
14367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014369f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014370 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372{
14373 int n = 0;
14374 buf_T *buf;
14375
14376 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14377 if (n < buf->b_fnum)
14378 n = buf->b_fnum;
14379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014380 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014381}
14382
14383/*
14384 * "len()" function
14385 */
14386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014388 typval_T *argvars;
14389 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014390{
14391 switch (argvars[0].v_type)
14392 {
14393 case VAR_STRING:
14394 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014395 rettv->vval.v_number = (varnumber_T)STRLEN(
14396 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014397 break;
14398 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014399 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014400 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014401 case VAR_DICT:
14402 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14403 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014404 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014405 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014406 break;
14407 }
14408}
14409
Bram Moolenaar33570922005-01-25 22:26:29 +000014410static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014411
14412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014413libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014414 typval_T *argvars;
14415 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014416 int type;
14417{
14418#ifdef FEAT_LIBCALL
14419 char_u *string_in;
14420 char_u **string_result;
14421 int nr_result;
14422#endif
14423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014424 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014425 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014426 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014427
14428 if (check_restricted() || check_secure())
14429 return;
14430
14431#ifdef FEAT_LIBCALL
14432 /* The first two args must be strings, otherwise its meaningless */
14433 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14434 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014435 string_in = NULL;
14436 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014437 string_in = argvars[2].vval.v_string;
14438 if (type == VAR_NUMBER)
14439 string_result = NULL;
14440 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014441 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014442 if (mch_libcall(argvars[0].vval.v_string,
14443 argvars[1].vval.v_string,
14444 string_in,
14445 argvars[2].vval.v_number,
14446 string_result,
14447 &nr_result) == OK
14448 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014449 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014450 }
14451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452}
14453
14454/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014455 * "libcall()" function
14456 */
14457 static void
14458f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014459 typval_T *argvars;
14460 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014461{
14462 libcall_common(argvars, rettv, VAR_STRING);
14463}
14464
14465/*
14466 * "libcallnr()" function
14467 */
14468 static void
14469f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014470 typval_T *argvars;
14471 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014472{
14473 libcall_common(argvars, rettv, VAR_NUMBER);
14474}
14475
14476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 * "line(string)" function
14478 */
14479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014480f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014481 typval_T *argvars;
14482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483{
14484 linenr_T lnum = 0;
14485 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014486 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014488 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 if (fp != NULL)
14490 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014491 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492}
14493
14494/*
14495 * "line2byte(lnum)" function
14496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014498f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014499 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501{
14502#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014503 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504#else
14505 linenr_T lnum;
14506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014507 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014509 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014511 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14512 if (rettv->vval.v_number >= 0)
14513 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514#endif
14515}
14516
14517/*
14518 * "lispindent(lnum)" function
14519 */
14520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014521f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014522 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524{
14525#ifdef FEAT_LISP
14526 pos_T pos;
14527 linenr_T lnum;
14528
14529 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014530 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14532 {
14533 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014534 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 curwin->w_cursor = pos;
14536 }
14537 else
14538#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014539 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540}
14541
14542/*
14543 * "localtime()" function
14544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014546f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014547 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014550 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551}
14552
Bram Moolenaar33570922005-01-25 22:26:29 +000014553static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554
14555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014556get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014557 typval_T *argvars;
14558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559 int exact;
14560{
14561 char_u *keys;
14562 char_u *which;
14563 char_u buf[NUMBUFLEN];
14564 char_u *keys_buf = NULL;
14565 char_u *rhs;
14566 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014567 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014568 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014569 mapblock_T *mp;
14570 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571
14572 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014573 rettv->v_type = VAR_STRING;
14574 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014576 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577 if (*keys == NUL)
14578 return;
14579
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014580 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014582 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014583 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014584 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014585 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014586 if (argvars[3].v_type != VAR_UNKNOWN)
14587 get_dict = get_tv_number(&argvars[3]);
14588 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590 else
14591 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014592 if (which == NULL)
14593 return;
14594
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595 mode = get_map_mode(&which, 0);
14596
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014597 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014598 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014600
14601 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014603 /* Return a string. */
14604 if (rhs != NULL)
14605 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606
Bram Moolenaarbd743252010-10-20 21:23:33 +020014607 }
14608 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14609 {
14610 /* Return a dictionary. */
14611 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14612 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14613 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614
Bram Moolenaarbd743252010-10-20 21:23:33 +020014615 dict_add_nr_str(dict, "lhs", 0L, lhs);
14616 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14617 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14618 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14619 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14620 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14621 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014622 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014623 dict_add_nr_str(dict, "mode", 0L, mapmode);
14624
14625 vim_free(lhs);
14626 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627 }
14628}
14629
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014630#ifdef FEAT_FLOAT
14631/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014632 * "log()" function
14633 */
14634 static void
14635f_log(argvars, rettv)
14636 typval_T *argvars;
14637 typval_T *rettv;
14638{
14639 float_T f;
14640
14641 rettv->v_type = VAR_FLOAT;
14642 if (get_float_arg(argvars, &f) == OK)
14643 rettv->vval.v_float = log(f);
14644 else
14645 rettv->vval.v_float = 0.0;
14646}
14647
14648/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014649 * "log10()" function
14650 */
14651 static void
14652f_log10(argvars, rettv)
14653 typval_T *argvars;
14654 typval_T *rettv;
14655{
14656 float_T f;
14657
14658 rettv->v_type = VAR_FLOAT;
14659 if (get_float_arg(argvars, &f) == OK)
14660 rettv->vval.v_float = log10(f);
14661 else
14662 rettv->vval.v_float = 0.0;
14663}
14664#endif
14665
Bram Moolenaar1dced572012-04-05 16:54:08 +020014666#ifdef FEAT_LUA
14667/*
14668 * "luaeval()" function
14669 */
14670 static void
14671f_luaeval(argvars, rettv)
14672 typval_T *argvars;
14673 typval_T *rettv;
14674{
14675 char_u *str;
14676 char_u buf[NUMBUFLEN];
14677
14678 str = get_tv_string_buf(&argvars[0], buf);
14679 do_luaeval(str, argvars + 1, rettv);
14680}
14681#endif
14682
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014684 * "map()" function
14685 */
14686 static void
14687f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014688 typval_T *argvars;
14689 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014690{
14691 filter_map(argvars, rettv, TRUE);
14692}
14693
14694/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014695 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014696 */
14697 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014698f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014699 typval_T *argvars;
14700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014702 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703}
14704
14705/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014706 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707 */
14708 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014709f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014710 typval_T *argvars;
14711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014713 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014714}
14715
Bram Moolenaar33570922005-01-25 22:26:29 +000014716static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717
14718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014719find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014720 typval_T *argvars;
14721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722 int type;
14723{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014724 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014725 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014726 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727 char_u *pat;
14728 regmatch_T regmatch;
14729 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014730 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731 char_u *save_cpo;
14732 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014733 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014734 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014735 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014736 list_T *l = NULL;
14737 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014738 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014739 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740
14741 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14742 save_cpo = p_cpo;
14743 p_cpo = (char_u *)"";
14744
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014745 rettv->vval.v_number = -1;
14746 if (type == 3)
14747 {
14748 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014749 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014750 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014751 }
14752 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754 rettv->v_type = VAR_STRING;
14755 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014758 if (argvars[0].v_type == VAR_LIST)
14759 {
14760 if ((l = argvars[0].vval.v_list) == NULL)
14761 goto theend;
14762 li = l->lv_first;
14763 }
14764 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014765 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014766 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014767 len = (long)STRLEN(str);
14768 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014770 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14771 if (pat == NULL)
14772 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014773
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014774 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014776 int error = FALSE;
14777
14778 start = get_tv_number_chk(&argvars[2], &error);
14779 if (error)
14780 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014781 if (l != NULL)
14782 {
14783 li = list_find(l, start);
14784 if (li == NULL)
14785 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014786 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014787 }
14788 else
14789 {
14790 if (start < 0)
14791 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014792 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014793 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014794 /* When "count" argument is there ignore matches before "start",
14795 * otherwise skip part of the string. Differs when pattern is "^"
14796 * or "\<". */
14797 if (argvars[3].v_type != VAR_UNKNOWN)
14798 startcol = start;
14799 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014800 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014801 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014802 len -= start;
14803 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014804 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014805
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014806 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014807 nth = get_tv_number_chk(&argvars[3], &error);
14808 if (error)
14809 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810 }
14811
14812 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14813 if (regmatch.regprog != NULL)
14814 {
14815 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014816
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014817 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014818 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014819 if (l != NULL)
14820 {
14821 if (li == NULL)
14822 {
14823 match = FALSE;
14824 break;
14825 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014826 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014827 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014828 if (str == NULL)
14829 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014830 }
14831
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014832 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014833
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014834 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014835 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014836 if (l == NULL && !match)
14837 break;
14838
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014839 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014840 if (l != NULL)
14841 {
14842 li = li->li_next;
14843 ++idx;
14844 }
14845 else
14846 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014847#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014848 startcol = (colnr_T)(regmatch.startp[0]
14849 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014850#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014851 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014852#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014853 if (startcol > (colnr_T)len
14854 || str + startcol <= regmatch.startp[0])
14855 {
14856 match = FALSE;
14857 break;
14858 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014859 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014860 }
14861
14862 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014864 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014865 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014866 int i;
14867
14868 /* return list with matched string and submatches */
14869 for (i = 0; i < NSUBEXP; ++i)
14870 {
14871 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014872 {
14873 if (list_append_string(rettv->vval.v_list,
14874 (char_u *)"", 0) == FAIL)
14875 break;
14876 }
14877 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014878 regmatch.startp[i],
14879 (int)(regmatch.endp[i] - regmatch.startp[i]))
14880 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014881 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014882 }
14883 }
14884 else if (type == 2)
14885 {
14886 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014887 if (l != NULL)
14888 copy_tv(&li->li_tv, rettv);
14889 else
14890 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014892 }
14893 else if (l != NULL)
14894 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895 else
14896 {
14897 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014898 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899 (varnumber_T)(regmatch.startp[0] - str);
14900 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014901 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014903 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 }
14905 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014906 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907 }
14908
14909theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014910 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911 p_cpo = save_cpo;
14912}
14913
14914/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014915 * "match()" function
14916 */
14917 static void
14918f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014919 typval_T *argvars;
14920 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014921{
14922 find_some_match(argvars, rettv, 1);
14923}
14924
14925/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014926 * "matchadd()" function
14927 */
14928 static void
14929f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014930 typval_T *argvars UNUSED;
14931 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014932{
14933#ifdef FEAT_SEARCH_EXTRA
14934 char_u buf[NUMBUFLEN];
14935 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14936 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14937 int prio = 10; /* default priority */
14938 int id = -1;
14939 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014940 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014941
14942 rettv->vval.v_number = -1;
14943
14944 if (grp == NULL || pat == NULL)
14945 return;
14946 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014947 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014948 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014949 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014950 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014951 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014952 if (argvars[4].v_type != VAR_UNKNOWN)
14953 {
14954 if (argvars[4].v_type != VAR_DICT)
14955 {
14956 EMSG(_(e_dictreq));
14957 return;
14958 }
14959 if (dict_find(argvars[4].vval.v_dict,
14960 (char_u *)"conceal", -1) != NULL)
14961 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14962 (char_u *)"conceal", FALSE);
14963 }
14964 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014965 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014966 if (error == TRUE)
14967 return;
14968 if (id >= 1 && id <= 3)
14969 {
14970 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14971 return;
14972 }
14973
Bram Moolenaar6561d522015-07-21 15:48:27 +020014974 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14975 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014976#endif
14977}
14978
14979/*
14980 * "matchaddpos()" function
14981 */
14982 static void
14983f_matchaddpos(argvars, rettv)
14984 typval_T *argvars UNUSED;
14985 typval_T *rettv UNUSED;
14986{
14987#ifdef FEAT_SEARCH_EXTRA
14988 char_u buf[NUMBUFLEN];
14989 char_u *group;
14990 int prio = 10;
14991 int id = -1;
14992 int error = FALSE;
14993 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014994 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014995
14996 rettv->vval.v_number = -1;
14997
14998 group = get_tv_string_buf_chk(&argvars[0], buf);
14999 if (group == NULL)
15000 return;
15001
15002 if (argvars[1].v_type != VAR_LIST)
15003 {
15004 EMSG2(_(e_listarg), "matchaddpos()");
15005 return;
15006 }
15007 l = argvars[1].vval.v_list;
15008 if (l == NULL)
15009 return;
15010
15011 if (argvars[2].v_type != VAR_UNKNOWN)
15012 {
15013 prio = get_tv_number_chk(&argvars[2], &error);
15014 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015015 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015016 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015017 if (argvars[4].v_type != VAR_UNKNOWN)
15018 {
15019 if (argvars[4].v_type != VAR_DICT)
15020 {
15021 EMSG(_(e_dictreq));
15022 return;
15023 }
15024 if (dict_find(argvars[4].vval.v_dict,
15025 (char_u *)"conceal", -1) != NULL)
15026 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15027 (char_u *)"conceal", FALSE);
15028 }
15029 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015030 }
15031 if (error == TRUE)
15032 return;
15033
15034 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15035 if (id == 1 || id == 2)
15036 {
15037 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15038 return;
15039 }
15040
Bram Moolenaar6561d522015-07-21 15:48:27 +020015041 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15042 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015043#endif
15044}
15045
15046/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015047 * "matcharg()" function
15048 */
15049 static void
15050f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015051 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015052 typval_T *rettv;
15053{
15054 if (rettv_list_alloc(rettv) == OK)
15055 {
15056#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015057 int id = get_tv_number(&argvars[0]);
15058 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015059
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015060 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015061 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015062 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15063 {
15064 list_append_string(rettv->vval.v_list,
15065 syn_id2name(m->hlg_id), -1);
15066 list_append_string(rettv->vval.v_list, m->pattern, -1);
15067 }
15068 else
15069 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015070 list_append_string(rettv->vval.v_list, NULL, -1);
15071 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015072 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015073 }
15074#endif
15075 }
15076}
15077
15078/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015079 * "matchdelete()" function
15080 */
15081 static void
15082f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015083 typval_T *argvars UNUSED;
15084 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015085{
15086#ifdef FEAT_SEARCH_EXTRA
15087 rettv->vval.v_number = match_delete(curwin,
15088 (int)get_tv_number(&argvars[0]), TRUE);
15089#endif
15090}
15091
15092/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093 * "matchend()" function
15094 */
15095 static void
15096f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015097 typval_T *argvars;
15098 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015099{
15100 find_some_match(argvars, rettv, 0);
15101}
15102
15103/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015104 * "matchlist()" function
15105 */
15106 static void
15107f_matchlist(argvars, rettv)
15108 typval_T *argvars;
15109 typval_T *rettv;
15110{
15111 find_some_match(argvars, rettv, 3);
15112}
15113
15114/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015115 * "matchstr()" function
15116 */
15117 static void
15118f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015119 typval_T *argvars;
15120 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015121{
15122 find_some_match(argvars, rettv, 2);
15123}
15124
Bram Moolenaar33570922005-01-25 22:26:29 +000015125static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015126
15127 static void
15128max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015129 typval_T *argvars;
15130 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015131 int domax;
15132{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015133 long n = 0;
15134 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015135 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015136
15137 if (argvars[0].v_type == VAR_LIST)
15138 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015139 list_T *l;
15140 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015141
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015142 l = argvars[0].vval.v_list;
15143 if (l != NULL)
15144 {
15145 li = l->lv_first;
15146 if (li != NULL)
15147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015148 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015149 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015150 {
15151 li = li->li_next;
15152 if (li == NULL)
15153 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015154 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015155 if (domax ? i > n : i < n)
15156 n = i;
15157 }
15158 }
15159 }
15160 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015161 else if (argvars[0].v_type == VAR_DICT)
15162 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015163 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015164 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015165 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015166 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015167
15168 d = argvars[0].vval.v_dict;
15169 if (d != NULL)
15170 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015171 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015172 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015173 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015174 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015175 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015176 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015177 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015178 if (first)
15179 {
15180 n = i;
15181 first = FALSE;
15182 }
15183 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015184 n = i;
15185 }
15186 }
15187 }
15188 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015189 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015190 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015191 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015192}
15193
15194/*
15195 * "max()" function
15196 */
15197 static void
15198f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015199 typval_T *argvars;
15200 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015201{
15202 max_min(argvars, rettv, TRUE);
15203}
15204
15205/*
15206 * "min()" function
15207 */
15208 static void
15209f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015210 typval_T *argvars;
15211 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015212{
15213 max_min(argvars, rettv, FALSE);
15214}
15215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015216static int mkdir_recurse __ARGS((char_u *dir, int prot));
15217
15218/*
15219 * Create the directory in which "dir" is located, and higher levels when
15220 * needed.
15221 */
15222 static int
15223mkdir_recurse(dir, prot)
15224 char_u *dir;
15225 int prot;
15226{
15227 char_u *p;
15228 char_u *updir;
15229 int r = FAIL;
15230
15231 /* Get end of directory name in "dir".
15232 * We're done when it's "/" or "c:/". */
15233 p = gettail_sep(dir);
15234 if (p <= get_past_head(dir))
15235 return OK;
15236
15237 /* If the directory exists we're done. Otherwise: create it.*/
15238 updir = vim_strnsave(dir, (int)(p - dir));
15239 if (updir == NULL)
15240 return FAIL;
15241 if (mch_isdir(updir))
15242 r = OK;
15243 else if (mkdir_recurse(updir, prot) == OK)
15244 r = vim_mkdir_emsg(updir, prot);
15245 vim_free(updir);
15246 return r;
15247}
15248
15249#ifdef vim_mkdir
15250/*
15251 * "mkdir()" function
15252 */
15253 static void
15254f_mkdir(argvars, rettv)
15255 typval_T *argvars;
15256 typval_T *rettv;
15257{
15258 char_u *dir;
15259 char_u buf[NUMBUFLEN];
15260 int prot = 0755;
15261
15262 rettv->vval.v_number = FAIL;
15263 if (check_restricted() || check_secure())
15264 return;
15265
15266 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015267 if (*dir == NUL)
15268 rettv->vval.v_number = FAIL;
15269 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015270 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015271 if (*gettail(dir) == NUL)
15272 /* remove trailing slashes */
15273 *gettail_sep(dir) = NUL;
15274
15275 if (argvars[1].v_type != VAR_UNKNOWN)
15276 {
15277 if (argvars[2].v_type != VAR_UNKNOWN)
15278 prot = get_tv_number_chk(&argvars[2], NULL);
15279 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15280 mkdir_recurse(dir, prot);
15281 }
15282 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015283 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015284}
15285#endif
15286
Bram Moolenaar0d660222005-01-07 21:51:51 +000015287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015288 * "mode()" function
15289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015291f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015292 typval_T *argvars;
15293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015295 char_u buf[3];
15296
15297 buf[1] = NUL;
15298 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015299
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300 if (VIsual_active)
15301 {
15302 if (VIsual_select)
15303 buf[0] = VIsual_mode + 's' - 'v';
15304 else
15305 buf[0] = VIsual_mode;
15306 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015307 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015308 || State == CONFIRM)
15309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015311 if (State == ASKMORE)
15312 buf[1] = 'm';
15313 else if (State == CONFIRM)
15314 buf[1] = '?';
15315 }
15316 else if (State == EXTERNCMD)
15317 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318 else if (State & INSERT)
15319 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015320#ifdef FEAT_VREPLACE
15321 if (State & VREPLACE_FLAG)
15322 {
15323 buf[0] = 'R';
15324 buf[1] = 'v';
15325 }
15326 else
15327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 if (State & REPLACE_FLAG)
15329 buf[0] = 'R';
15330 else
15331 buf[0] = 'i';
15332 }
15333 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015336 if (exmode_active)
15337 buf[1] = 'v';
15338 }
15339 else if (exmode_active)
15340 {
15341 buf[0] = 'c';
15342 buf[1] = 'e';
15343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015345 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015347 if (finish_op)
15348 buf[1] = 'o';
15349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015351 /* Clear out the minor mode when the argument is not a non-zero number or
15352 * non-empty string. */
15353 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015354 buf[1] = NUL;
15355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015356 rettv->vval.v_string = vim_strsave(buf);
15357 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358}
15359
Bram Moolenaar429fa852013-04-15 12:27:36 +020015360#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015361/*
15362 * "mzeval()" function
15363 */
15364 static void
15365f_mzeval(argvars, rettv)
15366 typval_T *argvars;
15367 typval_T *rettv;
15368{
15369 char_u *str;
15370 char_u buf[NUMBUFLEN];
15371
15372 str = get_tv_string_buf(&argvars[0], buf);
15373 do_mzeval(str, rettv);
15374}
Bram Moolenaar75676462013-01-30 14:55:42 +010015375
15376 void
15377mzscheme_call_vim(name, args, rettv)
15378 char_u *name;
15379 typval_T *args;
15380 typval_T *rettv;
15381{
15382 typval_T argvars[3];
15383
15384 argvars[0].v_type = VAR_STRING;
15385 argvars[0].vval.v_string = name;
15386 copy_tv(args, &argvars[1]);
15387 argvars[2].v_type = VAR_UNKNOWN;
15388 f_call(argvars, rettv);
15389 clear_tv(&argvars[1]);
15390}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015391#endif
15392
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015394 * "nextnonblank()" function
15395 */
15396 static void
15397f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015398 typval_T *argvars;
15399 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015400{
15401 linenr_T lnum;
15402
15403 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015405 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015406 {
15407 lnum = 0;
15408 break;
15409 }
15410 if (*skipwhite(ml_get(lnum)) != NUL)
15411 break;
15412 }
15413 rettv->vval.v_number = lnum;
15414}
15415
15416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417 * "nr2char()" function
15418 */
15419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015421 typval_T *argvars;
15422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423{
15424 char_u buf[NUMBUFLEN];
15425
15426#ifdef FEAT_MBYTE
15427 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015428 {
15429 int utf8 = 0;
15430
15431 if (argvars[1].v_type != VAR_UNKNOWN)
15432 utf8 = get_tv_number_chk(&argvars[1], NULL);
15433 if (utf8)
15434 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15435 else
15436 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438 else
15439#endif
15440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015441 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 buf[1] = NUL;
15443 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015444 rettv->v_type = VAR_STRING;
15445 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015446}
15447
15448/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015449 * "or(expr, expr)" function
15450 */
15451 static void
15452f_or(argvars, rettv)
15453 typval_T *argvars;
15454 typval_T *rettv;
15455{
15456 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15457 | get_tv_number_chk(&argvars[1], NULL);
15458}
15459
15460/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015461 * "pathshorten()" function
15462 */
15463 static void
15464f_pathshorten(argvars, rettv)
15465 typval_T *argvars;
15466 typval_T *rettv;
15467{
15468 char_u *p;
15469
15470 rettv->v_type = VAR_STRING;
15471 p = get_tv_string_chk(&argvars[0]);
15472 if (p == NULL)
15473 rettv->vval.v_string = NULL;
15474 else
15475 {
15476 p = vim_strsave(p);
15477 rettv->vval.v_string = p;
15478 if (p != NULL)
15479 shorten_dir(p);
15480 }
15481}
15482
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015483#ifdef FEAT_FLOAT
15484/*
15485 * "pow()" function
15486 */
15487 static void
15488f_pow(argvars, rettv)
15489 typval_T *argvars;
15490 typval_T *rettv;
15491{
15492 float_T fx, fy;
15493
15494 rettv->v_type = VAR_FLOAT;
15495 if (get_float_arg(argvars, &fx) == OK
15496 && get_float_arg(&argvars[1], &fy) == OK)
15497 rettv->vval.v_float = pow(fx, fy);
15498 else
15499 rettv->vval.v_float = 0.0;
15500}
15501#endif
15502
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015503/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015504 * "prevnonblank()" function
15505 */
15506 static void
15507f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015508 typval_T *argvars;
15509 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015510{
15511 linenr_T lnum;
15512
15513 lnum = get_tv_lnum(argvars);
15514 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15515 lnum = 0;
15516 else
15517 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15518 --lnum;
15519 rettv->vval.v_number = lnum;
15520}
15521
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015522#ifdef HAVE_STDARG_H
15523/* This dummy va_list is here because:
15524 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15525 * - locally in the function results in a "used before set" warning
15526 * - using va_start() to initialize it gives "function with fixed args" error */
15527static va_list ap;
15528#endif
15529
Bram Moolenaar8c711452005-01-14 21:53:12 +000015530/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015531 * "printf()" function
15532 */
15533 static void
15534f_printf(argvars, rettv)
15535 typval_T *argvars;
15536 typval_T *rettv;
15537{
15538 rettv->v_type = VAR_STRING;
15539 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015540#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015541 {
15542 char_u buf[NUMBUFLEN];
15543 int len;
15544 char_u *s;
15545 int saved_did_emsg = did_emsg;
15546 char *fmt;
15547
15548 /* Get the required length, allocate the buffer and do it for real. */
15549 did_emsg = FALSE;
15550 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015551 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015552 if (!did_emsg)
15553 {
15554 s = alloc(len + 1);
15555 if (s != NULL)
15556 {
15557 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015558 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015559 }
15560 }
15561 did_emsg |= saved_did_emsg;
15562 }
15563#endif
15564}
15565
15566/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015567 * "pumvisible()" function
15568 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015569 static void
15570f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015571 typval_T *argvars UNUSED;
15572 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015573{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015574#ifdef FEAT_INS_EXPAND
15575 if (pum_visible())
15576 rettv->vval.v_number = 1;
15577#endif
15578}
15579
Bram Moolenaardb913952012-06-29 12:54:53 +020015580#ifdef FEAT_PYTHON3
15581/*
15582 * "py3eval()" function
15583 */
15584 static void
15585f_py3eval(argvars, rettv)
15586 typval_T *argvars;
15587 typval_T *rettv;
15588{
15589 char_u *str;
15590 char_u buf[NUMBUFLEN];
15591
15592 str = get_tv_string_buf(&argvars[0], buf);
15593 do_py3eval(str, rettv);
15594}
15595#endif
15596
15597#ifdef FEAT_PYTHON
15598/*
15599 * "pyeval()" function
15600 */
15601 static void
15602f_pyeval(argvars, rettv)
15603 typval_T *argvars;
15604 typval_T *rettv;
15605{
15606 char_u *str;
15607 char_u buf[NUMBUFLEN];
15608
15609 str = get_tv_string_buf(&argvars[0], buf);
15610 do_pyeval(str, rettv);
15611}
15612#endif
15613
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015614/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015615 * "range()" function
15616 */
15617 static void
15618f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015619 typval_T *argvars;
15620 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015621{
15622 long start;
15623 long end;
15624 long stride = 1;
15625 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015626 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015627
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015628 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015629 if (argvars[1].v_type == VAR_UNKNOWN)
15630 {
15631 end = start - 1;
15632 start = 0;
15633 }
15634 else
15635 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015636 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015637 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015638 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015639 }
15640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015641 if (error)
15642 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015643 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015644 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015645 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015646 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015647 else
15648 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015649 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015650 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015651 if (list_append_number(rettv->vval.v_list,
15652 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015653 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015654 }
15655}
15656
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015657/*
15658 * "readfile()" function
15659 */
15660 static void
15661f_readfile(argvars, rettv)
15662 typval_T *argvars;
15663 typval_T *rettv;
15664{
15665 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015666 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015667 char_u *fname;
15668 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015669 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15670 int io_size = sizeof(buf);
15671 int readlen; /* size of last fread() */
15672 char_u *prev = NULL; /* previously read bytes, if any */
15673 long prevlen = 0; /* length of data in prev */
15674 long prevsize = 0; /* size of prev buffer */
15675 long maxline = MAXLNUM;
15676 long cnt = 0;
15677 char_u *p; /* position in buf */
15678 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015679
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015680 if (argvars[1].v_type != VAR_UNKNOWN)
15681 {
15682 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15683 binary = TRUE;
15684 if (argvars[2].v_type != VAR_UNKNOWN)
15685 maxline = get_tv_number(&argvars[2]);
15686 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015687
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015688 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015689 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015690
15691 /* Always open the file in binary mode, library functions have a mind of
15692 * their own about CR-LF conversion. */
15693 fname = get_tv_string(&argvars[0]);
15694 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15695 {
15696 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15697 return;
15698 }
15699
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015700 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015701 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015702 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015703
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015704 /* This for loop processes what was read, but is also entered at end
15705 * of file so that either:
15706 * - an incomplete line gets written
15707 * - a "binary" file gets an empty line at the end if it ends in a
15708 * newline. */
15709 for (p = buf, start = buf;
15710 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15711 ++p)
15712 {
15713 if (*p == '\n' || readlen <= 0)
15714 {
15715 listitem_T *li;
15716 char_u *s = NULL;
15717 long_u len = p - start;
15718
15719 /* Finished a line. Remove CRs before NL. */
15720 if (readlen > 0 && !binary)
15721 {
15722 while (len > 0 && start[len - 1] == '\r')
15723 --len;
15724 /* removal may cross back to the "prev" string */
15725 if (len == 0)
15726 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15727 --prevlen;
15728 }
15729 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015730 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015731 else
15732 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015733 /* Change "prev" buffer to be the right size. This way
15734 * the bytes are only copied once, and very long lines are
15735 * allocated only once. */
15736 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015737 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015738 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015739 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015740 prev = NULL; /* the list will own the string */
15741 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015742 }
15743 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015744 if (s == NULL)
15745 {
15746 do_outofmem_msg((long_u) prevlen + len + 1);
15747 failed = TRUE;
15748 break;
15749 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015750
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015751 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015752 {
15753 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015754 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015755 break;
15756 }
15757 li->li_tv.v_type = VAR_STRING;
15758 li->li_tv.v_lock = 0;
15759 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015760 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015761
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015762 start = p + 1; /* step over newline */
15763 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015764 break;
15765 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015766 else if (*p == NUL)
15767 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015768#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015769 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15770 * when finding the BF and check the previous two bytes. */
15771 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015772 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015773 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15774 * + 1, these may be in the "prev" string. */
15775 char_u back1 = p >= buf + 1 ? p[-1]
15776 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15777 char_u back2 = p >= buf + 2 ? p[-2]
15778 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15779 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015780
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015781 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015782 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015783 char_u *dest = p - 2;
15784
15785 /* Usually a BOM is at the beginning of a file, and so at
15786 * the beginning of a line; then we can just step over it.
15787 */
15788 if (start == dest)
15789 start = p + 1;
15790 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015791 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015792 /* have to shuffle buf to close gap */
15793 int adjust_prevlen = 0;
15794
15795 if (dest < buf)
15796 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015797 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015798 dest = buf;
15799 }
15800 if (readlen > p - buf + 1)
15801 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15802 readlen -= 3 - adjust_prevlen;
15803 prevlen -= adjust_prevlen;
15804 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015805 }
15806 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015807 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015808#endif
15809 } /* for */
15810
15811 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15812 break;
15813 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015814 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015815 /* There's part of a line in buf, store it in "prev". */
15816 if (p - start + prevlen >= prevsize)
15817 {
15818 /* need bigger "prev" buffer */
15819 char_u *newprev;
15820
15821 /* A common use case is ordinary text files and "prev" gets a
15822 * fragment of a line, so the first allocation is made
15823 * small, to avoid repeatedly 'allocing' large and
15824 * 'reallocing' small. */
15825 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015826 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015827 else
15828 {
15829 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015830 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015831 prevsize = grow50pc > growmin ? grow50pc : growmin;
15832 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015833 newprev = prev == NULL ? alloc(prevsize)
15834 : vim_realloc(prev, prevsize);
15835 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015836 {
15837 do_outofmem_msg((long_u)prevsize);
15838 failed = TRUE;
15839 break;
15840 }
15841 prev = newprev;
15842 }
15843 /* Add the line part to end of "prev". */
15844 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015845 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015846 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015847 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015848
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015849 /*
15850 * For a negative line count use only the lines at the end of the file,
15851 * free the rest.
15852 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015853 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015854 while (cnt > -maxline)
15855 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015856 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015857 --cnt;
15858 }
15859
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015860 if (failed)
15861 {
15862 list_free(rettv->vval.v_list, TRUE);
15863 /* readfile doc says an empty list is returned on error */
15864 rettv->vval.v_list = list_alloc();
15865 }
15866
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015867 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015868 fclose(fd);
15869}
15870
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015871#if defined(FEAT_RELTIME)
15872static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15873
15874/*
15875 * Convert a List to proftime_T.
15876 * Return FAIL when there is something wrong.
15877 */
15878 static int
15879list2proftime(arg, tm)
15880 typval_T *arg;
15881 proftime_T *tm;
15882{
15883 long n1, n2;
15884 int error = FALSE;
15885
15886 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15887 || arg->vval.v_list->lv_len != 2)
15888 return FAIL;
15889 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15890 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15891# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015892 tm->HighPart = n1;
15893 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015894# else
15895 tm->tv_sec = n1;
15896 tm->tv_usec = n2;
15897# endif
15898 return error ? FAIL : OK;
15899}
15900#endif /* FEAT_RELTIME */
15901
15902/*
15903 * "reltime()" function
15904 */
15905 static void
15906f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015907 typval_T *argvars UNUSED;
15908 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015909{
15910#ifdef FEAT_RELTIME
15911 proftime_T res;
15912 proftime_T start;
15913
15914 if (argvars[0].v_type == VAR_UNKNOWN)
15915 {
15916 /* No arguments: get current time. */
15917 profile_start(&res);
15918 }
15919 else if (argvars[1].v_type == VAR_UNKNOWN)
15920 {
15921 if (list2proftime(&argvars[0], &res) == FAIL)
15922 return;
15923 profile_end(&res);
15924 }
15925 else
15926 {
15927 /* Two arguments: compute the difference. */
15928 if (list2proftime(&argvars[0], &start) == FAIL
15929 || list2proftime(&argvars[1], &res) == FAIL)
15930 return;
15931 profile_sub(&res, &start);
15932 }
15933
15934 if (rettv_list_alloc(rettv) == OK)
15935 {
15936 long n1, n2;
15937
15938# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015939 n1 = res.HighPart;
15940 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015941# else
15942 n1 = res.tv_sec;
15943 n2 = res.tv_usec;
15944# endif
15945 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15946 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15947 }
15948#endif
15949}
15950
15951/*
15952 * "reltimestr()" function
15953 */
15954 static void
15955f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015956 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015957 typval_T *rettv;
15958{
15959#ifdef FEAT_RELTIME
15960 proftime_T tm;
15961#endif
15962
15963 rettv->v_type = VAR_STRING;
15964 rettv->vval.v_string = NULL;
15965#ifdef FEAT_RELTIME
15966 if (list2proftime(&argvars[0], &tm) == OK)
15967 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15968#endif
15969}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015970
Bram Moolenaar0d660222005-01-07 21:51:51 +000015971#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15972static void make_connection __ARGS((void));
15973static int check_connection __ARGS((void));
15974
15975 static void
15976make_connection()
15977{
15978 if (X_DISPLAY == NULL
15979# ifdef FEAT_GUI
15980 && !gui.in_use
15981# endif
15982 )
15983 {
15984 x_force_connect = TRUE;
15985 setup_term_clip();
15986 x_force_connect = FALSE;
15987 }
15988}
15989
15990 static int
15991check_connection()
15992{
15993 make_connection();
15994 if (X_DISPLAY == NULL)
15995 {
15996 EMSG(_("E240: No connection to Vim server"));
15997 return FAIL;
15998 }
15999 return OK;
16000}
16001#endif
16002
16003#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016004static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016005
16006 static void
16007remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000016008 typval_T *argvars;
16009 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016010 int expr;
16011{
16012 char_u *server_name;
16013 char_u *keys;
16014 char_u *r = NULL;
16015 char_u buf[NUMBUFLEN];
16016# ifdef WIN32
16017 HWND w;
16018# else
16019 Window w;
16020# endif
16021
16022 if (check_restricted() || check_secure())
16023 return;
16024
16025# ifdef FEAT_X11
16026 if (check_connection() == FAIL)
16027 return;
16028# endif
16029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016030 server_name = get_tv_string_chk(&argvars[0]);
16031 if (server_name == NULL)
16032 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016033 keys = get_tv_string_buf(&argvars[1], buf);
16034# ifdef WIN32
16035 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16036# else
16037 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16038 < 0)
16039# endif
16040 {
16041 if (r != NULL)
16042 EMSG(r); /* sending worked but evaluation failed */
16043 else
16044 EMSG2(_("E241: Unable to send to %s"), server_name);
16045 return;
16046 }
16047
16048 rettv->vval.v_string = r;
16049
16050 if (argvars[2].v_type != VAR_UNKNOWN)
16051 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016052 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016053 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016054 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016055
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016056 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016057 v.di_tv.v_type = VAR_STRING;
16058 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016059 idvar = get_tv_string_chk(&argvars[2]);
16060 if (idvar != NULL)
16061 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016062 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016063 }
16064}
16065#endif
16066
16067/*
16068 * "remote_expr()" function
16069 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016070 static void
16071f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016072 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016073 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016074{
16075 rettv->v_type = VAR_STRING;
16076 rettv->vval.v_string = NULL;
16077#ifdef FEAT_CLIENTSERVER
16078 remote_common(argvars, rettv, TRUE);
16079#endif
16080}
16081
16082/*
16083 * "remote_foreground()" function
16084 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016085 static void
16086f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016087 typval_T *argvars UNUSED;
16088 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016089{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016090#ifdef FEAT_CLIENTSERVER
16091# ifdef WIN32
16092 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016093 {
16094 char_u *server_name = get_tv_string_chk(&argvars[0]);
16095
16096 if (server_name != NULL)
16097 serverForeground(server_name);
16098 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016099# else
16100 /* Send a foreground() expression to the server. */
16101 argvars[1].v_type = VAR_STRING;
16102 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16103 argvars[2].v_type = VAR_UNKNOWN;
16104 remote_common(argvars, rettv, TRUE);
16105 vim_free(argvars[1].vval.v_string);
16106# endif
16107#endif
16108}
16109
Bram Moolenaar0d660222005-01-07 21:51:51 +000016110 static void
16111f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016112 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016113 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016114{
16115#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016116 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016117 char_u *s = NULL;
16118# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016119 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016120# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016121 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016122
16123 if (check_restricted() || check_secure())
16124 {
16125 rettv->vval.v_number = -1;
16126 return;
16127 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016128 serverid = get_tv_string_chk(&argvars[0]);
16129 if (serverid == NULL)
16130 {
16131 rettv->vval.v_number = -1;
16132 return; /* type error; errmsg already given */
16133 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016134# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016135 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016136 if (n == 0)
16137 rettv->vval.v_number = -1;
16138 else
16139 {
16140 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16141 rettv->vval.v_number = (s != NULL);
16142 }
16143# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016144 if (check_connection() == FAIL)
16145 return;
16146
16147 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016148 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016149# endif
16150
16151 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016153 char_u *retvar;
16154
Bram Moolenaar33570922005-01-25 22:26:29 +000016155 v.di_tv.v_type = VAR_STRING;
16156 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016157 retvar = get_tv_string_chk(&argvars[1]);
16158 if (retvar != NULL)
16159 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016160 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016161 }
16162#else
16163 rettv->vval.v_number = -1;
16164#endif
16165}
16166
Bram Moolenaar0d660222005-01-07 21:51:51 +000016167 static void
16168f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016169 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016170 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016171{
16172 char_u *r = NULL;
16173
16174#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016175 char_u *serverid = get_tv_string_chk(&argvars[0]);
16176
16177 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016178 {
16179# ifdef WIN32
16180 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016181 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016182
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016183 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016184 if (n != 0)
16185 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16186 if (r == NULL)
16187# else
16188 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016189 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016190# endif
16191 EMSG(_("E277: Unable to read a server reply"));
16192 }
16193#endif
16194 rettv->v_type = VAR_STRING;
16195 rettv->vval.v_string = r;
16196}
16197
16198/*
16199 * "remote_send()" function
16200 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016201 static void
16202f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016203 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016204 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016205{
16206 rettv->v_type = VAR_STRING;
16207 rettv->vval.v_string = NULL;
16208#ifdef FEAT_CLIENTSERVER
16209 remote_common(argvars, rettv, FALSE);
16210#endif
16211}
16212
16213/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016214 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016215 */
16216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016217f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016218 typval_T *argvars;
16219 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016220{
Bram Moolenaar33570922005-01-25 22:26:29 +000016221 list_T *l;
16222 listitem_T *item, *item2;
16223 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016224 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016225 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016226 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016227 dict_T *d;
16228 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016229 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016230
Bram Moolenaar8c711452005-01-14 21:53:12 +000016231 if (argvars[0].v_type == VAR_DICT)
16232 {
16233 if (argvars[2].v_type != VAR_UNKNOWN)
16234 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016235 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016236 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016237 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016238 key = get_tv_string_chk(&argvars[1]);
16239 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016241 di = dict_find(d, key, -1);
16242 if (di == NULL)
16243 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016244 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16245 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016246 {
16247 *rettv = di->di_tv;
16248 init_tv(&di->di_tv);
16249 dictitem_remove(d, di);
16250 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016251 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016252 }
16253 }
16254 else if (argvars[0].v_type != VAR_LIST)
16255 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016256 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016257 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016259 int error = FALSE;
16260
16261 idx = get_tv_number_chk(&argvars[1], &error);
16262 if (error)
16263 ; /* type error: do nothing, errmsg already given */
16264 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016265 EMSGN(_(e_listidx), idx);
16266 else
16267 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016268 if (argvars[2].v_type == VAR_UNKNOWN)
16269 {
16270 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016271 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016272 *rettv = item->li_tv;
16273 vim_free(item);
16274 }
16275 else
16276 {
16277 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016278 end = get_tv_number_chk(&argvars[2], &error);
16279 if (error)
16280 ; /* type error: do nothing */
16281 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016282 EMSGN(_(e_listidx), end);
16283 else
16284 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016285 int cnt = 0;
16286
16287 for (li = item; li != NULL; li = li->li_next)
16288 {
16289 ++cnt;
16290 if (li == item2)
16291 break;
16292 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016293 if (li == NULL) /* didn't find "item2" after "item" */
16294 EMSG(_(e_invrange));
16295 else
16296 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016297 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016298 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016299 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016300 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016301 l->lv_first = item;
16302 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016303 item->li_prev = NULL;
16304 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016305 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016306 }
16307 }
16308 }
16309 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016310 }
16311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312}
16313
16314/*
16315 * "rename({from}, {to})" function
16316 */
16317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016318f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016319 typval_T *argvars;
16320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321{
16322 char_u buf[NUMBUFLEN];
16323
16324 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016325 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016327 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16328 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329}
16330
16331/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016332 * "repeat()" function
16333 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016334 static void
16335f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016336 typval_T *argvars;
16337 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016338{
16339 char_u *p;
16340 int n;
16341 int slen;
16342 int len;
16343 char_u *r;
16344 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016345
16346 n = get_tv_number(&argvars[1]);
16347 if (argvars[0].v_type == VAR_LIST)
16348 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016349 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016350 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016351 if (list_extend(rettv->vval.v_list,
16352 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016353 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016354 }
16355 else
16356 {
16357 p = get_tv_string(&argvars[0]);
16358 rettv->v_type = VAR_STRING;
16359 rettv->vval.v_string = NULL;
16360
16361 slen = (int)STRLEN(p);
16362 len = slen * n;
16363 if (len <= 0)
16364 return;
16365
16366 r = alloc(len + 1);
16367 if (r != NULL)
16368 {
16369 for (i = 0; i < n; i++)
16370 mch_memmove(r + i * slen, p, (size_t)slen);
16371 r[len] = NUL;
16372 }
16373
16374 rettv->vval.v_string = r;
16375 }
16376}
16377
16378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379 * "resolve()" function
16380 */
16381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016382f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016383 typval_T *argvars;
16384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385{
16386 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016387#ifdef HAVE_READLINK
16388 char_u *buf = NULL;
16389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016391 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392#ifdef FEAT_SHORTCUT
16393 {
16394 char_u *v = NULL;
16395
16396 v = mch_resolve_shortcut(p);
16397 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016398 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016400 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401 }
16402#else
16403# ifdef HAVE_READLINK
16404 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405 char_u *cpy;
16406 int len;
16407 char_u *remain = NULL;
16408 char_u *q;
16409 int is_relative_to_current = FALSE;
16410 int has_trailing_pathsep = FALSE;
16411 int limit = 100;
16412
16413 p = vim_strsave(p);
16414
16415 if (p[0] == '.' && (vim_ispathsep(p[1])
16416 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16417 is_relative_to_current = TRUE;
16418
16419 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016420 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016423 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425
16426 q = getnextcomp(p);
16427 if (*q != NUL)
16428 {
16429 /* Separate the first path component in "p", and keep the
16430 * remainder (beginning with the path separator). */
16431 remain = vim_strsave(q - 1);
16432 q[-1] = NUL;
16433 }
16434
Bram Moolenaard9462e32011-04-11 21:35:11 +020016435 buf = alloc(MAXPATHL + 1);
16436 if (buf == NULL)
16437 goto fail;
16438
Bram Moolenaar071d4272004-06-13 20:20:40 +000016439 for (;;)
16440 {
16441 for (;;)
16442 {
16443 len = readlink((char *)p, (char *)buf, MAXPATHL);
16444 if (len <= 0)
16445 break;
16446 buf[len] = NUL;
16447
16448 if (limit-- == 0)
16449 {
16450 vim_free(p);
16451 vim_free(remain);
16452 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016453 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 goto fail;
16455 }
16456
16457 /* Ensure that the result will have a trailing path separator
16458 * if the argument has one. */
16459 if (remain == NULL && has_trailing_pathsep)
16460 add_pathsep(buf);
16461
16462 /* Separate the first path component in the link value and
16463 * concatenate the remainders. */
16464 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16465 if (*q != NUL)
16466 {
16467 if (remain == NULL)
16468 remain = vim_strsave(q - 1);
16469 else
16470 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016471 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472 if (cpy != NULL)
16473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474 vim_free(remain);
16475 remain = cpy;
16476 }
16477 }
16478 q[-1] = NUL;
16479 }
16480
16481 q = gettail(p);
16482 if (q > p && *q == NUL)
16483 {
16484 /* Ignore trailing path separator. */
16485 q[-1] = NUL;
16486 q = gettail(p);
16487 }
16488 if (q > p && !mch_isFullName(buf))
16489 {
16490 /* symlink is relative to directory of argument */
16491 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16492 if (cpy != NULL)
16493 {
16494 STRCPY(cpy, p);
16495 STRCPY(gettail(cpy), buf);
16496 vim_free(p);
16497 p = cpy;
16498 }
16499 }
16500 else
16501 {
16502 vim_free(p);
16503 p = vim_strsave(buf);
16504 }
16505 }
16506
16507 if (remain == NULL)
16508 break;
16509
16510 /* Append the first path component of "remain" to "p". */
16511 q = getnextcomp(remain + 1);
16512 len = q - remain - (*q != NUL);
16513 cpy = vim_strnsave(p, STRLEN(p) + len);
16514 if (cpy != NULL)
16515 {
16516 STRNCAT(cpy, remain, len);
16517 vim_free(p);
16518 p = cpy;
16519 }
16520 /* Shorten "remain". */
16521 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016522 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523 else
16524 {
16525 vim_free(remain);
16526 remain = NULL;
16527 }
16528 }
16529
16530 /* If the result is a relative path name, make it explicitly relative to
16531 * the current directory if and only if the argument had this form. */
16532 if (!vim_ispathsep(*p))
16533 {
16534 if (is_relative_to_current
16535 && *p != NUL
16536 && !(p[0] == '.'
16537 && (p[1] == NUL
16538 || vim_ispathsep(p[1])
16539 || (p[1] == '.'
16540 && (p[2] == NUL
16541 || vim_ispathsep(p[2]))))))
16542 {
16543 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016544 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545 if (cpy != NULL)
16546 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547 vim_free(p);
16548 p = cpy;
16549 }
16550 }
16551 else if (!is_relative_to_current)
16552 {
16553 /* Strip leading "./". */
16554 q = p;
16555 while (q[0] == '.' && vim_ispathsep(q[1]))
16556 q += 2;
16557 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016558 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 }
16560 }
16561
16562 /* Ensure that the result will have no trailing path separator
16563 * if the argument had none. But keep "/" or "//". */
16564 if (!has_trailing_pathsep)
16565 {
16566 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016567 if (after_pathsep(p, q))
16568 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569 }
16570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016571 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 }
16573# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016574 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575# endif
16576#endif
16577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016578 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016579
16580#ifdef HAVE_READLINK
16581fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016582 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016584 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585}
16586
16587/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016588 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 */
16590 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016591f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016592 typval_T *argvars;
16593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594{
Bram Moolenaar33570922005-01-25 22:26:29 +000016595 list_T *l;
16596 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597
Bram Moolenaar0d660222005-01-07 21:51:51 +000016598 if (argvars[0].v_type != VAR_LIST)
16599 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016600 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016601 && !tv_check_lock(l->lv_lock,
16602 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016603 {
16604 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016605 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016606 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016607 while (li != NULL)
16608 {
16609 ni = li->li_prev;
16610 list_append(l, li);
16611 li = ni;
16612 }
16613 rettv->vval.v_list = l;
16614 rettv->v_type = VAR_LIST;
16615 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016616 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618}
16619
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016620#define SP_NOMOVE 0x01 /* don't move cursor */
16621#define SP_REPEAT 0x02 /* repeat to find outer pair */
16622#define SP_RETCOUNT 0x04 /* return matchcount */
16623#define SP_SETPCMARK 0x08 /* set previous context mark */
16624#define SP_START 0x10 /* accept match at start position */
16625#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16626#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016627#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016628
Bram Moolenaar33570922005-01-25 22:26:29 +000016629static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016630
16631/*
16632 * Get flags for a search function.
16633 * Possibly sets "p_ws".
16634 * Returns BACKWARD, FORWARD or zero (for an error).
16635 */
16636 static int
16637get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016638 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016639 int *flagsp;
16640{
16641 int dir = FORWARD;
16642 char_u *flags;
16643 char_u nbuf[NUMBUFLEN];
16644 int mask;
16645
16646 if (varp->v_type != VAR_UNKNOWN)
16647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016648 flags = get_tv_string_buf_chk(varp, nbuf);
16649 if (flags == NULL)
16650 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016651 while (*flags != NUL)
16652 {
16653 switch (*flags)
16654 {
16655 case 'b': dir = BACKWARD; break;
16656 case 'w': p_ws = TRUE; break;
16657 case 'W': p_ws = FALSE; break;
16658 default: mask = 0;
16659 if (flagsp != NULL)
16660 switch (*flags)
16661 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016662 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016663 case 'e': mask = SP_END; break;
16664 case 'm': mask = SP_RETCOUNT; break;
16665 case 'n': mask = SP_NOMOVE; break;
16666 case 'p': mask = SP_SUBPAT; break;
16667 case 'r': mask = SP_REPEAT; break;
16668 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016669 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016670 }
16671 if (mask == 0)
16672 {
16673 EMSG2(_(e_invarg2), flags);
16674 dir = 0;
16675 }
16676 else
16677 *flagsp |= mask;
16678 }
16679 if (dir == 0)
16680 break;
16681 ++flags;
16682 }
16683 }
16684 return dir;
16685}
16686
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016688 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016690 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016691search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016692 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016693 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016694 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016696 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 char_u *pat;
16698 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016699 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700 int save_p_ws = p_ws;
16701 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016702 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016703 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016704 proftime_T tm;
16705#ifdef FEAT_RELTIME
16706 long time_limit = 0;
16707#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016708 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016709 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016711 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016712 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016713 if (dir == 0)
16714 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016715 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016716 if (flags & SP_START)
16717 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016718 if (flags & SP_END)
16719 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016720 if (flags & SP_COLUMN)
16721 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016722
Bram Moolenaar76929292008-01-06 19:07:36 +000016723 /* Optional arguments: line number to stop searching and timeout. */
16724 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016725 {
16726 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16727 if (lnum_stop < 0)
16728 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016729#ifdef FEAT_RELTIME
16730 if (argvars[3].v_type != VAR_UNKNOWN)
16731 {
16732 time_limit = get_tv_number_chk(&argvars[3], NULL);
16733 if (time_limit < 0)
16734 goto theend;
16735 }
16736#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016737 }
16738
Bram Moolenaar76929292008-01-06 19:07:36 +000016739#ifdef FEAT_RELTIME
16740 /* Set the time limit, if there is one. */
16741 profile_setlimit(time_limit, &tm);
16742#endif
16743
Bram Moolenaar231334e2005-07-25 20:46:57 +000016744 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016745 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016746 * Check to make sure only those flags are set.
16747 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16748 * flags cannot be set. Check for that condition also.
16749 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016750 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016751 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016752 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016753 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016754 goto theend;
16755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016757 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016758 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016759 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016760 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016762 if (flags & SP_SUBPAT)
16763 retval = subpatnum;
16764 else
16765 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016766 if (flags & SP_SETPCMARK)
16767 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016769 if (match_pos != NULL)
16770 {
16771 /* Store the match cursor position */
16772 match_pos->lnum = pos.lnum;
16773 match_pos->col = pos.col + 1;
16774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775 /* "/$" will put the cursor after the end of the line, may need to
16776 * correct that here */
16777 check_cursor();
16778 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016779
16780 /* If 'n' flag is used: restore cursor position. */
16781 if (flags & SP_NOMOVE)
16782 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016783 else
16784 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016785theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016787
16788 return retval;
16789}
16790
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016791#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016792
16793/*
16794 * round() is not in C90, use ceil() or floor() instead.
16795 */
16796 float_T
16797vim_round(f)
16798 float_T f;
16799{
16800 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16801}
16802
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016803/*
16804 * "round({float})" function
16805 */
16806 static void
16807f_round(argvars, rettv)
16808 typval_T *argvars;
16809 typval_T *rettv;
16810{
16811 float_T f;
16812
16813 rettv->v_type = VAR_FLOAT;
16814 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016815 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016816 else
16817 rettv->vval.v_float = 0.0;
16818}
16819#endif
16820
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016821/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016822 * "screenattr()" function
16823 */
16824 static void
16825f_screenattr(argvars, rettv)
16826 typval_T *argvars UNUSED;
16827 typval_T *rettv;
16828{
16829 int row;
16830 int col;
16831 int c;
16832
16833 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16834 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16835 if (row < 0 || row >= screen_Rows
16836 || col < 0 || col >= screen_Columns)
16837 c = -1;
16838 else
16839 c = ScreenAttrs[LineOffset[row] + col];
16840 rettv->vval.v_number = c;
16841}
16842
16843/*
16844 * "screenchar()" function
16845 */
16846 static void
16847f_screenchar(argvars, rettv)
16848 typval_T *argvars UNUSED;
16849 typval_T *rettv;
16850{
16851 int row;
16852 int col;
16853 int off;
16854 int c;
16855
16856 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16857 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16858 if (row < 0 || row >= screen_Rows
16859 || col < 0 || col >= screen_Columns)
16860 c = -1;
16861 else
16862 {
16863 off = LineOffset[row] + col;
16864#ifdef FEAT_MBYTE
16865 if (enc_utf8 && ScreenLinesUC[off] != 0)
16866 c = ScreenLinesUC[off];
16867 else
16868#endif
16869 c = ScreenLines[off];
16870 }
16871 rettv->vval.v_number = c;
16872}
16873
16874/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016875 * "screencol()" function
16876 *
16877 * First column is 1 to be consistent with virtcol().
16878 */
16879 static void
16880f_screencol(argvars, rettv)
16881 typval_T *argvars UNUSED;
16882 typval_T *rettv;
16883{
16884 rettv->vval.v_number = screen_screencol() + 1;
16885}
16886
16887/*
16888 * "screenrow()" function
16889 */
16890 static void
16891f_screenrow(argvars, rettv)
16892 typval_T *argvars UNUSED;
16893 typval_T *rettv;
16894{
16895 rettv->vval.v_number = screen_screenrow() + 1;
16896}
16897
16898/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016899 * "search()" function
16900 */
16901 static void
16902f_search(argvars, rettv)
16903 typval_T *argvars;
16904 typval_T *rettv;
16905{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016906 int flags = 0;
16907
16908 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909}
16910
Bram Moolenaar071d4272004-06-13 20:20:40 +000016911/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016912 * "searchdecl()" function
16913 */
16914 static void
16915f_searchdecl(argvars, rettv)
16916 typval_T *argvars;
16917 typval_T *rettv;
16918{
16919 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016920 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016921 int error = FALSE;
16922 char_u *name;
16923
16924 rettv->vval.v_number = 1; /* default: FAIL */
16925
16926 name = get_tv_string_chk(&argvars[0]);
16927 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016928 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016929 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016930 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16931 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16932 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016933 if (!error && name != NULL)
16934 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016935 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016936}
16937
16938/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016939 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016941 static int
16942searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016943 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016944 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016945{
16946 char_u *spat, *mpat, *epat;
16947 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949 int dir;
16950 int flags = 0;
16951 char_u nbuf1[NUMBUFLEN];
16952 char_u nbuf2[NUMBUFLEN];
16953 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016954 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016955 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016956 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016959 spat = get_tv_string_chk(&argvars[0]);
16960 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16961 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16962 if (spat == NULL || mpat == NULL || epat == NULL)
16963 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964
Bram Moolenaar071d4272004-06-13 20:20:40 +000016965 /* Handle the optional fourth argument: flags */
16966 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016967 if (dir == 0)
16968 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016969
16970 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016971 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16972 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016973 if ((flags & (SP_END | SP_SUBPAT)) != 0
16974 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016975 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016976 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016977 goto theend;
16978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016980 /* Using 'r' implies 'W', otherwise it doesn't work. */
16981 if (flags & SP_REPEAT)
16982 p_ws = FALSE;
16983
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016984 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016985 if (argvars[3].v_type == VAR_UNKNOWN
16986 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987 skip = (char_u *)"";
16988 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016990 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016991 if (argvars[5].v_type != VAR_UNKNOWN)
16992 {
16993 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16994 if (lnum_stop < 0)
16995 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016996#ifdef FEAT_RELTIME
16997 if (argvars[6].v_type != VAR_UNKNOWN)
16998 {
16999 time_limit = get_tv_number_chk(&argvars[6], NULL);
17000 if (time_limit < 0)
17001 goto theend;
17002 }
17003#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017004 }
17005 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017006 if (skip == NULL)
17007 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017009 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017010 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017011
17012theend:
17013 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017014
17015 return retval;
17016}
17017
17018/*
17019 * "searchpair()" function
17020 */
17021 static void
17022f_searchpair(argvars, rettv)
17023 typval_T *argvars;
17024 typval_T *rettv;
17025{
17026 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17027}
17028
17029/*
17030 * "searchpairpos()" function
17031 */
17032 static void
17033f_searchpairpos(argvars, rettv)
17034 typval_T *argvars;
17035 typval_T *rettv;
17036{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017037 pos_T match_pos;
17038 int lnum = 0;
17039 int col = 0;
17040
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017041 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017042 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017043
17044 if (searchpair_cmn(argvars, &match_pos) > 0)
17045 {
17046 lnum = match_pos.lnum;
17047 col = match_pos.col;
17048 }
17049
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017050 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17051 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017052}
17053
17054/*
17055 * Search for a start/middle/end thing.
17056 * Used by searchpair(), see its documentation for the details.
17057 * Returns 0 or -1 for no match,
17058 */
17059 long
Bram Moolenaar76929292008-01-06 19:07:36 +000017060do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
17061 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017062 char_u *spat; /* start pattern */
17063 char_u *mpat; /* middle pattern */
17064 char_u *epat; /* end pattern */
17065 int dir; /* BACKWARD or FORWARD */
17066 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017067 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017068 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017069 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017070 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017071{
17072 char_u *save_cpo;
17073 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17074 long retval = 0;
17075 pos_T pos;
17076 pos_T firstpos;
17077 pos_T foundpos;
17078 pos_T save_cursor;
17079 pos_T save_pos;
17080 int n;
17081 int r;
17082 int nest = 1;
17083 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017084 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017085 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017086
17087 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17088 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017089 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017090
Bram Moolenaar76929292008-01-06 19:07:36 +000017091#ifdef FEAT_RELTIME
17092 /* Set the time limit, if there is one. */
17093 profile_setlimit(time_limit, &tm);
17094#endif
17095
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017096 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17097 * start/middle/end (pat3, for the top pair). */
17098 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17099 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17100 if (pat2 == NULL || pat3 == NULL)
17101 goto theend;
17102 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17103 if (*mpat == NUL)
17104 STRCPY(pat3, pat2);
17105 else
17106 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17107 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017108 if (flags & SP_START)
17109 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017110
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111 save_cursor = curwin->w_cursor;
17112 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017113 clearpos(&firstpos);
17114 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 pat = pat3;
17116 for (;;)
17117 {
17118 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017119 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17121 /* didn't find it or found the first match again: FAIL */
17122 break;
17123
17124 if (firstpos.lnum == 0)
17125 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017126 if (equalpos(pos, foundpos))
17127 {
17128 /* Found the same position again. Can happen with a pattern that
17129 * has "\zs" at the end and searching backwards. Advance one
17130 * character and try again. */
17131 if (dir == BACKWARD)
17132 decl(&pos);
17133 else
17134 incl(&pos);
17135 }
17136 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017138 /* clear the start flag to avoid getting stuck here */
17139 options &= ~SEARCH_START;
17140
Bram Moolenaar071d4272004-06-13 20:20:40 +000017141 /* If the skip pattern matches, ignore this match. */
17142 if (*skip != NUL)
17143 {
17144 save_pos = curwin->w_cursor;
17145 curwin->w_cursor = pos;
17146 r = eval_to_bool(skip, &err, NULL, FALSE);
17147 curwin->w_cursor = save_pos;
17148 if (err)
17149 {
17150 /* Evaluating {skip} caused an error, break here. */
17151 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017152 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 break;
17154 }
17155 if (r)
17156 continue;
17157 }
17158
17159 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17160 {
17161 /* Found end when searching backwards or start when searching
17162 * forward: nested pair. */
17163 ++nest;
17164 pat = pat2; /* nested, don't search for middle */
17165 }
17166 else
17167 {
17168 /* Found end when searching forward or start when searching
17169 * backward: end of (nested) pair; or found middle in outer pair. */
17170 if (--nest == 1)
17171 pat = pat3; /* outer level, search for middle */
17172 }
17173
17174 if (nest == 0)
17175 {
17176 /* Found the match: return matchcount or line number. */
17177 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017178 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017180 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017181 if (flags & SP_SETPCMARK)
17182 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183 curwin->w_cursor = pos;
17184 if (!(flags & SP_REPEAT))
17185 break;
17186 nest = 1; /* search for next unmatched */
17187 }
17188 }
17189
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017190 if (match_pos != NULL)
17191 {
17192 /* Store the match cursor position */
17193 match_pos->lnum = curwin->w_cursor.lnum;
17194 match_pos->col = curwin->w_cursor.col + 1;
17195 }
17196
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017198 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199 curwin->w_cursor = save_cursor;
17200
17201theend:
17202 vim_free(pat2);
17203 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017204 if (p_cpo == empty_option)
17205 p_cpo = save_cpo;
17206 else
17207 /* Darn, evaluating the {skip} expression changed the value. */
17208 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017209
17210 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211}
17212
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017213/*
17214 * "searchpos()" function
17215 */
17216 static void
17217f_searchpos(argvars, rettv)
17218 typval_T *argvars;
17219 typval_T *rettv;
17220{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017221 pos_T match_pos;
17222 int lnum = 0;
17223 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017224 int n;
17225 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017226
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017227 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017228 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017229
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017230 n = search_cmn(argvars, &match_pos, &flags);
17231 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017232 {
17233 lnum = match_pos.lnum;
17234 col = match_pos.col;
17235 }
17236
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017237 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17238 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017239 if (flags & SP_SUBPAT)
17240 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017241}
17242
17243
Bram Moolenaar0d660222005-01-07 21:51:51 +000017244 static void
17245f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017246 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017249#ifdef FEAT_CLIENTSERVER
17250 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017251 char_u *server = get_tv_string_chk(&argvars[0]);
17252 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253
Bram Moolenaar0d660222005-01-07 21:51:51 +000017254 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017255 if (server == NULL || reply == NULL)
17256 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017257 if (check_restricted() || check_secure())
17258 return;
17259# ifdef FEAT_X11
17260 if (check_connection() == FAIL)
17261 return;
17262# endif
17263
17264 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017266 EMSG(_("E258: Unable to send to client"));
17267 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017269 rettv->vval.v_number = 0;
17270#else
17271 rettv->vval.v_number = -1;
17272#endif
17273}
17274
Bram Moolenaar0d660222005-01-07 21:51:51 +000017275 static void
17276f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017277 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017278 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017279{
17280 char_u *r = NULL;
17281
17282#ifdef FEAT_CLIENTSERVER
17283# ifdef WIN32
17284 r = serverGetVimNames();
17285# else
17286 make_connection();
17287 if (X_DISPLAY != NULL)
17288 r = serverGetVimNames(X_DISPLAY);
17289# endif
17290#endif
17291 rettv->v_type = VAR_STRING;
17292 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293}
17294
17295/*
17296 * "setbufvar()" function
17297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017299f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017300 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017301 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302{
17303 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017306 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 char_u nbuf[NUMBUFLEN];
17308
17309 if (check_restricted() || check_secure())
17310 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017311 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17312 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017313 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 varp = &argvars[2];
17315
17316 if (buf != NULL && varname != NULL && varp != NULL)
17317 {
17318 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320
17321 if (*varname == '&')
17322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017323 long numval;
17324 char_u *strval;
17325 int error = FALSE;
17326
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017328 numval = get_tv_number_chk(varp, &error);
17329 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017330 if (!error && strval != NULL)
17331 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 }
17333 else
17334 {
17335 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17336 if (bufvarname != NULL)
17337 {
17338 STRCPY(bufvarname, "b:");
17339 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017340 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341 vim_free(bufvarname);
17342 }
17343 }
17344
17345 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348}
17349
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017350 static void
17351f_setcharsearch(argvars, rettv)
17352 typval_T *argvars;
17353 typval_T *rettv UNUSED;
17354{
17355 dict_T *d;
17356 dictitem_T *di;
17357 char_u *csearch;
17358
17359 if (argvars[0].v_type != VAR_DICT)
17360 {
17361 EMSG(_(e_dictreq));
17362 return;
17363 }
17364
17365 if ((d = argvars[0].vval.v_dict) != NULL)
17366 {
17367 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17368 if (csearch != NULL)
17369 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017370#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017371 if (enc_utf8)
17372 {
17373 int pcc[MAX_MCO];
17374 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017375
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017376 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17377 }
17378 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017379#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017380 set_last_csearch(PTR2CHAR(csearch),
17381 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017382 }
17383
17384 di = dict_find(d, (char_u *)"forward", -1);
17385 if (di != NULL)
17386 set_csearch_direction(get_tv_number(&di->di_tv)
17387 ? FORWARD : BACKWARD);
17388
17389 di = dict_find(d, (char_u *)"until", -1);
17390 if (di != NULL)
17391 set_csearch_until(!!get_tv_number(&di->di_tv));
17392 }
17393}
17394
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395/*
17396 * "setcmdpos()" function
17397 */
17398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017399f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017400 typval_T *argvars;
17401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017403 int pos = (int)get_tv_number(&argvars[0]) - 1;
17404
17405 if (pos >= 0)
17406 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407}
17408
17409/*
17410 * "setline()" function
17411 */
17412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017413f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017414 typval_T *argvars;
17415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416{
17417 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017418 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017419 list_T *l = NULL;
17420 listitem_T *li = NULL;
17421 long added = 0;
17422 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017424 lnum = get_tv_lnum(&argvars[0]);
17425 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017427 l = argvars[1].vval.v_list;
17428 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017430 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017431 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017432
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017433 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017434 for (;;)
17435 {
17436 if (l != NULL)
17437 {
17438 /* list argument, get next string */
17439 if (li == NULL)
17440 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017441 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017442 li = li->li_next;
17443 }
17444
17445 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017446 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017447 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017448
17449 /* When coming here from Insert mode, sync undo, so that this can be
17450 * undone separately from what was previously inserted. */
17451 if (u_sync_once == 2)
17452 {
17453 u_sync_once = 1; /* notify that u_sync() was called */
17454 u_sync(TRUE);
17455 }
17456
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017457 if (lnum <= curbuf->b_ml.ml_line_count)
17458 {
17459 /* existing line, replace it */
17460 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17461 {
17462 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017463 if (lnum == curwin->w_cursor.lnum)
17464 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017465 rettv->vval.v_number = 0; /* OK */
17466 }
17467 }
17468 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17469 {
17470 /* lnum is one past the last line, append the line */
17471 ++added;
17472 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17473 rettv->vval.v_number = 0; /* OK */
17474 }
17475
17476 if (l == NULL) /* only one string argument */
17477 break;
17478 ++lnum;
17479 }
17480
17481 if (added > 0)
17482 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483}
17484
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017485static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17486
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017488 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017489 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017490 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017491set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017492 win_T *wp UNUSED;
17493 typval_T *list_arg UNUSED;
17494 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017495 typval_T *rettv;
17496{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017497#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017498 char_u *act;
17499 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017500#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017501
Bram Moolenaar2641f772005-03-25 21:58:17 +000017502 rettv->vval.v_number = -1;
17503
17504#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017505 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017506 EMSG(_(e_listreq));
17507 else
17508 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017509 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017510
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017511 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017512 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017513 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017514 if (act == NULL)
17515 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017516 if (*act == 'a' || *act == 'r')
17517 action = *act;
17518 }
17519
Bram Moolenaar81484f42012-12-05 15:16:47 +010017520 if (l != NULL && set_errorlist(wp, l, action,
17521 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017522 rettv->vval.v_number = 0;
17523 }
17524#endif
17525}
17526
17527/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017528 * "setloclist()" function
17529 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017530 static void
17531f_setloclist(argvars, rettv)
17532 typval_T *argvars;
17533 typval_T *rettv;
17534{
17535 win_T *win;
17536
17537 rettv->vval.v_number = -1;
17538
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017539 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017540 if (win != NULL)
17541 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17542}
17543
17544/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017545 * "setmatches()" function
17546 */
17547 static void
17548f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017549 typval_T *argvars UNUSED;
17550 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017551{
17552#ifdef FEAT_SEARCH_EXTRA
17553 list_T *l;
17554 listitem_T *li;
17555 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017556 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017557
17558 rettv->vval.v_number = -1;
17559 if (argvars[0].v_type != VAR_LIST)
17560 {
17561 EMSG(_(e_listreq));
17562 return;
17563 }
17564 if ((l = argvars[0].vval.v_list) != NULL)
17565 {
17566
17567 /* To some extent make sure that we are dealing with a list from
17568 * "getmatches()". */
17569 li = l->lv_first;
17570 while (li != NULL)
17571 {
17572 if (li->li_tv.v_type != VAR_DICT
17573 || (d = li->li_tv.vval.v_dict) == NULL)
17574 {
17575 EMSG(_(e_invarg));
17576 return;
17577 }
17578 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017579 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17580 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017581 && dict_find(d, (char_u *)"priority", -1) != NULL
17582 && dict_find(d, (char_u *)"id", -1) != NULL))
17583 {
17584 EMSG(_(e_invarg));
17585 return;
17586 }
17587 li = li->li_next;
17588 }
17589
17590 clear_matches(curwin);
17591 li = l->lv_first;
17592 while (li != NULL)
17593 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017594 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017595 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017596 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017597 char_u *group;
17598 int priority;
17599 int id;
17600 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017601
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017602 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017603 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17604 {
17605 if (s == NULL)
17606 {
17607 s = list_alloc();
17608 if (s == NULL)
17609 return;
17610 }
17611
17612 /* match from matchaddpos() */
17613 for (i = 1; i < 9; i++)
17614 {
17615 sprintf((char *)buf, (char *)"pos%d", i);
17616 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17617 {
17618 if (di->di_tv.v_type != VAR_LIST)
17619 return;
17620
17621 list_append_tv(s, &di->di_tv);
17622 s->lv_refcount++;
17623 }
17624 else
17625 break;
17626 }
17627 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017628
17629 group = get_dict_string(d, (char_u *)"group", FALSE);
17630 priority = (int)get_dict_number(d, (char_u *)"priority");
17631 id = (int)get_dict_number(d, (char_u *)"id");
17632 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17633 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17634 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017635 if (i == 0)
17636 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017637 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017638 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017639 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017640 }
17641 else
17642 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017643 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017644 list_unref(s);
17645 s = NULL;
17646 }
17647
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017648 li = li->li_next;
17649 }
17650 rettv->vval.v_number = 0;
17651 }
17652#endif
17653}
17654
17655/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017656 * "setpos()" function
17657 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017658 static void
17659f_setpos(argvars, rettv)
17660 typval_T *argvars;
17661 typval_T *rettv;
17662{
17663 pos_T pos;
17664 int fnum;
17665 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017666 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017667
Bram Moolenaar08250432008-02-13 11:42:46 +000017668 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017669 name = get_tv_string_chk(argvars);
17670 if (name != NULL)
17671 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017672 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017673 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017674 if (--pos.col < 0)
17675 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017676 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017677 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017678 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017679 if (fnum == curbuf->b_fnum)
17680 {
17681 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017682 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017683 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017684 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017685 curwin->w_set_curswant = FALSE;
17686 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017687 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017688 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017689 }
17690 else
17691 EMSG(_(e_invarg));
17692 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017693 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17694 {
17695 /* set mark */
17696 if (setmark_pos(name[1], &pos, fnum) == OK)
17697 rettv->vval.v_number = 0;
17698 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017699 else
17700 EMSG(_(e_invarg));
17701 }
17702 }
17703}
17704
17705/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017706 * "setqflist()" function
17707 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017708 static void
17709f_setqflist(argvars, rettv)
17710 typval_T *argvars;
17711 typval_T *rettv;
17712{
17713 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17714}
17715
17716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 * "setreg()" function
17718 */
17719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017720f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017721 typval_T *argvars;
17722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723{
17724 int regname;
17725 char_u *strregname;
17726 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017727 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 int append;
17729 char_u yank_type;
17730 long block_len;
17731
17732 block_len = -1;
17733 yank_type = MAUTO;
17734 append = FALSE;
17735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017736 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017737 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017739 if (strregname == NULL)
17740 return; /* type error; errmsg already given */
17741 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742 if (regname == 0 || regname == '@')
17743 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017745 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017747 stropt = get_tv_string_chk(&argvars[2]);
17748 if (stropt == NULL)
17749 return; /* type error */
17750 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 switch (*stropt)
17752 {
17753 case 'a': case 'A': /* append */
17754 append = TRUE;
17755 break;
17756 case 'v': case 'c': /* character-wise selection */
17757 yank_type = MCHAR;
17758 break;
17759 case 'V': case 'l': /* line-wise selection */
17760 yank_type = MLINE;
17761 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 case 'b': case Ctrl_V: /* block-wise selection */
17763 yank_type = MBLOCK;
17764 if (VIM_ISDIGIT(stropt[1]))
17765 {
17766 ++stropt;
17767 block_len = getdigits(&stropt) - 1;
17768 --stropt;
17769 }
17770 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 }
17772 }
17773
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017774 if (argvars[1].v_type == VAR_LIST)
17775 {
17776 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017777 char_u **allocval;
17778 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017779 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017780 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017781 int len = argvars[1].vval.v_list->lv_len;
17782 listitem_T *li;
17783
Bram Moolenaar7d647822014-04-05 21:28:56 +020017784 /* First half: use for pointers to result lines; second half: use for
17785 * pointers to allocated copies. */
17786 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017787 if (lstval == NULL)
17788 return;
17789 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017790 allocval = lstval + len + 2;
17791 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017792
17793 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17794 li = li->li_next)
17795 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017796 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017797 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017798 goto free_lstval;
17799 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017800 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017801 /* Need to make a copy, next get_tv_string_buf_chk() will
17802 * overwrite the string. */
17803 strval = vim_strsave(buf);
17804 if (strval == NULL)
17805 goto free_lstval;
17806 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017807 }
17808 *curval++ = strval;
17809 }
17810 *curval++ = NULL;
17811
17812 write_reg_contents_lst(regname, lstval, -1,
17813 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017814free_lstval:
17815 while (curallocval > allocval)
17816 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017817 vim_free(lstval);
17818 }
17819 else
17820 {
17821 strval = get_tv_string_chk(&argvars[1]);
17822 if (strval == NULL)
17823 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017824 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017826 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017827 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828}
17829
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017830/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017831 * "settabvar()" function
17832 */
17833 static void
17834f_settabvar(argvars, rettv)
17835 typval_T *argvars;
17836 typval_T *rettv;
17837{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017838#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017839 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017840 tabpage_T *tp;
17841#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017842 char_u *varname, *tabvarname;
17843 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017844
17845 rettv->vval.v_number = 0;
17846
17847 if (check_restricted() || check_secure())
17848 return;
17849
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017850#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017851 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017852#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017853 varname = get_tv_string_chk(&argvars[1]);
17854 varp = &argvars[2];
17855
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017856 if (varname != NULL && varp != NULL
17857#ifdef FEAT_WINDOWS
17858 && tp != NULL
17859#endif
17860 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017861 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017862#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017863 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017864 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017865#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017866
17867 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17868 if (tabvarname != NULL)
17869 {
17870 STRCPY(tabvarname, "t:");
17871 STRCPY(tabvarname + 2, varname);
17872 set_var(tabvarname, varp, TRUE);
17873 vim_free(tabvarname);
17874 }
17875
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017876#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017877 /* Restore current tabpage */
17878 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017879 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017880#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017881 }
17882}
17883
17884/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017885 * "settabwinvar()" function
17886 */
17887 static void
17888f_settabwinvar(argvars, rettv)
17889 typval_T *argvars;
17890 typval_T *rettv;
17891{
17892 setwinvar(argvars, rettv, 1);
17893}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894
17895/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017896 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017899f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017900 typval_T *argvars;
17901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017903 setwinvar(argvars, rettv, 0);
17904}
17905
17906/*
17907 * "setwinvar()" and "settabwinvar()" functions
17908 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017909
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017910 static void
17911setwinvar(argvars, rettv, off)
17912 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017913 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017914 int off;
17915{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 win_T *win;
17917#ifdef FEAT_WINDOWS
17918 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017919 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017920 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921#endif
17922 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017923 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017925 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926
17927 if (check_restricted() || check_secure())
17928 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017929
17930#ifdef FEAT_WINDOWS
17931 if (off == 1)
17932 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17933 else
17934 tp = curtab;
17935#endif
17936 win = find_win_by_nr(&argvars[off], tp);
17937 varname = get_tv_string_chk(&argvars[off + 1]);
17938 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939
17940 if (win != NULL && varname != NULL && varp != NULL)
17941 {
17942#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017943 need_switch_win = !(tp == curtab && win == curwin);
17944 if (!need_switch_win
17945 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017948 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017950 long numval;
17951 char_u *strval;
17952 int error = FALSE;
17953
17954 ++varname;
17955 numval = get_tv_number_chk(varp, &error);
17956 strval = get_tv_string_buf_chk(varp, nbuf);
17957 if (!error && strval != NULL)
17958 set_option_value(varname, numval, strval, OPT_LOCAL);
17959 }
17960 else
17961 {
17962 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17963 if (winvarname != NULL)
17964 {
17965 STRCPY(winvarname, "w:");
17966 STRCPY(winvarname + 2, varname);
17967 set_var(winvarname, varp, TRUE);
17968 vim_free(winvarname);
17969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 }
17971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017973 if (need_switch_win)
17974 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975#endif
17976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977}
17978
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017979#ifdef FEAT_CRYPT
17980/*
17981 * "sha256({string})" function
17982 */
17983 static void
17984f_sha256(argvars, rettv)
17985 typval_T *argvars;
17986 typval_T *rettv;
17987{
17988 char_u *p;
17989
17990 p = get_tv_string(&argvars[0]);
17991 rettv->vval.v_string = vim_strsave(
17992 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17993 rettv->v_type = VAR_STRING;
17994}
17995#endif /* FEAT_CRYPT */
17996
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017998 * "shellescape({string})" function
17999 */
18000 static void
18001f_shellescape(argvars, rettv)
18002 typval_T *argvars;
18003 typval_T *rettv;
18004{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018005 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018006 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018007 rettv->v_type = VAR_STRING;
18008}
18009
18010/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018011 * shiftwidth() function
18012 */
18013 static void
18014f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020018015 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018016 typval_T *rettv;
18017{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018018 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018019}
18020
18021/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018022 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 */
18024 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000018025f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018026 typval_T *argvars;
18027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018029 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030
Bram Moolenaar0d660222005-01-07 21:51:51 +000018031 p = get_tv_string(&argvars[0]);
18032 rettv->vval.v_string = vim_strsave(p);
18033 simplify_filename(rettv->vval.v_string); /* simplify in place */
18034 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035}
18036
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018037#ifdef FEAT_FLOAT
18038/*
18039 * "sin()" function
18040 */
18041 static void
18042f_sin(argvars, rettv)
18043 typval_T *argvars;
18044 typval_T *rettv;
18045{
18046 float_T f;
18047
18048 rettv->v_type = VAR_FLOAT;
18049 if (get_float_arg(argvars, &f) == OK)
18050 rettv->vval.v_float = sin(f);
18051 else
18052 rettv->vval.v_float = 0.0;
18053}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018054
18055/*
18056 * "sinh()" function
18057 */
18058 static void
18059f_sinh(argvars, rettv)
18060 typval_T *argvars;
18061 typval_T *rettv;
18062{
18063 float_T f;
18064
18065 rettv->v_type = VAR_FLOAT;
18066 if (get_float_arg(argvars, &f) == OK)
18067 rettv->vval.v_float = sinh(f);
18068 else
18069 rettv->vval.v_float = 0.0;
18070}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018071#endif
18072
Bram Moolenaar0d660222005-01-07 21:51:51 +000018073static int
18074#ifdef __BORLANDC__
18075 _RTLENTRYF
18076#endif
18077 item_compare __ARGS((const void *s1, const void *s2));
18078static int
18079#ifdef __BORLANDC__
18080 _RTLENTRYF
18081#endif
18082 item_compare2 __ARGS((const void *s1, const void *s2));
18083
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018084/* struct used in the array that's given to qsort() */
18085typedef struct
18086{
18087 listitem_T *item;
18088 int idx;
18089} sortItem_T;
18090
Bram Moolenaar0d660222005-01-07 21:51:51 +000018091static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018092static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018093static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018094static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018095static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018096static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018097static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018098static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018099#define ITEM_COMPARE_FAIL 999
18100
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018102 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018104 static int
18105#ifdef __BORLANDC__
18106_RTLENTRYF
18107#endif
18108item_compare(s1, s2)
18109 const void *s1;
18110 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018112 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018113 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018114 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018115 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018116 int res;
18117 char_u numbuf1[NUMBUFLEN];
18118 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018120 si1 = (sortItem_T *)s1;
18121 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018122 tv1 = &si1->item->li_tv;
18123 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018124
18125 if (item_compare_numbers)
18126 {
18127 long v1 = get_tv_number(tv1);
18128 long v2 = get_tv_number(tv2);
18129
18130 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18131 }
18132
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018133 /* tv2string() puts quotes around a string and allocates memory. Don't do
18134 * that for string variables. Use a single quote when comparing with a
18135 * non-string to do what the docs promise. */
18136 if (tv1->v_type == VAR_STRING)
18137 {
18138 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18139 p1 = (char_u *)"'";
18140 else
18141 p1 = tv1->vval.v_string;
18142 }
18143 else
18144 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18145 if (tv2->v_type == VAR_STRING)
18146 {
18147 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18148 p2 = (char_u *)"'";
18149 else
18150 p2 = tv2->vval.v_string;
18151 }
18152 else
18153 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018154 if (p1 == NULL)
18155 p1 = (char_u *)"";
18156 if (p2 == NULL)
18157 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018158 if (!item_compare_numeric)
18159 {
18160 if (item_compare_ic)
18161 res = STRICMP(p1, p2);
18162 else
18163 res = STRCMP(p1, p2);
18164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018166 {
18167 double n1, n2;
18168 n1 = strtod((char *)p1, (char **)&p1);
18169 n2 = strtod((char *)p2, (char **)&p2);
18170 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18171 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018172
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018173 /* When the result would be zero, compare the item indexes. Makes the
18174 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018175 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018176 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018177
Bram Moolenaar0d660222005-01-07 21:51:51 +000018178 vim_free(tofree1);
18179 vim_free(tofree2);
18180 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181}
18182
18183 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018184#ifdef __BORLANDC__
18185_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018187item_compare2(s1, s2)
18188 const void *s1;
18189 const void *s2;
18190{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018191 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018192 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018193 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018194 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018195 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018197 /* shortcut after failure in previous call; compare all items equal */
18198 if (item_compare_func_err)
18199 return 0;
18200
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018201 si1 = (sortItem_T *)s1;
18202 si2 = (sortItem_T *)s2;
18203
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018204 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018205 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018206 copy_tv(&si1->item->li_tv, &argv[0]);
18207 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018208
18209 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018210 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018211 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18212 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018213 clear_tv(&argv[0]);
18214 clear_tv(&argv[1]);
18215
18216 if (res == FAIL)
18217 res = ITEM_COMPARE_FAIL;
18218 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018219 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18220 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018221 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018222 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018223
18224 /* When the result would be zero, compare the pointers themselves. Makes
18225 * the sort stable. */
18226 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018227 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018228
Bram Moolenaar0d660222005-01-07 21:51:51 +000018229 return res;
18230}
18231
18232/*
18233 * "sort({list})" function
18234 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018236do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018237 typval_T *argvars;
18238 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018239 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240{
Bram Moolenaar33570922005-01-25 22:26:29 +000018241 list_T *l;
18242 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018243 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018244 long len;
18245 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246
Bram Moolenaar0d660222005-01-07 21:51:51 +000018247 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018248 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 else
18250 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018251 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018252 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018253 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18254 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018255 return;
18256 rettv->vval.v_list = l;
18257 rettv->v_type = VAR_LIST;
18258 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259
Bram Moolenaar0d660222005-01-07 21:51:51 +000018260 len = list_len(l);
18261 if (len <= 1)
18262 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018265 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018266 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018267 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018268 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018269 if (argvars[1].v_type != VAR_UNKNOWN)
18270 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018271 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018272 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018273 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018274 else
18275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018276 int error = FALSE;
18277
18278 i = get_tv_number_chk(&argvars[1], &error);
18279 if (error)
18280 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018281 if (i == 1)
18282 item_compare_ic = TRUE;
18283 else
18284 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018285 if (item_compare_func != NULL)
18286 {
18287 if (STRCMP(item_compare_func, "n") == 0)
18288 {
18289 item_compare_func = NULL;
18290 item_compare_numeric = TRUE;
18291 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018292 else if (STRCMP(item_compare_func, "N") == 0)
18293 {
18294 item_compare_func = NULL;
18295 item_compare_numbers = TRUE;
18296 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018297 else if (STRCMP(item_compare_func, "i") == 0)
18298 {
18299 item_compare_func = NULL;
18300 item_compare_ic = TRUE;
18301 }
18302 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018303 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018304
18305 if (argvars[2].v_type != VAR_UNKNOWN)
18306 {
18307 /* optional third argument: {dict} */
18308 if (argvars[2].v_type != VAR_DICT)
18309 {
18310 EMSG(_(e_dictreq));
18311 return;
18312 }
18313 item_compare_selfdict = argvars[2].vval.v_dict;
18314 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316
Bram Moolenaar0d660222005-01-07 21:51:51 +000018317 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018318 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018319 if (ptrs == NULL)
18320 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321
Bram Moolenaar327aa022014-03-25 18:24:23 +010018322 i = 0;
18323 if (sort)
18324 {
18325 /* sort(): ptrs will be the list to sort */
18326 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018327 {
18328 ptrs[i].item = li;
18329 ptrs[i].idx = i;
18330 ++i;
18331 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018332
18333 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018334 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018335 /* test the compare function */
18336 if (item_compare_func != NULL
18337 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018338 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018339 EMSG(_("E702: Sort compare function failed"));
18340 else
18341 {
18342 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018343 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018344 item_compare_func == NULL ? item_compare : item_compare2);
18345
18346 if (!item_compare_func_err)
18347 {
18348 /* Clear the List and append the items in sorted order. */
18349 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18350 l->lv_len = 0;
18351 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018352 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018353 }
18354 }
18355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018357 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018358 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18359
18360 /* f_uniq(): ptrs will be a stack of items to remove */
18361 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018362 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018363 item_compare_func_ptr = item_compare_func
18364 ? item_compare2 : item_compare;
18365
18366 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18367 li = li->li_next)
18368 {
18369 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18370 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018371 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018372 if (item_compare_func_err)
18373 {
18374 EMSG(_("E882: Uniq compare function failed"));
18375 break;
18376 }
18377 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018378
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018379 if (!item_compare_func_err)
18380 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018381 while (--i >= 0)
18382 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018383 li = ptrs[i].item->li_next;
18384 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018385 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018386 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018387 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018388 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018389 list_fix_watch(l, li);
18390 listitem_free(li);
18391 l->lv_len--;
18392 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018393 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018394 }
18395
18396 vim_free(ptrs);
18397 }
18398}
18399
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018400/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018401 * "sort({list})" function
18402 */
18403 static void
18404f_sort(argvars, rettv)
18405 typval_T *argvars;
18406 typval_T *rettv;
18407{
18408 do_sort_uniq(argvars, rettv, TRUE);
18409}
18410
18411/*
18412 * "uniq({list})" function
18413 */
18414 static void
18415f_uniq(argvars, rettv)
18416 typval_T *argvars;
18417 typval_T *rettv;
18418{
18419 do_sort_uniq(argvars, rettv, FALSE);
18420}
18421
18422/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018423 * "soundfold({word})" function
18424 */
18425 static void
18426f_soundfold(argvars, rettv)
18427 typval_T *argvars;
18428 typval_T *rettv;
18429{
18430 char_u *s;
18431
18432 rettv->v_type = VAR_STRING;
18433 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018434#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018435 rettv->vval.v_string = eval_soundfold(s);
18436#else
18437 rettv->vval.v_string = vim_strsave(s);
18438#endif
18439}
18440
18441/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018442 * "spellbadword()" function
18443 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018444 static void
18445f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018446 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018447 typval_T *rettv;
18448{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018449 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018450 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018451 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018452
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018453 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018454 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018455
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018456#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018457 if (argvars[0].v_type == VAR_UNKNOWN)
18458 {
18459 /* Find the start and length of the badly spelled word. */
18460 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18461 if (len != 0)
18462 word = ml_get_cursor();
18463 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018464 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018465 {
18466 char_u *str = get_tv_string_chk(&argvars[0]);
18467 int capcol = -1;
18468
18469 if (str != NULL)
18470 {
18471 /* Check the argument for spelling. */
18472 while (*str != NUL)
18473 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018474 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018475 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018476 {
18477 word = str;
18478 break;
18479 }
18480 str += len;
18481 }
18482 }
18483 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018484#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018485
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018486 list_append_string(rettv->vval.v_list, word, len);
18487 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018488 attr == HLF_SPB ? "bad" :
18489 attr == HLF_SPR ? "rare" :
18490 attr == HLF_SPL ? "local" :
18491 attr == HLF_SPC ? "caps" :
18492 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018493}
18494
18495/*
18496 * "spellsuggest()" function
18497 */
18498 static void
18499f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018500 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018501 typval_T *rettv;
18502{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018503#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018504 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018505 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018506 int maxcount;
18507 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018508 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018509 listitem_T *li;
18510 int need_capital = FALSE;
18511#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018512
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018513 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018514 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018515
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018516#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018517 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018518 {
18519 str = get_tv_string(&argvars[0]);
18520 if (argvars[1].v_type != VAR_UNKNOWN)
18521 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018522 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018523 if (maxcount <= 0)
18524 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018525 if (argvars[2].v_type != VAR_UNKNOWN)
18526 {
18527 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18528 if (typeerr)
18529 return;
18530 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018531 }
18532 else
18533 maxcount = 25;
18534
Bram Moolenaar4770d092006-01-12 23:22:24 +000018535 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018536
18537 for (i = 0; i < ga.ga_len; ++i)
18538 {
18539 str = ((char_u **)ga.ga_data)[i];
18540
18541 li = listitem_alloc();
18542 if (li == NULL)
18543 vim_free(str);
18544 else
18545 {
18546 li->li_tv.v_type = VAR_STRING;
18547 li->li_tv.v_lock = 0;
18548 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018549 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018550 }
18551 }
18552 ga_clear(&ga);
18553 }
18554#endif
18555}
18556
Bram Moolenaar0d660222005-01-07 21:51:51 +000018557 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018558f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018559 typval_T *argvars;
18560 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018561{
18562 char_u *str;
18563 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018564 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018565 regmatch_T regmatch;
18566 char_u patbuf[NUMBUFLEN];
18567 char_u *save_cpo;
18568 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018569 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018570 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018571 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018572
18573 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18574 save_cpo = p_cpo;
18575 p_cpo = (char_u *)"";
18576
18577 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018578 if (argvars[1].v_type != VAR_UNKNOWN)
18579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018580 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18581 if (pat == NULL)
18582 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018583 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018584 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018585 }
18586 if (pat == NULL || *pat == NUL)
18587 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018588
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018589 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018591 if (typeerr)
18592 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593
Bram Moolenaar0d660222005-01-07 21:51:51 +000018594 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18595 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018597 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018598 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018599 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018600 if (*str == NUL)
18601 match = FALSE; /* empty item at the end */
18602 else
18603 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018604 if (match)
18605 end = regmatch.startp[0];
18606 else
18607 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018608 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18609 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018610 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018611 if (list_append_string(rettv->vval.v_list, str,
18612 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018613 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018614 }
18615 if (!match)
18616 break;
18617 /* Advance to just after the match. */
18618 if (regmatch.endp[0] > str)
18619 col = 0;
18620 else
18621 {
18622 /* Don't get stuck at the same match. */
18623#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018624 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018625#else
18626 col = 1;
18627#endif
18628 }
18629 str = regmatch.endp[0];
18630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631
Bram Moolenaar473de612013-06-08 18:19:48 +020018632 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634
Bram Moolenaar0d660222005-01-07 21:51:51 +000018635 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636}
18637
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018638#ifdef FEAT_FLOAT
18639/*
18640 * "sqrt()" function
18641 */
18642 static void
18643f_sqrt(argvars, rettv)
18644 typval_T *argvars;
18645 typval_T *rettv;
18646{
18647 float_T f;
18648
18649 rettv->v_type = VAR_FLOAT;
18650 if (get_float_arg(argvars, &f) == OK)
18651 rettv->vval.v_float = sqrt(f);
18652 else
18653 rettv->vval.v_float = 0.0;
18654}
18655
18656/*
18657 * "str2float()" function
18658 */
18659 static void
18660f_str2float(argvars, rettv)
18661 typval_T *argvars;
18662 typval_T *rettv;
18663{
18664 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18665
18666 if (*p == '+')
18667 p = skipwhite(p + 1);
18668 (void)string2float(p, &rettv->vval.v_float);
18669 rettv->v_type = VAR_FLOAT;
18670}
18671#endif
18672
Bram Moolenaar2c932302006-03-18 21:42:09 +000018673/*
18674 * "str2nr()" function
18675 */
18676 static void
18677f_str2nr(argvars, rettv)
18678 typval_T *argvars;
18679 typval_T *rettv;
18680{
18681 int base = 10;
18682 char_u *p;
18683 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018684 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018685
18686 if (argvars[1].v_type != VAR_UNKNOWN)
18687 {
18688 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018689 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018690 {
18691 EMSG(_(e_invarg));
18692 return;
18693 }
18694 }
18695
18696 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018697 if (*p == '+')
18698 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018699 switch (base)
18700 {
18701 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18702 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18703 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18704 default: what = 0;
18705 }
18706 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018707 rettv->vval.v_number = n;
18708}
18709
Bram Moolenaar071d4272004-06-13 20:20:40 +000018710#ifdef HAVE_STRFTIME
18711/*
18712 * "strftime({format}[, {time}])" function
18713 */
18714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018715f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018716 typval_T *argvars;
18717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718{
18719 char_u result_buf[256];
18720 struct tm *curtime;
18721 time_t seconds;
18722 char_u *p;
18723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018724 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018726 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018727 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 seconds = time(NULL);
18729 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018730 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 curtime = localtime(&seconds);
18732 /* MSVC returns NULL for an invalid value of seconds. */
18733 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018734 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735 else
18736 {
18737# ifdef FEAT_MBYTE
18738 vimconv_T conv;
18739 char_u *enc;
18740
18741 conv.vc_type = CONV_NONE;
18742 enc = enc_locale();
18743 convert_setup(&conv, p_enc, enc);
18744 if (conv.vc_type != CONV_NONE)
18745 p = string_convert(&conv, p, NULL);
18746# endif
18747 if (p != NULL)
18748 (void)strftime((char *)result_buf, sizeof(result_buf),
18749 (char *)p, curtime);
18750 else
18751 result_buf[0] = NUL;
18752
18753# ifdef FEAT_MBYTE
18754 if (conv.vc_type != CONV_NONE)
18755 vim_free(p);
18756 convert_setup(&conv, enc, p_enc);
18757 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018758 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 else
18760# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018761 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762
18763# ifdef FEAT_MBYTE
18764 /* Release conversion descriptors */
18765 convert_setup(&conv, NULL, NULL);
18766 vim_free(enc);
18767# endif
18768 }
18769}
18770#endif
18771
18772/*
18773 * "stridx()" function
18774 */
18775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018776f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018777 typval_T *argvars;
18778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779{
18780 char_u buf[NUMBUFLEN];
18781 char_u *needle;
18782 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018783 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018785 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018787 needle = get_tv_string_chk(&argvars[1]);
18788 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018789 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018790 if (needle == NULL || haystack == NULL)
18791 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792
Bram Moolenaar33570922005-01-25 22:26:29 +000018793 if (argvars[2].v_type != VAR_UNKNOWN)
18794 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018795 int error = FALSE;
18796
18797 start_idx = get_tv_number_chk(&argvars[2], &error);
18798 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018799 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018800 if (start_idx >= 0)
18801 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018802 }
18803
18804 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18805 if (pos != NULL)
18806 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807}
18808
18809/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018810 * "string()" function
18811 */
18812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018813f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018814 typval_T *argvars;
18815 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018816{
18817 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018818 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018820 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018821 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018822 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018823 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018824 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825}
18826
18827/*
18828 * "strlen()" function
18829 */
18830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018831f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018832 typval_T *argvars;
18833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018835 rettv->vval.v_number = (varnumber_T)(STRLEN(
18836 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837}
18838
18839/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018840 * "strchars()" function
18841 */
18842 static void
18843f_strchars(argvars, rettv)
18844 typval_T *argvars;
18845 typval_T *rettv;
18846{
18847 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018848 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018849#ifdef FEAT_MBYTE
18850 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018851 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018852#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018853
18854 if (argvars[1].v_type != VAR_UNKNOWN)
18855 skipcc = get_tv_number_chk(&argvars[1], NULL);
18856 if (skipcc < 0 || skipcc > 1)
18857 EMSG(_(e_invarg));
18858 else
18859 {
18860#ifdef FEAT_MBYTE
18861 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18862 while (*s != NUL)
18863 {
18864 func_mb_ptr2char_adv(&s);
18865 ++len;
18866 }
18867 rettv->vval.v_number = len;
18868#else
18869 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18870#endif
18871 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018872}
18873
18874/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018875 * "strdisplaywidth()" function
18876 */
18877 static void
18878f_strdisplaywidth(argvars, rettv)
18879 typval_T *argvars;
18880 typval_T *rettv;
18881{
18882 char_u *s = get_tv_string(&argvars[0]);
18883 int col = 0;
18884
18885 if (argvars[1].v_type != VAR_UNKNOWN)
18886 col = get_tv_number(&argvars[1]);
18887
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018888 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018889}
18890
18891/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018892 * "strwidth()" function
18893 */
18894 static void
18895f_strwidth(argvars, rettv)
18896 typval_T *argvars;
18897 typval_T *rettv;
18898{
18899 char_u *s = get_tv_string(&argvars[0]);
18900
18901 rettv->vval.v_number = (varnumber_T)(
18902#ifdef FEAT_MBYTE
18903 mb_string2cells(s, -1)
18904#else
18905 STRLEN(s)
18906#endif
18907 );
18908}
18909
18910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 * "strpart()" function
18912 */
18913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018914f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018915 typval_T *argvars;
18916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917{
18918 char_u *p;
18919 int n;
18920 int len;
18921 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018922 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018924 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018925 slen = (int)STRLEN(p);
18926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018927 n = get_tv_number_chk(&argvars[1], &error);
18928 if (error)
18929 len = 0;
18930 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018931 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932 else
18933 len = slen - n; /* default len: all bytes that are available. */
18934
18935 /*
18936 * Only return the overlap between the specified part and the actual
18937 * string.
18938 */
18939 if (n < 0)
18940 {
18941 len += n;
18942 n = 0;
18943 }
18944 else if (n > slen)
18945 n = slen;
18946 if (len < 0)
18947 len = 0;
18948 else if (n + len > slen)
18949 len = slen - n;
18950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018951 rettv->v_type = VAR_STRING;
18952 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018953}
18954
18955/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018956 * "strridx()" function
18957 */
18958 static void
18959f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018960 typval_T *argvars;
18961 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018962{
18963 char_u buf[NUMBUFLEN];
18964 char_u *needle;
18965 char_u *haystack;
18966 char_u *rest;
18967 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018968 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018970 needle = get_tv_string_chk(&argvars[1]);
18971 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018972
18973 rettv->vval.v_number = -1;
18974 if (needle == NULL || haystack == NULL)
18975 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018976
18977 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018978 if (argvars[2].v_type != VAR_UNKNOWN)
18979 {
18980 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018981 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018982 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018983 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018984 }
18985 else
18986 end_idx = haystack_len;
18987
Bram Moolenaar0d660222005-01-07 21:51:51 +000018988 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018989 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018990 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018991 lastmatch = haystack + end_idx;
18992 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018993 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018994 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018995 for (rest = haystack; *rest != '\0'; ++rest)
18996 {
18997 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018998 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018999 break;
19000 lastmatch = rest;
19001 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019002 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019003
19004 if (lastmatch == NULL)
19005 rettv->vval.v_number = -1;
19006 else
19007 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19008}
19009
19010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 * "strtrans()" function
19012 */
19013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019014f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019015 typval_T *argvars;
19016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019018 rettv->v_type = VAR_STRING;
19019 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020}
19021
19022/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019023 * "submatch()" function
19024 */
19025 static void
19026f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019027 typval_T *argvars;
19028 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019029{
Bram Moolenaar41571762014-04-02 19:00:58 +020019030 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019031 int no;
19032 int retList = 0;
19033
19034 no = (int)get_tv_number_chk(&argvars[0], &error);
19035 if (error)
19036 return;
19037 error = FALSE;
19038 if (argvars[1].v_type != VAR_UNKNOWN)
19039 retList = get_tv_number_chk(&argvars[1], &error);
19040 if (error)
19041 return;
19042
19043 if (retList == 0)
19044 {
19045 rettv->v_type = VAR_STRING;
19046 rettv->vval.v_string = reg_submatch(no);
19047 }
19048 else
19049 {
19050 rettv->v_type = VAR_LIST;
19051 rettv->vval.v_list = reg_submatch_list(no);
19052 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019053}
19054
19055/*
19056 * "substitute()" function
19057 */
19058 static void
19059f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019060 typval_T *argvars;
19061 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019062{
19063 char_u patbuf[NUMBUFLEN];
19064 char_u subbuf[NUMBUFLEN];
19065 char_u flagsbuf[NUMBUFLEN];
19066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019067 char_u *str = get_tv_string_chk(&argvars[0]);
19068 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19069 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19070 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19071
Bram Moolenaar0d660222005-01-07 21:51:51 +000019072 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019073 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19074 rettv->vval.v_string = NULL;
19075 else
19076 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019077}
19078
19079/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019080 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019083f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019084 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086{
19087 int id = 0;
19088#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019089 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 long col;
19091 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019092 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019094 lnum = get_tv_lnum(argvars); /* -1 on type error */
19095 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19096 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019098 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019099 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019100 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101#endif
19102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019103 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104}
19105
19106/*
19107 * "synIDattr(id, what [, mode])" function
19108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019110f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019111 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113{
19114 char_u *p = NULL;
19115#ifdef FEAT_SYN_HL
19116 int id;
19117 char_u *what;
19118 char_u *mode;
19119 char_u modebuf[NUMBUFLEN];
19120 int modec;
19121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019122 id = get_tv_number(&argvars[0]);
19123 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019124 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019126 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019128 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 modec = 0; /* replace invalid with current */
19130 }
19131 else
19132 {
19133#ifdef FEAT_GUI
19134 if (gui.in_use)
19135 modec = 'g';
19136 else
19137#endif
19138 if (t_colors > 1)
19139 modec = 'c';
19140 else
19141 modec = 't';
19142 }
19143
19144
19145 switch (TOLOWER_ASC(what[0]))
19146 {
19147 case 'b':
19148 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19149 p = highlight_color(id, what, modec);
19150 else /* bold */
19151 p = highlight_has_attr(id, HL_BOLD, modec);
19152 break;
19153
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019154 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155 p = highlight_color(id, what, modec);
19156 break;
19157
19158 case 'i':
19159 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19160 p = highlight_has_attr(id, HL_INVERSE, modec);
19161 else /* italic */
19162 p = highlight_has_attr(id, HL_ITALIC, modec);
19163 break;
19164
19165 case 'n': /* name */
19166 p = get_highlight_name(NULL, id - 1);
19167 break;
19168
19169 case 'r': /* reverse */
19170 p = highlight_has_attr(id, HL_INVERSE, modec);
19171 break;
19172
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019173 case 's':
19174 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19175 p = highlight_color(id, what, modec);
19176 else /* standout */
19177 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178 break;
19179
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019180 case 'u':
19181 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19182 /* underline */
19183 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19184 else
19185 /* undercurl */
19186 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187 break;
19188 }
19189
19190 if (p != NULL)
19191 p = vim_strsave(p);
19192#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019193 rettv->v_type = VAR_STRING;
19194 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195}
19196
19197/*
19198 * "synIDtrans(id)" function
19199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019201f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019202 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204{
19205 int id;
19206
19207#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019208 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209
19210 if (id > 0)
19211 id = syn_get_final_id(id);
19212 else
19213#endif
19214 id = 0;
19215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019216 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217}
19218
19219/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019220 * "synconcealed(lnum, col)" function
19221 */
19222 static void
19223f_synconcealed(argvars, rettv)
19224 typval_T *argvars UNUSED;
19225 typval_T *rettv;
19226{
19227#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19228 long lnum;
19229 long col;
19230 int syntax_flags = 0;
19231 int cchar;
19232 int matchid = 0;
19233 char_u str[NUMBUFLEN];
19234#endif
19235
19236 rettv->v_type = VAR_LIST;
19237 rettv->vval.v_list = NULL;
19238
19239#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19240 lnum = get_tv_lnum(argvars); /* -1 on type error */
19241 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19242
19243 vim_memset(str, NUL, sizeof(str));
19244
19245 if (rettv_list_alloc(rettv) != FAIL)
19246 {
19247 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19248 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19249 && curwin->w_p_cole > 0)
19250 {
19251 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19252 syntax_flags = get_syntax_info(&matchid);
19253
19254 /* get the conceal character */
19255 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19256 {
19257 cchar = syn_get_sub_char();
19258 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19259 cchar = lcs_conceal;
19260 if (cchar != NUL)
19261 {
19262# ifdef FEAT_MBYTE
19263 if (has_mbyte)
19264 (*mb_char2bytes)(cchar, str);
19265 else
19266# endif
19267 str[0] = cchar;
19268 }
19269 }
19270 }
19271
19272 list_append_number(rettv->vval.v_list,
19273 (syntax_flags & HL_CONCEAL) != 0);
19274 /* -1 to auto-determine strlen */
19275 list_append_string(rettv->vval.v_list, str, -1);
19276 list_append_number(rettv->vval.v_list, matchid);
19277 }
19278#endif
19279}
19280
19281/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019282 * "synstack(lnum, col)" function
19283 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019284 static void
19285f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019286 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019287 typval_T *rettv;
19288{
19289#ifdef FEAT_SYN_HL
19290 long lnum;
19291 long col;
19292 int i;
19293 int id;
19294#endif
19295
19296 rettv->v_type = VAR_LIST;
19297 rettv->vval.v_list = NULL;
19298
19299#ifdef FEAT_SYN_HL
19300 lnum = get_tv_lnum(argvars); /* -1 on type error */
19301 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19302
19303 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019304 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019305 && rettv_list_alloc(rettv) != FAIL)
19306 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019307 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019308 for (i = 0; ; ++i)
19309 {
19310 id = syn_get_stack_item(i);
19311 if (id < 0)
19312 break;
19313 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19314 break;
19315 }
19316 }
19317#endif
19318}
19319
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019321get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019322 typval_T *argvars;
19323 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019324 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019326 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019328 char_u *infile = NULL;
19329 char_u buf[NUMBUFLEN];
19330 int err = FALSE;
19331 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019332 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019333 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019334
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019335 rettv->v_type = VAR_STRING;
19336 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019337 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019338 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019339
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019340 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019341 {
19342 /*
19343 * Write the string to a temp file, to be used for input of the shell
19344 * command.
19345 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019346 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019347 {
19348 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019349 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019350 }
19351
19352 fd = mch_fopen((char *)infile, WRITEBIN);
19353 if (fd == NULL)
19354 {
19355 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019356 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019357 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019358 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019359 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019360 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19361 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019362 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019363 else
19364 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019365 size_t len;
19366
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019367 p = get_tv_string_buf_chk(&argvars[1], buf);
19368 if (p == NULL)
19369 {
19370 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019371 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019372 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019373 len = STRLEN(p);
19374 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019375 err = TRUE;
19376 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019377 if (fclose(fd) != 0)
19378 err = TRUE;
19379 if (err)
19380 {
19381 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019382 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019383 }
19384 }
19385
Bram Moolenaar52a72462014-08-29 15:53:52 +020019386 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19387 * echoes typeahead, that messes up the display. */
19388 if (!msg_silent)
19389 flags += SHELL_COOKED;
19390
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019391 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019393 int len;
19394 listitem_T *li;
19395 char_u *s = NULL;
19396 char_u *start;
19397 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019398 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399
Bram Moolenaar52a72462014-08-29 15:53:52 +020019400 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019401 if (res == NULL)
19402 goto errret;
19403
19404 list = list_alloc();
19405 if (list == NULL)
19406 goto errret;
19407
19408 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019410 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019411 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019412 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019413 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019414
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019415 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019416 if (s == NULL)
19417 goto errret;
19418
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019419 for (p = s; start < end; ++p, ++start)
19420 *p = *start == NUL ? NL : *start;
19421 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019422
19423 li = listitem_alloc();
19424 if (li == NULL)
19425 {
19426 vim_free(s);
19427 goto errret;
19428 }
19429 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019430 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019431 li->li_tv.vval.v_string = s;
19432 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019434
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019435 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019436 rettv->v_type = VAR_LIST;
19437 rettv->vval.v_list = list;
19438 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019440 else
19441 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019442 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019443#ifdef USE_CR
19444 /* translate <CR> into <NL> */
19445 if (res != NULL)
19446 {
19447 char_u *s;
19448
19449 for (s = res; *s; ++s)
19450 {
19451 if (*s == CAR)
19452 *s = NL;
19453 }
19454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455#else
19456# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019457 /* translate <CR><NL> into <NL> */
19458 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019460 char_u *s, *d;
19461
19462 d = res;
19463 for (s = res; *s; ++s)
19464 {
19465 if (s[0] == CAR && s[1] == NL)
19466 ++s;
19467 *d++ = *s;
19468 }
19469 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471# endif
19472#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019473 rettv->vval.v_string = res;
19474 res = NULL;
19475 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019476
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019477errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019478 if (infile != NULL)
19479 {
19480 mch_remove(infile);
19481 vim_free(infile);
19482 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019483 if (res != NULL)
19484 vim_free(res);
19485 if (list != NULL)
19486 list_free(list, TRUE);
19487}
19488
19489/*
19490 * "system()" function
19491 */
19492 static void
19493f_system(argvars, rettv)
19494 typval_T *argvars;
19495 typval_T *rettv;
19496{
19497 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19498}
19499
19500/*
19501 * "systemlist()" function
19502 */
19503 static void
19504f_systemlist(argvars, rettv)
19505 typval_T *argvars;
19506 typval_T *rettv;
19507{
19508 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509}
19510
19511/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019512 * "tabpagebuflist()" function
19513 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019514 static void
19515f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019516 typval_T *argvars UNUSED;
19517 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019518{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019519#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019520 tabpage_T *tp;
19521 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019522
19523 if (argvars[0].v_type == VAR_UNKNOWN)
19524 wp = firstwin;
19525 else
19526 {
19527 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19528 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019529 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019530 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019531 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019532 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019533 for (; wp != NULL; wp = wp->w_next)
19534 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019535 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019536 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019537 }
19538#endif
19539}
19540
19541
19542/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019543 * "tabpagenr()" function
19544 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019545 static void
19546f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019547 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019548 typval_T *rettv;
19549{
19550 int nr = 1;
19551#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019552 char_u *arg;
19553
19554 if (argvars[0].v_type != VAR_UNKNOWN)
19555 {
19556 arg = get_tv_string_chk(&argvars[0]);
19557 nr = 0;
19558 if (arg != NULL)
19559 {
19560 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019561 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019562 else
19563 EMSG2(_(e_invexpr2), arg);
19564 }
19565 }
19566 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019567 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019568#endif
19569 rettv->vval.v_number = nr;
19570}
19571
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019572
19573#ifdef FEAT_WINDOWS
19574static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19575
19576/*
19577 * Common code for tabpagewinnr() and winnr().
19578 */
19579 static int
19580get_winnr(tp, argvar)
19581 tabpage_T *tp;
19582 typval_T *argvar;
19583{
19584 win_T *twin;
19585 int nr = 1;
19586 win_T *wp;
19587 char_u *arg;
19588
19589 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19590 if (argvar->v_type != VAR_UNKNOWN)
19591 {
19592 arg = get_tv_string_chk(argvar);
19593 if (arg == NULL)
19594 nr = 0; /* type error; errmsg already given */
19595 else if (STRCMP(arg, "$") == 0)
19596 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19597 else if (STRCMP(arg, "#") == 0)
19598 {
19599 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19600 if (twin == NULL)
19601 nr = 0;
19602 }
19603 else
19604 {
19605 EMSG2(_(e_invexpr2), arg);
19606 nr = 0;
19607 }
19608 }
19609
19610 if (nr > 0)
19611 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19612 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019613 {
19614 if (wp == NULL)
19615 {
19616 /* didn't find it in this tabpage */
19617 nr = 0;
19618 break;
19619 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019620 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019621 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019622 return nr;
19623}
19624#endif
19625
19626/*
19627 * "tabpagewinnr()" function
19628 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019629 static void
19630f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019631 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019632 typval_T *rettv;
19633{
19634 int nr = 1;
19635#ifdef FEAT_WINDOWS
19636 tabpage_T *tp;
19637
19638 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19639 if (tp == NULL)
19640 nr = 0;
19641 else
19642 nr = get_winnr(tp, &argvars[1]);
19643#endif
19644 rettv->vval.v_number = nr;
19645}
19646
19647
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019648/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019649 * "tagfiles()" function
19650 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019651 static void
19652f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019653 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019654 typval_T *rettv;
19655{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019656 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019657 tagname_T tn;
19658 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019659
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019660 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019661 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019662 fname = alloc(MAXPATHL);
19663 if (fname == NULL)
19664 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019665
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019666 for (first = TRUE; ; first = FALSE)
19667 if (get_tagfname(&tn, first, fname) == FAIL
19668 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019669 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019670 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019671 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019672}
19673
19674/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019675 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019676 */
19677 static void
19678f_taglist(argvars, rettv)
19679 typval_T *argvars;
19680 typval_T *rettv;
19681{
19682 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019683
19684 tag_pattern = get_tv_string(&argvars[0]);
19685
19686 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019687 if (*tag_pattern == NUL)
19688 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019689
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019690 if (rettv_list_alloc(rettv) == OK)
19691 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019692}
19693
19694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 * "tempname()" function
19696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019698f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019699 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701{
19702 static int x = 'A';
19703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019704 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019705 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706
19707 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19708 * names. Skip 'I' and 'O', they are used for shell redirection. */
19709 do
19710 {
19711 if (x == 'Z')
19712 x = '0';
19713 else if (x == '9')
19714 x = 'A';
19715 else
19716 {
19717#ifdef EBCDIC
19718 if (x == 'I')
19719 x = 'J';
19720 else if (x == 'R')
19721 x = 'S';
19722 else
19723#endif
19724 ++x;
19725 }
19726 } while (x == 'I' || x == 'O');
19727}
19728
19729/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019730 * "test(list)" function: Just checking the walls...
19731 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019732 static void
19733f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019734 typval_T *argvars UNUSED;
19735 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019736{
19737 /* Used for unit testing. Change the code below to your liking. */
19738#if 0
19739 listitem_T *li;
19740 list_T *l;
19741 char_u *bad, *good;
19742
19743 if (argvars[0].v_type != VAR_LIST)
19744 return;
19745 l = argvars[0].vval.v_list;
19746 if (l == NULL)
19747 return;
19748 li = l->lv_first;
19749 if (li == NULL)
19750 return;
19751 bad = get_tv_string(&li->li_tv);
19752 li = li->li_next;
19753 if (li == NULL)
19754 return;
19755 good = get_tv_string(&li->li_tv);
19756 rettv->vval.v_number = test_edit_score(bad, good);
19757#endif
19758}
19759
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019760#ifdef FEAT_FLOAT
19761/*
19762 * "tan()" function
19763 */
19764 static void
19765f_tan(argvars, rettv)
19766 typval_T *argvars;
19767 typval_T *rettv;
19768{
19769 float_T f;
19770
19771 rettv->v_type = VAR_FLOAT;
19772 if (get_float_arg(argvars, &f) == OK)
19773 rettv->vval.v_float = tan(f);
19774 else
19775 rettv->vval.v_float = 0.0;
19776}
19777
19778/*
19779 * "tanh()" function
19780 */
19781 static void
19782f_tanh(argvars, rettv)
19783 typval_T *argvars;
19784 typval_T *rettv;
19785{
19786 float_T f;
19787
19788 rettv->v_type = VAR_FLOAT;
19789 if (get_float_arg(argvars, &f) == OK)
19790 rettv->vval.v_float = tanh(f);
19791 else
19792 rettv->vval.v_float = 0.0;
19793}
19794#endif
19795
Bram Moolenaard52d9742005-08-21 22:20:28 +000019796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 * "tolower(string)" function
19798 */
19799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019800f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019801 typval_T *argvars;
19802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803{
19804 char_u *p;
19805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019806 p = vim_strsave(get_tv_string(&argvars[0]));
19807 rettv->v_type = VAR_STRING;
19808 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809
19810 if (p != NULL)
19811 while (*p != NUL)
19812 {
19813#ifdef FEAT_MBYTE
19814 int l;
19815
19816 if (enc_utf8)
19817 {
19818 int c, lc;
19819
19820 c = utf_ptr2char(p);
19821 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019822 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 /* TODO: reallocate string when byte count changes. */
19824 if (utf_char2len(lc) == l)
19825 utf_char2bytes(lc, p);
19826 p += l;
19827 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019828 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829 p += l; /* skip multi-byte character */
19830 else
19831#endif
19832 {
19833 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19834 ++p;
19835 }
19836 }
19837}
19838
19839/*
19840 * "toupper(string)" function
19841 */
19842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019843f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019844 typval_T *argvars;
19845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019847 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019848 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849}
19850
19851/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019852 * "tr(string, fromstr, tostr)" function
19853 */
19854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019855f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019856 typval_T *argvars;
19857 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019858{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019859 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019860 char_u *fromstr;
19861 char_u *tostr;
19862 char_u *p;
19863#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019864 int inlen;
19865 int fromlen;
19866 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019867 int idx;
19868 char_u *cpstr;
19869 int cplen;
19870 int first = TRUE;
19871#endif
19872 char_u buf[NUMBUFLEN];
19873 char_u buf2[NUMBUFLEN];
19874 garray_T ga;
19875
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019876 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019877 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19878 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019879
19880 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019881 rettv->v_type = VAR_STRING;
19882 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019883 if (fromstr == NULL || tostr == NULL)
19884 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019885 ga_init2(&ga, (int)sizeof(char), 80);
19886
19887#ifdef FEAT_MBYTE
19888 if (!has_mbyte)
19889#endif
19890 /* not multi-byte: fromstr and tostr must be the same length */
19891 if (STRLEN(fromstr) != STRLEN(tostr))
19892 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019893#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019894error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019895#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019896 EMSG2(_(e_invarg2), fromstr);
19897 ga_clear(&ga);
19898 return;
19899 }
19900
19901 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019902 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019903 {
19904#ifdef FEAT_MBYTE
19905 if (has_mbyte)
19906 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019907 inlen = (*mb_ptr2len)(in_str);
19908 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019909 cplen = inlen;
19910 idx = 0;
19911 for (p = fromstr; *p != NUL; p += fromlen)
19912 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019913 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019914 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019915 {
19916 for (p = tostr; *p != NUL; p += tolen)
19917 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019918 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019919 if (idx-- == 0)
19920 {
19921 cplen = tolen;
19922 cpstr = p;
19923 break;
19924 }
19925 }
19926 if (*p == NUL) /* tostr is shorter than fromstr */
19927 goto error;
19928 break;
19929 }
19930 ++idx;
19931 }
19932
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019933 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019934 {
19935 /* Check that fromstr and tostr have the same number of
19936 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019937 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019938 first = FALSE;
19939 for (p = tostr; *p != NUL; p += tolen)
19940 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019941 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019942 --idx;
19943 }
19944 if (idx != 0)
19945 goto error;
19946 }
19947
Bram Moolenaarcde88542015-08-11 19:14:00 +020019948 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019949 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019950 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019951
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019952 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019953 }
19954 else
19955#endif
19956 {
19957 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019958 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019959 if (p != NULL)
19960 ga_append(&ga, tostr[p - fromstr]);
19961 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019962 ga_append(&ga, *in_str);
19963 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019964 }
19965 }
19966
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019967 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019968 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019969 ga_append(&ga, NUL);
19970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019971 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019972}
19973
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019974#ifdef FEAT_FLOAT
19975/*
19976 * "trunc({float})" function
19977 */
19978 static void
19979f_trunc(argvars, rettv)
19980 typval_T *argvars;
19981 typval_T *rettv;
19982{
19983 float_T f;
19984
19985 rettv->v_type = VAR_FLOAT;
19986 if (get_float_arg(argvars, &f) == OK)
19987 /* trunc() is not in C90, use floor() or ceil() instead. */
19988 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19989 else
19990 rettv->vval.v_float = 0.0;
19991}
19992#endif
19993
Bram Moolenaar8299df92004-07-10 09:47:34 +000019994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995 * "type(expr)" function
19996 */
19997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019998f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019999 typval_T *argvars;
20000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020002 int n;
20003
20004 switch (argvars[0].v_type)
20005 {
20006 case VAR_NUMBER: n = 0; break;
20007 case VAR_STRING: n = 1; break;
20008 case VAR_FUNC: n = 2; break;
20009 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020010 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020011#ifdef FEAT_FLOAT
20012 case VAR_FLOAT: n = 5; break;
20013#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020014 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
20015 }
20016 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017}
20018
20019/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020020 * "undofile(name)" function
20021 */
20022 static void
20023f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010020024 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020025 typval_T *rettv;
20026{
20027 rettv->v_type = VAR_STRING;
20028#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020029 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020030 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020031
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020032 if (*fname == NUL)
20033 {
20034 /* If there is no file name there will be no undo file. */
20035 rettv->vval.v_string = NULL;
20036 }
20037 else
20038 {
20039 char_u *ffname = FullName_save(fname, FALSE);
20040
20041 if (ffname != NULL)
20042 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20043 vim_free(ffname);
20044 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020045 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020046#else
20047 rettv->vval.v_string = NULL;
20048#endif
20049}
20050
20051/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020052 * "undotree()" function
20053 */
20054 static void
20055f_undotree(argvars, rettv)
20056 typval_T *argvars UNUSED;
20057 typval_T *rettv;
20058{
20059 if (rettv_dict_alloc(rettv) == OK)
20060 {
20061 dict_T *dict = rettv->vval.v_dict;
20062 list_T *list;
20063
Bram Moolenaar730cde92010-06-27 05:18:54 +020020064 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020065 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020066 dict_add_nr_str(dict, "save_last",
20067 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020068 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20069 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020070 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020071
20072 list = list_alloc();
20073 if (list != NULL)
20074 {
20075 u_eval_tree(curbuf->b_u_oldhead, list);
20076 dict_add_list(dict, "entries", list);
20077 }
20078 }
20079}
20080
20081/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020082 * "values(dict)" function
20083 */
20084 static void
20085f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020086 typval_T *argvars;
20087 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020088{
20089 dict_list(argvars, rettv, 1);
20090}
20091
20092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 * "virtcol(string)" function
20094 */
20095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020097 typval_T *argvars;
20098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099{
20100 colnr_T vcol = 0;
20101 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020102 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020104 fp = var2fpos(&argvars[0], FALSE, &fnum);
20105 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20106 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 {
20108 getvvcol(curwin, fp, NULL, NULL, &vcol);
20109 ++vcol;
20110 }
20111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020112 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113}
20114
20115/*
20116 * "visualmode()" function
20117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020119f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020120 typval_T *argvars UNUSED;
20121 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123 char_u str[2];
20124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020125 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 str[0] = curbuf->b_visual_mode_eval;
20127 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020128 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129
20130 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020131 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133}
20134
20135/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020136 * "wildmenumode()" function
20137 */
20138 static void
20139f_wildmenumode(argvars, rettv)
20140 typval_T *argvars UNUSED;
20141 typval_T *rettv UNUSED;
20142{
20143#ifdef FEAT_WILDMENU
20144 if (wild_menu_showing)
20145 rettv->vval.v_number = 1;
20146#endif
20147}
20148
20149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150 * "winbufnr(nr)" function
20151 */
20152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020153f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020154 typval_T *argvars;
20155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156{
20157 win_T *wp;
20158
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020159 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020161 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020163 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164}
20165
20166/*
20167 * "wincol()" function
20168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020170f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020171 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173{
20174 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020175 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176}
20177
20178/*
20179 * "winheight(nr)" function
20180 */
20181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020182f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020183 typval_T *argvars;
20184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185{
20186 win_T *wp;
20187
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020188 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020190 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020192 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193}
20194
20195/*
20196 * "winline()" function
20197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020199f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020200 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202{
20203 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020204 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205}
20206
20207/*
20208 * "winnr()" function
20209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020211f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020212 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214{
20215 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020216
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020218 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020220 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221}
20222
20223/*
20224 * "winrestcmd()" function
20225 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020227f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020228 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230{
20231#ifdef FEAT_WINDOWS
20232 win_T *wp;
20233 int winnr = 1;
20234 garray_T ga;
20235 char_u buf[50];
20236
20237 ga_init2(&ga, (int)sizeof(char), 70);
20238 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20239 {
20240 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20241 ga_concat(&ga, buf);
20242# ifdef FEAT_VERTSPLIT
20243 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20244 ga_concat(&ga, buf);
20245# endif
20246 ++winnr;
20247 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020248 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020250 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020252 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020254 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255}
20256
20257/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020258 * "winrestview()" function
20259 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020260 static void
20261f_winrestview(argvars, rettv)
20262 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020263 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020264{
20265 dict_T *dict;
20266
20267 if (argvars[0].v_type != VAR_DICT
20268 || (dict = argvars[0].vval.v_dict) == NULL)
20269 EMSG(_(e_invarg));
20270 else
20271 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020272 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20273 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20274 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20275 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020276#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020277 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20278 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020279#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020280 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20281 {
20282 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20283 curwin->w_set_curswant = FALSE;
20284 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020285
Bram Moolenaar82c25852014-05-28 16:47:16 +020020286 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20287 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020288#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020289 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20290 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020291#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020292 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20293 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20294 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20295 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020296
20297 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020298 win_new_height(curwin, curwin->w_height);
20299# ifdef FEAT_VERTSPLIT
20300 win_new_width(curwin, W_WIDTH(curwin));
20301# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020302 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020303
Bram Moolenaarb851a962014-10-31 15:45:52 +010020304 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020305 curwin->w_topline = 1;
20306 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20307 curwin->w_topline = curbuf->b_ml.ml_line_count;
20308#ifdef FEAT_DIFF
20309 check_topfill(curwin, TRUE);
20310#endif
20311 }
20312}
20313
20314/*
20315 * "winsaveview()" function
20316 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020317 static void
20318f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020319 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020320 typval_T *rettv;
20321{
20322 dict_T *dict;
20323
Bram Moolenaara800b422010-06-27 01:15:55 +020020324 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020325 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020326 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020327
20328 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20329 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20330#ifdef FEAT_VIRTUALEDIT
20331 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20332#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020333 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020334 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20335
20336 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20337#ifdef FEAT_DIFF
20338 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20339#endif
20340 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20341 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20342}
20343
20344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 * "winwidth(nr)" function
20346 */
20347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020348f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020349 typval_T *argvars;
20350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351{
20352 win_T *wp;
20353
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020354 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020356 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 else
20358#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020359 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020361 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362#endif
20363}
20364
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020366 * "wordcount()" function
20367 */
20368 static void
20369f_wordcount(argvars, rettv)
20370 typval_T *argvars UNUSED;
20371 typval_T *rettv;
20372{
20373 if (rettv_dict_alloc(rettv) == FAIL)
20374 return;
20375 cursor_pos_info(rettv->vval.v_dict);
20376}
20377
20378/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020379 * Write list of strings to file
20380 */
20381 static int
20382write_list(fd, list, binary)
20383 FILE *fd;
20384 list_T *list;
20385 int binary;
20386{
20387 listitem_T *li;
20388 int c;
20389 int ret = OK;
20390 char_u *s;
20391
20392 for (li = list->lv_first; li != NULL; li = li->li_next)
20393 {
20394 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20395 {
20396 if (*s == '\n')
20397 c = putc(NUL, fd);
20398 else
20399 c = putc(*s, fd);
20400 if (c == EOF)
20401 {
20402 ret = FAIL;
20403 break;
20404 }
20405 }
20406 if (!binary || li->li_next != NULL)
20407 if (putc('\n', fd) == EOF)
20408 {
20409 ret = FAIL;
20410 break;
20411 }
20412 if (ret == FAIL)
20413 {
20414 EMSG(_(e_write));
20415 break;
20416 }
20417 }
20418 return ret;
20419}
20420
20421/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020422 * "writefile()" function
20423 */
20424 static void
20425f_writefile(argvars, rettv)
20426 typval_T *argvars;
20427 typval_T *rettv;
20428{
20429 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020430 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020431 char_u *fname;
20432 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020433 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020434
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020435 if (check_restricted() || check_secure())
20436 return;
20437
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020438 if (argvars[0].v_type != VAR_LIST)
20439 {
20440 EMSG2(_(e_listarg), "writefile()");
20441 return;
20442 }
20443 if (argvars[0].vval.v_list == NULL)
20444 return;
20445
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020446 if (argvars[2].v_type != VAR_UNKNOWN)
20447 {
20448 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20449 binary = TRUE;
20450 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20451 append = TRUE;
20452 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020453
20454 /* Always open the file in binary mode, library functions have a mind of
20455 * their own about CR-LF conversion. */
20456 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020457 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20458 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020459 {
20460 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20461 ret = -1;
20462 }
20463 else
20464 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020465 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20466 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020467 fclose(fd);
20468 }
20469
20470 rettv->vval.v_number = ret;
20471}
20472
20473/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020474 * "xor(expr, expr)" function
20475 */
20476 static void
20477f_xor(argvars, rettv)
20478 typval_T *argvars;
20479 typval_T *rettv;
20480{
20481 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20482 ^ get_tv_number_chk(&argvars[1], NULL);
20483}
20484
20485
20486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020488 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489 */
20490 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020491var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020492 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020493 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020494 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020496 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020498 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499
Bram Moolenaara5525202006-03-02 22:52:09 +000020500 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020501 if (varp->v_type == VAR_LIST)
20502 {
20503 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020504 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020505 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020506 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020507
20508 l = varp->vval.v_list;
20509 if (l == NULL)
20510 return NULL;
20511
20512 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020513 pos.lnum = list_find_nr(l, 0L, &error);
20514 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020515 return NULL; /* invalid line number */
20516
20517 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020518 pos.col = list_find_nr(l, 1L, &error);
20519 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020520 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020521 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020522
20523 /* We accept "$" for the column number: last column. */
20524 li = list_find(l, 1L);
20525 if (li != NULL && li->li_tv.v_type == VAR_STRING
20526 && li->li_tv.vval.v_string != NULL
20527 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20528 pos.col = len + 1;
20529
Bram Moolenaara5525202006-03-02 22:52:09 +000020530 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020531 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020532 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020533 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020534
Bram Moolenaara5525202006-03-02 22:52:09 +000020535#ifdef FEAT_VIRTUALEDIT
20536 /* Get the virtual offset. Defaults to zero. */
20537 pos.coladd = list_find_nr(l, 2L, &error);
20538 if (error)
20539 pos.coladd = 0;
20540#endif
20541
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020542 return &pos;
20543 }
20544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020545 name = get_tv_string_chk(varp);
20546 if (name == NULL)
20547 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020548 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020550 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20551 {
20552 if (VIsual_active)
20553 return &VIsual;
20554 return &curwin->w_cursor;
20555 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020556 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020558 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20560 return NULL;
20561 return pp;
20562 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020563
20564#ifdef FEAT_VIRTUALEDIT
20565 pos.coladd = 0;
20566#endif
20567
Bram Moolenaar477933c2007-07-17 14:32:23 +000020568 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020569 {
20570 pos.col = 0;
20571 if (name[1] == '0') /* "w0": first visible line */
20572 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020573 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020574 pos.lnum = curwin->w_topline;
20575 return &pos;
20576 }
20577 else if (name[1] == '$') /* "w$": last visible line */
20578 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020579 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020580 pos.lnum = curwin->w_botline - 1;
20581 return &pos;
20582 }
20583 }
20584 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020586 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587 {
20588 pos.lnum = curbuf->b_ml.ml_line_count;
20589 pos.col = 0;
20590 }
20591 else
20592 {
20593 pos.lnum = curwin->w_cursor.lnum;
20594 pos.col = (colnr_T)STRLEN(ml_get_curline());
20595 }
20596 return &pos;
20597 }
20598 return NULL;
20599}
20600
20601/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020602 * Convert list in "arg" into a position and optional file number.
20603 * When "fnump" is NULL there is no file number, only 3 items.
20604 * Note that the column is passed on as-is, the caller may want to decrement
20605 * it to use 1 for the first column.
20606 * Return FAIL when conversion is not possible, doesn't check the position for
20607 * validity.
20608 */
20609 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020610list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020611 typval_T *arg;
20612 pos_T *posp;
20613 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020614 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020615{
20616 list_T *l = arg->vval.v_list;
20617 long i = 0;
20618 long n;
20619
Bram Moolenaar493c1782014-05-28 14:34:46 +020020620 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20621 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020622 if (arg->v_type != VAR_LIST
20623 || l == NULL
20624 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020625 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020626 return FAIL;
20627
20628 if (fnump != NULL)
20629 {
20630 n = list_find_nr(l, i++, NULL); /* fnum */
20631 if (n < 0)
20632 return FAIL;
20633 if (n == 0)
20634 n = curbuf->b_fnum; /* current buffer */
20635 *fnump = n;
20636 }
20637
20638 n = list_find_nr(l, i++, NULL); /* lnum */
20639 if (n < 0)
20640 return FAIL;
20641 posp->lnum = n;
20642
20643 n = list_find_nr(l, i++, NULL); /* col */
20644 if (n < 0)
20645 return FAIL;
20646 posp->col = n;
20647
20648#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020649 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020650 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020651 posp->coladd = 0;
20652 else
20653 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020654#endif
20655
Bram Moolenaar493c1782014-05-28 14:34:46 +020020656 if (curswantp != NULL)
20657 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20658
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020659 return OK;
20660}
20661
20662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 * Get the length of an environment variable name.
20664 * Advance "arg" to the first character after the name.
20665 * Return 0 for error.
20666 */
20667 static int
20668get_env_len(arg)
20669 char_u **arg;
20670{
20671 char_u *p;
20672 int len;
20673
20674 for (p = *arg; vim_isIDc(*p); ++p)
20675 ;
20676 if (p == *arg) /* no name found */
20677 return 0;
20678
20679 len = (int)(p - *arg);
20680 *arg = p;
20681 return len;
20682}
20683
20684/*
20685 * Get the length of the name of a function or internal variable.
20686 * "arg" is advanced to the first non-white character after the name.
20687 * Return 0 if something is wrong.
20688 */
20689 static int
20690get_id_len(arg)
20691 char_u **arg;
20692{
20693 char_u *p;
20694 int len;
20695
20696 /* Find the end of the name. */
20697 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020698 {
20699 if (*p == ':')
20700 {
20701 /* "s:" is start of "s:var", but "n:" is not and can be used in
20702 * slice "[n:]". Also "xx:" is not a namespace. */
20703 len = (int)(p - *arg);
20704 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20705 || len > 1)
20706 break;
20707 }
20708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020709 if (p == *arg) /* no name found */
20710 return 0;
20711
20712 len = (int)(p - *arg);
20713 *arg = skipwhite(p);
20714
20715 return len;
20716}
20717
20718/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020719 * Get the length of the name of a variable or function.
20720 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020722 * Return -1 if curly braces expansion failed.
20723 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020724 * If the name contains 'magic' {}'s, expand them and return the
20725 * expanded name in an allocated string via 'alias' - caller must free.
20726 */
20727 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020728get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 char_u **arg;
20730 char_u **alias;
20731 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020732 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020733{
20734 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 char_u *p;
20736 char_u *expr_start;
20737 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738
20739 *alias = NULL; /* default to no alias */
20740
20741 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20742 && (*arg)[2] == (int)KE_SNR)
20743 {
20744 /* hard coded <SNR>, already translated */
20745 *arg += 3;
20746 return get_id_len(arg) + 3;
20747 }
20748 len = eval_fname_script(*arg);
20749 if (len > 0)
20750 {
20751 /* literal "<SID>", "s:" or "<SNR>" */
20752 *arg += len;
20753 }
20754
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020756 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020758 p = find_name_end(*arg, &expr_start, &expr_end,
20759 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760 if (expr_start != NULL)
20761 {
20762 char_u *temp_string;
20763
20764 if (!evaluate)
20765 {
20766 len += (int)(p - *arg);
20767 *arg = skipwhite(p);
20768 return len;
20769 }
20770
20771 /*
20772 * Include any <SID> etc in the expanded string:
20773 * Thus the -len here.
20774 */
20775 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20776 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020777 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 *alias = temp_string;
20779 *arg = skipwhite(p);
20780 return (int)STRLEN(temp_string);
20781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782
20783 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020784 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785 EMSG2(_(e_invexpr2), *arg);
20786
20787 return len;
20788}
20789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020790/*
20791 * Find the end of a variable or function name, taking care of magic braces.
20792 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20793 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020794 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020795 * Return a pointer to just after the name. Equal to "arg" if there is no
20796 * valid name.
20797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020799find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800 char_u *arg;
20801 char_u **expr_start;
20802 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020803 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020805 int mb_nest = 0;
20806 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020808 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020810 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020812 *expr_start = NULL;
20813 *expr_end = NULL;
20814 }
20815
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020816 /* Quick check for valid starting character. */
20817 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20818 return arg;
20819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020820 for (p = arg; *p != NUL
20821 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020822 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020823 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020824 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020825 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020826 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020827 if (*p == '\'')
20828 {
20829 /* skip over 'string' to avoid counting [ and ] inside it. */
20830 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20831 ;
20832 if (*p == NUL)
20833 break;
20834 }
20835 else if (*p == '"')
20836 {
20837 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20838 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20839 if (*p == '\\' && p[1] != NUL)
20840 ++p;
20841 if (*p == NUL)
20842 break;
20843 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020844 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20845 {
20846 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020847 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020848 len = (int)(p - arg);
20849 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020850 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020851 break;
20852 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020854 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020856 if (*p == '[')
20857 ++br_nest;
20858 else if (*p == ']')
20859 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020862 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020864 if (*p == '{')
20865 {
20866 mb_nest++;
20867 if (expr_start != NULL && *expr_start == NULL)
20868 *expr_start = p;
20869 }
20870 else if (*p == '}')
20871 {
20872 mb_nest--;
20873 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20874 *expr_end = p;
20875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877 }
20878
20879 return p;
20880}
20881
20882/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020883 * Expands out the 'magic' {}'s in a variable/function name.
20884 * Note that this can call itself recursively, to deal with
20885 * constructs like foo{bar}{baz}{bam}
20886 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20887 * "in_start" ^
20888 * "expr_start" ^
20889 * "expr_end" ^
20890 * "in_end" ^
20891 *
20892 * Returns a new allocated string, which the caller must free.
20893 * Returns NULL for failure.
20894 */
20895 static char_u *
20896make_expanded_name(in_start, expr_start, expr_end, in_end)
20897 char_u *in_start;
20898 char_u *expr_start;
20899 char_u *expr_end;
20900 char_u *in_end;
20901{
20902 char_u c1;
20903 char_u *retval = NULL;
20904 char_u *temp_result;
20905 char_u *nextcmd = NULL;
20906
20907 if (expr_end == NULL || in_end == NULL)
20908 return NULL;
20909 *expr_start = NUL;
20910 *expr_end = NUL;
20911 c1 = *in_end;
20912 *in_end = NUL;
20913
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020914 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020915 if (temp_result != NULL && nextcmd == NULL)
20916 {
20917 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20918 + (in_end - expr_end) + 1));
20919 if (retval != NULL)
20920 {
20921 STRCPY(retval, in_start);
20922 STRCAT(retval, temp_result);
20923 STRCAT(retval, expr_end + 1);
20924 }
20925 }
20926 vim_free(temp_result);
20927
20928 *in_end = c1; /* put char back for error messages */
20929 *expr_start = '{';
20930 *expr_end = '}';
20931
20932 if (retval != NULL)
20933 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020934 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020935 if (expr_start != NULL)
20936 {
20937 /* Further expansion! */
20938 temp_result = make_expanded_name(retval, expr_start,
20939 expr_end, temp_result);
20940 vim_free(retval);
20941 retval = temp_result;
20942 }
20943 }
20944
20945 return retval;
20946}
20947
20948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020950 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951 */
20952 static int
20953eval_isnamec(c)
20954 int c;
20955{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020956 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20957}
20958
20959/*
20960 * Return TRUE if character "c" can be used as the first character in a
20961 * variable or function name (excluding '{' and '}').
20962 */
20963 static int
20964eval_isnamec1(c)
20965 int c;
20966{
20967 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968}
20969
20970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 * Set number v: variable to "val".
20972 */
20973 void
20974set_vim_var_nr(idx, val)
20975 int idx;
20976 long val;
20977{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020978 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979}
20980
20981/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020982 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 */
20984 long
20985get_vim_var_nr(idx)
20986 int idx;
20987{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020988 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989}
20990
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020991/*
20992 * Get string v: variable value. Uses a static buffer, can only be used once.
20993 */
20994 char_u *
20995get_vim_var_str(idx)
20996 int idx;
20997{
20998 return get_tv_string(&vimvars[idx].vv_tv);
20999}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021000
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021002 * Get List v: variable value. Caller must take care of reference count when
21003 * needed.
21004 */
21005 list_T *
21006get_vim_var_list(idx)
21007 int idx;
21008{
21009 return vimvars[idx].vv_list;
21010}
21011
21012/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021013 * Set v:char to character "c".
21014 */
21015 void
21016set_vim_var_char(c)
21017 int c;
21018{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021019 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021020
21021#ifdef FEAT_MBYTE
21022 if (has_mbyte)
21023 buf[(*mb_char2bytes)(c, buf)] = NUL;
21024 else
21025#endif
21026 {
21027 buf[0] = c;
21028 buf[1] = NUL;
21029 }
21030 set_vim_var_string(VV_CHAR, buf, -1);
21031}
21032
21033/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021034 * Set v:count to "count" and v:count1 to "count1".
21035 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 */
21037 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021038set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021039 long count;
21040 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021041 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021043 if (set_prevcount)
21044 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021045 vimvars[VV_COUNT].vv_nr = count;
21046 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047}
21048
21049/*
21050 * Set string v: variable to a copy of "val".
21051 */
21052 void
21053set_vim_var_string(idx, val, len)
21054 int idx;
21055 char_u *val;
21056 int len; /* length of "val" to use or -1 (whole string) */
21057{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021058 /* Need to do this (at least) once, since we can't initialize a union.
21059 * Will always be invoked when "v:progname" is set. */
21060 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
21061
Bram Moolenaare9a41262005-01-15 22:18:47 +000021062 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021064 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021066 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021068 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069}
21070
21071/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021072 * Set List v: variable to "val".
21073 */
21074 void
21075set_vim_var_list(idx, val)
21076 int idx;
21077 list_T *val;
21078{
21079 list_unref(vimvars[idx].vv_list);
21080 vimvars[idx].vv_list = val;
21081 if (val != NULL)
21082 ++val->lv_refcount;
21083}
21084
21085/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021086 * Set Dictionary v: variable to "val".
21087 */
21088 void
21089set_vim_var_dict(idx, val)
21090 int idx;
21091 dict_T *val;
21092{
21093 int todo;
21094 hashitem_T *hi;
21095
21096 dict_unref(vimvars[idx].vv_dict);
21097 vimvars[idx].vv_dict = val;
21098 if (val != NULL)
21099 {
21100 ++val->dv_refcount;
21101
21102 /* Set readonly */
21103 todo = (int)val->dv_hashtab.ht_used;
21104 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21105 {
21106 if (HASHITEM_EMPTY(hi))
21107 continue;
21108 --todo;
21109 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21110 }
21111 }
21112}
21113
21114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 * Set v:register if needed.
21116 */
21117 void
21118set_reg_var(c)
21119 int c;
21120{
21121 char_u regname;
21122
21123 if (c == 0 || c == ' ')
21124 regname = '"';
21125 else
21126 regname = c;
21127 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021128 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 set_vim_var_string(VV_REG, &regname, 1);
21130}
21131
21132/*
21133 * Get or set v:exception. If "oldval" == NULL, return the current value.
21134 * Otherwise, restore the value to "oldval" and return NULL.
21135 * Must always be called in pairs to save and restore v:exception! Does not
21136 * take care of memory allocations.
21137 */
21138 char_u *
21139v_exception(oldval)
21140 char_u *oldval;
21141{
21142 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021143 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144
Bram Moolenaare9a41262005-01-15 22:18:47 +000021145 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 return NULL;
21147}
21148
21149/*
21150 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21151 * Otherwise, restore the value to "oldval" and return NULL.
21152 * Must always be called in pairs to save and restore v:throwpoint! Does not
21153 * take care of memory allocations.
21154 */
21155 char_u *
21156v_throwpoint(oldval)
21157 char_u *oldval;
21158{
21159 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021160 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161
Bram Moolenaare9a41262005-01-15 22:18:47 +000021162 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163 return NULL;
21164}
21165
21166#if defined(FEAT_AUTOCMD) || defined(PROTO)
21167/*
21168 * Set v:cmdarg.
21169 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21170 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21171 * Must always be called in pairs!
21172 */
21173 char_u *
21174set_cmdarg(eap, oldarg)
21175 exarg_T *eap;
21176 char_u *oldarg;
21177{
21178 char_u *oldval;
21179 char_u *newval;
21180 unsigned len;
21181
Bram Moolenaare9a41262005-01-15 22:18:47 +000021182 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021183 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021185 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021186 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021187 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 }
21189
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021190 if (eap->force_bin == FORCE_BIN)
21191 len = 6;
21192 else if (eap->force_bin == FORCE_NOBIN)
21193 len = 8;
21194 else
21195 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021196
21197 if (eap->read_edit)
21198 len += 7;
21199
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021200 if (eap->force_ff != 0)
21201 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21202# ifdef FEAT_MBYTE
21203 if (eap->force_enc != 0)
21204 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021205 if (eap->bad_char != 0)
21206 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021207# endif
21208
21209 newval = alloc(len + 1);
21210 if (newval == NULL)
21211 return NULL;
21212
21213 if (eap->force_bin == FORCE_BIN)
21214 sprintf((char *)newval, " ++bin");
21215 else if (eap->force_bin == FORCE_NOBIN)
21216 sprintf((char *)newval, " ++nobin");
21217 else
21218 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021219
21220 if (eap->read_edit)
21221 STRCAT(newval, " ++edit");
21222
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021223 if (eap->force_ff != 0)
21224 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21225 eap->cmd + eap->force_ff);
21226# ifdef FEAT_MBYTE
21227 if (eap->force_enc != 0)
21228 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21229 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021230 if (eap->bad_char == BAD_KEEP)
21231 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21232 else if (eap->bad_char == BAD_DROP)
21233 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21234 else if (eap->bad_char != 0)
21235 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021236# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021237 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021238 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239}
21240#endif
21241
21242/*
21243 * Get the value of internal variable "name".
21244 * Return OK or FAIL.
21245 */
21246 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021247get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248 char_u *name;
21249 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021250 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021251 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021252 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021253 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254{
21255 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021256 typval_T *tv = NULL;
21257 typval_T atv;
21258 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260
21261 /* truncate the name, so that we can use strcmp() */
21262 cc = name[len];
21263 name[len] = NUL;
21264
21265 /*
21266 * Check for "b:changedtick".
21267 */
21268 if (STRCMP(name, "b:changedtick") == 0)
21269 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021270 atv.v_type = VAR_NUMBER;
21271 atv.vval.v_number = curbuf->b_changedtick;
21272 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 }
21274
21275 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 * Check for user-defined variables.
21277 */
21278 else
21279 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021280 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021282 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021283 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021284 if (dip != NULL)
21285 *dip = v;
21286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 }
21288
Bram Moolenaare9a41262005-01-15 22:18:47 +000021289 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021291 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021292 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 ret = FAIL;
21294 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021295 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021296 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297
21298 name[len] = cc;
21299
21300 return ret;
21301}
21302
21303/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021304 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21305 * Also handle function call with Funcref variable: func(expr)
21306 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21307 */
21308 static int
21309handle_subscript(arg, rettv, evaluate, verbose)
21310 char_u **arg;
21311 typval_T *rettv;
21312 int evaluate; /* do more than finding the end */
21313 int verbose; /* give error messages */
21314{
21315 int ret = OK;
21316 dict_T *selfdict = NULL;
21317 char_u *s;
21318 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021319 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021320
21321 while (ret == OK
21322 && (**arg == '['
21323 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021324 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021325 && !vim_iswhite(*(*arg - 1)))
21326 {
21327 if (**arg == '(')
21328 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021329 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021330 if (evaluate)
21331 {
21332 functv = *rettv;
21333 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021334
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021335 /* Invoke the function. Recursive! */
21336 s = functv.vval.v_string;
21337 }
21338 else
21339 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021340 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021341 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21342 &len, evaluate, selfdict);
21343
21344 /* Clear the funcref afterwards, so that deleting it while
21345 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021346 if (evaluate)
21347 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021348
21349 /* Stop the expression evaluation when immediately aborting on
21350 * error, or when an interrupt occurred or an exception was thrown
21351 * but not caught. */
21352 if (aborting())
21353 {
21354 if (ret == OK)
21355 clear_tv(rettv);
21356 ret = FAIL;
21357 }
21358 dict_unref(selfdict);
21359 selfdict = NULL;
21360 }
21361 else /* **arg == '[' || **arg == '.' */
21362 {
21363 dict_unref(selfdict);
21364 if (rettv->v_type == VAR_DICT)
21365 {
21366 selfdict = rettv->vval.v_dict;
21367 if (selfdict != NULL)
21368 ++selfdict->dv_refcount;
21369 }
21370 else
21371 selfdict = NULL;
21372 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21373 {
21374 clear_tv(rettv);
21375 ret = FAIL;
21376 }
21377 }
21378 }
21379 dict_unref(selfdict);
21380 return ret;
21381}
21382
21383/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021384 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021385 * value).
21386 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021387 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021388alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021389{
Bram Moolenaar33570922005-01-25 22:26:29 +000021390 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021391}
21392
21393/*
21394 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 * The string "s" must have been allocated, it is consumed.
21396 * Return NULL for out of memory, the variable otherwise.
21397 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021398 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021399alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400 char_u *s;
21401{
Bram Moolenaar33570922005-01-25 22:26:29 +000021402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021404 rettv = alloc_tv();
21405 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021407 rettv->v_type = VAR_STRING;
21408 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409 }
21410 else
21411 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021412 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413}
21414
21415/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021416 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021418 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021419free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021420 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421{
21422 if (varp != NULL)
21423 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021424 switch (varp->v_type)
21425 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021426 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021427 func_unref(varp->vval.v_string);
21428 /*FALLTHROUGH*/
21429 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021430 vim_free(varp->vval.v_string);
21431 break;
21432 case VAR_LIST:
21433 list_unref(varp->vval.v_list);
21434 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021435 case VAR_DICT:
21436 dict_unref(varp->vval.v_dict);
21437 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021438 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021439#ifdef FEAT_FLOAT
21440 case VAR_FLOAT:
21441#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021442 case VAR_UNKNOWN:
21443 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021444 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021445 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021446 break;
21447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448 vim_free(varp);
21449 }
21450}
21451
21452/*
21453 * Free the memory for a variable value and set the value to NULL or 0.
21454 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021455 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021456clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021457 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458{
21459 if (varp != NULL)
21460 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021461 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021463 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021464 func_unref(varp->vval.v_string);
21465 /*FALLTHROUGH*/
21466 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021467 vim_free(varp->vval.v_string);
21468 varp->vval.v_string = NULL;
21469 break;
21470 case VAR_LIST:
21471 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021472 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021473 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021474 case VAR_DICT:
21475 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021476 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021477 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021478 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021479 varp->vval.v_number = 0;
21480 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021481#ifdef FEAT_FLOAT
21482 case VAR_FLOAT:
21483 varp->vval.v_float = 0.0;
21484 break;
21485#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021486 case VAR_UNKNOWN:
21487 break;
21488 default:
21489 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021491 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 }
21493}
21494
21495/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021496 * Set the value of a variable to NULL without freeing items.
21497 */
21498 static void
21499init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021500 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021501{
21502 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021503 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021504}
21505
21506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507 * Get the number value of a variable.
21508 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021509 * For incompatible types, return 0.
21510 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21511 * caller of incompatible types: it sets *denote to TRUE if "denote"
21512 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513 */
21514 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021515get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021516 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021518 int error = FALSE;
21519
21520 return get_tv_number_chk(varp, &error); /* return 0L on error */
21521}
21522
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021523 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021524get_tv_number_chk(varp, denote)
21525 typval_T *varp;
21526 int *denote;
21527{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021528 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021530 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021532 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021533 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021534#ifdef FEAT_FLOAT
21535 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021536 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021537 break;
21538#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021539 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021540 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021541 break;
21542 case VAR_STRING:
21543 if (varp->vval.v_string != NULL)
21544 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021545 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021546 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021547 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021548 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021549 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021550 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021551 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021552 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021553 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021554 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021555 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021557 if (denote == NULL) /* useful for values that must be unsigned */
21558 n = -1;
21559 else
21560 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021561 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562}
21563
21564/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021565 * Get the lnum from the first argument.
21566 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021567 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 */
21569 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021570get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021571 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572{
Bram Moolenaar33570922005-01-25 22:26:29 +000021573 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574 linenr_T lnum;
21575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021576 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 if (lnum == 0) /* no valid number, try using line() */
21578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021579 rettv.v_type = VAR_NUMBER;
21580 f_line(argvars, &rettv);
21581 lnum = rettv.vval.v_number;
21582 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 }
21584 return lnum;
21585}
21586
21587/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021588 * Get the lnum from the first argument.
21589 * Also accepts "$", then "buf" is used.
21590 * Returns 0 on error.
21591 */
21592 static linenr_T
21593get_tv_lnum_buf(argvars, buf)
21594 typval_T *argvars;
21595 buf_T *buf;
21596{
21597 if (argvars[0].v_type == VAR_STRING
21598 && argvars[0].vval.v_string != NULL
21599 && argvars[0].vval.v_string[0] == '$'
21600 && buf != NULL)
21601 return buf->b_ml.ml_line_count;
21602 return get_tv_number_chk(&argvars[0], NULL);
21603}
21604
21605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 * Get the string value of a variable.
21607 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021608 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21609 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610 * If the String variable has never been set, return an empty string.
21611 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021612 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21613 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 */
21615 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021616get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021617 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618{
21619 static char_u mybuf[NUMBUFLEN];
21620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021621 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622}
21623
21624 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021625get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021626 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021627 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021629 char_u *res = get_tv_string_buf_chk(varp, buf);
21630
21631 return res != NULL ? res : (char_u *)"";
21632}
21633
Bram Moolenaar7d647822014-04-05 21:28:56 +020021634/*
21635 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21636 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021637 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021638get_tv_string_chk(varp)
21639 typval_T *varp;
21640{
21641 static char_u mybuf[NUMBUFLEN];
21642
21643 return get_tv_string_buf_chk(varp, mybuf);
21644}
21645
21646 static char_u *
21647get_tv_string_buf_chk(varp, buf)
21648 typval_T *varp;
21649 char_u *buf;
21650{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021651 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021653 case VAR_NUMBER:
21654 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21655 return buf;
21656 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021657 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021658 break;
21659 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021660 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021661 break;
21662 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021663 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021664 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021665#ifdef FEAT_FLOAT
21666 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021667 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021668 break;
21669#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021670 case VAR_STRING:
21671 if (varp->vval.v_string != NULL)
21672 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021673 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021674 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021675 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021676 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021678 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679}
21680
21681/*
21682 * Find variable "name" in the list of variables.
21683 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021684 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021685 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021686 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021688 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021689find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021691 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021692 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021695 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696
Bram Moolenaara7043832005-01-21 11:56:39 +000021697 ht = find_var_ht(name, &varname);
21698 if (htp != NULL)
21699 *htp = ht;
21700 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021702 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703}
21704
21705/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021706 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021707 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021709 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021710find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021711 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021712 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021713 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021714 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021715{
Bram Moolenaar33570922005-01-25 22:26:29 +000021716 hashitem_T *hi;
21717
21718 if (*varname == NUL)
21719 {
21720 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021721 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021722 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021723 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021724 case 'g': return &globvars_var;
21725 case 'v': return &vimvars_var;
21726 case 'b': return &curbuf->b_bufvar;
21727 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021728#ifdef FEAT_WINDOWS
21729 case 't': return &curtab->tp_winvar;
21730#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021731 case 'l': return current_funccal == NULL
21732 ? NULL : &current_funccal->l_vars_var;
21733 case 'a': return current_funccal == NULL
21734 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021735 }
21736 return NULL;
21737 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021738
21739 hi = hash_find(ht, varname);
21740 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021741 {
21742 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021743 * worked find the variable again. Don't auto-load a script if it was
21744 * loaded already, otherwise it would be loaded every time when
21745 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021746 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021747 {
21748 /* Note: script_autoload() may make "hi" invalid. It must either
21749 * be obtained again or not used. */
21750 if (!script_autoload(varname, FALSE) || aborting())
21751 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021752 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021753 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021754 if (HASHITEM_EMPTY(hi))
21755 return NULL;
21756 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021757 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021758}
21759
21760/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021761 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021762 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021763 * Set "varname" to the start of name without ':'.
21764 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021765 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021766find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 char_u *name;
21768 char_u **varname;
21769{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021770 hashitem_T *hi;
21771
Bram Moolenaar73627d02015-08-11 15:46:09 +020021772 if (name[0] == NUL)
21773 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774 if (name[1] != ':')
21775 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021776 /* The name must not start with a colon or #. */
21777 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021778 return NULL;
21779 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021780
21781 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021782 hi = hash_find(&compat_hashtab, name);
21783 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021784 return &compat_hashtab;
21785
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021787 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021788 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789 }
21790 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021791 if (*name == 'g') /* global variable */
21792 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021793 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21794 */
21795 if (vim_strchr(name + 2, ':') != NULL
21796 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021797 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021799 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021801 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021802#ifdef FEAT_WINDOWS
21803 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021804 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021805#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021806 if (*name == 'v') /* v: variable */
21807 return &vimvarht;
21808 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021809 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021810 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021811 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812 if (*name == 's' /* script variable */
21813 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21814 return &SCRIPT_VARS(current_SID);
21815 return NULL;
21816}
21817
21818/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021819 * Get function call environment based on bactrace debug level
21820 */
21821 static funccall_T *
21822get_funccal()
21823{
21824 int i;
21825 funccall_T *funccal;
21826 funccall_T *temp_funccal;
21827
21828 funccal = current_funccal;
21829 if (debug_backtrace_level > 0)
21830 {
21831 for (i = 0; i < debug_backtrace_level; i++)
21832 {
21833 temp_funccal = funccal->caller;
21834 if (temp_funccal)
21835 funccal = temp_funccal;
21836 else
21837 /* backtrace level overflow. reset to max */
21838 debug_backtrace_level = i;
21839 }
21840 }
21841 return funccal;
21842}
21843
21844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021846 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 * Returns NULL when it doesn't exist.
21848 */
21849 char_u *
21850get_var_value(name)
21851 char_u *name;
21852{
Bram Moolenaar33570922005-01-25 22:26:29 +000021853 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021855 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856 if (v == NULL)
21857 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021858 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859}
21860
21861/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021862 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 * sourcing this script and when executing functions defined in the script.
21864 */
21865 void
21866new_script_vars(id)
21867 scid_T id;
21868{
Bram Moolenaara7043832005-01-21 11:56:39 +000021869 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021870 hashtab_T *ht;
21871 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021872
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21874 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021875 /* Re-allocating ga_data means that an ht_array pointing to
21876 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021877 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021878 for (i = 1; i <= ga_scripts.ga_len; ++i)
21879 {
21880 ht = &SCRIPT_VARS(i);
21881 if (ht->ht_mask == HT_INIT_SIZE - 1)
21882 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021883 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021884 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021885 }
21886
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887 while (ga_scripts.ga_len < id)
21888 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021889 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021890 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021891 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 }
21894 }
21895}
21896
21897/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021898 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21899 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900 */
21901 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021902init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021903 dict_T *dict;
21904 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021905 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906{
Bram Moolenaar33570922005-01-25 22:26:29 +000021907 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021908 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021909 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021910 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021911 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021912 dict_var->di_tv.vval.v_dict = dict;
21913 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021914 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021915 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21916 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021917}
21918
21919/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021920 * Unreference a dictionary initialized by init_var_dict().
21921 */
21922 void
21923unref_var_dict(dict)
21924 dict_T *dict;
21925{
21926 /* Now the dict needs to be freed if no one else is using it, go back to
21927 * normal reference counting. */
21928 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21929 dict_unref(dict);
21930}
21931
21932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021934 * Frees all allocated variables and the value they contain.
21935 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 */
21937 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021938vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021939 hashtab_T *ht;
21940{
21941 vars_clear_ext(ht, TRUE);
21942}
21943
21944/*
21945 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21946 */
21947 static void
21948vars_clear_ext(ht, free_val)
21949 hashtab_T *ht;
21950 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951{
Bram Moolenaara7043832005-01-21 11:56:39 +000021952 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021953 hashitem_T *hi;
21954 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955
Bram Moolenaar33570922005-01-25 22:26:29 +000021956 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021957 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021958 for (hi = ht->ht_array; todo > 0; ++hi)
21959 {
21960 if (!HASHITEM_EMPTY(hi))
21961 {
21962 --todo;
21963
Bram Moolenaar33570922005-01-25 22:26:29 +000021964 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021965 * ht_array might change then. hash_clear() takes care of it
21966 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021967 v = HI2DI(hi);
21968 if (free_val)
21969 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021970 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021971 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021972 }
21973 }
21974 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021975 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976}
21977
Bram Moolenaara7043832005-01-21 11:56:39 +000021978/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021979 * Delete a variable from hashtab "ht" at item "hi".
21980 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021983delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021984 hashtab_T *ht;
21985 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986{
Bram Moolenaar33570922005-01-25 22:26:29 +000021987 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021988
21989 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021990 clear_tv(&di->di_tv);
21991 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992}
21993
21994/*
21995 * List the value of one internal variable.
21996 */
21997 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021998list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021999 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022001 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022003 char_u *tofree;
22004 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022005 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022006
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022007 current_copyID += COPYID_INC;
22008 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000022009 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022010 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022011 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012}
22013
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022015list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016 char_u *prefix;
22017 char_u *name;
22018 int type;
22019 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022020 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021{
Bram Moolenaar31859182007-08-14 20:41:13 +000022022 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22023 msg_start();
22024 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 if (name != NULL) /* "a:" vars don't have a name stored */
22026 msg_puts(name);
22027 msg_putchar(' ');
22028 msg_advance(22);
22029 if (type == VAR_NUMBER)
22030 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022031 else if (type == VAR_FUNC)
22032 msg_putchar('*');
22033 else if (type == VAR_LIST)
22034 {
22035 msg_putchar('[');
22036 if (*string == '[')
22037 ++string;
22038 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022039 else if (type == VAR_DICT)
22040 {
22041 msg_putchar('{');
22042 if (*string == '{')
22043 ++string;
22044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045 else
22046 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022047
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022049
22050 if (type == VAR_FUNC)
22051 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022052 if (*first)
22053 {
22054 msg_clr_eos();
22055 *first = FALSE;
22056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057}
22058
22059/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022060 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 * If the variable already exists, the value is updated.
22062 * Otherwise the variable is created.
22063 */
22064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022065set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022067 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022068 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069{
Bram Moolenaar33570922005-01-25 22:26:29 +000022070 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022072 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022074 ht = find_var_ht(name, &varname);
22075 if (ht == NULL || *varname == NUL)
22076 {
22077 EMSG2(_(e_illvar), name);
22078 return;
22079 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022080 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022081
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022082 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22083 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022084
Bram Moolenaar33570922005-01-25 22:26:29 +000022085 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022087 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022088 if (var_check_ro(v->di_flags, name, FALSE)
22089 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022090 return;
22091 if (v->di_tv.v_type != tv->v_type
22092 && !((v->di_tv.v_type == VAR_STRING
22093 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022094 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022095 || tv->v_type == VAR_NUMBER))
22096#ifdef FEAT_FLOAT
22097 && !((v->di_tv.v_type == VAR_NUMBER
22098 || v->di_tv.v_type == VAR_FLOAT)
22099 && (tv->v_type == VAR_NUMBER
22100 || tv->v_type == VAR_FLOAT))
22101#endif
22102 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022103 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022104 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022105 return;
22106 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022107
22108 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022109 * Handle setting internal v: variables separately where needed to
22110 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022111 */
22112 if (ht == &vimvarht)
22113 {
22114 if (v->di_tv.v_type == VAR_STRING)
22115 {
22116 vim_free(v->di_tv.vval.v_string);
22117 if (copy || tv->v_type != VAR_STRING)
22118 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22119 else
22120 {
22121 /* Take over the string to avoid an extra alloc/free. */
22122 v->di_tv.vval.v_string = tv->vval.v_string;
22123 tv->vval.v_string = NULL;
22124 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022125 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022126 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022127 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022128 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022129 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022130 if (STRCMP(varname, "searchforward") == 0)
22131 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022132#ifdef FEAT_SEARCH_EXTRA
22133 else if (STRCMP(varname, "hlsearch") == 0)
22134 {
22135 no_hlsearch = !v->di_tv.vval.v_number;
22136 redraw_all_later(SOME_VALID);
22137 }
22138#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022139 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022140 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022141 else if (v->di_tv.v_type != tv->v_type)
22142 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022143 }
22144
22145 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 }
22147 else /* add a new variable */
22148 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022149 /* Can't add "v:" variable. */
22150 if (ht == &vimvarht)
22151 {
22152 EMSG2(_(e_illvar), name);
22153 return;
22154 }
22155
Bram Moolenaar92124a32005-06-17 22:03:40 +000022156 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022157 if (!valid_varname(varname))
22158 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022159
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022160 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22161 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022162 if (v == NULL)
22163 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022164 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022165 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022167 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 return;
22169 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022170 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022172
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022173 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022174 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022175 else
22176 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022177 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022178 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022179 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022181}
22182
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022183/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022184 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022185 * Also give an error message.
22186 */
22187 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022188var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022189 int flags;
22190 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022191 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022192{
22193 if (flags & DI_FLAGS_RO)
22194 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022195 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022196 return TRUE;
22197 }
22198 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22199 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022200 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022201 return TRUE;
22202 }
22203 return FALSE;
22204}
22205
22206/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022207 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22208 * Also give an error message.
22209 */
22210 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022211var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022212 int flags;
22213 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022214 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022215{
22216 if (flags & DI_FLAGS_FIX)
22217 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022218 EMSG2(_("E795: Cannot delete variable %s"),
22219 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022220 return TRUE;
22221 }
22222 return FALSE;
22223}
22224
22225/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022226 * Check if a funcref is assigned to a valid variable name.
22227 * Return TRUE and give an error if not.
22228 */
22229 static int
22230var_check_func_name(name, new_var)
22231 char_u *name; /* points to start of variable name */
22232 int new_var; /* TRUE when creating the variable */
22233{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022234 /* Allow for w: b: s: and t:. */
22235 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022236 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22237 ? name[2] : name[0]))
22238 {
22239 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22240 name);
22241 return TRUE;
22242 }
22243 /* Don't allow hiding a function. When "v" is not NULL we might be
22244 * assigning another function to the same var, the type is checked
22245 * below. */
22246 if (new_var && function_exists(name))
22247 {
22248 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22249 name);
22250 return TRUE;
22251 }
22252 return FALSE;
22253}
22254
22255/*
22256 * Check if a variable name is valid.
22257 * Return FALSE and give an error if not.
22258 */
22259 static int
22260valid_varname(varname)
22261 char_u *varname;
22262{
22263 char_u *p;
22264
22265 for (p = varname; *p != NUL; ++p)
22266 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22267 && *p != AUTOLOAD_CHAR)
22268 {
22269 EMSG2(_(e_illvar), varname);
22270 return FALSE;
22271 }
22272 return TRUE;
22273}
22274
22275/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022276 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022277 * Also give an error message, using "name" or _("name") when use_gettext is
22278 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022279 */
22280 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022281tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022282 int lock;
22283 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022284 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022285{
22286 if (lock & VAR_LOCKED)
22287 {
22288 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022289 name == NULL ? (char_u *)_("Unknown")
22290 : use_gettext ? (char_u *)_(name)
22291 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022292 return TRUE;
22293 }
22294 if (lock & VAR_FIXED)
22295 {
22296 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022297 name == NULL ? (char_u *)_("Unknown")
22298 : use_gettext ? (char_u *)_(name)
22299 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022300 return TRUE;
22301 }
22302 return FALSE;
22303}
22304
22305/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022306 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022307 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022308 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022309 * It is OK for "from" and "to" to point to the same item. This is used to
22310 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022311 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022312 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022313copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022314 typval_T *from;
22315 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022317 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022318 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022319 switch (from->v_type)
22320 {
22321 case VAR_NUMBER:
22322 to->vval.v_number = from->vval.v_number;
22323 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022324#ifdef FEAT_FLOAT
22325 case VAR_FLOAT:
22326 to->vval.v_float = from->vval.v_float;
22327 break;
22328#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022329 case VAR_STRING:
22330 case VAR_FUNC:
22331 if (from->vval.v_string == NULL)
22332 to->vval.v_string = NULL;
22333 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022334 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022335 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022336 if (from->v_type == VAR_FUNC)
22337 func_ref(to->vval.v_string);
22338 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022339 break;
22340 case VAR_LIST:
22341 if (from->vval.v_list == NULL)
22342 to->vval.v_list = NULL;
22343 else
22344 {
22345 to->vval.v_list = from->vval.v_list;
22346 ++to->vval.v_list->lv_refcount;
22347 }
22348 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022349 case VAR_DICT:
22350 if (from->vval.v_dict == NULL)
22351 to->vval.v_dict = NULL;
22352 else
22353 {
22354 to->vval.v_dict = from->vval.v_dict;
22355 ++to->vval.v_dict->dv_refcount;
22356 }
22357 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022358 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022359 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022360 break;
22361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362}
22363
22364/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022365 * Make a copy of an item.
22366 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022367 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22368 * reference to an already copied list/dict can be used.
22369 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022370 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022371 static int
22372item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022373 typval_T *from;
22374 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022375 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022376 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022377{
22378 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022379 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022380
Bram Moolenaar33570922005-01-25 22:26:29 +000022381 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022382 {
22383 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022384 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022385 }
22386 ++recurse;
22387
22388 switch (from->v_type)
22389 {
22390 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022391#ifdef FEAT_FLOAT
22392 case VAR_FLOAT:
22393#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022394 case VAR_STRING:
22395 case VAR_FUNC:
22396 copy_tv(from, to);
22397 break;
22398 case VAR_LIST:
22399 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022400 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022401 if (from->vval.v_list == NULL)
22402 to->vval.v_list = NULL;
22403 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22404 {
22405 /* use the copy made earlier */
22406 to->vval.v_list = from->vval.v_list->lv_copylist;
22407 ++to->vval.v_list->lv_refcount;
22408 }
22409 else
22410 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22411 if (to->vval.v_list == NULL)
22412 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022413 break;
22414 case VAR_DICT:
22415 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022416 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022417 if (from->vval.v_dict == NULL)
22418 to->vval.v_dict = NULL;
22419 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22420 {
22421 /* use the copy made earlier */
22422 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22423 ++to->vval.v_dict->dv_refcount;
22424 }
22425 else
22426 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22427 if (to->vval.v_dict == NULL)
22428 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022429 break;
22430 default:
22431 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022432 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022433 }
22434 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022435 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022436}
22437
22438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 * ":echo expr1 ..." print each argument separated with a space, add a
22440 * newline at the end.
22441 * ":echon expr1 ..." print each argument plain.
22442 */
22443 void
22444ex_echo(eap)
22445 exarg_T *eap;
22446{
22447 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022448 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022449 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 char_u *p;
22451 int needclr = TRUE;
22452 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022453 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454
22455 if (eap->skip)
22456 ++emsg_skip;
22457 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22458 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022459 /* If eval1() causes an error message the text from the command may
22460 * still need to be cleared. E.g., "echo 22,44". */
22461 need_clr_eos = needclr;
22462
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022464 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 {
22466 /*
22467 * Report the invalid expression unless the expression evaluation
22468 * has been cancelled due to an aborting error, an interrupt, or an
22469 * exception.
22470 */
22471 if (!aborting())
22472 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022473 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 break;
22475 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022476 need_clr_eos = FALSE;
22477
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478 if (!eap->skip)
22479 {
22480 if (atstart)
22481 {
22482 atstart = FALSE;
22483 /* Call msg_start() after eval1(), evaluating the expression
22484 * may cause a message to appear. */
22485 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022486 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022487 /* Mark the saved text as finishing the line, so that what
22488 * follows is displayed on a new line when scrolling back
22489 * at the more prompt. */
22490 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022491 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493 }
22494 else if (eap->cmdidx == CMD_echo)
22495 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022496 current_copyID += COPYID_INC;
22497 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022498 if (p != NULL)
22499 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022501 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022503 if (*p != TAB && needclr)
22504 {
22505 /* remove any text still there from the command */
22506 msg_clr_eos();
22507 needclr = FALSE;
22508 }
22509 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 }
22511 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022512 {
22513#ifdef FEAT_MBYTE
22514 if (has_mbyte)
22515 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022516 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022517
22518 (void)msg_outtrans_len_attr(p, i, echo_attr);
22519 p += i - 1;
22520 }
22521 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022523 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022526 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022528 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529 arg = skipwhite(arg);
22530 }
22531 eap->nextcmd = check_nextcmd(arg);
22532
22533 if (eap->skip)
22534 --emsg_skip;
22535 else
22536 {
22537 /* remove text that may still be there from the command */
22538 if (needclr)
22539 msg_clr_eos();
22540 if (eap->cmdidx == CMD_echo)
22541 msg_end();
22542 }
22543}
22544
22545/*
22546 * ":echohl {name}".
22547 */
22548 void
22549ex_echohl(eap)
22550 exarg_T *eap;
22551{
22552 int id;
22553
22554 id = syn_name2id(eap->arg);
22555 if (id == 0)
22556 echo_attr = 0;
22557 else
22558 echo_attr = syn_id2attr(id);
22559}
22560
22561/*
22562 * ":execute expr1 ..." execute the result of an expression.
22563 * ":echomsg expr1 ..." Print a message
22564 * ":echoerr expr1 ..." Print an error
22565 * Each gets spaces around each argument and a newline at the end for
22566 * echo commands
22567 */
22568 void
22569ex_execute(eap)
22570 exarg_T *eap;
22571{
22572 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022573 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574 int ret = OK;
22575 char_u *p;
22576 garray_T ga;
22577 int len;
22578 int save_did_emsg;
22579
22580 ga_init2(&ga, 1, 80);
22581
22582 if (eap->skip)
22583 ++emsg_skip;
22584 while (*arg != NUL && *arg != '|' && *arg != '\n')
22585 {
22586 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022587 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022588 {
22589 /*
22590 * Report the invalid expression unless the expression evaluation
22591 * has been cancelled due to an aborting error, an interrupt, or an
22592 * exception.
22593 */
22594 if (!aborting())
22595 EMSG2(_(e_invexpr2), p);
22596 ret = FAIL;
22597 break;
22598 }
22599
22600 if (!eap->skip)
22601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022602 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603 len = (int)STRLEN(p);
22604 if (ga_grow(&ga, len + 2) == FAIL)
22605 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022606 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607 ret = FAIL;
22608 break;
22609 }
22610 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022611 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 ga.ga_len += len;
22614 }
22615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022616 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617 arg = skipwhite(arg);
22618 }
22619
22620 if (ret != FAIL && ga.ga_data != NULL)
22621 {
22622 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022623 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022625 out_flush();
22626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 else if (eap->cmdidx == CMD_echoerr)
22628 {
22629 /* We don't want to abort following commands, restore did_emsg. */
22630 save_did_emsg = did_emsg;
22631 EMSG((char_u *)ga.ga_data);
22632 if (!force_abort)
22633 did_emsg = save_did_emsg;
22634 }
22635 else if (eap->cmdidx == CMD_execute)
22636 do_cmdline((char_u *)ga.ga_data,
22637 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22638 }
22639
22640 ga_clear(&ga);
22641
22642 if (eap->skip)
22643 --emsg_skip;
22644
22645 eap->nextcmd = check_nextcmd(arg);
22646}
22647
22648/*
22649 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22650 * "arg" points to the "&" or '+' when called, to "option" when returning.
22651 * Returns NULL when no option name found. Otherwise pointer to the char
22652 * after the option name.
22653 */
22654 static char_u *
22655find_option_end(arg, opt_flags)
22656 char_u **arg;
22657 int *opt_flags;
22658{
22659 char_u *p = *arg;
22660
22661 ++p;
22662 if (*p == 'g' && p[1] == ':')
22663 {
22664 *opt_flags = OPT_GLOBAL;
22665 p += 2;
22666 }
22667 else if (*p == 'l' && p[1] == ':')
22668 {
22669 *opt_flags = OPT_LOCAL;
22670 p += 2;
22671 }
22672 else
22673 *opt_flags = 0;
22674
22675 if (!ASCII_ISALPHA(*p))
22676 return NULL;
22677 *arg = p;
22678
22679 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22680 p += 4; /* termcap option */
22681 else
22682 while (ASCII_ISALPHA(*p))
22683 ++p;
22684 return p;
22685}
22686
22687/*
22688 * ":function"
22689 */
22690 void
22691ex_function(eap)
22692 exarg_T *eap;
22693{
22694 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022695 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696 int j;
22697 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022699 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700 char_u *name = NULL;
22701 char_u *p;
22702 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022703 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 garray_T newargs;
22705 garray_T newlines;
22706 int varargs = FALSE;
22707 int mustend = FALSE;
22708 int flags = 0;
22709 ufunc_T *fp;
22710 int indent;
22711 int nesting;
22712 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022713 dictitem_T *v;
22714 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022715 static int func_nr = 0; /* number for nameless function */
22716 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022717 hashtab_T *ht;
22718 int todo;
22719 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022720 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721
22722 /*
22723 * ":function" without argument: list functions.
22724 */
22725 if (ends_excmd(*eap->arg))
22726 {
22727 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022728 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022729 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022730 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022731 {
22732 if (!HASHITEM_EMPTY(hi))
22733 {
22734 --todo;
22735 fp = HI2UF(hi);
22736 if (!isdigit(*fp->uf_name))
22737 list_func_head(fp, FALSE);
22738 }
22739 }
22740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741 eap->nextcmd = check_nextcmd(eap->arg);
22742 return;
22743 }
22744
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022745 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022746 * ":function /pat": list functions matching pattern.
22747 */
22748 if (*eap->arg == '/')
22749 {
22750 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22751 if (!eap->skip)
22752 {
22753 regmatch_T regmatch;
22754
22755 c = *p;
22756 *p = NUL;
22757 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22758 *p = c;
22759 if (regmatch.regprog != NULL)
22760 {
22761 regmatch.rm_ic = p_ic;
22762
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022763 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022764 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22765 {
22766 if (!HASHITEM_EMPTY(hi))
22767 {
22768 --todo;
22769 fp = HI2UF(hi);
22770 if (!isdigit(*fp->uf_name)
22771 && vim_regexec(&regmatch, fp->uf_name, 0))
22772 list_func_head(fp, FALSE);
22773 }
22774 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022775 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022776 }
22777 }
22778 if (*p == '/')
22779 ++p;
22780 eap->nextcmd = check_nextcmd(p);
22781 return;
22782 }
22783
22784 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022785 * Get the function name. There are these situations:
22786 * func normal function name
22787 * "name" == func, "fudi.fd_dict" == NULL
22788 * dict.func new dictionary entry
22789 * "name" == NULL, "fudi.fd_dict" set,
22790 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22791 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022792 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022793 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22794 * dict.func existing dict entry that's not a Funcref
22795 * "name" == NULL, "fudi.fd_dict" set,
22796 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022797 * s:func script-local function name
22798 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022801 name = trans_function_name(&p, eap->skip, 0, &fudi);
22802 paren = (vim_strchr(p, '(') != NULL);
22803 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804 {
22805 /*
22806 * Return on an invalid expression in braces, unless the expression
22807 * evaluation has been cancelled due to an aborting error, an
22808 * interrupt, or an exception.
22809 */
22810 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022811 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022812 if (!eap->skip && fudi.fd_newkey != NULL)
22813 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022814 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817 else
22818 eap->skip = TRUE;
22819 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022820
Bram Moolenaar071d4272004-06-13 20:20:40 +000022821 /* An error in a function call during evaluation of an expression in magic
22822 * braces should not cause the function not to be defined. */
22823 saved_did_emsg = did_emsg;
22824 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825
22826 /*
22827 * ":function func" with only function name: list function.
22828 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022829 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830 {
22831 if (!ends_excmd(*skipwhite(p)))
22832 {
22833 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022834 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835 }
22836 eap->nextcmd = check_nextcmd(p);
22837 if (eap->nextcmd != NULL)
22838 *p = NUL;
22839 if (!eap->skip && !got_int)
22840 {
22841 fp = find_func(name);
22842 if (fp != NULL)
22843 {
22844 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022845 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022847 if (FUNCLINE(fp, j) == NULL)
22848 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 msg_putchar('\n');
22850 msg_outnum((long)(j + 1));
22851 if (j < 9)
22852 msg_putchar(' ');
22853 if (j < 99)
22854 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022855 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 out_flush(); /* show a line at a time */
22857 ui_breakcheck();
22858 }
22859 if (!got_int)
22860 {
22861 msg_putchar('\n');
22862 msg_puts((char_u *)" endfunction");
22863 }
22864 }
22865 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022866 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022868 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 }
22870
22871 /*
22872 * ":function name(arg1, arg2)" Define function.
22873 */
22874 p = skipwhite(p);
22875 if (*p != '(')
22876 {
22877 if (!eap->skip)
22878 {
22879 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022880 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881 }
22882 /* attempt to continue by skipping some text */
22883 if (vim_strchr(p, '(') != NULL)
22884 p = vim_strchr(p, '(');
22885 }
22886 p = skipwhite(p + 1);
22887
22888 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22889 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22890
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022891 if (!eap->skip)
22892 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022893 /* Check the name of the function. Unless it's a dictionary function
22894 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022895 if (name != NULL)
22896 arg = name;
22897 else
22898 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022899 if (arg != NULL && (fudi.fd_di == NULL
22900 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022901 {
22902 if (*arg == K_SPECIAL)
22903 j = 3;
22904 else
22905 j = 0;
22906 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22907 : eval_isnamec(arg[j])))
22908 ++j;
22909 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022910 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022911 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022912 /* Disallow using the g: dict. */
22913 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22914 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022915 }
22916
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917 /*
22918 * Isolate the arguments: "arg1, arg2, ...)"
22919 */
22920 while (*p != ')')
22921 {
22922 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22923 {
22924 varargs = TRUE;
22925 p += 3;
22926 mustend = TRUE;
22927 }
22928 else
22929 {
22930 arg = p;
22931 while (ASCII_ISALNUM(*p) || *p == '_')
22932 ++p;
22933 if (arg == p || isdigit(*arg)
22934 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22935 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22936 {
22937 if (!eap->skip)
22938 EMSG2(_("E125: Illegal argument: %s"), arg);
22939 break;
22940 }
22941 if (ga_grow(&newargs, 1) == FAIL)
22942 goto erret;
22943 c = *p;
22944 *p = NUL;
22945 arg = vim_strsave(arg);
22946 if (arg == NULL)
22947 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022948
22949 /* Check for duplicate argument name. */
22950 for (i = 0; i < newargs.ga_len; ++i)
22951 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22952 {
22953 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022954 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022955 goto erret;
22956 }
22957
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22959 *p = c;
22960 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 if (*p == ',')
22962 ++p;
22963 else
22964 mustend = TRUE;
22965 }
22966 p = skipwhite(p);
22967 if (mustend && *p != ')')
22968 {
22969 if (!eap->skip)
22970 EMSG2(_(e_invarg2), eap->arg);
22971 break;
22972 }
22973 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022974 if (*p != ')')
22975 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 ++p; /* skip the ')' */
22977
Bram Moolenaare9a41262005-01-15 22:18:47 +000022978 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 for (;;)
22980 {
22981 p = skipwhite(p);
22982 if (STRNCMP(p, "range", 5) == 0)
22983 {
22984 flags |= FC_RANGE;
22985 p += 5;
22986 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022987 else if (STRNCMP(p, "dict", 4) == 0)
22988 {
22989 flags |= FC_DICT;
22990 p += 4;
22991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992 else if (STRNCMP(p, "abort", 5) == 0)
22993 {
22994 flags |= FC_ABORT;
22995 p += 5;
22996 }
22997 else
22998 break;
22999 }
23000
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023001 /* When there is a line break use what follows for the function body.
23002 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23003 if (*p == '\n')
23004 line_arg = p + 1;
23005 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 EMSG(_(e_trailing));
23007
23008 /*
23009 * Read the body of the function, until ":endfunction" is found.
23010 */
23011 if (KeyTyped)
23012 {
23013 /* Check if the function already exists, don't let the user type the
23014 * whole function before telling him it doesn't work! For a script we
23015 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023016 if (!eap->skip && !eap->forceit)
23017 {
23018 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23019 EMSG(_(e_funcdict));
23020 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023021 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023024 if (!eap->skip && did_emsg)
23025 goto erret;
23026
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027 msg_putchar('\n'); /* don't overwrite the function name */
23028 cmdline_row = msg_row;
23029 }
23030
23031 indent = 2;
23032 nesting = 0;
23033 for (;;)
23034 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023035 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023036 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023037 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023038 saved_wait_return = FALSE;
23039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023041 sourcing_lnum_off = sourcing_lnum;
23042
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023043 if (line_arg != NULL)
23044 {
23045 /* Use eap->arg, split up in parts by line breaks. */
23046 theline = line_arg;
23047 p = vim_strchr(theline, '\n');
23048 if (p == NULL)
23049 line_arg += STRLEN(line_arg);
23050 else
23051 {
23052 *p = NUL;
23053 line_arg = p + 1;
23054 }
23055 }
23056 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 theline = getcmdline(':', 0L, indent);
23058 else
23059 theline = eap->getline(':', eap->cookie, indent);
23060 if (KeyTyped)
23061 lines_left = Rows - 1;
23062 if (theline == NULL)
23063 {
23064 EMSG(_("E126: Missing :endfunction"));
23065 goto erret;
23066 }
23067
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023068 /* Detect line continuation: sourcing_lnum increased more than one. */
23069 if (sourcing_lnum > sourcing_lnum_off + 1)
23070 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23071 else
23072 sourcing_lnum_off = 0;
23073
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074 if (skip_until != NULL)
23075 {
23076 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23077 * don't check for ":endfunc". */
23078 if (STRCMP(theline, skip_until) == 0)
23079 {
23080 vim_free(skip_until);
23081 skip_until = NULL;
23082 }
23083 }
23084 else
23085 {
23086 /* skip ':' and blanks*/
23087 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23088 ;
23089
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023090 /* Check for "endfunction". */
23091 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023092 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023093 if (line_arg == NULL)
23094 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023095 break;
23096 }
23097
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023098 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 * at "end". */
23100 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23101 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023102 else if (STRNCMP(p, "if", 2) == 0
23103 || STRNCMP(p, "wh", 2) == 0
23104 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023105 || STRNCMP(p, "try", 3) == 0)
23106 indent += 2;
23107
23108 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023109 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023111 if (*p == '!')
23112 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023114 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23115 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023117 ++nesting;
23118 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023119 }
23120 }
23121
23122 /* Check for ":append" or ":insert". */
23123 p = skip_range(p, NULL);
23124 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23125 || (p[0] == 'i'
23126 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23127 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23128 skip_until = vim_strsave((char_u *)".");
23129
23130 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23131 arg = skipwhite(skiptowhite(p));
23132 if (arg[0] == '<' && arg[1] =='<'
23133 && ((p[0] == 'p' && p[1] == 'y'
23134 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23135 || (p[0] == 'p' && p[1] == 'e'
23136 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23137 || (p[0] == 't' && p[1] == 'c'
23138 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023139 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23140 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23142 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023143 || (p[0] == 'm' && p[1] == 'z'
23144 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 ))
23146 {
23147 /* ":python <<" continues until a dot, like ":append" */
23148 p = skipwhite(arg + 2);
23149 if (*p == NUL)
23150 skip_until = vim_strsave((char_u *)".");
23151 else
23152 skip_until = vim_strsave(p);
23153 }
23154 }
23155
23156 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023157 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023158 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023159 if (line_arg == NULL)
23160 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023162 }
23163
23164 /* Copy the line to newly allocated memory. get_one_sourceline()
23165 * allocates 250 bytes per line, this saves 80% on average. The cost
23166 * is an extra alloc/free. */
23167 p = vim_strsave(theline);
23168 if (p != NULL)
23169 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023170 if (line_arg == NULL)
23171 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023172 theline = p;
23173 }
23174
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023175 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23176
23177 /* Add NULL lines for continuation lines, so that the line count is
23178 * equal to the index in the growarray. */
23179 while (sourcing_lnum_off-- > 0)
23180 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023181
23182 /* Check for end of eap->arg. */
23183 if (line_arg != NULL && *line_arg == NUL)
23184 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 }
23186
23187 /* Don't define the function when skipping commands or when an error was
23188 * detected. */
23189 if (eap->skip || did_emsg)
23190 goto erret;
23191
23192 /*
23193 * If there are no errors, add the function
23194 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023195 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023196 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023197 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023198 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023199 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023200 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023201 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023202 goto erret;
23203 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023204
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023205 fp = find_func(name);
23206 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023208 if (!eap->forceit)
23209 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023210 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023211 goto erret;
23212 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023213 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023214 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023215 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023216 name);
23217 goto erret;
23218 }
23219 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023220 ga_clear_strings(&(fp->uf_args));
23221 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023222 vim_free(name);
23223 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023225 }
23226 else
23227 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023228 char numbuf[20];
23229
23230 fp = NULL;
23231 if (fudi.fd_newkey == NULL && !eap->forceit)
23232 {
23233 EMSG(_(e_funcdict));
23234 goto erret;
23235 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023236 if (fudi.fd_di == NULL)
23237 {
23238 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023239 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023240 goto erret;
23241 }
23242 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023243 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023244 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023245
23246 /* Give the function a sequential number. Can only be used with a
23247 * Funcref! */
23248 vim_free(name);
23249 sprintf(numbuf, "%d", ++func_nr);
23250 name = vim_strsave((char_u *)numbuf);
23251 if (name == NULL)
23252 goto erret;
23253 }
23254
23255 if (fp == NULL)
23256 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023257 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023258 {
23259 int slen, plen;
23260 char_u *scriptname;
23261
23262 /* Check that the autoload name matches the script name. */
23263 j = FAIL;
23264 if (sourcing_name != NULL)
23265 {
23266 scriptname = autoload_name(name);
23267 if (scriptname != NULL)
23268 {
23269 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023270 plen = (int)STRLEN(p);
23271 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023272 if (slen > plen && fnamecmp(p,
23273 sourcing_name + slen - plen) == 0)
23274 j = OK;
23275 vim_free(scriptname);
23276 }
23277 }
23278 if (j == FAIL)
23279 {
23280 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23281 goto erret;
23282 }
23283 }
23284
23285 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023286 if (fp == NULL)
23287 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023288
23289 if (fudi.fd_dict != NULL)
23290 {
23291 if (fudi.fd_di == NULL)
23292 {
23293 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023294 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023295 if (fudi.fd_di == NULL)
23296 {
23297 vim_free(fp);
23298 goto erret;
23299 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023300 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23301 {
23302 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023303 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023304 goto erret;
23305 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023306 }
23307 else
23308 /* overwrite existing dict entry */
23309 clear_tv(&fudi.fd_di->di_tv);
23310 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023311 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023312 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023313 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023314
23315 /* behave like "dict" was used */
23316 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023317 }
23318
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023320 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023321 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23322 {
23323 vim_free(fp);
23324 goto erret;
23325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023327 fp->uf_args = newargs;
23328 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023329#ifdef FEAT_PROFILE
23330 fp->uf_tml_count = NULL;
23331 fp->uf_tml_total = NULL;
23332 fp->uf_tml_self = NULL;
23333 fp->uf_profiling = FALSE;
23334 if (prof_def_func())
23335 func_do_profile(fp);
23336#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023337 fp->uf_varargs = varargs;
23338 fp->uf_flags = flags;
23339 fp->uf_calls = 0;
23340 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023341 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023342
23343erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023344 ga_clear_strings(&newargs);
23345 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023346ret_free:
23347 vim_free(skip_until);
23348 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023349 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023351 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352}
23353
23354/*
23355 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023356 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023357 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023358 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023359 * TFN_INT: internal function name OK
23360 * TFN_QUIET: be quiet
23361 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362 * Advances "pp" to just after the function name (if no error).
23363 */
23364 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023365trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 char_u **pp;
23367 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023368 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023369 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023371 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023372 char_u *start;
23373 char_u *end;
23374 int lead;
23375 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023377 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023378
23379 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023380 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023381 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023382
23383 /* Check for hard coded <SNR>: already translated function ID (from a user
23384 * command). */
23385 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23386 && (*pp)[2] == (int)KE_SNR)
23387 {
23388 *pp += 3;
23389 len = get_id_len(pp) + 3;
23390 return vim_strnsave(start, len);
23391 }
23392
23393 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23394 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023396 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023397 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023398
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023399 /* Note that TFN_ flags use the same values as GLV_ flags. */
23400 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023401 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023402 if (end == start)
23403 {
23404 if (!skip)
23405 EMSG(_("E129: Function name required"));
23406 goto theend;
23407 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023408 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023409 {
23410 /*
23411 * Report an invalid expression in braces, unless the expression
23412 * evaluation has been cancelled due to an aborting error, an
23413 * interrupt, or an exception.
23414 */
23415 if (!aborting())
23416 {
23417 if (end != NULL)
23418 EMSG2(_(e_invarg2), start);
23419 }
23420 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023421 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023422 goto theend;
23423 }
23424
23425 if (lv.ll_tv != NULL)
23426 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023427 if (fdp != NULL)
23428 {
23429 fdp->fd_dict = lv.ll_dict;
23430 fdp->fd_newkey = lv.ll_newkey;
23431 lv.ll_newkey = NULL;
23432 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023433 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023434 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23435 {
23436 name = vim_strsave(lv.ll_tv->vval.v_string);
23437 *pp = end;
23438 }
23439 else
23440 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023441 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23442 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023443 EMSG(_(e_funcref));
23444 else
23445 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023446 name = NULL;
23447 }
23448 goto theend;
23449 }
23450
23451 if (lv.ll_name == NULL)
23452 {
23453 /* Error found, but continue after the function name. */
23454 *pp = end;
23455 goto theend;
23456 }
23457
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023458 /* Check if the name is a Funcref. If so, use the value. */
23459 if (lv.ll_exp_name != NULL)
23460 {
23461 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023462 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023463 if (name == lv.ll_exp_name)
23464 name = NULL;
23465 }
23466 else
23467 {
23468 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023469 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023470 if (name == *pp)
23471 name = NULL;
23472 }
23473 if (name != NULL)
23474 {
23475 name = vim_strsave(name);
23476 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023477 if (STRNCMP(name, "<SNR>", 5) == 0)
23478 {
23479 /* Change "<SNR>" to the byte sequence. */
23480 name[0] = K_SPECIAL;
23481 name[1] = KS_EXTRA;
23482 name[2] = (int)KE_SNR;
23483 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23484 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023485 goto theend;
23486 }
23487
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023488 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023489 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023490 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023491 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23492 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23493 {
23494 /* When there was "s:" already or the name expanded to get a
23495 * leading "s:" then remove it. */
23496 lv.ll_name += 2;
23497 len -= 2;
23498 lead = 2;
23499 }
23500 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023501 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023502 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023503 /* skip over "s:" and "g:" */
23504 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023505 lv.ll_name += 2;
23506 len = (int)(end - lv.ll_name);
23507 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023508
23509 /*
23510 * Copy the function name to allocated memory.
23511 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23512 * Accept <SNR>123_name() outside a script.
23513 */
23514 if (skip)
23515 lead = 0; /* do nothing */
23516 else if (lead > 0)
23517 {
23518 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023519 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23520 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023521 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023522 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023523 if (current_SID <= 0)
23524 {
23525 EMSG(_(e_usingsid));
23526 goto theend;
23527 }
23528 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23529 lead += (int)STRLEN(sid_buf);
23530 }
23531 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023532 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023533 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023534 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023535 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023536 goto theend;
23537 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023538 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023539 {
23540 char_u *cp = vim_strchr(lv.ll_name, ':');
23541
23542 if (cp != NULL && cp < end)
23543 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023544 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023545 goto theend;
23546 }
23547 }
23548
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023549 name = alloc((unsigned)(len + lead + 1));
23550 if (name != NULL)
23551 {
23552 if (lead > 0)
23553 {
23554 name[0] = K_SPECIAL;
23555 name[1] = KS_EXTRA;
23556 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023557 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023558 STRCPY(name + 3, sid_buf);
23559 }
23560 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023561 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023562 }
23563 *pp = end;
23564
23565theend:
23566 clear_lval(&lv);
23567 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568}
23569
23570/*
23571 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23572 * Return 2 if "p" starts with "s:".
23573 * Return 0 otherwise.
23574 */
23575 static int
23576eval_fname_script(p)
23577 char_u *p;
23578{
23579 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23580 || STRNICMP(p + 1, "SNR>", 4) == 0))
23581 return 5;
23582 if (p[0] == 's' && p[1] == ':')
23583 return 2;
23584 return 0;
23585}
23586
23587/*
23588 * Return TRUE if "p" starts with "<SID>" or "s:".
23589 * Only works if eval_fname_script() returned non-zero for "p"!
23590 */
23591 static int
23592eval_fname_sid(p)
23593 char_u *p;
23594{
23595 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23596}
23597
23598/*
23599 * List the head of the function: "name(arg1, arg2)".
23600 */
23601 static void
23602list_func_head(fp, indent)
23603 ufunc_T *fp;
23604 int indent;
23605{
23606 int j;
23607
23608 msg_start();
23609 if (indent)
23610 MSG_PUTS(" ");
23611 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023612 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613 {
23614 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023615 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616 }
23617 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023618 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023619 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023620 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621 {
23622 if (j)
23623 MSG_PUTS(", ");
23624 msg_puts(FUNCARG(fp, j));
23625 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023626 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627 {
23628 if (j)
23629 MSG_PUTS(", ");
23630 MSG_PUTS("...");
23631 }
23632 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023633 if (fp->uf_flags & FC_ABORT)
23634 MSG_PUTS(" abort");
23635 if (fp->uf_flags & FC_RANGE)
23636 MSG_PUTS(" range");
23637 if (fp->uf_flags & FC_DICT)
23638 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023639 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023640 if (p_verbose > 0)
23641 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642}
23643
23644/*
23645 * Find a function by name, return pointer to it in ufuncs.
23646 * Return NULL for unknown function.
23647 */
23648 static ufunc_T *
23649find_func(name)
23650 char_u *name;
23651{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023652 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023654 hi = hash_find(&func_hashtab, name);
23655 if (!HASHITEM_EMPTY(hi))
23656 return HI2UF(hi);
23657 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658}
23659
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023660#if defined(EXITFREE) || defined(PROTO)
23661 void
23662free_all_functions()
23663{
23664 hashitem_T *hi;
23665
23666 /* Need to start all over every time, because func_free() may change the
23667 * hash table. */
23668 while (func_hashtab.ht_used > 0)
23669 for (hi = func_hashtab.ht_array; ; ++hi)
23670 if (!HASHITEM_EMPTY(hi))
23671 {
23672 func_free(HI2UF(hi));
23673 break;
23674 }
23675}
23676#endif
23677
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023678 int
23679translated_function_exists(name)
23680 char_u *name;
23681{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023682 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023683 return find_internal_func(name) >= 0;
23684 return find_func(name) != NULL;
23685}
23686
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023687/*
23688 * Return TRUE if a function "name" exists.
23689 */
23690 static int
23691function_exists(name)
23692 char_u *name;
23693{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023694 char_u *nm = name;
23695 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023696 int n = FALSE;
23697
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023698 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23699 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023700 nm = skipwhite(nm);
23701
23702 /* Only accept "funcname", "funcname ", "funcname (..." and
23703 * "funcname(...", not "funcname!...". */
23704 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023705 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023706 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023707 return n;
23708}
23709
Bram Moolenaara1544c02013-05-30 12:35:52 +020023710 char_u *
23711get_expanded_name(name, check)
23712 char_u *name;
23713 int check;
23714{
23715 char_u *nm = name;
23716 char_u *p;
23717
23718 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23719
23720 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023721 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023722 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023723
Bram Moolenaara1544c02013-05-30 12:35:52 +020023724 vim_free(p);
23725 return NULL;
23726}
23727
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023728/*
23729 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023730 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23731 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023732 */
23733 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023734builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023735 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023736 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023737{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023738 char_u *p;
23739
23740 if (!ASCII_ISLOWER(name[0]))
23741 return FALSE;
23742 p = vim_strchr(name, AUTOLOAD_CHAR);
23743 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023744}
23745
Bram Moolenaar05159a02005-02-26 23:04:13 +000023746#if defined(FEAT_PROFILE) || defined(PROTO)
23747/*
23748 * Start profiling function "fp".
23749 */
23750 static void
23751func_do_profile(fp)
23752 ufunc_T *fp;
23753{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023754 int len = fp->uf_lines.ga_len;
23755
23756 if (len == 0)
23757 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023758 fp->uf_tm_count = 0;
23759 profile_zero(&fp->uf_tm_self);
23760 profile_zero(&fp->uf_tm_total);
23761 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023762 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023763 if (fp->uf_tml_total == NULL)
23764 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023765 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023766 if (fp->uf_tml_self == NULL)
23767 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023768 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023769 fp->uf_tml_idx = -1;
23770 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23771 || fp->uf_tml_self == NULL)
23772 return; /* out of memory */
23773
23774 fp->uf_profiling = TRUE;
23775}
23776
23777/*
23778 * Dump the profiling results for all functions in file "fd".
23779 */
23780 void
23781func_dump_profile(fd)
23782 FILE *fd;
23783{
23784 hashitem_T *hi;
23785 int todo;
23786 ufunc_T *fp;
23787 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023788 ufunc_T **sorttab;
23789 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023790
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023791 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023792 if (todo == 0)
23793 return; /* nothing to dump */
23794
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023795 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023796
Bram Moolenaar05159a02005-02-26 23:04:13 +000023797 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23798 {
23799 if (!HASHITEM_EMPTY(hi))
23800 {
23801 --todo;
23802 fp = HI2UF(hi);
23803 if (fp->uf_profiling)
23804 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023805 if (sorttab != NULL)
23806 sorttab[st_len++] = fp;
23807
Bram Moolenaar05159a02005-02-26 23:04:13 +000023808 if (fp->uf_name[0] == K_SPECIAL)
23809 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23810 else
23811 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23812 if (fp->uf_tm_count == 1)
23813 fprintf(fd, "Called 1 time\n");
23814 else
23815 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23816 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23817 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23818 fprintf(fd, "\n");
23819 fprintf(fd, "count total (s) self (s)\n");
23820
23821 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23822 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023823 if (FUNCLINE(fp, i) == NULL)
23824 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023825 prof_func_line(fd, fp->uf_tml_count[i],
23826 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023827 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23828 }
23829 fprintf(fd, "\n");
23830 }
23831 }
23832 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023833
23834 if (sorttab != NULL && st_len > 0)
23835 {
23836 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23837 prof_total_cmp);
23838 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23839 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23840 prof_self_cmp);
23841 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23842 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023843
23844 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023845}
Bram Moolenaar73830342005-02-28 22:48:19 +000023846
23847 static void
23848prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23849 FILE *fd;
23850 ufunc_T **sorttab;
23851 int st_len;
23852 char *title;
23853 int prefer_self; /* when equal print only self time */
23854{
23855 int i;
23856 ufunc_T *fp;
23857
23858 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23859 fprintf(fd, "count total (s) self (s) function\n");
23860 for (i = 0; i < 20 && i < st_len; ++i)
23861 {
23862 fp = sorttab[i];
23863 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23864 prefer_self);
23865 if (fp->uf_name[0] == K_SPECIAL)
23866 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23867 else
23868 fprintf(fd, " %s()\n", fp->uf_name);
23869 }
23870 fprintf(fd, "\n");
23871}
23872
23873/*
23874 * Print the count and times for one function or function line.
23875 */
23876 static void
23877prof_func_line(fd, count, total, self, prefer_self)
23878 FILE *fd;
23879 int count;
23880 proftime_T *total;
23881 proftime_T *self;
23882 int prefer_self; /* when equal print only self time */
23883{
23884 if (count > 0)
23885 {
23886 fprintf(fd, "%5d ", count);
23887 if (prefer_self && profile_equal(total, self))
23888 fprintf(fd, " ");
23889 else
23890 fprintf(fd, "%s ", profile_msg(total));
23891 if (!prefer_self && profile_equal(total, self))
23892 fprintf(fd, " ");
23893 else
23894 fprintf(fd, "%s ", profile_msg(self));
23895 }
23896 else
23897 fprintf(fd, " ");
23898}
23899
23900/*
23901 * Compare function for total time sorting.
23902 */
23903 static int
23904#ifdef __BORLANDC__
23905_RTLENTRYF
23906#endif
23907prof_total_cmp(s1, s2)
23908 const void *s1;
23909 const void *s2;
23910{
23911 ufunc_T *p1, *p2;
23912
23913 p1 = *(ufunc_T **)s1;
23914 p2 = *(ufunc_T **)s2;
23915 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23916}
23917
23918/*
23919 * Compare function for self time sorting.
23920 */
23921 static int
23922#ifdef __BORLANDC__
23923_RTLENTRYF
23924#endif
23925prof_self_cmp(s1, s2)
23926 const void *s1;
23927 const void *s2;
23928{
23929 ufunc_T *p1, *p2;
23930
23931 p1 = *(ufunc_T **)s1;
23932 p2 = *(ufunc_T **)s2;
23933 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23934}
23935
Bram Moolenaar05159a02005-02-26 23:04:13 +000023936#endif
23937
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023938/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023939 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023940 * Return TRUE if a package was loaded.
23941 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023942 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023943script_autoload(name, reload)
23944 char_u *name;
23945 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023946{
23947 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023948 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023949 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023950 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023951
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023952 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023953 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023954 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023955 return FALSE;
23956
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023957 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023958
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023959 /* Find the name in the list of previously loaded package names. Skip
23960 * "autoload/", it's always the same. */
23961 for (i = 0; i < ga_loaded.ga_len; ++i)
23962 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23963 break;
23964 if (!reload && i < ga_loaded.ga_len)
23965 ret = FALSE; /* was loaded already */
23966 else
23967 {
23968 /* Remember the name if it wasn't loaded already. */
23969 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23970 {
23971 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23972 tofree = NULL;
23973 }
23974
23975 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023976 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023977 ret = TRUE;
23978 }
23979
23980 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023981 return ret;
23982}
23983
23984/*
23985 * Return the autoload script name for a function or variable name.
23986 * Returns NULL when out of memory.
23987 */
23988 static char_u *
23989autoload_name(name)
23990 char_u *name;
23991{
23992 char_u *p;
23993 char_u *scriptname;
23994
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023995 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023996 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23997 if (scriptname == NULL)
23998 return FALSE;
23999 STRCPY(scriptname, "autoload/");
24000 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024001 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024002 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024003 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024004 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024005 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024006}
24007
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24009
24010/*
24011 * Function given to ExpandGeneric() to obtain the list of user defined
24012 * function names.
24013 */
24014 char_u *
24015get_user_func_name(xp, idx)
24016 expand_T *xp;
24017 int idx;
24018{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024019 static long_u done;
24020 static hashitem_T *hi;
24021 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024022
24023 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024024 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024025 done = 0;
24026 hi = func_hashtab.ht_array;
24027 }
24028 if (done < func_hashtab.ht_used)
24029 {
24030 if (done++ > 0)
24031 ++hi;
24032 while (HASHITEM_EMPTY(hi))
24033 ++hi;
24034 fp = HI2UF(hi);
24035
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024036 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024037 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024038
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024039 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24040 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024041
24042 cat_func_name(IObuff, fp);
24043 if (xp->xp_context != EXPAND_USER_FUNC)
24044 {
24045 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024046 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024047 STRCAT(IObuff, ")");
24048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049 return IObuff;
24050 }
24051 return NULL;
24052}
24053
24054#endif /* FEAT_CMDL_COMPL */
24055
24056/*
24057 * Copy the function name of "fp" to buffer "buf".
24058 * "buf" must be able to hold the function name plus three bytes.
24059 * Takes care of script-local function names.
24060 */
24061 static void
24062cat_func_name(buf, fp)
24063 char_u *buf;
24064 ufunc_T *fp;
24065{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024066 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024067 {
24068 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024069 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024070 }
24071 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024072 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073}
24074
24075/*
24076 * ":delfunction {name}"
24077 */
24078 void
24079ex_delfunction(eap)
24080 exarg_T *eap;
24081{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024082 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024083 char_u *p;
24084 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024085 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024086
24087 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024088 name = trans_function_name(&p, eap->skip, 0, &fudi);
24089 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024091 {
24092 if (fudi.fd_dict != NULL && !eap->skip)
24093 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024096 if (!ends_excmd(*skipwhite(p)))
24097 {
24098 vim_free(name);
24099 EMSG(_(e_trailing));
24100 return;
24101 }
24102 eap->nextcmd = check_nextcmd(p);
24103 if (eap->nextcmd != NULL)
24104 *p = NUL;
24105
24106 if (!eap->skip)
24107 fp = find_func(name);
24108 vim_free(name);
24109
24110 if (!eap->skip)
24111 {
24112 if (fp == NULL)
24113 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024114 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115 return;
24116 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024117 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 {
24119 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24120 return;
24121 }
24122
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024123 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024125 /* Delete the dict item that refers to the function, it will
24126 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024127 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024128 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024129 else
24130 func_free(fp);
24131 }
24132}
24133
24134/*
24135 * Free a function and remove it from the list of functions.
24136 */
24137 static void
24138func_free(fp)
24139 ufunc_T *fp;
24140{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024141 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024142
24143 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024144 ga_clear_strings(&(fp->uf_args));
24145 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024146#ifdef FEAT_PROFILE
24147 vim_free(fp->uf_tml_count);
24148 vim_free(fp->uf_tml_total);
24149 vim_free(fp->uf_tml_self);
24150#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024151
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024152 /* remove the function from the function hashtable */
24153 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24154 if (HASHITEM_EMPTY(hi))
24155 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024156 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024157 hash_remove(&func_hashtab, hi);
24158
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024159 vim_free(fp);
24160}
24161
24162/*
24163 * Unreference a Function: decrement the reference count and free it when it
24164 * becomes zero. Only for numbered functions.
24165 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024166 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024167func_unref(name)
24168 char_u *name;
24169{
24170 ufunc_T *fp;
24171
24172 if (name != NULL && isdigit(*name))
24173 {
24174 fp = find_func(name);
24175 if (fp == NULL)
24176 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024177 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024178 {
24179 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024180 * when "uf_calls" becomes zero. */
24181 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024182 func_free(fp);
24183 }
24184 }
24185}
24186
24187/*
24188 * Count a reference to a Function.
24189 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024190 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024191func_ref(name)
24192 char_u *name;
24193{
24194 ufunc_T *fp;
24195
24196 if (name != NULL && isdigit(*name))
24197 {
24198 fp = find_func(name);
24199 if (fp == NULL)
24200 EMSG2(_(e_intern2), "func_ref()");
24201 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024202 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203 }
24204}
24205
24206/*
24207 * Call a user function.
24208 */
24209 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024210call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024211 ufunc_T *fp; /* pointer to function */
24212 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024213 typval_T *argvars; /* arguments */
24214 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024215 linenr_T firstline; /* first line of range */
24216 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024217 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218{
Bram Moolenaar33570922005-01-25 22:26:29 +000024219 char_u *save_sourcing_name;
24220 linenr_T save_sourcing_lnum;
24221 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024222 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024223 int save_did_emsg;
24224 static int depth = 0;
24225 dictitem_T *v;
24226 int fixvar_idx = 0; /* index in fixvar[] */
24227 int i;
24228 int ai;
24229 char_u numbuf[NUMBUFLEN];
24230 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024231 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024232#ifdef FEAT_PROFILE
24233 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024234 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236
24237 /* If depth of calling is getting too high, don't execute the function */
24238 if (depth >= p_mfd)
24239 {
24240 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024241 rettv->v_type = VAR_NUMBER;
24242 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024243 return;
24244 }
24245 ++depth;
24246
24247 line_breakcheck(); /* check for CTRL-C hit */
24248
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024249 fc = (funccall_T *)alloc(sizeof(funccall_T));
24250 fc->caller = current_funccal;
24251 current_funccal = fc;
24252 fc->func = fp;
24253 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024254 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024255 fc->linenr = 0;
24256 fc->returned = FALSE;
24257 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024258 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024259 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24260 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024261
Bram Moolenaar33570922005-01-25 22:26:29 +000024262 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024263 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024264 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24265 * each argument variable and saves a lot of time.
24266 */
24267 /*
24268 * Init l: variables.
24269 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024270 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024271 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024272 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024273 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24274 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024275 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024276 name = v->di_key;
24277 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024278 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024279 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024280 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024281 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024282 v->di_tv.vval.v_dict = selfdict;
24283 ++selfdict->dv_refcount;
24284 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024285
Bram Moolenaar33570922005-01-25 22:26:29 +000024286 /*
24287 * Init a: variables.
24288 * Set a:0 to "argcount".
24289 * Set a:000 to a list with room for the "..." arguments.
24290 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024291 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024292 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024293 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024294 /* Use "name" to avoid a warning from some compiler that checks the
24295 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024296 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024297 name = v->di_key;
24298 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024299 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024300 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024301 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024302 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024303 v->di_tv.vval.v_list = &fc->l_varlist;
24304 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24305 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24306 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024307
24308 /*
24309 * Set a:firstline to "firstline" and a:lastline to "lastline".
24310 * Set a:name to named arguments.
24311 * Set a:N to the "..." arguments.
24312 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024313 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024314 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024315 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024316 (varnumber_T)lastline);
24317 for (i = 0; i < argcount; ++i)
24318 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024319 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024320 if (ai < 0)
24321 /* named argument a:name */
24322 name = FUNCARG(fp, i);
24323 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024324 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024325 /* "..." argument a:1, a:2, etc. */
24326 sprintf((char *)numbuf, "%d", ai + 1);
24327 name = numbuf;
24328 }
24329 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24330 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024331 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024332 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24333 }
24334 else
24335 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024336 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24337 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024338 if (v == NULL)
24339 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024340 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024341 }
24342 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024343 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024344
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024345 /* Note: the values are copied directly to avoid alloc/free.
24346 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024347 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024348 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024349
24350 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24351 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024352 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24353 fc->l_listitems[ai].li_tv = argvars[i];
24354 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024355 }
24356 }
24357
Bram Moolenaar071d4272004-06-13 20:20:40 +000024358 /* Don't redraw while executing the function. */
24359 ++RedrawingDisabled;
24360 save_sourcing_name = sourcing_name;
24361 save_sourcing_lnum = sourcing_lnum;
24362 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024363 /* need space for function name + ("function " + 3) or "[number]" */
24364 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24365 + STRLEN(fp->uf_name) + 20;
24366 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024367 if (sourcing_name != NULL)
24368 {
24369 if (save_sourcing_name != NULL
24370 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024371 sprintf((char *)sourcing_name, "%s[%d]..",
24372 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373 else
24374 STRCPY(sourcing_name, "function ");
24375 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24376
24377 if (p_verbose >= 12)
24378 {
24379 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024380 verbose_enter_scroll();
24381
Bram Moolenaar555b2802005-05-19 21:08:39 +000024382 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 if (p_verbose >= 14)
24384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024385 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024386 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024387 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024388 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024389
24390 msg_puts((char_u *)"(");
24391 for (i = 0; i < argcount; ++i)
24392 {
24393 if (i > 0)
24394 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024395 if (argvars[i].v_type == VAR_NUMBER)
24396 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024397 else
24398 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024399 /* Do not want errors such as E724 here. */
24400 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024401 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024402 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024403 if (s != NULL)
24404 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024405 if (vim_strsize(s) > MSG_BUF_CLEN)
24406 {
24407 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24408 s = buf;
24409 }
24410 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024411 vim_free(tofree);
24412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024413 }
24414 }
24415 msg_puts((char_u *)")");
24416 }
24417 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024418
24419 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024420 --no_wait_return;
24421 }
24422 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024423#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024424 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024425 {
24426 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24427 func_do_profile(fp);
24428 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024429 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024430 {
24431 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024432 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024433 profile_zero(&fp->uf_tm_children);
24434 }
24435 script_prof_save(&wait_start);
24436 }
24437#endif
24438
Bram Moolenaar071d4272004-06-13 20:20:40 +000024439 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024440 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024441 save_did_emsg = did_emsg;
24442 did_emsg = FALSE;
24443
24444 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024445 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24447
24448 --RedrawingDisabled;
24449
24450 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024451 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024452 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024453 clear_tv(rettv);
24454 rettv->v_type = VAR_NUMBER;
24455 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024456 }
24457
Bram Moolenaar05159a02005-02-26 23:04:13 +000024458#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024459 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024460 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024461 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024462 profile_end(&call_start);
24463 profile_sub_wait(&wait_start, &call_start);
24464 profile_add(&fp->uf_tm_total, &call_start);
24465 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024466 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024467 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024468 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24469 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024470 }
24471 }
24472#endif
24473
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474 /* when being verbose, mention the return value */
24475 if (p_verbose >= 12)
24476 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024477 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024478 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024479
Bram Moolenaar071d4272004-06-13 20:20:40 +000024480 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024481 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024482 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024483 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024484 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024485 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024486 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024487 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024488 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024489 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024490 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024491
Bram Moolenaar555b2802005-05-19 21:08:39 +000024492 /* The value may be very long. Skip the middle part, so that we
24493 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024494 * truncate it at the end. Don't want errors such as E724 here. */
24495 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024496 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024497 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024498 if (s != NULL)
24499 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024500 if (vim_strsize(s) > MSG_BUF_CLEN)
24501 {
24502 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24503 s = buf;
24504 }
24505 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024506 vim_free(tofree);
24507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024508 }
24509 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024510
24511 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512 --no_wait_return;
24513 }
24514
24515 vim_free(sourcing_name);
24516 sourcing_name = save_sourcing_name;
24517 sourcing_lnum = save_sourcing_lnum;
24518 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024519#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024520 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024521 script_prof_restore(&wait_start);
24522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024523
24524 if (p_verbose >= 12 && sourcing_name != NULL)
24525 {
24526 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024527 verbose_enter_scroll();
24528
Bram Moolenaar555b2802005-05-19 21:08:39 +000024529 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024530 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024531
24532 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024533 --no_wait_return;
24534 }
24535
24536 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024537 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024539
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024540 /* If the a:000 list and the l: and a: dicts are not referenced we can
24541 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024542 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24543 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24544 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24545 {
24546 free_funccal(fc, FALSE);
24547 }
24548 else
24549 {
24550 hashitem_T *hi;
24551 listitem_T *li;
24552 int todo;
24553
24554 /* "fc" is still in use. This can happen when returning "a:000" or
24555 * assigning "l:" to a global variable.
24556 * Link "fc" in the list for garbage collection later. */
24557 fc->caller = previous_funccal;
24558 previous_funccal = fc;
24559
24560 /* Make a copy of the a: variables, since we didn't do that above. */
24561 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24562 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24563 {
24564 if (!HASHITEM_EMPTY(hi))
24565 {
24566 --todo;
24567 v = HI2DI(hi);
24568 copy_tv(&v->di_tv, &v->di_tv);
24569 }
24570 }
24571
24572 /* Make a copy of the a:000 items, since we didn't do that above. */
24573 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24574 copy_tv(&li->li_tv, &li->li_tv);
24575 }
24576}
24577
24578/*
24579 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024580 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024581 */
24582 static int
24583can_free_funccal(fc, copyID)
24584 funccall_T *fc;
24585 int copyID;
24586{
24587 return (fc->l_varlist.lv_copyID != copyID
24588 && fc->l_vars.dv_copyID != copyID
24589 && fc->l_avars.dv_copyID != copyID);
24590}
24591
24592/*
24593 * Free "fc" and what it contains.
24594 */
24595 static void
24596free_funccal(fc, free_val)
24597 funccall_T *fc;
24598 int free_val; /* a: vars were allocated */
24599{
24600 listitem_T *li;
24601
24602 /* The a: variables typevals may not have been allocated, only free the
24603 * allocated variables. */
24604 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24605
24606 /* free all l: variables */
24607 vars_clear(&fc->l_vars.dv_hashtab);
24608
24609 /* Free the a:000 variables if they were allocated. */
24610 if (free_val)
24611 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24612 clear_tv(&li->li_tv);
24613
24614 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024615}
24616
24617/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024618 * Add a number variable "name" to dict "dp" with value "nr".
24619 */
24620 static void
24621add_nr_var(dp, v, name, nr)
24622 dict_T *dp;
24623 dictitem_T *v;
24624 char *name;
24625 varnumber_T nr;
24626{
24627 STRCPY(v->di_key, name);
24628 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24629 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24630 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024631 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024632 v->di_tv.vval.v_number = nr;
24633}
24634
24635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 * ":return [expr]"
24637 */
24638 void
24639ex_return(eap)
24640 exarg_T *eap;
24641{
24642 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024643 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024644 int returning = FALSE;
24645
24646 if (current_funccal == NULL)
24647 {
24648 EMSG(_("E133: :return not inside a function"));
24649 return;
24650 }
24651
24652 if (eap->skip)
24653 ++emsg_skip;
24654
24655 eap->nextcmd = NULL;
24656 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024657 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658 {
24659 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024660 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024662 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663 }
24664 /* It's safer to return also on error. */
24665 else if (!eap->skip)
24666 {
24667 /*
24668 * Return unless the expression evaluation has been cancelled due to an
24669 * aborting error, an interrupt, or an exception.
24670 */
24671 if (!aborting())
24672 returning = do_return(eap, FALSE, TRUE, NULL);
24673 }
24674
24675 /* When skipping or the return gets pending, advance to the next command
24676 * in this line (!returning). Otherwise, ignore the rest of the line.
24677 * Following lines will be ignored by get_func_line(). */
24678 if (returning)
24679 eap->nextcmd = NULL;
24680 else if (eap->nextcmd == NULL) /* no argument */
24681 eap->nextcmd = check_nextcmd(arg);
24682
24683 if (eap->skip)
24684 --emsg_skip;
24685}
24686
24687/*
24688 * Return from a function. Possibly makes the return pending. Also called
24689 * for a pending return at the ":endtry" or after returning from an extra
24690 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024691 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024692 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693 * FALSE when the return gets pending.
24694 */
24695 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024696do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024697 exarg_T *eap;
24698 int reanimate;
24699 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024700 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024701{
24702 int idx;
24703 struct condstack *cstack = eap->cstack;
24704
24705 if (reanimate)
24706 /* Undo the return. */
24707 current_funccal->returned = FALSE;
24708
24709 /*
24710 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24711 * not in its finally clause (which then is to be executed next) is found.
24712 * In this case, make the ":return" pending for execution at the ":endtry".
24713 * Otherwise, return normally.
24714 */
24715 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24716 if (idx >= 0)
24717 {
24718 cstack->cs_pending[idx] = CSTP_RETURN;
24719
24720 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024721 /* A pending return again gets pending. "rettv" points to an
24722 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024724 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024725 else
24726 {
24727 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024728 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024730 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024732 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024733 {
24734 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024735 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024736 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024737 else
24738 EMSG(_(e_outofmem));
24739 }
24740 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024741 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024742
24743 if (reanimate)
24744 {
24745 /* The pending return value could be overwritten by a ":return"
24746 * without argument in a finally clause; reset the default
24747 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024748 current_funccal->rettv->v_type = VAR_NUMBER;
24749 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024750 }
24751 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024752 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024753 }
24754 else
24755 {
24756 current_funccal->returned = TRUE;
24757
24758 /* If the return is carried out now, store the return value. For
24759 * a return immediately after reanimation, the value is already
24760 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024761 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024763 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024764 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024765 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024766 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024767 }
24768 }
24769
24770 return idx < 0;
24771}
24772
24773/*
24774 * Free the variable with a pending return value.
24775 */
24776 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024777discard_pending_return(rettv)
24778 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779{
Bram Moolenaar33570922005-01-25 22:26:29 +000024780 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781}
24782
24783/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024784 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785 * is an allocated string. Used by report_pending() for verbose messages.
24786 */
24787 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024788get_return_cmd(rettv)
24789 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024790{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024791 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024792 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024793 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024794
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024795 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024796 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024797 if (s == NULL)
24798 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024799
24800 STRCPY(IObuff, ":return ");
24801 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24802 if (STRLEN(s) + 8 >= IOSIZE)
24803 STRCPY(IObuff + IOSIZE - 4, "...");
24804 vim_free(tofree);
24805 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024806}
24807
24808/*
24809 * Get next function line.
24810 * Called by do_cmdline() to get the next line.
24811 * Returns allocated string, or NULL for end of function.
24812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024813 char_u *
24814get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024815 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024817 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024818{
Bram Moolenaar33570922005-01-25 22:26:29 +000024819 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024820 ufunc_T *fp = fcp->func;
24821 char_u *retval;
24822 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024823
24824 /* If breakpoints have been added/deleted need to check for it. */
24825 if (fcp->dbg_tick != debug_tick)
24826 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024827 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024828 sourcing_lnum);
24829 fcp->dbg_tick = debug_tick;
24830 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024831#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024832 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024833 func_line_end(cookie);
24834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024835
Bram Moolenaar05159a02005-02-26 23:04:13 +000024836 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024837 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24838 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839 retval = NULL;
24840 else
24841 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024842 /* Skip NULL lines (continuation lines). */
24843 while (fcp->linenr < gap->ga_len
24844 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24845 ++fcp->linenr;
24846 if (fcp->linenr >= gap->ga_len)
24847 retval = NULL;
24848 else
24849 {
24850 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24851 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024852#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024853 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024854 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024855#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024857 }
24858
24859 /* Did we encounter a breakpoint? */
24860 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24861 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024862 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024863 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024864 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024865 sourcing_lnum);
24866 fcp->dbg_tick = debug_tick;
24867 }
24868
24869 return retval;
24870}
24871
Bram Moolenaar05159a02005-02-26 23:04:13 +000024872#if defined(FEAT_PROFILE) || defined(PROTO)
24873/*
24874 * Called when starting to read a function line.
24875 * "sourcing_lnum" must be correct!
24876 * When skipping lines it may not actually be executed, but we won't find out
24877 * until later and we need to store the time now.
24878 */
24879 void
24880func_line_start(cookie)
24881 void *cookie;
24882{
24883 funccall_T *fcp = (funccall_T *)cookie;
24884 ufunc_T *fp = fcp->func;
24885
24886 if (fp->uf_profiling && sourcing_lnum >= 1
24887 && sourcing_lnum <= fp->uf_lines.ga_len)
24888 {
24889 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024890 /* Skip continuation lines. */
24891 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24892 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024893 fp->uf_tml_execed = FALSE;
24894 profile_start(&fp->uf_tml_start);
24895 profile_zero(&fp->uf_tml_children);
24896 profile_get_wait(&fp->uf_tml_wait);
24897 }
24898}
24899
24900/*
24901 * Called when actually executing a function line.
24902 */
24903 void
24904func_line_exec(cookie)
24905 void *cookie;
24906{
24907 funccall_T *fcp = (funccall_T *)cookie;
24908 ufunc_T *fp = fcp->func;
24909
24910 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24911 fp->uf_tml_execed = TRUE;
24912}
24913
24914/*
24915 * Called when done with a function line.
24916 */
24917 void
24918func_line_end(cookie)
24919 void *cookie;
24920{
24921 funccall_T *fcp = (funccall_T *)cookie;
24922 ufunc_T *fp = fcp->func;
24923
24924 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24925 {
24926 if (fp->uf_tml_execed)
24927 {
24928 ++fp->uf_tml_count[fp->uf_tml_idx];
24929 profile_end(&fp->uf_tml_start);
24930 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024931 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024932 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24933 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024934 }
24935 fp->uf_tml_idx = -1;
24936 }
24937}
24938#endif
24939
Bram Moolenaar071d4272004-06-13 20:20:40 +000024940/*
24941 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024942 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943 */
24944 int
24945func_has_ended(cookie)
24946 void *cookie;
24947{
Bram Moolenaar33570922005-01-25 22:26:29 +000024948 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024949
24950 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24951 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024952 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024953 || fcp->returned);
24954}
24955
24956/*
24957 * return TRUE if cookie indicates a function which "abort"s on errors.
24958 */
24959 int
24960func_has_abort(cookie)
24961 void *cookie;
24962{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024963 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964}
24965
24966#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24967typedef enum
24968{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024969 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24970 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24971 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024972} var_flavour_T;
24973
24974static var_flavour_T var_flavour __ARGS((char_u *varname));
24975
24976 static var_flavour_T
24977var_flavour(varname)
24978 char_u *varname;
24979{
24980 char_u *p = varname;
24981
24982 if (ASCII_ISUPPER(*p))
24983 {
24984 while (*(++p))
24985 if (ASCII_ISLOWER(*p))
24986 return VAR_FLAVOUR_SESSION;
24987 return VAR_FLAVOUR_VIMINFO;
24988 }
24989 else
24990 return VAR_FLAVOUR_DEFAULT;
24991}
24992#endif
24993
24994#if defined(FEAT_VIMINFO) || defined(PROTO)
24995/*
24996 * Restore global vars that start with a capital from the viminfo file
24997 */
24998 int
24999read_viminfo_varlist(virp, writing)
25000 vir_T *virp;
25001 int writing;
25002{
25003 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025004 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025005 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025006
25007 if (!writing && (find_viminfo_parameter('!') != NULL))
25008 {
25009 tab = vim_strchr(virp->vir_line + 1, '\t');
25010 if (tab != NULL)
25011 {
25012 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025013 switch (*tab)
25014 {
25015 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025016#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025017 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025018#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025019 case 'D': type = VAR_DICT; break;
25020 case 'L': type = VAR_LIST; break;
25021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025022
25023 tab = vim_strchr(tab, '\t');
25024 if (tab != NULL)
25025 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025026 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025027 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025028 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025029 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025030#ifdef FEAT_FLOAT
25031 else if (type == VAR_FLOAT)
25032 (void)string2float(tab + 1, &tv.vval.v_float);
25033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025034 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025035 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025036 if (type == VAR_DICT || type == VAR_LIST)
25037 {
25038 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25039
25040 if (etv == NULL)
25041 /* Failed to parse back the dict or list, use it as a
25042 * string. */
25043 tv.v_type = VAR_STRING;
25044 else
25045 {
25046 vim_free(tv.vval.v_string);
25047 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025048 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025049 }
25050 }
25051
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025052 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025053
25054 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025055 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025056 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25057 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025058 }
25059 }
25060 }
25061
25062 return viminfo_readline(virp);
25063}
25064
25065/*
25066 * Write global vars that start with a capital to the viminfo file
25067 */
25068 void
25069write_viminfo_varlist(fp)
25070 FILE *fp;
25071{
Bram Moolenaar33570922005-01-25 22:26:29 +000025072 hashitem_T *hi;
25073 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025074 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025075 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025076 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025077 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025078 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025079
25080 if (find_viminfo_parameter('!') == NULL)
25081 return;
25082
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025083 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025084
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025085 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025086 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025087 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025088 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025089 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025090 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025091 this_var = HI2DI(hi);
25092 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025093 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025094 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025095 {
25096 case VAR_STRING: s = "STR"; break;
25097 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025098#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025099 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025100#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025101 case VAR_DICT: s = "DIC"; break;
25102 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000025103 default: continue;
25104 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025105 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025106 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025107 if (p != NULL)
25108 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025109 vim_free(tofree);
25110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025111 }
25112 }
25113}
25114#endif
25115
25116#if defined(FEAT_SESSION) || defined(PROTO)
25117 int
25118store_session_globals(fd)
25119 FILE *fd;
25120{
Bram Moolenaar33570922005-01-25 22:26:29 +000025121 hashitem_T *hi;
25122 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025123 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025124 char_u *p, *t;
25125
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025126 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025127 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025128 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025129 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025130 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025131 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025132 this_var = HI2DI(hi);
25133 if ((this_var->di_tv.v_type == VAR_NUMBER
25134 || this_var->di_tv.v_type == VAR_STRING)
25135 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025136 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025137 /* Escape special characters with a backslash. Turn a LF and
25138 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025139 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025140 (char_u *)"\\\"\n\r");
25141 if (p == NULL) /* out of memory */
25142 break;
25143 for (t = p; *t != NUL; ++t)
25144 if (*t == '\n')
25145 *t = 'n';
25146 else if (*t == '\r')
25147 *t = 'r';
25148 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025149 this_var->di_key,
25150 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25151 : ' ',
25152 p,
25153 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25154 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025155 || put_eol(fd) == FAIL)
25156 {
25157 vim_free(p);
25158 return FAIL;
25159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025160 vim_free(p);
25161 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025162#ifdef FEAT_FLOAT
25163 else if (this_var->di_tv.v_type == VAR_FLOAT
25164 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25165 {
25166 float_T f = this_var->di_tv.vval.v_float;
25167 int sign = ' ';
25168
25169 if (f < 0)
25170 {
25171 f = -f;
25172 sign = '-';
25173 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025174 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025175 this_var->di_key, sign, f) < 0)
25176 || put_eol(fd) == FAIL)
25177 return FAIL;
25178 }
25179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025180 }
25181 }
25182 return OK;
25183}
25184#endif
25185
Bram Moolenaar661b1822005-07-28 22:36:45 +000025186/*
25187 * Display script name where an item was last set.
25188 * Should only be invoked when 'verbose' is non-zero.
25189 */
25190 void
25191last_set_msg(scriptID)
25192 scid_T scriptID;
25193{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025194 char_u *p;
25195
Bram Moolenaar661b1822005-07-28 22:36:45 +000025196 if (scriptID != 0)
25197 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025198 p = home_replace_save(NULL, get_scriptname(scriptID));
25199 if (p != NULL)
25200 {
25201 verbose_enter();
25202 MSG_PUTS(_("\n\tLast set from "));
25203 MSG_PUTS(p);
25204 vim_free(p);
25205 verbose_leave();
25206 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025207 }
25208}
25209
Bram Moolenaard812df62008-11-09 12:46:09 +000025210/*
25211 * List v:oldfiles in a nice way.
25212 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025213 void
25214ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025215 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025216{
25217 list_T *l = vimvars[VV_OLDFILES].vv_list;
25218 listitem_T *li;
25219 int nr = 0;
25220
25221 if (l == NULL)
25222 msg((char_u *)_("No old files"));
25223 else
25224 {
25225 msg_start();
25226 msg_scroll = TRUE;
25227 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25228 {
25229 msg_outnum((long)++nr);
25230 MSG_PUTS(": ");
25231 msg_outtrans(get_tv_string(&li->li_tv));
25232 msg_putchar('\n');
25233 out_flush(); /* output one line at a time */
25234 ui_breakcheck();
25235 }
25236 /* Assume "got_int" was set to truncate the listing. */
25237 got_int = FALSE;
25238
25239#ifdef FEAT_BROWSE_CMD
25240 if (cmdmod.browse)
25241 {
25242 quit_more = FALSE;
25243 nr = prompt_for_number(FALSE);
25244 msg_starthere();
25245 if (nr > 0)
25246 {
25247 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25248 (long)nr);
25249
25250 if (p != NULL)
25251 {
25252 p = expand_env_save(p);
25253 eap->arg = p;
25254 eap->cmdidx = CMD_edit;
25255 cmdmod.browse = FALSE;
25256 do_exedit(eap, NULL);
25257 vim_free(p);
25258 }
25259 }
25260 }
25261#endif
25262 }
25263}
25264
Bram Moolenaar53744302015-07-17 17:38:22 +020025265/* reset v:option_new, v:option_old and v:option_type */
25266 void
25267reset_v_option_vars()
25268{
25269 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25270 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25271 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25272}
25273
25274
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275#endif /* FEAT_EVAL */
25276
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025278#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279
25280#ifdef WIN3264
25281/*
25282 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25283 */
25284static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25285static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25286static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25287
25288/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025289 * Get the short path (8.3) for the filename in "fnamep".
25290 * Only works for a valid file name.
25291 * When the path gets longer "fnamep" is changed and the allocated buffer
25292 * is put in "bufp".
25293 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25294 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025295 */
25296 static int
25297get_short_pathname(fnamep, bufp, fnamelen)
25298 char_u **fnamep;
25299 char_u **bufp;
25300 int *fnamelen;
25301{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025302 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025303 char_u *newbuf;
25304
25305 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025306 l = GetShortPathName(*fnamep, *fnamep, len);
25307 if (l > len - 1)
25308 {
25309 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025310 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025311 newbuf = vim_strnsave(*fnamep, l);
25312 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025313 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025314
25315 vim_free(*bufp);
25316 *fnamep = *bufp = newbuf;
25317
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025318 /* Really should always succeed, as the buffer is big enough. */
25319 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025320 }
25321
25322 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025323 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025324}
25325
25326/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025327 * Get the short path (8.3) for the filename in "fname". The converted
25328 * path is returned in "bufp".
25329 *
25330 * Some of the directories specified in "fname" may not exist. This function
25331 * will shorten the existing directories at the beginning of the path and then
25332 * append the remaining non-existing path.
25333 *
25334 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025335 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025336 * bufp - Pointer to an allocated buffer for the filename.
25337 * fnamelen - Length of the filename pointed to by fname
25338 *
25339 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025340 */
25341 static int
25342shortpath_for_invalid_fname(fname, bufp, fnamelen)
25343 char_u **fname;
25344 char_u **bufp;
25345 int *fnamelen;
25346{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025347 char_u *short_fname, *save_fname, *pbuf_unused;
25348 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025349 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025350 int old_len, len;
25351 int new_len, sfx_len;
25352 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025353
25354 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025355 old_len = *fnamelen;
25356 save_fname = vim_strnsave(*fname, old_len);
25357 pbuf_unused = NULL;
25358 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025359
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025360 endp = save_fname + old_len - 1; /* Find the end of the copy */
25361 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025362
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025363 /*
25364 * Try shortening the supplied path till it succeeds by removing one
25365 * directory at a time from the tail of the path.
25366 */
25367 len = 0;
25368 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025370 /* go back one path-separator */
25371 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25372 --endp;
25373 if (endp <= save_fname)
25374 break; /* processed the complete path */
25375
25376 /*
25377 * Replace the path separator with a NUL and try to shorten the
25378 * resulting path.
25379 */
25380 ch = *endp;
25381 *endp = 0;
25382 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025383 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025384 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25385 {
25386 retval = FAIL;
25387 goto theend;
25388 }
25389 *endp = ch; /* preserve the string */
25390
25391 if (len > 0)
25392 break; /* successfully shortened the path */
25393
25394 /* failed to shorten the path. Skip the path separator */
25395 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025396 }
25397
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025398 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025399 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025400 /*
25401 * Succeeded in shortening the path. Now concatenate the shortened
25402 * path with the remaining path at the tail.
25403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025405 /* Compute the length of the new path. */
25406 sfx_len = (int)(save_endp - endp) + 1;
25407 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025408
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025409 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025410 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025411 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025412 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025413 /* There is not enough space in the currently allocated string,
25414 * copy it to a buffer big enough. */
25415 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025417 {
25418 retval = FAIL;
25419 goto theend;
25420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025421 }
25422 else
25423 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025424 /* Transfer short_fname to the main buffer (it's big enough),
25425 * unless get_short_pathname() did its work in-place. */
25426 *fname = *bufp = save_fname;
25427 if (short_fname != save_fname)
25428 vim_strncpy(save_fname, short_fname, len);
25429 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025430 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025431
25432 /* concat the not-shortened part of the path */
25433 vim_strncpy(*fname + len, endp, sfx_len);
25434 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025436
25437theend:
25438 vim_free(pbuf_unused);
25439 vim_free(save_fname);
25440
25441 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025442}
25443
25444/*
25445 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025446 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447 */
25448 static int
25449shortpath_for_partial(fnamep, bufp, fnamelen)
25450 char_u **fnamep;
25451 char_u **bufp;
25452 int *fnamelen;
25453{
25454 int sepcount, len, tflen;
25455 char_u *p;
25456 char_u *pbuf, *tfname;
25457 int hasTilde;
25458
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025459 /* Count up the path separators from the RHS.. so we know which part
25460 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025461 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025462 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025463 if (vim_ispathsep(*p))
25464 ++sepcount;
25465
25466 /* Need full path first (use expand_env() to remove a "~/") */
25467 hasTilde = (**fnamep == '~');
25468 if (hasTilde)
25469 pbuf = tfname = expand_env_save(*fnamep);
25470 else
25471 pbuf = tfname = FullName_save(*fnamep, FALSE);
25472
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025473 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025475 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25476 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025477
25478 if (len == 0)
25479 {
25480 /* Don't have a valid filename, so shorten the rest of the
25481 * path if we can. This CAN give us invalid 8.3 filenames, but
25482 * there's not a lot of point in guessing what it might be.
25483 */
25484 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025485 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25486 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025487 }
25488
25489 /* Count the paths backward to find the beginning of the desired string. */
25490 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025491 {
25492#ifdef FEAT_MBYTE
25493 if (has_mbyte)
25494 p -= mb_head_off(tfname, p);
25495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025496 if (vim_ispathsep(*p))
25497 {
25498 if (sepcount == 0 || (hasTilde && sepcount == 1))
25499 break;
25500 else
25501 sepcount --;
25502 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025504 if (hasTilde)
25505 {
25506 --p;
25507 if (p >= tfname)
25508 *p = '~';
25509 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025510 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025511 }
25512 else
25513 ++p;
25514
25515 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25516 vim_free(*bufp);
25517 *fnamelen = (int)STRLEN(p);
25518 *bufp = pbuf;
25519 *fnamep = p;
25520
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025521 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522}
25523#endif /* WIN3264 */
25524
25525/*
25526 * Adjust a filename, according to a string of modifiers.
25527 * *fnamep must be NUL terminated when called. When returning, the length is
25528 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025529 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530 * When there is an error, *fnamep is set to NULL.
25531 */
25532 int
25533modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25534 char_u *src; /* string with modifiers */
25535 int *usedlen; /* characters after src that are used */
25536 char_u **fnamep; /* file name so far */
25537 char_u **bufp; /* buffer for allocated file name or NULL */
25538 int *fnamelen; /* length of fnamep */
25539{
25540 int valid = 0;
25541 char_u *tail;
25542 char_u *s, *p, *pbuf;
25543 char_u dirname[MAXPATHL];
25544 int c;
25545 int has_fullname = 0;
25546#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025547 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025548 int has_shortname = 0;
25549#endif
25550
25551repeat:
25552 /* ":p" - full path/file_name */
25553 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25554 {
25555 has_fullname = 1;
25556
25557 valid |= VALID_PATH;
25558 *usedlen += 2;
25559
25560 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25561 if ((*fnamep)[0] == '~'
25562#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25563 && ((*fnamep)[1] == '/'
25564# ifdef BACKSLASH_IN_FILENAME
25565 || (*fnamep)[1] == '\\'
25566# endif
25567 || (*fnamep)[1] == NUL)
25568
25569#endif
25570 )
25571 {
25572 *fnamep = expand_env_save(*fnamep);
25573 vim_free(*bufp); /* free any allocated file name */
25574 *bufp = *fnamep;
25575 if (*fnamep == NULL)
25576 return -1;
25577 }
25578
25579 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025580 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025581 {
25582 if (vim_ispathsep(*p)
25583 && p[1] == '.'
25584 && (p[2] == NUL
25585 || vim_ispathsep(p[2])
25586 || (p[2] == '.'
25587 && (p[3] == NUL || vim_ispathsep(p[3])))))
25588 break;
25589 }
25590
25591 /* FullName_save() is slow, don't use it when not needed. */
25592 if (*p != NUL || !vim_isAbsName(*fnamep))
25593 {
25594 *fnamep = FullName_save(*fnamep, *p != NUL);
25595 vim_free(*bufp); /* free any allocated file name */
25596 *bufp = *fnamep;
25597 if (*fnamep == NULL)
25598 return -1;
25599 }
25600
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025601#ifdef WIN3264
25602# if _WIN32_WINNT >= 0x0500
25603 if (vim_strchr(*fnamep, '~') != NULL)
25604 {
25605 /* Expand 8.3 filename to full path. Needed to make sure the same
25606 * file does not have two different names.
25607 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25608 p = alloc(_MAX_PATH + 1);
25609 if (p != NULL)
25610 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025611 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025612 {
25613 vim_free(*bufp);
25614 *bufp = *fnamep = p;
25615 }
25616 else
25617 vim_free(p);
25618 }
25619 }
25620# endif
25621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025622 /* Append a path separator to a directory. */
25623 if (mch_isdir(*fnamep))
25624 {
25625 /* Make room for one or two extra characters. */
25626 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25627 vim_free(*bufp); /* free any allocated file name */
25628 *bufp = *fnamep;
25629 if (*fnamep == NULL)
25630 return -1;
25631 add_pathsep(*fnamep);
25632 }
25633 }
25634
25635 /* ":." - path relative to the current directory */
25636 /* ":~" - path relative to the home directory */
25637 /* ":8" - shortname path - postponed till after */
25638 while (src[*usedlen] == ':'
25639 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25640 {
25641 *usedlen += 2;
25642 if (c == '8')
25643 {
25644#ifdef WIN3264
25645 has_shortname = 1; /* Postpone this. */
25646#endif
25647 continue;
25648 }
25649 pbuf = NULL;
25650 /* Need full path first (use expand_env() to remove a "~/") */
25651 if (!has_fullname)
25652 {
25653 if (c == '.' && **fnamep == '~')
25654 p = pbuf = expand_env_save(*fnamep);
25655 else
25656 p = pbuf = FullName_save(*fnamep, FALSE);
25657 }
25658 else
25659 p = *fnamep;
25660
25661 has_fullname = 0;
25662
25663 if (p != NULL)
25664 {
25665 if (c == '.')
25666 {
25667 mch_dirname(dirname, MAXPATHL);
25668 s = shorten_fname(p, dirname);
25669 if (s != NULL)
25670 {
25671 *fnamep = s;
25672 if (pbuf != NULL)
25673 {
25674 vim_free(*bufp); /* free any allocated file name */
25675 *bufp = pbuf;
25676 pbuf = NULL;
25677 }
25678 }
25679 }
25680 else
25681 {
25682 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25683 /* Only replace it when it starts with '~' */
25684 if (*dirname == '~')
25685 {
25686 s = vim_strsave(dirname);
25687 if (s != NULL)
25688 {
25689 *fnamep = s;
25690 vim_free(*bufp);
25691 *bufp = s;
25692 }
25693 }
25694 }
25695 vim_free(pbuf);
25696 }
25697 }
25698
25699 tail = gettail(*fnamep);
25700 *fnamelen = (int)STRLEN(*fnamep);
25701
25702 /* ":h" - head, remove "/file_name", can be repeated */
25703 /* Don't remove the first "/" or "c:\" */
25704 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25705 {
25706 valid |= VALID_HEAD;
25707 *usedlen += 2;
25708 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025709 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025710 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025711 *fnamelen = (int)(tail - *fnamep);
25712#ifdef VMS
25713 if (*fnamelen > 0)
25714 *fnamelen += 1; /* the path separator is part of the path */
25715#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025716 if (*fnamelen == 0)
25717 {
25718 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25719 p = vim_strsave((char_u *)".");
25720 if (p == NULL)
25721 return -1;
25722 vim_free(*bufp);
25723 *bufp = *fnamep = tail = p;
25724 *fnamelen = 1;
25725 }
25726 else
25727 {
25728 while (tail > s && !after_pathsep(s, tail))
25729 mb_ptr_back(*fnamep, tail);
25730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025731 }
25732
25733 /* ":8" - shortname */
25734 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25735 {
25736 *usedlen += 2;
25737#ifdef WIN3264
25738 has_shortname = 1;
25739#endif
25740 }
25741
25742#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025743 /*
25744 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025745 */
25746 if (has_shortname)
25747 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025748 /* Copy the string if it is shortened by :h and when it wasn't copied
25749 * yet, because we are going to change it in place. Avoids changing
25750 * the buffer name for "%:8". */
25751 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025752 {
25753 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025754 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025755 return -1;
25756 vim_free(*bufp);
25757 *bufp = *fnamep = p;
25758 }
25759
25760 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025761 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025762 if (!has_fullname && !vim_isAbsName(*fnamep))
25763 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025764 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025765 return -1;
25766 }
25767 else
25768 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025769 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025770
Bram Moolenaardc935552011-08-17 15:23:23 +020025771 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025772 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025773 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774 return -1;
25775
25776 if (l == 0)
25777 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025778 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025779 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025780 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025781 return -1;
25782 }
25783 *fnamelen = l;
25784 }
25785 }
25786#endif /* WIN3264 */
25787
25788 /* ":t" - tail, just the basename */
25789 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25790 {
25791 *usedlen += 2;
25792 *fnamelen -= (int)(tail - *fnamep);
25793 *fnamep = tail;
25794 }
25795
25796 /* ":e" - extension, can be repeated */
25797 /* ":r" - root, without extension, can be repeated */
25798 while (src[*usedlen] == ':'
25799 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25800 {
25801 /* find a '.' in the tail:
25802 * - for second :e: before the current fname
25803 * - otherwise: The last '.'
25804 */
25805 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25806 s = *fnamep - 2;
25807 else
25808 s = *fnamep + *fnamelen - 1;
25809 for ( ; s > tail; --s)
25810 if (s[0] == '.')
25811 break;
25812 if (src[*usedlen + 1] == 'e') /* :e */
25813 {
25814 if (s > tail)
25815 {
25816 *fnamelen += (int)(*fnamep - (s + 1));
25817 *fnamep = s + 1;
25818#ifdef VMS
25819 /* cut version from the extension */
25820 s = *fnamep + *fnamelen - 1;
25821 for ( ; s > *fnamep; --s)
25822 if (s[0] == ';')
25823 break;
25824 if (s > *fnamep)
25825 *fnamelen = s - *fnamep;
25826#endif
25827 }
25828 else if (*fnamep <= tail)
25829 *fnamelen = 0;
25830 }
25831 else /* :r */
25832 {
25833 if (s > tail) /* remove one extension */
25834 *fnamelen = (int)(s - *fnamep);
25835 }
25836 *usedlen += 2;
25837 }
25838
25839 /* ":s?pat?foo?" - substitute */
25840 /* ":gs?pat?foo?" - global substitute */
25841 if (src[*usedlen] == ':'
25842 && (src[*usedlen + 1] == 's'
25843 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25844 {
25845 char_u *str;
25846 char_u *pat;
25847 char_u *sub;
25848 int sep;
25849 char_u *flags;
25850 int didit = FALSE;
25851
25852 flags = (char_u *)"";
25853 s = src + *usedlen + 2;
25854 if (src[*usedlen + 1] == 'g')
25855 {
25856 flags = (char_u *)"g";
25857 ++s;
25858 }
25859
25860 sep = *s++;
25861 if (sep)
25862 {
25863 /* find end of pattern */
25864 p = vim_strchr(s, sep);
25865 if (p != NULL)
25866 {
25867 pat = vim_strnsave(s, (int)(p - s));
25868 if (pat != NULL)
25869 {
25870 s = p + 1;
25871 /* find end of substitution */
25872 p = vim_strchr(s, sep);
25873 if (p != NULL)
25874 {
25875 sub = vim_strnsave(s, (int)(p - s));
25876 str = vim_strnsave(*fnamep, *fnamelen);
25877 if (sub != NULL && str != NULL)
25878 {
25879 *usedlen = (int)(p + 1 - src);
25880 s = do_string_sub(str, pat, sub, flags);
25881 if (s != NULL)
25882 {
25883 *fnamep = s;
25884 *fnamelen = (int)STRLEN(s);
25885 vim_free(*bufp);
25886 *bufp = s;
25887 didit = TRUE;
25888 }
25889 }
25890 vim_free(sub);
25891 vim_free(str);
25892 }
25893 vim_free(pat);
25894 }
25895 }
25896 /* after using ":s", repeat all the modifiers */
25897 if (didit)
25898 goto repeat;
25899 }
25900 }
25901
Bram Moolenaar26df0922014-02-23 23:39:13 +010025902 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25903 {
25904 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25905 if (p == NULL)
25906 return -1;
25907 vim_free(*bufp);
25908 *bufp = *fnamep = p;
25909 *fnamelen = (int)STRLEN(p);
25910 *usedlen += 2;
25911 }
25912
Bram Moolenaar071d4272004-06-13 20:20:40 +000025913 return valid;
25914}
25915
25916/*
25917 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25918 * "flags" can be "g" to do a global substitute.
25919 * Returns an allocated string, NULL for error.
25920 */
25921 char_u *
25922do_string_sub(str, pat, sub, flags)
25923 char_u *str;
25924 char_u *pat;
25925 char_u *sub;
25926 char_u *flags;
25927{
25928 int sublen;
25929 regmatch_T regmatch;
25930 int i;
25931 int do_all;
25932 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025933 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025934 garray_T ga;
25935 char_u *ret;
25936 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025937 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025938
25939 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25940 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025941 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025942
25943 ga_init2(&ga, 1, 200);
25944
25945 do_all = (flags[0] == 'g');
25946
25947 regmatch.rm_ic = p_ic;
25948 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25949 if (regmatch.regprog != NULL)
25950 {
25951 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025952 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025953 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25954 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025955 /* Skip empty match except for first match. */
25956 if (regmatch.startp[0] == regmatch.endp[0])
25957 {
25958 if (zero_width == regmatch.startp[0])
25959 {
25960 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025961 i = MB_PTR2LEN(tail);
25962 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25963 (size_t)i);
25964 ga.ga_len += i;
25965 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025966 continue;
25967 }
25968 zero_width = regmatch.startp[0];
25969 }
25970
Bram Moolenaar071d4272004-06-13 20:20:40 +000025971 /*
25972 * Get some space for a temporary buffer to do the substitution
25973 * into. It will contain:
25974 * - The text up to where the match is.
25975 * - The substituted text.
25976 * - The text after the match.
25977 */
25978 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025979 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025980 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25981 {
25982 ga_clear(&ga);
25983 break;
25984 }
25985
25986 /* copy the text up to where the match is */
25987 i = (int)(regmatch.startp[0] - tail);
25988 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25989 /* add the substituted text */
25990 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25991 + ga.ga_len + i, TRUE, TRUE, FALSE);
25992 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025993 tail = regmatch.endp[0];
25994 if (*tail == NUL)
25995 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025996 if (!do_all)
25997 break;
25998 }
25999
26000 if (ga.ga_data != NULL)
26001 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26002
Bram Moolenaar473de612013-06-08 18:19:48 +020026003 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026004 }
26005
26006 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26007 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026008 if (p_cpo == empty_option)
26009 p_cpo = save_cpo;
26010 else
26011 /* Darn, evaluating {sub} expression changed the value. */
26012 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026013
26014 return ret;
26015}
26016
26017#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */