blob: d4e3b9ef59512afb8470dfce9fc4c9701155b7f6 [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 Moolenaar071d4272004-06-13 20:20:40 +00008134 {"delete", 1, 1, f_delete},
8135 {"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{
10394 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010395 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010397 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398}
10399
10400/*
10401 * "did_filetype()" function
10402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010404f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010405 typval_T *argvars UNUSED;
10406 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407{
10408#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010409 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410#endif
10411}
10412
10413/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010414 * "diff_filler()" function
10415 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010417f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010418 typval_T *argvars UNUSED;
10419 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010420{
10421#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010422 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010423#endif
10424}
10425
10426/*
10427 * "diff_hlID()" function
10428 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010430f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010431 typval_T *argvars UNUSED;
10432 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010433{
10434#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010435 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010436 static linenr_T prev_lnum = 0;
10437 static int changedtick = 0;
10438 static int fnum = 0;
10439 static int change_start = 0;
10440 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010441 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010442 int filler_lines;
10443 int col;
10444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010445 if (lnum < 0) /* ignore type error in {lnum} arg */
10446 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010447 if (lnum != prev_lnum
10448 || changedtick != curbuf->b_changedtick
10449 || fnum != curbuf->b_fnum)
10450 {
10451 /* New line, buffer, change: need to get the values. */
10452 filler_lines = diff_check(curwin, lnum);
10453 if (filler_lines < 0)
10454 {
10455 if (filler_lines == -1)
10456 {
10457 change_start = MAXCOL;
10458 change_end = -1;
10459 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10460 hlID = HLF_ADD; /* added line */
10461 else
10462 hlID = HLF_CHD; /* changed line */
10463 }
10464 else
10465 hlID = HLF_ADD; /* added line */
10466 }
10467 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010468 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010469 prev_lnum = lnum;
10470 changedtick = curbuf->b_changedtick;
10471 fnum = curbuf->b_fnum;
10472 }
10473
10474 if (hlID == HLF_CHD || hlID == HLF_TXD)
10475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010476 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010477 if (col >= change_start && col <= change_end)
10478 hlID = HLF_TXD; /* changed text */
10479 else
10480 hlID = HLF_CHD; /* changed line */
10481 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010482 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010483#endif
10484}
10485
10486/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010487 * "empty({expr})" function
10488 */
10489 static void
10490f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010491 typval_T *argvars;
10492 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010493{
10494 int n;
10495
10496 switch (argvars[0].v_type)
10497 {
10498 case VAR_STRING:
10499 case VAR_FUNC:
10500 n = argvars[0].vval.v_string == NULL
10501 || *argvars[0].vval.v_string == NUL;
10502 break;
10503 case VAR_NUMBER:
10504 n = argvars[0].vval.v_number == 0;
10505 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010506#ifdef FEAT_FLOAT
10507 case VAR_FLOAT:
10508 n = argvars[0].vval.v_float == 0.0;
10509 break;
10510#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010511 case VAR_LIST:
10512 n = argvars[0].vval.v_list == NULL
10513 || argvars[0].vval.v_list->lv_first == NULL;
10514 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010515 case VAR_DICT:
10516 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010517 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010518 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010519 default:
10520 EMSG2(_(e_intern2), "f_empty()");
10521 n = 0;
10522 }
10523
10524 rettv->vval.v_number = n;
10525}
10526
10527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 * "escape({string}, {chars})" function
10529 */
10530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010531f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010532 typval_T *argvars;
10533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534{
10535 char_u buf[NUMBUFLEN];
10536
Bram Moolenaar758711c2005-02-02 23:11:38 +000010537 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10538 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010539 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540}
10541
10542/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010543 * "eval()" function
10544 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010545 static void
10546f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010547 typval_T *argvars;
10548 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010549{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010550 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010551
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010552 s = get_tv_string_chk(&argvars[0]);
10553 if (s != NULL)
10554 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010555
Bram Moolenaar615b9972015-01-14 17:15:05 +010010556 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010557 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10558 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010559 if (p != NULL && !aborting())
10560 EMSG2(_(e_invexpr2), p);
10561 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010562 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010563 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010564 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010565 else if (*s != NUL)
10566 EMSG(_(e_trailing));
10567}
10568
10569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 * "eventhandler()" function
10571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010573f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010574 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010577 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578}
10579
10580/*
10581 * "executable()" function
10582 */
10583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010584f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010585 typval_T *argvars;
10586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010588 char_u *name = get_tv_string(&argvars[0]);
10589
10590 /* Check in $PATH and also check directly if there is a directory name. */
10591 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10592 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010593}
10594
10595/*
10596 * "exepath()" function
10597 */
10598 static void
10599f_exepath(argvars, rettv)
10600 typval_T *argvars;
10601 typval_T *rettv;
10602{
10603 char_u *p = NULL;
10604
Bram Moolenaarb5971142015-03-21 17:32:19 +010010605 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010606 rettv->v_type = VAR_STRING;
10607 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608}
10609
10610/*
10611 * "exists()" function
10612 */
10613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010614f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010615 typval_T *argvars;
10616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617{
10618 char_u *p;
10619 char_u *name;
10620 int n = FALSE;
10621 int len = 0;
10622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010623 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 if (*p == '$') /* environment variable */
10625 {
10626 /* first try "normal" environment variables (fast) */
10627 if (mch_getenv(p + 1) != NULL)
10628 n = TRUE;
10629 else
10630 {
10631 /* try expanding things like $VIM and ${HOME} */
10632 p = expand_env_save(p);
10633 if (p != NULL && *p != '$')
10634 n = TRUE;
10635 vim_free(p);
10636 }
10637 }
10638 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010639 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010640 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010641 if (*skipwhite(p) != NUL)
10642 n = FALSE; /* trailing garbage */
10643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 else if (*p == '*') /* internal or user defined function */
10645 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010646 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 }
10648 else if (*p == ':')
10649 {
10650 n = cmd_exists(p + 1);
10651 }
10652 else if (*p == '#')
10653 {
10654#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010655 if (p[1] == '#')
10656 n = autocmd_supported(p + 2);
10657 else
10658 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659#endif
10660 }
10661 else /* internal variable */
10662 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010663 char_u *tofree;
10664 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010666 /* get_name_len() takes care of expanding curly braces */
10667 name = p;
10668 len = get_name_len(&p, &tofree, TRUE, FALSE);
10669 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010671 if (tofree != NULL)
10672 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010673 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010674 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010676 /* handle d.key, l[idx], f(expr) */
10677 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10678 if (n)
10679 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 }
10681 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010682 if (*p != NUL)
10683 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010685 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 }
10687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010688 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689}
10690
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010691#ifdef FEAT_FLOAT
10692/*
10693 * "exp()" function
10694 */
10695 static void
10696f_exp(argvars, rettv)
10697 typval_T *argvars;
10698 typval_T *rettv;
10699{
10700 float_T f;
10701
10702 rettv->v_type = VAR_FLOAT;
10703 if (get_float_arg(argvars, &f) == OK)
10704 rettv->vval.v_float = exp(f);
10705 else
10706 rettv->vval.v_float = 0.0;
10707}
10708#endif
10709
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710/*
10711 * "expand()" function
10712 */
10713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010714f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010715 typval_T *argvars;
10716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717{
10718 char_u *s;
10719 int len;
10720 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010721 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010723 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010724 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010726 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010727 if (argvars[1].v_type != VAR_UNKNOWN
10728 && argvars[2].v_type != VAR_UNKNOWN
10729 && get_tv_number_chk(&argvars[2], &error)
10730 && !error)
10731 {
10732 rettv->v_type = VAR_LIST;
10733 rettv->vval.v_list = NULL;
10734 }
10735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010736 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 if (*s == '%' || *s == '#' || *s == '<')
10738 {
10739 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010740 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010742 if (rettv->v_type == VAR_LIST)
10743 {
10744 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10745 list_append_string(rettv->vval.v_list, result, -1);
10746 else
10747 vim_free(result);
10748 }
10749 else
10750 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 }
10752 else
10753 {
10754 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010755 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010756 if (argvars[1].v_type != VAR_UNKNOWN
10757 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010758 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010759 if (!error)
10760 {
10761 ExpandInit(&xpc);
10762 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010763 if (p_wic)
10764 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010765 if (rettv->v_type == VAR_STRING)
10766 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10767 options, WILD_ALL);
10768 else if (rettv_list_alloc(rettv) != FAIL)
10769 {
10770 int i;
10771
10772 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10773 for (i = 0; i < xpc.xp_numfiles; i++)
10774 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10775 ExpandCleanup(&xpc);
10776 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010777 }
10778 else
10779 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 }
10781}
10782
10783/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010784 * Go over all entries in "d2" and add them to "d1".
10785 * When "action" is "error" then a duplicate key is an error.
10786 * When "action" is "force" then a duplicate key is overwritten.
10787 * Otherwise duplicate keys are ignored ("action" is "keep").
10788 */
10789 void
10790dict_extend(d1, d2, action)
10791 dict_T *d1;
10792 dict_T *d2;
10793 char_u *action;
10794{
10795 dictitem_T *di1;
10796 hashitem_T *hi2;
10797 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010798 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010799
10800 todo = (int)d2->dv_hashtab.ht_used;
10801 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10802 {
10803 if (!HASHITEM_EMPTY(hi2))
10804 {
10805 --todo;
10806 di1 = dict_find(d1, hi2->hi_key, -1);
10807 if (d1->dv_scope != 0)
10808 {
10809 /* Disallow replacing a builtin function in l: and g:.
10810 * Check the key to be valid when adding to any
10811 * scope. */
10812 if (d1->dv_scope == VAR_DEF_SCOPE
10813 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10814 && var_check_func_name(hi2->hi_key,
10815 di1 == NULL))
10816 break;
10817 if (!valid_varname(hi2->hi_key))
10818 break;
10819 }
10820 if (di1 == NULL)
10821 {
10822 di1 = dictitem_copy(HI2DI(hi2));
10823 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10824 dictitem_free(di1);
10825 }
10826 else if (*action == 'e')
10827 {
10828 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10829 break;
10830 }
10831 else if (*action == 'f' && HI2DI(hi2) != di1)
10832 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010833 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10834 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010835 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010836 clear_tv(&di1->di_tv);
10837 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10838 }
10839 }
10840 }
10841}
10842
10843/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010844 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010846 */
10847 static void
10848f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010849 typval_T *argvars;
10850 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010851{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010852 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010853
Bram Moolenaare9a41262005-01-15 22:18:47 +000010854 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010855 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010856 list_T *l1, *l2;
10857 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010858 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010859 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010860
Bram Moolenaare9a41262005-01-15 22:18:47 +000010861 l1 = argvars[0].vval.v_list;
10862 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010863 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010864 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010865 {
10866 if (argvars[2].v_type != VAR_UNKNOWN)
10867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868 before = get_tv_number_chk(&argvars[2], &error);
10869 if (error)
10870 return; /* type error; errmsg already given */
10871
Bram Moolenaar758711c2005-02-02 23:11:38 +000010872 if (before == l1->lv_len)
10873 item = NULL;
10874 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010875 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010876 item = list_find(l1, before);
10877 if (item == NULL)
10878 {
10879 EMSGN(_(e_listidx), before);
10880 return;
10881 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010882 }
10883 }
10884 else
10885 item = NULL;
10886 list_extend(l1, l2, item);
10887
Bram Moolenaare9a41262005-01-15 22:18:47 +000010888 copy_tv(&argvars[0], rettv);
10889 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010890 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010891 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10892 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010893 dict_T *d1, *d2;
10894 char_u *action;
10895 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010896
10897 d1 = argvars[0].vval.v_dict;
10898 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010899 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010900 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010901 {
10902 /* Check the third argument. */
10903 if (argvars[2].v_type != VAR_UNKNOWN)
10904 {
10905 static char *(av[]) = {"keep", "force", "error"};
10906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010907 action = get_tv_string_chk(&argvars[2]);
10908 if (action == NULL)
10909 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010910 for (i = 0; i < 3; ++i)
10911 if (STRCMP(action, av[i]) == 0)
10912 break;
10913 if (i == 3)
10914 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010915 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010916 return;
10917 }
10918 }
10919 else
10920 action = (char_u *)"force";
10921
Bram Moolenaara9922d62013-05-30 13:01:18 +020010922 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010923
Bram Moolenaare9a41262005-01-15 22:18:47 +000010924 copy_tv(&argvars[0], rettv);
10925 }
10926 }
10927 else
10928 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010929}
10930
10931/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010932 * "feedkeys()" function
10933 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010934 static void
10935f_feedkeys(argvars, rettv)
10936 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010937 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010938{
10939 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010940 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010941 char_u *keys, *flags;
10942 char_u nbuf[NUMBUFLEN];
10943 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010944 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010945
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010946 /* This is not allowed in the sandbox. If the commands would still be
10947 * executed in the sandbox it would be OK, but it probably happens later,
10948 * when "sandbox" is no longer set. */
10949 if (check_secure())
10950 return;
10951
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010952 keys = get_tv_string(&argvars[0]);
10953 if (*keys != NUL)
10954 {
10955 if (argvars[1].v_type != VAR_UNKNOWN)
10956 {
10957 flags = get_tv_string_buf(&argvars[1], nbuf);
10958 for ( ; *flags != NUL; ++flags)
10959 {
10960 switch (*flags)
10961 {
10962 case 'n': remap = FALSE; break;
10963 case 'm': remap = TRUE; break;
10964 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010965 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010966 }
10967 }
10968 }
10969
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010970 /* Need to escape K_SPECIAL and CSI before putting the string in the
10971 * typeahead buffer. */
10972 keys_esc = vim_strsave_escape_csi(keys);
10973 if (keys_esc != NULL)
10974 {
10975 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010976 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010977 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010978 if (vgetc_busy)
10979 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010980 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010981 }
10982}
10983
10984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 * "filereadable()" function
10986 */
10987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010988f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010989 typval_T *argvars;
10990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010992 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 char_u *p;
10994 int n;
10995
Bram Moolenaarc236c162008-07-13 17:41:49 +000010996#ifndef O_NONBLOCK
10997# define O_NONBLOCK 0
10998#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011000 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11001 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 {
11003 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011004 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 }
11006 else
11007 n = FALSE;
11008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011009 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010}
11011
11012/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011013 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 * rights to write into.
11015 */
11016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011018 typval_T *argvars;
11019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011021 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022}
11023
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011024static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011025
11026 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011027findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011028 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011029 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011030 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011031{
11032#ifdef FEAT_SEARCHPATH
11033 char_u *fname;
11034 char_u *fresult = NULL;
11035 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11036 char_u *p;
11037 char_u pathbuf[NUMBUFLEN];
11038 int count = 1;
11039 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011040 int error = FALSE;
11041#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011042
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011043 rettv->vval.v_string = NULL;
11044 rettv->v_type = VAR_STRING;
11045
11046#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011047 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011048
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011049 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011051 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11052 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011053 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011054 else
11055 {
11056 if (*p != NUL)
11057 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011059 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011060 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011061 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011062 }
11063
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011064 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11065 error = TRUE;
11066
11067 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011069 do
11070 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011071 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011072 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011073 fresult = find_file_in_path_option(first ? fname : NULL,
11074 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011075 0, first, path,
11076 find_what,
11077 curbuf->b_ffname,
11078 find_what == FINDFILE_DIR
11079 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011080 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011081
11082 if (fresult != NULL && rettv->v_type == VAR_LIST)
11083 list_append_string(rettv->vval.v_list, fresult, -1);
11084
11085 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011086 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011087
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011088 if (rettv->v_type == VAR_STRING)
11089 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011090#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011091}
11092
Bram Moolenaar33570922005-01-25 22:26:29 +000011093static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
11094static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011095
11096/*
11097 * Implementation of map() and filter().
11098 */
11099 static void
11100filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011101 typval_T *argvars;
11102 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011103 int map;
11104{
11105 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011106 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011107 listitem_T *li, *nli;
11108 list_T *l = NULL;
11109 dictitem_T *di;
11110 hashtab_T *ht;
11111 hashitem_T *hi;
11112 dict_T *d = NULL;
11113 typval_T save_val;
11114 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011115 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011116 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011117 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011118 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011119 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011120 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011121 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011122
Bram Moolenaare9a41262005-01-15 22:18:47 +000011123 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011124 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011125 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011126 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011127 return;
11128 }
11129 else if (argvars[0].v_type == VAR_DICT)
11130 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011131 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011132 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011133 return;
11134 }
11135 else
11136 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011137 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011138 return;
11139 }
11140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011141 expr = get_tv_string_buf_chk(&argvars[1], buf);
11142 /* On type errors, the preceding call has already displayed an error
11143 * message. Avoid a misleading error message for an empty string that
11144 * was not passed as argument. */
11145 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011146 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011147 prepare_vimvar(VV_VAL, &save_val);
11148 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011149
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011150 /* We reset "did_emsg" to be able to detect whether an error
11151 * occurred during evaluation of the expression. */
11152 save_did_emsg = did_emsg;
11153 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011154
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011155 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011156 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011158 vimvars[VV_KEY].vv_type = VAR_STRING;
11159
11160 ht = &d->dv_hashtab;
11161 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011162 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011163 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011165 if (!HASHITEM_EMPTY(hi))
11166 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011167 int r;
11168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011169 --todo;
11170 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011171 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011172 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11173 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011174 break;
11175 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011176 r = filter_map_one(&di->di_tv, expr, map, &rem);
11177 clear_tv(&vimvars[VV_KEY].vv_tv);
11178 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011179 break;
11180 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011181 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011182 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11183 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011184 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011185 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011186 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187 }
11188 }
11189 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011190 }
11191 else
11192 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011193 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011195 for (li = l->lv_first; li != NULL; li = nli)
11196 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011197 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011198 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011199 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011200 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011201 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011202 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011203 break;
11204 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011205 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011206 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011207 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011208 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011209
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011210 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011211 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011212
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011213 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011214 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011215
11216 copy_tv(&argvars[0], rettv);
11217}
11218
11219 static int
11220filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011221 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011222 char_u *expr;
11223 int map;
11224 int *remp;
11225{
Bram Moolenaar33570922005-01-25 22:26:29 +000011226 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011227 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011228 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011229
Bram Moolenaar33570922005-01-25 22:26:29 +000011230 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011231 s = expr;
11232 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011233 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011234 if (*s != NUL) /* check for trailing chars after expr */
11235 {
11236 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011237 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011238 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011239 }
11240 if (map)
11241 {
11242 /* map(): replace the list item value */
11243 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011244 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011245 *tv = rettv;
11246 }
11247 else
11248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011249 int error = FALSE;
11250
Bram Moolenaare9a41262005-01-15 22:18:47 +000011251 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011252 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011253 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011254 /* On type error, nothing has been removed; return FAIL to stop the
11255 * loop. The error message was given by get_tv_number_chk(). */
11256 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011257 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011258 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011259 retval = OK;
11260theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011261 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011262 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011263}
11264
11265/*
11266 * "filter()" function
11267 */
11268 static void
11269f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011270 typval_T *argvars;
11271 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011272{
11273 filter_map(argvars, rettv, FALSE);
11274}
11275
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011276/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011277 * "finddir({fname}[, {path}[, {count}]])" function
11278 */
11279 static void
11280f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011281 typval_T *argvars;
11282 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011283{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011284 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011285}
11286
11287/*
11288 * "findfile({fname}[, {path}[, {count}]])" function
11289 */
11290 static void
11291f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 typval_T *argvars;
11293 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011294{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011295 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011296}
11297
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011298#ifdef FEAT_FLOAT
11299/*
11300 * "float2nr({float})" function
11301 */
11302 static void
11303f_float2nr(argvars, rettv)
11304 typval_T *argvars;
11305 typval_T *rettv;
11306{
11307 float_T f;
11308
11309 if (get_float_arg(argvars, &f) == OK)
11310 {
11311 if (f < -0x7fffffff)
11312 rettv->vval.v_number = -0x7fffffff;
11313 else if (f > 0x7fffffff)
11314 rettv->vval.v_number = 0x7fffffff;
11315 else
11316 rettv->vval.v_number = (varnumber_T)f;
11317 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011318}
11319
11320/*
11321 * "floor({float})" function
11322 */
11323 static void
11324f_floor(argvars, rettv)
11325 typval_T *argvars;
11326 typval_T *rettv;
11327{
11328 float_T f;
11329
11330 rettv->v_type = VAR_FLOAT;
11331 if (get_float_arg(argvars, &f) == OK)
11332 rettv->vval.v_float = floor(f);
11333 else
11334 rettv->vval.v_float = 0.0;
11335}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011336
11337/*
11338 * "fmod()" function
11339 */
11340 static void
11341f_fmod(argvars, rettv)
11342 typval_T *argvars;
11343 typval_T *rettv;
11344{
11345 float_T fx, fy;
11346
11347 rettv->v_type = VAR_FLOAT;
11348 if (get_float_arg(argvars, &fx) == OK
11349 && get_float_arg(&argvars[1], &fy) == OK)
11350 rettv->vval.v_float = fmod(fx, fy);
11351 else
11352 rettv->vval.v_float = 0.0;
11353}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011354#endif
11355
Bram Moolenaar0d660222005-01-07 21:51:51 +000011356/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011357 * "fnameescape({string})" function
11358 */
11359 static void
11360f_fnameescape(argvars, rettv)
11361 typval_T *argvars;
11362 typval_T *rettv;
11363{
11364 rettv->vval.v_string = vim_strsave_fnameescape(
11365 get_tv_string(&argvars[0]), FALSE);
11366 rettv->v_type = VAR_STRING;
11367}
11368
11369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 * "fnamemodify({fname}, {mods})" function
11371 */
11372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011373f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011374 typval_T *argvars;
11375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376{
11377 char_u *fname;
11378 char_u *mods;
11379 int usedlen = 0;
11380 int len;
11381 char_u *fbuf = NULL;
11382 char_u buf[NUMBUFLEN];
11383
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011384 fname = get_tv_string_chk(&argvars[0]);
11385 mods = get_tv_string_buf_chk(&argvars[1], buf);
11386 if (fname == NULL || mods == NULL)
11387 fname = NULL;
11388 else
11389 {
11390 len = (int)STRLEN(fname);
11391 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011394 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011398 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 vim_free(fbuf);
11400}
11401
Bram Moolenaar33570922005-01-25 22:26:29 +000011402static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403
11404/*
11405 * "foldclosed()" function
11406 */
11407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011408foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011409 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011410 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011411 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412{
11413#ifdef FEAT_FOLDING
11414 linenr_T lnum;
11415 linenr_T first, last;
11416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11419 {
11420 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11421 {
11422 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011423 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 return;
11427 }
11428 }
11429#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011430 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431}
11432
11433/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011434 * "foldclosed()" function
11435 */
11436 static void
11437f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011438 typval_T *argvars;
11439 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011440{
11441 foldclosed_both(argvars, rettv, FALSE);
11442}
11443
11444/*
11445 * "foldclosedend()" function
11446 */
11447 static void
11448f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011449 typval_T *argvars;
11450 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011451{
11452 foldclosed_both(argvars, rettv, TRUE);
11453}
11454
11455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456 * "foldlevel()" function
11457 */
11458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011460 typval_T *argvars UNUSED;
11461 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462{
11463#ifdef FEAT_FOLDING
11464 linenr_T lnum;
11465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470}
11471
11472/*
11473 * "foldtext()" function
11474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011477 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479{
11480#ifdef FEAT_FOLDING
11481 linenr_T lnum;
11482 char_u *s;
11483 char_u *r;
11484 int len;
11485 char *txt;
11486#endif
11487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 rettv->v_type = VAR_STRING;
11489 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011491 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11492 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11493 <= curbuf->b_ml.ml_line_count
11494 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495 {
11496 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011497 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11498 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 {
11500 if (!linewhite(lnum))
11501 break;
11502 ++lnum;
11503 }
11504
11505 /* Find interesting text in this line. */
11506 s = skipwhite(ml_get(lnum));
11507 /* skip C comment-start */
11508 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011509 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011511 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011512 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011513 {
11514 s = skipwhite(ml_get(lnum + 1));
11515 if (*s == '*')
11516 s = skipwhite(s + 1);
11517 }
11518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519 txt = _("+-%s%3ld lines: ");
11520 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011521 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 + 20 /* for %3ld */
11523 + STRLEN(s))); /* concatenated */
11524 if (r != NULL)
11525 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011526 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11527 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11528 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529 len = (int)STRLEN(r);
11530 STRCAT(r, s);
11531 /* remove 'foldmarker' and 'commentstring' */
11532 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011533 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 }
11535 }
11536#endif
11537}
11538
11539/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011540 * "foldtextresult(lnum)" function
11541 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011543f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011544 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011545 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011546{
11547#ifdef FEAT_FOLDING
11548 linenr_T lnum;
11549 char_u *text;
11550 char_u buf[51];
11551 foldinfo_T foldinfo;
11552 int fold_count;
11553#endif
11554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011555 rettv->v_type = VAR_STRING;
11556 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011557#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 /* treat illegal types and illegal string values for {lnum} the same */
11560 if (lnum < 0)
11561 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011562 fold_count = foldedCount(curwin, lnum, &foldinfo);
11563 if (fold_count > 0)
11564 {
11565 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11566 &foldinfo, buf);
11567 if (text == buf)
11568 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011569 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011570 }
11571#endif
11572}
11573
11574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575 * "foreground()" function
11576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011578f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011579 typval_T *argvars UNUSED;
11580 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582#ifdef FEAT_GUI
11583 if (gui.in_use)
11584 gui_mch_set_foreground();
11585#else
11586# ifdef WIN32
11587 win32_set_foreground();
11588# endif
11589#endif
11590}
11591
11592/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011593 * "function()" function
11594 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011597 typval_T *argvars;
11598 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011599{
11600 char_u *s;
11601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011603 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011604 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011605 /* Don't check an autoload name for existence here. */
11606 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011607 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011608 else
11609 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011610 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011611 {
11612 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011613 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011614
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011615 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11616 * also be called from another script. Using trans_function_name()
11617 * would also work, but some plugins depend on the name being
11618 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011619 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011620 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011621 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011622 if (rettv->vval.v_string != NULL)
11623 {
11624 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011625 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011626 }
11627 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011628 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011629 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011631 }
11632}
11633
11634/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011635 * "garbagecollect()" function
11636 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011637 static void
11638f_garbagecollect(argvars, rettv)
11639 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011640 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011641{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011642 /* This is postponed until we are back at the toplevel, because we may be
11643 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11644 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011645
11646 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11647 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011648}
11649
11650/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011651 * "get()" function
11652 */
11653 static void
11654f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011655 typval_T *argvars;
11656 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011657{
Bram Moolenaar33570922005-01-25 22:26:29 +000011658 listitem_T *li;
11659 list_T *l;
11660 dictitem_T *di;
11661 dict_T *d;
11662 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011663
Bram Moolenaare9a41262005-01-15 22:18:47 +000011664 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011665 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011666 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011667 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011668 int error = FALSE;
11669
11670 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11671 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011672 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011673 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011674 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011675 else if (argvars[0].v_type == VAR_DICT)
11676 {
11677 if ((d = argvars[0].vval.v_dict) != NULL)
11678 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011679 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011680 if (di != NULL)
11681 tv = &di->di_tv;
11682 }
11683 }
11684 else
11685 EMSG2(_(e_listdictarg), "get()");
11686
11687 if (tv == NULL)
11688 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011689 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011690 copy_tv(&argvars[2], rettv);
11691 }
11692 else
11693 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011694}
11695
Bram Moolenaar342337a2005-07-21 21:11:17 +000011696static 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 +000011697
11698/*
11699 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011700 * Return a range (from start to end) of lines in rettv from the specified
11701 * buffer.
11702 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011703 */
11704 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011705get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011706 buf_T *buf;
11707 linenr_T start;
11708 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011709 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011710 typval_T *rettv;
11711{
11712 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011713
Bram Moolenaar959a1432013-12-14 12:17:38 +010011714 rettv->v_type = VAR_STRING;
11715 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011716 if (retlist && rettv_list_alloc(rettv) == FAIL)
11717 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011718
11719 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11720 return;
11721
11722 if (!retlist)
11723 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011724 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11725 p = ml_get_buf(buf, start, FALSE);
11726 else
11727 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011728 rettv->vval.v_string = vim_strsave(p);
11729 }
11730 else
11731 {
11732 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011733 return;
11734
11735 if (start < 1)
11736 start = 1;
11737 if (end > buf->b_ml.ml_line_count)
11738 end = buf->b_ml.ml_line_count;
11739 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011740 if (list_append_string(rettv->vval.v_list,
11741 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011742 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011743 }
11744}
11745
11746/*
11747 * "getbufline()" function
11748 */
11749 static void
11750f_getbufline(argvars, rettv)
11751 typval_T *argvars;
11752 typval_T *rettv;
11753{
11754 linenr_T lnum;
11755 linenr_T end;
11756 buf_T *buf;
11757
11758 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11759 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011760 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011761 --emsg_off;
11762
Bram Moolenaar661b1822005-07-28 22:36:45 +000011763 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011764 if (argvars[2].v_type == VAR_UNKNOWN)
11765 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011766 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011767 end = get_tv_lnum_buf(&argvars[2], buf);
11768
Bram Moolenaar342337a2005-07-21 21:11:17 +000011769 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011770}
11771
Bram Moolenaar0d660222005-01-07 21:51:51 +000011772/*
11773 * "getbufvar()" function
11774 */
11775 static void
11776f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011777 typval_T *argvars;
11778 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011779{
11780 buf_T *buf;
11781 buf_T *save_curbuf;
11782 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011783 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011784 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11787 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011788 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011789 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011790
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011791 rettv->v_type = VAR_STRING;
11792 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011793
11794 if (buf != NULL && varname != NULL)
11795 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011796 /* set curbuf to be our buf, temporarily */
11797 save_curbuf = curbuf;
11798 curbuf = buf;
11799
Bram Moolenaar0d660222005-01-07 21:51:51 +000011800 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011801 {
11802 if (get_option_tv(&varname, rettv, TRUE) == OK)
11803 done = TRUE;
11804 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011805 else if (STRCMP(varname, "changedtick") == 0)
11806 {
11807 rettv->v_type = VAR_NUMBER;
11808 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011809 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011810 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011811 else
11812 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011813 /* Look up the variable. */
11814 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11815 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11816 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011817 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011818 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011819 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011820 done = TRUE;
11821 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011822 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011823
11824 /* restore previous notion of curbuf */
11825 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011826 }
11827
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011828 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11829 /* use the default value */
11830 copy_tv(&argvars[2], rettv);
11831
Bram Moolenaar0d660222005-01-07 21:51:51 +000011832 --emsg_off;
11833}
11834
11835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 * "getchar()" function
11837 */
11838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011839f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011840 typval_T *argvars;
11841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842{
11843 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011844 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011846 /* Position the cursor. Needed after a message that ends in a space. */
11847 windgoto(msg_row, msg_col);
11848
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 ++no_mapping;
11850 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011851 for (;;)
11852 {
11853 if (argvars[0].v_type == VAR_UNKNOWN)
11854 /* getchar(): blocking wait. */
11855 n = safe_vgetc();
11856 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11857 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011858 n = vpeekc_any();
11859 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011860 /* illegal argument or getchar(0) and no char avail: return zero */
11861 n = 0;
11862 else
11863 /* getchar(0) and char avail: return char */
11864 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011865
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011866 if (n == K_IGNORE)
11867 continue;
11868 break;
11869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870 --no_mapping;
11871 --allow_keys;
11872
Bram Moolenaar219b8702006-11-01 14:32:36 +000011873 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11874 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11875 vimvars[VV_MOUSE_COL].vv_nr = 0;
11876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011877 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878 if (IS_SPECIAL(n) || mod_mask != 0)
11879 {
11880 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11881 int i = 0;
11882
11883 /* Turn a special key into three bytes, plus modifier. */
11884 if (mod_mask != 0)
11885 {
11886 temp[i++] = K_SPECIAL;
11887 temp[i++] = KS_MODIFIER;
11888 temp[i++] = mod_mask;
11889 }
11890 if (IS_SPECIAL(n))
11891 {
11892 temp[i++] = K_SPECIAL;
11893 temp[i++] = K_SECOND(n);
11894 temp[i++] = K_THIRD(n);
11895 }
11896#ifdef FEAT_MBYTE
11897 else if (has_mbyte)
11898 i += (*mb_char2bytes)(n, temp + i);
11899#endif
11900 else
11901 temp[i++] = n;
11902 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011903 rettv->v_type = VAR_STRING;
11904 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011905
11906#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011907 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011908 {
11909 int row = mouse_row;
11910 int col = mouse_col;
11911 win_T *win;
11912 linenr_T lnum;
11913# ifdef FEAT_WINDOWS
11914 win_T *wp;
11915# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011916 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011917
11918 if (row >= 0 && col >= 0)
11919 {
11920 /* Find the window at the mouse coordinates and compute the
11921 * text position. */
11922 win = mouse_find_win(&row, &col);
11923 (void)mouse_comp_pos(win, &row, &col, &lnum);
11924# ifdef FEAT_WINDOWS
11925 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011926 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011927# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011928 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011929 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11930 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11931 }
11932 }
11933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934 }
11935}
11936
11937/*
11938 * "getcharmod()" function
11939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011941f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011942 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011945 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946}
11947
11948/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011949 * "getcharsearch()" function
11950 */
11951 static void
11952f_getcharsearch(argvars, rettv)
11953 typval_T *argvars UNUSED;
11954 typval_T *rettv;
11955{
11956 if (rettv_dict_alloc(rettv) != FAIL)
11957 {
11958 dict_T *dict = rettv->vval.v_dict;
11959
11960 dict_add_nr_str(dict, "char", 0L, last_csearch());
11961 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11962 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11963 }
11964}
11965
11966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 * "getcmdline()" function
11968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011971 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974 rettv->v_type = VAR_STRING;
11975 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976}
11977
11978/*
11979 * "getcmdpos()" function
11980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011982f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011983 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011986 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987}
11988
11989/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011990 * "getcmdtype()" function
11991 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011992 static void
11993f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011994 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011995 typval_T *rettv;
11996{
11997 rettv->v_type = VAR_STRING;
11998 rettv->vval.v_string = alloc(2);
11999 if (rettv->vval.v_string != NULL)
12000 {
12001 rettv->vval.v_string[0] = get_cmdline_type();
12002 rettv->vval.v_string[1] = NUL;
12003 }
12004}
12005
12006/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012007 * "getcmdwintype()" function
12008 */
12009 static void
12010f_getcmdwintype(argvars, rettv)
12011 typval_T *argvars UNUSED;
12012 typval_T *rettv;
12013{
12014 rettv->v_type = VAR_STRING;
12015 rettv->vval.v_string = NULL;
12016#ifdef FEAT_CMDWIN
12017 rettv->vval.v_string = alloc(2);
12018 if (rettv->vval.v_string != NULL)
12019 {
12020 rettv->vval.v_string[0] = cmdwin_type;
12021 rettv->vval.v_string[1] = NUL;
12022 }
12023#endif
12024}
12025
12026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027 * "getcwd()" function
12028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012030f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012031 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033{
Bram Moolenaard9462e32011-04-11 21:35:11 +020012034 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012036 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012037 rettv->vval.v_string = NULL;
12038 cwd = alloc(MAXPATHL);
12039 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020012041 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12042 {
12043 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020012045 if (rettv->vval.v_string != NULL)
12046 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020012048 }
12049 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 }
12051}
12052
12053/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012054 * "getfontname()" function
12055 */
12056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057f_getfontname(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 Moolenaar46c9c732004-12-12 11:37:09 +000012060{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012061 rettv->v_type = VAR_STRING;
12062 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012063#ifdef FEAT_GUI
12064 if (gui.in_use)
12065 {
12066 GuiFont font;
12067 char_u *name = NULL;
12068
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012069 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012070 {
12071 /* Get the "Normal" font. Either the name saved by
12072 * hl_set_font_name() or from the font ID. */
12073 font = gui.norm_font;
12074 name = hl_get_font_name();
12075 }
12076 else
12077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012078 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012079 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12080 return;
12081 font = gui_mch_get_font(name, FALSE);
12082 if (font == NOFONT)
12083 return; /* Invalid font name, return empty string. */
12084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012085 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012086 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012087 gui_mch_free_font(font);
12088 }
12089#endif
12090}
12091
12092/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012093 * "getfperm({fname})" function
12094 */
12095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012096f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012097 typval_T *argvars;
12098 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012099{
12100 char_u *fname;
12101 struct stat st;
12102 char_u *perm = NULL;
12103 char_u flags[] = "rwx";
12104 int i;
12105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012109 if (mch_stat((char *)fname, &st) >= 0)
12110 {
12111 perm = vim_strsave((char_u *)"---------");
12112 if (perm != NULL)
12113 {
12114 for (i = 0; i < 9; i++)
12115 {
12116 if (st.st_mode & (1 << (8 - i)))
12117 perm[i] = flags[i % 3];
12118 }
12119 }
12120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012122}
12123
12124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125 * "getfsize({fname})" function
12126 */
12127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012128f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012129 typval_T *argvars;
12130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131{
12132 char_u *fname;
12133 struct stat st;
12134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012135 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012137 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012138
12139 if (mch_stat((char *)fname, &st) >= 0)
12140 {
12141 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012144 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012145 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012146
12147 /* non-perfect check for overflow */
12148 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12149 rettv->vval.v_number = -2;
12150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 }
12152 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012153 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154}
12155
12156/*
12157 * "getftime({fname})" function
12158 */
12159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012161 typval_T *argvars;
12162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163{
12164 char_u *fname;
12165 struct stat st;
12166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012167 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168
12169 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012170 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173}
12174
12175/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012176 * "getftype({fname})" function
12177 */
12178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012179f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012180 typval_T *argvars;
12181 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012182{
12183 char_u *fname;
12184 struct stat st;
12185 char_u *type = NULL;
12186 char *t;
12187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012188 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012190 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012191 if (mch_lstat((char *)fname, &st) >= 0)
12192 {
12193#ifdef S_ISREG
12194 if (S_ISREG(st.st_mode))
12195 t = "file";
12196 else if (S_ISDIR(st.st_mode))
12197 t = "dir";
12198# ifdef S_ISLNK
12199 else if (S_ISLNK(st.st_mode))
12200 t = "link";
12201# endif
12202# ifdef S_ISBLK
12203 else if (S_ISBLK(st.st_mode))
12204 t = "bdev";
12205# endif
12206# ifdef S_ISCHR
12207 else if (S_ISCHR(st.st_mode))
12208 t = "cdev";
12209# endif
12210# ifdef S_ISFIFO
12211 else if (S_ISFIFO(st.st_mode))
12212 t = "fifo";
12213# endif
12214# ifdef S_ISSOCK
12215 else if (S_ISSOCK(st.st_mode))
12216 t = "fifo";
12217# endif
12218 else
12219 t = "other";
12220#else
12221# ifdef S_IFMT
12222 switch (st.st_mode & S_IFMT)
12223 {
12224 case S_IFREG: t = "file"; break;
12225 case S_IFDIR: t = "dir"; break;
12226# ifdef S_IFLNK
12227 case S_IFLNK: t = "link"; break;
12228# endif
12229# ifdef S_IFBLK
12230 case S_IFBLK: t = "bdev"; break;
12231# endif
12232# ifdef S_IFCHR
12233 case S_IFCHR: t = "cdev"; break;
12234# endif
12235# ifdef S_IFIFO
12236 case S_IFIFO: t = "fifo"; break;
12237# endif
12238# ifdef S_IFSOCK
12239 case S_IFSOCK: t = "socket"; break;
12240# endif
12241 default: t = "other";
12242 }
12243# else
12244 if (mch_isdir(fname))
12245 t = "dir";
12246 else
12247 t = "file";
12248# endif
12249#endif
12250 type = vim_strsave((char_u *)t);
12251 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012252 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012253}
12254
12255/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012256 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012257 */
12258 static void
12259f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012260 typval_T *argvars;
12261 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012262{
12263 linenr_T lnum;
12264 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012265 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012266
12267 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012268 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012269 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012270 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012271 retlist = FALSE;
12272 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012273 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012274 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012275 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012276 retlist = TRUE;
12277 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012278
Bram Moolenaar342337a2005-07-21 21:11:17 +000012279 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012280}
12281
12282/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012283 * "getmatches()" function
12284 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012285 static void
12286f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012287 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012288 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012289{
12290#ifdef FEAT_SEARCH_EXTRA
12291 dict_T *dict;
12292 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012293 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012294
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012295 if (rettv_list_alloc(rettv) == OK)
12296 {
12297 while (cur != NULL)
12298 {
12299 dict = dict_alloc();
12300 if (dict == NULL)
12301 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012302 if (cur->match.regprog == NULL)
12303 {
12304 /* match added with matchaddpos() */
12305 for (i = 0; i < MAXPOSMATCH; ++i)
12306 {
12307 llpos_T *llpos;
12308 char buf[6];
12309 list_T *l;
12310
12311 llpos = &cur->pos.pos[i];
12312 if (llpos->lnum == 0)
12313 break;
12314 l = list_alloc();
12315 if (l == NULL)
12316 break;
12317 list_append_number(l, (varnumber_T)llpos->lnum);
12318 if (llpos->col > 0)
12319 {
12320 list_append_number(l, (varnumber_T)llpos->col);
12321 list_append_number(l, (varnumber_T)llpos->len);
12322 }
12323 sprintf(buf, "pos%d", i + 1);
12324 dict_add_list(dict, buf, l);
12325 }
12326 }
12327 else
12328 {
12329 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12330 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012331 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012332 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12333 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012334# ifdef FEAT_CONCEAL
12335 if (cur->conceal_char)
12336 {
12337 char_u buf[MB_MAXBYTES + 1];
12338
12339 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12340 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12341 }
12342# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012343 list_append_dict(rettv->vval.v_list, dict);
12344 cur = cur->next;
12345 }
12346 }
12347#endif
12348}
12349
12350/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012351 * "getpid()" function
12352 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012353 static void
12354f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012355 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012356 typval_T *rettv;
12357{
12358 rettv->vval.v_number = mch_get_pid();
12359}
12360
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012361static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12362
12363/*
12364 * "getcurpos()" function
12365 */
12366 static void
12367f_getcurpos(argvars, rettv)
12368 typval_T *argvars;
12369 typval_T *rettv;
12370{
12371 getpos_both(argvars, rettv, TRUE);
12372}
12373
Bram Moolenaar18081e32008-02-20 19:11:07 +000012374/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012375 * "getpos(string)" function
12376 */
12377 static void
12378f_getpos(argvars, rettv)
12379 typval_T *argvars;
12380 typval_T *rettv;
12381{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012382 getpos_both(argvars, rettv, FALSE);
12383}
12384
12385 static void
12386getpos_both(argvars, rettv, getcurpos)
12387 typval_T *argvars;
12388 typval_T *rettv;
12389 int getcurpos;
12390{
Bram Moolenaara5525202006-03-02 22:52:09 +000012391 pos_T *fp;
12392 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012393 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012394
12395 if (rettv_list_alloc(rettv) == OK)
12396 {
12397 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012398 if (getcurpos)
12399 fp = &curwin->w_cursor;
12400 else
12401 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012402 if (fnum != -1)
12403 list_append_number(l, (varnumber_T)fnum);
12404 else
12405 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012406 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12407 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012408 list_append_number(l, (fp != NULL)
12409 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012410 : (varnumber_T)0);
12411 list_append_number(l,
12412#ifdef FEAT_VIRTUALEDIT
12413 (fp != NULL) ? (varnumber_T)fp->coladd :
12414#endif
12415 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012416 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012417 list_append_number(l, curwin->w_curswant == MAXCOL ?
12418 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012419 }
12420 else
12421 rettv->vval.v_number = FALSE;
12422}
12423
12424/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012425 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012426 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012427 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012428f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012429 typval_T *argvars UNUSED;
12430 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012431{
12432#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012433 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012434#endif
12435
Bram Moolenaar2641f772005-03-25 21:58:17 +000012436#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012437 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012438 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012439 wp = NULL;
12440 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12441 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012442 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012443 if (wp == NULL)
12444 return;
12445 }
12446
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012447 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012448 }
12449#endif
12450}
12451
12452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453 * "getreg()" function
12454 */
12455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012456f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012457 typval_T *argvars;
12458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459{
12460 char_u *strregname;
12461 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012462 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012463 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012464 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012466 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012468 strregname = get_tv_string_chk(&argvars[0]);
12469 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012470 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012471 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012472 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012473 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12474 return_list = get_tv_number_chk(&argvars[2], &error);
12475 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012478 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012479
12480 if (error)
12481 return;
12482
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483 regname = (strregname == NULL ? '"' : *strregname);
12484 if (regname == 0)
12485 regname = '"';
12486
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012487 if (return_list)
12488 {
12489 rettv->v_type = VAR_LIST;
12490 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12491 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012492 if (rettv->vval.v_list != NULL)
12493 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012494 }
12495 else
12496 {
12497 rettv->v_type = VAR_STRING;
12498 rettv->vval.v_string = get_reg_contents(regname,
12499 arg2 ? GREG_EXPR_SRC : 0);
12500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501}
12502
12503/*
12504 * "getregtype()" function
12505 */
12506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012507f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012508 typval_T *argvars;
12509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510{
12511 char_u *strregname;
12512 int regname;
12513 char_u buf[NUMBUFLEN + 2];
12514 long reglen = 0;
12515
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012516 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012517 {
12518 strregname = get_tv_string_chk(&argvars[0]);
12519 if (strregname == NULL) /* type error; errmsg already given */
12520 {
12521 rettv->v_type = VAR_STRING;
12522 rettv->vval.v_string = NULL;
12523 return;
12524 }
12525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526 else
12527 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012528 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529
12530 regname = (strregname == NULL ? '"' : *strregname);
12531 if (regname == 0)
12532 regname = '"';
12533
12534 buf[0] = NUL;
12535 buf[1] = NUL;
12536 switch (get_reg_type(regname, &reglen))
12537 {
12538 case MLINE: buf[0] = 'V'; break;
12539 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540 case MBLOCK:
12541 buf[0] = Ctrl_V;
12542 sprintf((char *)buf + 1, "%ld", reglen + 1);
12543 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012545 rettv->v_type = VAR_STRING;
12546 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547}
12548
12549/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012550 * "gettabvar()" function
12551 */
12552 static void
12553f_gettabvar(argvars, rettv)
12554 typval_T *argvars;
12555 typval_T *rettv;
12556{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012557 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012558 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012559 dictitem_T *v;
12560 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012561 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012562
12563 rettv->v_type = VAR_STRING;
12564 rettv->vval.v_string = NULL;
12565
12566 varname = get_tv_string_chk(&argvars[1]);
12567 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12568 if (tp != NULL && varname != NULL)
12569 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012570 /* Set tp to be our tabpage, temporarily. Also set the window to the
12571 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012572 if (switch_win(&oldcurwin, &oldtabpage,
12573 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012574 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012575 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012576 /* look up the variable */
12577 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12578 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12579 if (v != NULL)
12580 {
12581 copy_tv(&v->di_tv, rettv);
12582 done = TRUE;
12583 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012584 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012585
12586 /* restore previous notion of curwin */
12587 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012588 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012589
12590 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012591 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012592}
12593
12594/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012595 * "gettabwinvar()" function
12596 */
12597 static void
12598f_gettabwinvar(argvars, rettv)
12599 typval_T *argvars;
12600 typval_T *rettv;
12601{
12602 getwinvar(argvars, rettv, 1);
12603}
12604
12605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606 * "getwinposx()" function
12607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012609f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012610 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012613 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614#ifdef FEAT_GUI
12615 if (gui.in_use)
12616 {
12617 int x, y;
12618
12619 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012620 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621 }
12622#endif
12623}
12624
12625/*
12626 * "getwinposy()" function
12627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012629f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012630 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012633 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634#ifdef FEAT_GUI
12635 if (gui.in_use)
12636 {
12637 int x, y;
12638
12639 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012640 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641 }
12642#endif
12643}
12644
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012645/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012646 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012647 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012648 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012649find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012650 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012651 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012652{
12653#ifdef FEAT_WINDOWS
12654 win_T *wp;
12655#endif
12656 int nr;
12657
12658 nr = get_tv_number_chk(vp, NULL);
12659
12660#ifdef FEAT_WINDOWS
12661 if (nr < 0)
12662 return NULL;
12663 if (nr == 0)
12664 return curwin;
12665
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012666 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12667 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012668 if (--nr <= 0)
12669 break;
12670 return wp;
12671#else
12672 if (nr == 0 || nr == 1)
12673 return curwin;
12674 return NULL;
12675#endif
12676}
12677
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678/*
12679 * "getwinvar()" function
12680 */
12681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012683 typval_T *argvars;
12684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012686 getwinvar(argvars, rettv, 0);
12687}
12688
12689/*
12690 * getwinvar() and gettabwinvar()
12691 */
12692 static void
12693getwinvar(argvars, rettv, off)
12694 typval_T *argvars;
12695 typval_T *rettv;
12696 int off; /* 1 for gettabwinvar() */
12697{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012698 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012700 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012701 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012702 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012703#ifdef FEAT_WINDOWS
12704 win_T *oldcurwin;
12705 tabpage_T *oldtabpage;
12706 int need_switch_win;
12707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012709#ifdef FEAT_WINDOWS
12710 if (off == 1)
12711 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12712 else
12713 tp = curtab;
12714#endif
12715 win = find_win_by_nr(&argvars[off], tp);
12716 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012717 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012719 rettv->v_type = VAR_STRING;
12720 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721
12722 if (win != NULL && varname != NULL)
12723 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012724#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012725 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012726 * otherwise the window is not valid. Only do this when needed,
12727 * autocommands get blocked. */
12728 need_switch_win = !(tp == curtab && win == curwin);
12729 if (!need_switch_win
12730 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12731#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012732 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012733 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012734 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012735 if (get_option_tv(&varname, rettv, 1) == OK)
12736 done = TRUE;
12737 }
12738 else
12739 {
12740 /* Look up the variable. */
12741 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12742 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12743 varname, FALSE);
12744 if (v != NULL)
12745 {
12746 copy_tv(&v->di_tv, rettv);
12747 done = TRUE;
12748 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012751
Bram Moolenaarba117c22015-09-29 16:53:22 +020012752#ifdef FEAT_WINDOWS
12753 if (need_switch_win)
12754 /* restore previous notion of curwin */
12755 restore_win(oldcurwin, oldtabpage, TRUE);
12756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757 }
12758
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012759 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12760 /* use the default return value */
12761 copy_tv(&argvars[off + 2], rettv);
12762
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 --emsg_off;
12764}
12765
12766/*
12767 * "glob()" function
12768 */
12769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012771 typval_T *argvars;
12772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012774 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012776 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012778 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012779 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012780 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012781 if (argvars[1].v_type != VAR_UNKNOWN)
12782 {
12783 if (get_tv_number_chk(&argvars[1], &error))
12784 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012785 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012786 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012787 if (get_tv_number_chk(&argvars[2], &error))
12788 {
12789 rettv->v_type = VAR_LIST;
12790 rettv->vval.v_list = NULL;
12791 }
12792 if (argvars[3].v_type != VAR_UNKNOWN
12793 && get_tv_number_chk(&argvars[3], &error))
12794 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012795 }
12796 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012797 if (!error)
12798 {
12799 ExpandInit(&xpc);
12800 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012801 if (p_wic)
12802 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012803 if (rettv->v_type == VAR_STRING)
12804 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012805 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012806 else if (rettv_list_alloc(rettv) != FAIL)
12807 {
12808 int i;
12809
12810 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12811 NULL, options, WILD_ALL_KEEP);
12812 for (i = 0; i < xpc.xp_numfiles; i++)
12813 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12814
12815 ExpandCleanup(&xpc);
12816 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012817 }
12818 else
12819 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820}
12821
12822/*
12823 * "globpath()" function
12824 */
12825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012826f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012827 typval_T *argvars;
12828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012830 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012832 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012833 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012834 garray_T ga;
12835 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012837 /* When the optional second argument is non-zero, don't remove matches
12838 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012840 if (argvars[2].v_type != VAR_UNKNOWN)
12841 {
12842 if (get_tv_number_chk(&argvars[2], &error))
12843 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012844 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012845 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012846 if (get_tv_number_chk(&argvars[3], &error))
12847 {
12848 rettv->v_type = VAR_LIST;
12849 rettv->vval.v_list = NULL;
12850 }
12851 if (argvars[4].v_type != VAR_UNKNOWN
12852 && get_tv_number_chk(&argvars[4], &error))
12853 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012854 }
12855 }
12856 if (file != NULL && !error)
12857 {
12858 ga_init2(&ga, (int)sizeof(char_u *), 10);
12859 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12860 if (rettv->v_type == VAR_STRING)
12861 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12862 else if (rettv_list_alloc(rettv) != FAIL)
12863 for (i = 0; i < ga.ga_len; ++i)
12864 list_append_string(rettv->vval.v_list,
12865 ((char_u **)(ga.ga_data))[i], -1);
12866 ga_clear_strings(&ga);
12867 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012868 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012869 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870}
12871
12872/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012873 * "glob2regpat()" function
12874 */
12875 static void
12876f_glob2regpat(argvars, rettv)
12877 typval_T *argvars;
12878 typval_T *rettv;
12879{
12880 char_u *pat = get_tv_string_chk(&argvars[0]);
12881
12882 rettv->v_type = VAR_STRING;
12883 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12884}
12885
12886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887 * "has()" function
12888 */
12889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012890f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012891 typval_T *argvars;
12892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893{
12894 int i;
12895 char_u *name;
12896 int n = FALSE;
12897 static char *(has_list[]) =
12898 {
12899#ifdef AMIGA
12900 "amiga",
12901# ifdef FEAT_ARP
12902 "arp",
12903# endif
12904#endif
12905#ifdef __BEOS__
12906 "beos",
12907#endif
12908#ifdef MSDOS
12909# ifdef DJGPP
12910 "dos32",
12911# else
12912 "dos16",
12913# endif
12914#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012915#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916 "mac",
12917#endif
12918#if defined(MACOS_X_UNIX)
12919 "macunix",
12920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921#ifdef __QNX__
12922 "qnx",
12923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924#ifdef UNIX
12925 "unix",
12926#endif
12927#ifdef VMS
12928 "vms",
12929#endif
12930#ifdef WIN16
12931 "win16",
12932#endif
12933#ifdef WIN32
12934 "win32",
12935#endif
12936#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12937 "win32unix",
12938#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012939#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 "win64",
12941#endif
12942#ifdef EBCDIC
12943 "ebcdic",
12944#endif
12945#ifndef CASE_INSENSITIVE_FILENAME
12946 "fname_case",
12947#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012948#ifdef HAVE_ACL
12949 "acl",
12950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951#ifdef FEAT_ARABIC
12952 "arabic",
12953#endif
12954#ifdef FEAT_AUTOCMD
12955 "autocmd",
12956#endif
12957#ifdef FEAT_BEVAL
12958 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012959# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12960 "balloon_multiline",
12961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962#endif
12963#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12964 "builtin_terms",
12965# ifdef ALL_BUILTIN_TCAPS
12966 "all_builtin_terms",
12967# endif
12968#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012969#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12970 || defined(FEAT_GUI_W32) \
12971 || defined(FEAT_GUI_MOTIF))
12972 "browsefilter",
12973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974#ifdef FEAT_BYTEOFF
12975 "byte_offset",
12976#endif
12977#ifdef FEAT_CINDENT
12978 "cindent",
12979#endif
12980#ifdef FEAT_CLIENTSERVER
12981 "clientserver",
12982#endif
12983#ifdef FEAT_CLIPBOARD
12984 "clipboard",
12985#endif
12986#ifdef FEAT_CMDL_COMPL
12987 "cmdline_compl",
12988#endif
12989#ifdef FEAT_CMDHIST
12990 "cmdline_hist",
12991#endif
12992#ifdef FEAT_COMMENTS
12993 "comments",
12994#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012995#ifdef FEAT_CONCEAL
12996 "conceal",
12997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998#ifdef FEAT_CRYPT
12999 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013000 "crypt-blowfish",
13001 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002#endif
13003#ifdef FEAT_CSCOPE
13004 "cscope",
13005#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013006#ifdef FEAT_CURSORBIND
13007 "cursorbind",
13008#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013009#ifdef CURSOR_SHAPE
13010 "cursorshape",
13011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012#ifdef DEBUG
13013 "debug",
13014#endif
13015#ifdef FEAT_CON_DIALOG
13016 "dialog_con",
13017#endif
13018#ifdef FEAT_GUI_DIALOG
13019 "dialog_gui",
13020#endif
13021#ifdef FEAT_DIFF
13022 "diff",
13023#endif
13024#ifdef FEAT_DIGRAPHS
13025 "digraphs",
13026#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013027#ifdef FEAT_DIRECTX
13028 "directx",
13029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030#ifdef FEAT_DND
13031 "dnd",
13032#endif
13033#ifdef FEAT_EMACS_TAGS
13034 "emacs_tags",
13035#endif
13036 "eval", /* always present, of course! */
13037#ifdef FEAT_EX_EXTRA
13038 "ex_extra",
13039#endif
13040#ifdef FEAT_SEARCH_EXTRA
13041 "extra_search",
13042#endif
13043#ifdef FEAT_FKMAP
13044 "farsi",
13045#endif
13046#ifdef FEAT_SEARCHPATH
13047 "file_in_path",
13048#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013049#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013050 "filterpipe",
13051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052#ifdef FEAT_FIND_ID
13053 "find_in_path",
13054#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013055#ifdef FEAT_FLOAT
13056 "float",
13057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058#ifdef FEAT_FOLDING
13059 "folding",
13060#endif
13061#ifdef FEAT_FOOTER
13062 "footer",
13063#endif
13064#if !defined(USE_SYSTEM) && defined(UNIX)
13065 "fork",
13066#endif
13067#ifdef FEAT_GETTEXT
13068 "gettext",
13069#endif
13070#ifdef FEAT_GUI
13071 "gui",
13072#endif
13073#ifdef FEAT_GUI_ATHENA
13074# ifdef FEAT_GUI_NEXTAW
13075 "gui_neXtaw",
13076# else
13077 "gui_athena",
13078# endif
13079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080#ifdef FEAT_GUI_GTK
13081 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013084#ifdef FEAT_GUI_GNOME
13085 "gui_gnome",
13086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087#ifdef FEAT_GUI_MAC
13088 "gui_mac",
13089#endif
13090#ifdef FEAT_GUI_MOTIF
13091 "gui_motif",
13092#endif
13093#ifdef FEAT_GUI_PHOTON
13094 "gui_photon",
13095#endif
13096#ifdef FEAT_GUI_W16
13097 "gui_win16",
13098#endif
13099#ifdef FEAT_GUI_W32
13100 "gui_win32",
13101#endif
13102#ifdef FEAT_HANGULIN
13103 "hangul_input",
13104#endif
13105#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13106 "iconv",
13107#endif
13108#ifdef FEAT_INS_EXPAND
13109 "insert_expand",
13110#endif
13111#ifdef FEAT_JUMPLIST
13112 "jumplist",
13113#endif
13114#ifdef FEAT_KEYMAP
13115 "keymap",
13116#endif
13117#ifdef FEAT_LANGMAP
13118 "langmap",
13119#endif
13120#ifdef FEAT_LIBCALL
13121 "libcall",
13122#endif
13123#ifdef FEAT_LINEBREAK
13124 "linebreak",
13125#endif
13126#ifdef FEAT_LISP
13127 "lispindent",
13128#endif
13129#ifdef FEAT_LISTCMDS
13130 "listcmds",
13131#endif
13132#ifdef FEAT_LOCALMAP
13133 "localmap",
13134#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013135#ifdef FEAT_LUA
13136# ifndef DYNAMIC_LUA
13137 "lua",
13138# endif
13139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140#ifdef FEAT_MENU
13141 "menu",
13142#endif
13143#ifdef FEAT_SESSION
13144 "mksession",
13145#endif
13146#ifdef FEAT_MODIFY_FNAME
13147 "modify_fname",
13148#endif
13149#ifdef FEAT_MOUSE
13150 "mouse",
13151#endif
13152#ifdef FEAT_MOUSESHAPE
13153 "mouseshape",
13154#endif
13155#if defined(UNIX) || defined(VMS)
13156# ifdef FEAT_MOUSE_DEC
13157 "mouse_dec",
13158# endif
13159# ifdef FEAT_MOUSE_GPM
13160 "mouse_gpm",
13161# endif
13162# ifdef FEAT_MOUSE_JSB
13163 "mouse_jsbterm",
13164# endif
13165# ifdef FEAT_MOUSE_NET
13166 "mouse_netterm",
13167# endif
13168# ifdef FEAT_MOUSE_PTERM
13169 "mouse_pterm",
13170# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013171# ifdef FEAT_MOUSE_SGR
13172 "mouse_sgr",
13173# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013174# ifdef FEAT_SYSMOUSE
13175 "mouse_sysmouse",
13176# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013177# ifdef FEAT_MOUSE_URXVT
13178 "mouse_urxvt",
13179# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180# ifdef FEAT_MOUSE_XTERM
13181 "mouse_xterm",
13182# endif
13183#endif
13184#ifdef FEAT_MBYTE
13185 "multi_byte",
13186#endif
13187#ifdef FEAT_MBYTE_IME
13188 "multi_byte_ime",
13189#endif
13190#ifdef FEAT_MULTI_LANG
13191 "multi_lang",
13192#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013193#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013194#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013195 "mzscheme",
13196#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198#ifdef FEAT_OLE
13199 "ole",
13200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201#ifdef FEAT_PATH_EXTRA
13202 "path_extra",
13203#endif
13204#ifdef FEAT_PERL
13205#ifndef DYNAMIC_PERL
13206 "perl",
13207#endif
13208#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013209#ifdef FEAT_PERSISTENT_UNDO
13210 "persistent_undo",
13211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212#ifdef FEAT_PYTHON
13213#ifndef DYNAMIC_PYTHON
13214 "python",
13215#endif
13216#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013217#ifdef FEAT_PYTHON3
13218#ifndef DYNAMIC_PYTHON3
13219 "python3",
13220#endif
13221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222#ifdef FEAT_POSTSCRIPT
13223 "postscript",
13224#endif
13225#ifdef FEAT_PRINTER
13226 "printer",
13227#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013228#ifdef FEAT_PROFILE
13229 "profile",
13230#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013231#ifdef FEAT_RELTIME
13232 "reltime",
13233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234#ifdef FEAT_QUICKFIX
13235 "quickfix",
13236#endif
13237#ifdef FEAT_RIGHTLEFT
13238 "rightleft",
13239#endif
13240#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13241 "ruby",
13242#endif
13243#ifdef FEAT_SCROLLBIND
13244 "scrollbind",
13245#endif
13246#ifdef FEAT_CMDL_INFO
13247 "showcmd",
13248 "cmdline_info",
13249#endif
13250#ifdef FEAT_SIGNS
13251 "signs",
13252#endif
13253#ifdef FEAT_SMARTINDENT
13254 "smartindent",
13255#endif
13256#ifdef FEAT_SNIFF
13257 "sniff",
13258#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013259#ifdef STARTUPTIME
13260 "startuptime",
13261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262#ifdef FEAT_STL_OPT
13263 "statusline",
13264#endif
13265#ifdef FEAT_SUN_WORKSHOP
13266 "sun_workshop",
13267#endif
13268#ifdef FEAT_NETBEANS_INTG
13269 "netbeans_intg",
13270#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013271#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013272 "spell",
13273#endif
13274#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 "syntax",
13276#endif
13277#if defined(USE_SYSTEM) || !defined(UNIX)
13278 "system",
13279#endif
13280#ifdef FEAT_TAG_BINS
13281 "tag_binary",
13282#endif
13283#ifdef FEAT_TAG_OLDSTATIC
13284 "tag_old_static",
13285#endif
13286#ifdef FEAT_TAG_ANYWHITE
13287 "tag_any_white",
13288#endif
13289#ifdef FEAT_TCL
13290# ifndef DYNAMIC_TCL
13291 "tcl",
13292# endif
13293#endif
13294#ifdef TERMINFO
13295 "terminfo",
13296#endif
13297#ifdef FEAT_TERMRESPONSE
13298 "termresponse",
13299#endif
13300#ifdef FEAT_TEXTOBJ
13301 "textobjects",
13302#endif
13303#ifdef HAVE_TGETENT
13304 "tgetent",
13305#endif
13306#ifdef FEAT_TITLE
13307 "title",
13308#endif
13309#ifdef FEAT_TOOLBAR
13310 "toolbar",
13311#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013312#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13313 "unnamedplus",
13314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315#ifdef FEAT_USR_CMDS
13316 "user-commands", /* was accidentally included in 5.4 */
13317 "user_commands",
13318#endif
13319#ifdef FEAT_VIMINFO
13320 "viminfo",
13321#endif
13322#ifdef FEAT_VERTSPLIT
13323 "vertsplit",
13324#endif
13325#ifdef FEAT_VIRTUALEDIT
13326 "virtualedit",
13327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329#ifdef FEAT_VISUALEXTRA
13330 "visualextra",
13331#endif
13332#ifdef FEAT_VREPLACE
13333 "vreplace",
13334#endif
13335#ifdef FEAT_WILDIGN
13336 "wildignore",
13337#endif
13338#ifdef FEAT_WILDMENU
13339 "wildmenu",
13340#endif
13341#ifdef FEAT_WINDOWS
13342 "windows",
13343#endif
13344#ifdef FEAT_WAK
13345 "winaltkeys",
13346#endif
13347#ifdef FEAT_WRITEBACKUP
13348 "writebackup",
13349#endif
13350#ifdef FEAT_XIM
13351 "xim",
13352#endif
13353#ifdef FEAT_XFONTSET
13354 "xfontset",
13355#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013356#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013357 "xpm",
13358 "xpm_w32", /* for backward compatibility */
13359#else
13360# if defined(HAVE_XPM)
13361 "xpm",
13362# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364#ifdef USE_XSMP
13365 "xsmp",
13366#endif
13367#ifdef USE_XSMP_INTERACT
13368 "xsmp_interact",
13369#endif
13370#ifdef FEAT_XCLIPBOARD
13371 "xterm_clipboard",
13372#endif
13373#ifdef FEAT_XTERM_SAVE
13374 "xterm_save",
13375#endif
13376#if defined(UNIX) && defined(FEAT_X11)
13377 "X11",
13378#endif
13379 NULL
13380 };
13381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013382 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383 for (i = 0; has_list[i] != NULL; ++i)
13384 if (STRICMP(name, has_list[i]) == 0)
13385 {
13386 n = TRUE;
13387 break;
13388 }
13389
13390 if (n == FALSE)
13391 {
13392 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013393 {
13394 if (name[5] == '-'
13395 && STRLEN(name) > 11
13396 && vim_isdigit(name[6])
13397 && vim_isdigit(name[8])
13398 && vim_isdigit(name[10]))
13399 {
13400 int major = atoi((char *)name + 6);
13401 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013402
13403 /* Expect "patch-9.9.01234". */
13404 n = (major < VIM_VERSION_MAJOR
13405 || (major == VIM_VERSION_MAJOR
13406 && (minor < VIM_VERSION_MINOR
13407 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013408 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013409 }
13410 else
13411 n = has_patch(atoi((char *)name + 5));
13412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 else if (STRICMP(name, "vim_starting") == 0)
13414 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013415#ifdef FEAT_MBYTE
13416 else if (STRICMP(name, "multi_byte_encoding") == 0)
13417 n = has_mbyte;
13418#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013419#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13420 else if (STRICMP(name, "balloon_multiline") == 0)
13421 n = multiline_balloon_available();
13422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423#ifdef DYNAMIC_TCL
13424 else if (STRICMP(name, "tcl") == 0)
13425 n = tcl_enabled(FALSE);
13426#endif
13427#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13428 else if (STRICMP(name, "iconv") == 0)
13429 n = iconv_enabled(FALSE);
13430#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013431#ifdef DYNAMIC_LUA
13432 else if (STRICMP(name, "lua") == 0)
13433 n = lua_enabled(FALSE);
13434#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013435#ifdef DYNAMIC_MZSCHEME
13436 else if (STRICMP(name, "mzscheme") == 0)
13437 n = mzscheme_enabled(FALSE);
13438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439#ifdef DYNAMIC_RUBY
13440 else if (STRICMP(name, "ruby") == 0)
13441 n = ruby_enabled(FALSE);
13442#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013443#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444#ifdef DYNAMIC_PYTHON
13445 else if (STRICMP(name, "python") == 0)
13446 n = python_enabled(FALSE);
13447#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013448#endif
13449#ifdef FEAT_PYTHON3
13450#ifdef DYNAMIC_PYTHON3
13451 else if (STRICMP(name, "python3") == 0)
13452 n = python3_enabled(FALSE);
13453#endif
13454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455#ifdef DYNAMIC_PERL
13456 else if (STRICMP(name, "perl") == 0)
13457 n = perl_enabled(FALSE);
13458#endif
13459#ifdef FEAT_GUI
13460 else if (STRICMP(name, "gui_running") == 0)
13461 n = (gui.in_use || gui.starting);
13462# ifdef FEAT_GUI_W32
13463 else if (STRICMP(name, "gui_win32s") == 0)
13464 n = gui_is_win32s();
13465# endif
13466# ifdef FEAT_BROWSE
13467 else if (STRICMP(name, "browse") == 0)
13468 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13469# endif
13470#endif
13471#ifdef FEAT_SYN_HL
13472 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013473 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474#endif
13475#if defined(WIN3264)
13476 else if (STRICMP(name, "win95") == 0)
13477 n = mch_windows95();
13478#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013479#ifdef FEAT_NETBEANS_INTG
13480 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013481 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483 }
13484
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013485 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486}
13487
13488/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013489 * "has_key()" function
13490 */
13491 static void
13492f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013493 typval_T *argvars;
13494 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013495{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013496 if (argvars[0].v_type != VAR_DICT)
13497 {
13498 EMSG(_(e_dictreq));
13499 return;
13500 }
13501 if (argvars[0].vval.v_dict == NULL)
13502 return;
13503
13504 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013505 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013506}
13507
13508/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013509 * "haslocaldir()" function
13510 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013511 static void
13512f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013513 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013514 typval_T *rettv;
13515{
13516 rettv->vval.v_number = (curwin->w_localdir != NULL);
13517}
13518
13519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 * "hasmapto()" function
13521 */
13522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013523f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013524 typval_T *argvars;
13525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526{
13527 char_u *name;
13528 char_u *mode;
13529 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013530 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013532 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013533 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 mode = (char_u *)"nvo";
13535 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013537 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013538 if (argvars[2].v_type != VAR_UNKNOWN)
13539 abbr = get_tv_number(&argvars[2]);
13540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541
Bram Moolenaar2c932302006-03-18 21:42:09 +000013542 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013543 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013545 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546}
13547
13548/*
13549 * "histadd()" function
13550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013552f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013553 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555{
13556#ifdef FEAT_CMDHIST
13557 int histype;
13558 char_u *str;
13559 char_u buf[NUMBUFLEN];
13560#endif
13561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013562 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563 if (check_restricted() || check_secure())
13564 return;
13565#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013566 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13567 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568 if (histype >= 0)
13569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013570 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 if (*str != NUL)
13572 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013573 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013575 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576 return;
13577 }
13578 }
13579#endif
13580}
13581
13582/*
13583 * "histdel()" function
13584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013586f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013587 typval_T *argvars UNUSED;
13588 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589{
13590#ifdef FEAT_CMDHIST
13591 int n;
13592 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013593 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013595 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13596 if (str == NULL)
13597 n = 0;
13598 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013600 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013601 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013603 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013604 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 else
13606 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013607 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013608 get_tv_string_buf(&argvars[1], buf));
13609 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610#endif
13611}
13612
13613/*
13614 * "histget()" function
13615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013618 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620{
13621#ifdef FEAT_CMDHIST
13622 int type;
13623 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013624 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013626 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13627 if (str == NULL)
13628 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013630 {
13631 type = get_histtype(str);
13632 if (argvars[1].v_type == VAR_UNKNOWN)
13633 idx = get_history_idx(type);
13634 else
13635 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13636 /* -1 on type error */
13637 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643}
13644
13645/*
13646 * "histnr()" function
13647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013650 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652{
13653 int i;
13654
13655#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013656 char_u *history = get_tv_string_chk(&argvars[0]);
13657
13658 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 if (i >= HIST_CMD && i < HIST_COUNT)
13660 i = get_history_idx(i);
13661 else
13662#endif
13663 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013664 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665}
13666
13667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 * "highlightID(name)" function
13669 */
13670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013672 typval_T *argvars;
13673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013675 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676}
13677
13678/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013679 * "highlight_exists()" function
13680 */
13681 static void
13682f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013683 typval_T *argvars;
13684 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013685{
13686 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13687}
13688
13689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 * "hostname()" function
13691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013693f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013694 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696{
13697 char_u hostname[256];
13698
13699 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013700 rettv->v_type = VAR_STRING;
13701 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702}
13703
13704/*
13705 * iconv() function
13706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013708f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013709 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711{
13712#ifdef FEAT_MBYTE
13713 char_u buf1[NUMBUFLEN];
13714 char_u buf2[NUMBUFLEN];
13715 char_u *from, *to, *str;
13716 vimconv_T vimconv;
13717#endif
13718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013719 rettv->v_type = VAR_STRING;
13720 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721
13722#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013723 str = get_tv_string(&argvars[0]);
13724 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13725 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726 vimconv.vc_type = CONV_NONE;
13727 convert_setup(&vimconv, from, to);
13728
13729 /* If the encodings are equal, no conversion needed. */
13730 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013731 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734
13735 convert_setup(&vimconv, NULL, NULL);
13736 vim_free(from);
13737 vim_free(to);
13738#endif
13739}
13740
13741/*
13742 * "indent()" function
13743 */
13744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013745f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013746 typval_T *argvars;
13747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748{
13749 linenr_T lnum;
13750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013751 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756}
13757
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013758/*
13759 * "index()" function
13760 */
13761 static void
13762f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013763 typval_T *argvars;
13764 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013765{
Bram Moolenaar33570922005-01-25 22:26:29 +000013766 list_T *l;
13767 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013768 long idx = 0;
13769 int ic = FALSE;
13770
13771 rettv->vval.v_number = -1;
13772 if (argvars[0].v_type != VAR_LIST)
13773 {
13774 EMSG(_(e_listreq));
13775 return;
13776 }
13777 l = argvars[0].vval.v_list;
13778 if (l != NULL)
13779 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013780 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013781 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013783 int error = FALSE;
13784
Bram Moolenaar758711c2005-02-02 23:11:38 +000013785 /* Start at specified item. Use the cached index that list_find()
13786 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013787 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013788 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013789 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013790 ic = get_tv_number_chk(&argvars[3], &error);
13791 if (error)
13792 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013793 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013794
Bram Moolenaar758711c2005-02-02 23:11:38 +000013795 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013796 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013797 {
13798 rettv->vval.v_number = idx;
13799 break;
13800 }
13801 }
13802}
13803
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804static int inputsecret_flag = 0;
13805
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013806static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13807
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013809 * This function is used by f_input() and f_inputdialog() functions. The third
13810 * argument to f_input() specifies the type of completion to use at the
13811 * prompt. The third argument to f_inputdialog() specifies the value to return
13812 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 */
13814 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013815get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013816 typval_T *argvars;
13817 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013818 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013820 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 char_u *p = NULL;
13822 int c;
13823 char_u buf[NUMBUFLEN];
13824 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013825 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013826 int xp_type = EXPAND_NOTHING;
13827 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013829 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013830 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831
13832#ifdef NO_CONSOLE_INPUT
13833 /* While starting up, there is no place to enter text. */
13834 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836#endif
13837
13838 cmd_silent = FALSE; /* Want to see the prompt. */
13839 if (prompt != NULL)
13840 {
13841 /* Only the part of the message after the last NL is considered as
13842 * prompt for the command line */
13843 p = vim_strrchr(prompt, '\n');
13844 if (p == NULL)
13845 p = prompt;
13846 else
13847 {
13848 ++p;
13849 c = *p;
13850 *p = NUL;
13851 msg_start();
13852 msg_clr_eos();
13853 msg_puts_attr(prompt, echo_attr);
13854 msg_didout = FALSE;
13855 msg_starthere();
13856 *p = c;
13857 }
13858 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013860 if (argvars[1].v_type != VAR_UNKNOWN)
13861 {
13862 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13863 if (defstr != NULL)
13864 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013866 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013867 {
13868 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013869 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013870 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013871
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013872 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013873 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013874
Bram Moolenaar4463f292005-09-25 22:20:24 +000013875 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13876 if (xp_name == NULL)
13877 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013878
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013879 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013880
Bram Moolenaar4463f292005-09-25 22:20:24 +000013881 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13882 &xp_arg) == FAIL)
13883 return;
13884 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013885 }
13886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013887 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013888 {
13889# ifdef FEAT_EX_EXTRA
13890 int save_ex_normal_busy = ex_normal_busy;
13891 ex_normal_busy = 0;
13892# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013893 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013894 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13895 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013896# ifdef FEAT_EX_EXTRA
13897 ex_normal_busy = save_ex_normal_busy;
13898# endif
13899 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013900 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013901 && argvars[1].v_type != VAR_UNKNOWN
13902 && argvars[2].v_type != VAR_UNKNOWN)
13903 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13904 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013905
13906 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013908 /* since the user typed this, no need to wait for return */
13909 need_wait_return = FALSE;
13910 msg_didout = FALSE;
13911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912 cmd_silent = cmd_silent_save;
13913}
13914
13915/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013916 * "input()" function
13917 * Also handles inputsecret() when inputsecret is set.
13918 */
13919 static void
13920f_input(argvars, rettv)
13921 typval_T *argvars;
13922 typval_T *rettv;
13923{
13924 get_user_input(argvars, rettv, FALSE);
13925}
13926
13927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928 * "inputdialog()" function
13929 */
13930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013931f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013932 typval_T *argvars;
13933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934{
13935#if defined(FEAT_GUI_TEXTDIALOG)
13936 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13937 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13938 {
13939 char_u *message;
13940 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013941 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013943 message = get_tv_string_chk(&argvars[0]);
13944 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013945 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013946 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 else
13948 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013949 if (message != NULL && defstr != NULL
13950 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013951 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013952 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953 else
13954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013955 if (message != NULL && defstr != NULL
13956 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013957 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013958 rettv->vval.v_string = vim_strsave(
13959 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013961 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013963 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964 }
13965 else
13966#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013967 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968}
13969
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013970/*
13971 * "inputlist()" function
13972 */
13973 static void
13974f_inputlist(argvars, rettv)
13975 typval_T *argvars;
13976 typval_T *rettv;
13977{
13978 listitem_T *li;
13979 int selected;
13980 int mouse_used;
13981
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013982#ifdef NO_CONSOLE_INPUT
13983 /* While starting up, there is no place to enter text. */
13984 if (no_console_input())
13985 return;
13986#endif
13987 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13988 {
13989 EMSG2(_(e_listarg), "inputlist()");
13990 return;
13991 }
13992
13993 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013994 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013995 lines_left = Rows; /* avoid more prompt */
13996 msg_scroll = TRUE;
13997 msg_clr_eos();
13998
13999 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14000 {
14001 msg_puts(get_tv_string(&li->li_tv));
14002 msg_putchar('\n');
14003 }
14004
14005 /* Ask for choice. */
14006 selected = prompt_for_number(&mouse_used);
14007 if (mouse_used)
14008 selected -= lines_left;
14009
14010 rettv->vval.v_number = selected;
14011}
14012
14013
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14015
14016/*
14017 * "inputrestore()" function
14018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014020f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014021 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023{
14024 if (ga_userinput.ga_len > 0)
14025 {
14026 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14028 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014029 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030 }
14031 else if (p_verbose > 1)
14032 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014033 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014034 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 }
14036}
14037
14038/*
14039 * "inputsave()" function
14040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014042f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014043 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014046 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047 if (ga_grow(&ga_userinput, 1) == OK)
14048 {
14049 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14050 + ga_userinput.ga_len);
14051 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014052 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053 }
14054 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014055 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056}
14057
14058/*
14059 * "inputsecret()" function
14060 */
14061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014062f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014063 typval_T *argvars;
14064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065{
14066 ++cmdline_star;
14067 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014068 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 --cmdline_star;
14070 --inputsecret_flag;
14071}
14072
14073/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014074 * "insert()" function
14075 */
14076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014077f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014078 typval_T *argvars;
14079 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014080{
14081 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014082 listitem_T *item;
14083 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014084 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014085
14086 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014087 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014088 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014089 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014090 {
14091 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014092 before = get_tv_number_chk(&argvars[2], &error);
14093 if (error)
14094 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014095
Bram Moolenaar758711c2005-02-02 23:11:38 +000014096 if (before == l->lv_len)
14097 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014098 else
14099 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014100 item = list_find(l, before);
14101 if (item == NULL)
14102 {
14103 EMSGN(_(e_listidx), before);
14104 l = NULL;
14105 }
14106 }
14107 if (l != NULL)
14108 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014109 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014110 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014111 }
14112 }
14113}
14114
14115/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014116 * "invert(expr)" function
14117 */
14118 static void
14119f_invert(argvars, rettv)
14120 typval_T *argvars;
14121 typval_T *rettv;
14122{
14123 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14124}
14125
14126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 * "isdirectory()" function
14128 */
14129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014130f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014131 typval_T *argvars;
14132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014134 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135}
14136
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014137/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014138 * "islocked()" function
14139 */
14140 static void
14141f_islocked(argvars, rettv)
14142 typval_T *argvars;
14143 typval_T *rettv;
14144{
14145 lval_T lv;
14146 char_u *end;
14147 dictitem_T *di;
14148
14149 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014150 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14151 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014152 if (end != NULL && lv.ll_name != NULL)
14153 {
14154 if (*end != NUL)
14155 EMSG(_(e_trailing));
14156 else
14157 {
14158 if (lv.ll_tv == NULL)
14159 {
14160 if (check_changedtick(lv.ll_name))
14161 rettv->vval.v_number = 1; /* always locked */
14162 else
14163 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014164 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014165 if (di != NULL)
14166 {
14167 /* Consider a variable locked when:
14168 * 1. the variable itself is locked
14169 * 2. the value of the variable is locked.
14170 * 3. the List or Dict value is locked.
14171 */
14172 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14173 || tv_islocked(&di->di_tv));
14174 }
14175 }
14176 }
14177 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014178 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014179 else if (lv.ll_newkey != NULL)
14180 EMSG2(_(e_dictkey), lv.ll_newkey);
14181 else if (lv.ll_list != NULL)
14182 /* List item. */
14183 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14184 else
14185 /* Dictionary item. */
14186 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14187 }
14188 }
14189
14190 clear_lval(&lv);
14191}
14192
Bram Moolenaar33570922005-01-25 22:26:29 +000014193static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014194
14195/*
14196 * Turn a dict into a list:
14197 * "what" == 0: list of keys
14198 * "what" == 1: list of values
14199 * "what" == 2: list of items
14200 */
14201 static void
14202dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014203 typval_T *argvars;
14204 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014205 int what;
14206{
Bram Moolenaar33570922005-01-25 22:26:29 +000014207 list_T *l2;
14208 dictitem_T *di;
14209 hashitem_T *hi;
14210 listitem_T *li;
14211 listitem_T *li2;
14212 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014213 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014214
Bram Moolenaar8c711452005-01-14 21:53:12 +000014215 if (argvars[0].v_type != VAR_DICT)
14216 {
14217 EMSG(_(e_dictreq));
14218 return;
14219 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014220 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014221 return;
14222
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014223 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014224 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014225
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014226 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014227 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014228 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014229 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014230 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014231 --todo;
14232 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014233
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014234 li = listitem_alloc();
14235 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014236 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014237 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014238
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014239 if (what == 0)
14240 {
14241 /* keys() */
14242 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014243 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014244 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14245 }
14246 else if (what == 1)
14247 {
14248 /* values() */
14249 copy_tv(&di->di_tv, &li->li_tv);
14250 }
14251 else
14252 {
14253 /* items() */
14254 l2 = list_alloc();
14255 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014256 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014257 li->li_tv.vval.v_list = l2;
14258 if (l2 == NULL)
14259 break;
14260 ++l2->lv_refcount;
14261
14262 li2 = listitem_alloc();
14263 if (li2 == NULL)
14264 break;
14265 list_append(l2, li2);
14266 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014267 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014268 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14269
14270 li2 = listitem_alloc();
14271 if (li2 == NULL)
14272 break;
14273 list_append(l2, li2);
14274 copy_tv(&di->di_tv, &li2->li_tv);
14275 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014276 }
14277 }
14278}
14279
14280/*
14281 * "items(dict)" function
14282 */
14283 static void
14284f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014285 typval_T *argvars;
14286 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014287{
14288 dict_list(argvars, rettv, 2);
14289}
14290
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014292 * "join()" function
14293 */
14294 static void
14295f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014296 typval_T *argvars;
14297 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014298{
14299 garray_T ga;
14300 char_u *sep;
14301
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014302 if (argvars[0].v_type != VAR_LIST)
14303 {
14304 EMSG(_(e_listreq));
14305 return;
14306 }
14307 if (argvars[0].vval.v_list == NULL)
14308 return;
14309 if (argvars[1].v_type == VAR_UNKNOWN)
14310 sep = (char_u *)" ";
14311 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014312 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014313
14314 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014315
14316 if (sep != NULL)
14317 {
14318 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014319 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014320 ga_append(&ga, NUL);
14321 rettv->vval.v_string = (char_u *)ga.ga_data;
14322 }
14323 else
14324 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014325}
14326
14327/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014328 * "keys()" function
14329 */
14330 static void
14331f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014332 typval_T *argvars;
14333 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014334{
14335 dict_list(argvars, rettv, 0);
14336}
14337
14338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 * "last_buffer_nr()" function.
14340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014342f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014343 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345{
14346 int n = 0;
14347 buf_T *buf;
14348
14349 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14350 if (n < buf->b_fnum)
14351 n = buf->b_fnum;
14352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014353 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014354}
14355
14356/*
14357 * "len()" function
14358 */
14359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014360f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014361 typval_T *argvars;
14362 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014363{
14364 switch (argvars[0].v_type)
14365 {
14366 case VAR_STRING:
14367 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368 rettv->vval.v_number = (varnumber_T)STRLEN(
14369 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014370 break;
14371 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014372 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014373 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014374 case VAR_DICT:
14375 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14376 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014377 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014378 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014379 break;
14380 }
14381}
14382
Bram Moolenaar33570922005-01-25 22:26:29 +000014383static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014384
14385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014386libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014387 typval_T *argvars;
14388 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014389 int type;
14390{
14391#ifdef FEAT_LIBCALL
14392 char_u *string_in;
14393 char_u **string_result;
14394 int nr_result;
14395#endif
14396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014397 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014398 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014399 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014400
14401 if (check_restricted() || check_secure())
14402 return;
14403
14404#ifdef FEAT_LIBCALL
14405 /* The first two args must be strings, otherwise its meaningless */
14406 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14407 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014408 string_in = NULL;
14409 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014410 string_in = argvars[2].vval.v_string;
14411 if (type == VAR_NUMBER)
14412 string_result = NULL;
14413 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014414 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014415 if (mch_libcall(argvars[0].vval.v_string,
14416 argvars[1].vval.v_string,
14417 string_in,
14418 argvars[2].vval.v_number,
14419 string_result,
14420 &nr_result) == OK
14421 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014422 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014423 }
14424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425}
14426
14427/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014428 * "libcall()" function
14429 */
14430 static void
14431f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014432 typval_T *argvars;
14433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014434{
14435 libcall_common(argvars, rettv, VAR_STRING);
14436}
14437
14438/*
14439 * "libcallnr()" function
14440 */
14441 static void
14442f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014443 typval_T *argvars;
14444 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014445{
14446 libcall_common(argvars, rettv, VAR_NUMBER);
14447}
14448
14449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450 * "line(string)" function
14451 */
14452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014453f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014454 typval_T *argvars;
14455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456{
14457 linenr_T lnum = 0;
14458 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014459 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014461 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 if (fp != NULL)
14463 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465}
14466
14467/*
14468 * "line2byte(lnum)" function
14469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014471f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014472 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474{
14475#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014476 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477#else
14478 linenr_T lnum;
14479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014480 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014482 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014484 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14485 if (rettv->vval.v_number >= 0)
14486 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487#endif
14488}
14489
14490/*
14491 * "lispindent(lnum)" function
14492 */
14493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014494f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014495 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497{
14498#ifdef FEAT_LISP
14499 pos_T pos;
14500 linenr_T lnum;
14501
14502 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014503 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14505 {
14506 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014507 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508 curwin->w_cursor = pos;
14509 }
14510 else
14511#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014512 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513}
14514
14515/*
14516 * "localtime()" function
14517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014519f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014520 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014523 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524}
14525
Bram Moolenaar33570922005-01-25 22:26:29 +000014526static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527
14528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014529get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014530 typval_T *argvars;
14531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 int exact;
14533{
14534 char_u *keys;
14535 char_u *which;
14536 char_u buf[NUMBUFLEN];
14537 char_u *keys_buf = NULL;
14538 char_u *rhs;
14539 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014540 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014541 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014542 mapblock_T *mp;
14543 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544
14545 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014546 rettv->v_type = VAR_STRING;
14547 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014549 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550 if (*keys == NUL)
14551 return;
14552
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014553 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014555 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014556 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014557 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014558 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014559 if (argvars[3].v_type != VAR_UNKNOWN)
14560 get_dict = get_tv_number(&argvars[3]);
14561 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563 else
14564 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014565 if (which == NULL)
14566 return;
14567
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568 mode = get_map_mode(&which, 0);
14569
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014570 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014571 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014572 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014573
14574 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014576 /* Return a string. */
14577 if (rhs != NULL)
14578 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579
Bram Moolenaarbd743252010-10-20 21:23:33 +020014580 }
14581 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14582 {
14583 /* Return a dictionary. */
14584 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14585 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14586 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587
Bram Moolenaarbd743252010-10-20 21:23:33 +020014588 dict_add_nr_str(dict, "lhs", 0L, lhs);
14589 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14590 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14591 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14592 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14593 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14594 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014595 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014596 dict_add_nr_str(dict, "mode", 0L, mapmode);
14597
14598 vim_free(lhs);
14599 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600 }
14601}
14602
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014603#ifdef FEAT_FLOAT
14604/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014605 * "log()" function
14606 */
14607 static void
14608f_log(argvars, rettv)
14609 typval_T *argvars;
14610 typval_T *rettv;
14611{
14612 float_T f;
14613
14614 rettv->v_type = VAR_FLOAT;
14615 if (get_float_arg(argvars, &f) == OK)
14616 rettv->vval.v_float = log(f);
14617 else
14618 rettv->vval.v_float = 0.0;
14619}
14620
14621/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014622 * "log10()" function
14623 */
14624 static void
14625f_log10(argvars, rettv)
14626 typval_T *argvars;
14627 typval_T *rettv;
14628{
14629 float_T f;
14630
14631 rettv->v_type = VAR_FLOAT;
14632 if (get_float_arg(argvars, &f) == OK)
14633 rettv->vval.v_float = log10(f);
14634 else
14635 rettv->vval.v_float = 0.0;
14636}
14637#endif
14638
Bram Moolenaar1dced572012-04-05 16:54:08 +020014639#ifdef FEAT_LUA
14640/*
14641 * "luaeval()" function
14642 */
14643 static void
14644f_luaeval(argvars, rettv)
14645 typval_T *argvars;
14646 typval_T *rettv;
14647{
14648 char_u *str;
14649 char_u buf[NUMBUFLEN];
14650
14651 str = get_tv_string_buf(&argvars[0], buf);
14652 do_luaeval(str, argvars + 1, rettv);
14653}
14654#endif
14655
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014657 * "map()" function
14658 */
14659 static void
14660f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014661 typval_T *argvars;
14662 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014663{
14664 filter_map(argvars, rettv, TRUE);
14665}
14666
14667/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014668 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 */
14670 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014671f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014672 typval_T *argvars;
14673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014675 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676}
14677
14678/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014679 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680 */
14681 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014682f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014683 typval_T *argvars;
14684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014686 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687}
14688
Bram Moolenaar33570922005-01-25 22:26:29 +000014689static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690
14691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014692find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014693 typval_T *argvars;
14694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695 int type;
14696{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014697 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014698 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014699 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700 char_u *pat;
14701 regmatch_T regmatch;
14702 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014703 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704 char_u *save_cpo;
14705 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014706 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014707 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014708 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014709 list_T *l = NULL;
14710 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014711 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014712 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713
14714 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14715 save_cpo = p_cpo;
14716 p_cpo = (char_u *)"";
14717
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014718 rettv->vval.v_number = -1;
14719 if (type == 3)
14720 {
14721 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014722 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014723 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014724 }
14725 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014727 rettv->v_type = VAR_STRING;
14728 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014731 if (argvars[0].v_type == VAR_LIST)
14732 {
14733 if ((l = argvars[0].vval.v_list) == NULL)
14734 goto theend;
14735 li = l->lv_first;
14736 }
14737 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014738 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014739 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014740 len = (long)STRLEN(str);
14741 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014743 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14744 if (pat == NULL)
14745 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014746
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014747 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014749 int error = FALSE;
14750
14751 start = get_tv_number_chk(&argvars[2], &error);
14752 if (error)
14753 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014754 if (l != NULL)
14755 {
14756 li = list_find(l, start);
14757 if (li == NULL)
14758 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014759 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014760 }
14761 else
14762 {
14763 if (start < 0)
14764 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014765 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014766 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014767 /* When "count" argument is there ignore matches before "start",
14768 * otherwise skip part of the string. Differs when pattern is "^"
14769 * or "\<". */
14770 if (argvars[3].v_type != VAR_UNKNOWN)
14771 startcol = start;
14772 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014773 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014774 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014775 len -= start;
14776 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014777 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014778
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014779 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014780 nth = get_tv_number_chk(&argvars[3], &error);
14781 if (error)
14782 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014783 }
14784
14785 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14786 if (regmatch.regprog != NULL)
14787 {
14788 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014789
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014790 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014791 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014792 if (l != NULL)
14793 {
14794 if (li == NULL)
14795 {
14796 match = FALSE;
14797 break;
14798 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014799 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014800 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014801 if (str == NULL)
14802 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014803 }
14804
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014805 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014806
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014807 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014808 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014809 if (l == NULL && !match)
14810 break;
14811
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014812 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014813 if (l != NULL)
14814 {
14815 li = li->li_next;
14816 ++idx;
14817 }
14818 else
14819 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014820#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014821 startcol = (colnr_T)(regmatch.startp[0]
14822 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014823#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014824 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014825#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014826 if (startcol > (colnr_T)len
14827 || str + startcol <= regmatch.startp[0])
14828 {
14829 match = FALSE;
14830 break;
14831 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014832 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014833 }
14834
14835 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014837 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014838 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014839 int i;
14840
14841 /* return list with matched string and submatches */
14842 for (i = 0; i < NSUBEXP; ++i)
14843 {
14844 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014845 {
14846 if (list_append_string(rettv->vval.v_list,
14847 (char_u *)"", 0) == FAIL)
14848 break;
14849 }
14850 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014851 regmatch.startp[i],
14852 (int)(regmatch.endp[i] - regmatch.startp[i]))
14853 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014854 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014855 }
14856 }
14857 else if (type == 2)
14858 {
14859 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014860 if (l != NULL)
14861 copy_tv(&li->li_tv, rettv);
14862 else
14863 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014865 }
14866 else if (l != NULL)
14867 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868 else
14869 {
14870 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014871 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872 (varnumber_T)(regmatch.startp[0] - str);
14873 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014874 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014876 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014877 }
14878 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014879 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 }
14881
14882theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014883 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014884 p_cpo = save_cpo;
14885}
14886
14887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014888 * "match()" function
14889 */
14890 static void
14891f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014892 typval_T *argvars;
14893 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014894{
14895 find_some_match(argvars, rettv, 1);
14896}
14897
14898/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014899 * "matchadd()" function
14900 */
14901 static void
14902f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014903 typval_T *argvars UNUSED;
14904 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014905{
14906#ifdef FEAT_SEARCH_EXTRA
14907 char_u buf[NUMBUFLEN];
14908 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14909 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14910 int prio = 10; /* default priority */
14911 int id = -1;
14912 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014913 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014914
14915 rettv->vval.v_number = -1;
14916
14917 if (grp == NULL || pat == NULL)
14918 return;
14919 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014920 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014921 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014922 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014923 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014924 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014925 if (argvars[4].v_type != VAR_UNKNOWN)
14926 {
14927 if (argvars[4].v_type != VAR_DICT)
14928 {
14929 EMSG(_(e_dictreq));
14930 return;
14931 }
14932 if (dict_find(argvars[4].vval.v_dict,
14933 (char_u *)"conceal", -1) != NULL)
14934 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14935 (char_u *)"conceal", FALSE);
14936 }
14937 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014938 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014939 if (error == TRUE)
14940 return;
14941 if (id >= 1 && id <= 3)
14942 {
14943 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14944 return;
14945 }
14946
Bram Moolenaar6561d522015-07-21 15:48:27 +020014947 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14948 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014949#endif
14950}
14951
14952/*
14953 * "matchaddpos()" function
14954 */
14955 static void
14956f_matchaddpos(argvars, rettv)
14957 typval_T *argvars UNUSED;
14958 typval_T *rettv UNUSED;
14959{
14960#ifdef FEAT_SEARCH_EXTRA
14961 char_u buf[NUMBUFLEN];
14962 char_u *group;
14963 int prio = 10;
14964 int id = -1;
14965 int error = FALSE;
14966 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014967 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014968
14969 rettv->vval.v_number = -1;
14970
14971 group = get_tv_string_buf_chk(&argvars[0], buf);
14972 if (group == NULL)
14973 return;
14974
14975 if (argvars[1].v_type != VAR_LIST)
14976 {
14977 EMSG2(_(e_listarg), "matchaddpos()");
14978 return;
14979 }
14980 l = argvars[1].vval.v_list;
14981 if (l == NULL)
14982 return;
14983
14984 if (argvars[2].v_type != VAR_UNKNOWN)
14985 {
14986 prio = get_tv_number_chk(&argvars[2], &error);
14987 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014988 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014989 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014990 if (argvars[4].v_type != VAR_UNKNOWN)
14991 {
14992 if (argvars[4].v_type != VAR_DICT)
14993 {
14994 EMSG(_(e_dictreq));
14995 return;
14996 }
14997 if (dict_find(argvars[4].vval.v_dict,
14998 (char_u *)"conceal", -1) != NULL)
14999 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15000 (char_u *)"conceal", FALSE);
15001 }
15002 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015003 }
15004 if (error == TRUE)
15005 return;
15006
15007 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15008 if (id == 1 || id == 2)
15009 {
15010 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15011 return;
15012 }
15013
Bram Moolenaar6561d522015-07-21 15:48:27 +020015014 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15015 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015016#endif
15017}
15018
15019/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015020 * "matcharg()" function
15021 */
15022 static void
15023f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015024 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015025 typval_T *rettv;
15026{
15027 if (rettv_list_alloc(rettv) == OK)
15028 {
15029#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015030 int id = get_tv_number(&argvars[0]);
15031 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015032
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015033 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015034 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015035 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15036 {
15037 list_append_string(rettv->vval.v_list,
15038 syn_id2name(m->hlg_id), -1);
15039 list_append_string(rettv->vval.v_list, m->pattern, -1);
15040 }
15041 else
15042 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015043 list_append_string(rettv->vval.v_list, NULL, -1);
15044 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015045 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015046 }
15047#endif
15048 }
15049}
15050
15051/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015052 * "matchdelete()" function
15053 */
15054 static void
15055f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015056 typval_T *argvars UNUSED;
15057 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015058{
15059#ifdef FEAT_SEARCH_EXTRA
15060 rettv->vval.v_number = match_delete(curwin,
15061 (int)get_tv_number(&argvars[0]), TRUE);
15062#endif
15063}
15064
15065/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015066 * "matchend()" function
15067 */
15068 static void
15069f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015070 typval_T *argvars;
15071 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015072{
15073 find_some_match(argvars, rettv, 0);
15074}
15075
15076/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015077 * "matchlist()" function
15078 */
15079 static void
15080f_matchlist(argvars, rettv)
15081 typval_T *argvars;
15082 typval_T *rettv;
15083{
15084 find_some_match(argvars, rettv, 3);
15085}
15086
15087/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015088 * "matchstr()" function
15089 */
15090 static void
15091f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015092 typval_T *argvars;
15093 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015094{
15095 find_some_match(argvars, rettv, 2);
15096}
15097
Bram Moolenaar33570922005-01-25 22:26:29 +000015098static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015099
15100 static void
15101max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015102 typval_T *argvars;
15103 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015104 int domax;
15105{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015106 long n = 0;
15107 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015108 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015109
15110 if (argvars[0].v_type == VAR_LIST)
15111 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015112 list_T *l;
15113 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015114
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015115 l = argvars[0].vval.v_list;
15116 if (l != NULL)
15117 {
15118 li = l->lv_first;
15119 if (li != NULL)
15120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015121 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015122 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015123 {
15124 li = li->li_next;
15125 if (li == NULL)
15126 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015127 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015128 if (domax ? i > n : i < n)
15129 n = i;
15130 }
15131 }
15132 }
15133 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015134 else if (argvars[0].v_type == VAR_DICT)
15135 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015136 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015137 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015138 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015139 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015140
15141 d = argvars[0].vval.v_dict;
15142 if (d != NULL)
15143 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015144 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015145 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015146 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015147 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015148 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015149 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015150 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015151 if (first)
15152 {
15153 n = i;
15154 first = FALSE;
15155 }
15156 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015157 n = i;
15158 }
15159 }
15160 }
15161 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015162 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015163 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015164 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015165}
15166
15167/*
15168 * "max()" function
15169 */
15170 static void
15171f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015172 typval_T *argvars;
15173 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015174{
15175 max_min(argvars, rettv, TRUE);
15176}
15177
15178/*
15179 * "min()" function
15180 */
15181 static void
15182f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015183 typval_T *argvars;
15184 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015185{
15186 max_min(argvars, rettv, FALSE);
15187}
15188
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015189static int mkdir_recurse __ARGS((char_u *dir, int prot));
15190
15191/*
15192 * Create the directory in which "dir" is located, and higher levels when
15193 * needed.
15194 */
15195 static int
15196mkdir_recurse(dir, prot)
15197 char_u *dir;
15198 int prot;
15199{
15200 char_u *p;
15201 char_u *updir;
15202 int r = FAIL;
15203
15204 /* Get end of directory name in "dir".
15205 * We're done when it's "/" or "c:/". */
15206 p = gettail_sep(dir);
15207 if (p <= get_past_head(dir))
15208 return OK;
15209
15210 /* If the directory exists we're done. Otherwise: create it.*/
15211 updir = vim_strnsave(dir, (int)(p - dir));
15212 if (updir == NULL)
15213 return FAIL;
15214 if (mch_isdir(updir))
15215 r = OK;
15216 else if (mkdir_recurse(updir, prot) == OK)
15217 r = vim_mkdir_emsg(updir, prot);
15218 vim_free(updir);
15219 return r;
15220}
15221
15222#ifdef vim_mkdir
15223/*
15224 * "mkdir()" function
15225 */
15226 static void
15227f_mkdir(argvars, rettv)
15228 typval_T *argvars;
15229 typval_T *rettv;
15230{
15231 char_u *dir;
15232 char_u buf[NUMBUFLEN];
15233 int prot = 0755;
15234
15235 rettv->vval.v_number = FAIL;
15236 if (check_restricted() || check_secure())
15237 return;
15238
15239 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015240 if (*dir == NUL)
15241 rettv->vval.v_number = FAIL;
15242 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015243 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015244 if (*gettail(dir) == NUL)
15245 /* remove trailing slashes */
15246 *gettail_sep(dir) = NUL;
15247
15248 if (argvars[1].v_type != VAR_UNKNOWN)
15249 {
15250 if (argvars[2].v_type != VAR_UNKNOWN)
15251 prot = get_tv_number_chk(&argvars[2], NULL);
15252 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15253 mkdir_recurse(dir, prot);
15254 }
15255 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015256 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015257}
15258#endif
15259
Bram Moolenaar0d660222005-01-07 21:51:51 +000015260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261 * "mode()" function
15262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015264f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015265 typval_T *argvars;
15266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015268 char_u buf[3];
15269
15270 buf[1] = NUL;
15271 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273 if (VIsual_active)
15274 {
15275 if (VIsual_select)
15276 buf[0] = VIsual_mode + 's' - 'v';
15277 else
15278 buf[0] = VIsual_mode;
15279 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015280 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015281 || State == CONFIRM)
15282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015284 if (State == ASKMORE)
15285 buf[1] = 'm';
15286 else if (State == CONFIRM)
15287 buf[1] = '?';
15288 }
15289 else if (State == EXTERNCMD)
15290 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291 else if (State & INSERT)
15292 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015293#ifdef FEAT_VREPLACE
15294 if (State & VREPLACE_FLAG)
15295 {
15296 buf[0] = 'R';
15297 buf[1] = 'v';
15298 }
15299 else
15300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 if (State & REPLACE_FLAG)
15302 buf[0] = 'R';
15303 else
15304 buf[0] = 'i';
15305 }
15306 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015309 if (exmode_active)
15310 buf[1] = 'v';
15311 }
15312 else if (exmode_active)
15313 {
15314 buf[0] = 'c';
15315 buf[1] = 'e';
15316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015320 if (finish_op)
15321 buf[1] = 'o';
15322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015324 /* Clear out the minor mode when the argument is not a non-zero number or
15325 * non-empty string. */
15326 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015327 buf[1] = NUL;
15328
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015329 rettv->vval.v_string = vim_strsave(buf);
15330 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331}
15332
Bram Moolenaar429fa852013-04-15 12:27:36 +020015333#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015334/*
15335 * "mzeval()" function
15336 */
15337 static void
15338f_mzeval(argvars, rettv)
15339 typval_T *argvars;
15340 typval_T *rettv;
15341{
15342 char_u *str;
15343 char_u buf[NUMBUFLEN];
15344
15345 str = get_tv_string_buf(&argvars[0], buf);
15346 do_mzeval(str, rettv);
15347}
Bram Moolenaar75676462013-01-30 14:55:42 +010015348
15349 void
15350mzscheme_call_vim(name, args, rettv)
15351 char_u *name;
15352 typval_T *args;
15353 typval_T *rettv;
15354{
15355 typval_T argvars[3];
15356
15357 argvars[0].v_type = VAR_STRING;
15358 argvars[0].vval.v_string = name;
15359 copy_tv(args, &argvars[1]);
15360 argvars[2].v_type = VAR_UNKNOWN;
15361 f_call(argvars, rettv);
15362 clear_tv(&argvars[1]);
15363}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015364#endif
15365
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015367 * "nextnonblank()" function
15368 */
15369 static void
15370f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015371 typval_T *argvars;
15372 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015373{
15374 linenr_T lnum;
15375
15376 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015378 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015379 {
15380 lnum = 0;
15381 break;
15382 }
15383 if (*skipwhite(ml_get(lnum)) != NUL)
15384 break;
15385 }
15386 rettv->vval.v_number = lnum;
15387}
15388
15389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390 * "nr2char()" function
15391 */
15392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015393f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015394 typval_T *argvars;
15395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396{
15397 char_u buf[NUMBUFLEN];
15398
15399#ifdef FEAT_MBYTE
15400 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015401 {
15402 int utf8 = 0;
15403
15404 if (argvars[1].v_type != VAR_UNKNOWN)
15405 utf8 = get_tv_number_chk(&argvars[1], NULL);
15406 if (utf8)
15407 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15408 else
15409 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411 else
15412#endif
15413 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015414 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415 buf[1] = NUL;
15416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015417 rettv->v_type = VAR_STRING;
15418 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015419}
15420
15421/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015422 * "or(expr, expr)" function
15423 */
15424 static void
15425f_or(argvars, rettv)
15426 typval_T *argvars;
15427 typval_T *rettv;
15428{
15429 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15430 | get_tv_number_chk(&argvars[1], NULL);
15431}
15432
15433/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015434 * "pathshorten()" function
15435 */
15436 static void
15437f_pathshorten(argvars, rettv)
15438 typval_T *argvars;
15439 typval_T *rettv;
15440{
15441 char_u *p;
15442
15443 rettv->v_type = VAR_STRING;
15444 p = get_tv_string_chk(&argvars[0]);
15445 if (p == NULL)
15446 rettv->vval.v_string = NULL;
15447 else
15448 {
15449 p = vim_strsave(p);
15450 rettv->vval.v_string = p;
15451 if (p != NULL)
15452 shorten_dir(p);
15453 }
15454}
15455
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015456#ifdef FEAT_FLOAT
15457/*
15458 * "pow()" function
15459 */
15460 static void
15461f_pow(argvars, rettv)
15462 typval_T *argvars;
15463 typval_T *rettv;
15464{
15465 float_T fx, fy;
15466
15467 rettv->v_type = VAR_FLOAT;
15468 if (get_float_arg(argvars, &fx) == OK
15469 && get_float_arg(&argvars[1], &fy) == OK)
15470 rettv->vval.v_float = pow(fx, fy);
15471 else
15472 rettv->vval.v_float = 0.0;
15473}
15474#endif
15475
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015476/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015477 * "prevnonblank()" function
15478 */
15479 static void
15480f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015481 typval_T *argvars;
15482 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015483{
15484 linenr_T lnum;
15485
15486 lnum = get_tv_lnum(argvars);
15487 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15488 lnum = 0;
15489 else
15490 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15491 --lnum;
15492 rettv->vval.v_number = lnum;
15493}
15494
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015495#ifdef HAVE_STDARG_H
15496/* This dummy va_list is here because:
15497 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15498 * - locally in the function results in a "used before set" warning
15499 * - using va_start() to initialize it gives "function with fixed args" error */
15500static va_list ap;
15501#endif
15502
Bram Moolenaar8c711452005-01-14 21:53:12 +000015503/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015504 * "printf()" function
15505 */
15506 static void
15507f_printf(argvars, rettv)
15508 typval_T *argvars;
15509 typval_T *rettv;
15510{
15511 rettv->v_type = VAR_STRING;
15512 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015513#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015514 {
15515 char_u buf[NUMBUFLEN];
15516 int len;
15517 char_u *s;
15518 int saved_did_emsg = did_emsg;
15519 char *fmt;
15520
15521 /* Get the required length, allocate the buffer and do it for real. */
15522 did_emsg = FALSE;
15523 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015524 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015525 if (!did_emsg)
15526 {
15527 s = alloc(len + 1);
15528 if (s != NULL)
15529 {
15530 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015531 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015532 }
15533 }
15534 did_emsg |= saved_did_emsg;
15535 }
15536#endif
15537}
15538
15539/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015540 * "pumvisible()" function
15541 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015542 static void
15543f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015544 typval_T *argvars UNUSED;
15545 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015546{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015547#ifdef FEAT_INS_EXPAND
15548 if (pum_visible())
15549 rettv->vval.v_number = 1;
15550#endif
15551}
15552
Bram Moolenaardb913952012-06-29 12:54:53 +020015553#ifdef FEAT_PYTHON3
15554/*
15555 * "py3eval()" function
15556 */
15557 static void
15558f_py3eval(argvars, rettv)
15559 typval_T *argvars;
15560 typval_T *rettv;
15561{
15562 char_u *str;
15563 char_u buf[NUMBUFLEN];
15564
15565 str = get_tv_string_buf(&argvars[0], buf);
15566 do_py3eval(str, rettv);
15567}
15568#endif
15569
15570#ifdef FEAT_PYTHON
15571/*
15572 * "pyeval()" function
15573 */
15574 static void
15575f_pyeval(argvars, rettv)
15576 typval_T *argvars;
15577 typval_T *rettv;
15578{
15579 char_u *str;
15580 char_u buf[NUMBUFLEN];
15581
15582 str = get_tv_string_buf(&argvars[0], buf);
15583 do_pyeval(str, rettv);
15584}
15585#endif
15586
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015587/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015588 * "range()" function
15589 */
15590 static void
15591f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015592 typval_T *argvars;
15593 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015594{
15595 long start;
15596 long end;
15597 long stride = 1;
15598 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015599 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015601 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015602 if (argvars[1].v_type == VAR_UNKNOWN)
15603 {
15604 end = start - 1;
15605 start = 0;
15606 }
15607 else
15608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015609 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015610 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015611 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015612 }
15613
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015614 if (error)
15615 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015616 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015617 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015618 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015619 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015620 else
15621 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015622 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015623 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015624 if (list_append_number(rettv->vval.v_list,
15625 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015626 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015627 }
15628}
15629
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015630/*
15631 * "readfile()" function
15632 */
15633 static void
15634f_readfile(argvars, rettv)
15635 typval_T *argvars;
15636 typval_T *rettv;
15637{
15638 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015639 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015640 char_u *fname;
15641 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015642 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15643 int io_size = sizeof(buf);
15644 int readlen; /* size of last fread() */
15645 char_u *prev = NULL; /* previously read bytes, if any */
15646 long prevlen = 0; /* length of data in prev */
15647 long prevsize = 0; /* size of prev buffer */
15648 long maxline = MAXLNUM;
15649 long cnt = 0;
15650 char_u *p; /* position in buf */
15651 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015652
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015653 if (argvars[1].v_type != VAR_UNKNOWN)
15654 {
15655 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15656 binary = TRUE;
15657 if (argvars[2].v_type != VAR_UNKNOWN)
15658 maxline = get_tv_number(&argvars[2]);
15659 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015660
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015661 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015662 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015663
15664 /* Always open the file in binary mode, library functions have a mind of
15665 * their own about CR-LF conversion. */
15666 fname = get_tv_string(&argvars[0]);
15667 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15668 {
15669 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15670 return;
15671 }
15672
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015673 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015674 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015675 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015676
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015677 /* This for loop processes what was read, but is also entered at end
15678 * of file so that either:
15679 * - an incomplete line gets written
15680 * - a "binary" file gets an empty line at the end if it ends in a
15681 * newline. */
15682 for (p = buf, start = buf;
15683 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15684 ++p)
15685 {
15686 if (*p == '\n' || readlen <= 0)
15687 {
15688 listitem_T *li;
15689 char_u *s = NULL;
15690 long_u len = p - start;
15691
15692 /* Finished a line. Remove CRs before NL. */
15693 if (readlen > 0 && !binary)
15694 {
15695 while (len > 0 && start[len - 1] == '\r')
15696 --len;
15697 /* removal may cross back to the "prev" string */
15698 if (len == 0)
15699 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15700 --prevlen;
15701 }
15702 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015703 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015704 else
15705 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015706 /* Change "prev" buffer to be the right size. This way
15707 * the bytes are only copied once, and very long lines are
15708 * allocated only once. */
15709 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015710 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015711 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015712 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015713 prev = NULL; /* the list will own the string */
15714 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015715 }
15716 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015717 if (s == NULL)
15718 {
15719 do_outofmem_msg((long_u) prevlen + len + 1);
15720 failed = TRUE;
15721 break;
15722 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015723
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015724 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015725 {
15726 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015727 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015728 break;
15729 }
15730 li->li_tv.v_type = VAR_STRING;
15731 li->li_tv.v_lock = 0;
15732 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015733 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015734
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015735 start = p + 1; /* step over newline */
15736 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015737 break;
15738 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015739 else if (*p == NUL)
15740 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015741#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015742 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15743 * when finding the BF and check the previous two bytes. */
15744 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015745 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015746 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15747 * + 1, these may be in the "prev" string. */
15748 char_u back1 = p >= buf + 1 ? p[-1]
15749 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15750 char_u back2 = p >= buf + 2 ? p[-2]
15751 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15752 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015753
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015754 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015755 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015756 char_u *dest = p - 2;
15757
15758 /* Usually a BOM is at the beginning of a file, and so at
15759 * the beginning of a line; then we can just step over it.
15760 */
15761 if (start == dest)
15762 start = p + 1;
15763 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015764 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015765 /* have to shuffle buf to close gap */
15766 int adjust_prevlen = 0;
15767
15768 if (dest < buf)
15769 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015770 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015771 dest = buf;
15772 }
15773 if (readlen > p - buf + 1)
15774 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15775 readlen -= 3 - adjust_prevlen;
15776 prevlen -= adjust_prevlen;
15777 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015778 }
15779 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015780 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015781#endif
15782 } /* for */
15783
15784 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15785 break;
15786 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015787 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015788 /* There's part of a line in buf, store it in "prev". */
15789 if (p - start + prevlen >= prevsize)
15790 {
15791 /* need bigger "prev" buffer */
15792 char_u *newprev;
15793
15794 /* A common use case is ordinary text files and "prev" gets a
15795 * fragment of a line, so the first allocation is made
15796 * small, to avoid repeatedly 'allocing' large and
15797 * 'reallocing' small. */
15798 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015799 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015800 else
15801 {
15802 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015803 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015804 prevsize = grow50pc > growmin ? grow50pc : growmin;
15805 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015806 newprev = prev == NULL ? alloc(prevsize)
15807 : vim_realloc(prev, prevsize);
15808 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015809 {
15810 do_outofmem_msg((long_u)prevsize);
15811 failed = TRUE;
15812 break;
15813 }
15814 prev = newprev;
15815 }
15816 /* Add the line part to end of "prev". */
15817 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015818 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015819 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015820 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015821
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015822 /*
15823 * For a negative line count use only the lines at the end of the file,
15824 * free the rest.
15825 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015826 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015827 while (cnt > -maxline)
15828 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015829 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015830 --cnt;
15831 }
15832
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015833 if (failed)
15834 {
15835 list_free(rettv->vval.v_list, TRUE);
15836 /* readfile doc says an empty list is returned on error */
15837 rettv->vval.v_list = list_alloc();
15838 }
15839
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015840 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015841 fclose(fd);
15842}
15843
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015844#if defined(FEAT_RELTIME)
15845static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15846
15847/*
15848 * Convert a List to proftime_T.
15849 * Return FAIL when there is something wrong.
15850 */
15851 static int
15852list2proftime(arg, tm)
15853 typval_T *arg;
15854 proftime_T *tm;
15855{
15856 long n1, n2;
15857 int error = FALSE;
15858
15859 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15860 || arg->vval.v_list->lv_len != 2)
15861 return FAIL;
15862 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15863 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15864# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015865 tm->HighPart = n1;
15866 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015867# else
15868 tm->tv_sec = n1;
15869 tm->tv_usec = n2;
15870# endif
15871 return error ? FAIL : OK;
15872}
15873#endif /* FEAT_RELTIME */
15874
15875/*
15876 * "reltime()" function
15877 */
15878 static void
15879f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015880 typval_T *argvars UNUSED;
15881 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015882{
15883#ifdef FEAT_RELTIME
15884 proftime_T res;
15885 proftime_T start;
15886
15887 if (argvars[0].v_type == VAR_UNKNOWN)
15888 {
15889 /* No arguments: get current time. */
15890 profile_start(&res);
15891 }
15892 else if (argvars[1].v_type == VAR_UNKNOWN)
15893 {
15894 if (list2proftime(&argvars[0], &res) == FAIL)
15895 return;
15896 profile_end(&res);
15897 }
15898 else
15899 {
15900 /* Two arguments: compute the difference. */
15901 if (list2proftime(&argvars[0], &start) == FAIL
15902 || list2proftime(&argvars[1], &res) == FAIL)
15903 return;
15904 profile_sub(&res, &start);
15905 }
15906
15907 if (rettv_list_alloc(rettv) == OK)
15908 {
15909 long n1, n2;
15910
15911# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015912 n1 = res.HighPart;
15913 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015914# else
15915 n1 = res.tv_sec;
15916 n2 = res.tv_usec;
15917# endif
15918 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15919 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15920 }
15921#endif
15922}
15923
15924/*
15925 * "reltimestr()" function
15926 */
15927 static void
15928f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015929 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015930 typval_T *rettv;
15931{
15932#ifdef FEAT_RELTIME
15933 proftime_T tm;
15934#endif
15935
15936 rettv->v_type = VAR_STRING;
15937 rettv->vval.v_string = NULL;
15938#ifdef FEAT_RELTIME
15939 if (list2proftime(&argvars[0], &tm) == OK)
15940 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15941#endif
15942}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015943
Bram Moolenaar0d660222005-01-07 21:51:51 +000015944#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15945static void make_connection __ARGS((void));
15946static int check_connection __ARGS((void));
15947
15948 static void
15949make_connection()
15950{
15951 if (X_DISPLAY == NULL
15952# ifdef FEAT_GUI
15953 && !gui.in_use
15954# endif
15955 )
15956 {
15957 x_force_connect = TRUE;
15958 setup_term_clip();
15959 x_force_connect = FALSE;
15960 }
15961}
15962
15963 static int
15964check_connection()
15965{
15966 make_connection();
15967 if (X_DISPLAY == NULL)
15968 {
15969 EMSG(_("E240: No connection to Vim server"));
15970 return FAIL;
15971 }
15972 return OK;
15973}
15974#endif
15975
15976#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015977static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015978
15979 static void
15980remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015981 typval_T *argvars;
15982 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015983 int expr;
15984{
15985 char_u *server_name;
15986 char_u *keys;
15987 char_u *r = NULL;
15988 char_u buf[NUMBUFLEN];
15989# ifdef WIN32
15990 HWND w;
15991# else
15992 Window w;
15993# endif
15994
15995 if (check_restricted() || check_secure())
15996 return;
15997
15998# ifdef FEAT_X11
15999 if (check_connection() == FAIL)
16000 return;
16001# endif
16002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016003 server_name = get_tv_string_chk(&argvars[0]);
16004 if (server_name == NULL)
16005 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016006 keys = get_tv_string_buf(&argvars[1], buf);
16007# ifdef WIN32
16008 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16009# else
16010 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16011 < 0)
16012# endif
16013 {
16014 if (r != NULL)
16015 EMSG(r); /* sending worked but evaluation failed */
16016 else
16017 EMSG2(_("E241: Unable to send to %s"), server_name);
16018 return;
16019 }
16020
16021 rettv->vval.v_string = r;
16022
16023 if (argvars[2].v_type != VAR_UNKNOWN)
16024 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016025 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016026 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016027 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016028
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016029 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016030 v.di_tv.v_type = VAR_STRING;
16031 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016032 idvar = get_tv_string_chk(&argvars[2]);
16033 if (idvar != NULL)
16034 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016035 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016036 }
16037}
16038#endif
16039
16040/*
16041 * "remote_expr()" function
16042 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016043 static void
16044f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016045 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016046 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016047{
16048 rettv->v_type = VAR_STRING;
16049 rettv->vval.v_string = NULL;
16050#ifdef FEAT_CLIENTSERVER
16051 remote_common(argvars, rettv, TRUE);
16052#endif
16053}
16054
16055/*
16056 * "remote_foreground()" function
16057 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016058 static void
16059f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016060 typval_T *argvars UNUSED;
16061 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016062{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016063#ifdef FEAT_CLIENTSERVER
16064# ifdef WIN32
16065 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016066 {
16067 char_u *server_name = get_tv_string_chk(&argvars[0]);
16068
16069 if (server_name != NULL)
16070 serverForeground(server_name);
16071 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016072# else
16073 /* Send a foreground() expression to the server. */
16074 argvars[1].v_type = VAR_STRING;
16075 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16076 argvars[2].v_type = VAR_UNKNOWN;
16077 remote_common(argvars, rettv, TRUE);
16078 vim_free(argvars[1].vval.v_string);
16079# endif
16080#endif
16081}
16082
Bram Moolenaar0d660222005-01-07 21:51:51 +000016083 static void
16084f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016085 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016086 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016087{
16088#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016089 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016090 char_u *s = NULL;
16091# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016092 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016093# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016094 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016095
16096 if (check_restricted() || check_secure())
16097 {
16098 rettv->vval.v_number = -1;
16099 return;
16100 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016101 serverid = get_tv_string_chk(&argvars[0]);
16102 if (serverid == NULL)
16103 {
16104 rettv->vval.v_number = -1;
16105 return; /* type error; errmsg already given */
16106 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016107# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016108 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016109 if (n == 0)
16110 rettv->vval.v_number = -1;
16111 else
16112 {
16113 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16114 rettv->vval.v_number = (s != NULL);
16115 }
16116# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016117 if (check_connection() == FAIL)
16118 return;
16119
16120 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016121 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016122# endif
16123
16124 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016126 char_u *retvar;
16127
Bram Moolenaar33570922005-01-25 22:26:29 +000016128 v.di_tv.v_type = VAR_STRING;
16129 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016130 retvar = get_tv_string_chk(&argvars[1]);
16131 if (retvar != NULL)
16132 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016133 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016134 }
16135#else
16136 rettv->vval.v_number = -1;
16137#endif
16138}
16139
Bram Moolenaar0d660222005-01-07 21:51:51 +000016140 static void
16141f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016142 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016143 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016144{
16145 char_u *r = NULL;
16146
16147#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016148 char_u *serverid = get_tv_string_chk(&argvars[0]);
16149
16150 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016151 {
16152# ifdef WIN32
16153 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016154 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016155
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016156 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016157 if (n != 0)
16158 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16159 if (r == NULL)
16160# else
16161 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016162 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016163# endif
16164 EMSG(_("E277: Unable to read a server reply"));
16165 }
16166#endif
16167 rettv->v_type = VAR_STRING;
16168 rettv->vval.v_string = r;
16169}
16170
16171/*
16172 * "remote_send()" function
16173 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016174 static void
16175f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016176 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016177 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016178{
16179 rettv->v_type = VAR_STRING;
16180 rettv->vval.v_string = NULL;
16181#ifdef FEAT_CLIENTSERVER
16182 remote_common(argvars, rettv, FALSE);
16183#endif
16184}
16185
16186/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016187 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016188 */
16189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016190f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016191 typval_T *argvars;
16192 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016193{
Bram Moolenaar33570922005-01-25 22:26:29 +000016194 list_T *l;
16195 listitem_T *item, *item2;
16196 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016197 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016198 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016199 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016200 dict_T *d;
16201 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016202 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016203
Bram Moolenaar8c711452005-01-14 21:53:12 +000016204 if (argvars[0].v_type == VAR_DICT)
16205 {
16206 if (argvars[2].v_type != VAR_UNKNOWN)
16207 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016208 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016209 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016211 key = get_tv_string_chk(&argvars[1]);
16212 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016214 di = dict_find(d, key, -1);
16215 if (di == NULL)
16216 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016217 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16218 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016219 {
16220 *rettv = di->di_tv;
16221 init_tv(&di->di_tv);
16222 dictitem_remove(d, di);
16223 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016225 }
16226 }
16227 else if (argvars[0].v_type != VAR_LIST)
16228 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016229 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016230 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016232 int error = FALSE;
16233
16234 idx = get_tv_number_chk(&argvars[1], &error);
16235 if (error)
16236 ; /* type error: do nothing, errmsg already given */
16237 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016238 EMSGN(_(e_listidx), idx);
16239 else
16240 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016241 if (argvars[2].v_type == VAR_UNKNOWN)
16242 {
16243 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016244 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016245 *rettv = item->li_tv;
16246 vim_free(item);
16247 }
16248 else
16249 {
16250 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016251 end = get_tv_number_chk(&argvars[2], &error);
16252 if (error)
16253 ; /* type error: do nothing */
16254 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016255 EMSGN(_(e_listidx), end);
16256 else
16257 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016258 int cnt = 0;
16259
16260 for (li = item; li != NULL; li = li->li_next)
16261 {
16262 ++cnt;
16263 if (li == item2)
16264 break;
16265 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016266 if (li == NULL) /* didn't find "item2" after "item" */
16267 EMSG(_(e_invrange));
16268 else
16269 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016270 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016271 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016272 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016273 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016274 l->lv_first = item;
16275 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016276 item->li_prev = NULL;
16277 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016278 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016279 }
16280 }
16281 }
16282 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016283 }
16284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285}
16286
16287/*
16288 * "rename({from}, {to})" function
16289 */
16290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016291f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016292 typval_T *argvars;
16293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294{
16295 char_u buf[NUMBUFLEN];
16296
16297 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016298 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016300 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16301 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302}
16303
16304/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016305 * "repeat()" function
16306 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016307 static void
16308f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016309 typval_T *argvars;
16310 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016311{
16312 char_u *p;
16313 int n;
16314 int slen;
16315 int len;
16316 char_u *r;
16317 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016318
16319 n = get_tv_number(&argvars[1]);
16320 if (argvars[0].v_type == VAR_LIST)
16321 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016322 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016323 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016324 if (list_extend(rettv->vval.v_list,
16325 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016326 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016327 }
16328 else
16329 {
16330 p = get_tv_string(&argvars[0]);
16331 rettv->v_type = VAR_STRING;
16332 rettv->vval.v_string = NULL;
16333
16334 slen = (int)STRLEN(p);
16335 len = slen * n;
16336 if (len <= 0)
16337 return;
16338
16339 r = alloc(len + 1);
16340 if (r != NULL)
16341 {
16342 for (i = 0; i < n; i++)
16343 mch_memmove(r + i * slen, p, (size_t)slen);
16344 r[len] = NUL;
16345 }
16346
16347 rettv->vval.v_string = r;
16348 }
16349}
16350
16351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 * "resolve()" function
16353 */
16354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016355f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016356 typval_T *argvars;
16357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358{
16359 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016360#ifdef HAVE_READLINK
16361 char_u *buf = NULL;
16362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016364 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365#ifdef FEAT_SHORTCUT
16366 {
16367 char_u *v = NULL;
16368
16369 v = mch_resolve_shortcut(p);
16370 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016371 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016373 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374 }
16375#else
16376# ifdef HAVE_READLINK
16377 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 char_u *cpy;
16379 int len;
16380 char_u *remain = NULL;
16381 char_u *q;
16382 int is_relative_to_current = FALSE;
16383 int has_trailing_pathsep = FALSE;
16384 int limit = 100;
16385
16386 p = vim_strsave(p);
16387
16388 if (p[0] == '.' && (vim_ispathsep(p[1])
16389 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16390 is_relative_to_current = TRUE;
16391
16392 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016393 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016396 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398
16399 q = getnextcomp(p);
16400 if (*q != NUL)
16401 {
16402 /* Separate the first path component in "p", and keep the
16403 * remainder (beginning with the path separator). */
16404 remain = vim_strsave(q - 1);
16405 q[-1] = NUL;
16406 }
16407
Bram Moolenaard9462e32011-04-11 21:35:11 +020016408 buf = alloc(MAXPATHL + 1);
16409 if (buf == NULL)
16410 goto fail;
16411
Bram Moolenaar071d4272004-06-13 20:20:40 +000016412 for (;;)
16413 {
16414 for (;;)
16415 {
16416 len = readlink((char *)p, (char *)buf, MAXPATHL);
16417 if (len <= 0)
16418 break;
16419 buf[len] = NUL;
16420
16421 if (limit-- == 0)
16422 {
16423 vim_free(p);
16424 vim_free(remain);
16425 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016426 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427 goto fail;
16428 }
16429
16430 /* Ensure that the result will have a trailing path separator
16431 * if the argument has one. */
16432 if (remain == NULL && has_trailing_pathsep)
16433 add_pathsep(buf);
16434
16435 /* Separate the first path component in the link value and
16436 * concatenate the remainders. */
16437 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16438 if (*q != NUL)
16439 {
16440 if (remain == NULL)
16441 remain = vim_strsave(q - 1);
16442 else
16443 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016444 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 if (cpy != NULL)
16446 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 vim_free(remain);
16448 remain = cpy;
16449 }
16450 }
16451 q[-1] = NUL;
16452 }
16453
16454 q = gettail(p);
16455 if (q > p && *q == NUL)
16456 {
16457 /* Ignore trailing path separator. */
16458 q[-1] = NUL;
16459 q = gettail(p);
16460 }
16461 if (q > p && !mch_isFullName(buf))
16462 {
16463 /* symlink is relative to directory of argument */
16464 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16465 if (cpy != NULL)
16466 {
16467 STRCPY(cpy, p);
16468 STRCPY(gettail(cpy), buf);
16469 vim_free(p);
16470 p = cpy;
16471 }
16472 }
16473 else
16474 {
16475 vim_free(p);
16476 p = vim_strsave(buf);
16477 }
16478 }
16479
16480 if (remain == NULL)
16481 break;
16482
16483 /* Append the first path component of "remain" to "p". */
16484 q = getnextcomp(remain + 1);
16485 len = q - remain - (*q != NUL);
16486 cpy = vim_strnsave(p, STRLEN(p) + len);
16487 if (cpy != NULL)
16488 {
16489 STRNCAT(cpy, remain, len);
16490 vim_free(p);
16491 p = cpy;
16492 }
16493 /* Shorten "remain". */
16494 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016495 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 else
16497 {
16498 vim_free(remain);
16499 remain = NULL;
16500 }
16501 }
16502
16503 /* If the result is a relative path name, make it explicitly relative to
16504 * the current directory if and only if the argument had this form. */
16505 if (!vim_ispathsep(*p))
16506 {
16507 if (is_relative_to_current
16508 && *p != NUL
16509 && !(p[0] == '.'
16510 && (p[1] == NUL
16511 || vim_ispathsep(p[1])
16512 || (p[1] == '.'
16513 && (p[2] == NUL
16514 || vim_ispathsep(p[2]))))))
16515 {
16516 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016517 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518 if (cpy != NULL)
16519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520 vim_free(p);
16521 p = cpy;
16522 }
16523 }
16524 else if (!is_relative_to_current)
16525 {
16526 /* Strip leading "./". */
16527 q = p;
16528 while (q[0] == '.' && vim_ispathsep(q[1]))
16529 q += 2;
16530 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016531 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532 }
16533 }
16534
16535 /* Ensure that the result will have no trailing path separator
16536 * if the argument had none. But keep "/" or "//". */
16537 if (!has_trailing_pathsep)
16538 {
16539 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016540 if (after_pathsep(p, q))
16541 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 }
16543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016544 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545 }
16546# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016547 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548# endif
16549#endif
16550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016551 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552
16553#ifdef HAVE_READLINK
16554fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016555 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016557 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558}
16559
16560/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016561 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 */
16563 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016564f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016565 typval_T *argvars;
16566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567{
Bram Moolenaar33570922005-01-25 22:26:29 +000016568 list_T *l;
16569 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570
Bram Moolenaar0d660222005-01-07 21:51:51 +000016571 if (argvars[0].v_type != VAR_LIST)
16572 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016573 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016574 && !tv_check_lock(l->lv_lock,
16575 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016576 {
16577 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016578 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016579 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016580 while (li != NULL)
16581 {
16582 ni = li->li_prev;
16583 list_append(l, li);
16584 li = ni;
16585 }
16586 rettv->vval.v_list = l;
16587 rettv->v_type = VAR_LIST;
16588 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016589 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591}
16592
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016593#define SP_NOMOVE 0x01 /* don't move cursor */
16594#define SP_REPEAT 0x02 /* repeat to find outer pair */
16595#define SP_RETCOUNT 0x04 /* return matchcount */
16596#define SP_SETPCMARK 0x08 /* set previous context mark */
16597#define SP_START 0x10 /* accept match at start position */
16598#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16599#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016600#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016601
Bram Moolenaar33570922005-01-25 22:26:29 +000016602static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016603
16604/*
16605 * Get flags for a search function.
16606 * Possibly sets "p_ws".
16607 * Returns BACKWARD, FORWARD or zero (for an error).
16608 */
16609 static int
16610get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016611 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016612 int *flagsp;
16613{
16614 int dir = FORWARD;
16615 char_u *flags;
16616 char_u nbuf[NUMBUFLEN];
16617 int mask;
16618
16619 if (varp->v_type != VAR_UNKNOWN)
16620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016621 flags = get_tv_string_buf_chk(varp, nbuf);
16622 if (flags == NULL)
16623 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016624 while (*flags != NUL)
16625 {
16626 switch (*flags)
16627 {
16628 case 'b': dir = BACKWARD; break;
16629 case 'w': p_ws = TRUE; break;
16630 case 'W': p_ws = FALSE; break;
16631 default: mask = 0;
16632 if (flagsp != NULL)
16633 switch (*flags)
16634 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016635 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016636 case 'e': mask = SP_END; break;
16637 case 'm': mask = SP_RETCOUNT; break;
16638 case 'n': mask = SP_NOMOVE; break;
16639 case 'p': mask = SP_SUBPAT; break;
16640 case 'r': mask = SP_REPEAT; break;
16641 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016642 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016643 }
16644 if (mask == 0)
16645 {
16646 EMSG2(_(e_invarg2), flags);
16647 dir = 0;
16648 }
16649 else
16650 *flagsp |= mask;
16651 }
16652 if (dir == 0)
16653 break;
16654 ++flags;
16655 }
16656 }
16657 return dir;
16658}
16659
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016661 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016663 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016664search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016665 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016666 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016667 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016669 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 char_u *pat;
16671 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016672 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 int save_p_ws = p_ws;
16674 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016675 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016676 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016677 proftime_T tm;
16678#ifdef FEAT_RELTIME
16679 long time_limit = 0;
16680#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016681 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016682 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016684 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016685 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016686 if (dir == 0)
16687 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016688 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016689 if (flags & SP_START)
16690 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016691 if (flags & SP_END)
16692 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016693 if (flags & SP_COLUMN)
16694 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016695
Bram Moolenaar76929292008-01-06 19:07:36 +000016696 /* Optional arguments: line number to stop searching and timeout. */
16697 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016698 {
16699 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16700 if (lnum_stop < 0)
16701 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016702#ifdef FEAT_RELTIME
16703 if (argvars[3].v_type != VAR_UNKNOWN)
16704 {
16705 time_limit = get_tv_number_chk(&argvars[3], NULL);
16706 if (time_limit < 0)
16707 goto theend;
16708 }
16709#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016710 }
16711
Bram Moolenaar76929292008-01-06 19:07:36 +000016712#ifdef FEAT_RELTIME
16713 /* Set the time limit, if there is one. */
16714 profile_setlimit(time_limit, &tm);
16715#endif
16716
Bram Moolenaar231334e2005-07-25 20:46:57 +000016717 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016718 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016719 * Check to make sure only those flags are set.
16720 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16721 * flags cannot be set. Check for that condition also.
16722 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016723 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016724 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016726 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016727 goto theend;
16728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016730 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016731 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016732 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016733 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016735 if (flags & SP_SUBPAT)
16736 retval = subpatnum;
16737 else
16738 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016739 if (flags & SP_SETPCMARK)
16740 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016742 if (match_pos != NULL)
16743 {
16744 /* Store the match cursor position */
16745 match_pos->lnum = pos.lnum;
16746 match_pos->col = pos.col + 1;
16747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 /* "/$" will put the cursor after the end of the line, may need to
16749 * correct that here */
16750 check_cursor();
16751 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016752
16753 /* If 'n' flag is used: restore cursor position. */
16754 if (flags & SP_NOMOVE)
16755 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016756 else
16757 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016758theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016760
16761 return retval;
16762}
16763
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016764#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016765
16766/*
16767 * round() is not in C90, use ceil() or floor() instead.
16768 */
16769 float_T
16770vim_round(f)
16771 float_T f;
16772{
16773 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16774}
16775
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016776/*
16777 * "round({float})" function
16778 */
16779 static void
16780f_round(argvars, rettv)
16781 typval_T *argvars;
16782 typval_T *rettv;
16783{
16784 float_T f;
16785
16786 rettv->v_type = VAR_FLOAT;
16787 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016788 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016789 else
16790 rettv->vval.v_float = 0.0;
16791}
16792#endif
16793
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016794/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016795 * "screenattr()" function
16796 */
16797 static void
16798f_screenattr(argvars, rettv)
16799 typval_T *argvars UNUSED;
16800 typval_T *rettv;
16801{
16802 int row;
16803 int col;
16804 int c;
16805
16806 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16807 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16808 if (row < 0 || row >= screen_Rows
16809 || col < 0 || col >= screen_Columns)
16810 c = -1;
16811 else
16812 c = ScreenAttrs[LineOffset[row] + col];
16813 rettv->vval.v_number = c;
16814}
16815
16816/*
16817 * "screenchar()" function
16818 */
16819 static void
16820f_screenchar(argvars, rettv)
16821 typval_T *argvars UNUSED;
16822 typval_T *rettv;
16823{
16824 int row;
16825 int col;
16826 int off;
16827 int c;
16828
16829 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16830 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16831 if (row < 0 || row >= screen_Rows
16832 || col < 0 || col >= screen_Columns)
16833 c = -1;
16834 else
16835 {
16836 off = LineOffset[row] + col;
16837#ifdef FEAT_MBYTE
16838 if (enc_utf8 && ScreenLinesUC[off] != 0)
16839 c = ScreenLinesUC[off];
16840 else
16841#endif
16842 c = ScreenLines[off];
16843 }
16844 rettv->vval.v_number = c;
16845}
16846
16847/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016848 * "screencol()" function
16849 *
16850 * First column is 1 to be consistent with virtcol().
16851 */
16852 static void
16853f_screencol(argvars, rettv)
16854 typval_T *argvars UNUSED;
16855 typval_T *rettv;
16856{
16857 rettv->vval.v_number = screen_screencol() + 1;
16858}
16859
16860/*
16861 * "screenrow()" function
16862 */
16863 static void
16864f_screenrow(argvars, rettv)
16865 typval_T *argvars UNUSED;
16866 typval_T *rettv;
16867{
16868 rettv->vval.v_number = screen_screenrow() + 1;
16869}
16870
16871/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016872 * "search()" function
16873 */
16874 static void
16875f_search(argvars, rettv)
16876 typval_T *argvars;
16877 typval_T *rettv;
16878{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016879 int flags = 0;
16880
16881 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882}
16883
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016885 * "searchdecl()" function
16886 */
16887 static void
16888f_searchdecl(argvars, rettv)
16889 typval_T *argvars;
16890 typval_T *rettv;
16891{
16892 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016893 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016894 int error = FALSE;
16895 char_u *name;
16896
16897 rettv->vval.v_number = 1; /* default: FAIL */
16898
16899 name = get_tv_string_chk(&argvars[0]);
16900 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016901 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016902 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016903 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16904 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16905 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016906 if (!error && name != NULL)
16907 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016908 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016909}
16910
16911/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016912 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016914 static int
16915searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016916 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016917 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918{
16919 char_u *spat, *mpat, *epat;
16920 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922 int dir;
16923 int flags = 0;
16924 char_u nbuf1[NUMBUFLEN];
16925 char_u nbuf2[NUMBUFLEN];
16926 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016927 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016928 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016929 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016932 spat = get_tv_string_chk(&argvars[0]);
16933 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16934 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16935 if (spat == NULL || mpat == NULL || epat == NULL)
16936 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938 /* Handle the optional fourth argument: flags */
16939 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016940 if (dir == 0)
16941 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016942
16943 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016944 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16945 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016946 if ((flags & (SP_END | SP_SUBPAT)) != 0
16947 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016948 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016949 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016950 goto theend;
16951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016953 /* Using 'r' implies 'W', otherwise it doesn't work. */
16954 if (flags & SP_REPEAT)
16955 p_ws = FALSE;
16956
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016957 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016958 if (argvars[3].v_type == VAR_UNKNOWN
16959 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960 skip = (char_u *)"";
16961 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016963 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016964 if (argvars[5].v_type != VAR_UNKNOWN)
16965 {
16966 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16967 if (lnum_stop < 0)
16968 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016969#ifdef FEAT_RELTIME
16970 if (argvars[6].v_type != VAR_UNKNOWN)
16971 {
16972 time_limit = get_tv_number_chk(&argvars[6], NULL);
16973 if (time_limit < 0)
16974 goto theend;
16975 }
16976#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016977 }
16978 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016979 if (skip == NULL)
16980 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016982 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016983 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016984
16985theend:
16986 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016987
16988 return retval;
16989}
16990
16991/*
16992 * "searchpair()" function
16993 */
16994 static void
16995f_searchpair(argvars, rettv)
16996 typval_T *argvars;
16997 typval_T *rettv;
16998{
16999 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17000}
17001
17002/*
17003 * "searchpairpos()" function
17004 */
17005 static void
17006f_searchpairpos(argvars, rettv)
17007 typval_T *argvars;
17008 typval_T *rettv;
17009{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017010 pos_T match_pos;
17011 int lnum = 0;
17012 int col = 0;
17013
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017014 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017015 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017016
17017 if (searchpair_cmn(argvars, &match_pos) > 0)
17018 {
17019 lnum = match_pos.lnum;
17020 col = match_pos.col;
17021 }
17022
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017023 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17024 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017025}
17026
17027/*
17028 * Search for a start/middle/end thing.
17029 * Used by searchpair(), see its documentation for the details.
17030 * Returns 0 or -1 for no match,
17031 */
17032 long
Bram Moolenaar76929292008-01-06 19:07:36 +000017033do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
17034 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017035 char_u *spat; /* start pattern */
17036 char_u *mpat; /* middle pattern */
17037 char_u *epat; /* end pattern */
17038 int dir; /* BACKWARD or FORWARD */
17039 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017040 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017041 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017042 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017043 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017044{
17045 char_u *save_cpo;
17046 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17047 long retval = 0;
17048 pos_T pos;
17049 pos_T firstpos;
17050 pos_T foundpos;
17051 pos_T save_cursor;
17052 pos_T save_pos;
17053 int n;
17054 int r;
17055 int nest = 1;
17056 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017057 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017058 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017059
17060 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17061 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017062 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017063
Bram Moolenaar76929292008-01-06 19:07:36 +000017064#ifdef FEAT_RELTIME
17065 /* Set the time limit, if there is one. */
17066 profile_setlimit(time_limit, &tm);
17067#endif
17068
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017069 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17070 * start/middle/end (pat3, for the top pair). */
17071 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17072 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17073 if (pat2 == NULL || pat3 == NULL)
17074 goto theend;
17075 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17076 if (*mpat == NUL)
17077 STRCPY(pat3, pat2);
17078 else
17079 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17080 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017081 if (flags & SP_START)
17082 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017083
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 save_cursor = curwin->w_cursor;
17085 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017086 clearpos(&firstpos);
17087 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088 pat = pat3;
17089 for (;;)
17090 {
17091 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017092 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17094 /* didn't find it or found the first match again: FAIL */
17095 break;
17096
17097 if (firstpos.lnum == 0)
17098 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017099 if (equalpos(pos, foundpos))
17100 {
17101 /* Found the same position again. Can happen with a pattern that
17102 * has "\zs" at the end and searching backwards. Advance one
17103 * character and try again. */
17104 if (dir == BACKWARD)
17105 decl(&pos);
17106 else
17107 incl(&pos);
17108 }
17109 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017111 /* clear the start flag to avoid getting stuck here */
17112 options &= ~SEARCH_START;
17113
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114 /* If the skip pattern matches, ignore this match. */
17115 if (*skip != NUL)
17116 {
17117 save_pos = curwin->w_cursor;
17118 curwin->w_cursor = pos;
17119 r = eval_to_bool(skip, &err, NULL, FALSE);
17120 curwin->w_cursor = save_pos;
17121 if (err)
17122 {
17123 /* Evaluating {skip} caused an error, break here. */
17124 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017125 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126 break;
17127 }
17128 if (r)
17129 continue;
17130 }
17131
17132 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17133 {
17134 /* Found end when searching backwards or start when searching
17135 * forward: nested pair. */
17136 ++nest;
17137 pat = pat2; /* nested, don't search for middle */
17138 }
17139 else
17140 {
17141 /* Found end when searching forward or start when searching
17142 * backward: end of (nested) pair; or found middle in outer pair. */
17143 if (--nest == 1)
17144 pat = pat3; /* outer level, search for middle */
17145 }
17146
17147 if (nest == 0)
17148 {
17149 /* Found the match: return matchcount or line number. */
17150 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017151 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017153 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017154 if (flags & SP_SETPCMARK)
17155 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156 curwin->w_cursor = pos;
17157 if (!(flags & SP_REPEAT))
17158 break;
17159 nest = 1; /* search for next unmatched */
17160 }
17161 }
17162
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017163 if (match_pos != NULL)
17164 {
17165 /* Store the match cursor position */
17166 match_pos->lnum = curwin->w_cursor.lnum;
17167 match_pos->col = curwin->w_cursor.col + 1;
17168 }
17169
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017171 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 curwin->w_cursor = save_cursor;
17173
17174theend:
17175 vim_free(pat2);
17176 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017177 if (p_cpo == empty_option)
17178 p_cpo = save_cpo;
17179 else
17180 /* Darn, evaluating the {skip} expression changed the value. */
17181 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017182
17183 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184}
17185
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017186/*
17187 * "searchpos()" function
17188 */
17189 static void
17190f_searchpos(argvars, rettv)
17191 typval_T *argvars;
17192 typval_T *rettv;
17193{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017194 pos_T match_pos;
17195 int lnum = 0;
17196 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017197 int n;
17198 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017199
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017200 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017201 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017202
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017203 n = search_cmn(argvars, &match_pos, &flags);
17204 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017205 {
17206 lnum = match_pos.lnum;
17207 col = match_pos.col;
17208 }
17209
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017210 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17211 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017212 if (flags & SP_SUBPAT)
17213 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017214}
17215
17216
Bram Moolenaar0d660222005-01-07 21:51:51 +000017217 static void
17218f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017219 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017222#ifdef FEAT_CLIENTSERVER
17223 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017224 char_u *server = get_tv_string_chk(&argvars[0]);
17225 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226
Bram Moolenaar0d660222005-01-07 21:51:51 +000017227 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017228 if (server == NULL || reply == NULL)
17229 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017230 if (check_restricted() || check_secure())
17231 return;
17232# ifdef FEAT_X11
17233 if (check_connection() == FAIL)
17234 return;
17235# endif
17236
17237 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017239 EMSG(_("E258: Unable to send to client"));
17240 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017242 rettv->vval.v_number = 0;
17243#else
17244 rettv->vval.v_number = -1;
17245#endif
17246}
17247
Bram Moolenaar0d660222005-01-07 21:51:51 +000017248 static void
17249f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017250 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017251 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017252{
17253 char_u *r = NULL;
17254
17255#ifdef FEAT_CLIENTSERVER
17256# ifdef WIN32
17257 r = serverGetVimNames();
17258# else
17259 make_connection();
17260 if (X_DISPLAY != NULL)
17261 r = serverGetVimNames(X_DISPLAY);
17262# endif
17263#endif
17264 rettv->v_type = VAR_STRING;
17265 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266}
17267
17268/*
17269 * "setbufvar()" function
17270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017272f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017273 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017274 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275{
17276 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017279 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 char_u nbuf[NUMBUFLEN];
17281
17282 if (check_restricted() || check_secure())
17283 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017284 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17285 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017286 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287 varp = &argvars[2];
17288
17289 if (buf != NULL && varname != NULL && varp != NULL)
17290 {
17291 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293
17294 if (*varname == '&')
17295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017296 long numval;
17297 char_u *strval;
17298 int error = FALSE;
17299
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017301 numval = get_tv_number_chk(varp, &error);
17302 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017303 if (!error && strval != NULL)
17304 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305 }
17306 else
17307 {
17308 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17309 if (bufvarname != NULL)
17310 {
17311 STRCPY(bufvarname, "b:");
17312 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017313 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 vim_free(bufvarname);
17315 }
17316 }
17317
17318 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321}
17322
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017323 static void
17324f_setcharsearch(argvars, rettv)
17325 typval_T *argvars;
17326 typval_T *rettv UNUSED;
17327{
17328 dict_T *d;
17329 dictitem_T *di;
17330 char_u *csearch;
17331
17332 if (argvars[0].v_type != VAR_DICT)
17333 {
17334 EMSG(_(e_dictreq));
17335 return;
17336 }
17337
17338 if ((d = argvars[0].vval.v_dict) != NULL)
17339 {
17340 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17341 if (csearch != NULL)
17342 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017343#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017344 if (enc_utf8)
17345 {
17346 int pcc[MAX_MCO];
17347 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017348
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017349 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17350 }
17351 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017352#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017353 set_last_csearch(PTR2CHAR(csearch),
17354 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017355 }
17356
17357 di = dict_find(d, (char_u *)"forward", -1);
17358 if (di != NULL)
17359 set_csearch_direction(get_tv_number(&di->di_tv)
17360 ? FORWARD : BACKWARD);
17361
17362 di = dict_find(d, (char_u *)"until", -1);
17363 if (di != NULL)
17364 set_csearch_until(!!get_tv_number(&di->di_tv));
17365 }
17366}
17367
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368/*
17369 * "setcmdpos()" function
17370 */
17371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017372f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017373 typval_T *argvars;
17374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017376 int pos = (int)get_tv_number(&argvars[0]) - 1;
17377
17378 if (pos >= 0)
17379 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380}
17381
17382/*
17383 * "setline()" function
17384 */
17385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017386f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017387 typval_T *argvars;
17388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389{
17390 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017391 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017392 list_T *l = NULL;
17393 listitem_T *li = NULL;
17394 long added = 0;
17395 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017397 lnum = get_tv_lnum(&argvars[0]);
17398 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017400 l = argvars[1].vval.v_list;
17401 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017403 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017404 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017405
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017406 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017407 for (;;)
17408 {
17409 if (l != NULL)
17410 {
17411 /* list argument, get next string */
17412 if (li == NULL)
17413 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017414 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017415 li = li->li_next;
17416 }
17417
17418 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017419 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017420 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017421
17422 /* When coming here from Insert mode, sync undo, so that this can be
17423 * undone separately from what was previously inserted. */
17424 if (u_sync_once == 2)
17425 {
17426 u_sync_once = 1; /* notify that u_sync() was called */
17427 u_sync(TRUE);
17428 }
17429
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017430 if (lnum <= curbuf->b_ml.ml_line_count)
17431 {
17432 /* existing line, replace it */
17433 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17434 {
17435 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017436 if (lnum == curwin->w_cursor.lnum)
17437 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017438 rettv->vval.v_number = 0; /* OK */
17439 }
17440 }
17441 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17442 {
17443 /* lnum is one past the last line, append the line */
17444 ++added;
17445 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17446 rettv->vval.v_number = 0; /* OK */
17447 }
17448
17449 if (l == NULL) /* only one string argument */
17450 break;
17451 ++lnum;
17452 }
17453
17454 if (added > 0)
17455 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456}
17457
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017458static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17459
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017461 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017462 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017463 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017464set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017465 win_T *wp UNUSED;
17466 typval_T *list_arg UNUSED;
17467 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017468 typval_T *rettv;
17469{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017470#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017471 char_u *act;
17472 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017473#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017474
Bram Moolenaar2641f772005-03-25 21:58:17 +000017475 rettv->vval.v_number = -1;
17476
17477#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017478 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017479 EMSG(_(e_listreq));
17480 else
17481 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017482 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017483
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017484 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017485 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017486 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017487 if (act == NULL)
17488 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017489 if (*act == 'a' || *act == 'r')
17490 action = *act;
17491 }
17492
Bram Moolenaar81484f42012-12-05 15:16:47 +010017493 if (l != NULL && set_errorlist(wp, l, action,
17494 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017495 rettv->vval.v_number = 0;
17496 }
17497#endif
17498}
17499
17500/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017501 * "setloclist()" function
17502 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017503 static void
17504f_setloclist(argvars, rettv)
17505 typval_T *argvars;
17506 typval_T *rettv;
17507{
17508 win_T *win;
17509
17510 rettv->vval.v_number = -1;
17511
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017512 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017513 if (win != NULL)
17514 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17515}
17516
17517/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017518 * "setmatches()" function
17519 */
17520 static void
17521f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017522 typval_T *argvars UNUSED;
17523 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017524{
17525#ifdef FEAT_SEARCH_EXTRA
17526 list_T *l;
17527 listitem_T *li;
17528 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017529 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017530
17531 rettv->vval.v_number = -1;
17532 if (argvars[0].v_type != VAR_LIST)
17533 {
17534 EMSG(_(e_listreq));
17535 return;
17536 }
17537 if ((l = argvars[0].vval.v_list) != NULL)
17538 {
17539
17540 /* To some extent make sure that we are dealing with a list from
17541 * "getmatches()". */
17542 li = l->lv_first;
17543 while (li != NULL)
17544 {
17545 if (li->li_tv.v_type != VAR_DICT
17546 || (d = li->li_tv.vval.v_dict) == NULL)
17547 {
17548 EMSG(_(e_invarg));
17549 return;
17550 }
17551 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017552 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17553 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017554 && dict_find(d, (char_u *)"priority", -1) != NULL
17555 && dict_find(d, (char_u *)"id", -1) != NULL))
17556 {
17557 EMSG(_(e_invarg));
17558 return;
17559 }
17560 li = li->li_next;
17561 }
17562
17563 clear_matches(curwin);
17564 li = l->lv_first;
17565 while (li != NULL)
17566 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017567 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017568 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017569 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017570 char_u *group;
17571 int priority;
17572 int id;
17573 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017574
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017575 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017576 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17577 {
17578 if (s == NULL)
17579 {
17580 s = list_alloc();
17581 if (s == NULL)
17582 return;
17583 }
17584
17585 /* match from matchaddpos() */
17586 for (i = 1; i < 9; i++)
17587 {
17588 sprintf((char *)buf, (char *)"pos%d", i);
17589 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17590 {
17591 if (di->di_tv.v_type != VAR_LIST)
17592 return;
17593
17594 list_append_tv(s, &di->di_tv);
17595 s->lv_refcount++;
17596 }
17597 else
17598 break;
17599 }
17600 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017601
17602 group = get_dict_string(d, (char_u *)"group", FALSE);
17603 priority = (int)get_dict_number(d, (char_u *)"priority");
17604 id = (int)get_dict_number(d, (char_u *)"id");
17605 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17606 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17607 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017608 if (i == 0)
17609 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017610 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017611 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017612 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017613 }
17614 else
17615 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017616 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017617 list_unref(s);
17618 s = NULL;
17619 }
17620
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017621 li = li->li_next;
17622 }
17623 rettv->vval.v_number = 0;
17624 }
17625#endif
17626}
17627
17628/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017629 * "setpos()" function
17630 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017631 static void
17632f_setpos(argvars, rettv)
17633 typval_T *argvars;
17634 typval_T *rettv;
17635{
17636 pos_T pos;
17637 int fnum;
17638 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017639 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017640
Bram Moolenaar08250432008-02-13 11:42:46 +000017641 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017642 name = get_tv_string_chk(argvars);
17643 if (name != NULL)
17644 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017645 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017646 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017647 if (--pos.col < 0)
17648 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017649 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017650 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017651 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017652 if (fnum == curbuf->b_fnum)
17653 {
17654 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017655 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017656 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017657 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017658 curwin->w_set_curswant = FALSE;
17659 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017660 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017661 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017662 }
17663 else
17664 EMSG(_(e_invarg));
17665 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017666 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17667 {
17668 /* set mark */
17669 if (setmark_pos(name[1], &pos, fnum) == OK)
17670 rettv->vval.v_number = 0;
17671 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017672 else
17673 EMSG(_(e_invarg));
17674 }
17675 }
17676}
17677
17678/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017679 * "setqflist()" function
17680 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017681 static void
17682f_setqflist(argvars, rettv)
17683 typval_T *argvars;
17684 typval_T *rettv;
17685{
17686 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17687}
17688
17689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690 * "setreg()" function
17691 */
17692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017693f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017694 typval_T *argvars;
17695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696{
17697 int regname;
17698 char_u *strregname;
17699 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017700 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 int append;
17702 char_u yank_type;
17703 long block_len;
17704
17705 block_len = -1;
17706 yank_type = MAUTO;
17707 append = FALSE;
17708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017709 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017710 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017712 if (strregname == NULL)
17713 return; /* type error; errmsg already given */
17714 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 if (regname == 0 || regname == '@')
17716 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017718 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017720 stropt = get_tv_string_chk(&argvars[2]);
17721 if (stropt == NULL)
17722 return; /* type error */
17723 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 switch (*stropt)
17725 {
17726 case 'a': case 'A': /* append */
17727 append = TRUE;
17728 break;
17729 case 'v': case 'c': /* character-wise selection */
17730 yank_type = MCHAR;
17731 break;
17732 case 'V': case 'l': /* line-wise selection */
17733 yank_type = MLINE;
17734 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735 case 'b': case Ctrl_V: /* block-wise selection */
17736 yank_type = MBLOCK;
17737 if (VIM_ISDIGIT(stropt[1]))
17738 {
17739 ++stropt;
17740 block_len = getdigits(&stropt) - 1;
17741 --stropt;
17742 }
17743 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 }
17745 }
17746
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017747 if (argvars[1].v_type == VAR_LIST)
17748 {
17749 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017750 char_u **allocval;
17751 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017752 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017753 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017754 int len = argvars[1].vval.v_list->lv_len;
17755 listitem_T *li;
17756
Bram Moolenaar7d647822014-04-05 21:28:56 +020017757 /* First half: use for pointers to result lines; second half: use for
17758 * pointers to allocated copies. */
17759 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017760 if (lstval == NULL)
17761 return;
17762 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017763 allocval = lstval + len + 2;
17764 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017765
17766 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17767 li = li->li_next)
17768 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017769 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017770 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017771 goto free_lstval;
17772 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017773 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017774 /* Need to make a copy, next get_tv_string_buf_chk() will
17775 * overwrite the string. */
17776 strval = vim_strsave(buf);
17777 if (strval == NULL)
17778 goto free_lstval;
17779 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017780 }
17781 *curval++ = strval;
17782 }
17783 *curval++ = NULL;
17784
17785 write_reg_contents_lst(regname, lstval, -1,
17786 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017787free_lstval:
17788 while (curallocval > allocval)
17789 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017790 vim_free(lstval);
17791 }
17792 else
17793 {
17794 strval = get_tv_string_chk(&argvars[1]);
17795 if (strval == NULL)
17796 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017797 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017799 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017800 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801}
17802
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017803/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017804 * "settabvar()" function
17805 */
17806 static void
17807f_settabvar(argvars, rettv)
17808 typval_T *argvars;
17809 typval_T *rettv;
17810{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017811#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017812 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017813 tabpage_T *tp;
17814#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017815 char_u *varname, *tabvarname;
17816 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017817
17818 rettv->vval.v_number = 0;
17819
17820 if (check_restricted() || check_secure())
17821 return;
17822
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017823#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017824 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017825#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017826 varname = get_tv_string_chk(&argvars[1]);
17827 varp = &argvars[2];
17828
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017829 if (varname != NULL && varp != NULL
17830#ifdef FEAT_WINDOWS
17831 && tp != NULL
17832#endif
17833 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017834 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017835#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017836 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017837 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017838#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017839
17840 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17841 if (tabvarname != NULL)
17842 {
17843 STRCPY(tabvarname, "t:");
17844 STRCPY(tabvarname + 2, varname);
17845 set_var(tabvarname, varp, TRUE);
17846 vim_free(tabvarname);
17847 }
17848
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017849#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017850 /* Restore current tabpage */
17851 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017852 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017853#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017854 }
17855}
17856
17857/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017858 * "settabwinvar()" function
17859 */
17860 static void
17861f_settabwinvar(argvars, rettv)
17862 typval_T *argvars;
17863 typval_T *rettv;
17864{
17865 setwinvar(argvars, rettv, 1);
17866}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867
17868/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017869 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017872f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017873 typval_T *argvars;
17874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017876 setwinvar(argvars, rettv, 0);
17877}
17878
17879/*
17880 * "setwinvar()" and "settabwinvar()" functions
17881 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017882
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017883 static void
17884setwinvar(argvars, rettv, off)
17885 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017886 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017887 int off;
17888{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 win_T *win;
17890#ifdef FEAT_WINDOWS
17891 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017892 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017893 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894#endif
17895 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017896 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017898 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899
17900 if (check_restricted() || check_secure())
17901 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017902
17903#ifdef FEAT_WINDOWS
17904 if (off == 1)
17905 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17906 else
17907 tp = curtab;
17908#endif
17909 win = find_win_by_nr(&argvars[off], tp);
17910 varname = get_tv_string_chk(&argvars[off + 1]);
17911 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912
17913 if (win != NULL && varname != NULL && varp != NULL)
17914 {
17915#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017916 need_switch_win = !(tp == curtab && win == curwin);
17917 if (!need_switch_win
17918 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017921 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017923 long numval;
17924 char_u *strval;
17925 int error = FALSE;
17926
17927 ++varname;
17928 numval = get_tv_number_chk(varp, &error);
17929 strval = get_tv_string_buf_chk(varp, nbuf);
17930 if (!error && strval != NULL)
17931 set_option_value(varname, numval, strval, OPT_LOCAL);
17932 }
17933 else
17934 {
17935 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17936 if (winvarname != NULL)
17937 {
17938 STRCPY(winvarname, "w:");
17939 STRCPY(winvarname + 2, varname);
17940 set_var(winvarname, varp, TRUE);
17941 vim_free(winvarname);
17942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 }
17944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017946 if (need_switch_win)
17947 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948#endif
17949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950}
17951
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017952#ifdef FEAT_CRYPT
17953/*
17954 * "sha256({string})" function
17955 */
17956 static void
17957f_sha256(argvars, rettv)
17958 typval_T *argvars;
17959 typval_T *rettv;
17960{
17961 char_u *p;
17962
17963 p = get_tv_string(&argvars[0]);
17964 rettv->vval.v_string = vim_strsave(
17965 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17966 rettv->v_type = VAR_STRING;
17967}
17968#endif /* FEAT_CRYPT */
17969
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017971 * "shellescape({string})" function
17972 */
17973 static void
17974f_shellescape(argvars, rettv)
17975 typval_T *argvars;
17976 typval_T *rettv;
17977{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017978 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017979 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017980 rettv->v_type = VAR_STRING;
17981}
17982
17983/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017984 * shiftwidth() function
17985 */
17986 static void
17987f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017988 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017989 typval_T *rettv;
17990{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017991 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017992}
17993
17994/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017995 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996 */
17997 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017998f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017999 typval_T *argvars;
18000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018002 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018003
Bram Moolenaar0d660222005-01-07 21:51:51 +000018004 p = get_tv_string(&argvars[0]);
18005 rettv->vval.v_string = vim_strsave(p);
18006 simplify_filename(rettv->vval.v_string); /* simplify in place */
18007 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008}
18009
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018010#ifdef FEAT_FLOAT
18011/*
18012 * "sin()" function
18013 */
18014 static void
18015f_sin(argvars, rettv)
18016 typval_T *argvars;
18017 typval_T *rettv;
18018{
18019 float_T f;
18020
18021 rettv->v_type = VAR_FLOAT;
18022 if (get_float_arg(argvars, &f) == OK)
18023 rettv->vval.v_float = sin(f);
18024 else
18025 rettv->vval.v_float = 0.0;
18026}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018027
18028/*
18029 * "sinh()" function
18030 */
18031 static void
18032f_sinh(argvars, rettv)
18033 typval_T *argvars;
18034 typval_T *rettv;
18035{
18036 float_T f;
18037
18038 rettv->v_type = VAR_FLOAT;
18039 if (get_float_arg(argvars, &f) == OK)
18040 rettv->vval.v_float = sinh(f);
18041 else
18042 rettv->vval.v_float = 0.0;
18043}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018044#endif
18045
Bram Moolenaar0d660222005-01-07 21:51:51 +000018046static int
18047#ifdef __BORLANDC__
18048 _RTLENTRYF
18049#endif
18050 item_compare __ARGS((const void *s1, const void *s2));
18051static int
18052#ifdef __BORLANDC__
18053 _RTLENTRYF
18054#endif
18055 item_compare2 __ARGS((const void *s1, const void *s2));
18056
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018057/* struct used in the array that's given to qsort() */
18058typedef struct
18059{
18060 listitem_T *item;
18061 int idx;
18062} sortItem_T;
18063
Bram Moolenaar0d660222005-01-07 21:51:51 +000018064static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018065static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018066static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018067static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018068static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018069static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018070static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018071static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018072#define ITEM_COMPARE_FAIL 999
18073
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018075 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018077 static int
18078#ifdef __BORLANDC__
18079_RTLENTRYF
18080#endif
18081item_compare(s1, s2)
18082 const void *s1;
18083 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018085 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018086 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018087 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018088 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018089 int res;
18090 char_u numbuf1[NUMBUFLEN];
18091 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018093 si1 = (sortItem_T *)s1;
18094 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018095 tv1 = &si1->item->li_tv;
18096 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018097
18098 if (item_compare_numbers)
18099 {
18100 long v1 = get_tv_number(tv1);
18101 long v2 = get_tv_number(tv2);
18102
18103 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18104 }
18105
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018106 /* tv2string() puts quotes around a string and allocates memory. Don't do
18107 * that for string variables. Use a single quote when comparing with a
18108 * non-string to do what the docs promise. */
18109 if (tv1->v_type == VAR_STRING)
18110 {
18111 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18112 p1 = (char_u *)"'";
18113 else
18114 p1 = tv1->vval.v_string;
18115 }
18116 else
18117 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18118 if (tv2->v_type == VAR_STRING)
18119 {
18120 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18121 p2 = (char_u *)"'";
18122 else
18123 p2 = tv2->vval.v_string;
18124 }
18125 else
18126 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018127 if (p1 == NULL)
18128 p1 = (char_u *)"";
18129 if (p2 == NULL)
18130 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018131 if (!item_compare_numeric)
18132 {
18133 if (item_compare_ic)
18134 res = STRICMP(p1, p2);
18135 else
18136 res = STRCMP(p1, p2);
18137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018139 {
18140 double n1, n2;
18141 n1 = strtod((char *)p1, (char **)&p1);
18142 n2 = strtod((char *)p2, (char **)&p2);
18143 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18144 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018145
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018146 /* When the result would be zero, compare the item indexes. Makes the
18147 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018148 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018149 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018150
Bram Moolenaar0d660222005-01-07 21:51:51 +000018151 vim_free(tofree1);
18152 vim_free(tofree2);
18153 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154}
18155
18156 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018157#ifdef __BORLANDC__
18158_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018160item_compare2(s1, s2)
18161 const void *s1;
18162 const void *s2;
18163{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018164 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018165 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018166 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018167 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018168 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018170 /* shortcut after failure in previous call; compare all items equal */
18171 if (item_compare_func_err)
18172 return 0;
18173
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018174 si1 = (sortItem_T *)s1;
18175 si2 = (sortItem_T *)s2;
18176
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018177 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018178 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018179 copy_tv(&si1->item->li_tv, &argv[0]);
18180 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018181
18182 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018183 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018184 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18185 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018186 clear_tv(&argv[0]);
18187 clear_tv(&argv[1]);
18188
18189 if (res == FAIL)
18190 res = ITEM_COMPARE_FAIL;
18191 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018192 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18193 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018194 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018195 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018196
18197 /* When the result would be zero, compare the pointers themselves. Makes
18198 * the sort stable. */
18199 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018200 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018201
Bram Moolenaar0d660222005-01-07 21:51:51 +000018202 return res;
18203}
18204
18205/*
18206 * "sort({list})" function
18207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018209do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018210 typval_T *argvars;
18211 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018212 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213{
Bram Moolenaar33570922005-01-25 22:26:29 +000018214 list_T *l;
18215 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018216 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018217 long len;
18218 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219
Bram Moolenaar0d660222005-01-07 21:51:51 +000018220 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018221 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222 else
18223 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018224 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018225 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018226 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18227 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018228 return;
18229 rettv->vval.v_list = l;
18230 rettv->v_type = VAR_LIST;
18231 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232
Bram Moolenaar0d660222005-01-07 21:51:51 +000018233 len = list_len(l);
18234 if (len <= 1)
18235 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236
Bram Moolenaar0d660222005-01-07 21:51:51 +000018237 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018238 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018239 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018240 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018241 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018242 if (argvars[1].v_type != VAR_UNKNOWN)
18243 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018244 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018245 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018246 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018247 else
18248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018249 int error = FALSE;
18250
18251 i = get_tv_number_chk(&argvars[1], &error);
18252 if (error)
18253 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018254 if (i == 1)
18255 item_compare_ic = TRUE;
18256 else
18257 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018258 if (item_compare_func != NULL)
18259 {
18260 if (STRCMP(item_compare_func, "n") == 0)
18261 {
18262 item_compare_func = NULL;
18263 item_compare_numeric = TRUE;
18264 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018265 else if (STRCMP(item_compare_func, "N") == 0)
18266 {
18267 item_compare_func = NULL;
18268 item_compare_numbers = TRUE;
18269 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018270 else if (STRCMP(item_compare_func, "i") == 0)
18271 {
18272 item_compare_func = NULL;
18273 item_compare_ic = TRUE;
18274 }
18275 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018276 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018277
18278 if (argvars[2].v_type != VAR_UNKNOWN)
18279 {
18280 /* optional third argument: {dict} */
18281 if (argvars[2].v_type != VAR_DICT)
18282 {
18283 EMSG(_(e_dictreq));
18284 return;
18285 }
18286 item_compare_selfdict = argvars[2].vval.v_dict;
18287 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289
Bram Moolenaar0d660222005-01-07 21:51:51 +000018290 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018291 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018292 if (ptrs == NULL)
18293 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294
Bram Moolenaar327aa022014-03-25 18:24:23 +010018295 i = 0;
18296 if (sort)
18297 {
18298 /* sort(): ptrs will be the list to sort */
18299 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018300 {
18301 ptrs[i].item = li;
18302 ptrs[i].idx = i;
18303 ++i;
18304 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018305
18306 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018307 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018308 /* test the compare function */
18309 if (item_compare_func != NULL
18310 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018311 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018312 EMSG(_("E702: Sort compare function failed"));
18313 else
18314 {
18315 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018316 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018317 item_compare_func == NULL ? item_compare : item_compare2);
18318
18319 if (!item_compare_func_err)
18320 {
18321 /* Clear the List and append the items in sorted order. */
18322 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18323 l->lv_len = 0;
18324 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018325 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018326 }
18327 }
18328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018330 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018331 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18332
18333 /* f_uniq(): ptrs will be a stack of items to remove */
18334 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018335 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018336 item_compare_func_ptr = item_compare_func
18337 ? item_compare2 : item_compare;
18338
18339 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18340 li = li->li_next)
18341 {
18342 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18343 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018344 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018345 if (item_compare_func_err)
18346 {
18347 EMSG(_("E882: Uniq compare function failed"));
18348 break;
18349 }
18350 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018351
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018352 if (!item_compare_func_err)
18353 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018354 while (--i >= 0)
18355 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018356 li = ptrs[i].item->li_next;
18357 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018358 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018359 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018360 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018361 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018362 list_fix_watch(l, li);
18363 listitem_free(li);
18364 l->lv_len--;
18365 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018366 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018367 }
18368
18369 vim_free(ptrs);
18370 }
18371}
18372
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018373/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018374 * "sort({list})" function
18375 */
18376 static void
18377f_sort(argvars, rettv)
18378 typval_T *argvars;
18379 typval_T *rettv;
18380{
18381 do_sort_uniq(argvars, rettv, TRUE);
18382}
18383
18384/*
18385 * "uniq({list})" function
18386 */
18387 static void
18388f_uniq(argvars, rettv)
18389 typval_T *argvars;
18390 typval_T *rettv;
18391{
18392 do_sort_uniq(argvars, rettv, FALSE);
18393}
18394
18395/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018396 * "soundfold({word})" function
18397 */
18398 static void
18399f_soundfold(argvars, rettv)
18400 typval_T *argvars;
18401 typval_T *rettv;
18402{
18403 char_u *s;
18404
18405 rettv->v_type = VAR_STRING;
18406 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018407#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018408 rettv->vval.v_string = eval_soundfold(s);
18409#else
18410 rettv->vval.v_string = vim_strsave(s);
18411#endif
18412}
18413
18414/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018415 * "spellbadword()" function
18416 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018417 static void
18418f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018419 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018420 typval_T *rettv;
18421{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018422 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018423 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018424 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018425
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018426 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018427 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018428
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018429#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018430 if (argvars[0].v_type == VAR_UNKNOWN)
18431 {
18432 /* Find the start and length of the badly spelled word. */
18433 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18434 if (len != 0)
18435 word = ml_get_cursor();
18436 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018437 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018438 {
18439 char_u *str = get_tv_string_chk(&argvars[0]);
18440 int capcol = -1;
18441
18442 if (str != NULL)
18443 {
18444 /* Check the argument for spelling. */
18445 while (*str != NUL)
18446 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018447 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018448 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018449 {
18450 word = str;
18451 break;
18452 }
18453 str += len;
18454 }
18455 }
18456 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018457#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018458
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018459 list_append_string(rettv->vval.v_list, word, len);
18460 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018461 attr == HLF_SPB ? "bad" :
18462 attr == HLF_SPR ? "rare" :
18463 attr == HLF_SPL ? "local" :
18464 attr == HLF_SPC ? "caps" :
18465 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018466}
18467
18468/*
18469 * "spellsuggest()" function
18470 */
18471 static void
18472f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018473 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018474 typval_T *rettv;
18475{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018476#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018477 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018478 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018479 int maxcount;
18480 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018481 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018482 listitem_T *li;
18483 int need_capital = FALSE;
18484#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018485
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018486 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018487 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018488
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018489#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018490 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018491 {
18492 str = get_tv_string(&argvars[0]);
18493 if (argvars[1].v_type != VAR_UNKNOWN)
18494 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018495 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018496 if (maxcount <= 0)
18497 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018498 if (argvars[2].v_type != VAR_UNKNOWN)
18499 {
18500 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18501 if (typeerr)
18502 return;
18503 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018504 }
18505 else
18506 maxcount = 25;
18507
Bram Moolenaar4770d092006-01-12 23:22:24 +000018508 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018509
18510 for (i = 0; i < ga.ga_len; ++i)
18511 {
18512 str = ((char_u **)ga.ga_data)[i];
18513
18514 li = listitem_alloc();
18515 if (li == NULL)
18516 vim_free(str);
18517 else
18518 {
18519 li->li_tv.v_type = VAR_STRING;
18520 li->li_tv.v_lock = 0;
18521 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018522 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018523 }
18524 }
18525 ga_clear(&ga);
18526 }
18527#endif
18528}
18529
Bram Moolenaar0d660222005-01-07 21:51:51 +000018530 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018531f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018532 typval_T *argvars;
18533 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018534{
18535 char_u *str;
18536 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018537 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018538 regmatch_T regmatch;
18539 char_u patbuf[NUMBUFLEN];
18540 char_u *save_cpo;
18541 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018542 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018543 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018544 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018545
18546 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18547 save_cpo = p_cpo;
18548 p_cpo = (char_u *)"";
18549
18550 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018551 if (argvars[1].v_type != VAR_UNKNOWN)
18552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018553 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18554 if (pat == NULL)
18555 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018556 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018557 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018558 }
18559 if (pat == NULL || *pat == NUL)
18560 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018561
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018562 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018564 if (typeerr)
18565 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566
Bram Moolenaar0d660222005-01-07 21:51:51 +000018567 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18568 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018570 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018571 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018572 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018573 if (*str == NUL)
18574 match = FALSE; /* empty item at the end */
18575 else
18576 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018577 if (match)
18578 end = regmatch.startp[0];
18579 else
18580 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018581 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18582 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018583 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018584 if (list_append_string(rettv->vval.v_list, str,
18585 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018586 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018587 }
18588 if (!match)
18589 break;
18590 /* Advance to just after the match. */
18591 if (regmatch.endp[0] > str)
18592 col = 0;
18593 else
18594 {
18595 /* Don't get stuck at the same match. */
18596#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018597 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018598#else
18599 col = 1;
18600#endif
18601 }
18602 str = regmatch.endp[0];
18603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604
Bram Moolenaar473de612013-06-08 18:19:48 +020018605 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607
Bram Moolenaar0d660222005-01-07 21:51:51 +000018608 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609}
18610
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018611#ifdef FEAT_FLOAT
18612/*
18613 * "sqrt()" function
18614 */
18615 static void
18616f_sqrt(argvars, rettv)
18617 typval_T *argvars;
18618 typval_T *rettv;
18619{
18620 float_T f;
18621
18622 rettv->v_type = VAR_FLOAT;
18623 if (get_float_arg(argvars, &f) == OK)
18624 rettv->vval.v_float = sqrt(f);
18625 else
18626 rettv->vval.v_float = 0.0;
18627}
18628
18629/*
18630 * "str2float()" function
18631 */
18632 static void
18633f_str2float(argvars, rettv)
18634 typval_T *argvars;
18635 typval_T *rettv;
18636{
18637 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18638
18639 if (*p == '+')
18640 p = skipwhite(p + 1);
18641 (void)string2float(p, &rettv->vval.v_float);
18642 rettv->v_type = VAR_FLOAT;
18643}
18644#endif
18645
Bram Moolenaar2c932302006-03-18 21:42:09 +000018646/*
18647 * "str2nr()" function
18648 */
18649 static void
18650f_str2nr(argvars, rettv)
18651 typval_T *argvars;
18652 typval_T *rettv;
18653{
18654 int base = 10;
18655 char_u *p;
18656 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018657 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018658
18659 if (argvars[1].v_type != VAR_UNKNOWN)
18660 {
18661 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018662 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018663 {
18664 EMSG(_(e_invarg));
18665 return;
18666 }
18667 }
18668
18669 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018670 if (*p == '+')
18671 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018672 switch (base)
18673 {
18674 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18675 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18676 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18677 default: what = 0;
18678 }
18679 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018680 rettv->vval.v_number = n;
18681}
18682
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683#ifdef HAVE_STRFTIME
18684/*
18685 * "strftime({format}[, {time}])" function
18686 */
18687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018688f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018689 typval_T *argvars;
18690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691{
18692 char_u result_buf[256];
18693 struct tm *curtime;
18694 time_t seconds;
18695 char_u *p;
18696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018697 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018699 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018700 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701 seconds = time(NULL);
18702 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018703 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704 curtime = localtime(&seconds);
18705 /* MSVC returns NULL for an invalid value of seconds. */
18706 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018707 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708 else
18709 {
18710# ifdef FEAT_MBYTE
18711 vimconv_T conv;
18712 char_u *enc;
18713
18714 conv.vc_type = CONV_NONE;
18715 enc = enc_locale();
18716 convert_setup(&conv, p_enc, enc);
18717 if (conv.vc_type != CONV_NONE)
18718 p = string_convert(&conv, p, NULL);
18719# endif
18720 if (p != NULL)
18721 (void)strftime((char *)result_buf, sizeof(result_buf),
18722 (char *)p, curtime);
18723 else
18724 result_buf[0] = NUL;
18725
18726# ifdef FEAT_MBYTE
18727 if (conv.vc_type != CONV_NONE)
18728 vim_free(p);
18729 convert_setup(&conv, enc, p_enc);
18730 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018731 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732 else
18733# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018734 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735
18736# ifdef FEAT_MBYTE
18737 /* Release conversion descriptors */
18738 convert_setup(&conv, NULL, NULL);
18739 vim_free(enc);
18740# endif
18741 }
18742}
18743#endif
18744
18745/*
18746 * "stridx()" function
18747 */
18748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018749f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018750 typval_T *argvars;
18751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752{
18753 char_u buf[NUMBUFLEN];
18754 char_u *needle;
18755 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018756 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018758 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018760 needle = get_tv_string_chk(&argvars[1]);
18761 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018762 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018763 if (needle == NULL || haystack == NULL)
18764 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765
Bram Moolenaar33570922005-01-25 22:26:29 +000018766 if (argvars[2].v_type != VAR_UNKNOWN)
18767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018768 int error = FALSE;
18769
18770 start_idx = get_tv_number_chk(&argvars[2], &error);
18771 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018772 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018773 if (start_idx >= 0)
18774 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018775 }
18776
18777 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18778 if (pos != NULL)
18779 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780}
18781
18782/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018783 * "string()" function
18784 */
18785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018786f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018787 typval_T *argvars;
18788 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018789{
18790 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018791 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018793 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018794 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018795 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018796 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018797 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798}
18799
18800/*
18801 * "strlen()" function
18802 */
18803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018804f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018805 typval_T *argvars;
18806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018808 rettv->vval.v_number = (varnumber_T)(STRLEN(
18809 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810}
18811
18812/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018813 * "strchars()" function
18814 */
18815 static void
18816f_strchars(argvars, rettv)
18817 typval_T *argvars;
18818 typval_T *rettv;
18819{
18820 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018821 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018822#ifdef FEAT_MBYTE
18823 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018824 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018825#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018826
18827 if (argvars[1].v_type != VAR_UNKNOWN)
18828 skipcc = get_tv_number_chk(&argvars[1], NULL);
18829 if (skipcc < 0 || skipcc > 1)
18830 EMSG(_(e_invarg));
18831 else
18832 {
18833#ifdef FEAT_MBYTE
18834 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18835 while (*s != NUL)
18836 {
18837 func_mb_ptr2char_adv(&s);
18838 ++len;
18839 }
18840 rettv->vval.v_number = len;
18841#else
18842 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18843#endif
18844 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018845}
18846
18847/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018848 * "strdisplaywidth()" function
18849 */
18850 static void
18851f_strdisplaywidth(argvars, rettv)
18852 typval_T *argvars;
18853 typval_T *rettv;
18854{
18855 char_u *s = get_tv_string(&argvars[0]);
18856 int col = 0;
18857
18858 if (argvars[1].v_type != VAR_UNKNOWN)
18859 col = get_tv_number(&argvars[1]);
18860
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018861 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018862}
18863
18864/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018865 * "strwidth()" function
18866 */
18867 static void
18868f_strwidth(argvars, rettv)
18869 typval_T *argvars;
18870 typval_T *rettv;
18871{
18872 char_u *s = get_tv_string(&argvars[0]);
18873
18874 rettv->vval.v_number = (varnumber_T)(
18875#ifdef FEAT_MBYTE
18876 mb_string2cells(s, -1)
18877#else
18878 STRLEN(s)
18879#endif
18880 );
18881}
18882
18883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 * "strpart()" function
18885 */
18886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018887f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018888 typval_T *argvars;
18889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890{
18891 char_u *p;
18892 int n;
18893 int len;
18894 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018895 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018897 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 slen = (int)STRLEN(p);
18899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018900 n = get_tv_number_chk(&argvars[1], &error);
18901 if (error)
18902 len = 0;
18903 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018904 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 else
18906 len = slen - n; /* default len: all bytes that are available. */
18907
18908 /*
18909 * Only return the overlap between the specified part and the actual
18910 * string.
18911 */
18912 if (n < 0)
18913 {
18914 len += n;
18915 n = 0;
18916 }
18917 else if (n > slen)
18918 n = slen;
18919 if (len < 0)
18920 len = 0;
18921 else if (n + len > slen)
18922 len = slen - n;
18923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018924 rettv->v_type = VAR_STRING;
18925 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926}
18927
18928/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018929 * "strridx()" function
18930 */
18931 static void
18932f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018933 typval_T *argvars;
18934 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018935{
18936 char_u buf[NUMBUFLEN];
18937 char_u *needle;
18938 char_u *haystack;
18939 char_u *rest;
18940 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018941 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018943 needle = get_tv_string_chk(&argvars[1]);
18944 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018945
18946 rettv->vval.v_number = -1;
18947 if (needle == NULL || haystack == NULL)
18948 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018949
18950 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018951 if (argvars[2].v_type != VAR_UNKNOWN)
18952 {
18953 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018954 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018955 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018956 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018957 }
18958 else
18959 end_idx = haystack_len;
18960
Bram Moolenaar0d660222005-01-07 21:51:51 +000018961 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018962 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018963 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018964 lastmatch = haystack + end_idx;
18965 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018966 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018967 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018968 for (rest = haystack; *rest != '\0'; ++rest)
18969 {
18970 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018971 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018972 break;
18973 lastmatch = rest;
18974 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018975 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018976
18977 if (lastmatch == NULL)
18978 rettv->vval.v_number = -1;
18979 else
18980 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18981}
18982
18983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 * "strtrans()" function
18985 */
18986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018987f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018988 typval_T *argvars;
18989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018991 rettv->v_type = VAR_STRING;
18992 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993}
18994
18995/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018996 * "submatch()" function
18997 */
18998 static void
18999f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019000 typval_T *argvars;
19001 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019002{
Bram Moolenaar41571762014-04-02 19:00:58 +020019003 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019004 int no;
19005 int retList = 0;
19006
19007 no = (int)get_tv_number_chk(&argvars[0], &error);
19008 if (error)
19009 return;
19010 error = FALSE;
19011 if (argvars[1].v_type != VAR_UNKNOWN)
19012 retList = get_tv_number_chk(&argvars[1], &error);
19013 if (error)
19014 return;
19015
19016 if (retList == 0)
19017 {
19018 rettv->v_type = VAR_STRING;
19019 rettv->vval.v_string = reg_submatch(no);
19020 }
19021 else
19022 {
19023 rettv->v_type = VAR_LIST;
19024 rettv->vval.v_list = reg_submatch_list(no);
19025 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019026}
19027
19028/*
19029 * "substitute()" function
19030 */
19031 static void
19032f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019033 typval_T *argvars;
19034 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019035{
19036 char_u patbuf[NUMBUFLEN];
19037 char_u subbuf[NUMBUFLEN];
19038 char_u flagsbuf[NUMBUFLEN];
19039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019040 char_u *str = get_tv_string_chk(&argvars[0]);
19041 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19042 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19043 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19044
Bram Moolenaar0d660222005-01-07 21:51:51 +000019045 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019046 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19047 rettv->vval.v_string = NULL;
19048 else
19049 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019050}
19051
19052/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019053 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019056f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019057 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059{
19060 int id = 0;
19061#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019062 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063 long col;
19064 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019065 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019067 lnum = get_tv_lnum(argvars); /* -1 on type error */
19068 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19069 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019071 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019072 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019073 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074#endif
19075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019076 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077}
19078
19079/*
19080 * "synIDattr(id, what [, mode])" function
19081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019083f_synIDattr(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 char_u *p = NULL;
19088#ifdef FEAT_SYN_HL
19089 int id;
19090 char_u *what;
19091 char_u *mode;
19092 char_u modebuf[NUMBUFLEN];
19093 int modec;
19094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019095 id = get_tv_number(&argvars[0]);
19096 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019097 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019099 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019101 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102 modec = 0; /* replace invalid with current */
19103 }
19104 else
19105 {
19106#ifdef FEAT_GUI
19107 if (gui.in_use)
19108 modec = 'g';
19109 else
19110#endif
19111 if (t_colors > 1)
19112 modec = 'c';
19113 else
19114 modec = 't';
19115 }
19116
19117
19118 switch (TOLOWER_ASC(what[0]))
19119 {
19120 case 'b':
19121 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19122 p = highlight_color(id, what, modec);
19123 else /* bold */
19124 p = highlight_has_attr(id, HL_BOLD, modec);
19125 break;
19126
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019127 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 p = highlight_color(id, what, modec);
19129 break;
19130
19131 case 'i':
19132 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19133 p = highlight_has_attr(id, HL_INVERSE, modec);
19134 else /* italic */
19135 p = highlight_has_attr(id, HL_ITALIC, modec);
19136 break;
19137
19138 case 'n': /* name */
19139 p = get_highlight_name(NULL, id - 1);
19140 break;
19141
19142 case 'r': /* reverse */
19143 p = highlight_has_attr(id, HL_INVERSE, modec);
19144 break;
19145
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019146 case 's':
19147 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19148 p = highlight_color(id, what, modec);
19149 else /* standout */
19150 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151 break;
19152
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019153 case 'u':
19154 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19155 /* underline */
19156 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19157 else
19158 /* undercurl */
19159 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 break;
19161 }
19162
19163 if (p != NULL)
19164 p = vim_strsave(p);
19165#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019166 rettv->v_type = VAR_STRING;
19167 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168}
19169
19170/*
19171 * "synIDtrans(id)" function
19172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019174f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019175 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177{
19178 int id;
19179
19180#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019181 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182
19183 if (id > 0)
19184 id = syn_get_final_id(id);
19185 else
19186#endif
19187 id = 0;
19188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019189 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190}
19191
19192/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019193 * "synconcealed(lnum, col)" function
19194 */
19195 static void
19196f_synconcealed(argvars, rettv)
19197 typval_T *argvars UNUSED;
19198 typval_T *rettv;
19199{
19200#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19201 long lnum;
19202 long col;
19203 int syntax_flags = 0;
19204 int cchar;
19205 int matchid = 0;
19206 char_u str[NUMBUFLEN];
19207#endif
19208
19209 rettv->v_type = VAR_LIST;
19210 rettv->vval.v_list = NULL;
19211
19212#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19213 lnum = get_tv_lnum(argvars); /* -1 on type error */
19214 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19215
19216 vim_memset(str, NUL, sizeof(str));
19217
19218 if (rettv_list_alloc(rettv) != FAIL)
19219 {
19220 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19221 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19222 && curwin->w_p_cole > 0)
19223 {
19224 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19225 syntax_flags = get_syntax_info(&matchid);
19226
19227 /* get the conceal character */
19228 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19229 {
19230 cchar = syn_get_sub_char();
19231 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19232 cchar = lcs_conceal;
19233 if (cchar != NUL)
19234 {
19235# ifdef FEAT_MBYTE
19236 if (has_mbyte)
19237 (*mb_char2bytes)(cchar, str);
19238 else
19239# endif
19240 str[0] = cchar;
19241 }
19242 }
19243 }
19244
19245 list_append_number(rettv->vval.v_list,
19246 (syntax_flags & HL_CONCEAL) != 0);
19247 /* -1 to auto-determine strlen */
19248 list_append_string(rettv->vval.v_list, str, -1);
19249 list_append_number(rettv->vval.v_list, matchid);
19250 }
19251#endif
19252}
19253
19254/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019255 * "synstack(lnum, col)" function
19256 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019257 static void
19258f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019259 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019260 typval_T *rettv;
19261{
19262#ifdef FEAT_SYN_HL
19263 long lnum;
19264 long col;
19265 int i;
19266 int id;
19267#endif
19268
19269 rettv->v_type = VAR_LIST;
19270 rettv->vval.v_list = NULL;
19271
19272#ifdef FEAT_SYN_HL
19273 lnum = get_tv_lnum(argvars); /* -1 on type error */
19274 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19275
19276 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019277 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019278 && rettv_list_alloc(rettv) != FAIL)
19279 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019280 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019281 for (i = 0; ; ++i)
19282 {
19283 id = syn_get_stack_item(i);
19284 if (id < 0)
19285 break;
19286 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19287 break;
19288 }
19289 }
19290#endif
19291}
19292
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019294get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019295 typval_T *argvars;
19296 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019297 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019299 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019301 char_u *infile = NULL;
19302 char_u buf[NUMBUFLEN];
19303 int err = FALSE;
19304 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019305 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019306 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019308 rettv->v_type = VAR_STRING;
19309 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019310 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019311 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019312
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019313 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019314 {
19315 /*
19316 * Write the string to a temp file, to be used for input of the shell
19317 * command.
19318 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019319 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019320 {
19321 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019322 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019323 }
19324
19325 fd = mch_fopen((char *)infile, WRITEBIN);
19326 if (fd == NULL)
19327 {
19328 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019329 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019330 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019331 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019332 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019333 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19334 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019335 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019336 else
19337 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019338 size_t len;
19339
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019340 p = get_tv_string_buf_chk(&argvars[1], buf);
19341 if (p == NULL)
19342 {
19343 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019344 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019345 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019346 len = STRLEN(p);
19347 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019348 err = TRUE;
19349 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019350 if (fclose(fd) != 0)
19351 err = TRUE;
19352 if (err)
19353 {
19354 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019355 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019356 }
19357 }
19358
Bram Moolenaar52a72462014-08-29 15:53:52 +020019359 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19360 * echoes typeahead, that messes up the display. */
19361 if (!msg_silent)
19362 flags += SHELL_COOKED;
19363
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019364 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019366 int len;
19367 listitem_T *li;
19368 char_u *s = NULL;
19369 char_u *start;
19370 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019371 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372
Bram Moolenaar52a72462014-08-29 15:53:52 +020019373 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019374 if (res == NULL)
19375 goto errret;
19376
19377 list = list_alloc();
19378 if (list == NULL)
19379 goto errret;
19380
19381 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019383 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019384 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019385 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019386 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019387
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019388 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019389 if (s == NULL)
19390 goto errret;
19391
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019392 for (p = s; start < end; ++p, ++start)
19393 *p = *start == NUL ? NL : *start;
19394 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019395
19396 li = listitem_alloc();
19397 if (li == NULL)
19398 {
19399 vim_free(s);
19400 goto errret;
19401 }
19402 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019403 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019404 li->li_tv.vval.v_string = s;
19405 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019407
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019408 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019409 rettv->v_type = VAR_LIST;
19410 rettv->vval.v_list = list;
19411 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019413 else
19414 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019415 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019416#ifdef USE_CR
19417 /* translate <CR> into <NL> */
19418 if (res != NULL)
19419 {
19420 char_u *s;
19421
19422 for (s = res; *s; ++s)
19423 {
19424 if (*s == CAR)
19425 *s = NL;
19426 }
19427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428#else
19429# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019430 /* translate <CR><NL> into <NL> */
19431 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019433 char_u *s, *d;
19434
19435 d = res;
19436 for (s = res; *s; ++s)
19437 {
19438 if (s[0] == CAR && s[1] == NL)
19439 ++s;
19440 *d++ = *s;
19441 }
19442 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019444# endif
19445#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019446 rettv->vval.v_string = res;
19447 res = NULL;
19448 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019449
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019450errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019451 if (infile != NULL)
19452 {
19453 mch_remove(infile);
19454 vim_free(infile);
19455 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019456 if (res != NULL)
19457 vim_free(res);
19458 if (list != NULL)
19459 list_free(list, TRUE);
19460}
19461
19462/*
19463 * "system()" function
19464 */
19465 static void
19466f_system(argvars, rettv)
19467 typval_T *argvars;
19468 typval_T *rettv;
19469{
19470 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19471}
19472
19473/*
19474 * "systemlist()" function
19475 */
19476 static void
19477f_systemlist(argvars, rettv)
19478 typval_T *argvars;
19479 typval_T *rettv;
19480{
19481 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482}
19483
19484/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019485 * "tabpagebuflist()" function
19486 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019487 static void
19488f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019489 typval_T *argvars UNUSED;
19490 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019491{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019492#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019493 tabpage_T *tp;
19494 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019495
19496 if (argvars[0].v_type == VAR_UNKNOWN)
19497 wp = firstwin;
19498 else
19499 {
19500 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19501 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019502 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019503 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019504 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019505 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019506 for (; wp != NULL; wp = wp->w_next)
19507 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019508 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019509 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019510 }
19511#endif
19512}
19513
19514
19515/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019516 * "tabpagenr()" function
19517 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019518 static void
19519f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019520 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019521 typval_T *rettv;
19522{
19523 int nr = 1;
19524#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019525 char_u *arg;
19526
19527 if (argvars[0].v_type != VAR_UNKNOWN)
19528 {
19529 arg = get_tv_string_chk(&argvars[0]);
19530 nr = 0;
19531 if (arg != NULL)
19532 {
19533 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019534 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019535 else
19536 EMSG2(_(e_invexpr2), arg);
19537 }
19538 }
19539 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019540 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019541#endif
19542 rettv->vval.v_number = nr;
19543}
19544
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019545
19546#ifdef FEAT_WINDOWS
19547static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19548
19549/*
19550 * Common code for tabpagewinnr() and winnr().
19551 */
19552 static int
19553get_winnr(tp, argvar)
19554 tabpage_T *tp;
19555 typval_T *argvar;
19556{
19557 win_T *twin;
19558 int nr = 1;
19559 win_T *wp;
19560 char_u *arg;
19561
19562 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19563 if (argvar->v_type != VAR_UNKNOWN)
19564 {
19565 arg = get_tv_string_chk(argvar);
19566 if (arg == NULL)
19567 nr = 0; /* type error; errmsg already given */
19568 else if (STRCMP(arg, "$") == 0)
19569 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19570 else if (STRCMP(arg, "#") == 0)
19571 {
19572 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19573 if (twin == NULL)
19574 nr = 0;
19575 }
19576 else
19577 {
19578 EMSG2(_(e_invexpr2), arg);
19579 nr = 0;
19580 }
19581 }
19582
19583 if (nr > 0)
19584 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19585 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019586 {
19587 if (wp == NULL)
19588 {
19589 /* didn't find it in this tabpage */
19590 nr = 0;
19591 break;
19592 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019593 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019594 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019595 return nr;
19596}
19597#endif
19598
19599/*
19600 * "tabpagewinnr()" function
19601 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019602 static void
19603f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019604 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019605 typval_T *rettv;
19606{
19607 int nr = 1;
19608#ifdef FEAT_WINDOWS
19609 tabpage_T *tp;
19610
19611 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19612 if (tp == NULL)
19613 nr = 0;
19614 else
19615 nr = get_winnr(tp, &argvars[1]);
19616#endif
19617 rettv->vval.v_number = nr;
19618}
19619
19620
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019621/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019622 * "tagfiles()" function
19623 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019624 static void
19625f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019626 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019627 typval_T *rettv;
19628{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019629 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019630 tagname_T tn;
19631 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019632
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019633 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019634 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019635 fname = alloc(MAXPATHL);
19636 if (fname == NULL)
19637 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019638
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019639 for (first = TRUE; ; first = FALSE)
19640 if (get_tagfname(&tn, first, fname) == FAIL
19641 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019642 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019643 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019644 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019645}
19646
19647/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019648 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019649 */
19650 static void
19651f_taglist(argvars, rettv)
19652 typval_T *argvars;
19653 typval_T *rettv;
19654{
19655 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019656
19657 tag_pattern = get_tv_string(&argvars[0]);
19658
19659 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019660 if (*tag_pattern == NUL)
19661 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019662
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019663 if (rettv_list_alloc(rettv) == OK)
19664 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019665}
19666
19667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668 * "tempname()" function
19669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019671f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019672 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674{
19675 static int x = 'A';
19676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019677 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019678 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679
19680 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19681 * names. Skip 'I' and 'O', they are used for shell redirection. */
19682 do
19683 {
19684 if (x == 'Z')
19685 x = '0';
19686 else if (x == '9')
19687 x = 'A';
19688 else
19689 {
19690#ifdef EBCDIC
19691 if (x == 'I')
19692 x = 'J';
19693 else if (x == 'R')
19694 x = 'S';
19695 else
19696#endif
19697 ++x;
19698 }
19699 } while (x == 'I' || x == 'O');
19700}
19701
19702/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019703 * "test(list)" function: Just checking the walls...
19704 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019705 static void
19706f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019707 typval_T *argvars UNUSED;
19708 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019709{
19710 /* Used for unit testing. Change the code below to your liking. */
19711#if 0
19712 listitem_T *li;
19713 list_T *l;
19714 char_u *bad, *good;
19715
19716 if (argvars[0].v_type != VAR_LIST)
19717 return;
19718 l = argvars[0].vval.v_list;
19719 if (l == NULL)
19720 return;
19721 li = l->lv_first;
19722 if (li == NULL)
19723 return;
19724 bad = get_tv_string(&li->li_tv);
19725 li = li->li_next;
19726 if (li == NULL)
19727 return;
19728 good = get_tv_string(&li->li_tv);
19729 rettv->vval.v_number = test_edit_score(bad, good);
19730#endif
19731}
19732
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019733#ifdef FEAT_FLOAT
19734/*
19735 * "tan()" function
19736 */
19737 static void
19738f_tan(argvars, rettv)
19739 typval_T *argvars;
19740 typval_T *rettv;
19741{
19742 float_T f;
19743
19744 rettv->v_type = VAR_FLOAT;
19745 if (get_float_arg(argvars, &f) == OK)
19746 rettv->vval.v_float = tan(f);
19747 else
19748 rettv->vval.v_float = 0.0;
19749}
19750
19751/*
19752 * "tanh()" function
19753 */
19754 static void
19755f_tanh(argvars, rettv)
19756 typval_T *argvars;
19757 typval_T *rettv;
19758{
19759 float_T f;
19760
19761 rettv->v_type = VAR_FLOAT;
19762 if (get_float_arg(argvars, &f) == OK)
19763 rettv->vval.v_float = tanh(f);
19764 else
19765 rettv->vval.v_float = 0.0;
19766}
19767#endif
19768
Bram Moolenaard52d9742005-08-21 22:20:28 +000019769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 * "tolower(string)" function
19771 */
19772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019773f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019774 typval_T *argvars;
19775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776{
19777 char_u *p;
19778
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019779 p = vim_strsave(get_tv_string(&argvars[0]));
19780 rettv->v_type = VAR_STRING;
19781 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782
19783 if (p != NULL)
19784 while (*p != NUL)
19785 {
19786#ifdef FEAT_MBYTE
19787 int l;
19788
19789 if (enc_utf8)
19790 {
19791 int c, lc;
19792
19793 c = utf_ptr2char(p);
19794 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019795 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 /* TODO: reallocate string when byte count changes. */
19797 if (utf_char2len(lc) == l)
19798 utf_char2bytes(lc, p);
19799 p += l;
19800 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019801 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 p += l; /* skip multi-byte character */
19803 else
19804#endif
19805 {
19806 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19807 ++p;
19808 }
19809 }
19810}
19811
19812/*
19813 * "toupper(string)" function
19814 */
19815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019816f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019817 typval_T *argvars;
19818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019820 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019821 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822}
19823
19824/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019825 * "tr(string, fromstr, tostr)" function
19826 */
19827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019828f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019829 typval_T *argvars;
19830 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019831{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019832 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019833 char_u *fromstr;
19834 char_u *tostr;
19835 char_u *p;
19836#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019837 int inlen;
19838 int fromlen;
19839 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019840 int idx;
19841 char_u *cpstr;
19842 int cplen;
19843 int first = TRUE;
19844#endif
19845 char_u buf[NUMBUFLEN];
19846 char_u buf2[NUMBUFLEN];
19847 garray_T ga;
19848
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019849 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019850 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19851 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019852
19853 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019854 rettv->v_type = VAR_STRING;
19855 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019856 if (fromstr == NULL || tostr == NULL)
19857 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019858 ga_init2(&ga, (int)sizeof(char), 80);
19859
19860#ifdef FEAT_MBYTE
19861 if (!has_mbyte)
19862#endif
19863 /* not multi-byte: fromstr and tostr must be the same length */
19864 if (STRLEN(fromstr) != STRLEN(tostr))
19865 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019866#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019867error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019868#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019869 EMSG2(_(e_invarg2), fromstr);
19870 ga_clear(&ga);
19871 return;
19872 }
19873
19874 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019875 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019876 {
19877#ifdef FEAT_MBYTE
19878 if (has_mbyte)
19879 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019880 inlen = (*mb_ptr2len)(in_str);
19881 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019882 cplen = inlen;
19883 idx = 0;
19884 for (p = fromstr; *p != NUL; p += fromlen)
19885 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019886 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019887 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019888 {
19889 for (p = tostr; *p != NUL; p += tolen)
19890 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019891 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019892 if (idx-- == 0)
19893 {
19894 cplen = tolen;
19895 cpstr = p;
19896 break;
19897 }
19898 }
19899 if (*p == NUL) /* tostr is shorter than fromstr */
19900 goto error;
19901 break;
19902 }
19903 ++idx;
19904 }
19905
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019906 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019907 {
19908 /* Check that fromstr and tostr have the same number of
19909 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019910 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019911 first = FALSE;
19912 for (p = tostr; *p != NUL; p += tolen)
19913 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019914 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019915 --idx;
19916 }
19917 if (idx != 0)
19918 goto error;
19919 }
19920
Bram Moolenaarcde88542015-08-11 19:14:00 +020019921 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019922 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019923 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019924
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019925 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019926 }
19927 else
19928#endif
19929 {
19930 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019931 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019932 if (p != NULL)
19933 ga_append(&ga, tostr[p - fromstr]);
19934 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019935 ga_append(&ga, *in_str);
19936 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019937 }
19938 }
19939
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019940 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019941 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019942 ga_append(&ga, NUL);
19943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019944 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019945}
19946
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019947#ifdef FEAT_FLOAT
19948/*
19949 * "trunc({float})" function
19950 */
19951 static void
19952f_trunc(argvars, rettv)
19953 typval_T *argvars;
19954 typval_T *rettv;
19955{
19956 float_T f;
19957
19958 rettv->v_type = VAR_FLOAT;
19959 if (get_float_arg(argvars, &f) == OK)
19960 /* trunc() is not in C90, use floor() or ceil() instead. */
19961 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19962 else
19963 rettv->vval.v_float = 0.0;
19964}
19965#endif
19966
Bram Moolenaar8299df92004-07-10 09:47:34 +000019967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 * "type(expr)" function
19969 */
19970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019971f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019972 typval_T *argvars;
19973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019975 int n;
19976
19977 switch (argvars[0].v_type)
19978 {
19979 case VAR_NUMBER: n = 0; break;
19980 case VAR_STRING: n = 1; break;
19981 case VAR_FUNC: n = 2; break;
19982 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019983 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019984#ifdef FEAT_FLOAT
19985 case VAR_FLOAT: n = 5; break;
19986#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019987 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19988 }
19989 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990}
19991
19992/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019993 * "undofile(name)" function
19994 */
19995 static void
19996f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019997 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019998 typval_T *rettv;
19999{
20000 rettv->v_type = VAR_STRING;
20001#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020002 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020003 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020004
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020005 if (*fname == NUL)
20006 {
20007 /* If there is no file name there will be no undo file. */
20008 rettv->vval.v_string = NULL;
20009 }
20010 else
20011 {
20012 char_u *ffname = FullName_save(fname, FALSE);
20013
20014 if (ffname != NULL)
20015 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20016 vim_free(ffname);
20017 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020018 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020019#else
20020 rettv->vval.v_string = NULL;
20021#endif
20022}
20023
20024/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020025 * "undotree()" function
20026 */
20027 static void
20028f_undotree(argvars, rettv)
20029 typval_T *argvars UNUSED;
20030 typval_T *rettv;
20031{
20032 if (rettv_dict_alloc(rettv) == OK)
20033 {
20034 dict_T *dict = rettv->vval.v_dict;
20035 list_T *list;
20036
Bram Moolenaar730cde92010-06-27 05:18:54 +020020037 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020038 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020039 dict_add_nr_str(dict, "save_last",
20040 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020041 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20042 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020043 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020044
20045 list = list_alloc();
20046 if (list != NULL)
20047 {
20048 u_eval_tree(curbuf->b_u_oldhead, list);
20049 dict_add_list(dict, "entries", list);
20050 }
20051 }
20052}
20053
20054/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020055 * "values(dict)" function
20056 */
20057 static void
20058f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020059 typval_T *argvars;
20060 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020061{
20062 dict_list(argvars, rettv, 1);
20063}
20064
20065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066 * "virtcol(string)" function
20067 */
20068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020069f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020070 typval_T *argvars;
20071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072{
20073 colnr_T vcol = 0;
20074 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020075 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020077 fp = var2fpos(&argvars[0], FALSE, &fnum);
20078 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20079 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080 {
20081 getvvcol(curwin, fp, NULL, NULL, &vcol);
20082 ++vcol;
20083 }
20084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020085 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086}
20087
20088/*
20089 * "visualmode()" function
20090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020092f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020093 typval_T *argvars UNUSED;
20094 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 char_u str[2];
20097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020098 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 str[0] = curbuf->b_visual_mode_eval;
20100 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020101 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102
20103 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020104 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106}
20107
20108/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020109 * "wildmenumode()" function
20110 */
20111 static void
20112f_wildmenumode(argvars, rettv)
20113 typval_T *argvars UNUSED;
20114 typval_T *rettv UNUSED;
20115{
20116#ifdef FEAT_WILDMENU
20117 if (wild_menu_showing)
20118 rettv->vval.v_number = 1;
20119#endif
20120}
20121
20122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123 * "winbufnr(nr)" function
20124 */
20125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020126f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 typval_T *argvars;
20128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129{
20130 win_T *wp;
20131
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020132 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020134 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020136 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137}
20138
20139/*
20140 * "wincol()" function
20141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020143f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020144 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146{
20147 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020148 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149}
20150
20151/*
20152 * "winheight(nr)" function
20153 */
20154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020155f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020156 typval_T *argvars;
20157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158{
20159 win_T *wp;
20160
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020161 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020163 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020165 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166}
20167
20168/*
20169 * "winline()" function
20170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020172f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020173 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175{
20176 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020177 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178}
20179
20180/*
20181 * "winnr()" function
20182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020184f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020185 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187{
20188 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020189
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020191 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020193 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194}
20195
20196/*
20197 * "winrestcmd()" function
20198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020200f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020201 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203{
20204#ifdef FEAT_WINDOWS
20205 win_T *wp;
20206 int winnr = 1;
20207 garray_T ga;
20208 char_u buf[50];
20209
20210 ga_init2(&ga, (int)sizeof(char), 70);
20211 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20212 {
20213 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20214 ga_concat(&ga, buf);
20215# ifdef FEAT_VERTSPLIT
20216 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20217 ga_concat(&ga, buf);
20218# endif
20219 ++winnr;
20220 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020221 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020223 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020225 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020227 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228}
20229
20230/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020231 * "winrestview()" function
20232 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020233 static void
20234f_winrestview(argvars, rettv)
20235 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020236 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020237{
20238 dict_T *dict;
20239
20240 if (argvars[0].v_type != VAR_DICT
20241 || (dict = argvars[0].vval.v_dict) == NULL)
20242 EMSG(_(e_invarg));
20243 else
20244 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020245 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20246 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20247 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20248 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020249#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020250 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20251 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020252#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020253 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20254 {
20255 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20256 curwin->w_set_curswant = FALSE;
20257 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020258
Bram Moolenaar82c25852014-05-28 16:47:16 +020020259 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20260 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020261#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020262 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20263 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020264#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020265 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20266 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20267 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20268 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020269
20270 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020271 win_new_height(curwin, curwin->w_height);
20272# ifdef FEAT_VERTSPLIT
20273 win_new_width(curwin, W_WIDTH(curwin));
20274# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020275 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020276
Bram Moolenaarb851a962014-10-31 15:45:52 +010020277 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020278 curwin->w_topline = 1;
20279 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20280 curwin->w_topline = curbuf->b_ml.ml_line_count;
20281#ifdef FEAT_DIFF
20282 check_topfill(curwin, TRUE);
20283#endif
20284 }
20285}
20286
20287/*
20288 * "winsaveview()" function
20289 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020290 static void
20291f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020292 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020293 typval_T *rettv;
20294{
20295 dict_T *dict;
20296
Bram Moolenaara800b422010-06-27 01:15:55 +020020297 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020298 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020299 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020300
20301 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20302 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20303#ifdef FEAT_VIRTUALEDIT
20304 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20305#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020306 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020307 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20308
20309 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20310#ifdef FEAT_DIFF
20311 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20312#endif
20313 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20314 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20315}
20316
20317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 * "winwidth(nr)" function
20319 */
20320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020321f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020322 typval_T *argvars;
20323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324{
20325 win_T *wp;
20326
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020327 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020329 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 else
20331#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020332 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020334 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335#endif
20336}
20337
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020339 * "wordcount()" function
20340 */
20341 static void
20342f_wordcount(argvars, rettv)
20343 typval_T *argvars UNUSED;
20344 typval_T *rettv;
20345{
20346 if (rettv_dict_alloc(rettv) == FAIL)
20347 return;
20348 cursor_pos_info(rettv->vval.v_dict);
20349}
20350
20351/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020352 * Write list of strings to file
20353 */
20354 static int
20355write_list(fd, list, binary)
20356 FILE *fd;
20357 list_T *list;
20358 int binary;
20359{
20360 listitem_T *li;
20361 int c;
20362 int ret = OK;
20363 char_u *s;
20364
20365 for (li = list->lv_first; li != NULL; li = li->li_next)
20366 {
20367 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20368 {
20369 if (*s == '\n')
20370 c = putc(NUL, fd);
20371 else
20372 c = putc(*s, fd);
20373 if (c == EOF)
20374 {
20375 ret = FAIL;
20376 break;
20377 }
20378 }
20379 if (!binary || li->li_next != NULL)
20380 if (putc('\n', fd) == EOF)
20381 {
20382 ret = FAIL;
20383 break;
20384 }
20385 if (ret == FAIL)
20386 {
20387 EMSG(_(e_write));
20388 break;
20389 }
20390 }
20391 return ret;
20392}
20393
20394/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020395 * "writefile()" function
20396 */
20397 static void
20398f_writefile(argvars, rettv)
20399 typval_T *argvars;
20400 typval_T *rettv;
20401{
20402 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020403 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020404 char_u *fname;
20405 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020406 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020407
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020408 if (check_restricted() || check_secure())
20409 return;
20410
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020411 if (argvars[0].v_type != VAR_LIST)
20412 {
20413 EMSG2(_(e_listarg), "writefile()");
20414 return;
20415 }
20416 if (argvars[0].vval.v_list == NULL)
20417 return;
20418
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020419 if (argvars[2].v_type != VAR_UNKNOWN)
20420 {
20421 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20422 binary = TRUE;
20423 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20424 append = TRUE;
20425 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020426
20427 /* Always open the file in binary mode, library functions have a mind of
20428 * their own about CR-LF conversion. */
20429 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020430 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20431 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020432 {
20433 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20434 ret = -1;
20435 }
20436 else
20437 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020438 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20439 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020440 fclose(fd);
20441 }
20442
20443 rettv->vval.v_number = ret;
20444}
20445
20446/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020447 * "xor(expr, expr)" function
20448 */
20449 static void
20450f_xor(argvars, rettv)
20451 typval_T *argvars;
20452 typval_T *rettv;
20453{
20454 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20455 ^ get_tv_number_chk(&argvars[1], NULL);
20456}
20457
20458
20459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020461 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462 */
20463 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020464var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020465 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020466 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020467 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020469 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020471 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472
Bram Moolenaara5525202006-03-02 22:52:09 +000020473 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020474 if (varp->v_type == VAR_LIST)
20475 {
20476 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020477 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020478 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020479 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020480
20481 l = varp->vval.v_list;
20482 if (l == NULL)
20483 return NULL;
20484
20485 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020486 pos.lnum = list_find_nr(l, 0L, &error);
20487 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020488 return NULL; /* invalid line number */
20489
20490 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020491 pos.col = list_find_nr(l, 1L, &error);
20492 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020493 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020494 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020495
20496 /* We accept "$" for the column number: last column. */
20497 li = list_find(l, 1L);
20498 if (li != NULL && li->li_tv.v_type == VAR_STRING
20499 && li->li_tv.vval.v_string != NULL
20500 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20501 pos.col = len + 1;
20502
Bram Moolenaara5525202006-03-02 22:52:09 +000020503 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020504 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020505 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020506 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020507
Bram Moolenaara5525202006-03-02 22:52:09 +000020508#ifdef FEAT_VIRTUALEDIT
20509 /* Get the virtual offset. Defaults to zero. */
20510 pos.coladd = list_find_nr(l, 2L, &error);
20511 if (error)
20512 pos.coladd = 0;
20513#endif
20514
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020515 return &pos;
20516 }
20517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020518 name = get_tv_string_chk(varp);
20519 if (name == NULL)
20520 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020521 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020523 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20524 {
20525 if (VIsual_active)
20526 return &VIsual;
20527 return &curwin->w_cursor;
20528 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020529 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020531 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20533 return NULL;
20534 return pp;
20535 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020536
20537#ifdef FEAT_VIRTUALEDIT
20538 pos.coladd = 0;
20539#endif
20540
Bram Moolenaar477933c2007-07-17 14:32:23 +000020541 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020542 {
20543 pos.col = 0;
20544 if (name[1] == '0') /* "w0": first visible line */
20545 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020546 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020547 pos.lnum = curwin->w_topline;
20548 return &pos;
20549 }
20550 else if (name[1] == '$') /* "w$": last visible line */
20551 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020552 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020553 pos.lnum = curwin->w_botline - 1;
20554 return &pos;
20555 }
20556 }
20557 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020558 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020559 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560 {
20561 pos.lnum = curbuf->b_ml.ml_line_count;
20562 pos.col = 0;
20563 }
20564 else
20565 {
20566 pos.lnum = curwin->w_cursor.lnum;
20567 pos.col = (colnr_T)STRLEN(ml_get_curline());
20568 }
20569 return &pos;
20570 }
20571 return NULL;
20572}
20573
20574/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020575 * Convert list in "arg" into a position and optional file number.
20576 * When "fnump" is NULL there is no file number, only 3 items.
20577 * Note that the column is passed on as-is, the caller may want to decrement
20578 * it to use 1 for the first column.
20579 * Return FAIL when conversion is not possible, doesn't check the position for
20580 * validity.
20581 */
20582 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020583list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020584 typval_T *arg;
20585 pos_T *posp;
20586 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020587 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020588{
20589 list_T *l = arg->vval.v_list;
20590 long i = 0;
20591 long n;
20592
Bram Moolenaar493c1782014-05-28 14:34:46 +020020593 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20594 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020595 if (arg->v_type != VAR_LIST
20596 || l == NULL
20597 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020598 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020599 return FAIL;
20600
20601 if (fnump != NULL)
20602 {
20603 n = list_find_nr(l, i++, NULL); /* fnum */
20604 if (n < 0)
20605 return FAIL;
20606 if (n == 0)
20607 n = curbuf->b_fnum; /* current buffer */
20608 *fnump = n;
20609 }
20610
20611 n = list_find_nr(l, i++, NULL); /* lnum */
20612 if (n < 0)
20613 return FAIL;
20614 posp->lnum = n;
20615
20616 n = list_find_nr(l, i++, NULL); /* col */
20617 if (n < 0)
20618 return FAIL;
20619 posp->col = n;
20620
20621#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020622 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020623 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020624 posp->coladd = 0;
20625 else
20626 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020627#endif
20628
Bram Moolenaar493c1782014-05-28 14:34:46 +020020629 if (curswantp != NULL)
20630 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20631
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020632 return OK;
20633}
20634
20635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 * Get the length of an environment variable name.
20637 * Advance "arg" to the first character after the name.
20638 * Return 0 for error.
20639 */
20640 static int
20641get_env_len(arg)
20642 char_u **arg;
20643{
20644 char_u *p;
20645 int len;
20646
20647 for (p = *arg; vim_isIDc(*p); ++p)
20648 ;
20649 if (p == *arg) /* no name found */
20650 return 0;
20651
20652 len = (int)(p - *arg);
20653 *arg = p;
20654 return len;
20655}
20656
20657/*
20658 * Get the length of the name of a function or internal variable.
20659 * "arg" is advanced to the first non-white character after the name.
20660 * Return 0 if something is wrong.
20661 */
20662 static int
20663get_id_len(arg)
20664 char_u **arg;
20665{
20666 char_u *p;
20667 int len;
20668
20669 /* Find the end of the name. */
20670 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020671 {
20672 if (*p == ':')
20673 {
20674 /* "s:" is start of "s:var", but "n:" is not and can be used in
20675 * slice "[n:]". Also "xx:" is not a namespace. */
20676 len = (int)(p - *arg);
20677 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20678 || len > 1)
20679 break;
20680 }
20681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 if (p == *arg) /* no name found */
20683 return 0;
20684
20685 len = (int)(p - *arg);
20686 *arg = skipwhite(p);
20687
20688 return len;
20689}
20690
20691/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020692 * Get the length of the name of a variable or function.
20693 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020695 * Return -1 if curly braces expansion failed.
20696 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 * If the name contains 'magic' {}'s, expand them and return the
20698 * expanded name in an allocated string via 'alias' - caller must free.
20699 */
20700 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020701get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 char_u **arg;
20703 char_u **alias;
20704 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020705 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020706{
20707 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 char_u *p;
20709 char_u *expr_start;
20710 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711
20712 *alias = NULL; /* default to no alias */
20713
20714 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20715 && (*arg)[2] == (int)KE_SNR)
20716 {
20717 /* hard coded <SNR>, already translated */
20718 *arg += 3;
20719 return get_id_len(arg) + 3;
20720 }
20721 len = eval_fname_script(*arg);
20722 if (len > 0)
20723 {
20724 /* literal "<SID>", "s:" or "<SNR>" */
20725 *arg += len;
20726 }
20727
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020729 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020731 p = find_name_end(*arg, &expr_start, &expr_end,
20732 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020733 if (expr_start != NULL)
20734 {
20735 char_u *temp_string;
20736
20737 if (!evaluate)
20738 {
20739 len += (int)(p - *arg);
20740 *arg = skipwhite(p);
20741 return len;
20742 }
20743
20744 /*
20745 * Include any <SID> etc in the expanded string:
20746 * Thus the -len here.
20747 */
20748 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20749 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020750 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751 *alias = temp_string;
20752 *arg = skipwhite(p);
20753 return (int)STRLEN(temp_string);
20754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755
20756 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020757 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 EMSG2(_(e_invexpr2), *arg);
20759
20760 return len;
20761}
20762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020763/*
20764 * Find the end of a variable or function name, taking care of magic braces.
20765 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20766 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020767 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020768 * Return a pointer to just after the name. Equal to "arg" if there is no
20769 * valid name.
20770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020772find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 char_u *arg;
20774 char_u **expr_start;
20775 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020776 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020778 int mb_nest = 0;
20779 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020781 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020783 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020785 *expr_start = NULL;
20786 *expr_end = NULL;
20787 }
20788
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020789 /* Quick check for valid starting character. */
20790 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20791 return arg;
20792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020793 for (p = arg; *p != NUL
20794 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020795 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020796 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020797 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020798 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020799 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020800 if (*p == '\'')
20801 {
20802 /* skip over 'string' to avoid counting [ and ] inside it. */
20803 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20804 ;
20805 if (*p == NUL)
20806 break;
20807 }
20808 else if (*p == '"')
20809 {
20810 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20811 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20812 if (*p == '\\' && p[1] != NUL)
20813 ++p;
20814 if (*p == NUL)
20815 break;
20816 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020817 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20818 {
20819 /* "s:" is start of "s:var", but "n:" is not and can be used in
20820 * slice "[n:]". Also "xx:" is not a namespace. */
20821 len = (int)(p - arg);
20822 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
20823 || len > 1)
20824 break;
20825 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020827 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020829 if (*p == '[')
20830 ++br_nest;
20831 else if (*p == ']')
20832 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020835 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020837 if (*p == '{')
20838 {
20839 mb_nest++;
20840 if (expr_start != NULL && *expr_start == NULL)
20841 *expr_start = p;
20842 }
20843 else if (*p == '}')
20844 {
20845 mb_nest--;
20846 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20847 *expr_end = p;
20848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850 }
20851
20852 return p;
20853}
20854
20855/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020856 * Expands out the 'magic' {}'s in a variable/function name.
20857 * Note that this can call itself recursively, to deal with
20858 * constructs like foo{bar}{baz}{bam}
20859 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20860 * "in_start" ^
20861 * "expr_start" ^
20862 * "expr_end" ^
20863 * "in_end" ^
20864 *
20865 * Returns a new allocated string, which the caller must free.
20866 * Returns NULL for failure.
20867 */
20868 static char_u *
20869make_expanded_name(in_start, expr_start, expr_end, in_end)
20870 char_u *in_start;
20871 char_u *expr_start;
20872 char_u *expr_end;
20873 char_u *in_end;
20874{
20875 char_u c1;
20876 char_u *retval = NULL;
20877 char_u *temp_result;
20878 char_u *nextcmd = NULL;
20879
20880 if (expr_end == NULL || in_end == NULL)
20881 return NULL;
20882 *expr_start = NUL;
20883 *expr_end = NUL;
20884 c1 = *in_end;
20885 *in_end = NUL;
20886
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020887 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020888 if (temp_result != NULL && nextcmd == NULL)
20889 {
20890 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20891 + (in_end - expr_end) + 1));
20892 if (retval != NULL)
20893 {
20894 STRCPY(retval, in_start);
20895 STRCAT(retval, temp_result);
20896 STRCAT(retval, expr_end + 1);
20897 }
20898 }
20899 vim_free(temp_result);
20900
20901 *in_end = c1; /* put char back for error messages */
20902 *expr_start = '{';
20903 *expr_end = '}';
20904
20905 if (retval != NULL)
20906 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020907 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020908 if (expr_start != NULL)
20909 {
20910 /* Further expansion! */
20911 temp_result = make_expanded_name(retval, expr_start,
20912 expr_end, temp_result);
20913 vim_free(retval);
20914 retval = temp_result;
20915 }
20916 }
20917
20918 return retval;
20919}
20920
20921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020923 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924 */
20925 static int
20926eval_isnamec(c)
20927 int c;
20928{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020929 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20930}
20931
20932/*
20933 * Return TRUE if character "c" can be used as the first character in a
20934 * variable or function name (excluding '{' and '}').
20935 */
20936 static int
20937eval_isnamec1(c)
20938 int c;
20939{
20940 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941}
20942
20943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 * Set number v: variable to "val".
20945 */
20946 void
20947set_vim_var_nr(idx, val)
20948 int idx;
20949 long val;
20950{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020951 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952}
20953
20954/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020955 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 */
20957 long
20958get_vim_var_nr(idx)
20959 int idx;
20960{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020961 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962}
20963
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020964/*
20965 * Get string v: variable value. Uses a static buffer, can only be used once.
20966 */
20967 char_u *
20968get_vim_var_str(idx)
20969 int idx;
20970{
20971 return get_tv_string(&vimvars[idx].vv_tv);
20972}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020973
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020975 * Get List v: variable value. Caller must take care of reference count when
20976 * needed.
20977 */
20978 list_T *
20979get_vim_var_list(idx)
20980 int idx;
20981{
20982 return vimvars[idx].vv_list;
20983}
20984
20985/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020986 * Set v:char to character "c".
20987 */
20988 void
20989set_vim_var_char(c)
20990 int c;
20991{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020992 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020993
20994#ifdef FEAT_MBYTE
20995 if (has_mbyte)
20996 buf[(*mb_char2bytes)(c, buf)] = NUL;
20997 else
20998#endif
20999 {
21000 buf[0] = c;
21001 buf[1] = NUL;
21002 }
21003 set_vim_var_string(VV_CHAR, buf, -1);
21004}
21005
21006/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021007 * Set v:count to "count" and v:count1 to "count1".
21008 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009 */
21010 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021011set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 long count;
21013 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021014 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021016 if (set_prevcount)
21017 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021018 vimvars[VV_COUNT].vv_nr = count;
21019 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020}
21021
21022/*
21023 * Set string v: variable to a copy of "val".
21024 */
21025 void
21026set_vim_var_string(idx, val, len)
21027 int idx;
21028 char_u *val;
21029 int len; /* length of "val" to use or -1 (whole string) */
21030{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021031 /* Need to do this (at least) once, since we can't initialize a union.
21032 * Will always be invoked when "v:progname" is set. */
21033 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
21034
Bram Moolenaare9a41262005-01-15 22:18:47 +000021035 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021037 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021039 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021041 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042}
21043
21044/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021045 * Set List v: variable to "val".
21046 */
21047 void
21048set_vim_var_list(idx, val)
21049 int idx;
21050 list_T *val;
21051{
21052 list_unref(vimvars[idx].vv_list);
21053 vimvars[idx].vv_list = val;
21054 if (val != NULL)
21055 ++val->lv_refcount;
21056}
21057
21058/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021059 * Set Dictionary v: variable to "val".
21060 */
21061 void
21062set_vim_var_dict(idx, val)
21063 int idx;
21064 dict_T *val;
21065{
21066 int todo;
21067 hashitem_T *hi;
21068
21069 dict_unref(vimvars[idx].vv_dict);
21070 vimvars[idx].vv_dict = val;
21071 if (val != NULL)
21072 {
21073 ++val->dv_refcount;
21074
21075 /* Set readonly */
21076 todo = (int)val->dv_hashtab.ht_used;
21077 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21078 {
21079 if (HASHITEM_EMPTY(hi))
21080 continue;
21081 --todo;
21082 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21083 }
21084 }
21085}
21086
21087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 * Set v:register if needed.
21089 */
21090 void
21091set_reg_var(c)
21092 int c;
21093{
21094 char_u regname;
21095
21096 if (c == 0 || c == ' ')
21097 regname = '"';
21098 else
21099 regname = c;
21100 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021101 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 set_vim_var_string(VV_REG, &regname, 1);
21103}
21104
21105/*
21106 * Get or set v:exception. If "oldval" == NULL, return the current value.
21107 * Otherwise, restore the value to "oldval" and return NULL.
21108 * Must always be called in pairs to save and restore v:exception! Does not
21109 * take care of memory allocations.
21110 */
21111 char_u *
21112v_exception(oldval)
21113 char_u *oldval;
21114{
21115 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021116 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117
Bram Moolenaare9a41262005-01-15 22:18:47 +000021118 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 return NULL;
21120}
21121
21122/*
21123 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21124 * Otherwise, restore the value to "oldval" and return NULL.
21125 * Must always be called in pairs to save and restore v:throwpoint! Does not
21126 * take care of memory allocations.
21127 */
21128 char_u *
21129v_throwpoint(oldval)
21130 char_u *oldval;
21131{
21132 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021133 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134
Bram Moolenaare9a41262005-01-15 22:18:47 +000021135 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 return NULL;
21137}
21138
21139#if defined(FEAT_AUTOCMD) || defined(PROTO)
21140/*
21141 * Set v:cmdarg.
21142 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21143 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21144 * Must always be called in pairs!
21145 */
21146 char_u *
21147set_cmdarg(eap, oldarg)
21148 exarg_T *eap;
21149 char_u *oldarg;
21150{
21151 char_u *oldval;
21152 char_u *newval;
21153 unsigned len;
21154
Bram Moolenaare9a41262005-01-15 22:18:47 +000021155 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021156 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021158 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021159 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021160 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 }
21162
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021163 if (eap->force_bin == FORCE_BIN)
21164 len = 6;
21165 else if (eap->force_bin == FORCE_NOBIN)
21166 len = 8;
21167 else
21168 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021169
21170 if (eap->read_edit)
21171 len += 7;
21172
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021173 if (eap->force_ff != 0)
21174 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21175# ifdef FEAT_MBYTE
21176 if (eap->force_enc != 0)
21177 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021178 if (eap->bad_char != 0)
21179 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021180# endif
21181
21182 newval = alloc(len + 1);
21183 if (newval == NULL)
21184 return NULL;
21185
21186 if (eap->force_bin == FORCE_BIN)
21187 sprintf((char *)newval, " ++bin");
21188 else if (eap->force_bin == FORCE_NOBIN)
21189 sprintf((char *)newval, " ++nobin");
21190 else
21191 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021192
21193 if (eap->read_edit)
21194 STRCAT(newval, " ++edit");
21195
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021196 if (eap->force_ff != 0)
21197 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21198 eap->cmd + eap->force_ff);
21199# ifdef FEAT_MBYTE
21200 if (eap->force_enc != 0)
21201 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21202 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021203 if (eap->bad_char == BAD_KEEP)
21204 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21205 else if (eap->bad_char == BAD_DROP)
21206 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21207 else if (eap->bad_char != 0)
21208 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021209# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021210 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021211 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212}
21213#endif
21214
21215/*
21216 * Get the value of internal variable "name".
21217 * Return OK or FAIL.
21218 */
21219 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021220get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 char_u *name;
21222 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021223 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021224 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021225 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021226 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227{
21228 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021229 typval_T *tv = NULL;
21230 typval_T atv;
21231 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233
21234 /* truncate the name, so that we can use strcmp() */
21235 cc = name[len];
21236 name[len] = NUL;
21237
21238 /*
21239 * Check for "b:changedtick".
21240 */
21241 if (STRCMP(name, "b:changedtick") == 0)
21242 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021243 atv.v_type = VAR_NUMBER;
21244 atv.vval.v_number = curbuf->b_changedtick;
21245 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246 }
21247
21248 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 * Check for user-defined variables.
21250 */
21251 else
21252 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021253 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021255 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021256 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021257 if (dip != NULL)
21258 *dip = v;
21259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 }
21261
Bram Moolenaare9a41262005-01-15 22:18:47 +000021262 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021264 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021265 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 ret = FAIL;
21267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021268 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021269 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270
21271 name[len] = cc;
21272
21273 return ret;
21274}
21275
21276/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021277 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21278 * Also handle function call with Funcref variable: func(expr)
21279 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21280 */
21281 static int
21282handle_subscript(arg, rettv, evaluate, verbose)
21283 char_u **arg;
21284 typval_T *rettv;
21285 int evaluate; /* do more than finding the end */
21286 int verbose; /* give error messages */
21287{
21288 int ret = OK;
21289 dict_T *selfdict = NULL;
21290 char_u *s;
21291 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021292 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021293
21294 while (ret == OK
21295 && (**arg == '['
21296 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021297 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021298 && !vim_iswhite(*(*arg - 1)))
21299 {
21300 if (**arg == '(')
21301 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021302 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021303 if (evaluate)
21304 {
21305 functv = *rettv;
21306 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021307
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021308 /* Invoke the function. Recursive! */
21309 s = functv.vval.v_string;
21310 }
21311 else
21312 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021313 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021314 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21315 &len, evaluate, selfdict);
21316
21317 /* Clear the funcref afterwards, so that deleting it while
21318 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021319 if (evaluate)
21320 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021321
21322 /* Stop the expression evaluation when immediately aborting on
21323 * error, or when an interrupt occurred or an exception was thrown
21324 * but not caught. */
21325 if (aborting())
21326 {
21327 if (ret == OK)
21328 clear_tv(rettv);
21329 ret = FAIL;
21330 }
21331 dict_unref(selfdict);
21332 selfdict = NULL;
21333 }
21334 else /* **arg == '[' || **arg == '.' */
21335 {
21336 dict_unref(selfdict);
21337 if (rettv->v_type == VAR_DICT)
21338 {
21339 selfdict = rettv->vval.v_dict;
21340 if (selfdict != NULL)
21341 ++selfdict->dv_refcount;
21342 }
21343 else
21344 selfdict = NULL;
21345 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21346 {
21347 clear_tv(rettv);
21348 ret = FAIL;
21349 }
21350 }
21351 }
21352 dict_unref(selfdict);
21353 return ret;
21354}
21355
21356/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021357 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021358 * value).
21359 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021360 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021361alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021362{
Bram Moolenaar33570922005-01-25 22:26:29 +000021363 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021364}
21365
21366/*
21367 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 * The string "s" must have been allocated, it is consumed.
21369 * Return NULL for out of memory, the variable otherwise.
21370 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021371 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021372alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 char_u *s;
21374{
Bram Moolenaar33570922005-01-25 22:26:29 +000021375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021377 rettv = alloc_tv();
21378 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021380 rettv->v_type = VAR_STRING;
21381 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 }
21383 else
21384 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021385 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386}
21387
21388/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021389 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021391 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021392free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021393 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394{
21395 if (varp != NULL)
21396 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021397 switch (varp->v_type)
21398 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021399 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021400 func_unref(varp->vval.v_string);
21401 /*FALLTHROUGH*/
21402 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021403 vim_free(varp->vval.v_string);
21404 break;
21405 case VAR_LIST:
21406 list_unref(varp->vval.v_list);
21407 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021408 case VAR_DICT:
21409 dict_unref(varp->vval.v_dict);
21410 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021411 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021412#ifdef FEAT_FLOAT
21413 case VAR_FLOAT:
21414#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021415 case VAR_UNKNOWN:
21416 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021417 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021418 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021419 break;
21420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421 vim_free(varp);
21422 }
21423}
21424
21425/*
21426 * Free the memory for a variable value and set the value to NULL or 0.
21427 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021428 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021429clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021430 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431{
21432 if (varp != NULL)
21433 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021434 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021436 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021437 func_unref(varp->vval.v_string);
21438 /*FALLTHROUGH*/
21439 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021440 vim_free(varp->vval.v_string);
21441 varp->vval.v_string = NULL;
21442 break;
21443 case VAR_LIST:
21444 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021445 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021446 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021447 case VAR_DICT:
21448 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021449 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021450 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021451 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021452 varp->vval.v_number = 0;
21453 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021454#ifdef FEAT_FLOAT
21455 case VAR_FLOAT:
21456 varp->vval.v_float = 0.0;
21457 break;
21458#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021459 case VAR_UNKNOWN:
21460 break;
21461 default:
21462 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021464 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465 }
21466}
21467
21468/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021469 * Set the value of a variable to NULL without freeing items.
21470 */
21471 static void
21472init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021473 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021474{
21475 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021476 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021477}
21478
21479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480 * Get the number value of a variable.
21481 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021482 * For incompatible types, return 0.
21483 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21484 * caller of incompatible types: it sets *denote to TRUE if "denote"
21485 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 */
21487 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021488get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021489 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021491 int error = FALSE;
21492
21493 return get_tv_number_chk(varp, &error); /* return 0L on error */
21494}
21495
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021496 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021497get_tv_number_chk(varp, denote)
21498 typval_T *varp;
21499 int *denote;
21500{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021501 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021503 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021505 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021506 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021507#ifdef FEAT_FLOAT
21508 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021509 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021510 break;
21511#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021512 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021513 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021514 break;
21515 case VAR_STRING:
21516 if (varp->vval.v_string != NULL)
21517 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021518 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021519 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021520 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021521 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021522 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021523 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021524 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021525 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021526 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021527 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021528 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021530 if (denote == NULL) /* useful for values that must be unsigned */
21531 n = -1;
21532 else
21533 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021534 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535}
21536
21537/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021538 * Get the lnum from the first argument.
21539 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021540 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541 */
21542 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021543get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021544 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545{
Bram Moolenaar33570922005-01-25 22:26:29 +000021546 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 linenr_T lnum;
21548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021549 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550 if (lnum == 0) /* no valid number, try using line() */
21551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021552 rettv.v_type = VAR_NUMBER;
21553 f_line(argvars, &rettv);
21554 lnum = rettv.vval.v_number;
21555 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 }
21557 return lnum;
21558}
21559
21560/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021561 * Get the lnum from the first argument.
21562 * Also accepts "$", then "buf" is used.
21563 * Returns 0 on error.
21564 */
21565 static linenr_T
21566get_tv_lnum_buf(argvars, buf)
21567 typval_T *argvars;
21568 buf_T *buf;
21569{
21570 if (argvars[0].v_type == VAR_STRING
21571 && argvars[0].vval.v_string != NULL
21572 && argvars[0].vval.v_string[0] == '$'
21573 && buf != NULL)
21574 return buf->b_ml.ml_line_count;
21575 return get_tv_number_chk(&argvars[0], NULL);
21576}
21577
21578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579 * Get the string value of a variable.
21580 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021581 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21582 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 * If the String variable has never been set, return an empty string.
21584 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021585 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21586 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587 */
21588 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021589get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021590 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591{
21592 static char_u mybuf[NUMBUFLEN];
21593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021594 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595}
21596
21597 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021598get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021599 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021600 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021602 char_u *res = get_tv_string_buf_chk(varp, buf);
21603
21604 return res != NULL ? res : (char_u *)"";
21605}
21606
Bram Moolenaar7d647822014-04-05 21:28:56 +020021607/*
21608 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21609 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021610 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021611get_tv_string_chk(varp)
21612 typval_T *varp;
21613{
21614 static char_u mybuf[NUMBUFLEN];
21615
21616 return get_tv_string_buf_chk(varp, mybuf);
21617}
21618
21619 static char_u *
21620get_tv_string_buf_chk(varp, buf)
21621 typval_T *varp;
21622 char_u *buf;
21623{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021624 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021626 case VAR_NUMBER:
21627 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21628 return buf;
21629 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021630 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021631 break;
21632 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021633 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021634 break;
21635 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021636 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021637 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021638#ifdef FEAT_FLOAT
21639 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021640 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021641 break;
21642#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021643 case VAR_STRING:
21644 if (varp->vval.v_string != NULL)
21645 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021646 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021647 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021648 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021649 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021651 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652}
21653
21654/*
21655 * Find variable "name" in the list of variables.
21656 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021657 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021658 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021659 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021661 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021662find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021664 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021665 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021668 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669
Bram Moolenaara7043832005-01-21 11:56:39 +000021670 ht = find_var_ht(name, &varname);
21671 if (htp != NULL)
21672 *htp = ht;
21673 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021675 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676}
21677
21678/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021679 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021680 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021682 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021683find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021684 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021685 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021686 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021687 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021688{
Bram Moolenaar33570922005-01-25 22:26:29 +000021689 hashitem_T *hi;
21690
21691 if (*varname == NUL)
21692 {
21693 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021694 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021695 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021696 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021697 case 'g': return &globvars_var;
21698 case 'v': return &vimvars_var;
21699 case 'b': return &curbuf->b_bufvar;
21700 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021701#ifdef FEAT_WINDOWS
21702 case 't': return &curtab->tp_winvar;
21703#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021704 case 'l': return current_funccal == NULL
21705 ? NULL : &current_funccal->l_vars_var;
21706 case 'a': return current_funccal == NULL
21707 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021708 }
21709 return NULL;
21710 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021711
21712 hi = hash_find(ht, varname);
21713 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021714 {
21715 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021716 * worked find the variable again. Don't auto-load a script if it was
21717 * loaded already, otherwise it would be loaded every time when
21718 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021719 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021720 {
21721 /* Note: script_autoload() may make "hi" invalid. It must either
21722 * be obtained again or not used. */
21723 if (!script_autoload(varname, FALSE) || aborting())
21724 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021725 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021726 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021727 if (HASHITEM_EMPTY(hi))
21728 return NULL;
21729 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021730 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021731}
21732
21733/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021734 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021735 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021736 * Set "varname" to the start of name without ':'.
21737 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021738 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021739find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 char_u *name;
21741 char_u **varname;
21742{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021743 hashitem_T *hi;
21744
Bram Moolenaar73627d02015-08-11 15:46:09 +020021745 if (name[0] == NUL)
21746 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747 if (name[1] != ':')
21748 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021749 /* The name must not start with a colon or #. */
21750 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021751 return NULL;
21752 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021753
21754 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021755 hi = hash_find(&compat_hashtab, name);
21756 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021757 return &compat_hashtab;
21758
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021760 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021761 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762 }
21763 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021764 if (*name == 'g') /* global variable */
21765 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021766 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21767 */
21768 if (vim_strchr(name + 2, ':') != NULL
21769 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021770 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021772 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021774 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021775#ifdef FEAT_WINDOWS
21776 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021777 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021778#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021779 if (*name == 'v') /* v: variable */
21780 return &vimvarht;
21781 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021782 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021783 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021784 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 if (*name == 's' /* script variable */
21786 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21787 return &SCRIPT_VARS(current_SID);
21788 return NULL;
21789}
21790
21791/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021792 * Get function call environment based on bactrace debug level
21793 */
21794 static funccall_T *
21795get_funccal()
21796{
21797 int i;
21798 funccall_T *funccal;
21799 funccall_T *temp_funccal;
21800
21801 funccal = current_funccal;
21802 if (debug_backtrace_level > 0)
21803 {
21804 for (i = 0; i < debug_backtrace_level; i++)
21805 {
21806 temp_funccal = funccal->caller;
21807 if (temp_funccal)
21808 funccal = temp_funccal;
21809 else
21810 /* backtrace level overflow. reset to max */
21811 debug_backtrace_level = i;
21812 }
21813 }
21814 return funccal;
21815}
21816
21817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021819 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 * Returns NULL when it doesn't exist.
21821 */
21822 char_u *
21823get_var_value(name)
21824 char_u *name;
21825{
Bram Moolenaar33570922005-01-25 22:26:29 +000021826 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021828 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021829 if (v == NULL)
21830 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021831 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832}
21833
21834/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021835 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836 * sourcing this script and when executing functions defined in the script.
21837 */
21838 void
21839new_script_vars(id)
21840 scid_T id;
21841{
Bram Moolenaara7043832005-01-21 11:56:39 +000021842 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021843 hashtab_T *ht;
21844 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021845
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21847 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021848 /* Re-allocating ga_data means that an ht_array pointing to
21849 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021850 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021851 for (i = 1; i <= ga_scripts.ga_len; ++i)
21852 {
21853 ht = &SCRIPT_VARS(i);
21854 if (ht->ht_mask == HT_INIT_SIZE - 1)
21855 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021856 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021857 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021858 }
21859
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 while (ga_scripts.ga_len < id)
21861 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021862 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021863 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021864 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866 }
21867 }
21868}
21869
21870/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021871 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21872 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873 */
21874 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021875init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021876 dict_T *dict;
21877 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021878 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879{
Bram Moolenaar33570922005-01-25 22:26:29 +000021880 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021881 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021882 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021883 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021884 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021885 dict_var->di_tv.vval.v_dict = dict;
21886 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021887 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021888 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21889 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890}
21891
21892/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021893 * Unreference a dictionary initialized by init_var_dict().
21894 */
21895 void
21896unref_var_dict(dict)
21897 dict_T *dict;
21898{
21899 /* Now the dict needs to be freed if no one else is using it, go back to
21900 * normal reference counting. */
21901 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21902 dict_unref(dict);
21903}
21904
21905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021907 * Frees all allocated variables and the value they contain.
21908 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909 */
21910 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021911vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021912 hashtab_T *ht;
21913{
21914 vars_clear_ext(ht, TRUE);
21915}
21916
21917/*
21918 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21919 */
21920 static void
21921vars_clear_ext(ht, free_val)
21922 hashtab_T *ht;
21923 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924{
Bram Moolenaara7043832005-01-21 11:56:39 +000021925 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021926 hashitem_T *hi;
21927 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928
Bram Moolenaar33570922005-01-25 22:26:29 +000021929 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021930 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021931 for (hi = ht->ht_array; todo > 0; ++hi)
21932 {
21933 if (!HASHITEM_EMPTY(hi))
21934 {
21935 --todo;
21936
Bram Moolenaar33570922005-01-25 22:26:29 +000021937 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021938 * ht_array might change then. hash_clear() takes care of it
21939 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021940 v = HI2DI(hi);
21941 if (free_val)
21942 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021943 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021944 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021945 }
21946 }
21947 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021948 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949}
21950
Bram Moolenaara7043832005-01-21 11:56:39 +000021951/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021952 * Delete a variable from hashtab "ht" at item "hi".
21953 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021956delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021957 hashtab_T *ht;
21958 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959{
Bram Moolenaar33570922005-01-25 22:26:29 +000021960 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021961
21962 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021963 clear_tv(&di->di_tv);
21964 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021965}
21966
21967/*
21968 * List the value of one internal variable.
21969 */
21970 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021971list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021972 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021974 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021976 char_u *tofree;
21977 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021978 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021979
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021980 current_copyID += COPYID_INC;
21981 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021982 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021983 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021984 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985}
21986
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021988list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 char_u *prefix;
21990 char_u *name;
21991 int type;
21992 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021993 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994{
Bram Moolenaar31859182007-08-14 20:41:13 +000021995 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21996 msg_start();
21997 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 if (name != NULL) /* "a:" vars don't have a name stored */
21999 msg_puts(name);
22000 msg_putchar(' ');
22001 msg_advance(22);
22002 if (type == VAR_NUMBER)
22003 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022004 else if (type == VAR_FUNC)
22005 msg_putchar('*');
22006 else if (type == VAR_LIST)
22007 {
22008 msg_putchar('[');
22009 if (*string == '[')
22010 ++string;
22011 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022012 else if (type == VAR_DICT)
22013 {
22014 msg_putchar('{');
22015 if (*string == '{')
22016 ++string;
22017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018 else
22019 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022020
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022022
22023 if (type == VAR_FUNC)
22024 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022025 if (*first)
22026 {
22027 msg_clr_eos();
22028 *first = FALSE;
22029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030}
22031
22032/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022033 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034 * If the variable already exists, the value is updated.
22035 * Otherwise the variable is created.
22036 */
22037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022038set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022040 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022041 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042{
Bram Moolenaar33570922005-01-25 22:26:29 +000022043 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022045 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022047 ht = find_var_ht(name, &varname);
22048 if (ht == NULL || *varname == NUL)
22049 {
22050 EMSG2(_(e_illvar), name);
22051 return;
22052 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022053 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022054
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022055 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22056 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022057
Bram Moolenaar33570922005-01-25 22:26:29 +000022058 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022060 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022061 if (var_check_ro(v->di_flags, name, FALSE)
22062 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022063 return;
22064 if (v->di_tv.v_type != tv->v_type
22065 && !((v->di_tv.v_type == VAR_STRING
22066 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022067 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022068 || tv->v_type == VAR_NUMBER))
22069#ifdef FEAT_FLOAT
22070 && !((v->di_tv.v_type == VAR_NUMBER
22071 || v->di_tv.v_type == VAR_FLOAT)
22072 && (tv->v_type == VAR_NUMBER
22073 || tv->v_type == VAR_FLOAT))
22074#endif
22075 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022076 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022077 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022078 return;
22079 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022080
22081 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022082 * Handle setting internal v: variables separately where needed to
22083 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022084 */
22085 if (ht == &vimvarht)
22086 {
22087 if (v->di_tv.v_type == VAR_STRING)
22088 {
22089 vim_free(v->di_tv.vval.v_string);
22090 if (copy || tv->v_type != VAR_STRING)
22091 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22092 else
22093 {
22094 /* Take over the string to avoid an extra alloc/free. */
22095 v->di_tv.vval.v_string = tv->vval.v_string;
22096 tv->vval.v_string = NULL;
22097 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022098 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022099 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022100 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022101 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022102 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022103 if (STRCMP(varname, "searchforward") == 0)
22104 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022105#ifdef FEAT_SEARCH_EXTRA
22106 else if (STRCMP(varname, "hlsearch") == 0)
22107 {
22108 no_hlsearch = !v->di_tv.vval.v_number;
22109 redraw_all_later(SOME_VALID);
22110 }
22111#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022112 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022113 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022114 else if (v->di_tv.v_type != tv->v_type)
22115 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022116 }
22117
22118 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 }
22120 else /* add a new variable */
22121 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022122 /* Can't add "v:" variable. */
22123 if (ht == &vimvarht)
22124 {
22125 EMSG2(_(e_illvar), name);
22126 return;
22127 }
22128
Bram Moolenaar92124a32005-06-17 22:03:40 +000022129 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022130 if (!valid_varname(varname))
22131 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022132
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022133 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22134 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022135 if (v == NULL)
22136 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022137 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022138 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022140 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 return;
22142 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022143 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022145
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022146 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022147 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022148 else
22149 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022150 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022151 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022152 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154}
22155
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022156/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022157 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022158 * Also give an error message.
22159 */
22160 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022161var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022162 int flags;
22163 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022164 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022165{
22166 if (flags & DI_FLAGS_RO)
22167 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022168 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022169 return TRUE;
22170 }
22171 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22172 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022173 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022174 return TRUE;
22175 }
22176 return FALSE;
22177}
22178
22179/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022180 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22181 * Also give an error message.
22182 */
22183 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022184var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022185 int flags;
22186 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022187 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022188{
22189 if (flags & DI_FLAGS_FIX)
22190 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022191 EMSG2(_("E795: Cannot delete variable %s"),
22192 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022193 return TRUE;
22194 }
22195 return FALSE;
22196}
22197
22198/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022199 * Check if a funcref is assigned to a valid variable name.
22200 * Return TRUE and give an error if not.
22201 */
22202 static int
22203var_check_func_name(name, new_var)
22204 char_u *name; /* points to start of variable name */
22205 int new_var; /* TRUE when creating the variable */
22206{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022207 /* Allow for w: b: s: and t:. */
22208 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022209 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22210 ? name[2] : name[0]))
22211 {
22212 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22213 name);
22214 return TRUE;
22215 }
22216 /* Don't allow hiding a function. When "v" is not NULL we might be
22217 * assigning another function to the same var, the type is checked
22218 * below. */
22219 if (new_var && function_exists(name))
22220 {
22221 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22222 name);
22223 return TRUE;
22224 }
22225 return FALSE;
22226}
22227
22228/*
22229 * Check if a variable name is valid.
22230 * Return FALSE and give an error if not.
22231 */
22232 static int
22233valid_varname(varname)
22234 char_u *varname;
22235{
22236 char_u *p;
22237
22238 for (p = varname; *p != NUL; ++p)
22239 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22240 && *p != AUTOLOAD_CHAR)
22241 {
22242 EMSG2(_(e_illvar), varname);
22243 return FALSE;
22244 }
22245 return TRUE;
22246}
22247
22248/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022249 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022250 * Also give an error message, using "name" or _("name") when use_gettext is
22251 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022252 */
22253 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022254tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022255 int lock;
22256 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022257 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022258{
22259 if (lock & VAR_LOCKED)
22260 {
22261 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022262 name == NULL ? (char_u *)_("Unknown")
22263 : use_gettext ? (char_u *)_(name)
22264 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022265 return TRUE;
22266 }
22267 if (lock & VAR_FIXED)
22268 {
22269 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022270 name == NULL ? (char_u *)_("Unknown")
22271 : use_gettext ? (char_u *)_(name)
22272 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022273 return TRUE;
22274 }
22275 return FALSE;
22276}
22277
22278/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022279 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022280 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022281 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022282 * It is OK for "from" and "to" to point to the same item. This is used to
22283 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022284 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022285 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022286copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022287 typval_T *from;
22288 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022290 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022291 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022292 switch (from->v_type)
22293 {
22294 case VAR_NUMBER:
22295 to->vval.v_number = from->vval.v_number;
22296 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022297#ifdef FEAT_FLOAT
22298 case VAR_FLOAT:
22299 to->vval.v_float = from->vval.v_float;
22300 break;
22301#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022302 case VAR_STRING:
22303 case VAR_FUNC:
22304 if (from->vval.v_string == NULL)
22305 to->vval.v_string = NULL;
22306 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022307 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022308 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022309 if (from->v_type == VAR_FUNC)
22310 func_ref(to->vval.v_string);
22311 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022312 break;
22313 case VAR_LIST:
22314 if (from->vval.v_list == NULL)
22315 to->vval.v_list = NULL;
22316 else
22317 {
22318 to->vval.v_list = from->vval.v_list;
22319 ++to->vval.v_list->lv_refcount;
22320 }
22321 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022322 case VAR_DICT:
22323 if (from->vval.v_dict == NULL)
22324 to->vval.v_dict = NULL;
22325 else
22326 {
22327 to->vval.v_dict = from->vval.v_dict;
22328 ++to->vval.v_dict->dv_refcount;
22329 }
22330 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022331 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022332 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022333 break;
22334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335}
22336
22337/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022338 * Make a copy of an item.
22339 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022340 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22341 * reference to an already copied list/dict can be used.
22342 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022343 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022344 static int
22345item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022346 typval_T *from;
22347 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022348 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022349 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022350{
22351 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022352 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022353
Bram Moolenaar33570922005-01-25 22:26:29 +000022354 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022355 {
22356 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022357 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022358 }
22359 ++recurse;
22360
22361 switch (from->v_type)
22362 {
22363 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022364#ifdef FEAT_FLOAT
22365 case VAR_FLOAT:
22366#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022367 case VAR_STRING:
22368 case VAR_FUNC:
22369 copy_tv(from, to);
22370 break;
22371 case VAR_LIST:
22372 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022373 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022374 if (from->vval.v_list == NULL)
22375 to->vval.v_list = NULL;
22376 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22377 {
22378 /* use the copy made earlier */
22379 to->vval.v_list = from->vval.v_list->lv_copylist;
22380 ++to->vval.v_list->lv_refcount;
22381 }
22382 else
22383 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22384 if (to->vval.v_list == NULL)
22385 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022386 break;
22387 case VAR_DICT:
22388 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022389 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022390 if (from->vval.v_dict == NULL)
22391 to->vval.v_dict = NULL;
22392 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22393 {
22394 /* use the copy made earlier */
22395 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22396 ++to->vval.v_dict->dv_refcount;
22397 }
22398 else
22399 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22400 if (to->vval.v_dict == NULL)
22401 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022402 break;
22403 default:
22404 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022405 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022406 }
22407 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022408 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022409}
22410
22411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412 * ":echo expr1 ..." print each argument separated with a space, add a
22413 * newline at the end.
22414 * ":echon expr1 ..." print each argument plain.
22415 */
22416 void
22417ex_echo(eap)
22418 exarg_T *eap;
22419{
22420 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022421 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022422 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 char_u *p;
22424 int needclr = TRUE;
22425 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022426 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427
22428 if (eap->skip)
22429 ++emsg_skip;
22430 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22431 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022432 /* If eval1() causes an error message the text from the command may
22433 * still need to be cleared. E.g., "echo 22,44". */
22434 need_clr_eos = needclr;
22435
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022437 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438 {
22439 /*
22440 * Report the invalid expression unless the expression evaluation
22441 * has been cancelled due to an aborting error, an interrupt, or an
22442 * exception.
22443 */
22444 if (!aborting())
22445 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022446 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 break;
22448 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022449 need_clr_eos = FALSE;
22450
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451 if (!eap->skip)
22452 {
22453 if (atstart)
22454 {
22455 atstart = FALSE;
22456 /* Call msg_start() after eval1(), evaluating the expression
22457 * may cause a message to appear. */
22458 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022459 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022460 /* Mark the saved text as finishing the line, so that what
22461 * follows is displayed on a new line when scrolling back
22462 * at the more prompt. */
22463 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466 }
22467 else if (eap->cmdidx == CMD_echo)
22468 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022469 current_copyID += COPYID_INC;
22470 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022471 if (p != NULL)
22472 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022474 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022476 if (*p != TAB && needclr)
22477 {
22478 /* remove any text still there from the command */
22479 msg_clr_eos();
22480 needclr = FALSE;
22481 }
22482 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483 }
22484 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022485 {
22486#ifdef FEAT_MBYTE
22487 if (has_mbyte)
22488 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022489 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022490
22491 (void)msg_outtrans_len_attr(p, i, echo_attr);
22492 p += i - 1;
22493 }
22494 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022496 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022499 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022501 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502 arg = skipwhite(arg);
22503 }
22504 eap->nextcmd = check_nextcmd(arg);
22505
22506 if (eap->skip)
22507 --emsg_skip;
22508 else
22509 {
22510 /* remove text that may still be there from the command */
22511 if (needclr)
22512 msg_clr_eos();
22513 if (eap->cmdidx == CMD_echo)
22514 msg_end();
22515 }
22516}
22517
22518/*
22519 * ":echohl {name}".
22520 */
22521 void
22522ex_echohl(eap)
22523 exarg_T *eap;
22524{
22525 int id;
22526
22527 id = syn_name2id(eap->arg);
22528 if (id == 0)
22529 echo_attr = 0;
22530 else
22531 echo_attr = syn_id2attr(id);
22532}
22533
22534/*
22535 * ":execute expr1 ..." execute the result of an expression.
22536 * ":echomsg expr1 ..." Print a message
22537 * ":echoerr expr1 ..." Print an error
22538 * Each gets spaces around each argument and a newline at the end for
22539 * echo commands
22540 */
22541 void
22542ex_execute(eap)
22543 exarg_T *eap;
22544{
22545 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022546 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 int ret = OK;
22548 char_u *p;
22549 garray_T ga;
22550 int len;
22551 int save_did_emsg;
22552
22553 ga_init2(&ga, 1, 80);
22554
22555 if (eap->skip)
22556 ++emsg_skip;
22557 while (*arg != NUL && *arg != '|' && *arg != '\n')
22558 {
22559 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022560 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561 {
22562 /*
22563 * Report the invalid expression unless the expression evaluation
22564 * has been cancelled due to an aborting error, an interrupt, or an
22565 * exception.
22566 */
22567 if (!aborting())
22568 EMSG2(_(e_invexpr2), p);
22569 ret = FAIL;
22570 break;
22571 }
22572
22573 if (!eap->skip)
22574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022575 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 len = (int)STRLEN(p);
22577 if (ga_grow(&ga, len + 2) == FAIL)
22578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022579 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 ret = FAIL;
22581 break;
22582 }
22583 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586 ga.ga_len += len;
22587 }
22588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022589 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 arg = skipwhite(arg);
22591 }
22592
22593 if (ret != FAIL && ga.ga_data != NULL)
22594 {
22595 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022596 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022598 out_flush();
22599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600 else if (eap->cmdidx == CMD_echoerr)
22601 {
22602 /* We don't want to abort following commands, restore did_emsg. */
22603 save_did_emsg = did_emsg;
22604 EMSG((char_u *)ga.ga_data);
22605 if (!force_abort)
22606 did_emsg = save_did_emsg;
22607 }
22608 else if (eap->cmdidx == CMD_execute)
22609 do_cmdline((char_u *)ga.ga_data,
22610 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22611 }
22612
22613 ga_clear(&ga);
22614
22615 if (eap->skip)
22616 --emsg_skip;
22617
22618 eap->nextcmd = check_nextcmd(arg);
22619}
22620
22621/*
22622 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22623 * "arg" points to the "&" or '+' when called, to "option" when returning.
22624 * Returns NULL when no option name found. Otherwise pointer to the char
22625 * after the option name.
22626 */
22627 static char_u *
22628find_option_end(arg, opt_flags)
22629 char_u **arg;
22630 int *opt_flags;
22631{
22632 char_u *p = *arg;
22633
22634 ++p;
22635 if (*p == 'g' && p[1] == ':')
22636 {
22637 *opt_flags = OPT_GLOBAL;
22638 p += 2;
22639 }
22640 else if (*p == 'l' && p[1] == ':')
22641 {
22642 *opt_flags = OPT_LOCAL;
22643 p += 2;
22644 }
22645 else
22646 *opt_flags = 0;
22647
22648 if (!ASCII_ISALPHA(*p))
22649 return NULL;
22650 *arg = p;
22651
22652 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22653 p += 4; /* termcap option */
22654 else
22655 while (ASCII_ISALPHA(*p))
22656 ++p;
22657 return p;
22658}
22659
22660/*
22661 * ":function"
22662 */
22663 void
22664ex_function(eap)
22665 exarg_T *eap;
22666{
22667 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022668 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 int j;
22670 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022672 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673 char_u *name = NULL;
22674 char_u *p;
22675 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022676 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677 garray_T newargs;
22678 garray_T newlines;
22679 int varargs = FALSE;
22680 int mustend = FALSE;
22681 int flags = 0;
22682 ufunc_T *fp;
22683 int indent;
22684 int nesting;
22685 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022686 dictitem_T *v;
22687 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022688 static int func_nr = 0; /* number for nameless function */
22689 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022690 hashtab_T *ht;
22691 int todo;
22692 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022693 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694
22695 /*
22696 * ":function" without argument: list functions.
22697 */
22698 if (ends_excmd(*eap->arg))
22699 {
22700 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022701 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022702 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022703 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022704 {
22705 if (!HASHITEM_EMPTY(hi))
22706 {
22707 --todo;
22708 fp = HI2UF(hi);
22709 if (!isdigit(*fp->uf_name))
22710 list_func_head(fp, FALSE);
22711 }
22712 }
22713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 eap->nextcmd = check_nextcmd(eap->arg);
22715 return;
22716 }
22717
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022718 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022719 * ":function /pat": list functions matching pattern.
22720 */
22721 if (*eap->arg == '/')
22722 {
22723 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22724 if (!eap->skip)
22725 {
22726 regmatch_T regmatch;
22727
22728 c = *p;
22729 *p = NUL;
22730 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22731 *p = c;
22732 if (regmatch.regprog != NULL)
22733 {
22734 regmatch.rm_ic = p_ic;
22735
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022736 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022737 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22738 {
22739 if (!HASHITEM_EMPTY(hi))
22740 {
22741 --todo;
22742 fp = HI2UF(hi);
22743 if (!isdigit(*fp->uf_name)
22744 && vim_regexec(&regmatch, fp->uf_name, 0))
22745 list_func_head(fp, FALSE);
22746 }
22747 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022748 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022749 }
22750 }
22751 if (*p == '/')
22752 ++p;
22753 eap->nextcmd = check_nextcmd(p);
22754 return;
22755 }
22756
22757 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022758 * Get the function name. There are these situations:
22759 * func normal function name
22760 * "name" == func, "fudi.fd_dict" == NULL
22761 * dict.func new dictionary entry
22762 * "name" == NULL, "fudi.fd_dict" set,
22763 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22764 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022765 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022766 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22767 * dict.func existing dict entry that's not a Funcref
22768 * "name" == NULL, "fudi.fd_dict" set,
22769 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022770 * s:func script-local function name
22771 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022773 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022774 name = trans_function_name(&p, eap->skip, 0, &fudi);
22775 paren = (vim_strchr(p, '(') != NULL);
22776 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777 {
22778 /*
22779 * Return on an invalid expression in braces, unless the expression
22780 * evaluation has been cancelled due to an aborting error, an
22781 * interrupt, or an exception.
22782 */
22783 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022784 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022785 if (!eap->skip && fudi.fd_newkey != NULL)
22786 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022787 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 else
22791 eap->skip = TRUE;
22792 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022793
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 /* An error in a function call during evaluation of an expression in magic
22795 * braces should not cause the function not to be defined. */
22796 saved_did_emsg = did_emsg;
22797 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022798
22799 /*
22800 * ":function func" with only function name: list function.
22801 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022802 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803 {
22804 if (!ends_excmd(*skipwhite(p)))
22805 {
22806 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022807 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808 }
22809 eap->nextcmd = check_nextcmd(p);
22810 if (eap->nextcmd != NULL)
22811 *p = NUL;
22812 if (!eap->skip && !got_int)
22813 {
22814 fp = find_func(name);
22815 if (fp != NULL)
22816 {
22817 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022818 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022820 if (FUNCLINE(fp, j) == NULL)
22821 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022822 msg_putchar('\n');
22823 msg_outnum((long)(j + 1));
22824 if (j < 9)
22825 msg_putchar(' ');
22826 if (j < 99)
22827 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022828 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829 out_flush(); /* show a line at a time */
22830 ui_breakcheck();
22831 }
22832 if (!got_int)
22833 {
22834 msg_putchar('\n');
22835 msg_puts((char_u *)" endfunction");
22836 }
22837 }
22838 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022839 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022840 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022841 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842 }
22843
22844 /*
22845 * ":function name(arg1, arg2)" Define function.
22846 */
22847 p = skipwhite(p);
22848 if (*p != '(')
22849 {
22850 if (!eap->skip)
22851 {
22852 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022853 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 }
22855 /* attempt to continue by skipping some text */
22856 if (vim_strchr(p, '(') != NULL)
22857 p = vim_strchr(p, '(');
22858 }
22859 p = skipwhite(p + 1);
22860
22861 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22862 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22863
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022864 if (!eap->skip)
22865 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022866 /* Check the name of the function. Unless it's a dictionary function
22867 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022868 if (name != NULL)
22869 arg = name;
22870 else
22871 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022872 if (arg != NULL && (fudi.fd_di == NULL
22873 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022874 {
22875 if (*arg == K_SPECIAL)
22876 j = 3;
22877 else
22878 j = 0;
22879 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22880 : eval_isnamec(arg[j])))
22881 ++j;
22882 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022883 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022884 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022885 /* Disallow using the g: dict. */
22886 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22887 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022888 }
22889
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 /*
22891 * Isolate the arguments: "arg1, arg2, ...)"
22892 */
22893 while (*p != ')')
22894 {
22895 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22896 {
22897 varargs = TRUE;
22898 p += 3;
22899 mustend = TRUE;
22900 }
22901 else
22902 {
22903 arg = p;
22904 while (ASCII_ISALNUM(*p) || *p == '_')
22905 ++p;
22906 if (arg == p || isdigit(*arg)
22907 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22908 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22909 {
22910 if (!eap->skip)
22911 EMSG2(_("E125: Illegal argument: %s"), arg);
22912 break;
22913 }
22914 if (ga_grow(&newargs, 1) == FAIL)
22915 goto erret;
22916 c = *p;
22917 *p = NUL;
22918 arg = vim_strsave(arg);
22919 if (arg == NULL)
22920 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022921
22922 /* Check for duplicate argument name. */
22923 for (i = 0; i < newargs.ga_len; ++i)
22924 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22925 {
22926 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022927 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022928 goto erret;
22929 }
22930
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22932 *p = c;
22933 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934 if (*p == ',')
22935 ++p;
22936 else
22937 mustend = TRUE;
22938 }
22939 p = skipwhite(p);
22940 if (mustend && *p != ')')
22941 {
22942 if (!eap->skip)
22943 EMSG2(_(e_invarg2), eap->arg);
22944 break;
22945 }
22946 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022947 if (*p != ')')
22948 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949 ++p; /* skip the ')' */
22950
Bram Moolenaare9a41262005-01-15 22:18:47 +000022951 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 for (;;)
22953 {
22954 p = skipwhite(p);
22955 if (STRNCMP(p, "range", 5) == 0)
22956 {
22957 flags |= FC_RANGE;
22958 p += 5;
22959 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022960 else if (STRNCMP(p, "dict", 4) == 0)
22961 {
22962 flags |= FC_DICT;
22963 p += 4;
22964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 else if (STRNCMP(p, "abort", 5) == 0)
22966 {
22967 flags |= FC_ABORT;
22968 p += 5;
22969 }
22970 else
22971 break;
22972 }
22973
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022974 /* When there is a line break use what follows for the function body.
22975 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22976 if (*p == '\n')
22977 line_arg = p + 1;
22978 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 EMSG(_(e_trailing));
22980
22981 /*
22982 * Read the body of the function, until ":endfunction" is found.
22983 */
22984 if (KeyTyped)
22985 {
22986 /* Check if the function already exists, don't let the user type the
22987 * whole function before telling him it doesn't work! For a script we
22988 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022989 if (!eap->skip && !eap->forceit)
22990 {
22991 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22992 EMSG(_(e_funcdict));
22993 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022994 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022997 if (!eap->skip && did_emsg)
22998 goto erret;
22999
Bram Moolenaar071d4272004-06-13 20:20:40 +000023000 msg_putchar('\n'); /* don't overwrite the function name */
23001 cmdline_row = msg_row;
23002 }
23003
23004 indent = 2;
23005 nesting = 0;
23006 for (;;)
23007 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023008 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023009 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023010 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023011 saved_wait_return = FALSE;
23012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023014 sourcing_lnum_off = sourcing_lnum;
23015
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023016 if (line_arg != NULL)
23017 {
23018 /* Use eap->arg, split up in parts by line breaks. */
23019 theline = line_arg;
23020 p = vim_strchr(theline, '\n');
23021 if (p == NULL)
23022 line_arg += STRLEN(line_arg);
23023 else
23024 {
23025 *p = NUL;
23026 line_arg = p + 1;
23027 }
23028 }
23029 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 theline = getcmdline(':', 0L, indent);
23031 else
23032 theline = eap->getline(':', eap->cookie, indent);
23033 if (KeyTyped)
23034 lines_left = Rows - 1;
23035 if (theline == NULL)
23036 {
23037 EMSG(_("E126: Missing :endfunction"));
23038 goto erret;
23039 }
23040
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023041 /* Detect line continuation: sourcing_lnum increased more than one. */
23042 if (sourcing_lnum > sourcing_lnum_off + 1)
23043 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23044 else
23045 sourcing_lnum_off = 0;
23046
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047 if (skip_until != NULL)
23048 {
23049 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23050 * don't check for ":endfunc". */
23051 if (STRCMP(theline, skip_until) == 0)
23052 {
23053 vim_free(skip_until);
23054 skip_until = NULL;
23055 }
23056 }
23057 else
23058 {
23059 /* skip ':' and blanks*/
23060 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23061 ;
23062
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023063 /* Check for "endfunction". */
23064 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023066 if (line_arg == NULL)
23067 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 break;
23069 }
23070
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023071 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023072 * at "end". */
23073 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23074 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023075 else if (STRNCMP(p, "if", 2) == 0
23076 || STRNCMP(p, "wh", 2) == 0
23077 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078 || STRNCMP(p, "try", 3) == 0)
23079 indent += 2;
23080
23081 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023082 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023083 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023084 if (*p == '!')
23085 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023086 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023087 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23088 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023090 ++nesting;
23091 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023092 }
23093 }
23094
23095 /* Check for ":append" or ":insert". */
23096 p = skip_range(p, NULL);
23097 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23098 || (p[0] == 'i'
23099 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23100 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23101 skip_until = vim_strsave((char_u *)".");
23102
23103 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23104 arg = skipwhite(skiptowhite(p));
23105 if (arg[0] == '<' && arg[1] =='<'
23106 && ((p[0] == 'p' && p[1] == 'y'
23107 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23108 || (p[0] == 'p' && p[1] == 'e'
23109 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23110 || (p[0] == 't' && p[1] == 'c'
23111 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023112 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23113 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23115 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023116 || (p[0] == 'm' && p[1] == 'z'
23117 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118 ))
23119 {
23120 /* ":python <<" continues until a dot, like ":append" */
23121 p = skipwhite(arg + 2);
23122 if (*p == NUL)
23123 skip_until = vim_strsave((char_u *)".");
23124 else
23125 skip_until = vim_strsave(p);
23126 }
23127 }
23128
23129 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023130 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023131 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023132 if (line_arg == NULL)
23133 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023135 }
23136
23137 /* Copy the line to newly allocated memory. get_one_sourceline()
23138 * allocates 250 bytes per line, this saves 80% on average. The cost
23139 * is an extra alloc/free. */
23140 p = vim_strsave(theline);
23141 if (p != NULL)
23142 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023143 if (line_arg == NULL)
23144 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023145 theline = p;
23146 }
23147
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023148 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23149
23150 /* Add NULL lines for continuation lines, so that the line count is
23151 * equal to the index in the growarray. */
23152 while (sourcing_lnum_off-- > 0)
23153 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023154
23155 /* Check for end of eap->arg. */
23156 if (line_arg != NULL && *line_arg == NUL)
23157 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 }
23159
23160 /* Don't define the function when skipping commands or when an error was
23161 * detected. */
23162 if (eap->skip || did_emsg)
23163 goto erret;
23164
23165 /*
23166 * If there are no errors, add the function
23167 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023168 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023169 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023170 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023171 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023172 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023173 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023174 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023175 goto erret;
23176 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023177
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023178 fp = find_func(name);
23179 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023181 if (!eap->forceit)
23182 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023183 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023184 goto erret;
23185 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023186 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023187 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023188 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023189 name);
23190 goto erret;
23191 }
23192 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023193 ga_clear_strings(&(fp->uf_args));
23194 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023195 vim_free(name);
23196 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023198 }
23199 else
23200 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023201 char numbuf[20];
23202
23203 fp = NULL;
23204 if (fudi.fd_newkey == NULL && !eap->forceit)
23205 {
23206 EMSG(_(e_funcdict));
23207 goto erret;
23208 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023209 if (fudi.fd_di == NULL)
23210 {
23211 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023212 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023213 goto erret;
23214 }
23215 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023216 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023217 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023218
23219 /* Give the function a sequential number. Can only be used with a
23220 * Funcref! */
23221 vim_free(name);
23222 sprintf(numbuf, "%d", ++func_nr);
23223 name = vim_strsave((char_u *)numbuf);
23224 if (name == NULL)
23225 goto erret;
23226 }
23227
23228 if (fp == NULL)
23229 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023230 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023231 {
23232 int slen, plen;
23233 char_u *scriptname;
23234
23235 /* Check that the autoload name matches the script name. */
23236 j = FAIL;
23237 if (sourcing_name != NULL)
23238 {
23239 scriptname = autoload_name(name);
23240 if (scriptname != NULL)
23241 {
23242 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023243 plen = (int)STRLEN(p);
23244 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023245 if (slen > plen && fnamecmp(p,
23246 sourcing_name + slen - plen) == 0)
23247 j = OK;
23248 vim_free(scriptname);
23249 }
23250 }
23251 if (j == FAIL)
23252 {
23253 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23254 goto erret;
23255 }
23256 }
23257
23258 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023259 if (fp == NULL)
23260 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023261
23262 if (fudi.fd_dict != NULL)
23263 {
23264 if (fudi.fd_di == NULL)
23265 {
23266 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023267 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023268 if (fudi.fd_di == NULL)
23269 {
23270 vim_free(fp);
23271 goto erret;
23272 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023273 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23274 {
23275 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023276 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023277 goto erret;
23278 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023279 }
23280 else
23281 /* overwrite existing dict entry */
23282 clear_tv(&fudi.fd_di->di_tv);
23283 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023284 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023285 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023286 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023287
23288 /* behave like "dict" was used */
23289 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023290 }
23291
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023293 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023294 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23295 {
23296 vim_free(fp);
23297 goto erret;
23298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023299 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023300 fp->uf_args = newargs;
23301 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023302#ifdef FEAT_PROFILE
23303 fp->uf_tml_count = NULL;
23304 fp->uf_tml_total = NULL;
23305 fp->uf_tml_self = NULL;
23306 fp->uf_profiling = FALSE;
23307 if (prof_def_func())
23308 func_do_profile(fp);
23309#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023310 fp->uf_varargs = varargs;
23311 fp->uf_flags = flags;
23312 fp->uf_calls = 0;
23313 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023314 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315
23316erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317 ga_clear_strings(&newargs);
23318 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023319ret_free:
23320 vim_free(skip_until);
23321 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023322 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023324 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023325}
23326
23327/*
23328 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023329 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023331 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023332 * TFN_INT: internal function name OK
23333 * TFN_QUIET: be quiet
23334 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023335 * Advances "pp" to just after the function name (if no error).
23336 */
23337 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023338trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023339 char_u **pp;
23340 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023341 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023342 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023343{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023344 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023345 char_u *start;
23346 char_u *end;
23347 int lead;
23348 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023349 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023350 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023351
23352 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023353 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023355
23356 /* Check for hard coded <SNR>: already translated function ID (from a user
23357 * command). */
23358 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23359 && (*pp)[2] == (int)KE_SNR)
23360 {
23361 *pp += 3;
23362 len = get_id_len(pp) + 3;
23363 return vim_strnsave(start, len);
23364 }
23365
23366 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23367 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023368 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023369 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023371
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023372 /* Note that TFN_ flags use the same values as GLV_ flags. */
23373 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023374 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023375 if (end == start)
23376 {
23377 if (!skip)
23378 EMSG(_("E129: Function name required"));
23379 goto theend;
23380 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023381 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023382 {
23383 /*
23384 * Report an invalid expression in braces, unless the expression
23385 * evaluation has been cancelled due to an aborting error, an
23386 * interrupt, or an exception.
23387 */
23388 if (!aborting())
23389 {
23390 if (end != NULL)
23391 EMSG2(_(e_invarg2), start);
23392 }
23393 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023394 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023395 goto theend;
23396 }
23397
23398 if (lv.ll_tv != NULL)
23399 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023400 if (fdp != NULL)
23401 {
23402 fdp->fd_dict = lv.ll_dict;
23403 fdp->fd_newkey = lv.ll_newkey;
23404 lv.ll_newkey = NULL;
23405 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023406 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023407 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23408 {
23409 name = vim_strsave(lv.ll_tv->vval.v_string);
23410 *pp = end;
23411 }
23412 else
23413 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023414 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23415 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023416 EMSG(_(e_funcref));
23417 else
23418 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023419 name = NULL;
23420 }
23421 goto theend;
23422 }
23423
23424 if (lv.ll_name == NULL)
23425 {
23426 /* Error found, but continue after the function name. */
23427 *pp = end;
23428 goto theend;
23429 }
23430
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023431 /* Check if the name is a Funcref. If so, use the value. */
23432 if (lv.ll_exp_name != NULL)
23433 {
23434 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023435 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023436 if (name == lv.ll_exp_name)
23437 name = NULL;
23438 }
23439 else
23440 {
23441 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023442 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023443 if (name == *pp)
23444 name = NULL;
23445 }
23446 if (name != NULL)
23447 {
23448 name = vim_strsave(name);
23449 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023450 if (STRNCMP(name, "<SNR>", 5) == 0)
23451 {
23452 /* Change "<SNR>" to the byte sequence. */
23453 name[0] = K_SPECIAL;
23454 name[1] = KS_EXTRA;
23455 name[2] = (int)KE_SNR;
23456 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23457 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023458 goto theend;
23459 }
23460
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023461 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023462 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023463 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023464 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23465 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23466 {
23467 /* When there was "s:" already or the name expanded to get a
23468 * leading "s:" then remove it. */
23469 lv.ll_name += 2;
23470 len -= 2;
23471 lead = 2;
23472 }
23473 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023474 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023475 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023476 /* skip over "s:" and "g:" */
23477 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023478 lv.ll_name += 2;
23479 len = (int)(end - lv.ll_name);
23480 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023481
23482 /*
23483 * Copy the function name to allocated memory.
23484 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23485 * Accept <SNR>123_name() outside a script.
23486 */
23487 if (skip)
23488 lead = 0; /* do nothing */
23489 else if (lead > 0)
23490 {
23491 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023492 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23493 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023494 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023495 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023496 if (current_SID <= 0)
23497 {
23498 EMSG(_(e_usingsid));
23499 goto theend;
23500 }
23501 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23502 lead += (int)STRLEN(sid_buf);
23503 }
23504 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023505 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023506 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023507 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023508 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023509 goto theend;
23510 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023511 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023512 {
23513 char_u *cp = vim_strchr(lv.ll_name, ':');
23514
23515 if (cp != NULL && cp < end)
23516 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023517 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023518 goto theend;
23519 }
23520 }
23521
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023522 name = alloc((unsigned)(len + lead + 1));
23523 if (name != NULL)
23524 {
23525 if (lead > 0)
23526 {
23527 name[0] = K_SPECIAL;
23528 name[1] = KS_EXTRA;
23529 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023530 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023531 STRCPY(name + 3, sid_buf);
23532 }
23533 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023534 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023535 }
23536 *pp = end;
23537
23538theend:
23539 clear_lval(&lv);
23540 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541}
23542
23543/*
23544 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23545 * Return 2 if "p" starts with "s:".
23546 * Return 0 otherwise.
23547 */
23548 static int
23549eval_fname_script(p)
23550 char_u *p;
23551{
23552 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23553 || STRNICMP(p + 1, "SNR>", 4) == 0))
23554 return 5;
23555 if (p[0] == 's' && p[1] == ':')
23556 return 2;
23557 return 0;
23558}
23559
23560/*
23561 * Return TRUE if "p" starts with "<SID>" or "s:".
23562 * Only works if eval_fname_script() returned non-zero for "p"!
23563 */
23564 static int
23565eval_fname_sid(p)
23566 char_u *p;
23567{
23568 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23569}
23570
23571/*
23572 * List the head of the function: "name(arg1, arg2)".
23573 */
23574 static void
23575list_func_head(fp, indent)
23576 ufunc_T *fp;
23577 int indent;
23578{
23579 int j;
23580
23581 msg_start();
23582 if (indent)
23583 MSG_PUTS(" ");
23584 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023585 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586 {
23587 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023588 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589 }
23590 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023591 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023593 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594 {
23595 if (j)
23596 MSG_PUTS(", ");
23597 msg_puts(FUNCARG(fp, j));
23598 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023599 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600 {
23601 if (j)
23602 MSG_PUTS(", ");
23603 MSG_PUTS("...");
23604 }
23605 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023606 if (fp->uf_flags & FC_ABORT)
23607 MSG_PUTS(" abort");
23608 if (fp->uf_flags & FC_RANGE)
23609 MSG_PUTS(" range");
23610 if (fp->uf_flags & FC_DICT)
23611 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023612 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023613 if (p_verbose > 0)
23614 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615}
23616
23617/*
23618 * Find a function by name, return pointer to it in ufuncs.
23619 * Return NULL for unknown function.
23620 */
23621 static ufunc_T *
23622find_func(name)
23623 char_u *name;
23624{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023625 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023627 hi = hash_find(&func_hashtab, name);
23628 if (!HASHITEM_EMPTY(hi))
23629 return HI2UF(hi);
23630 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631}
23632
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023633#if defined(EXITFREE) || defined(PROTO)
23634 void
23635free_all_functions()
23636{
23637 hashitem_T *hi;
23638
23639 /* Need to start all over every time, because func_free() may change the
23640 * hash table. */
23641 while (func_hashtab.ht_used > 0)
23642 for (hi = func_hashtab.ht_array; ; ++hi)
23643 if (!HASHITEM_EMPTY(hi))
23644 {
23645 func_free(HI2UF(hi));
23646 break;
23647 }
23648}
23649#endif
23650
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023651 int
23652translated_function_exists(name)
23653 char_u *name;
23654{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023655 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023656 return find_internal_func(name) >= 0;
23657 return find_func(name) != NULL;
23658}
23659
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023660/*
23661 * Return TRUE if a function "name" exists.
23662 */
23663 static int
23664function_exists(name)
23665 char_u *name;
23666{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023667 char_u *nm = name;
23668 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023669 int n = FALSE;
23670
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023671 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23672 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023673 nm = skipwhite(nm);
23674
23675 /* Only accept "funcname", "funcname ", "funcname (..." and
23676 * "funcname(...", not "funcname!...". */
23677 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023678 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023679 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023680 return n;
23681}
23682
Bram Moolenaara1544c02013-05-30 12:35:52 +020023683 char_u *
23684get_expanded_name(name, check)
23685 char_u *name;
23686 int check;
23687{
23688 char_u *nm = name;
23689 char_u *p;
23690
23691 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23692
23693 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023694 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023695 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023696
Bram Moolenaara1544c02013-05-30 12:35:52 +020023697 vim_free(p);
23698 return NULL;
23699}
23700
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023701/*
23702 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023703 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23704 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023705 */
23706 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023707builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023708 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023709 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023710{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023711 char_u *p;
23712
23713 if (!ASCII_ISLOWER(name[0]))
23714 return FALSE;
23715 p = vim_strchr(name, AUTOLOAD_CHAR);
23716 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023717}
23718
Bram Moolenaar05159a02005-02-26 23:04:13 +000023719#if defined(FEAT_PROFILE) || defined(PROTO)
23720/*
23721 * Start profiling function "fp".
23722 */
23723 static void
23724func_do_profile(fp)
23725 ufunc_T *fp;
23726{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023727 int len = fp->uf_lines.ga_len;
23728
23729 if (len == 0)
23730 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023731 fp->uf_tm_count = 0;
23732 profile_zero(&fp->uf_tm_self);
23733 profile_zero(&fp->uf_tm_total);
23734 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023735 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023736 if (fp->uf_tml_total == NULL)
23737 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023738 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023739 if (fp->uf_tml_self == NULL)
23740 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023741 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023742 fp->uf_tml_idx = -1;
23743 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23744 || fp->uf_tml_self == NULL)
23745 return; /* out of memory */
23746
23747 fp->uf_profiling = TRUE;
23748}
23749
23750/*
23751 * Dump the profiling results for all functions in file "fd".
23752 */
23753 void
23754func_dump_profile(fd)
23755 FILE *fd;
23756{
23757 hashitem_T *hi;
23758 int todo;
23759 ufunc_T *fp;
23760 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023761 ufunc_T **sorttab;
23762 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023763
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023764 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023765 if (todo == 0)
23766 return; /* nothing to dump */
23767
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023768 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023769
Bram Moolenaar05159a02005-02-26 23:04:13 +000023770 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23771 {
23772 if (!HASHITEM_EMPTY(hi))
23773 {
23774 --todo;
23775 fp = HI2UF(hi);
23776 if (fp->uf_profiling)
23777 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023778 if (sorttab != NULL)
23779 sorttab[st_len++] = fp;
23780
Bram Moolenaar05159a02005-02-26 23:04:13 +000023781 if (fp->uf_name[0] == K_SPECIAL)
23782 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23783 else
23784 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23785 if (fp->uf_tm_count == 1)
23786 fprintf(fd, "Called 1 time\n");
23787 else
23788 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23789 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23790 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23791 fprintf(fd, "\n");
23792 fprintf(fd, "count total (s) self (s)\n");
23793
23794 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23795 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023796 if (FUNCLINE(fp, i) == NULL)
23797 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023798 prof_func_line(fd, fp->uf_tml_count[i],
23799 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023800 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23801 }
23802 fprintf(fd, "\n");
23803 }
23804 }
23805 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023806
23807 if (sorttab != NULL && st_len > 0)
23808 {
23809 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23810 prof_total_cmp);
23811 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23812 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23813 prof_self_cmp);
23814 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23815 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023816
23817 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023818}
Bram Moolenaar73830342005-02-28 22:48:19 +000023819
23820 static void
23821prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23822 FILE *fd;
23823 ufunc_T **sorttab;
23824 int st_len;
23825 char *title;
23826 int prefer_self; /* when equal print only self time */
23827{
23828 int i;
23829 ufunc_T *fp;
23830
23831 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23832 fprintf(fd, "count total (s) self (s) function\n");
23833 for (i = 0; i < 20 && i < st_len; ++i)
23834 {
23835 fp = sorttab[i];
23836 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23837 prefer_self);
23838 if (fp->uf_name[0] == K_SPECIAL)
23839 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23840 else
23841 fprintf(fd, " %s()\n", fp->uf_name);
23842 }
23843 fprintf(fd, "\n");
23844}
23845
23846/*
23847 * Print the count and times for one function or function line.
23848 */
23849 static void
23850prof_func_line(fd, count, total, self, prefer_self)
23851 FILE *fd;
23852 int count;
23853 proftime_T *total;
23854 proftime_T *self;
23855 int prefer_self; /* when equal print only self time */
23856{
23857 if (count > 0)
23858 {
23859 fprintf(fd, "%5d ", count);
23860 if (prefer_self && profile_equal(total, self))
23861 fprintf(fd, " ");
23862 else
23863 fprintf(fd, "%s ", profile_msg(total));
23864 if (!prefer_self && profile_equal(total, self))
23865 fprintf(fd, " ");
23866 else
23867 fprintf(fd, "%s ", profile_msg(self));
23868 }
23869 else
23870 fprintf(fd, " ");
23871}
23872
23873/*
23874 * Compare function for total time sorting.
23875 */
23876 static int
23877#ifdef __BORLANDC__
23878_RTLENTRYF
23879#endif
23880prof_total_cmp(s1, s2)
23881 const void *s1;
23882 const void *s2;
23883{
23884 ufunc_T *p1, *p2;
23885
23886 p1 = *(ufunc_T **)s1;
23887 p2 = *(ufunc_T **)s2;
23888 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23889}
23890
23891/*
23892 * Compare function for self time sorting.
23893 */
23894 static int
23895#ifdef __BORLANDC__
23896_RTLENTRYF
23897#endif
23898prof_self_cmp(s1, s2)
23899 const void *s1;
23900 const void *s2;
23901{
23902 ufunc_T *p1, *p2;
23903
23904 p1 = *(ufunc_T **)s1;
23905 p2 = *(ufunc_T **)s2;
23906 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23907}
23908
Bram Moolenaar05159a02005-02-26 23:04:13 +000023909#endif
23910
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023911/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023912 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023913 * Return TRUE if a package was loaded.
23914 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023915 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023916script_autoload(name, reload)
23917 char_u *name;
23918 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023919{
23920 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023921 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023922 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023923 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023924
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023925 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023926 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023927 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023928 return FALSE;
23929
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023930 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023931
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023932 /* Find the name in the list of previously loaded package names. Skip
23933 * "autoload/", it's always the same. */
23934 for (i = 0; i < ga_loaded.ga_len; ++i)
23935 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23936 break;
23937 if (!reload && i < ga_loaded.ga_len)
23938 ret = FALSE; /* was loaded already */
23939 else
23940 {
23941 /* Remember the name if it wasn't loaded already. */
23942 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23943 {
23944 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23945 tofree = NULL;
23946 }
23947
23948 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023949 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023950 ret = TRUE;
23951 }
23952
23953 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023954 return ret;
23955}
23956
23957/*
23958 * Return the autoload script name for a function or variable name.
23959 * Returns NULL when out of memory.
23960 */
23961 static char_u *
23962autoload_name(name)
23963 char_u *name;
23964{
23965 char_u *p;
23966 char_u *scriptname;
23967
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023968 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023969 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23970 if (scriptname == NULL)
23971 return FALSE;
23972 STRCPY(scriptname, "autoload/");
23973 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023974 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023975 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023976 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023977 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023978 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023979}
23980
Bram Moolenaar071d4272004-06-13 20:20:40 +000023981#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23982
23983/*
23984 * Function given to ExpandGeneric() to obtain the list of user defined
23985 * function names.
23986 */
23987 char_u *
23988get_user_func_name(xp, idx)
23989 expand_T *xp;
23990 int idx;
23991{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023992 static long_u done;
23993 static hashitem_T *hi;
23994 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995
23996 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023998 done = 0;
23999 hi = func_hashtab.ht_array;
24000 }
24001 if (done < func_hashtab.ht_used)
24002 {
24003 if (done++ > 0)
24004 ++hi;
24005 while (HASHITEM_EMPTY(hi))
24006 ++hi;
24007 fp = HI2UF(hi);
24008
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024009 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024010 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024011
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024012 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24013 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024014
24015 cat_func_name(IObuff, fp);
24016 if (xp->xp_context != EXPAND_USER_FUNC)
24017 {
24018 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024019 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024020 STRCAT(IObuff, ")");
24021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024022 return IObuff;
24023 }
24024 return NULL;
24025}
24026
24027#endif /* FEAT_CMDL_COMPL */
24028
24029/*
24030 * Copy the function name of "fp" to buffer "buf".
24031 * "buf" must be able to hold the function name plus three bytes.
24032 * Takes care of script-local function names.
24033 */
24034 static void
24035cat_func_name(buf, fp)
24036 char_u *buf;
24037 ufunc_T *fp;
24038{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024039 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024040 {
24041 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024042 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024043 }
24044 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024045 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024046}
24047
24048/*
24049 * ":delfunction {name}"
24050 */
24051 void
24052ex_delfunction(eap)
24053 exarg_T *eap;
24054{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024055 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024056 char_u *p;
24057 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024058 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059
24060 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024061 name = trans_function_name(&p, eap->skip, 0, &fudi);
24062 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024064 {
24065 if (fudi.fd_dict != NULL && !eap->skip)
24066 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024067 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024069 if (!ends_excmd(*skipwhite(p)))
24070 {
24071 vim_free(name);
24072 EMSG(_(e_trailing));
24073 return;
24074 }
24075 eap->nextcmd = check_nextcmd(p);
24076 if (eap->nextcmd != NULL)
24077 *p = NUL;
24078
24079 if (!eap->skip)
24080 fp = find_func(name);
24081 vim_free(name);
24082
24083 if (!eap->skip)
24084 {
24085 if (fp == NULL)
24086 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024087 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088 return;
24089 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024090 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024091 {
24092 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24093 return;
24094 }
24095
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024096 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024098 /* Delete the dict item that refers to the function, it will
24099 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024100 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024102 else
24103 func_free(fp);
24104 }
24105}
24106
24107/*
24108 * Free a function and remove it from the list of functions.
24109 */
24110 static void
24111func_free(fp)
24112 ufunc_T *fp;
24113{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024114 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024115
24116 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024117 ga_clear_strings(&(fp->uf_args));
24118 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024119#ifdef FEAT_PROFILE
24120 vim_free(fp->uf_tml_count);
24121 vim_free(fp->uf_tml_total);
24122 vim_free(fp->uf_tml_self);
24123#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024124
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024125 /* remove the function from the function hashtable */
24126 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24127 if (HASHITEM_EMPTY(hi))
24128 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024129 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024130 hash_remove(&func_hashtab, hi);
24131
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024132 vim_free(fp);
24133}
24134
24135/*
24136 * Unreference a Function: decrement the reference count and free it when it
24137 * becomes zero. Only for numbered functions.
24138 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024139 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024140func_unref(name)
24141 char_u *name;
24142{
24143 ufunc_T *fp;
24144
24145 if (name != NULL && isdigit(*name))
24146 {
24147 fp = find_func(name);
24148 if (fp == NULL)
24149 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024150 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024151 {
24152 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024153 * when "uf_calls" becomes zero. */
24154 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024155 func_free(fp);
24156 }
24157 }
24158}
24159
24160/*
24161 * Count a reference to a Function.
24162 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024163 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024164func_ref(name)
24165 char_u *name;
24166{
24167 ufunc_T *fp;
24168
24169 if (name != NULL && isdigit(*name))
24170 {
24171 fp = find_func(name);
24172 if (fp == NULL)
24173 EMSG2(_(e_intern2), "func_ref()");
24174 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024175 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176 }
24177}
24178
24179/*
24180 * Call a user function.
24181 */
24182 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024183call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184 ufunc_T *fp; /* pointer to function */
24185 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024186 typval_T *argvars; /* arguments */
24187 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188 linenr_T firstline; /* first line of range */
24189 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024190 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191{
Bram Moolenaar33570922005-01-25 22:26:29 +000024192 char_u *save_sourcing_name;
24193 linenr_T save_sourcing_lnum;
24194 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024195 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024196 int save_did_emsg;
24197 static int depth = 0;
24198 dictitem_T *v;
24199 int fixvar_idx = 0; /* index in fixvar[] */
24200 int i;
24201 int ai;
24202 char_u numbuf[NUMBUFLEN];
24203 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024204 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024205#ifdef FEAT_PROFILE
24206 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024207 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024209
24210 /* If depth of calling is getting too high, don't execute the function */
24211 if (depth >= p_mfd)
24212 {
24213 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024214 rettv->v_type = VAR_NUMBER;
24215 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216 return;
24217 }
24218 ++depth;
24219
24220 line_breakcheck(); /* check for CTRL-C hit */
24221
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024222 fc = (funccall_T *)alloc(sizeof(funccall_T));
24223 fc->caller = current_funccal;
24224 current_funccal = fc;
24225 fc->func = fp;
24226 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024227 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024228 fc->linenr = 0;
24229 fc->returned = FALSE;
24230 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024231 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024232 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24233 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234
Bram Moolenaar33570922005-01-25 22:26:29 +000024235 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024236 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024237 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24238 * each argument variable and saves a lot of time.
24239 */
24240 /*
24241 * Init l: variables.
24242 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024243 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024244 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024245 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024246 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24247 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024248 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024249 name = v->di_key;
24250 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024251 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024252 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024253 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024254 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024255 v->di_tv.vval.v_dict = selfdict;
24256 ++selfdict->dv_refcount;
24257 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024258
Bram Moolenaar33570922005-01-25 22:26:29 +000024259 /*
24260 * Init a: variables.
24261 * Set a:0 to "argcount".
24262 * Set a:000 to a list with room for the "..." arguments.
24263 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024264 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024265 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024266 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024267 /* Use "name" to avoid a warning from some compiler that checks the
24268 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024269 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024270 name = v->di_key;
24271 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024272 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024273 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024274 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024275 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024276 v->di_tv.vval.v_list = &fc->l_varlist;
24277 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24278 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24279 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024280
24281 /*
24282 * Set a:firstline to "firstline" and a:lastline to "lastline".
24283 * Set a:name to named arguments.
24284 * Set a:N to the "..." arguments.
24285 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024286 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024287 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024288 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024289 (varnumber_T)lastline);
24290 for (i = 0; i < argcount; ++i)
24291 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024292 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024293 if (ai < 0)
24294 /* named argument a:name */
24295 name = FUNCARG(fp, i);
24296 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024297 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024298 /* "..." argument a:1, a:2, etc. */
24299 sprintf((char *)numbuf, "%d", ai + 1);
24300 name = numbuf;
24301 }
24302 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24303 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024304 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024305 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24306 }
24307 else
24308 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024309 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24310 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024311 if (v == NULL)
24312 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024313 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024314 }
24315 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024316 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024317
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024318 /* Note: the values are copied directly to avoid alloc/free.
24319 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024320 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024321 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024322
24323 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24324 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024325 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24326 fc->l_listitems[ai].li_tv = argvars[i];
24327 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024328 }
24329 }
24330
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331 /* Don't redraw while executing the function. */
24332 ++RedrawingDisabled;
24333 save_sourcing_name = sourcing_name;
24334 save_sourcing_lnum = sourcing_lnum;
24335 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024336 /* need space for function name + ("function " + 3) or "[number]" */
24337 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24338 + STRLEN(fp->uf_name) + 20;
24339 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340 if (sourcing_name != NULL)
24341 {
24342 if (save_sourcing_name != NULL
24343 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024344 sprintf((char *)sourcing_name, "%s[%d]..",
24345 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346 else
24347 STRCPY(sourcing_name, "function ");
24348 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24349
24350 if (p_verbose >= 12)
24351 {
24352 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024353 verbose_enter_scroll();
24354
Bram Moolenaar555b2802005-05-19 21:08:39 +000024355 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024356 if (p_verbose >= 14)
24357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024358 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024359 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024360 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024361 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024362
24363 msg_puts((char_u *)"(");
24364 for (i = 0; i < argcount; ++i)
24365 {
24366 if (i > 0)
24367 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024368 if (argvars[i].v_type == VAR_NUMBER)
24369 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024370 else
24371 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024372 /* Do not want errors such as E724 here. */
24373 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024374 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024375 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024376 if (s != NULL)
24377 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024378 if (vim_strsize(s) > MSG_BUF_CLEN)
24379 {
24380 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24381 s = buf;
24382 }
24383 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024384 vim_free(tofree);
24385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386 }
24387 }
24388 msg_puts((char_u *)")");
24389 }
24390 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024391
24392 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024393 --no_wait_return;
24394 }
24395 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024396#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024397 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024398 {
24399 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24400 func_do_profile(fp);
24401 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024402 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024403 {
24404 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024405 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024406 profile_zero(&fp->uf_tm_children);
24407 }
24408 script_prof_save(&wait_start);
24409 }
24410#endif
24411
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024413 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414 save_did_emsg = did_emsg;
24415 did_emsg = FALSE;
24416
24417 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024418 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024419 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24420
24421 --RedrawingDisabled;
24422
24423 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024424 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024425 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024426 clear_tv(rettv);
24427 rettv->v_type = VAR_NUMBER;
24428 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024429 }
24430
Bram Moolenaar05159a02005-02-26 23:04:13 +000024431#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024432 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024433 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024434 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024435 profile_end(&call_start);
24436 profile_sub_wait(&wait_start, &call_start);
24437 profile_add(&fp->uf_tm_total, &call_start);
24438 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024439 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024440 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024441 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24442 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024443 }
24444 }
24445#endif
24446
Bram Moolenaar071d4272004-06-13 20:20:40 +000024447 /* when being verbose, mention the return value */
24448 if (p_verbose >= 12)
24449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024450 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024451 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024452
Bram Moolenaar071d4272004-06-13 20:20:40 +000024453 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024454 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024455 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024456 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024457 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024458 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024459 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024460 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024461 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024462 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024463 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024464
Bram Moolenaar555b2802005-05-19 21:08:39 +000024465 /* The value may be very long. Skip the middle part, so that we
24466 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024467 * truncate it at the end. Don't want errors such as E724 here. */
24468 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024469 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024470 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024471 if (s != NULL)
24472 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024473 if (vim_strsize(s) > MSG_BUF_CLEN)
24474 {
24475 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24476 s = buf;
24477 }
24478 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024479 vim_free(tofree);
24480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481 }
24482 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024483
24484 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024485 --no_wait_return;
24486 }
24487
24488 vim_free(sourcing_name);
24489 sourcing_name = save_sourcing_name;
24490 sourcing_lnum = save_sourcing_lnum;
24491 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024492#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024493 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024494 script_prof_restore(&wait_start);
24495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024496
24497 if (p_verbose >= 12 && sourcing_name != NULL)
24498 {
24499 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024500 verbose_enter_scroll();
24501
Bram Moolenaar555b2802005-05-19 21:08:39 +000024502 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024504
24505 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024506 --no_wait_return;
24507 }
24508
24509 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024510 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024511 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024512
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024513 /* If the a:000 list and the l: and a: dicts are not referenced we can
24514 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024515 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24516 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24517 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24518 {
24519 free_funccal(fc, FALSE);
24520 }
24521 else
24522 {
24523 hashitem_T *hi;
24524 listitem_T *li;
24525 int todo;
24526
24527 /* "fc" is still in use. This can happen when returning "a:000" or
24528 * assigning "l:" to a global variable.
24529 * Link "fc" in the list for garbage collection later. */
24530 fc->caller = previous_funccal;
24531 previous_funccal = fc;
24532
24533 /* Make a copy of the a: variables, since we didn't do that above. */
24534 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24535 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24536 {
24537 if (!HASHITEM_EMPTY(hi))
24538 {
24539 --todo;
24540 v = HI2DI(hi);
24541 copy_tv(&v->di_tv, &v->di_tv);
24542 }
24543 }
24544
24545 /* Make a copy of the a:000 items, since we didn't do that above. */
24546 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24547 copy_tv(&li->li_tv, &li->li_tv);
24548 }
24549}
24550
24551/*
24552 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024553 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024554 */
24555 static int
24556can_free_funccal(fc, copyID)
24557 funccall_T *fc;
24558 int copyID;
24559{
24560 return (fc->l_varlist.lv_copyID != copyID
24561 && fc->l_vars.dv_copyID != copyID
24562 && fc->l_avars.dv_copyID != copyID);
24563}
24564
24565/*
24566 * Free "fc" and what it contains.
24567 */
24568 static void
24569free_funccal(fc, free_val)
24570 funccall_T *fc;
24571 int free_val; /* a: vars were allocated */
24572{
24573 listitem_T *li;
24574
24575 /* The a: variables typevals may not have been allocated, only free the
24576 * allocated variables. */
24577 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24578
24579 /* free all l: variables */
24580 vars_clear(&fc->l_vars.dv_hashtab);
24581
24582 /* Free the a:000 variables if they were allocated. */
24583 if (free_val)
24584 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24585 clear_tv(&li->li_tv);
24586
24587 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588}
24589
24590/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024591 * Add a number variable "name" to dict "dp" with value "nr".
24592 */
24593 static void
24594add_nr_var(dp, v, name, nr)
24595 dict_T *dp;
24596 dictitem_T *v;
24597 char *name;
24598 varnumber_T nr;
24599{
24600 STRCPY(v->di_key, name);
24601 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24602 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24603 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024604 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024605 v->di_tv.vval.v_number = nr;
24606}
24607
24608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024609 * ":return [expr]"
24610 */
24611 void
24612ex_return(eap)
24613 exarg_T *eap;
24614{
24615 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024616 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024617 int returning = FALSE;
24618
24619 if (current_funccal == NULL)
24620 {
24621 EMSG(_("E133: :return not inside a function"));
24622 return;
24623 }
24624
24625 if (eap->skip)
24626 ++emsg_skip;
24627
24628 eap->nextcmd = NULL;
24629 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024630 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631 {
24632 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024633 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024635 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 }
24637 /* It's safer to return also on error. */
24638 else if (!eap->skip)
24639 {
24640 /*
24641 * Return unless the expression evaluation has been cancelled due to an
24642 * aborting error, an interrupt, or an exception.
24643 */
24644 if (!aborting())
24645 returning = do_return(eap, FALSE, TRUE, NULL);
24646 }
24647
24648 /* When skipping or the return gets pending, advance to the next command
24649 * in this line (!returning). Otherwise, ignore the rest of the line.
24650 * Following lines will be ignored by get_func_line(). */
24651 if (returning)
24652 eap->nextcmd = NULL;
24653 else if (eap->nextcmd == NULL) /* no argument */
24654 eap->nextcmd = check_nextcmd(arg);
24655
24656 if (eap->skip)
24657 --emsg_skip;
24658}
24659
24660/*
24661 * Return from a function. Possibly makes the return pending. Also called
24662 * for a pending return at the ":endtry" or after returning from an extra
24663 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024664 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024665 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024666 * FALSE when the return gets pending.
24667 */
24668 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024669do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670 exarg_T *eap;
24671 int reanimate;
24672 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024673 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024674{
24675 int idx;
24676 struct condstack *cstack = eap->cstack;
24677
24678 if (reanimate)
24679 /* Undo the return. */
24680 current_funccal->returned = FALSE;
24681
24682 /*
24683 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24684 * not in its finally clause (which then is to be executed next) is found.
24685 * In this case, make the ":return" pending for execution at the ":endtry".
24686 * Otherwise, return normally.
24687 */
24688 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24689 if (idx >= 0)
24690 {
24691 cstack->cs_pending[idx] = CSTP_RETURN;
24692
24693 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024694 /* A pending return again gets pending. "rettv" points to an
24695 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024696 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024697 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024698 else
24699 {
24700 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024701 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024702 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024703 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024704
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024705 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024706 {
24707 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024708 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024709 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024710 else
24711 EMSG(_(e_outofmem));
24712 }
24713 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024714 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024715
24716 if (reanimate)
24717 {
24718 /* The pending return value could be overwritten by a ":return"
24719 * without argument in a finally clause; reset the default
24720 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024721 current_funccal->rettv->v_type = VAR_NUMBER;
24722 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723 }
24724 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024725 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024726 }
24727 else
24728 {
24729 current_funccal->returned = TRUE;
24730
24731 /* If the return is carried out now, store the return value. For
24732 * a return immediately after reanimation, the value is already
24733 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024734 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024736 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024737 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024738 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024739 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024740 }
24741 }
24742
24743 return idx < 0;
24744}
24745
24746/*
24747 * Free the variable with a pending return value.
24748 */
24749 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024750discard_pending_return(rettv)
24751 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024752{
Bram Moolenaar33570922005-01-25 22:26:29 +000024753 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024754}
24755
24756/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024757 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024758 * is an allocated string. Used by report_pending() for verbose messages.
24759 */
24760 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024761get_return_cmd(rettv)
24762 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024763{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024764 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024765 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024766 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024767
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024768 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024769 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024770 if (s == NULL)
24771 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024772
24773 STRCPY(IObuff, ":return ");
24774 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24775 if (STRLEN(s) + 8 >= IOSIZE)
24776 STRCPY(IObuff + IOSIZE - 4, "...");
24777 vim_free(tofree);
24778 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779}
24780
24781/*
24782 * Get next function line.
24783 * Called by do_cmdline() to get the next line.
24784 * Returns allocated string, or NULL for end of function.
24785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024786 char_u *
24787get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024788 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024790 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024791{
Bram Moolenaar33570922005-01-25 22:26:29 +000024792 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024793 ufunc_T *fp = fcp->func;
24794 char_u *retval;
24795 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024796
24797 /* If breakpoints have been added/deleted need to check for it. */
24798 if (fcp->dbg_tick != debug_tick)
24799 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024800 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024801 sourcing_lnum);
24802 fcp->dbg_tick = debug_tick;
24803 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024804#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024805 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024806 func_line_end(cookie);
24807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024808
Bram Moolenaar05159a02005-02-26 23:04:13 +000024809 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024810 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24811 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812 retval = NULL;
24813 else
24814 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024815 /* Skip NULL lines (continuation lines). */
24816 while (fcp->linenr < gap->ga_len
24817 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24818 ++fcp->linenr;
24819 if (fcp->linenr >= gap->ga_len)
24820 retval = NULL;
24821 else
24822 {
24823 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24824 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024825#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024826 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024827 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024828#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024830 }
24831
24832 /* Did we encounter a breakpoint? */
24833 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24834 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024835 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024836 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024837 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024838 sourcing_lnum);
24839 fcp->dbg_tick = debug_tick;
24840 }
24841
24842 return retval;
24843}
24844
Bram Moolenaar05159a02005-02-26 23:04:13 +000024845#if defined(FEAT_PROFILE) || defined(PROTO)
24846/*
24847 * Called when starting to read a function line.
24848 * "sourcing_lnum" must be correct!
24849 * When skipping lines it may not actually be executed, but we won't find out
24850 * until later and we need to store the time now.
24851 */
24852 void
24853func_line_start(cookie)
24854 void *cookie;
24855{
24856 funccall_T *fcp = (funccall_T *)cookie;
24857 ufunc_T *fp = fcp->func;
24858
24859 if (fp->uf_profiling && sourcing_lnum >= 1
24860 && sourcing_lnum <= fp->uf_lines.ga_len)
24861 {
24862 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024863 /* Skip continuation lines. */
24864 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24865 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024866 fp->uf_tml_execed = FALSE;
24867 profile_start(&fp->uf_tml_start);
24868 profile_zero(&fp->uf_tml_children);
24869 profile_get_wait(&fp->uf_tml_wait);
24870 }
24871}
24872
24873/*
24874 * Called when actually executing a function line.
24875 */
24876 void
24877func_line_exec(cookie)
24878 void *cookie;
24879{
24880 funccall_T *fcp = (funccall_T *)cookie;
24881 ufunc_T *fp = fcp->func;
24882
24883 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24884 fp->uf_tml_execed = TRUE;
24885}
24886
24887/*
24888 * Called when done with a function line.
24889 */
24890 void
24891func_line_end(cookie)
24892 void *cookie;
24893{
24894 funccall_T *fcp = (funccall_T *)cookie;
24895 ufunc_T *fp = fcp->func;
24896
24897 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24898 {
24899 if (fp->uf_tml_execed)
24900 {
24901 ++fp->uf_tml_count[fp->uf_tml_idx];
24902 profile_end(&fp->uf_tml_start);
24903 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024904 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024905 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24906 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024907 }
24908 fp->uf_tml_idx = -1;
24909 }
24910}
24911#endif
24912
Bram Moolenaar071d4272004-06-13 20:20:40 +000024913/*
24914 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024915 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916 */
24917 int
24918func_has_ended(cookie)
24919 void *cookie;
24920{
Bram Moolenaar33570922005-01-25 22:26:29 +000024921 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922
24923 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24924 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024925 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024926 || fcp->returned);
24927}
24928
24929/*
24930 * return TRUE if cookie indicates a function which "abort"s on errors.
24931 */
24932 int
24933func_has_abort(cookie)
24934 void *cookie;
24935{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024936 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937}
24938
24939#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24940typedef enum
24941{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024942 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24943 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24944 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024945} var_flavour_T;
24946
24947static var_flavour_T var_flavour __ARGS((char_u *varname));
24948
24949 static var_flavour_T
24950var_flavour(varname)
24951 char_u *varname;
24952{
24953 char_u *p = varname;
24954
24955 if (ASCII_ISUPPER(*p))
24956 {
24957 while (*(++p))
24958 if (ASCII_ISLOWER(*p))
24959 return VAR_FLAVOUR_SESSION;
24960 return VAR_FLAVOUR_VIMINFO;
24961 }
24962 else
24963 return VAR_FLAVOUR_DEFAULT;
24964}
24965#endif
24966
24967#if defined(FEAT_VIMINFO) || defined(PROTO)
24968/*
24969 * Restore global vars that start with a capital from the viminfo file
24970 */
24971 int
24972read_viminfo_varlist(virp, writing)
24973 vir_T *virp;
24974 int writing;
24975{
24976 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024977 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024978 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024979
24980 if (!writing && (find_viminfo_parameter('!') != NULL))
24981 {
24982 tab = vim_strchr(virp->vir_line + 1, '\t');
24983 if (tab != NULL)
24984 {
24985 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024986 switch (*tab)
24987 {
24988 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024989#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024990 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024991#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024992 case 'D': type = VAR_DICT; break;
24993 case 'L': type = VAR_LIST; break;
24994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024995
24996 tab = vim_strchr(tab, '\t');
24997 if (tab != NULL)
24998 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024999 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025000 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025001 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025002 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025003#ifdef FEAT_FLOAT
25004 else if (type == VAR_FLOAT)
25005 (void)string2float(tab + 1, &tv.vval.v_float);
25006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025007 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025008 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025009 if (type == VAR_DICT || type == VAR_LIST)
25010 {
25011 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25012
25013 if (etv == NULL)
25014 /* Failed to parse back the dict or list, use it as a
25015 * string. */
25016 tv.v_type = VAR_STRING;
25017 else
25018 {
25019 vim_free(tv.vval.v_string);
25020 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025021 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025022 }
25023 }
25024
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025025 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025026
25027 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025028 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025029 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25030 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025031 }
25032 }
25033 }
25034
25035 return viminfo_readline(virp);
25036}
25037
25038/*
25039 * Write global vars that start with a capital to the viminfo file
25040 */
25041 void
25042write_viminfo_varlist(fp)
25043 FILE *fp;
25044{
Bram Moolenaar33570922005-01-25 22:26:29 +000025045 hashitem_T *hi;
25046 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025047 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025048 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025049 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025050 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025051 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052
25053 if (find_viminfo_parameter('!') == NULL)
25054 return;
25055
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025056 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025057
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025058 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025059 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025060 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025061 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025062 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025063 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025064 this_var = HI2DI(hi);
25065 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025066 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025067 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025068 {
25069 case VAR_STRING: s = "STR"; break;
25070 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025071#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025072 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025073#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025074 case VAR_DICT: s = "DIC"; break;
25075 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000025076 default: continue;
25077 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025078 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025079 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025080 if (p != NULL)
25081 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025082 vim_free(tofree);
25083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084 }
25085 }
25086}
25087#endif
25088
25089#if defined(FEAT_SESSION) || defined(PROTO)
25090 int
25091store_session_globals(fd)
25092 FILE *fd;
25093{
Bram Moolenaar33570922005-01-25 22:26:29 +000025094 hashitem_T *hi;
25095 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025096 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025097 char_u *p, *t;
25098
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025099 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025100 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025101 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025102 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025103 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025104 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025105 this_var = HI2DI(hi);
25106 if ((this_var->di_tv.v_type == VAR_NUMBER
25107 || this_var->di_tv.v_type == VAR_STRING)
25108 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025109 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025110 /* Escape special characters with a backslash. Turn a LF and
25111 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025112 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025113 (char_u *)"\\\"\n\r");
25114 if (p == NULL) /* out of memory */
25115 break;
25116 for (t = p; *t != NUL; ++t)
25117 if (*t == '\n')
25118 *t = 'n';
25119 else if (*t == '\r')
25120 *t = 'r';
25121 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025122 this_var->di_key,
25123 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25124 : ' ',
25125 p,
25126 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25127 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025128 || put_eol(fd) == FAIL)
25129 {
25130 vim_free(p);
25131 return FAIL;
25132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025133 vim_free(p);
25134 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025135#ifdef FEAT_FLOAT
25136 else if (this_var->di_tv.v_type == VAR_FLOAT
25137 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25138 {
25139 float_T f = this_var->di_tv.vval.v_float;
25140 int sign = ' ';
25141
25142 if (f < 0)
25143 {
25144 f = -f;
25145 sign = '-';
25146 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025147 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025148 this_var->di_key, sign, f) < 0)
25149 || put_eol(fd) == FAIL)
25150 return FAIL;
25151 }
25152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025153 }
25154 }
25155 return OK;
25156}
25157#endif
25158
Bram Moolenaar661b1822005-07-28 22:36:45 +000025159/*
25160 * Display script name where an item was last set.
25161 * Should only be invoked when 'verbose' is non-zero.
25162 */
25163 void
25164last_set_msg(scriptID)
25165 scid_T scriptID;
25166{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025167 char_u *p;
25168
Bram Moolenaar661b1822005-07-28 22:36:45 +000025169 if (scriptID != 0)
25170 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025171 p = home_replace_save(NULL, get_scriptname(scriptID));
25172 if (p != NULL)
25173 {
25174 verbose_enter();
25175 MSG_PUTS(_("\n\tLast set from "));
25176 MSG_PUTS(p);
25177 vim_free(p);
25178 verbose_leave();
25179 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025180 }
25181}
25182
Bram Moolenaard812df62008-11-09 12:46:09 +000025183/*
25184 * List v:oldfiles in a nice way.
25185 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025186 void
25187ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025188 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025189{
25190 list_T *l = vimvars[VV_OLDFILES].vv_list;
25191 listitem_T *li;
25192 int nr = 0;
25193
25194 if (l == NULL)
25195 msg((char_u *)_("No old files"));
25196 else
25197 {
25198 msg_start();
25199 msg_scroll = TRUE;
25200 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25201 {
25202 msg_outnum((long)++nr);
25203 MSG_PUTS(": ");
25204 msg_outtrans(get_tv_string(&li->li_tv));
25205 msg_putchar('\n');
25206 out_flush(); /* output one line at a time */
25207 ui_breakcheck();
25208 }
25209 /* Assume "got_int" was set to truncate the listing. */
25210 got_int = FALSE;
25211
25212#ifdef FEAT_BROWSE_CMD
25213 if (cmdmod.browse)
25214 {
25215 quit_more = FALSE;
25216 nr = prompt_for_number(FALSE);
25217 msg_starthere();
25218 if (nr > 0)
25219 {
25220 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25221 (long)nr);
25222
25223 if (p != NULL)
25224 {
25225 p = expand_env_save(p);
25226 eap->arg = p;
25227 eap->cmdidx = CMD_edit;
25228 cmdmod.browse = FALSE;
25229 do_exedit(eap, NULL);
25230 vim_free(p);
25231 }
25232 }
25233 }
25234#endif
25235 }
25236}
25237
Bram Moolenaar53744302015-07-17 17:38:22 +020025238/* reset v:option_new, v:option_old and v:option_type */
25239 void
25240reset_v_option_vars()
25241{
25242 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25243 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25244 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25245}
25246
25247
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248#endif /* FEAT_EVAL */
25249
Bram Moolenaar071d4272004-06-13 20:20:40 +000025250
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025251#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252
25253#ifdef WIN3264
25254/*
25255 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25256 */
25257static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25258static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25259static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25260
25261/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025262 * Get the short path (8.3) for the filename in "fnamep".
25263 * Only works for a valid file name.
25264 * When the path gets longer "fnamep" is changed and the allocated buffer
25265 * is put in "bufp".
25266 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25267 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025268 */
25269 static int
25270get_short_pathname(fnamep, bufp, fnamelen)
25271 char_u **fnamep;
25272 char_u **bufp;
25273 int *fnamelen;
25274{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025275 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025276 char_u *newbuf;
25277
25278 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279 l = GetShortPathName(*fnamep, *fnamep, len);
25280 if (l > len - 1)
25281 {
25282 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025283 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025284 newbuf = vim_strnsave(*fnamep, l);
25285 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025286 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025287
25288 vim_free(*bufp);
25289 *fnamep = *bufp = newbuf;
25290
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025291 /* Really should always succeed, as the buffer is big enough. */
25292 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293 }
25294
25295 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025296 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025297}
25298
25299/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025300 * Get the short path (8.3) for the filename in "fname". The converted
25301 * path is returned in "bufp".
25302 *
25303 * Some of the directories specified in "fname" may not exist. This function
25304 * will shorten the existing directories at the beginning of the path and then
25305 * append the remaining non-existing path.
25306 *
25307 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025308 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025309 * bufp - Pointer to an allocated buffer for the filename.
25310 * fnamelen - Length of the filename pointed to by fname
25311 *
25312 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025313 */
25314 static int
25315shortpath_for_invalid_fname(fname, bufp, fnamelen)
25316 char_u **fname;
25317 char_u **bufp;
25318 int *fnamelen;
25319{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025320 char_u *short_fname, *save_fname, *pbuf_unused;
25321 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025322 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025323 int old_len, len;
25324 int new_len, sfx_len;
25325 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025326
25327 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025328 old_len = *fnamelen;
25329 save_fname = vim_strnsave(*fname, old_len);
25330 pbuf_unused = NULL;
25331 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025332
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025333 endp = save_fname + old_len - 1; /* Find the end of the copy */
25334 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025336 /*
25337 * Try shortening the supplied path till it succeeds by removing one
25338 * directory at a time from the tail of the path.
25339 */
25340 len = 0;
25341 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025342 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025343 /* go back one path-separator */
25344 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25345 --endp;
25346 if (endp <= save_fname)
25347 break; /* processed the complete path */
25348
25349 /*
25350 * Replace the path separator with a NUL and try to shorten the
25351 * resulting path.
25352 */
25353 ch = *endp;
25354 *endp = 0;
25355 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025356 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025357 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25358 {
25359 retval = FAIL;
25360 goto theend;
25361 }
25362 *endp = ch; /* preserve the string */
25363
25364 if (len > 0)
25365 break; /* successfully shortened the path */
25366
25367 /* failed to shorten the path. Skip the path separator */
25368 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369 }
25370
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025371 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025373 /*
25374 * Succeeded in shortening the path. Now concatenate the shortened
25375 * path with the remaining path at the tail.
25376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025377
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025378 /* Compute the length of the new path. */
25379 sfx_len = (int)(save_endp - endp) + 1;
25380 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025381
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025382 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025384 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025385 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025386 /* There is not enough space in the currently allocated string,
25387 * copy it to a buffer big enough. */
25388 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025389 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025390 {
25391 retval = FAIL;
25392 goto theend;
25393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025394 }
25395 else
25396 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025397 /* Transfer short_fname to the main buffer (it's big enough),
25398 * unless get_short_pathname() did its work in-place. */
25399 *fname = *bufp = save_fname;
25400 if (short_fname != save_fname)
25401 vim_strncpy(save_fname, short_fname, len);
25402 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025403 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025404
25405 /* concat the not-shortened part of the path */
25406 vim_strncpy(*fname + len, endp, sfx_len);
25407 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025408 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025409
25410theend:
25411 vim_free(pbuf_unused);
25412 vim_free(save_fname);
25413
25414 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025415}
25416
25417/*
25418 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025419 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025420 */
25421 static int
25422shortpath_for_partial(fnamep, bufp, fnamelen)
25423 char_u **fnamep;
25424 char_u **bufp;
25425 int *fnamelen;
25426{
25427 int sepcount, len, tflen;
25428 char_u *p;
25429 char_u *pbuf, *tfname;
25430 int hasTilde;
25431
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025432 /* Count up the path separators from the RHS.. so we know which part
25433 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025434 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025435 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025436 if (vim_ispathsep(*p))
25437 ++sepcount;
25438
25439 /* Need full path first (use expand_env() to remove a "~/") */
25440 hasTilde = (**fnamep == '~');
25441 if (hasTilde)
25442 pbuf = tfname = expand_env_save(*fnamep);
25443 else
25444 pbuf = tfname = FullName_save(*fnamep, FALSE);
25445
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025446 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025448 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25449 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025450
25451 if (len == 0)
25452 {
25453 /* Don't have a valid filename, so shorten the rest of the
25454 * path if we can. This CAN give us invalid 8.3 filenames, but
25455 * there's not a lot of point in guessing what it might be.
25456 */
25457 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025458 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25459 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025460 }
25461
25462 /* Count the paths backward to find the beginning of the desired string. */
25463 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025464 {
25465#ifdef FEAT_MBYTE
25466 if (has_mbyte)
25467 p -= mb_head_off(tfname, p);
25468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025469 if (vim_ispathsep(*p))
25470 {
25471 if (sepcount == 0 || (hasTilde && sepcount == 1))
25472 break;
25473 else
25474 sepcount --;
25475 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025477 if (hasTilde)
25478 {
25479 --p;
25480 if (p >= tfname)
25481 *p = '~';
25482 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025483 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484 }
25485 else
25486 ++p;
25487
25488 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25489 vim_free(*bufp);
25490 *fnamelen = (int)STRLEN(p);
25491 *bufp = pbuf;
25492 *fnamep = p;
25493
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025494 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025495}
25496#endif /* WIN3264 */
25497
25498/*
25499 * Adjust a filename, according to a string of modifiers.
25500 * *fnamep must be NUL terminated when called. When returning, the length is
25501 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025502 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025503 * When there is an error, *fnamep is set to NULL.
25504 */
25505 int
25506modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25507 char_u *src; /* string with modifiers */
25508 int *usedlen; /* characters after src that are used */
25509 char_u **fnamep; /* file name so far */
25510 char_u **bufp; /* buffer for allocated file name or NULL */
25511 int *fnamelen; /* length of fnamep */
25512{
25513 int valid = 0;
25514 char_u *tail;
25515 char_u *s, *p, *pbuf;
25516 char_u dirname[MAXPATHL];
25517 int c;
25518 int has_fullname = 0;
25519#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025520 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025521 int has_shortname = 0;
25522#endif
25523
25524repeat:
25525 /* ":p" - full path/file_name */
25526 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25527 {
25528 has_fullname = 1;
25529
25530 valid |= VALID_PATH;
25531 *usedlen += 2;
25532
25533 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25534 if ((*fnamep)[0] == '~'
25535#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25536 && ((*fnamep)[1] == '/'
25537# ifdef BACKSLASH_IN_FILENAME
25538 || (*fnamep)[1] == '\\'
25539# endif
25540 || (*fnamep)[1] == NUL)
25541
25542#endif
25543 )
25544 {
25545 *fnamep = expand_env_save(*fnamep);
25546 vim_free(*bufp); /* free any allocated file name */
25547 *bufp = *fnamep;
25548 if (*fnamep == NULL)
25549 return -1;
25550 }
25551
25552 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025553 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025554 {
25555 if (vim_ispathsep(*p)
25556 && p[1] == '.'
25557 && (p[2] == NUL
25558 || vim_ispathsep(p[2])
25559 || (p[2] == '.'
25560 && (p[3] == NUL || vim_ispathsep(p[3])))))
25561 break;
25562 }
25563
25564 /* FullName_save() is slow, don't use it when not needed. */
25565 if (*p != NUL || !vim_isAbsName(*fnamep))
25566 {
25567 *fnamep = FullName_save(*fnamep, *p != NUL);
25568 vim_free(*bufp); /* free any allocated file name */
25569 *bufp = *fnamep;
25570 if (*fnamep == NULL)
25571 return -1;
25572 }
25573
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025574#ifdef WIN3264
25575# if _WIN32_WINNT >= 0x0500
25576 if (vim_strchr(*fnamep, '~') != NULL)
25577 {
25578 /* Expand 8.3 filename to full path. Needed to make sure the same
25579 * file does not have two different names.
25580 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25581 p = alloc(_MAX_PATH + 1);
25582 if (p != NULL)
25583 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025584 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025585 {
25586 vim_free(*bufp);
25587 *bufp = *fnamep = p;
25588 }
25589 else
25590 vim_free(p);
25591 }
25592 }
25593# endif
25594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025595 /* Append a path separator to a directory. */
25596 if (mch_isdir(*fnamep))
25597 {
25598 /* Make room for one or two extra characters. */
25599 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25600 vim_free(*bufp); /* free any allocated file name */
25601 *bufp = *fnamep;
25602 if (*fnamep == NULL)
25603 return -1;
25604 add_pathsep(*fnamep);
25605 }
25606 }
25607
25608 /* ":." - path relative to the current directory */
25609 /* ":~" - path relative to the home directory */
25610 /* ":8" - shortname path - postponed till after */
25611 while (src[*usedlen] == ':'
25612 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25613 {
25614 *usedlen += 2;
25615 if (c == '8')
25616 {
25617#ifdef WIN3264
25618 has_shortname = 1; /* Postpone this. */
25619#endif
25620 continue;
25621 }
25622 pbuf = NULL;
25623 /* Need full path first (use expand_env() to remove a "~/") */
25624 if (!has_fullname)
25625 {
25626 if (c == '.' && **fnamep == '~')
25627 p = pbuf = expand_env_save(*fnamep);
25628 else
25629 p = pbuf = FullName_save(*fnamep, FALSE);
25630 }
25631 else
25632 p = *fnamep;
25633
25634 has_fullname = 0;
25635
25636 if (p != NULL)
25637 {
25638 if (c == '.')
25639 {
25640 mch_dirname(dirname, MAXPATHL);
25641 s = shorten_fname(p, dirname);
25642 if (s != NULL)
25643 {
25644 *fnamep = s;
25645 if (pbuf != NULL)
25646 {
25647 vim_free(*bufp); /* free any allocated file name */
25648 *bufp = pbuf;
25649 pbuf = NULL;
25650 }
25651 }
25652 }
25653 else
25654 {
25655 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25656 /* Only replace it when it starts with '~' */
25657 if (*dirname == '~')
25658 {
25659 s = vim_strsave(dirname);
25660 if (s != NULL)
25661 {
25662 *fnamep = s;
25663 vim_free(*bufp);
25664 *bufp = s;
25665 }
25666 }
25667 }
25668 vim_free(pbuf);
25669 }
25670 }
25671
25672 tail = gettail(*fnamep);
25673 *fnamelen = (int)STRLEN(*fnamep);
25674
25675 /* ":h" - head, remove "/file_name", can be repeated */
25676 /* Don't remove the first "/" or "c:\" */
25677 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25678 {
25679 valid |= VALID_HEAD;
25680 *usedlen += 2;
25681 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025682 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025683 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025684 *fnamelen = (int)(tail - *fnamep);
25685#ifdef VMS
25686 if (*fnamelen > 0)
25687 *fnamelen += 1; /* the path separator is part of the path */
25688#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025689 if (*fnamelen == 0)
25690 {
25691 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25692 p = vim_strsave((char_u *)".");
25693 if (p == NULL)
25694 return -1;
25695 vim_free(*bufp);
25696 *bufp = *fnamep = tail = p;
25697 *fnamelen = 1;
25698 }
25699 else
25700 {
25701 while (tail > s && !after_pathsep(s, tail))
25702 mb_ptr_back(*fnamep, tail);
25703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025704 }
25705
25706 /* ":8" - shortname */
25707 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25708 {
25709 *usedlen += 2;
25710#ifdef WIN3264
25711 has_shortname = 1;
25712#endif
25713 }
25714
25715#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025716 /*
25717 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025718 */
25719 if (has_shortname)
25720 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025721 /* Copy the string if it is shortened by :h and when it wasn't copied
25722 * yet, because we are going to change it in place. Avoids changing
25723 * the buffer name for "%:8". */
25724 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025725 {
25726 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025727 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025728 return -1;
25729 vim_free(*bufp);
25730 *bufp = *fnamep = p;
25731 }
25732
25733 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025734 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025735 if (!has_fullname && !vim_isAbsName(*fnamep))
25736 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025737 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738 return -1;
25739 }
25740 else
25741 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025742 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025743
Bram Moolenaardc935552011-08-17 15:23:23 +020025744 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025745 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025746 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025747 return -1;
25748
25749 if (l == 0)
25750 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025751 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025752 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025753 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025754 return -1;
25755 }
25756 *fnamelen = l;
25757 }
25758 }
25759#endif /* WIN3264 */
25760
25761 /* ":t" - tail, just the basename */
25762 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25763 {
25764 *usedlen += 2;
25765 *fnamelen -= (int)(tail - *fnamep);
25766 *fnamep = tail;
25767 }
25768
25769 /* ":e" - extension, can be repeated */
25770 /* ":r" - root, without extension, can be repeated */
25771 while (src[*usedlen] == ':'
25772 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25773 {
25774 /* find a '.' in the tail:
25775 * - for second :e: before the current fname
25776 * - otherwise: The last '.'
25777 */
25778 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25779 s = *fnamep - 2;
25780 else
25781 s = *fnamep + *fnamelen - 1;
25782 for ( ; s > tail; --s)
25783 if (s[0] == '.')
25784 break;
25785 if (src[*usedlen + 1] == 'e') /* :e */
25786 {
25787 if (s > tail)
25788 {
25789 *fnamelen += (int)(*fnamep - (s + 1));
25790 *fnamep = s + 1;
25791#ifdef VMS
25792 /* cut version from the extension */
25793 s = *fnamep + *fnamelen - 1;
25794 for ( ; s > *fnamep; --s)
25795 if (s[0] == ';')
25796 break;
25797 if (s > *fnamep)
25798 *fnamelen = s - *fnamep;
25799#endif
25800 }
25801 else if (*fnamep <= tail)
25802 *fnamelen = 0;
25803 }
25804 else /* :r */
25805 {
25806 if (s > tail) /* remove one extension */
25807 *fnamelen = (int)(s - *fnamep);
25808 }
25809 *usedlen += 2;
25810 }
25811
25812 /* ":s?pat?foo?" - substitute */
25813 /* ":gs?pat?foo?" - global substitute */
25814 if (src[*usedlen] == ':'
25815 && (src[*usedlen + 1] == 's'
25816 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25817 {
25818 char_u *str;
25819 char_u *pat;
25820 char_u *sub;
25821 int sep;
25822 char_u *flags;
25823 int didit = FALSE;
25824
25825 flags = (char_u *)"";
25826 s = src + *usedlen + 2;
25827 if (src[*usedlen + 1] == 'g')
25828 {
25829 flags = (char_u *)"g";
25830 ++s;
25831 }
25832
25833 sep = *s++;
25834 if (sep)
25835 {
25836 /* find end of pattern */
25837 p = vim_strchr(s, sep);
25838 if (p != NULL)
25839 {
25840 pat = vim_strnsave(s, (int)(p - s));
25841 if (pat != NULL)
25842 {
25843 s = p + 1;
25844 /* find end of substitution */
25845 p = vim_strchr(s, sep);
25846 if (p != NULL)
25847 {
25848 sub = vim_strnsave(s, (int)(p - s));
25849 str = vim_strnsave(*fnamep, *fnamelen);
25850 if (sub != NULL && str != NULL)
25851 {
25852 *usedlen = (int)(p + 1 - src);
25853 s = do_string_sub(str, pat, sub, flags);
25854 if (s != NULL)
25855 {
25856 *fnamep = s;
25857 *fnamelen = (int)STRLEN(s);
25858 vim_free(*bufp);
25859 *bufp = s;
25860 didit = TRUE;
25861 }
25862 }
25863 vim_free(sub);
25864 vim_free(str);
25865 }
25866 vim_free(pat);
25867 }
25868 }
25869 /* after using ":s", repeat all the modifiers */
25870 if (didit)
25871 goto repeat;
25872 }
25873 }
25874
Bram Moolenaar26df0922014-02-23 23:39:13 +010025875 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25876 {
25877 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25878 if (p == NULL)
25879 return -1;
25880 vim_free(*bufp);
25881 *bufp = *fnamep = p;
25882 *fnamelen = (int)STRLEN(p);
25883 *usedlen += 2;
25884 }
25885
Bram Moolenaar071d4272004-06-13 20:20:40 +000025886 return valid;
25887}
25888
25889/*
25890 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25891 * "flags" can be "g" to do a global substitute.
25892 * Returns an allocated string, NULL for error.
25893 */
25894 char_u *
25895do_string_sub(str, pat, sub, flags)
25896 char_u *str;
25897 char_u *pat;
25898 char_u *sub;
25899 char_u *flags;
25900{
25901 int sublen;
25902 regmatch_T regmatch;
25903 int i;
25904 int do_all;
25905 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025906 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025907 garray_T ga;
25908 char_u *ret;
25909 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025910 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025911
25912 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25913 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025914 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025915
25916 ga_init2(&ga, 1, 200);
25917
25918 do_all = (flags[0] == 'g');
25919
25920 regmatch.rm_ic = p_ic;
25921 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25922 if (regmatch.regprog != NULL)
25923 {
25924 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025925 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025926 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25927 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025928 /* Skip empty match except for first match. */
25929 if (regmatch.startp[0] == regmatch.endp[0])
25930 {
25931 if (zero_width == regmatch.startp[0])
25932 {
25933 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025934 i = MB_PTR2LEN(tail);
25935 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25936 (size_t)i);
25937 ga.ga_len += i;
25938 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025939 continue;
25940 }
25941 zero_width = regmatch.startp[0];
25942 }
25943
Bram Moolenaar071d4272004-06-13 20:20:40 +000025944 /*
25945 * Get some space for a temporary buffer to do the substitution
25946 * into. It will contain:
25947 * - The text up to where the match is.
25948 * - The substituted text.
25949 * - The text after the match.
25950 */
25951 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025952 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025953 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25954 {
25955 ga_clear(&ga);
25956 break;
25957 }
25958
25959 /* copy the text up to where the match is */
25960 i = (int)(regmatch.startp[0] - tail);
25961 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25962 /* add the substituted text */
25963 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25964 + ga.ga_len + i, TRUE, TRUE, FALSE);
25965 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025966 tail = regmatch.endp[0];
25967 if (*tail == NUL)
25968 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025969 if (!do_all)
25970 break;
25971 }
25972
25973 if (ga.ga_data != NULL)
25974 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25975
Bram Moolenaar473de612013-06-08 18:19:48 +020025976 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025977 }
25978
25979 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25980 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025981 if (p_cpo == empty_option)
25982 p_cpo = save_cpo;
25983 else
25984 /* Darn, evaluating {sub} expression changed the value. */
25985 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025986
25987 return ret;
25988}
25989
25990#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */