blob: b42ecc78f1f5ab55e6d4ed65fa94023f868b5024 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200114#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000117
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100118#define NAMESPACE_CHAR (char_u *)"abglstvw"
119
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200120static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000121#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124 * Old Vim variables such as "v:version" are also available without the "v:".
125 * Also in functions. We need a special hashtable for them.
126 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000127static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128
129/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000130 * When recursively copying lists and dicts we need to remember which ones we
131 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000132 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133 */
134static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000135#define COPYID_INC 2
136#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137
Bram Moolenaar8502c702014-06-17 12:51:16 +0200138/* Abort conversion to string after a recursion error. */
139static int did_echo_string_emsg = FALSE;
140
Bram Moolenaard9fba312005-06-26 22:34:35 +0000141/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000142 * Array to hold the hashtab with variables local to each sourced script.
143 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000145typedef struct
146{
147 dictitem_T sv_var;
148 dict_T sv_dict;
149} scriptvar_T;
150
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200151static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
152#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
153#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
155static int echo_attr = 0; /* attributes used for ":echo" */
156
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000157/* Values for trans_function_name() argument: */
158#define TFN_INT 1 /* internal function name OK */
159#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100160#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
161
162/* Values for get_lval() flags argument: */
163#define GLV_QUIET TFN_QUIET /* no error messages */
164#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166/*
167 * Structure to hold info for a user function.
168 */
169typedef struct ufunc ufunc_T;
170
171struct ufunc
172{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000173 int uf_varargs; /* variable nr of arguments */
174 int uf_flags;
175 int uf_calls; /* nr of active calls */
176 garray_T uf_args; /* arguments */
177 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000178#ifdef FEAT_PROFILE
179 int uf_profiling; /* TRUE when func is being profiled */
180 /* profiling the function as a whole */
181 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000182 proftime_T uf_tm_total; /* time spent in function + children */
183 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000184 proftime_T uf_tm_children; /* time spent in children this call */
185 /* profiling the function per line */
186 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000187 proftime_T *uf_tml_total; /* time spent in a line + children */
188 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000189 proftime_T uf_tml_start; /* start time for current line */
190 proftime_T uf_tml_children; /* time spent in children for this line */
191 proftime_T uf_tml_wait; /* start wait time for current line */
192 int uf_tml_idx; /* index of line being timed; -1 if none */
193 int uf_tml_execed; /* line being timed was executed */
194#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000195 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000197 int uf_refcount; /* for numbered function: reference count */
198 char_u uf_name[1]; /* name of function (actually longer); can
199 start with <SNR>123_ (<SNR> is K_SPECIAL
200 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201};
202
203/* function flags */
204#define FC_ABORT 1 /* abort function on error */
205#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000206#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207
208/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000209 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000211static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000213/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000214static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
215
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000216/* list heads for garbage collection */
217static dict_T *first_dict = NULL; /* list of all dicts */
218static list_T *first_list = NULL; /* list of all lists */
219
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000220/* From user function to hashitem and back. */
221static ufunc_T dumuf;
222#define UF2HIKEY(fp) ((fp)->uf_name)
223#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
224#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
225
226#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
227#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
Bram Moolenaar33570922005-01-25 22:26:29 +0000229#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
230#define VAR_SHORT_LEN 20 /* short variable name length */
231#define FIXVAR_CNT 12 /* number of fixed variables */
232
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000234typedef struct funccall_S funccall_T;
235
236struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237{
238 ufunc_T *func; /* function being called */
239 int linenr; /* next line to be executed */
240 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000241 struct /* fixed variables for arguments */
242 {
243 dictitem_T var; /* variable (without room for name) */
244 char_u room[VAR_SHORT_LEN]; /* room for the name */
245 } fixvar[FIXVAR_CNT];
246 dict_T l_vars; /* l: local function variables */
247 dictitem_T l_vars_var; /* variable for l: scope */
248 dict_T l_avars; /* a: argument variables */
249 dictitem_T l_avars_var; /* variable for a: scope */
250 list_T l_varlist; /* list for a:000 */
251 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
252 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 linenr_T breakpoint; /* next line with breakpoint or zero */
254 int dbg_tick; /* debug_tick when breakpoint was set */
255 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000256#ifdef FEAT_PROFILE
257 proftime_T prof_child; /* time spent in a child */
258#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000259 funccall_T *caller; /* calling function or NULL */
260};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
262/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000263 * Info used by a ":for" loop.
264 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000266{
267 int fi_semicolon; /* TRUE if ending in '; var]' */
268 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000269 listwatch_T fi_lw; /* keep an eye on the item used. */
270 list_T *fi_list; /* list being used */
271} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000274 * Struct used by trans_function_name()
275 */
276typedef struct
277{
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000279 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000280 dictitem_T *fd_di; /* Dictionary item used */
281} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000282
Bram Moolenaara7043832005-01-21 11:56:39 +0000283
284/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000285 * Array to hold the value of v: variables.
286 * The value is in a dictitem, so that it can also be used in the v: scope.
287 * The reason to use this table anyway is for very quick access to the
288 * variables with the VV_ defines.
289 */
290#include "version.h"
291
292/* values for vv_flags: */
293#define VV_COMPAT 1 /* compatible, also used without "v:" */
294#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000295#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000297#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000298
299static struct vimvar
300{
301 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000302 dictitem_T vv_di; /* value and name for key */
303 char vv_filler[16]; /* space for LONGEST name below!!! */
304 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
305} vimvars[VV_LEN] =
306{
307 /*
308 * The order here must match the VV_ defines in vim.h!
309 * Initializing a union does not work, leave tv.vval empty to get zero's.
310 */
311 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("count1", VAR_NUMBER), VV_RO},
313 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
314 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
315 {VV_NAME("warningmsg", VAR_STRING), 0},
316 {VV_NAME("statusmsg", VAR_STRING), 0},
317 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
319 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
320 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("termresponse", VAR_STRING), VV_RO},
322 {VV_NAME("fname", VAR_STRING), VV_RO},
323 {VV_NAME("lang", VAR_STRING), VV_RO},
324 {VV_NAME("lc_time", VAR_STRING), VV_RO},
325 {VV_NAME("ctype", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
327 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
328 {VV_NAME("fname_in", VAR_STRING), VV_RO},
329 {VV_NAME("fname_out", VAR_STRING), VV_RO},
330 {VV_NAME("fname_new", VAR_STRING), VV_RO},
331 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
332 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
333 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
336 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
337 {VV_NAME("progname", VAR_STRING), VV_RO},
338 {VV_NAME("servername", VAR_STRING), VV_RO},
339 {VV_NAME("dying", VAR_NUMBER), VV_RO},
340 {VV_NAME("exception", VAR_STRING), VV_RO},
341 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
342 {VV_NAME("register", VAR_STRING), VV_RO},
343 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
344 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
346 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000347 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000348 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
349 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000350 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000355 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000356 {VV_NAME("swapname", VAR_STRING), VV_RO},
357 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000358 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200359 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000360 {VV_NAME("mouse_win", VAR_NUMBER), 0},
361 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
362 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000363 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100365 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000366 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200367 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200368 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200369 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200370 {VV_NAME("option_new", VAR_STRING), VV_RO},
371 {VV_NAME("option_old", VAR_STRING), VV_RO},
372 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100373 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000374};
375
376/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000377#define vv_type vv_di.di_tv.v_type
378#define vv_nr vv_di.di_tv.vval.v_number
379#define vv_float vv_di.di_tv.vval.v_float
380#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000381#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200382#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000384
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200385static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000386#define vimvarht vimvardict.dv_hashtab
387
Bram Moolenaara40058a2005-07-11 22:42:07 +0000388static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
389static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000390static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
391static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
392static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
394static void list_glob_vars __ARGS((int *first));
395static void list_buf_vars __ARGS((int *first));
396static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000398static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000399#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000400static void list_vim_vars __ARGS((int *first));
401static void list_script_vars __ARGS((int *first));
402static void list_func_vars __ARGS((int *first));
403static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000404static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
405static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100406static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000407static void clear_lval __ARGS((lval_T *lp));
408static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
409static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000410static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
411static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
412static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
413static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
414static void item_lock __ARGS((typval_T *tv, int deep, int lock));
415static int tv_islocked __ARGS((typval_T *tv));
416
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
418static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000423static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
424static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000425
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000426static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
429static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
430static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000431static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100433static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
434static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
435static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000436static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000438static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
440static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000441static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100443static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000444static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000445static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200446static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
448static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000449static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000451static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000453static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
454static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#ifdef FEAT_FLOAT
457static int string2float __ARGS((char_u *text, float_T *value));
458#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
460static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100461static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000462static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200463static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000464static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000465static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
468static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200469static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100472static void f_alloc_fail __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100473static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000474static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200477static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000478static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100479static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara803c7f2016-01-15 15:31:39 +0100480static void f_assert_exception __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara260b872016-01-15 20:48:22 +0100481static void f_assert_fails __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100482static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200485static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000486static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200487static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100498static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000499static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100500static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#ifdef FEAT_FLOAT
503static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
504#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000505static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000508static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000510#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000511static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000512static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#ifdef FEAT_FLOAT
518static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200519static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000520#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
524static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200534static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200536#ifdef FEAT_FLOAT
537static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
538#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000541static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000547#ifdef FEAT_FLOAT
548static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200550static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000551#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000552static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000561static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000563static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200567static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000570static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200571static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000579static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000580static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200581static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000582static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000583static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200586static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000587static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100593static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000596static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000610static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100615static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000617static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000629#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200630static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000631static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
632#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200633#ifdef FEAT_LUA
634static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
635#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000640static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200641static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000642static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000643static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000645static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000646static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000649#ifdef vim_mkdir
650static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
651#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100653#ifdef FEAT_MZSCHEME
654static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
655#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100658static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000659static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100660#ifdef FEAT_PERL
661static void f_perleval __ARGS((typval_T *argvars, typval_T *rettv));
662#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000663#ifdef FEAT_FLOAT
664static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
665#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000667static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000668static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200669#ifdef FEAT_PYTHON3
670static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
671#endif
672#ifdef FEAT_PYTHON
673static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
674#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000676static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000677static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#ifdef FEAT_FLOAT
690static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
691#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200692static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100694static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000697static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000699static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
703static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200704static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000707static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000708static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000709static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000710static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000711static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200712static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000713static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100715#ifdef FEAT_CRYPT
716static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
717#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000718static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200719static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000721#ifdef FEAT_FLOAT
722static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200723static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000724#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000725static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000726static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000727static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000729static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
731static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
733#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000734static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200735static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000736#ifdef HAVE_STRFTIME
737static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
739static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200745static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200746static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000752static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200753static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200755static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000756static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000757static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000758static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000759static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000760static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000762static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200763#ifdef FEAT_FLOAT
764static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
766#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
768static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
769static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000770#ifdef FEAT_FLOAT
771static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
772#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200774static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200775static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100776static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
778static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
779static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100780static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
782static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
783static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
784static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
785static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
786static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000787static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
788static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000789static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000790static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaared767a22016-01-03 22:49:16 +0100791static void f_wordcount __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100792static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000793
Bram Moolenaar493c1782014-05-28 14:34:46 +0200794static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000795static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796static int get_env_len __ARGS((char_u **arg));
797static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000798static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000799static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
800#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
801#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
802 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000803static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000805static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200806static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000807static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000808static typval_T *alloc_tv __ARGS((void));
809static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000810static void init_tv __ARGS((typval_T *varp));
811static long get_tv_number __ARGS((typval_T *varp));
812static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000813static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000814static char_u *get_tv_string __ARGS((typval_T *varp));
815static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000816static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100817static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
818static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000819static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
Bram Moolenaarf1f60f82016-01-16 15:40:53 +0100820static funccall_T *get_funccal __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000821static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
822static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000823static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
824static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000825static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200826static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
827static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200828static int var_check_func_name __ARGS((char_u *name, int new_var));
829static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200830static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000831static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000832static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
833static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
834static int eval_fname_script __ARGS((char_u *p));
835static int eval_fname_sid __ARGS((char_u *p));
836static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000837static ufunc_T *find_func __ARGS((char_u *name));
838static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200839static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000840#ifdef FEAT_PROFILE
841static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000842static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
843static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
844static int
845# ifdef __BORLANDC__
846 _RTLENTRYF
847# endif
848 prof_total_cmp __ARGS((const void *s1, const void *s2));
849static int
850# ifdef __BORLANDC__
851 _RTLENTRYF
852# endif
853 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200855static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000856static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000857static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000858static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000859static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000860static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
861static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000862static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000863static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
Bram Moolenaarc9703302016-01-17 21:49:33 +0100864static win_T *find_tabwin __ARGS((typval_T *wvp, typval_T *tvp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000865static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000866static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000867static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000868static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200869static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200870static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000871
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200872
873#ifdef EBCDIC
874static int compare_func_name __ARGS((const void *s1, const void *s2));
875static void sortFunctions __ARGS(());
876#endif
877
Bram Moolenaar33570922005-01-25 22:26:29 +0000878/*
879 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000880 */
881 void
882eval_init()
883{
Bram Moolenaar33570922005-01-25 22:26:29 +0000884 int i;
885 struct vimvar *p;
886
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200887 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
888 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200889 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000890 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000891 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000892
893 for (i = 0; i < VV_LEN; ++i)
894 {
895 p = &vimvars[i];
896 STRCPY(p->vv_di.di_key, p->vv_name);
897 if (p->vv_flags & VV_RO)
898 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
899 else if (p->vv_flags & VV_RO_SBX)
900 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
901 else
902 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000903
904 /* add to v: scope dict, unless the value is not always available */
905 if (p->vv_type != VAR_UNKNOWN)
906 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000907 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000908 /* add to compat scope dict */
909 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000910 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000911 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100912 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200913 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100914 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200915 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200916
917#ifdef EBCDIC
918 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100919 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200920 */
921 sortFunctions();
922#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000923}
924
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000925#if defined(EXITFREE) || defined(PROTO)
926 void
927eval_clear()
928{
929 int i;
930 struct vimvar *p;
931
932 for (i = 0; i < VV_LEN; ++i)
933 {
934 p = &vimvars[i];
935 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000936 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000937 vim_free(p->vv_str);
938 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000939 }
940 else if (p->vv_di.di_tv.v_type == VAR_LIST)
941 {
942 list_unref(p->vv_list);
943 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000944 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000945 }
946 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000947 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000948 hash_clear(&compat_hashtab);
949
Bram Moolenaard9fba312005-06-26 22:34:35 +0000950 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100951# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200952 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100953# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000954
955 /* global variables */
956 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000957
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000958 /* autoloaded script names */
959 ga_clear_strings(&ga_loaded);
960
Bram Moolenaarcca74132013-09-25 21:00:28 +0200961 /* Script-local variables. First clear all the variables and in a second
962 * loop free the scriptvar_T, because a variable in one script might hold
963 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200964 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200965 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200966 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200967 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200968 ga_clear(&ga_scripts);
969
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000970 /* unreferenced lists and dicts */
971 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000972
973 /* functions */
974 free_all_functions();
975 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000976}
977#endif
978
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 * Return the name of the executed function.
981 */
982 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100983func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000985 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988/*
989 * Return the address holding the next breakpoint line for a funccall cookie.
990 */
991 linenr_T *
992func_breakpoint(cookie)
993 void *cookie;
994{
Bram Moolenaar33570922005-01-25 22:26:29 +0000995 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996}
997
998/*
999 * Return the address holding the debug tick for a funccall cookie.
1000 */
1001 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001002func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
Bram Moolenaar33570922005-01-25 22:26:29 +00001004 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005}
1006
1007/*
1008 * Return the nesting level for a funccall cookie.
1009 */
1010 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001011func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012{
Bram Moolenaar33570922005-01-25 22:26:29 +00001013 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014}
1015
1016/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001017funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001019/* pointer to list of previously used funccal, still around because some
1020 * item in it is still being used. */
1021funccall_T *previous_funccal = NULL;
1022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023/*
1024 * Return TRUE when a function was ended by a ":return" command.
1025 */
1026 int
1027current_func_returned()
1028{
1029 return current_funccal->returned;
1030}
1031
1032
1033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 * Set an internal variable to a string value. Creates the variable if it does
1035 * not already exist.
1036 */
1037 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001038set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001040 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001041 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
1043 val = vim_strsave(value);
1044 if (val != NULL)
1045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001046 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001047 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001049 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001050 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 }
1052 }
1053}
1054
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001056static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057static char_u *redir_endp = NULL;
1058static char_u *redir_varname = NULL;
1059
1060/*
1061 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001062 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 * Returns OK if successfully completed the setup. FAIL otherwise.
1064 */
1065 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001066var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001067{
1068 int save_emsg;
1069 int err;
1070 typval_T tv;
1071
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001072 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001073 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001074 {
1075 EMSG(_(e_invarg));
1076 return FAIL;
1077 }
1078
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001079 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080 redir_varname = vim_strsave(name);
1081 if (redir_varname == NULL)
1082 return FAIL;
1083
1084 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1085 if (redir_lval == NULL)
1086 {
1087 var_redir_stop();
1088 return FAIL;
1089 }
1090
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001091 /* The output is stored in growarray "redir_ga" until redirection ends. */
1092 ga_init2(&redir_ga, (int)sizeof(char), 500);
1093
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001095 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001096 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1098 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001099 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100 if (redir_endp != NULL && *redir_endp != NUL)
1101 /* Trailing characters are present after the variable name */
1102 EMSG(_(e_trailing));
1103 else
1104 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001105 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 var_redir_stop();
1107 return FAIL;
1108 }
1109
1110 /* check if we can write to the variable: set it to or append an empty
1111 * string */
1112 save_emsg = did_emsg;
1113 did_emsg = FALSE;
1114 tv.v_type = VAR_STRING;
1115 tv.vval.v_string = (char_u *)"";
1116 if (append)
1117 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1118 else
1119 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001120 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001122 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 if (err)
1124 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001125 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 var_redir_stop();
1127 return FAIL;
1128 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129
1130 return OK;
1131}
1132
1133/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 * Append "value[value_len]" to the variable set by var_redir_start().
1135 * The actual appending is postponed until redirection ends, because the value
1136 * appended may in fact be the string we write to, changing it may cause freed
1137 * memory to be used:
1138 * :redir => foo
1139 * :let foo
1140 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 */
1142 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001143var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001145 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146
1147 if (redir_lval == NULL)
1148 return;
1149
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001150 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001151 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001153 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001155 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001156 {
1157 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001158 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001159 }
1160 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162}
1163
1164/*
1165 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001166 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 */
1168 void
1169var_redir_stop()
1170{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001171 typval_T tv;
1172
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173 if (redir_lval != NULL)
1174 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001175 /* If there was no error: assign the text to the variable. */
1176 if (redir_endp != NULL)
1177 {
1178 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1179 tv.v_type = VAR_STRING;
1180 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001181 /* Call get_lval() again, if it's inside a Dict or List it may
1182 * have changed. */
1183 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001184 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001185 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1186 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1187 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001188 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001190 /* free the collected output */
1191 vim_free(redir_ga.ga_data);
1192 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001193
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194 vim_free(redir_lval);
1195 redir_lval = NULL;
1196 }
1197 vim_free(redir_varname);
1198 redir_varname = NULL;
1199}
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201# if defined(FEAT_MBYTE) || defined(PROTO)
1202 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001203eval_charconvert(
1204 char_u *enc_from,
1205 char_u *enc_to,
1206 char_u *fname_from,
1207 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208{
1209 int err = FALSE;
1210
1211 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1212 set_vim_var_string(VV_CC_TO, enc_to, -1);
1213 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1214 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1215 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1216 err = TRUE;
1217 set_vim_var_string(VV_CC_FROM, NULL, -1);
1218 set_vim_var_string(VV_CC_TO, NULL, -1);
1219 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1220 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1221
1222 if (err)
1223 return FAIL;
1224 return OK;
1225}
1226# endif
1227
1228# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1229 int
1230eval_printexpr(fname, args)
1231 char_u *fname;
1232 char_u *args;
1233{
1234 int err = FALSE;
1235
1236 set_vim_var_string(VV_FNAME_IN, fname, -1);
1237 set_vim_var_string(VV_CMDARG, args, -1);
1238 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1239 err = TRUE;
1240 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1241 set_vim_var_string(VV_CMDARG, NULL, -1);
1242
1243 if (err)
1244 {
1245 mch_remove(fname);
1246 return FAIL;
1247 }
1248 return OK;
1249}
1250# endif
1251
1252# if defined(FEAT_DIFF) || defined(PROTO)
1253 void
1254eval_diff(origfile, newfile, outfile)
1255 char_u *origfile;
1256 char_u *newfile;
1257 char_u *outfile;
1258{
1259 int err = FALSE;
1260
1261 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1262 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1263 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1264 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1265 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1266 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1267 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1268}
1269
1270 void
1271eval_patch(origfile, difffile, outfile)
1272 char_u *origfile;
1273 char_u *difffile;
1274 char_u *outfile;
1275{
1276 int err;
1277
1278 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1279 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1280 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1281 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1282 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1283 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1284 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1285}
1286# endif
1287
1288/*
1289 * Top level evaluation function, returning a boolean.
1290 * Sets "error" to TRUE if there was an error.
1291 * Return TRUE or FALSE.
1292 */
1293 int
1294eval_to_bool(arg, error, nextcmd, skip)
1295 char_u *arg;
1296 int *error;
1297 char_u **nextcmd;
1298 int skip; /* only parse, don't execute */
1299{
Bram Moolenaar33570922005-01-25 22:26:29 +00001300 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 int retval = FALSE;
1302
1303 if (skip)
1304 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 else
1308 {
1309 *error = FALSE;
1310 if (!skip)
1311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001312 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315 }
1316 if (skip)
1317 --emsg_skip;
1318
1319 return retval;
1320}
1321
1322/*
1323 * Top level evaluation function, returning a string. If "skip" is TRUE,
1324 * only parsing to "nextcmd" is done, without reporting errors. Return
1325 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1326 */
1327 char_u *
1328eval_to_string_skip(arg, nextcmd, skip)
1329 char_u *arg;
1330 char_u **nextcmd;
1331 int skip; /* only parse, don't execute */
1332{
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 char_u *retval;
1335
1336 if (skip)
1337 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001338 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 retval = NULL;
1340 else
1341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001342 retval = vim_strsave(get_tv_string(&tv));
1343 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
1345 if (skip)
1346 --emsg_skip;
1347
1348 return retval;
1349}
1350
1351/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001352 * Skip over an expression at "*pp".
1353 * Return FAIL for an error, OK otherwise.
1354 */
1355 int
1356skip_expr(pp)
1357 char_u **pp;
1358{
Bram Moolenaar33570922005-01-25 22:26:29 +00001359 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001360
1361 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001362 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001363}
1364
1365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001367 * When "convert" is TRUE convert a List into a sequence of lines and convert
1368 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 * Return pointer to allocated memory, or NULL for failure.
1370 */
1371 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 char_u *arg;
1374 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001375 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
Bram Moolenaar33570922005-01-25 22:26:29 +00001377 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001379 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001380#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001381 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001384 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 retval = NULL;
1386 else
1387 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001388 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 {
1390 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001391 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001392 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001393 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001394 if (tv.vval.v_list->lv_len > 0)
1395 ga_append(&ga, NL);
1396 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001397 ga_append(&ga, NUL);
1398 retval = (char_u *)ga.ga_data;
1399 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001400#ifdef FEAT_FLOAT
1401 else if (convert && tv.v_type == VAR_FLOAT)
1402 {
1403 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1404 retval = vim_strsave(numbuf);
1405 }
1406#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001407 else
1408 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001409 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411
1412 return retval;
1413}
1414
1415/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001416 * Call eval_to_string() without using current local variables and using
1417 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 */
1419 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001420eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 char_u *arg;
1422 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001423 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
1425 char_u *retval;
1426 void *save_funccalp;
1427
1428 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001429 if (use_sandbox)
1430 ++sandbox;
1431 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001432 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001433 if (use_sandbox)
1434 --sandbox;
1435 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 restore_funccal(save_funccalp);
1437 return retval;
1438}
1439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440/*
1441 * Top level evaluation function, returning a number.
1442 * Evaluates "expr" silently.
1443 * Returns -1 for an error.
1444 */
1445 int
1446eval_to_number(expr)
1447 char_u *expr;
1448{
Bram Moolenaar33570922005-01-25 22:26:29 +00001449 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001451 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
1453 ++emsg_off;
1454
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001455 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 retval = -1;
1457 else
1458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001459 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001460 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
1462 --emsg_off;
1463
1464 return retval;
1465}
1466
Bram Moolenaara40058a2005-07-11 22:42:07 +00001467/*
1468 * Prepare v: variable "idx" to be used.
1469 * Save the current typeval in "save_tv".
1470 * When not used yet add the variable to the v: hashtable.
1471 */
1472 static void
1473prepare_vimvar(idx, save_tv)
1474 int idx;
1475 typval_T *save_tv;
1476{
1477 *save_tv = vimvars[idx].vv_tv;
1478 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1479 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1480}
1481
1482/*
1483 * Restore v: variable "idx" to typeval "save_tv".
1484 * When no longer defined, remove the variable from the v: hashtable.
1485 */
1486 static void
1487restore_vimvar(idx, save_tv)
1488 int idx;
1489 typval_T *save_tv;
1490{
1491 hashitem_T *hi;
1492
Bram Moolenaara40058a2005-07-11 22:42:07 +00001493 vimvars[idx].vv_tv = *save_tv;
1494 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1495 {
1496 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1497 if (HASHITEM_EMPTY(hi))
1498 EMSG2(_(e_intern2), "restore_vimvar()");
1499 else
1500 hash_remove(&vimvarht, hi);
1501 }
1502}
1503
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001504#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001505/*
1506 * Evaluate an expression to a list with suggestions.
1507 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001508 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001509 */
1510 list_T *
1511eval_spell_expr(badword, expr)
1512 char_u *badword;
1513 char_u *expr;
1514{
1515 typval_T save_val;
1516 typval_T rettv;
1517 list_T *list = NULL;
1518 char_u *p = skipwhite(expr);
1519
1520 /* Set "v:val" to the bad word. */
1521 prepare_vimvar(VV_VAL, &save_val);
1522 vimvars[VV_VAL].vv_type = VAR_STRING;
1523 vimvars[VV_VAL].vv_str = badword;
1524 if (p_verbose == 0)
1525 ++emsg_off;
1526
1527 if (eval1(&p, &rettv, TRUE) == OK)
1528 {
1529 if (rettv.v_type != VAR_LIST)
1530 clear_tv(&rettv);
1531 else
1532 list = rettv.vval.v_list;
1533 }
1534
1535 if (p_verbose == 0)
1536 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001537 restore_vimvar(VV_VAL, &save_val);
1538
1539 return list;
1540}
1541
1542/*
1543 * "list" is supposed to contain two items: a word and a number. Return the
1544 * word in "pp" and the number as the return value.
1545 * Return -1 if anything isn't right.
1546 * Used to get the good word and score from the eval_spell_expr() result.
1547 */
1548 int
1549get_spellword(list, pp)
1550 list_T *list;
1551 char_u **pp;
1552{
1553 listitem_T *li;
1554
1555 li = list->lv_first;
1556 if (li == NULL)
1557 return -1;
1558 *pp = get_tv_string(&li->li_tv);
1559
1560 li = li->li_next;
1561 if (li == NULL)
1562 return -1;
1563 return get_tv_number(&li->li_tv);
1564}
1565#endif
1566
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001567/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001568 * Top level evaluation function.
1569 * Returns an allocated typval_T with the result.
1570 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001571 */
1572 typval_T *
1573eval_expr(arg, nextcmd)
1574 char_u *arg;
1575 char_u **nextcmd;
1576{
1577 typval_T *tv;
1578
1579 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001580 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001581 {
1582 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001583 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001584 }
1585
1586 return tv;
1587}
1588
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001591 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001592 * Uses argv[argc] for the function arguments. Only Number and String
1593 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001594 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001596 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001597call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 char_u *func;
1599 int argc;
1600 char_u **argv;
1601 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001602 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604{
Bram Moolenaar33570922005-01-25 22:26:29 +00001605 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 long n;
1607 int len;
1608 int i;
1609 int doesrange;
1610 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001613 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617 for (i = 0; i < argc; i++)
1618 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001619 /* Pass a NULL or empty argument as an empty string */
1620 if (argv[i] == NULL || *argv[i] == NUL)
1621 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001622 argvars[i].v_type = VAR_STRING;
1623 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001624 continue;
1625 }
1626
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001627 if (str_arg_only)
1628 len = 0;
1629 else
1630 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001631 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 if (len != 0 && len == (int)STRLEN(argv[i]))
1633 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001634 argvars[i].v_type = VAR_NUMBER;
1635 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 }
1637 else
1638 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001639 argvars[i].v_type = VAR_STRING;
1640 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
1642 }
1643
1644 if (safe)
1645 {
1646 save_funccalp = save_funccal();
1647 ++sandbox;
1648 }
1649
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001650 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1651 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (safe)
1655 {
1656 --sandbox;
1657 restore_funccal(save_funccalp);
1658 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659 vim_free(argvars);
1660
1661 if (ret == FAIL)
1662 clear_tv(rettv);
1663
1664 return ret;
1665}
1666
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001667/*
1668 * Call vimL function "func" and return the result as a number.
1669 * Returns -1 when calling the function fails.
1670 * Uses argv[argc] for the function arguments.
1671 */
1672 long
1673call_func_retnr(func, argc, argv, safe)
1674 char_u *func;
1675 int argc;
1676 char_u **argv;
1677 int safe; /* use the sandbox */
1678{
1679 typval_T rettv;
1680 long retval;
1681
1682 /* All arguments are passed as strings, no conversion to number. */
1683 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1684 return -1;
1685
1686 retval = get_tv_number_chk(&rettv, NULL);
1687 clear_tv(&rettv);
1688 return retval;
1689}
1690
1691#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1692 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1693
Bram Moolenaar4f688582007-07-24 12:34:30 +00001694# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001696 * Call vimL function "func" and return the result as a string.
1697 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698 * Uses argv[argc] for the function arguments.
1699 */
1700 void *
1701call_func_retstr(func, argc, argv, safe)
1702 char_u *func;
1703 int argc;
1704 char_u **argv;
1705 int safe; /* use the sandbox */
1706{
1707 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001708 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001709
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001710 /* All arguments are passed as strings, no conversion to number. */
1711 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712 return NULL;
1713
1714 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001715 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 return retval;
1717}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001718# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001720/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001721 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001723 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 */
1725 void *
1726call_func_retlist(func, argc, argv, safe)
1727 char_u *func;
1728 int argc;
1729 char_u **argv;
1730 int safe; /* use the sandbox */
1731{
1732 typval_T rettv;
1733
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001734 /* All arguments are passed as strings, no conversion to number. */
1735 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736 return NULL;
1737
1738 if (rettv.v_type != VAR_LIST)
1739 {
1740 clear_tv(&rettv);
1741 return NULL;
1742 }
1743
1744 return rettv.vval.v_list;
1745}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#endif
1747
1748/*
1749 * Save the current function call pointer, and set it to NULL.
1750 * Used when executing autocommands and for ":source".
1751 */
1752 void *
1753save_funccal()
1754{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001755 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 current_funccal = NULL;
1758 return (void *)fc;
1759}
1760
1761 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001762restore_funccal(vfc)
1763 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001765 funccall_T *fc = (funccall_T *)vfc;
1766
1767 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768}
1769
Bram Moolenaar05159a02005-02-26 23:04:13 +00001770#if defined(FEAT_PROFILE) || defined(PROTO)
1771/*
1772 * Prepare profiling for entering a child or something else that is not
1773 * counted for the script/function itself.
1774 * Should always be called in pair with prof_child_exit().
1775 */
1776 void
1777prof_child_enter(tm)
1778 proftime_T *tm; /* place to store waittime */
1779{
1780 funccall_T *fc = current_funccal;
1781
1782 if (fc != NULL && fc->func->uf_profiling)
1783 profile_start(&fc->prof_child);
1784 script_prof_save(tm);
1785}
1786
1787/*
1788 * Take care of time spent in a child.
1789 * Should always be called after prof_child_enter().
1790 */
1791 void
1792prof_child_exit(tm)
1793 proftime_T *tm; /* where waittime was stored */
1794{
1795 funccall_T *fc = current_funccal;
1796
1797 if (fc != NULL && fc->func->uf_profiling)
1798 {
1799 profile_end(&fc->prof_child);
1800 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1801 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1802 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1803 }
1804 script_prof_restore(tm);
1805}
1806#endif
1807
1808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#ifdef FEAT_FOLDING
1810/*
1811 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1812 * it in "*cp". Doesn't give error messages.
1813 */
1814 int
1815eval_foldexpr(arg, cp)
1816 char_u *arg;
1817 int *cp;
1818{
Bram Moolenaar33570922005-01-25 22:26:29 +00001819 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 int retval;
1821 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001822 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1823 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001826 if (use_sandbox)
1827 ++sandbox;
1828 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 retval = 0;
1832 else
1833 {
1834 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 if (tv.v_type == VAR_NUMBER)
1836 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001837 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 retval = 0;
1839 else
1840 {
1841 /* If the result is a string, check if there is a non-digit before
1842 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 if (!VIM_ISDIGIT(*s) && *s != '-')
1845 *cp = *s++;
1846 retval = atol((char *)s);
1847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
1850 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001851 if (use_sandbox)
1852 --sandbox;
1853 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
1855 return retval;
1856}
1857#endif
1858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 * ":let" list all variable values
1861 * ":let var1 var2" list variable values
1862 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001863 * ":let var += expr" assignment command.
1864 * ":let var -= expr" assignment command.
1865 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 */
1868 void
1869ex_let(eap)
1870 exarg_T *eap;
1871{
1872 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001874 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 int var_count = 0;
1877 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001878 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001879 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001880 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881
Bram Moolenaardb552d602006-03-23 22:59:57 +00001882 argend = skip_var_list(arg, &var_count, &semicolon);
1883 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001885 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1886 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001887 expr = skipwhite(argend);
1888 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1889 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001891 /*
1892 * ":let" without "=": list variables
1893 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 if (*arg == '[')
1895 EMSG(_(e_invarg));
1896 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001898 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001902 list_glob_vars(&first);
1903 list_buf_vars(&first);
1904 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001905#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001907#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001908 list_script_vars(&first);
1909 list_func_vars(&first);
1910 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 eap->nextcmd = check_nextcmd(arg);
1913 }
1914 else
1915 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 op[0] = '=';
1917 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001918 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001919 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001920 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1921 op[0] = *expr; /* +=, -= or .= */
1922 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001924 else
1925 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (eap->skip)
1928 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (eap->skip)
1931 {
1932 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 --emsg_skip;
1935 }
1936 else if (i != FAIL)
1937 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
1942 }
1943}
1944
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945/*
1946 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1947 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 * When "nextchars" is not NULL it points to a string with characters that
1949 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1950 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 * Returns OK or FAIL;
1952 */
1953 static int
1954ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1955 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001956 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 int copy; /* copy values from "tv", don't move */
1958 int semicolon; /* from skip_var_list() */
1959 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961{
1962 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001963 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001965 listitem_T *item;
1966 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967
1968 if (*arg != '[')
1969 {
1970 /*
1971 * ":let var = expr" or ":for var in list"
1972 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 return FAIL;
1975 return OK;
1976 }
1977
1978 /*
1979 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1980 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001981 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 {
1983 EMSG(_(e_listreq));
1984 return FAIL;
1985 }
1986
1987 i = list_len(l);
1988 if (semicolon == 0 && var_count < i)
1989 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001990 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 return FAIL;
1992 }
1993 if (var_count - semicolon > i)
1994 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001995 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 return FAIL;
1997 }
1998
1999 item = l->lv_first;
2000 while (*arg != ']')
2001 {
2002 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002003 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 item = item->li_next;
2005 if (arg == NULL)
2006 return FAIL;
2007
2008 arg = skipwhite(arg);
2009 if (*arg == ';')
2010 {
2011 /* Put the rest of the list (may be empty) in the var after ';'.
2012 * Create a new list for this. */
2013 l = list_alloc();
2014 if (l == NULL)
2015 return FAIL;
2016 while (item != NULL)
2017 {
2018 list_append_tv(l, &item->li_tv);
2019 item = item->li_next;
2020 }
2021
2022 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002023 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 ltv.vval.v_list = l;
2025 l->lv_refcount = 1;
2026
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002027 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2028 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 clear_tv(&ltv);
2030 if (arg == NULL)
2031 return FAIL;
2032 break;
2033 }
2034 else if (*arg != ',' && *arg != ']')
2035 {
2036 EMSG2(_(e_intern2), "ex_let_vars()");
2037 return FAIL;
2038 }
2039 }
2040
2041 return OK;
2042}
2043
2044/*
2045 * Skip over assignable variable "var" or list of variables "[var, var]".
2046 * Used for ":let varvar = expr" and ":for varvar in expr".
2047 * For "[var, var]" increment "*var_count" for each variable.
2048 * for "[var, var; var]" set "semicolon".
2049 * Return NULL for an error.
2050 */
2051 static char_u *
2052skip_var_list(arg, var_count, semicolon)
2053 char_u *arg;
2054 int *var_count;
2055 int *semicolon;
2056{
2057 char_u *p, *s;
2058
2059 if (*arg == '[')
2060 {
2061 /* "[var, var]": find the matching ']'. */
2062 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002063 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064 {
2065 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2066 s = skip_var_one(p);
2067 if (s == p)
2068 {
2069 EMSG2(_(e_invarg2), p);
2070 return NULL;
2071 }
2072 ++*var_count;
2073
2074 p = skipwhite(s);
2075 if (*p == ']')
2076 break;
2077 else if (*p == ';')
2078 {
2079 if (*semicolon == 1)
2080 {
2081 EMSG(_("Double ; in list of variables"));
2082 return NULL;
2083 }
2084 *semicolon = 1;
2085 }
2086 else if (*p != ',')
2087 {
2088 EMSG2(_(e_invarg2), p);
2089 return NULL;
2090 }
2091 }
2092 return p + 1;
2093 }
2094 else
2095 return skip_var_one(arg);
2096}
2097
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002098/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002099 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002100 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002101 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102 static char_u *
2103skip_var_one(arg)
2104 char_u *arg;
2105{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002106 if (*arg == '@' && arg[1] != NUL)
2107 return arg + 2;
2108 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2109 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002110}
2111
Bram Moolenaara7043832005-01-21 11:56:39 +00002112/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002113 * List variables for hashtab "ht" with prefix "prefix".
2114 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002115 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002116 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002118 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002120 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002121 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122{
Bram Moolenaar33570922005-01-25 22:26:29 +00002123 hashitem_T *hi;
2124 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125 int todo;
2126
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002127 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002128 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2129 {
2130 if (!HASHITEM_EMPTY(hi))
2131 {
2132 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002133 di = HI2DI(hi);
2134 if (empty || di->di_tv.v_type != VAR_STRING
2135 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002137 }
2138 }
2139}
2140
2141/*
2142 * List global variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_glob_vars(first)
2146 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002147{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002149}
2150
2151/*
2152 * List buffer variables.
2153 */
2154 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155list_buf_vars(first)
2156 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002157{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158 char_u numbuf[NUMBUFLEN];
2159
Bram Moolenaar429fa852013-04-15 12:27:36 +02002160 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002162
2163 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2165 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002166}
2167
2168/*
2169 * List window variables.
2170 */
2171 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172list_win_vars(first)
2173 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002174{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002175 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002177}
2178
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179#ifdef FEAT_WINDOWS
2180/*
2181 * List tab page variables.
2182 */
2183 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184list_tab_vars(first)
2185 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002186{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002187 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002189}
2190#endif
2191
Bram Moolenaara7043832005-01-21 11:56:39 +00002192/*
2193 * List Vim variables.
2194 */
2195 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196list_vim_vars(first)
2197 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002199 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200}
2201
2202/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002203 * List script-local variables, if there is a script.
2204 */
2205 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206list_script_vars(first)
2207 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208{
2209 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2211 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002212}
2213
2214/*
2215 * List function variables, if there is a function.
2216 */
2217 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218list_func_vars(first)
2219 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220{
2221 if (current_funccal != NULL)
2222 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002224}
2225
2226/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 * List variables in "arg".
2228 */
2229 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 exarg_T *eap;
2232 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002233 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234{
2235 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 char_u *name_start;
2239 char_u *arg_subsc;
2240 char_u *tofree;
2241 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242
2243 while (!ends_excmd(*arg) && !got_int)
2244 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002247 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2249 {
2250 emsg_severe = TRUE;
2251 EMSG(_(e_trailing));
2252 break;
2253 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 /* get_name_len() takes care of expanding curly braces */
2258 name_start = name = arg;
2259 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2260 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 /* This is mainly to keep test 49 working: when expanding
2263 * curly braces fails overrule the exception error message. */
2264 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 emsg_severe = TRUE;
2267 EMSG2(_(e_invarg2), arg);
2268 break;
2269 }
2270 error = TRUE;
2271 }
2272 else
2273 {
2274 if (tofree != NULL)
2275 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002276 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 else
2279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 /* handle d.key, l[idx], f(expr) */
2281 arg_subsc = arg;
2282 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002290 case 'g': list_glob_vars(first); break;
2291 case 'b': list_buf_vars(first); break;
2292 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002293#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002294 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002295#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002296 case 'v': list_vim_vars(first); break;
2297 case 's': list_script_vars(first); break;
2298 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 default:
2300 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002301 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002302 }
2303 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 {
2305 char_u numbuf[NUMBUFLEN];
2306 char_u *tf;
2307 int c;
2308 char_u *s;
2309
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002310 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311 c = *arg;
2312 *arg = NUL;
2313 list_one_var_a((char_u *)"",
2314 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002315 tv.v_type,
2316 s == NULL ? (char_u *)"" : s,
2317 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318 *arg = c;
2319 vim_free(tf);
2320 }
2321 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002322 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 }
2324 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325
2326 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328
2329 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 }
2331
2332 return arg;
2333}
2334
2335/*
2336 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2337 * Returns a pointer to the char just after the var name.
2338 * Returns NULL if there is an error.
2339 */
2340 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002343 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002345 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347{
2348 int c1;
2349 char_u *name;
2350 char_u *p;
2351 char_u *arg_end = NULL;
2352 int len;
2353 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355
2356 /*
2357 * ":let $VAR = expr": Set environment variable.
2358 */
2359 if (*arg == '$')
2360 {
2361 /* Find the end of the name. */
2362 ++arg;
2363 name = arg;
2364 len = get_env_len(&arg);
2365 if (len == 0)
2366 EMSG2(_(e_invarg2), name - 1);
2367 else
2368 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002369 if (op != NULL && (*op == '+' || *op == '-'))
2370 EMSG2(_(e_letwrong), op);
2371 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002372 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002374 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 {
2376 c1 = name[len];
2377 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 p = get_tv_string_chk(tv);
2379 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 {
2381 int mustfree = FALSE;
2382 char_u *s = vim_getenv(name, &mustfree);
2383
2384 if (s != NULL)
2385 {
2386 p = tofree = concat_str(s, p);
2387 if (mustfree)
2388 vim_free(s);
2389 }
2390 }
2391 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 if (STRICMP(name, "HOME") == 0)
2395 init_homedir();
2396 else if (didset_vim && STRICMP(name, "VIM") == 0)
2397 didset_vim = FALSE;
2398 else if (didset_vimruntime
2399 && STRICMP(name, "VIMRUNTIME") == 0)
2400 didset_vimruntime = FALSE;
2401 arg_end = arg;
2402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002405 }
2406 }
2407 }
2408
2409 /*
2410 * ":let &option = expr": Set option value.
2411 * ":let &l:option = expr": Set local option value.
2412 * ":let &g:option = expr": Set global option value.
2413 */
2414 else if (*arg == '&')
2415 {
2416 /* Find the end of the name. */
2417 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002418 if (p == NULL || (endchars != NULL
2419 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 EMSG(_(e_letunexp));
2421 else
2422 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 long n;
2424 int opt_type;
2425 long numval;
2426 char_u *stringval = NULL;
2427 char_u *s;
2428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 c1 = *p;
2430 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431
2432 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002433 s = get_tv_string_chk(tv); /* != NULL if number or string */
2434 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 {
2436 opt_type = get_option_value(arg, &numval,
2437 &stringval, opt_flags);
2438 if ((opt_type == 1 && *op == '.')
2439 || (opt_type == 0 && *op != '.'))
2440 EMSG2(_(e_letwrong), op);
2441 else
2442 {
2443 if (opt_type == 1) /* number */
2444 {
2445 if (*op == '+')
2446 n = numval + n;
2447 else
2448 n = numval - n;
2449 }
2450 else if (opt_type == 0 && stringval != NULL) /* string */
2451 {
2452 s = concat_str(stringval, s);
2453 vim_free(stringval);
2454 stringval = s;
2455 }
2456 }
2457 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 if (s != NULL)
2459 {
2460 set_option_value(arg, n, s, opt_flags);
2461 arg_end = p;
2462 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 }
2466 }
2467
2468 /*
2469 * ":let @r = expr": Set register contents.
2470 */
2471 else if (*arg == '@')
2472 {
2473 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 if (op != NULL && (*op == '+' || *op == '-'))
2475 EMSG2(_(e_letwrong), op);
2476 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002477 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 EMSG(_(e_letunexp));
2479 else
2480 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002481 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 char_u *s;
2483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 p = get_tv_string_chk(tv);
2485 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002487 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 if (s != NULL)
2489 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002490 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 vim_free(s);
2492 }
2493 }
2494 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002497 arg_end = arg + 1;
2498 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002499 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 }
2501 }
2502
2503 /*
2504 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002507 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002509 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002511 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2515 EMSG(_(e_letunexp));
2516 else
2517 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002518 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 arg_end = p;
2520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 }
2524
2525 else
2526 EMSG2(_(e_invarg2), arg);
2527
2528 return arg_end;
2529}
2530
2531/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2533 */
2534 static int
2535check_changedtick(arg)
2536 char_u *arg;
2537{
2538 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2539 {
2540 EMSG2(_(e_readonlyvar), arg);
2541 return TRUE;
2542 }
2543 return FALSE;
2544}
2545
2546/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 * Get an lval: variable, Dict item or List item that can be assigned a value
2548 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2549 * "name.key", "name.key[expr]" etc.
2550 * Indexing only works if "name" is an existing List or Dictionary.
2551 * "name" points to the start of the name.
2552 * If "rettv" is not NULL it points to the value to be assigned.
2553 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2554 * wrong; must end in space or cmd separator.
2555 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002556 * flags:
2557 * GLV_QUIET: do not give error messages
2558 * GLV_NO_AUTOLOAD: do not use script autoloading
2559 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002561 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 */
2564 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002565get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 typval_T *rettv;
2568 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 int unlet;
2570 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002571 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002572 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 char_u *expr_start, *expr_end;
2576 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 dictitem_T *v;
2578 typval_T var1;
2579 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002581 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002585 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589
2590 if (skip)
2591 {
2592 /* When skipping just find the end of the name. */
2593 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002594 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 }
2596
2597 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002598 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (expr_start != NULL)
2600 {
2601 /* Don't expand the name when we already know there is an error. */
2602 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2603 && *p != '[' && *p != '.')
2604 {
2605 EMSG(_(e_trailing));
2606 return NULL;
2607 }
2608
2609 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2610 if (lp->ll_exp_name == NULL)
2611 {
2612 /* Report an invalid expression in braces, unless the
2613 * expression evaluation has been cancelled due to an
2614 * aborting error, an interrupt, or an exception. */
2615 if (!aborting() && !quiet)
2616 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002617 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 EMSG2(_(e_invarg2), name);
2619 return NULL;
2620 }
2621 }
2622 lp->ll_name = lp->ll_exp_name;
2623 }
2624 else
2625 lp->ll_name = name;
2626
2627 /* Without [idx] or .key we are done. */
2628 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2629 return p;
2630
2631 cc = *p;
2632 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002633 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (v == NULL && !quiet)
2635 EMSG2(_(e_undefvar), lp->ll_name);
2636 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 if (v == NULL)
2638 return NULL;
2639
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 /*
2641 * Loop until no more [idx] or .key is following.
2642 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002643 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2647 && !(lp->ll_tv->v_type == VAR_DICT
2648 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (!quiet)
2651 EMSG(_("E689: Can only index a List or Dictionary"));
2652 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!quiet)
2657 EMSG(_("E708: [:] must come last"));
2658 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 len = -1;
2662 if (*p == '.')
2663 {
2664 key = p + 1;
2665 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2666 ;
2667 if (len == 0)
2668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 if (!quiet)
2670 EMSG(_(e_emptykey));
2671 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 }
2673 p = key + len;
2674 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675 else
2676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (*p == ':')
2680 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 else
2682 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 empty1 = FALSE;
2684 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002686 if (get_tv_string_chk(&var1) == NULL)
2687 {
2688 /* not a number or string */
2689 clear_tv(&var1);
2690 return NULL;
2691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 }
2693
2694 /* Optionally get the second index [ :expr]. */
2695 if (*p == ':')
2696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002700 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701 if (!empty1)
2702 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (rettv != NULL && (rettv->v_type != VAR_LIST
2706 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!quiet)
2709 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 if (!empty1)
2711 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 }
2714 p = skipwhite(p + 1);
2715 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 else
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 if (!empty1)
2723 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002726 if (get_tv_string_chk(&var2) == NULL)
2727 {
2728 /* not a number or string */
2729 if (!empty1)
2730 clear_tv(&var1);
2731 clear_tv(&var2);
2732 return NULL;
2733 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (!quiet)
2743 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 if (!empty1)
2745 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 }
2750
2751 /* Skip to past ']'. */
2752 ++p;
2753 }
2754
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 {
2757 if (len == -1)
2758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002760 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 if (*key == NUL)
2762 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (!quiet)
2764 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 }
2768 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002769 lp->ll_list = NULL;
2770 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002771 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002772
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002773 /* When assigning to a scope dictionary check that a function and
2774 * variable name is valid (only variable name unless it is l: or
2775 * g: dictionary). Disallow overwriting a builtin function. */
2776 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002778 int prevval;
2779 int wrong;
2780
2781 if (len != -1)
2782 {
2783 prevval = key[len];
2784 key[len] = NUL;
2785 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002786 else
2787 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002788 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2789 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002790 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002791 || !valid_varname(key);
2792 if (len != -1)
2793 key[len] = prevval;
2794 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002795 return NULL;
2796 }
2797
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 /* Can't add "v:" variable. */
2801 if (lp->ll_dict == &vimvardict)
2802 {
2803 EMSG2(_(e_illvar), name);
2804 return NULL;
2805 }
2806
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002807 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002811 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 if (len == -1)
2813 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 }
2816 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 if (len == -1)
2821 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 p = NULL;
2824 break;
2825 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002826 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002827 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002828 return NULL;
2829
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 if (len == -1)
2831 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 }
2834 else
2835 {
2836 /*
2837 * Get the number and item for the only or first index of the List.
2838 */
2839 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 else
2842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002843 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 clear_tv(&var1);
2845 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002846 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 lp->ll_list = lp->ll_tv->vval.v_list;
2848 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2849 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002851 if (lp->ll_n1 < 0)
2852 {
2853 lp->ll_n1 = 0;
2854 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2855 }
2856 }
2857 if (lp->ll_li == NULL)
2858 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002861 if (!quiet)
2862 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 }
2865
2866 /*
2867 * May need to find the item or absolute index for the second
2868 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 * When no index given: "lp->ll_empty2" is TRUE.
2870 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002874 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 {
2881 if (!quiet)
2882 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002884 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 }
2887
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2889 if (lp->ll_n1 < 0)
2890 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2891 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002892 {
2893 if (!quiet)
2894 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002896 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002897 }
2898
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002901 }
2902
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 return p;
2904}
2905
2906/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002907 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 */
2909 static void
2910clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002911 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912{
2913 vim_free(lp->ll_exp_name);
2914 vim_free(lp->ll_newkey);
2915}
2916
2917/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002918 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 */
2922 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002923set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002928 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929{
2930 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002931 listitem_T *ri;
2932 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933
2934 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 cc = *endp;
2939 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940 if (op != NULL && *op != '=')
2941 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002942 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002943
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002944 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002945 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002946 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002947 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002949 if ((di == NULL
2950 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2951 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2952 FALSE)))
2953 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 set_var(lp->ll_name, &tv, FALSE);
2955 clear_tv(&tv);
2956 }
2957 }
2958 else
2959 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002963 else if (tv_check_lock(lp->ll_newkey == NULL
2964 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002965 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002966 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 else if (lp->ll_range)
2968 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002969 listitem_T *ll_li = lp->ll_li;
2970 int ll_n1 = lp->ll_n1;
2971
2972 /*
2973 * Check whether any of the list items is locked
2974 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002975 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002976 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002977 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002978 return;
2979 ri = ri->li_next;
2980 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2981 break;
2982 ll_li = ll_li->li_next;
2983 ++ll_n1;
2984 }
2985
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986 /*
2987 * Assign the List values to the list items.
2988 */
2989 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002990 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002991 if (op != NULL && *op != '=')
2992 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2993 else
2994 {
2995 clear_tv(&lp->ll_li->li_tv);
2996 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2997 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 ri = ri->li_next;
2999 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3000 break;
3001 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003004 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 ri = NULL;
3007 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003008 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 lp->ll_li = lp->ll_li->li_next;
3011 ++lp->ll_n1;
3012 }
3013 if (ri != NULL)
3014 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 else if (lp->ll_empty2
3016 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 : lp->ll_n1 != lp->ll_n2)
3018 EMSG(_("E711: List value has not enough items"));
3019 }
3020 else
3021 {
3022 /*
3023 * Assign to a List or Dictionary item.
3024 */
3025 if (lp->ll_newkey != NULL)
3026 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 if (op != NULL && *op != '=')
3028 {
3029 EMSG2(_(e_letwrong), op);
3030 return;
3031 }
3032
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003033 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003034 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003035 if (di == NULL)
3036 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003037 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3038 {
3039 vim_free(di);
3040 return;
3041 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003043 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 else if (op != NULL && *op != '=')
3045 {
3046 tv_op(lp->ll_tv, rettv, op);
3047 return;
3048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003049 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003051
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 /*
3053 * Assign the value to the variable or list item.
3054 */
3055 if (copy)
3056 copy_tv(rettv, lp->ll_tv);
3057 else
3058 {
3059 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003060 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003061 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003062 }
3063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003064}
3065
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003066/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003067 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3068 * Returns OK or FAIL.
3069 */
3070 static int
3071tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003072 typval_T *tv1;
3073 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 char_u *op;
3075{
3076 long n;
3077 char_u numbuf[NUMBUFLEN];
3078 char_u *s;
3079
3080 /* Can't do anything with a Funcref or a Dict on the right. */
3081 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3082 {
3083 switch (tv1->v_type)
3084 {
3085 case VAR_DICT:
3086 case VAR_FUNC:
3087 break;
3088
3089 case VAR_LIST:
3090 if (*op != '+' || tv2->v_type != VAR_LIST)
3091 break;
3092 /* List += List */
3093 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3094 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3095 return OK;
3096
3097 case VAR_NUMBER:
3098 case VAR_STRING:
3099 if (tv2->v_type == VAR_LIST)
3100 break;
3101 if (*op == '+' || *op == '-')
3102 {
3103 /* nr += nr or nr -= nr*/
3104 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003105#ifdef FEAT_FLOAT
3106 if (tv2->v_type == VAR_FLOAT)
3107 {
3108 float_T f = n;
3109
3110 if (*op == '+')
3111 f += tv2->vval.v_float;
3112 else
3113 f -= tv2->vval.v_float;
3114 clear_tv(tv1);
3115 tv1->v_type = VAR_FLOAT;
3116 tv1->vval.v_float = f;
3117 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003118 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003119#endif
3120 {
3121 if (*op == '+')
3122 n += get_tv_number(tv2);
3123 else
3124 n -= get_tv_number(tv2);
3125 clear_tv(tv1);
3126 tv1->v_type = VAR_NUMBER;
3127 tv1->vval.v_number = n;
3128 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129 }
3130 else
3131 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003132 if (tv2->v_type == VAR_FLOAT)
3133 break;
3134
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 /* str .= str */
3136 s = get_tv_string(tv1);
3137 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3138 clear_tv(tv1);
3139 tv1->v_type = VAR_STRING;
3140 tv1->vval.v_string = s;
3141 }
3142 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143
3144#ifdef FEAT_FLOAT
3145 case VAR_FLOAT:
3146 {
3147 float_T f;
3148
3149 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3150 && tv2->v_type != VAR_NUMBER
3151 && tv2->v_type != VAR_STRING))
3152 break;
3153 if (tv2->v_type == VAR_FLOAT)
3154 f = tv2->vval.v_float;
3155 else
3156 f = get_tv_number(tv2);
3157 if (*op == '+')
3158 tv1->vval.v_float += f;
3159 else
3160 tv1->vval.v_float -= f;
3161 }
3162 return OK;
3163#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003164 }
3165 }
3166
3167 EMSG2(_(e_letwrong), op);
3168 return FAIL;
3169}
3170
3171/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172 * Add a watcher to a list.
3173 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003174 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 list_T *l;
3177 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178{
3179 lw->lw_next = l->lv_watch;
3180 l->lv_watch = lw;
3181}
3182
3183/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003184 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185 * No warning when it isn't found...
3186 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003187 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003189 list_T *l;
3190 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191{
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193
3194 lwp = &l->lv_watch;
3195 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3196 {
3197 if (lw == lwrem)
3198 {
3199 *lwp = lw->lw_next;
3200 break;
3201 }
3202 lwp = &lw->lw_next;
3203 }
3204}
3205
3206/*
3207 * Just before removing an item from a list: advance watchers to the next
3208 * item.
3209 */
3210 static void
3211list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003212 list_T *l;
3213 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214{
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216
3217 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3218 if (lw->lw_item == item)
3219 lw->lw_item = item->li_next;
3220}
3221
3222/*
3223 * Evaluate the expression used in a ":for var in expr" command.
3224 * "arg" points to "var".
3225 * Set "*errp" to TRUE for an error, FALSE otherwise;
3226 * Return a pointer that holds the info. Null when there is an error.
3227 */
3228 void *
3229eval_for_line(arg, errp, nextcmdp, skip)
3230 char_u *arg;
3231 int *errp;
3232 char_u **nextcmdp;
3233 int skip;
3234{
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 typval_T tv;
3238 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239
3240 *errp = TRUE; /* default: there is an error */
3241
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 if (fi == NULL)
3244 return NULL;
3245
3246 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3247 if (expr == NULL)
3248 return fi;
3249
3250 expr = skipwhite(expr);
3251 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3252 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003253 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 return fi;
3255 }
3256
3257 if (skip)
3258 ++emsg_skip;
3259 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3260 {
3261 *errp = FALSE;
3262 if (!skip)
3263 {
3264 l = tv.vval.v_list;
3265 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 clear_tv(&tv);
3269 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 else
3271 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003272 /* No need to increment the refcount, it's already set for the
3273 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 fi->fi_list = l;
3275 list_add_watch(l, &fi->fi_lw);
3276 fi->fi_lw.lw_item = l->lv_first;
3277 }
3278 }
3279 }
3280 if (skip)
3281 --emsg_skip;
3282
3283 return fi;
3284}
3285
3286/*
3287 * Use the first item in a ":for" list. Advance to the next.
3288 * Assign the values to the variable (list). "arg" points to the first one.
3289 * Return TRUE when a valid item was found, FALSE when at end of list or
3290 * something wrong.
3291 */
3292 int
3293next_for_item(fi_void, arg)
3294 void *fi_void;
3295 char_u *arg;
3296{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003297 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300
3301 item = fi->fi_lw.lw_item;
3302 if (item == NULL)
3303 result = FALSE;
3304 else
3305 {
3306 fi->fi_lw.lw_item = item->li_next;
3307 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3308 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3309 }
3310 return result;
3311}
3312
3313/*
3314 * Free the structure used to store info used by ":for".
3315 */
3316 void
3317free_for_info(fi_void)
3318 void *fi_void;
3319{
Bram Moolenaar33570922005-01-25 22:26:29 +00003320 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003322 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003323 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003325 list_unref(fi->fi_list);
3326 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 vim_free(fi);
3328}
3329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3331
3332 void
3333set_context_for_expression(xp, arg, cmdidx)
3334 expand_T *xp;
3335 char_u *arg;
3336 cmdidx_T cmdidx;
3337{
3338 int got_eq = FALSE;
3339 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 if (cmdidx == CMD_let)
3343 {
3344 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003345 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 {
3347 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003348 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 {
3350 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003351 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 if (vim_iswhite(*p))
3353 break;
3354 }
3355 return;
3356 }
3357 }
3358 else
3359 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3360 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 while ((xp->xp_pattern = vim_strpbrk(arg,
3362 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3363 {
3364 c = *xp->xp_pattern;
3365 if (c == '&')
3366 {
3367 c = xp->xp_pattern[1];
3368 if (c == '&')
3369 {
3370 ++xp->xp_pattern;
3371 xp->xp_context = cmdidx != CMD_let || got_eq
3372 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3373 }
3374 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003377 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3378 xp->xp_pattern += 2;
3379
3380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
3382 else if (c == '$')
3383 {
3384 /* environment variable */
3385 xp->xp_context = EXPAND_ENV_VARS;
3386 }
3387 else if (c == '=')
3388 {
3389 got_eq = TRUE;
3390 xp->xp_context = EXPAND_EXPRESSION;
3391 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003392 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 && xp->xp_context == EXPAND_FUNCTIONS
3394 && vim_strchr(xp->xp_pattern, '(') == NULL)
3395 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003396 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 break;
3398 }
3399 else if (cmdidx != CMD_let || got_eq)
3400 {
3401 if (c == '"') /* string */
3402 {
3403 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3404 if (c == '\\' && xp->xp_pattern[1] != NUL)
3405 ++xp->xp_pattern;
3406 xp->xp_context = EXPAND_NOTHING;
3407 }
3408 else if (c == '\'') /* literal string */
3409 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003410 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3412 /* skip */ ;
3413 xp->xp_context = EXPAND_NOTHING;
3414 }
3415 else if (c == '|')
3416 {
3417 if (xp->xp_pattern[1] == '|')
3418 {
3419 ++xp->xp_pattern;
3420 xp->xp_context = EXPAND_EXPRESSION;
3421 }
3422 else
3423 xp->xp_context = EXPAND_COMMANDS;
3424 }
3425 else
3426 xp->xp_context = EXPAND_EXPRESSION;
3427 }
3428 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003429 /* Doesn't look like something valid, expand as an expression
3430 * anyway. */
3431 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 arg = xp->xp_pattern;
3433 if (*arg != NUL)
3434 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3435 /* skip */ ;
3436 }
3437 xp->xp_pattern = arg;
3438}
3439
3440#endif /* FEAT_CMDL_COMPL */
3441
3442/*
3443 * ":1,25call func(arg1, arg2)" function call.
3444 */
3445 void
3446ex_call(eap)
3447 exarg_T *eap;
3448{
3449 char_u *arg = eap->arg;
3450 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003452 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003454 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 linenr_T lnum;
3456 int doesrange;
3457 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003458 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003460 if (eap->skip)
3461 {
3462 /* trans_function_name() doesn't work well when skipping, use eval0()
3463 * instead to skip to any following command, e.g. for:
3464 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003465 ++emsg_skip;
3466 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3467 clear_tv(&rettv);
3468 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003469 return;
3470 }
3471
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003472 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003473 if (fudi.fd_newkey != NULL)
3474 {
3475 /* Still need to give an error message for missing key. */
3476 EMSG2(_(e_dictkey), fudi.fd_newkey);
3477 vim_free(fudi.fd_newkey);
3478 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003479 if (tofree == NULL)
3480 return;
3481
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003482 /* Increase refcount on dictionary, it could get deleted when evaluating
3483 * the arguments. */
3484 if (fudi.fd_dict != NULL)
3485 ++fudi.fd_dict->dv_refcount;
3486
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003487 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003488 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003489 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
Bram Moolenaar532c7802005-01-27 14:44:31 +00003491 /* Skip white space to allow ":call func ()". Not good, but required for
3492 * backward compatibility. */
3493 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003494 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496 if (*startarg != '(')
3497 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003498 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 goto end;
3500 }
3501
3502 /*
3503 * When skipping, evaluate the function once, to find the end of the
3504 * arguments.
3505 * When the function takes a range, this is discovered after the first
3506 * call, and the loop is broken.
3507 */
3508 if (eap->skip)
3509 {
3510 ++emsg_skip;
3511 lnum = eap->line2; /* do it once, also with an invalid range */
3512 }
3513 else
3514 lnum = eap->line1;
3515 for ( ; lnum <= eap->line2; ++lnum)
3516 {
3517 if (!eap->skip && eap->addr_count > 0)
3518 {
3519 curwin->w_cursor.lnum = lnum;
3520 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003521#ifdef FEAT_VIRTUALEDIT
3522 curwin->w_cursor.coladd = 0;
3523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
3525 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003526 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003527 eap->line1, eap->line2, &doesrange,
3528 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
3530 failed = TRUE;
3531 break;
3532 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003533
3534 /* Handle a function returning a Funcref, Dictionary or List. */
3535 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3536 {
3537 failed = TRUE;
3538 break;
3539 }
3540
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003541 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (doesrange || eap->skip)
3543 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003546 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003547 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003548 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 if (aborting())
3550 break;
3551 }
3552 if (eap->skip)
3553 --emsg_skip;
3554
3555 if (!failed)
3556 {
3557 /* Check for trailing illegal characters and a following command. */
3558 if (!ends_excmd(*arg))
3559 {
3560 emsg_severe = TRUE;
3561 EMSG(_(e_trailing));
3562 }
3563 else
3564 eap->nextcmd = check_nextcmd(arg);
3565 }
3566
3567end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003568 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003569 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570}
3571
3572/*
3573 * ":unlet[!] var1 ... " command.
3574 */
3575 void
3576ex_unlet(eap)
3577 exarg_T *eap;
3578{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003579 ex_unletlock(eap, eap->arg, 0);
3580}
3581
3582/*
3583 * ":lockvar" and ":unlockvar" commands
3584 */
3585 void
3586ex_lockvar(eap)
3587 exarg_T *eap;
3588{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 int deep = 2;
3591
3592 if (eap->forceit)
3593 deep = -1;
3594 else if (vim_isdigit(*arg))
3595 {
3596 deep = getdigits(&arg);
3597 arg = skipwhite(arg);
3598 }
3599
3600 ex_unletlock(eap, arg, deep);
3601}
3602
3603/*
3604 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3605 */
3606 static void
3607ex_unletlock(eap, argstart, deep)
3608 exarg_T *eap;
3609 char_u *argstart;
3610 int deep;
3611{
3612 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003615 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
3617 do
3618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003620 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003621 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 if (lv.ll_name == NULL)
3623 error = TRUE; /* error but continue parsing */
3624 if (name_end == NULL || (!vim_iswhite(*name_end)
3625 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 if (name_end != NULL)
3628 {
3629 emsg_severe = TRUE;
3630 EMSG(_(e_trailing));
3631 }
3632 if (!(eap->skip || error))
3633 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 break;
3635 }
3636
3637 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 {
3639 if (eap->cmdidx == CMD_unlet)
3640 {
3641 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3642 error = TRUE;
3643 }
3644 else
3645 {
3646 if (do_lock_var(&lv, name_end, deep,
3647 eap->cmdidx == CMD_lockvar) == FAIL)
3648 error = TRUE;
3649 }
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 if (!eap->skip)
3653 clear_lval(&lv);
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 arg = skipwhite(name_end);
3656 } while (!ends_excmd(*arg));
3657
3658 eap->nextcmd = check_nextcmd(arg);
3659}
3660
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003663 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 int forceit;
3666{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 int ret = OK;
3668 int cc;
3669
3670 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 cc = *name_end;
3673 *name_end = NUL;
3674
3675 /* Normal name or expanded name. */
3676 if (check_changedtick(lp->ll_name))
3677 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003682 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003683 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003684 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003685 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 else if (lp->ll_range)
3688 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003689 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003690 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003691 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003692
3693 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3694 {
3695 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003696 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003697 return FAIL;
3698 ll_li = li;
3699 ++ll_n1;
3700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701
3702 /* Delete a range of List items. */
3703 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3704 {
3705 li = lp->ll_li->li_next;
3706 listitem_remove(lp->ll_list, lp->ll_li);
3707 lp->ll_li = li;
3708 ++lp->ll_n1;
3709 }
3710 }
3711 else
3712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 /* unlet a List item. */
3715 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003718 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 }
3720
3721 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003722}
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724/*
3725 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
3728 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003729do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003731 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732{
Bram Moolenaar33570922005-01-25 22:26:29 +00003733 hashtab_T *ht;
3734 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003735 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003736 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003737 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
Bram Moolenaar33570922005-01-25 22:26:29 +00003739 ht = find_var_ht(name, &varname);
3740 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003742 if (ht == &globvarht)
3743 d = &globvardict;
3744 else if (current_funccal != NULL
3745 && ht == &current_funccal->l_vars.dv_hashtab)
3746 d = &current_funccal->l_vars;
3747 else if (ht == &compat_hashtab)
3748 d = &vimvardict;
3749 else
3750 {
3751 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3752 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3753 }
3754 if (d == NULL)
3755 {
3756 EMSG2(_(e_intern2), "do_unlet()");
3757 return FAIL;
3758 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003759 hi = hash_find(ht, varname);
3760 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003761 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003762 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003763 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003764 || var_check_ro(di->di_flags, name, FALSE)
3765 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003766 return FAIL;
3767
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003768 delete_var(ht, hi);
3769 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003772 if (forceit)
3773 return OK;
3774 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 return FAIL;
3776}
3777
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003778/*
3779 * Lock or unlock variable indicated by "lp".
3780 * "deep" is the levels to go (-1 for unlimited);
3781 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3782 */
3783 static int
3784do_lock_var(lp, name_end, deep, lock)
3785 lval_T *lp;
3786 char_u *name_end;
3787 int deep;
3788 int lock;
3789{
3790 int ret = OK;
3791 int cc;
3792 dictitem_T *di;
3793
3794 if (deep == 0) /* nothing to do */
3795 return OK;
3796
3797 if (lp->ll_tv == NULL)
3798 {
3799 cc = *name_end;
3800 *name_end = NUL;
3801
3802 /* Normal name or expanded name. */
3803 if (check_changedtick(lp->ll_name))
3804 ret = FAIL;
3805 else
3806 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003807 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003808 if (di == NULL)
3809 ret = FAIL;
3810 else
3811 {
3812 if (lock)
3813 di->di_flags |= DI_FLAGS_LOCK;
3814 else
3815 di->di_flags &= ~DI_FLAGS_LOCK;
3816 item_lock(&di->di_tv, deep, lock);
3817 }
3818 }
3819 *name_end = cc;
3820 }
3821 else if (lp->ll_range)
3822 {
3823 listitem_T *li = lp->ll_li;
3824
3825 /* (un)lock a range of List items. */
3826 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3827 {
3828 item_lock(&li->li_tv, deep, lock);
3829 li = li->li_next;
3830 ++lp->ll_n1;
3831 }
3832 }
3833 else if (lp->ll_list != NULL)
3834 /* (un)lock a List item. */
3835 item_lock(&lp->ll_li->li_tv, deep, lock);
3836 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003837 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003838 item_lock(&lp->ll_di->di_tv, deep, lock);
3839
3840 return ret;
3841}
3842
3843/*
3844 * Lock or unlock an item. "deep" is nr of levels to go.
3845 */
3846 static void
3847item_lock(tv, deep, lock)
3848 typval_T *tv;
3849 int deep;
3850 int lock;
3851{
3852 static int recurse = 0;
3853 list_T *l;
3854 listitem_T *li;
3855 dict_T *d;
3856 hashitem_T *hi;
3857 int todo;
3858
3859 if (recurse >= DICT_MAXNEST)
3860 {
3861 EMSG(_("E743: variable nested too deep for (un)lock"));
3862 return;
3863 }
3864 if (deep == 0)
3865 return;
3866 ++recurse;
3867
3868 /* lock/unlock the item itself */
3869 if (lock)
3870 tv->v_lock |= VAR_LOCKED;
3871 else
3872 tv->v_lock &= ~VAR_LOCKED;
3873
3874 switch (tv->v_type)
3875 {
3876 case VAR_LIST:
3877 if ((l = tv->vval.v_list) != NULL)
3878 {
3879 if (lock)
3880 l->lv_lock |= VAR_LOCKED;
3881 else
3882 l->lv_lock &= ~VAR_LOCKED;
3883 if (deep < 0 || deep > 1)
3884 /* recursive: lock/unlock the items the List contains */
3885 for (li = l->lv_first; li != NULL; li = li->li_next)
3886 item_lock(&li->li_tv, deep - 1, lock);
3887 }
3888 break;
3889 case VAR_DICT:
3890 if ((d = tv->vval.v_dict) != NULL)
3891 {
3892 if (lock)
3893 d->dv_lock |= VAR_LOCKED;
3894 else
3895 d->dv_lock &= ~VAR_LOCKED;
3896 if (deep < 0 || deep > 1)
3897 {
3898 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003899 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003900 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3901 {
3902 if (!HASHITEM_EMPTY(hi))
3903 {
3904 --todo;
3905 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3906 }
3907 }
3908 }
3909 }
3910 }
3911 --recurse;
3912}
3913
Bram Moolenaara40058a2005-07-11 22:42:07 +00003914/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003915 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3916 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003917 */
3918 static int
3919tv_islocked(tv)
3920 typval_T *tv;
3921{
3922 return (tv->v_lock & VAR_LOCKED)
3923 || (tv->v_type == VAR_LIST
3924 && tv->vval.v_list != NULL
3925 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3926 || (tv->v_type == VAR_DICT
3927 && tv->vval.v_dict != NULL
3928 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3929}
3930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3932/*
3933 * Delete all "menutrans_" variables.
3934 */
3935 void
3936del_menutrans_vars()
3937{
Bram Moolenaar33570922005-01-25 22:26:29 +00003938 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003942 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003943 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 {
3945 if (!HASHITEM_EMPTY(hi))
3946 {
3947 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3949 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 }
3951 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953}
3954#endif
3955
3956#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3957
3958/*
3959 * Local string buffer for the next two functions to store a variable name
3960 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3961 * get_user_var_name().
3962 */
3963
3964static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3965
3966static char_u *varnamebuf = NULL;
3967static int varnamebuflen = 0;
3968
3969/*
3970 * Function to concatenate a prefix and a variable name.
3971 */
3972 static char_u *
3973cat_prefix_varname(prefix, name)
3974 int prefix;
3975 char_u *name;
3976{
3977 int len;
3978
3979 len = (int)STRLEN(name) + 3;
3980 if (len > varnamebuflen)
3981 {
3982 vim_free(varnamebuf);
3983 len += 10; /* some additional space */
3984 varnamebuf = alloc(len);
3985 if (varnamebuf == NULL)
3986 {
3987 varnamebuflen = 0;
3988 return NULL;
3989 }
3990 varnamebuflen = len;
3991 }
3992 *varnamebuf = prefix;
3993 varnamebuf[1] = ':';
3994 STRCPY(varnamebuf + 2, name);
3995 return varnamebuf;
3996}
3997
3998/*
3999 * Function given to ExpandGeneric() to obtain the list of user defined
4000 * (global/buffer/window/built-in) variable names.
4001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 char_u *
4003get_user_var_name(xp, idx)
4004 expand_T *xp;
4005 int idx;
4006{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004007 static long_u gdone;
4008 static long_u bdone;
4009 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004010#ifdef FEAT_WINDOWS
4011 static long_u tdone;
4012#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004013 static int vidx;
4014 static hashitem_T *hi;
4015 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016
4017 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004018 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004019 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020#ifdef FEAT_WINDOWS
4021 tdone = 0;
4022#endif
4023 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004024
4025 /* Global variables */
4026 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004028 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004030 else
4031 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004032 while (HASHITEM_EMPTY(hi))
4033 ++hi;
4034 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4035 return cat_prefix_varname('g', hi->hi_key);
4036 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004038
4039 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004040 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004045 else
4046 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 while (HASHITEM_EMPTY(hi))
4048 ++hi;
4049 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004053 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 return (char_u *)"b:changedtick";
4055 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004056
4057 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004058 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004059 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004061 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004062 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004063 else
4064 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004065 while (HASHITEM_EMPTY(hi))
4066 ++hi;
4067 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004069
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004070#ifdef FEAT_WINDOWS
4071 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004072 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004073 if (tdone < ht->ht_used)
4074 {
4075 if (tdone++ == 0)
4076 hi = ht->ht_array;
4077 else
4078 ++hi;
4079 while (HASHITEM_EMPTY(hi))
4080 ++hi;
4081 return cat_prefix_varname('t', hi->hi_key);
4082 }
4083#endif
4084
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 /* v: variables */
4086 if (vidx < VV_LEN)
4087 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 vim_free(varnamebuf);
4090 varnamebuf = NULL;
4091 varnamebuflen = 0;
4092 return NULL;
4093}
4094
4095#endif /* FEAT_CMDL_COMPL */
4096
4097/*
4098 * types for expressions.
4099 */
4100typedef enum
4101{
4102 TYPE_UNKNOWN = 0
4103 , TYPE_EQUAL /* == */
4104 , TYPE_NEQUAL /* != */
4105 , TYPE_GREATER /* > */
4106 , TYPE_GEQUAL /* >= */
4107 , TYPE_SMALLER /* < */
4108 , TYPE_SEQUAL /* <= */
4109 , TYPE_MATCH /* =~ */
4110 , TYPE_NOMATCH /* !~ */
4111} exptype_T;
4112
4113/*
4114 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4117 */
4118
4119/*
4120 * Handle zero level expression.
4121 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004123 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 * Return OK or FAIL.
4125 */
4126 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 char_u **nextcmd;
4131 int evaluate;
4132{
4133 int ret;
4134 char_u *p;
4135
4136 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 if (ret == FAIL || !ends_excmd(*p))
4139 {
4140 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 /*
4143 * Report the invalid expression unless the expression evaluation has
4144 * been cancelled due to an aborting error, an interrupt, or an
4145 * exception.
4146 */
4147 if (!aborting())
4148 EMSG2(_(e_invexpr2), arg);
4149 ret = FAIL;
4150 }
4151 if (nextcmd != NULL)
4152 *nextcmd = check_nextcmd(p);
4153
4154 return ret;
4155}
4156
4157/*
4158 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004159 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 *
4161 * "arg" must point to the first non-white of the expression.
4162 * "arg" is advanced to the next non-white after the recognized expression.
4163 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004164 * Note: "rettv.v_lock" is not set.
4165 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 * Return OK or FAIL.
4167 */
4168 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 int evaluate;
4173{
4174 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004175 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
4177 /*
4178 * Get the first variable.
4179 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 return FAIL;
4182
4183 if ((*arg)[0] == '?')
4184 {
4185 result = FALSE;
4186 if (evaluate)
4187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 int error = FALSE;
4189
4190 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193 if (error)
4194 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196
4197 /*
4198 * Get the second variable.
4199 */
4200 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 return FAIL;
4203
4204 /*
4205 * Check for the ":".
4206 */
4207 if ((*arg)[0] != ':')
4208 {
4209 EMSG(_("E109: Missing ':' after '?'"));
4210 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213 }
4214
4215 /*
4216 * Get the third variable.
4217 */
4218 *arg = skipwhite(*arg + 1);
4219 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4220 {
4221 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 return FAIL;
4224 }
4225 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228
4229 return OK;
4230}
4231
4232/*
4233 * Handle first level expression:
4234 * expr2 || expr2 || expr2 logical OR
4235 *
4236 * "arg" must point to the first non-white of the expression.
4237 * "arg" is advanced to the next non-white after the recognized expression.
4238 *
4239 * Return OK or FAIL.
4240 */
4241 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 int evaluate;
4246{
Bram Moolenaar33570922005-01-25 22:26:29 +00004247 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 long result;
4249 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 /*
4253 * Get the first variable.
4254 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 return FAIL;
4257
4258 /*
4259 * Repeat until there is no following "||".
4260 */
4261 first = TRUE;
4262 result = FALSE;
4263 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4264 {
4265 if (evaluate && first)
4266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004267 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004269 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 if (error)
4271 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 first = FALSE;
4273 }
4274
4275 /*
4276 * Get the second variable.
4277 */
4278 *arg = skipwhite(*arg + 2);
4279 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4280 return FAIL;
4281
4282 /*
4283 * Compute the result.
4284 */
4285 if (evaluate && !result)
4286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004287 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004290 if (error)
4291 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293 if (evaluate)
4294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 rettv->v_type = VAR_NUMBER;
4296 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298 }
4299
4300 return OK;
4301}
4302
4303/*
4304 * Handle second level expression:
4305 * expr3 && expr3 && expr3 logical AND
4306 *
4307 * "arg" must point to the first non-white of the expression.
4308 * "arg" is advanced to the next non-white after the recognized expression.
4309 *
4310 * Return OK or FAIL.
4311 */
4312 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 int evaluate;
4317{
Bram Moolenaar33570922005-01-25 22:26:29 +00004318 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 long result;
4320 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 /*
4324 * Get the first variable.
4325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return FAIL;
4328
4329 /*
4330 * Repeat until there is no following "&&".
4331 */
4332 first = TRUE;
4333 result = TRUE;
4334 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4335 {
4336 if (evaluate && first)
4337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (error)
4342 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 first = FALSE;
4344 }
4345
4346 /*
4347 * Get the second variable.
4348 */
4349 *arg = skipwhite(*arg + 2);
4350 if (eval4(arg, &var2, evaluate && result) == FAIL)
4351 return FAIL;
4352
4353 /*
4354 * Compute the result.
4355 */
4356 if (evaluate && result)
4357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004361 if (error)
4362 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
4364 if (evaluate)
4365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 rettv->v_type = VAR_NUMBER;
4367 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 }
4370
4371 return OK;
4372}
4373
4374/*
4375 * Handle third level expression:
4376 * var1 == var2
4377 * var1 =~ var2
4378 * var1 != var2
4379 * var1 !~ var2
4380 * var1 > var2
4381 * var1 >= var2
4382 * var1 < var2
4383 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004384 * var1 is var2
4385 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 *
4387 * "arg" must point to the first non-white of the expression.
4388 * "arg" is advanced to the next non-white after the recognized expression.
4389 *
4390 * Return OK or FAIL.
4391 */
4392 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004393eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 int evaluate;
4397{
Bram Moolenaar33570922005-01-25 22:26:29 +00004398 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 char_u *p;
4400 int i;
4401 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004402 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 int len = 2;
4404 long n1, n2;
4405 char_u *s1, *s2;
4406 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4407 regmatch_T regmatch;
4408 int ic;
4409 char_u *save_cpo;
4410
4411 /*
4412 * Get the first variable.
4413 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004414 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 return FAIL;
4416
4417 p = *arg;
4418 switch (p[0])
4419 {
4420 case '=': if (p[1] == '=')
4421 type = TYPE_EQUAL;
4422 else if (p[1] == '~')
4423 type = TYPE_MATCH;
4424 break;
4425 case '!': if (p[1] == '=')
4426 type = TYPE_NEQUAL;
4427 else if (p[1] == '~')
4428 type = TYPE_NOMATCH;
4429 break;
4430 case '>': if (p[1] != '=')
4431 {
4432 type = TYPE_GREATER;
4433 len = 1;
4434 }
4435 else
4436 type = TYPE_GEQUAL;
4437 break;
4438 case '<': if (p[1] != '=')
4439 {
4440 type = TYPE_SMALLER;
4441 len = 1;
4442 }
4443 else
4444 type = TYPE_SEQUAL;
4445 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004446 case 'i': if (p[1] == 's')
4447 {
4448 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4449 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004450 i = p[len];
4451 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004452 {
4453 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4454 type_is = TRUE;
4455 }
4456 }
4457 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 }
4459
4460 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004461 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 */
4463 if (type != TYPE_UNKNOWN)
4464 {
4465 /* extra question mark appended: ignore case */
4466 if (p[len] == '?')
4467 {
4468 ic = TRUE;
4469 ++len;
4470 }
4471 /* extra '#' appended: match case */
4472 else if (p[len] == '#')
4473 {
4474 ic = FALSE;
4475 ++len;
4476 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004477 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 else
4479 ic = p_ic;
4480
4481 /*
4482 * Get the second variable.
4483 */
4484 *arg = skipwhite(p + len);
4485 if (eval5(arg, &var2, evaluate) == FAIL)
4486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004487 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 return FAIL;
4489 }
4490
4491 if (evaluate)
4492 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004493 if (type_is && rettv->v_type != var2.v_type)
4494 {
4495 /* For "is" a different type always means FALSE, for "notis"
4496 * it means TRUE. */
4497 n1 = (type == TYPE_NEQUAL);
4498 }
4499 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4500 {
4501 if (type_is)
4502 {
4503 n1 = (rettv->v_type == var2.v_type
4504 && rettv->vval.v_list == var2.vval.v_list);
4505 if (type == TYPE_NEQUAL)
4506 n1 = !n1;
4507 }
4508 else if (rettv->v_type != var2.v_type
4509 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4510 {
4511 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004512 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004514 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004515 clear_tv(rettv);
4516 clear_tv(&var2);
4517 return FAIL;
4518 }
4519 else
4520 {
4521 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004522 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4523 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004524 if (type == TYPE_NEQUAL)
4525 n1 = !n1;
4526 }
4527 }
4528
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004529 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4530 {
4531 if (type_is)
4532 {
4533 n1 = (rettv->v_type == var2.v_type
4534 && rettv->vval.v_dict == var2.vval.v_dict);
4535 if (type == TYPE_NEQUAL)
4536 n1 = !n1;
4537 }
4538 else if (rettv->v_type != var2.v_type
4539 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4540 {
4541 if (rettv->v_type != var2.v_type)
4542 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4543 else
4544 EMSG(_("E736: Invalid operation for Dictionary"));
4545 clear_tv(rettv);
4546 clear_tv(&var2);
4547 return FAIL;
4548 }
4549 else
4550 {
4551 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004552 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4553 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004554 if (type == TYPE_NEQUAL)
4555 n1 = !n1;
4556 }
4557 }
4558
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4560 {
4561 if (rettv->v_type != var2.v_type
4562 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4563 {
4564 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004565 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004566 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004567 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004568 clear_tv(rettv);
4569 clear_tv(&var2);
4570 return FAIL;
4571 }
4572 else
4573 {
4574 /* Compare two Funcrefs for being equal or unequal. */
4575 if (rettv->vval.v_string == NULL
4576 || var2.vval.v_string == NULL)
4577 n1 = FALSE;
4578 else
4579 n1 = STRCMP(rettv->vval.v_string,
4580 var2.vval.v_string) == 0;
4581 if (type == TYPE_NEQUAL)
4582 n1 = !n1;
4583 }
4584 }
4585
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004586#ifdef FEAT_FLOAT
4587 /*
4588 * If one of the two variables is a float, compare as a float.
4589 * When using "=~" or "!~", always compare as string.
4590 */
4591 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4592 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4593 {
4594 float_T f1, f2;
4595
4596 if (rettv->v_type == VAR_FLOAT)
4597 f1 = rettv->vval.v_float;
4598 else
4599 f1 = get_tv_number(rettv);
4600 if (var2.v_type == VAR_FLOAT)
4601 f2 = var2.vval.v_float;
4602 else
4603 f2 = get_tv_number(&var2);
4604 n1 = FALSE;
4605 switch (type)
4606 {
4607 case TYPE_EQUAL: n1 = (f1 == f2); break;
4608 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4609 case TYPE_GREATER: n1 = (f1 > f2); break;
4610 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4611 case TYPE_SMALLER: n1 = (f1 < f2); break;
4612 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4613 case TYPE_UNKNOWN:
4614 case TYPE_MATCH:
4615 case TYPE_NOMATCH: break; /* avoid gcc warning */
4616 }
4617 }
4618#endif
4619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 /*
4621 * If one of the two variables is a number, compare as a number.
4622 * When using "=~" or "!~", always compare as string.
4623 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004624 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4626 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004627 n1 = get_tv_number(rettv);
4628 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 switch (type)
4630 {
4631 case TYPE_EQUAL: n1 = (n1 == n2); break;
4632 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4633 case TYPE_GREATER: n1 = (n1 > n2); break;
4634 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4635 case TYPE_SMALLER: n1 = (n1 < n2); break;
4636 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4637 case TYPE_UNKNOWN:
4638 case TYPE_MATCH:
4639 case TYPE_NOMATCH: break; /* avoid gcc warning */
4640 }
4641 }
4642 else
4643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 s1 = get_tv_string_buf(rettv, buf1);
4645 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4647 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4648 else
4649 i = 0;
4650 n1 = FALSE;
4651 switch (type)
4652 {
4653 case TYPE_EQUAL: n1 = (i == 0); break;
4654 case TYPE_NEQUAL: n1 = (i != 0); break;
4655 case TYPE_GREATER: n1 = (i > 0); break;
4656 case TYPE_GEQUAL: n1 = (i >= 0); break;
4657 case TYPE_SMALLER: n1 = (i < 0); break;
4658 case TYPE_SEQUAL: n1 = (i <= 0); break;
4659
4660 case TYPE_MATCH:
4661 case TYPE_NOMATCH:
4662 /* avoid 'l' flag in 'cpoptions' */
4663 save_cpo = p_cpo;
4664 p_cpo = (char_u *)"";
4665 regmatch.regprog = vim_regcomp(s2,
4666 RE_MAGIC + RE_STRING);
4667 regmatch.rm_ic = ic;
4668 if (regmatch.regprog != NULL)
4669 {
4670 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004671 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (type == TYPE_NOMATCH)
4673 n1 = !n1;
4674 }
4675 p_cpo = save_cpo;
4676 break;
4677
4678 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4679 }
4680 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004681 clear_tv(rettv);
4682 clear_tv(&var2);
4683 rettv->v_type = VAR_NUMBER;
4684 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 }
4686 }
4687
4688 return OK;
4689}
4690
4691/*
4692 * Handle fourth level expression:
4693 * + number addition
4694 * - number subtraction
4695 * . string concatenation
4696 *
4697 * "arg" must point to the first non-white of the expression.
4698 * "arg" is advanced to the next non-white after the recognized expression.
4699 *
4700 * Return OK or FAIL.
4701 */
4702 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004703eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 int evaluate;
4707{
Bram Moolenaar33570922005-01-25 22:26:29 +00004708 typval_T var2;
4709 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 int op;
4711 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712#ifdef FEAT_FLOAT
4713 float_T f1 = 0, f2 = 0;
4714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 char_u *s1, *s2;
4716 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4717 char_u *p;
4718
4719 /*
4720 * Get the first variable.
4721 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004722 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 return FAIL;
4724
4725 /*
4726 * Repeat computing, until no '+', '-' or '.' is following.
4727 */
4728 for (;;)
4729 {
4730 op = **arg;
4731 if (op != '+' && op != '-' && op != '.')
4732 break;
4733
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734 if ((op != '+' || rettv->v_type != VAR_LIST)
4735#ifdef FEAT_FLOAT
4736 && (op == '.' || rettv->v_type != VAR_FLOAT)
4737#endif
4738 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004739 {
4740 /* For "list + ...", an illegal use of the first operand as
4741 * a number cannot be determined before evaluating the 2nd
4742 * operand: if this is also a list, all is ok.
4743 * For "something . ...", "something - ..." or "non-list + ...",
4744 * we know that the first operand needs to be a string or number
4745 * without evaluating the 2nd operand. So check before to avoid
4746 * side effects after an error. */
4747 if (evaluate && get_tv_string_chk(rettv) == NULL)
4748 {
4749 clear_tv(rettv);
4750 return FAIL;
4751 }
4752 }
4753
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 /*
4755 * Get the second variable.
4756 */
4757 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004758 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004760 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 return FAIL;
4762 }
4763
4764 if (evaluate)
4765 {
4766 /*
4767 * Compute the result.
4768 */
4769 if (op == '.')
4770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004771 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4772 s2 = get_tv_string_buf_chk(&var2, buf2);
4773 if (s2 == NULL) /* type error ? */
4774 {
4775 clear_tv(rettv);
4776 clear_tv(&var2);
4777 return FAIL;
4778 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004779 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 clear_tv(rettv);
4781 rettv->v_type = VAR_STRING;
4782 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004784 else if (op == '+' && rettv->v_type == VAR_LIST
4785 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004786 {
4787 /* concatenate Lists */
4788 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4789 &var3) == FAIL)
4790 {
4791 clear_tv(rettv);
4792 clear_tv(&var2);
4793 return FAIL;
4794 }
4795 clear_tv(rettv);
4796 *rettv = var3;
4797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 else
4799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 int error = FALSE;
4801
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802#ifdef FEAT_FLOAT
4803 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004804 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805 f1 = rettv->vval.v_float;
4806 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808 else
4809#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811 n1 = get_tv_number_chk(rettv, &error);
4812 if (error)
4813 {
4814 /* This can only happen for "list + non-list". For
4815 * "non-list + ..." or "something - ...", we returned
4816 * before evaluating the 2nd operand. */
4817 clear_tv(rettv);
4818 return FAIL;
4819 }
4820#ifdef FEAT_FLOAT
4821 if (var2.v_type == VAR_FLOAT)
4822 f1 = n1;
4823#endif
4824 }
4825#ifdef FEAT_FLOAT
4826 if (var2.v_type == VAR_FLOAT)
4827 {
4828 f2 = var2.vval.v_float;
4829 n2 = 0;
4830 }
4831 else
4832#endif
4833 {
4834 n2 = get_tv_number_chk(&var2, &error);
4835 if (error)
4836 {
4837 clear_tv(rettv);
4838 clear_tv(&var2);
4839 return FAIL;
4840 }
4841#ifdef FEAT_FLOAT
4842 if (rettv->v_type == VAR_FLOAT)
4843 f2 = n2;
4844#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004847
4848#ifdef FEAT_FLOAT
4849 /* If there is a float on either side the result is a float. */
4850 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4851 {
4852 if (op == '+')
4853 f1 = f1 + f2;
4854 else
4855 f1 = f1 - f2;
4856 rettv->v_type = VAR_FLOAT;
4857 rettv->vval.v_float = f1;
4858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004860#endif
4861 {
4862 if (op == '+')
4863 n1 = n1 + n2;
4864 else
4865 n1 = n1 - n2;
4866 rettv->v_type = VAR_NUMBER;
4867 rettv->vval.v_number = n1;
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 }
4873 return OK;
4874}
4875
4876/*
4877 * Handle fifth level expression:
4878 * * number multiplication
4879 * / number division
4880 * % number modulo
4881 *
4882 * "arg" must point to the first non-white of the expression.
4883 * "arg" is advanced to the next non-white after the recognized expression.
4884 *
4885 * Return OK or FAIL.
4886 */
4887 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004888eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004892 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893{
Bram Moolenaar33570922005-01-25 22:26:29 +00004894 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 int op;
4896 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897#ifdef FEAT_FLOAT
4898 int use_float = FALSE;
4899 float_T f1 = 0, f2;
4900#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004901 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
4903 /*
4904 * Get the first variable.
4905 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004906 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 return FAIL;
4908
4909 /*
4910 * Repeat computing, until no '*', '/' or '%' is following.
4911 */
4912 for (;;)
4913 {
4914 op = **arg;
4915 if (op != '*' && op != '/' && op != '%')
4916 break;
4917
4918 if (evaluate)
4919 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920#ifdef FEAT_FLOAT
4921 if (rettv->v_type == VAR_FLOAT)
4922 {
4923 f1 = rettv->vval.v_float;
4924 use_float = TRUE;
4925 n1 = 0;
4926 }
4927 else
4928#endif
4929 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004930 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004931 if (error)
4932 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 else
4935 n1 = 0;
4936
4937 /*
4938 * Get the second variable.
4939 */
4940 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004941 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 return FAIL;
4943
4944 if (evaluate)
4945 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004946#ifdef FEAT_FLOAT
4947 if (var2.v_type == VAR_FLOAT)
4948 {
4949 if (!use_float)
4950 {
4951 f1 = n1;
4952 use_float = TRUE;
4953 }
4954 f2 = var2.vval.v_float;
4955 n2 = 0;
4956 }
4957 else
4958#endif
4959 {
4960 n2 = get_tv_number_chk(&var2, &error);
4961 clear_tv(&var2);
4962 if (error)
4963 return FAIL;
4964#ifdef FEAT_FLOAT
4965 if (use_float)
4966 f2 = n2;
4967#endif
4968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
4970 /*
4971 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974#ifdef FEAT_FLOAT
4975 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977 if (op == '*')
4978 f1 = f1 * f2;
4979 else if (op == '/')
4980 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981# ifdef VMS
4982 /* VMS crashes on divide by zero, work around it */
4983 if (f2 == 0.0)
4984 {
4985 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004986 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004987 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004988 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004989 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004990 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004991 }
4992 else
4993 f1 = f1 / f2;
4994# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 /* We rely on the floating point library to handle divide
4996 * by zero to result in "inf" and not a crash. */
4997 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004998# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005002 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005003 return FAIL;
5004 }
5005 rettv->v_type = VAR_FLOAT;
5006 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
5008 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 if (op == '*')
5012 n1 = n1 * n2;
5013 else if (op == '/')
5014 {
5015 if (n2 == 0) /* give an error message? */
5016 {
5017 if (n1 == 0)
5018 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5019 else if (n1 < 0)
5020 n1 = -0x7fffffffL;
5021 else
5022 n1 = 0x7fffffffL;
5023 }
5024 else
5025 n1 = n1 / n2;
5026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005028 {
5029 if (n2 == 0) /* give an error message? */
5030 n1 = 0;
5031 else
5032 n1 = n1 % n2;
5033 }
5034 rettv->v_type = VAR_NUMBER;
5035 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038 }
5039
5040 return OK;
5041}
5042
5043/*
5044 * Handle sixth level expression:
5045 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005046 * "string" string constant
5047 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 * &option-name option value
5049 * @r register contents
5050 * identifier variable value
5051 * function() function call
5052 * $VAR environment variable
5053 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005054 * [expr, expr] List
5055 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 *
5057 * Also handle:
5058 * ! in front logical NOT
5059 * - in front unary minus
5060 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 * trailing [] subscript in String or List
5062 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 *
5064 * "arg" must point to the first non-white of the expression.
5065 * "arg" is advanced to the next non-white after the recognized expression.
5066 *
5067 * Return OK or FAIL.
5068 */
5069 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005070eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005074 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 long n;
5077 int len;
5078 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 char_u *start_leader, *end_leader;
5080 int ret = OK;
5081 char_u *alias;
5082
5083 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088
5089 /*
5090 * Skip '!' and '-' characters. They are handled later.
5091 */
5092 start_leader = *arg;
5093 while (**arg == '!' || **arg == '-' || **arg == '+')
5094 *arg = skipwhite(*arg + 1);
5095 end_leader = *arg;
5096
5097 switch (**arg)
5098 {
5099 /*
5100 * Number constant.
5101 */
5102 case '0':
5103 case '1':
5104 case '2':
5105 case '3':
5106 case '4':
5107 case '5':
5108 case '6':
5109 case '7':
5110 case '8':
5111 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005112 {
5113#ifdef FEAT_FLOAT
5114 char_u *p = skipdigits(*arg + 1);
5115 int get_float = FALSE;
5116
5117 /* We accept a float when the format matches
5118 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005119 * strict to avoid backwards compatibility problems.
5120 * Don't look for a float after the "." operator, so that
5121 * ":let vers = 1.2.3" doesn't fail. */
5122 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005124 get_float = TRUE;
5125 p = skipdigits(p + 2);
5126 if (*p == 'e' || *p == 'E')
5127 {
5128 ++p;
5129 if (*p == '-' || *p == '+')
5130 ++p;
5131 if (!vim_isdigit(*p))
5132 get_float = FALSE;
5133 else
5134 p = skipdigits(p + 1);
5135 }
5136 if (ASCII_ISALPHA(*p) || *p == '.')
5137 get_float = FALSE;
5138 }
5139 if (get_float)
5140 {
5141 float_T f;
5142
5143 *arg += string2float(*arg, &f);
5144 if (evaluate)
5145 {
5146 rettv->v_type = VAR_FLOAT;
5147 rettv->vval.v_float = f;
5148 }
5149 }
5150 else
5151#endif
5152 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005153 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005154 *arg += len;
5155 if (evaluate)
5156 {
5157 rettv->v_type = VAR_NUMBER;
5158 rettv->vval.v_number = n;
5159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
5161 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
5164 /*
5165 * String constant: "string".
5166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 break;
5169
5170 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 break;
5175
5176 /*
5177 * List: [expr, expr]
5178 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 break;
5181
5182 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 * Dictionary: {key: val, key: val}
5184 */
5185 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5186 break;
5187
5188 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005189 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005191 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 break;
5193
5194 /*
5195 * Environment variable: $VAR.
5196 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005197 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 break;
5199
5200 /*
5201 * Register contents: @r.
5202 */
5203 case '@': ++*arg;
5204 if (evaluate)
5205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005207 rettv->vval.v_string = get_reg_contents(**arg,
5208 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210 if (**arg != NUL)
5211 ++*arg;
5212 break;
5213
5214 /*
5215 * nested expression: (expression).
5216 */
5217 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005218 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 if (**arg == ')')
5220 ++*arg;
5221 else if (ret == OK)
5222 {
5223 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005224 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 ret = FAIL;
5226 }
5227 break;
5228
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 break;
5231 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232
5233 if (ret == NOTDONE)
5234 {
5235 /*
5236 * Must be a variable or function name.
5237 * Can also be a curly-braces kind of name: {expr}.
5238 */
5239 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005240 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 if (alias != NULL)
5242 s = alias;
5243
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005244 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 ret = FAIL;
5246 else
5247 {
5248 if (**arg == '(') /* recursive! */
5249 {
5250 /* If "s" is the name of a variable of type VAR_FUNC
5251 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005252 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253
5254 /* Invoke the function. */
5255 ret = get_func_tv(s, len, rettv, arg,
5256 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005257 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005258
5259 /* If evaluate is FALSE rettv->v_type was not set in
5260 * get_func_tv, but it's needed in handle_subscript() to parse
5261 * what follows. So set it here. */
5262 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5263 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005264 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005265 rettv->v_type = VAR_FUNC;
5266 }
5267
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 /* Stop the expression evaluation when immediately
5269 * aborting on error, or when an interrupt occurred or
5270 * an exception was thrown but not caught. */
5271 if (aborting())
5272 {
5273 if (ret == OK)
5274 clear_tv(rettv);
5275 ret = FAIL;
5276 }
5277 }
5278 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005279 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005280 else
5281 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005283 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 }
5285
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 *arg = skipwhite(*arg);
5287
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5289 * expr(expr). */
5290 if (ret == OK)
5291 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292
5293 /*
5294 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5295 */
5296 if (ret == OK && evaluate && end_leader > start_leader)
5297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005299 int val = 0;
5300#ifdef FEAT_FLOAT
5301 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 if (rettv->v_type == VAR_FLOAT)
5304 f = rettv->vval.v_float;
5305 else
5306#endif
5307 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 clear_tv(rettv);
5311 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 else
5314 {
5315 while (end_leader > start_leader)
5316 {
5317 --end_leader;
5318 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005319 {
5320#ifdef FEAT_FLOAT
5321 if (rettv->v_type == VAR_FLOAT)
5322 f = !f;
5323 else
5324#endif
5325 val = !val;
5326 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005328 {
5329#ifdef FEAT_FLOAT
5330 if (rettv->v_type == VAR_FLOAT)
5331 f = -f;
5332 else
5333#endif
5334 val = -val;
5335 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005336 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005337#ifdef FEAT_FLOAT
5338 if (rettv->v_type == VAR_FLOAT)
5339 {
5340 clear_tv(rettv);
5341 rettv->vval.v_float = f;
5342 }
5343 else
5344#endif
5345 {
5346 clear_tv(rettv);
5347 rettv->v_type = VAR_NUMBER;
5348 rettv->vval.v_number = val;
5349 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
5352
5353 return ret;
5354}
5355
5356/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005357 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5358 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5360 */
5361 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005362eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005364 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005366 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367{
5368 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005369 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371 long len = -1;
5372 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005376 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005378 if (verbose)
5379 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 return FAIL;
5381 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005382#ifdef FEAT_FLOAT
5383 else if (rettv->v_type == VAR_FLOAT)
5384 {
5385 if (verbose)
5386 EMSG(_(e_float_as_string));
5387 return FAIL;
5388 }
5389#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005391 init_tv(&var1);
5392 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 /*
5396 * dict.name
5397 */
5398 key = *arg + 1;
5399 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5400 ;
5401 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005403 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 }
5405 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 /*
5408 * something[idx]
5409 *
5410 * Get the (first) variable from inside the [].
5411 */
5412 *arg = skipwhite(*arg + 1);
5413 if (**arg == ':')
5414 empty1 = TRUE;
5415 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5416 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005417 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5418 {
5419 /* not a number or string */
5420 clear_tv(&var1);
5421 return FAIL;
5422 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005423
5424 /*
5425 * Get the second variable from inside the [:].
5426 */
5427 if (**arg == ':')
5428 {
5429 range = TRUE;
5430 *arg = skipwhite(*arg + 1);
5431 if (**arg == ']')
5432 empty2 = TRUE;
5433 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005435 if (!empty1)
5436 clear_tv(&var1);
5437 return FAIL;
5438 }
5439 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5440 {
5441 /* not a number or string */
5442 if (!empty1)
5443 clear_tv(&var1);
5444 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 return FAIL;
5446 }
5447 }
5448
5449 /* Check for the ']'. */
5450 if (**arg != ']')
5451 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005452 if (verbose)
5453 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005454 clear_tv(&var1);
5455 if (range)
5456 clear_tv(&var2);
5457 return FAIL;
5458 }
5459 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461
5462 if (evaluate)
5463 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464 n1 = 0;
5465 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 n1 = get_tv_number(&var1);
5468 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 }
5470 if (range)
5471 {
5472 if (empty2)
5473 n2 = -1;
5474 else
5475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005476 n2 = get_tv_number(&var2);
5477 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 }
5479 }
5480
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 {
5483 case VAR_NUMBER:
5484 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 len = (long)STRLEN(s);
5487 if (range)
5488 {
5489 /* The resulting variable is a substring. If the indexes
5490 * are out of range the result is empty. */
5491 if (n1 < 0)
5492 {
5493 n1 = len + n1;
5494 if (n1 < 0)
5495 n1 = 0;
5496 }
5497 if (n2 < 0)
5498 n2 = len + n2;
5499 else if (n2 >= len)
5500 n2 = len;
5501 if (n1 >= len || n2 < 0 || n1 > n2)
5502 s = NULL;
5503 else
5504 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5505 }
5506 else
5507 {
5508 /* The resulting variable is a string of a single
5509 * character. If the index is too big or negative the
5510 * result is empty. */
5511 if (n1 >= len || n1 < 0)
5512 s = NULL;
5513 else
5514 s = vim_strnsave(s + n1, 1);
5515 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516 clear_tv(rettv);
5517 rettv->v_type = VAR_STRING;
5518 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 break;
5520
5521 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 if (n1 < 0)
5524 n1 = len + n1;
5525 if (!empty1 && (n1 < 0 || n1 >= len))
5526 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005527 /* For a range we allow invalid values and return an empty
5528 * list. A list index out of range is an error. */
5529 if (!range)
5530 {
5531 if (verbose)
5532 EMSGN(_(e_listidx), n1);
5533 return FAIL;
5534 }
5535 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 }
5537 if (range)
5538 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005539 list_T *l;
5540 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541
5542 if (n2 < 0)
5543 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005544 else if (n2 >= len)
5545 n2 = len - 1;
5546 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005547 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 l = list_alloc();
5549 if (l == NULL)
5550 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 n1 <= n2; ++n1)
5553 {
5554 if (list_append_tv(l, &item->li_tv) == FAIL)
5555 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005556 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 return FAIL;
5558 }
5559 item = item->li_next;
5560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 clear_tv(rettv);
5562 rettv->v_type = VAR_LIST;
5563 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005564 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 }
5566 else
5567 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005568 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 clear_tv(rettv);
5570 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 }
5572 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573
5574 case VAR_DICT:
5575 if (range)
5576 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005577 if (verbose)
5578 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579 if (len == -1)
5580 clear_tv(&var1);
5581 return FAIL;
5582 }
5583 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005584 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585
5586 if (len == -1)
5587 {
5588 key = get_tv_string(&var1);
5589 if (*key == NUL)
5590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005591 if (verbose)
5592 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 clear_tv(&var1);
5594 return FAIL;
5595 }
5596 }
5597
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005598 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005600 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005601 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602 if (len == -1)
5603 clear_tv(&var1);
5604 if (item == NULL)
5605 return FAIL;
5606
5607 copy_tv(&item->di_tv, &var1);
5608 clear_tv(rettv);
5609 *rettv = var1;
5610 }
5611 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 }
5613 }
5614
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615 return OK;
5616}
5617
5618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 * Get an option value.
5620 * "arg" points to the '&' or '+' before the option name.
5621 * "arg" is advanced to character after the option name.
5622 * Return OK or FAIL.
5623 */
5624 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005625get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 int evaluate;
5629{
5630 char_u *option_end;
5631 long numval;
5632 char_u *stringval;
5633 int opt_type;
5634 int c;
5635 int working = (**arg == '+'); /* has("+option") */
5636 int ret = OK;
5637 int opt_flags;
5638
5639 /*
5640 * Isolate the option name and find its value.
5641 */
5642 option_end = find_option_end(arg, &opt_flags);
5643 if (option_end == NULL)
5644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005645 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 EMSG2(_("E112: Option name missing: %s"), *arg);
5647 return FAIL;
5648 }
5649
5650 if (!evaluate)
5651 {
5652 *arg = option_end;
5653 return OK;
5654 }
5655
5656 c = *option_end;
5657 *option_end = NUL;
5658 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
5661 if (opt_type == -3) /* invalid name */
5662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 EMSG2(_("E113: Unknown option: %s"), *arg);
5665 ret = FAIL;
5666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 if (opt_type == -2) /* hidden string option */
5670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 rettv->v_type = VAR_STRING;
5672 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674 else if (opt_type == -1) /* hidden number option */
5675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 rettv->v_type = VAR_NUMBER;
5677 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679 else if (opt_type == 1) /* number option */
5680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 rettv->v_type = VAR_NUMBER;
5682 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684 else /* string option */
5685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 rettv->v_type = VAR_STRING;
5687 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 }
5690 else if (working && (opt_type == -2 || opt_type == -1))
5691 ret = FAIL;
5692
5693 *option_end = c; /* put back for error messages */
5694 *arg = option_end;
5695
5696 return ret;
5697}
5698
5699/*
5700 * Allocate a variable for a string constant.
5701 * Return OK or FAIL.
5702 */
5703 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 int evaluate;
5708{
5709 char_u *p;
5710 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 int extra = 0;
5712
5713 /*
5714 * Find the end of the string, skipping backslashed characters.
5715 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005716 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
5718 if (*p == '\\' && p[1] != NUL)
5719 {
5720 ++p;
5721 /* A "\<x>" form occupies at least 4 characters, and produces up
5722 * to 6 characters: reserve space for 2 extra */
5723 if (*p == '<')
5724 extra += 2;
5725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
5727
5728 if (*p != '"')
5729 {
5730 EMSG2(_("E114: Missing quote: %s"), *arg);
5731 return FAIL;
5732 }
5733
5734 /* If only parsing, set *arg and return here */
5735 if (!evaluate)
5736 {
5737 *arg = p + 1;
5738 return OK;
5739 }
5740
5741 /*
5742 * Copy the string into allocated memory, handling backslashed
5743 * characters.
5744 */
5745 name = alloc((unsigned)(p - *arg + extra));
5746 if (name == NULL)
5747 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 rettv->v_type = VAR_STRING;
5749 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
5753 if (*p == '\\')
5754 {
5755 switch (*++p)
5756 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 case 'b': *name++ = BS; ++p; break;
5758 case 'e': *name++ = ESC; ++p; break;
5759 case 'f': *name++ = FF; ++p; break;
5760 case 'n': *name++ = NL; ++p; break;
5761 case 'r': *name++ = CAR; ++p; break;
5762 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
5764 case 'X': /* hex: "\x1", "\x12" */
5765 case 'x':
5766 case 'u': /* Unicode: "\u0023" */
5767 case 'U':
5768 if (vim_isxdigit(p[1]))
5769 {
5770 int n, nr;
5771 int c = toupper(*p);
5772
5773 if (c == 'X')
5774 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005775 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005777 else
5778 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 nr = 0;
5780 while (--n >= 0 && vim_isxdigit(p[1]))
5781 {
5782 ++p;
5783 nr = (nr << 4) + hex2nr(*p);
5784 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786#ifdef FEAT_MBYTE
5787 /* For "\u" store the number according to
5788 * 'encoding'. */
5789 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 else
5792#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 break;
5796
5797 /* octal: "\1", "\12", "\123" */
5798 case '0':
5799 case '1':
5800 case '2':
5801 case '3':
5802 case '4':
5803 case '5':
5804 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 case '7': *name = *p++ - '0';
5806 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 *name = (*name << 3) + *p++ - '0';
5809 if (*p >= '0' && *p <= '7')
5810 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 break;
5814
5815 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 if (extra != 0)
5818 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 break;
5821 }
5822 /* FALLTHROUGH */
5823
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 break;
5826 }
5827 }
5828 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 *arg = p + 1;
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 return OK;
5836}
5837
5838/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 * Return OK or FAIL.
5841 */
5842 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005843get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 int evaluate;
5847{
5848 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 int reduce = 0;
5851
5852 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 */
5855 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 break;
5861 ++reduce;
5862 ++p;
5863 }
5864 }
5865
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 return FAIL;
5870 }
5871
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 if (!evaluate)
5874 {
5875 *arg = p + 1;
5876 return OK;
5877 }
5878
5879 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 */
5882 str = alloc((unsigned)((p - *arg) - reduce));
5883 if (str == NULL)
5884 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 rettv->v_type = VAR_STRING;
5886 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 break;
5894 ++p;
5895 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 *arg = p + 1;
5900
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901 return OK;
5902}
5903
5904/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905 * Allocate a variable for a List and fill it from "*arg".
5906 * Return OK or FAIL.
5907 */
5908 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005909get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005911 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 int evaluate;
5913{
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 list_T *l = NULL;
5915 typval_T tv;
5916 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917
5918 if (evaluate)
5919 {
5920 l = list_alloc();
5921 if (l == NULL)
5922 return FAIL;
5923 }
5924
5925 *arg = skipwhite(*arg + 1);
5926 while (**arg != ']' && **arg != NUL)
5927 {
5928 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5929 goto failret;
5930 if (evaluate)
5931 {
5932 item = listitem_alloc();
5933 if (item != NULL)
5934 {
5935 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005936 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 list_append(l, item);
5938 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005939 else
5940 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 }
5942
5943 if (**arg == ']')
5944 break;
5945 if (**arg != ',')
5946 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005947 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 goto failret;
5949 }
5950 *arg = skipwhite(*arg + 1);
5951 }
5952
5953 if (**arg != ']')
5954 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956failret:
5957 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005958 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 return FAIL;
5960 }
5961
5962 *arg = skipwhite(*arg + 1);
5963 if (evaluate)
5964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005965 rettv->v_type = VAR_LIST;
5966 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 ++l->lv_refcount;
5968 }
5969
5970 return OK;
5971}
5972
5973/*
5974 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005975 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005977 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978list_alloc()
5979{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005980 list_T *l;
5981
5982 l = (list_T *)alloc_clear(sizeof(list_T));
5983 if (l != NULL)
5984 {
5985 /* Prepend the list to the list of lists for garbage collection. */
5986 if (first_list != NULL)
5987 first_list->lv_used_prev = l;
5988 l->lv_used_prev = NULL;
5989 l->lv_used_next = first_list;
5990 first_list = l;
5991 }
5992 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993}
5994
5995/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005996 * Allocate an empty list for a return value.
5997 * Returns OK or FAIL.
5998 */
5999 static int
6000rettv_list_alloc(rettv)
6001 typval_T *rettv;
6002{
6003 list_T *l = list_alloc();
6004
6005 if (l == NULL)
6006 return FAIL;
6007
6008 rettv->vval.v_list = l;
6009 rettv->v_type = VAR_LIST;
6010 ++l->lv_refcount;
6011 return OK;
6012}
6013
6014/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015 * Unreference a list: decrement the reference count and free it when it
6016 * becomes zero.
6017 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006018 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006020 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006022 if (l != NULL && --l->lv_refcount <= 0)
6023 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024}
6025
6026/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006027 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 * Ignores the reference count.
6029 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006030 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006031list_free(l, recurse)
6032 list_T *l;
6033 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034{
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006037 /* Remove the list from the list of lists for garbage collection. */
6038 if (l->lv_used_prev == NULL)
6039 first_list = l->lv_used_next;
6040 else
6041 l->lv_used_prev->lv_used_next = l->lv_used_next;
6042 if (l->lv_used_next != NULL)
6043 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6044
Bram Moolenaard9fba312005-06-26 22:34:35 +00006045 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006047 /* Remove the item before deleting it. */
6048 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006049 if (recurse || (item->li_tv.v_type != VAR_LIST
6050 && item->li_tv.v_type != VAR_DICT))
6051 clear_tv(&item->li_tv);
6052 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 }
6054 vim_free(l);
6055}
6056
6057/*
6058 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006059 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006061 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062listitem_alloc()
6063{
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065}
6066
6067/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006068 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006070 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006074 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075 vim_free(item);
6076}
6077
6078/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006079 * Remove a list item from a List and free it. Also clears the value.
6080 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006081 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006082listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 list_T *l;
6084 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006085{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006086 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006087 listitem_free(item);
6088}
6089
6090/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 * Get the number of items in a list.
6092 */
6093 static long
6094list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006095 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 if (l == NULL)
6098 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006099 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006100}
6101
6102/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103 * Return TRUE when two lists have exactly the same values.
6104 */
6105 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006106list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 list_T *l1;
6108 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111{
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006114 if (l1 == NULL || l2 == NULL)
6115 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006116 if (l1 == l2)
6117 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006118 if (list_len(l1) != list_len(l2))
6119 return FALSE;
6120
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 for (item1 = l1->lv_first, item2 = l2->lv_first;
6122 item1 != NULL && item2 != NULL;
6123 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125 return FALSE;
6126 return item1 == NULL && item2 == NULL;
6127}
6128
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006129#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6130 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006131/*
6132 * Return the dictitem that an entry in a hashtable points to.
6133 */
6134 dictitem_T *
6135dict_lookup(hi)
6136 hashitem_T *hi;
6137{
6138 return HI2DI(hi);
6139}
6140#endif
6141
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006142/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143 * Return TRUE when two dictionaries have exactly the same key/values.
6144 */
6145 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006146dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 dict_T *d1;
6148 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006150 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006151{
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 hashitem_T *hi;
6153 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006154 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006155
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006156 if (d1 == NULL || d2 == NULL)
6157 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006158 if (d1 == d2)
6159 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006160 if (dict_len(d1) != dict_len(d2))
6161 return FALSE;
6162
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006163 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006165 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006166 if (!HASHITEM_EMPTY(hi))
6167 {
6168 item2 = dict_find(d2, hi->hi_key, -1);
6169 if (item2 == NULL)
6170 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006171 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006172 return FALSE;
6173 --todo;
6174 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006175 }
6176 return TRUE;
6177}
6178
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006179static int tv_equal_recurse_limit;
6180
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006181/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006183 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006184 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185 */
6186 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006187tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006188 typval_T *tv1;
6189 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190 int ic; /* ignore case */
6191 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006192{
6193 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006194 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006195 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006196 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006198 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006199 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006200
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006201 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006202 * recursiveness to a limit. We guess they are equal then.
6203 * A fixed limit has the problem of still taking an awful long time.
6204 * Reduce the limit every time running into it. That should work fine for
6205 * deeply linked structures that are not recursively linked and catch
6206 * recursiveness quickly. */
6207 if (!recursive)
6208 tv_equal_recurse_limit = 1000;
6209 if (recursive_cnt >= tv_equal_recurse_limit)
6210 {
6211 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006212 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006213 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006214
6215 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006216 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006217 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006218 ++recursive_cnt;
6219 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6220 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006221 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006222
6223 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006224 ++recursive_cnt;
6225 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6226 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006227 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006228
6229 case VAR_FUNC:
6230 return (tv1->vval.v_string != NULL
6231 && tv2->vval.v_string != NULL
6232 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6233
6234 case VAR_NUMBER:
6235 return tv1->vval.v_number == tv2->vval.v_number;
6236
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006237#ifdef FEAT_FLOAT
6238 case VAR_FLOAT:
6239 return tv1->vval.v_float == tv2->vval.v_float;
6240#endif
6241
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006242 case VAR_STRING:
6243 s1 = get_tv_string_buf(tv1, buf1);
6244 s2 = get_tv_string_buf(tv2, buf2);
6245 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006246 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006247
6248 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249 return TRUE;
6250}
6251
6252/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 * Locate item with index "n" in list "l" and return it.
6254 * A negative index is counted from the end; -1 is the last item.
6255 * Returns NULL when "n" is out of range.
6256 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006257 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006258list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260 long n;
6261{
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 long idx;
6264
6265 if (l == NULL)
6266 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006267
6268 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006270 n = l->lv_len + n;
6271
6272 /* Check for index out of range. */
6273 if (n < 0 || n >= l->lv_len)
6274 return NULL;
6275
6276 /* When there is a cached index may start search from there. */
6277 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006279 if (n < l->lv_idx / 2)
6280 {
6281 /* closest to the start of the list */
6282 item = l->lv_first;
6283 idx = 0;
6284 }
6285 else if (n > (l->lv_idx + l->lv_len) / 2)
6286 {
6287 /* closest to the end of the list */
6288 item = l->lv_last;
6289 idx = l->lv_len - 1;
6290 }
6291 else
6292 {
6293 /* closest to the cached index */
6294 item = l->lv_idx_item;
6295 idx = l->lv_idx;
6296 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 }
6298 else
6299 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006300 if (n < l->lv_len / 2)
6301 {
6302 /* closest to the start of the list */
6303 item = l->lv_first;
6304 idx = 0;
6305 }
6306 else
6307 {
6308 /* closest to the end of the list */
6309 item = l->lv_last;
6310 idx = l->lv_len - 1;
6311 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313
6314 while (n > idx)
6315 {
6316 /* search forward */
6317 item = item->li_next;
6318 ++idx;
6319 }
6320 while (n < idx)
6321 {
6322 /* search backward */
6323 item = item->li_prev;
6324 --idx;
6325 }
6326
6327 /* cache the used index */
6328 l->lv_idx = idx;
6329 l->lv_idx_item = item;
6330
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 return item;
6332}
6333
6334/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006335 * Get list item "l[idx]" as a number.
6336 */
6337 static long
6338list_find_nr(l, idx, errorp)
6339 list_T *l;
6340 long idx;
6341 int *errorp; /* set to TRUE when something wrong */
6342{
6343 listitem_T *li;
6344
6345 li = list_find(l, idx);
6346 if (li == NULL)
6347 {
6348 if (errorp != NULL)
6349 *errorp = TRUE;
6350 return -1L;
6351 }
6352 return get_tv_number_chk(&li->li_tv, errorp);
6353}
6354
6355/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006356 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6357 */
6358 char_u *
6359list_find_str(l, idx)
6360 list_T *l;
6361 long idx;
6362{
6363 listitem_T *li;
6364
6365 li = list_find(l, idx - 1);
6366 if (li == NULL)
6367 {
6368 EMSGN(_(e_listidx), idx);
6369 return NULL;
6370 }
6371 return get_tv_string(&li->li_tv);
6372}
6373
6374/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006375 * Locate "item" list "l" and return its index.
6376 * Returns -1 when "item" is not in the list.
6377 */
6378 static long
6379list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 list_T *l;
6381 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006382{
6383 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006385
6386 if (l == NULL)
6387 return -1;
6388 idx = 0;
6389 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6390 ++idx;
6391 if (li == NULL)
6392 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006393 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006394}
6395
6396/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397 * Append item "item" to the end of list "l".
6398 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006399 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 list_T *l;
6402 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403{
6404 if (l->lv_last == NULL)
6405 {
6406 /* empty list */
6407 l->lv_first = item;
6408 l->lv_last = item;
6409 item->li_prev = NULL;
6410 }
6411 else
6412 {
6413 l->lv_last->li_next = item;
6414 item->li_prev = l->lv_last;
6415 l->lv_last = item;
6416 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006417 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 item->li_next = NULL;
6419}
6420
6421/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006425 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006427 list_T *l;
6428 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431
Bram Moolenaar05159a02005-02-26 23:04:13 +00006432 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434 copy_tv(tv, &li->li_tv);
6435 list_append(l, li);
6436 return OK;
6437}
6438
6439/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006440 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006441 * Return FAIL when out of memory.
6442 */
6443 int
6444list_append_dict(list, dict)
6445 list_T *list;
6446 dict_T *dict;
6447{
6448 listitem_T *li = listitem_alloc();
6449
6450 if (li == NULL)
6451 return FAIL;
6452 li->li_tv.v_type = VAR_DICT;
6453 li->li_tv.v_lock = 0;
6454 li->li_tv.vval.v_dict = dict;
6455 list_append(list, li);
6456 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457 return OK;
6458}
6459
6460/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006461 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006462 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006463 * Returns FAIL when out of memory.
6464 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006465 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006466list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006467 list_T *l;
6468 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006469 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006470{
6471 listitem_T *li = listitem_alloc();
6472
6473 if (li == NULL)
6474 return FAIL;
6475 list_append(l, li);
6476 li->li_tv.v_type = VAR_STRING;
6477 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006478 if (str == NULL)
6479 li->li_tv.vval.v_string = NULL;
6480 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006481 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006482 return FAIL;
6483 return OK;
6484}
6485
6486/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006487 * Append "n" to list "l".
6488 * Returns FAIL when out of memory.
6489 */
6490 static int
6491list_append_number(l, n)
6492 list_T *l;
6493 varnumber_T n;
6494{
6495 listitem_T *li;
6496
6497 li = listitem_alloc();
6498 if (li == NULL)
6499 return FAIL;
6500 li->li_tv.v_type = VAR_NUMBER;
6501 li->li_tv.v_lock = 0;
6502 li->li_tv.vval.v_number = n;
6503 list_append(l, li);
6504 return OK;
6505}
6506
6507/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509 * If "item" is NULL append at the end.
6510 * Return FAIL when out of memory.
6511 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006512 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006513list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006514 list_T *l;
6515 typval_T *tv;
6516 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517{
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519
6520 if (ni == NULL)
6521 return FAIL;
6522 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006523 list_insert(l, ni, item);
6524 return OK;
6525}
6526
6527 void
6528list_insert(l, ni, item)
6529 list_T *l;
6530 listitem_T *ni;
6531 listitem_T *item;
6532{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 if (item == NULL)
6534 /* Append new item at end of list. */
6535 list_append(l, ni);
6536 else
6537 {
6538 /* Insert new item before existing item. */
6539 ni->li_prev = item->li_prev;
6540 ni->li_next = item;
6541 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006542 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006544 ++l->lv_idx;
6545 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006547 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006549 l->lv_idx_item = NULL;
6550 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006552 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554}
6555
6556/*
6557 * Extend "l1" with "l2".
6558 * If "bef" is NULL append at the end, otherwise insert before this item.
6559 * Returns FAIL when out of memory.
6560 */
6561 static int
6562list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 list_T *l1;
6564 list_T *l2;
6565 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566{
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006568 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006570 /* We also quit the loop when we have inserted the original item count of
6571 * the list, avoid a hang when we extend a list with itself. */
6572 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6574 return FAIL;
6575 return OK;
6576}
6577
6578/*
6579 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6580 * Return FAIL when out of memory.
6581 */
6582 static int
6583list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 list_T *l1;
6585 list_T *l2;
6586 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006587{
Bram Moolenaar33570922005-01-25 22:26:29 +00006588 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006590 if (l1 == NULL || l2 == NULL)
6591 return FAIL;
6592
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006595 if (l == NULL)
6596 return FAIL;
6597 tv->v_type = VAR_LIST;
6598 tv->vval.v_list = l;
6599
6600 /* append all items from the second list */
6601 return list_extend(l, l2, NULL);
6602}
6603
6604/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006605 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608 * Returns NULL when out of memory.
6609 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006610 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006611list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006612 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006614 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615{
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 list_T *copy;
6617 listitem_T *item;
6618 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619
6620 if (orig == NULL)
6621 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622
6623 copy = list_alloc();
6624 if (copy != NULL)
6625 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006626 if (copyID != 0)
6627 {
6628 /* Do this before adding the items, because one of the items may
6629 * refer back to this list. */
6630 orig->lv_copyID = copyID;
6631 orig->lv_copylist = copy;
6632 }
6633 for (item = orig->lv_first; item != NULL && !got_int;
6634 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 {
6636 ni = listitem_alloc();
6637 if (ni == NULL)
6638 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006639 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006640 {
6641 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6642 {
6643 vim_free(ni);
6644 break;
6645 }
6646 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006648 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 list_append(copy, ni);
6650 }
6651 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006652 if (item != NULL)
6653 {
6654 list_unref(copy);
6655 copy = NULL;
6656 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657 }
6658
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 return copy;
6660}
6661
6662/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006664 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006665 * This used to be called list_remove, but that conflicts with a Sun header
6666 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006668 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006669vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006670 list_T *l;
6671 listitem_T *item;
6672 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673{
Bram Moolenaar33570922005-01-25 22:26:29 +00006674 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006676 /* notify watchers */
6677 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006679 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006680 list_fix_watch(l, ip);
6681 if (ip == item2)
6682 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006684
6685 if (item2->li_next == NULL)
6686 l->lv_last = item->li_prev;
6687 else
6688 item2->li_next->li_prev = item->li_prev;
6689 if (item->li_prev == NULL)
6690 l->lv_first = item2->li_next;
6691 else
6692 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006693 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694}
6695
6696/*
6697 * Return an allocated string with the string representation of a list.
6698 * May return NULL.
6699 */
6700 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006701list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006703 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704{
6705 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006706
6707 if (tv->vval.v_list == NULL)
6708 return NULL;
6709 ga_init2(&ga, (int)sizeof(char), 80);
6710 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006711 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006712 {
6713 vim_free(ga.ga_data);
6714 return NULL;
6715 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006716 ga_append(&ga, ']');
6717 ga_append(&ga, NUL);
6718 return (char_u *)ga.ga_data;
6719}
6720
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006721typedef struct join_S {
6722 char_u *s;
6723 char_u *tofree;
6724} join_T;
6725
6726 static int
6727list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6728 garray_T *gap; /* to store the result in */
6729 list_T *l;
6730 char_u *sep;
6731 int echo_style;
6732 int copyID;
6733 garray_T *join_gap; /* to keep each list item string */
6734{
6735 int i;
6736 join_T *p;
6737 int len;
6738 int sumlen = 0;
6739 int first = TRUE;
6740 char_u *tofree;
6741 char_u numbuf[NUMBUFLEN];
6742 listitem_T *item;
6743 char_u *s;
6744
6745 /* Stringify each item in the list. */
6746 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6747 {
6748 if (echo_style)
6749 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6750 else
6751 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6752 if (s == NULL)
6753 return FAIL;
6754
6755 len = (int)STRLEN(s);
6756 sumlen += len;
6757
Bram Moolenaarcde88542015-08-11 19:14:00 +02006758 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006759 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6760 if (tofree != NULL || s != numbuf)
6761 {
6762 p->s = s;
6763 p->tofree = tofree;
6764 }
6765 else
6766 {
6767 p->s = vim_strnsave(s, len);
6768 p->tofree = p->s;
6769 }
6770
6771 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006772 if (did_echo_string_emsg) /* recursion error, bail out */
6773 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006774 }
6775
6776 /* Allocate result buffer with its total size, avoid re-allocation and
6777 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6778 if (join_gap->ga_len >= 2)
6779 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6780 if (ga_grow(gap, sumlen + 2) == FAIL)
6781 return FAIL;
6782
6783 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6784 {
6785 if (first)
6786 first = FALSE;
6787 else
6788 ga_concat(gap, sep);
6789 p = ((join_T *)join_gap->ga_data) + i;
6790
6791 if (p->s != NULL)
6792 ga_concat(gap, p->s);
6793 line_breakcheck();
6794 }
6795
6796 return OK;
6797}
6798
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006799/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006801 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006802 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006804 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006805list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006807 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006808 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006809 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006810 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006812 garray_T join_ga;
6813 int retval;
6814 join_T *p;
6815 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006816
Bram Moolenaard39a7512015-04-16 22:51:22 +02006817 if (l->lv_len < 1)
6818 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006819 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6820 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6821
6822 /* Dispose each item in join_ga. */
6823 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006824 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006825 p = (join_T *)join_ga.ga_data;
6826 for (i = 0; i < join_ga.ga_len; ++i)
6827 {
6828 vim_free(p->tofree);
6829 ++p;
6830 }
6831 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006832 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006833
6834 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006835}
6836
6837/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006838 * Garbage collection for lists and dictionaries.
6839 *
6840 * We use reference counts to be able to free most items right away when they
6841 * are no longer used. But for composite items it's possible that it becomes
6842 * unused while the reference count is > 0: When there is a recursive
6843 * reference. Example:
6844 * :let l = [1, 2, 3]
6845 * :let d = {9: l}
6846 * :let l[1] = d
6847 *
6848 * Since this is quite unusual we handle this with garbage collection: every
6849 * once in a while find out which lists and dicts are not referenced from any
6850 * variable.
6851 *
6852 * Here is a good reference text about garbage collection (refers to Python
6853 * but it applies to all reference-counting mechanisms):
6854 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006855 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006856
6857/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858 * Do garbage collection for lists and dicts.
6859 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006860 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 int
6862garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006863{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006864 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006865 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866 buf_T *buf;
6867 win_T *wp;
6868 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006869 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006870 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006872#ifdef FEAT_WINDOWS
6873 tabpage_T *tp;
6874#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006876 /* Only do this once. */
6877 want_garbage_collect = FALSE;
6878 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006879 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006880
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 /* We advance by two because we add one for items referenced through
6882 * previous_funccal. */
6883 current_copyID += COPYID_INC;
6884 copyID = current_copyID;
6885
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 /*
6887 * 1. Go through all accessible variables and mark all lists and dicts
6888 * with copyID.
6889 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006890
6891 /* Don't free variables in the previous_funccal list unless they are only
6892 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006893 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006894 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6895 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6897 NULL);
6898 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6899 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006900 }
6901
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 /* script-local variables */
6903 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905
6906 /* buffer-local variables */
6907 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006908 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6909 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910
6911 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006912 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6914 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006915#ifdef FEAT_AUTOCMD
6916 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6918 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006919#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006921#ifdef FEAT_WINDOWS
6922 /* tabpage-local variables */
6923 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006924 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6925 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006926#endif
6927
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930
6931 /* function-local variables */
6932 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6933 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6935 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 }
6937
Bram Moolenaard812df62008-11-09 12:46:09 +00006938 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006940
Bram Moolenaar1dced572012-04-05 16:54:08 +02006941#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006943#endif
6944
Bram Moolenaardb913952012-06-29 12:54:53 +02006945#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006947#endif
6948
6949#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006950 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006951#endif
6952
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 /*
6956 * 2. Free lists and dictionaries that are not referenced.
6957 */
6958 did_free = free_unref_items(copyID);
6959
6960 /*
6961 * 3. Check if any funccal can be freed now.
6962 */
6963 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006965 if (can_free_funccal(*pfc, copyID))
6966 {
6967 fc = *pfc;
6968 *pfc = fc->caller;
6969 free_funccal(fc, TRUE);
6970 did_free = TRUE;
6971 did_free_funccal = TRUE;
6972 }
6973 else
6974 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006975 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006976 if (did_free_funccal)
6977 /* When a funccal was freed some more items might be garbage
6978 * collected, so run again. */
6979 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006980 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006981 else if (p_verbose > 0)
6982 {
6983 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6984 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985
6986 return did_free;
6987}
6988
6989/*
6990 * Free lists and dictionaries that are no longer referenced.
6991 */
6992 static int
6993free_unref_items(copyID)
6994 int copyID;
6995{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006996 dict_T *dd, *dd_next;
6997 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006998 int did_free = FALSE;
6999
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007001 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 */
7003 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007004 {
7005 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007006 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007008 /* Free the Dictionary and ordinary items it contains, but don't
7009 * recurse into Lists and Dictionaries, they will be in the list
7010 * of dicts or list of lists. */
7011 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007014 dd = dd_next;
7015 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016
7017 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007018 * Go through the list of lists and free items without the copyID.
7019 * But don't free a list that has a watcher (used in a for loop), these
7020 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 */
7022 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007023 {
7024 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007025 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7026 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007028 /* Free the List and ordinary items it contains, but don't recurse
7029 * into Lists and Dictionaries, they will be in the list of dicts
7030 * or list of lists. */
7031 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007034 ll = ll_next;
7035 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036 return did_free;
7037}
7038
7039/*
7040 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041 * "list_stack" is used to add lists to be marked. Can be NULL.
7042 *
7043 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007045 int
7046set_ref_in_ht(ht, copyID, list_stack)
7047 hashtab_T *ht;
7048 int copyID;
7049 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050{
7051 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054 hashtab_T *cur_ht;
7055 ht_stack_T *ht_stack = NULL;
7056 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 cur_ht = ht;
7059 for (;;)
7060 {
7061 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007062 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007063 /* Mark each item in the hashtab. If the item contains a hashtab
7064 * it is added to ht_stack, if it contains a list it is added to
7065 * list_stack. */
7066 todo = (int)cur_ht->ht_used;
7067 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7068 if (!HASHITEM_EMPTY(hi))
7069 {
7070 --todo;
7071 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7072 &ht_stack, list_stack);
7073 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075
7076 if (ht_stack == NULL)
7077 break;
7078
7079 /* take an item from the stack */
7080 cur_ht = ht_stack->ht;
7081 tempitem = ht_stack;
7082 ht_stack = ht_stack->prev;
7083 free(tempitem);
7084 }
7085
7086 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087}
7088
7089/*
7090 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7092 *
7093 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007095 int
7096set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007097 list_T *l;
7098 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007099 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 listitem_T *li;
7102 int abort = FALSE;
7103 list_T *cur_l;
7104 list_stack_T *list_stack = NULL;
7105 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007106
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007107 cur_l = l;
7108 for (;;)
7109 {
7110 if (!abort)
7111 /* Mark each item in the list. If the item contains a hashtab
7112 * it is added to ht_stack, if it contains a list it is added to
7113 * list_stack. */
7114 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7115 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7116 ht_stack, &list_stack);
7117 if (list_stack == NULL)
7118 break;
7119
7120 /* take an item from the stack */
7121 cur_l = list_stack->list;
7122 tempitem = list_stack;
7123 list_stack = list_stack->prev;
7124 free(tempitem);
7125 }
7126
7127 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007128}
7129
7130/*
7131 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007132 * "list_stack" is used to add lists to be marked. Can be NULL.
7133 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7134 *
7135 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007136 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007137 int
7138set_ref_in_item(tv, copyID, ht_stack, list_stack)
7139 typval_T *tv;
7140 int copyID;
7141 ht_stack_T **ht_stack;
7142 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007143{
7144 dict_T *dd;
7145 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007146 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007147
7148 switch (tv->v_type)
7149 {
7150 case VAR_DICT:
7151 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007152 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007153 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007154 /* Didn't see this dict yet. */
7155 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007156 if (ht_stack == NULL)
7157 {
7158 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7159 }
7160 else
7161 {
7162 ht_stack_T *newitem = (ht_stack_T*)malloc(
7163 sizeof(ht_stack_T));
7164 if (newitem == NULL)
7165 abort = TRUE;
7166 else
7167 {
7168 newitem->ht = &dd->dv_hashtab;
7169 newitem->prev = *ht_stack;
7170 *ht_stack = newitem;
7171 }
7172 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007173 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007174 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007175
7176 case VAR_LIST:
7177 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007178 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007179 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007180 /* Didn't see this list yet. */
7181 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007182 if (list_stack == NULL)
7183 {
7184 abort = set_ref_in_list(ll, copyID, ht_stack);
7185 }
7186 else
7187 {
7188 list_stack_T *newitem = (list_stack_T*)malloc(
7189 sizeof(list_stack_T));
7190 if (newitem == NULL)
7191 abort = TRUE;
7192 else
7193 {
7194 newitem->list = ll;
7195 newitem->prev = *list_stack;
7196 *list_stack = newitem;
7197 }
7198 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007199 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007200 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007201 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007202 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007203}
7204
7205/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206 * Allocate an empty header for a dictionary.
7207 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007208 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209dict_alloc()
7210{
Bram Moolenaar33570922005-01-25 22:26:29 +00007211 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212
Bram Moolenaar33570922005-01-25 22:26:29 +00007213 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007215 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007216 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007217 if (first_dict != NULL)
7218 first_dict->dv_used_prev = d;
7219 d->dv_used_next = first_dict;
7220 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007221 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007222
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007224 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007225 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007226 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007227 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007228 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007229 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230}
7231
7232/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007233 * Allocate an empty dict for a return value.
7234 * Returns OK or FAIL.
7235 */
7236 static int
7237rettv_dict_alloc(rettv)
7238 typval_T *rettv;
7239{
7240 dict_T *d = dict_alloc();
7241
7242 if (d == NULL)
7243 return FAIL;
7244
7245 rettv->vval.v_dict = d;
7246 rettv->v_type = VAR_DICT;
7247 ++d->dv_refcount;
7248 return OK;
7249}
7250
7251
7252/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 * Unreference a Dictionary: decrement the reference count and free it when it
7254 * becomes zero.
7255 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007256 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007260 if (d != NULL && --d->dv_refcount <= 0)
7261 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262}
7263
7264/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007265 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 * Ignores the reference count.
7267 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007268 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007269dict_free(d, recurse)
7270 dict_T *d;
7271 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007273 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007274 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007275 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007277 /* Remove the dict from the list of dicts for garbage collection. */
7278 if (d->dv_used_prev == NULL)
7279 first_dict = d->dv_used_next;
7280 else
7281 d->dv_used_prev->dv_used_next = d->dv_used_next;
7282 if (d->dv_used_next != NULL)
7283 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7284
7285 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007286 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007287 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 if (!HASHITEM_EMPTY(hi))
7291 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007292 /* Remove the item before deleting it, just in case there is
7293 * something recursive causing trouble. */
7294 di = HI2DI(hi);
7295 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007296 if (recurse || (di->di_tv.v_type != VAR_LIST
7297 && di->di_tv.v_type != VAR_DICT))
7298 clear_tv(&di->di_tv);
7299 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 --todo;
7301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007303 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 vim_free(d);
7305}
7306
7307/*
7308 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 * The "key" is copied to the new item.
7310 * Note that the value of the item "di_tv" still needs to be initialized!
7311 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007313 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314dictitem_alloc(key)
7315 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316{
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007319 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007323 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007324 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326}
7327
7328/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329 * Make a copy of a Dictionary item.
7330 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007333 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007334{
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007336
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007337 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7338 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 if (di != NULL)
7340 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007342 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 copy_tv(&org->di_tv, &di->di_tv);
7344 }
7345 return di;
7346}
7347
7348/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 * Remove item "item" from Dictionary "dict" and free it.
7350 */
7351 static void
7352dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 dict_T *dict;
7354 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355{
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 if (HASHITEM_EMPTY(hi))
7360 EMSG2(_(e_intern2), "dictitem_remove()");
7361 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 dictitem_free(item);
7364}
7365
7366/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007367 * Free a dict item. Also clears the value.
7368 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007369 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007370dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007371 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007372{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007373 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007374 if (item->di_flags & DI_FLAGS_ALLOC)
7375 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376}
7377
7378/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7380 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007381 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382 * Returns NULL when out of memory.
7383 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007385dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007388 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389{
Bram Moolenaar33570922005-01-25 22:26:29 +00007390 dict_T *copy;
7391 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394
7395 if (orig == NULL)
7396 return NULL;
7397
7398 copy = dict_alloc();
7399 if (copy != NULL)
7400 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007401 if (copyID != 0)
7402 {
7403 orig->dv_copyID = copyID;
7404 orig->dv_copydict = copy;
7405 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007406 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007407 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007408 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007409 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007410 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007411 --todo;
7412
7413 di = dictitem_alloc(hi->hi_key);
7414 if (di == NULL)
7415 break;
7416 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007417 {
7418 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7419 copyID) == FAIL)
7420 {
7421 vim_free(di);
7422 break;
7423 }
7424 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425 else
7426 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7427 if (dict_add(copy, di) == FAIL)
7428 {
7429 dictitem_free(di);
7430 break;
7431 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007432 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007433 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007434
Bram Moolenaare9a41262005-01-15 22:18:47 +00007435 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007436 if (todo > 0)
7437 {
7438 dict_unref(copy);
7439 copy = NULL;
7440 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007441 }
7442
7443 return copy;
7444}
7445
7446/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007448 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007450 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007452 dict_T *d;
7453 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454{
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456}
7457
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007459 * Add a number or string entry to dictionary "d".
7460 * When "str" is NULL use number "nr", otherwise use "str".
7461 * Returns FAIL when out of memory and when key already exists.
7462 */
7463 int
7464dict_add_nr_str(d, key, nr, str)
7465 dict_T *d;
7466 char *key;
7467 long nr;
7468 char_u *str;
7469{
7470 dictitem_T *item;
7471
7472 item = dictitem_alloc((char_u *)key);
7473 if (item == NULL)
7474 return FAIL;
7475 item->di_tv.v_lock = 0;
7476 if (str == NULL)
7477 {
7478 item->di_tv.v_type = VAR_NUMBER;
7479 item->di_tv.vval.v_number = nr;
7480 }
7481 else
7482 {
7483 item->di_tv.v_type = VAR_STRING;
7484 item->di_tv.vval.v_string = vim_strsave(str);
7485 }
7486 if (dict_add(d, item) == FAIL)
7487 {
7488 dictitem_free(item);
7489 return FAIL;
7490 }
7491 return OK;
7492}
7493
7494/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007495 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007496 * Returns FAIL when out of memory and when key already exists.
7497 */
7498 int
7499dict_add_list(d, key, list)
7500 dict_T *d;
7501 char *key;
7502 list_T *list;
7503{
7504 dictitem_T *item;
7505
7506 item = dictitem_alloc((char_u *)key);
7507 if (item == NULL)
7508 return FAIL;
7509 item->di_tv.v_lock = 0;
7510 item->di_tv.v_type = VAR_LIST;
7511 item->di_tv.vval.v_list = list;
7512 if (dict_add(d, item) == FAIL)
7513 {
7514 dictitem_free(item);
7515 return FAIL;
7516 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007517 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007518 return OK;
7519}
7520
7521/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007522 * Get the number of items in a Dictionary.
7523 */
7524 static long
7525dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007527{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007528 if (d == NULL)
7529 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007530 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007531}
7532
7533/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 * Find item "key[len]" in Dictionary "d".
7535 * If "len" is negative use strlen(key).
7536 * Returns NULL when not found.
7537 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007538 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541 char_u *key;
7542 int len;
7543{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007544#define AKEYLEN 200
7545 char_u buf[AKEYLEN];
7546 char_u *akey;
7547 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550 if (len < 0)
7551 akey = key;
7552 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 tofree = akey = vim_strnsave(key, len);
7555 if (akey == NULL)
7556 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007557 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007558 else
7559 {
7560 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007561 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007562 akey = buf;
7563 }
7564
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566 vim_free(tofree);
7567 if (HASHITEM_EMPTY(hi))
7568 return NULL;
7569 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570}
7571
7572/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007573 * Get a string item from a dictionary.
7574 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007575 * Returns NULL if the entry doesn't exist or out of memory.
7576 */
7577 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007578get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007579 dict_T *d;
7580 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007581 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007582{
7583 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007584 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007585
7586 di = dict_find(d, key, -1);
7587 if (di == NULL)
7588 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007589 s = get_tv_string(&di->di_tv);
7590 if (save && s != NULL)
7591 s = vim_strsave(s);
7592 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007593}
7594
7595/*
7596 * Get a number item from a dictionary.
7597 * Returns 0 if the entry doesn't exist or out of memory.
7598 */
7599 long
7600get_dict_number(d, key)
7601 dict_T *d;
7602 char_u *key;
7603{
7604 dictitem_T *di;
7605
7606 di = dict_find(d, key, -1);
7607 if (di == NULL)
7608 return 0;
7609 return get_tv_number(&di->di_tv);
7610}
7611
7612/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 * Return an allocated string with the string representation of a Dictionary.
7614 * May return NULL.
7615 */
7616 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007617dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007619 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620{
7621 garray_T ga;
7622 int first = TRUE;
7623 char_u *tofree;
7624 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007628 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 return NULL;
7632 ga_init2(&ga, (int)sizeof(char), 80);
7633 ga_append(&ga, '{');
7634
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007635 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007636 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007638 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007640 --todo;
7641
7642 if (first)
7643 first = FALSE;
7644 else
7645 ga_concat(&ga, (char_u *)", ");
7646
7647 tofree = string_quote(hi->hi_key, FALSE);
7648 if (tofree != NULL)
7649 {
7650 ga_concat(&ga, tofree);
7651 vim_free(tofree);
7652 }
7653 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007654 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 if (s != NULL)
7656 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007658 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007659 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007660 line_breakcheck();
7661
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007664 if (todo > 0)
7665 {
7666 vim_free(ga.ga_data);
7667 return NULL;
7668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669
7670 ga_append(&ga, '}');
7671 ga_append(&ga, NUL);
7672 return (char_u *)ga.ga_data;
7673}
7674
7675/*
7676 * Allocate a variable for a Dictionary and fill it from "*arg".
7677 * Return OK or FAIL. Returns NOTDONE for {expr}.
7678 */
7679 static int
7680get_dict_tv(arg, rettv, evaluate)
7681 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 int evaluate;
7684{
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 dict_T *d = NULL;
7686 typval_T tvkey;
7687 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007688 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007689 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692
7693 /*
7694 * First check if it's not a curly-braces thing: {expr}.
7695 * Must do this without evaluating, otherwise a function may be called
7696 * twice. Unfortunately this means we need to call eval1() twice for the
7697 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007698 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007700 if (*start != '}')
7701 {
7702 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7703 return FAIL;
7704 if (*start == '}')
7705 return NOTDONE;
7706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707
7708 if (evaluate)
7709 {
7710 d = dict_alloc();
7711 if (d == NULL)
7712 return FAIL;
7713 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007714 tvkey.v_type = VAR_UNKNOWN;
7715 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716
7717 *arg = skipwhite(*arg + 1);
7718 while (**arg != '}' && **arg != NUL)
7719 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007720 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 goto failret;
7722 if (**arg != ':')
7723 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007724 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007725 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 goto failret;
7727 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007728 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007730 key = get_tv_string_buf_chk(&tvkey, buf);
7731 if (key == NULL || *key == NUL)
7732 {
7733 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7734 if (key != NULL)
7735 EMSG(_(e_emptykey));
7736 clear_tv(&tvkey);
7737 goto failret;
7738 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740
7741 *arg = skipwhite(*arg + 1);
7742 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7743 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007744 if (evaluate)
7745 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007746 goto failret;
7747 }
7748 if (evaluate)
7749 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007750 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007751 if (item != NULL)
7752 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007753 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007754 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 clear_tv(&tv);
7756 goto failret;
7757 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007758 item = dictitem_alloc(key);
7759 clear_tv(&tvkey);
7760 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007763 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007764 if (dict_add(d, item) == FAIL)
7765 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766 }
7767 }
7768
7769 if (**arg == '}')
7770 break;
7771 if (**arg != ',')
7772 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007773 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007774 goto failret;
7775 }
7776 *arg = skipwhite(*arg + 1);
7777 }
7778
7779 if (**arg != '}')
7780 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007781 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007782failret:
7783 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007784 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007785 return FAIL;
7786 }
7787
7788 *arg = skipwhite(*arg + 1);
7789 if (evaluate)
7790 {
7791 rettv->v_type = VAR_DICT;
7792 rettv->vval.v_dict = d;
7793 ++d->dv_refcount;
7794 }
7795
7796 return OK;
7797}
7798
Bram Moolenaar8c711452005-01-14 21:53:12 +00007799/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007800 * Return a string with the string representation of a variable.
7801 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007802 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007803 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007804 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007805 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007806 */
7807 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007808echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007809 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007810 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007811 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007812 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007814 static int recurse = 0;
7815 char_u *r = NULL;
7816
Bram Moolenaar33570922005-01-25 22:26:29 +00007817 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007819 if (!did_echo_string_emsg)
7820 {
7821 /* Only give this message once for a recursive call to avoid
7822 * flooding the user with errors. And stop iterating over lists
7823 * and dicts. */
7824 did_echo_string_emsg = TRUE;
7825 EMSG(_("E724: variable nested too deep for displaying"));
7826 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007827 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007828 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007829 }
7830 ++recurse;
7831
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007832 switch (tv->v_type)
7833 {
7834 case VAR_FUNC:
7835 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007836 r = tv->vval.v_string;
7837 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007838
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007839 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007840 if (tv->vval.v_list == NULL)
7841 {
7842 *tofree = NULL;
7843 r = NULL;
7844 }
7845 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7846 {
7847 *tofree = NULL;
7848 r = (char_u *)"[...]";
7849 }
7850 else
7851 {
7852 tv->vval.v_list->lv_copyID = copyID;
7853 *tofree = list2string(tv, copyID);
7854 r = *tofree;
7855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007857
Bram Moolenaar8c711452005-01-14 21:53:12 +00007858 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007859 if (tv->vval.v_dict == NULL)
7860 {
7861 *tofree = NULL;
7862 r = NULL;
7863 }
7864 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7865 {
7866 *tofree = NULL;
7867 r = (char_u *)"{...}";
7868 }
7869 else
7870 {
7871 tv->vval.v_dict->dv_copyID = copyID;
7872 *tofree = dict2string(tv, copyID);
7873 r = *tofree;
7874 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007875 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007876
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007877 case VAR_STRING:
7878 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 *tofree = NULL;
7880 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007881 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007882
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883#ifdef FEAT_FLOAT
7884 case VAR_FLOAT:
7885 *tofree = NULL;
7886 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7887 r = numbuf;
7888 break;
7889#endif
7890
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007891 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007892 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007893 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007894 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007895
Bram Moolenaar8502c702014-06-17 12:51:16 +02007896 if (--recurse == 0)
7897 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007899}
7900
7901/*
7902 * Return a string with the string representation of a variable.
7903 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7904 * "numbuf" is used for a number.
7905 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007906 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007907 */
7908 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007909tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007910 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007911 char_u **tofree;
7912 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007913 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914{
7915 switch (tv->v_type)
7916 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007917 case VAR_FUNC:
7918 *tofree = string_quote(tv->vval.v_string, TRUE);
7919 return *tofree;
7920 case VAR_STRING:
7921 *tofree = string_quote(tv->vval.v_string, FALSE);
7922 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007923#ifdef FEAT_FLOAT
7924 case VAR_FLOAT:
7925 *tofree = NULL;
7926 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7927 return numbuf;
7928#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007929 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007930 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007931 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007932 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007933 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007934 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007935 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007936 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007937}
7938
7939/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007940 * Return string "str" in ' quotes, doubling ' characters.
7941 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007942 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007943 */
7944 static char_u *
7945string_quote(str, function)
7946 char_u *str;
7947 int function;
7948{
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007950 char_u *p, *r, *s;
7951
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 len = (function ? 13 : 3);
7953 if (str != NULL)
7954 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007955 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 for (p = str; *p != NUL; mb_ptr_adv(p))
7957 if (*p == '\'')
7958 ++len;
7959 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960 s = r = alloc(len);
7961 if (r != NULL)
7962 {
7963 if (function)
7964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007965 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007966 r += 10;
7967 }
7968 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007969 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007970 if (str != NULL)
7971 for (p = str; *p != NUL; )
7972 {
7973 if (*p == '\'')
7974 *r++ = '\'';
7975 MB_COPY_CHAR(p, r);
7976 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007978 if (function)
7979 *r++ = ')';
7980 *r++ = NUL;
7981 }
7982 return s;
7983}
7984
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007985#ifdef FEAT_FLOAT
7986/*
7987 * Convert the string "text" to a floating point number.
7988 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7989 * this always uses a decimal point.
7990 * Returns the length of the text that was consumed.
7991 */
7992 static int
7993string2float(text, value)
7994 char_u *text;
7995 float_T *value; /* result stored here */
7996{
7997 char *s = (char *)text;
7998 float_T f;
7999
8000 f = strtod(s, &s);
8001 *value = f;
8002 return (int)((char_u *)s - text);
8003}
8004#endif
8005
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 * Get the value of an environment variable.
8008 * "arg" is pointing to the '$'. It is advanced to after the name.
8009 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008010 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 */
8012 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 int evaluate;
8017{
8018 char_u *string = NULL;
8019 int len;
8020 int cc;
8021 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008022 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023
8024 ++*arg;
8025 name = *arg;
8026 len = get_env_len(arg);
8027 if (evaluate)
8028 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008029 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008030 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008031
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008032 cc = name[len];
8033 name[len] = NUL;
8034 /* first try vim_getenv(), fast for normal environment vars */
8035 string = vim_getenv(name, &mustfree);
8036 if (string != NULL && *string != NUL)
8037 {
8038 if (!mustfree)
8039 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008041 else
8042 {
8043 if (mustfree)
8044 vim_free(string);
8045
8046 /* next try expanding things like $VIM and ${HOME} */
8047 string = expand_env_save(name - 1);
8048 if (string != NULL && *string == '$')
8049 {
8050 vim_free(string);
8051 string = NULL;
8052 }
8053 }
8054 name[len] = cc;
8055
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008056 rettv->v_type = VAR_STRING;
8057 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 }
8059
8060 return OK;
8061}
8062
8063/*
8064 * Array with names and number of arguments of all internal functions
8065 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8066 */
8067static struct fst
8068{
8069 char *f_name; /* function name */
8070 char f_min_argc; /* minimal number of arguments */
8071 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008072 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008073 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074} functions[] =
8075{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008076#ifdef FEAT_FLOAT
8077 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008078 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008080 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008081 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008082 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"append", 2, 2, f_append},
8084 {"argc", 0, 0, f_argc},
8085 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008086 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008087 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008088#ifdef FEAT_FLOAT
8089 {"asin", 1, 1, f_asin}, /* WJMc */
8090#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008091 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008092 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008093 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008094 {"assert_false", 1, 2, f_assert_false},
8095 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008096#ifdef FEAT_FLOAT
8097 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008098 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008101 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {"bufexists", 1, 1, f_bufexists},
8103 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8104 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8105 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8106 {"buflisted", 1, 1, f_buflisted},
8107 {"bufloaded", 1, 1, f_bufloaded},
8108 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008109 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {"bufwinnr", 1, 1, f_bufwinnr},
8111 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008112 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008113 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008114 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008115#ifdef FEAT_FLOAT
8116 {"ceil", 1, 1, f_ceil},
8117#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008118 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008119 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008121 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008123#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008124 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008125 {"complete_add", 1, 1, f_complete_add},
8126 {"complete_check", 0, 0, f_complete_check},
8127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008129 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008130#ifdef FEAT_FLOAT
8131 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008132 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008133#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008134 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008136 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008137 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008138 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008140 {"diff_filler", 1, 1, f_diff_filler},
8141 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008142 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008144 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {"eventhandler", 0, 0, f_eventhandler},
8146 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008147 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008149#ifdef FEAT_FLOAT
8150 {"exp", 1, 1, f_exp},
8151#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008152 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008153 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008154 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8156 {"filereadable", 1, 1, f_filereadable},
8157 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008158 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008159 {"finddir", 1, 3, f_finddir},
8160 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008161#ifdef FEAT_FLOAT
8162 {"float2nr", 1, 1, f_float2nr},
8163 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008164 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008166 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {"fnamemodify", 2, 2, f_fnamemodify},
8168 {"foldclosed", 1, 1, f_foldclosed},
8169 {"foldclosedend", 1, 1, f_foldclosedend},
8170 {"foldlevel", 1, 1, f_foldlevel},
8171 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008172 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008174 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008175 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008176 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008177 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008178 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"getchar", 0, 1, f_getchar},
8180 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008181 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"getcmdline", 0, 0, f_getcmdline},
8183 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008184 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008185 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008186 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008187 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008188 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008189 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"getfsize", 1, 1, f_getfsize},
8191 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008192 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008193 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008194 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008195 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008196 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008197 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008198 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008199 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008201 {"gettabvar", 2, 3, f_gettabvar},
8202 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {"getwinposx", 0, 0, f_getwinposx},
8204 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008205 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008206 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008207 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008208 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008210 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008211 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008212 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8214 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8215 {"histadd", 2, 2, f_histadd},
8216 {"histdel", 1, 2, f_histdel},
8217 {"histget", 1, 2, f_histget},
8218 {"histnr", 1, 1, f_histnr},
8219 {"hlID", 1, 1, f_hlID},
8220 {"hlexists", 1, 1, f_hlexists},
8221 {"hostname", 0, 0, f_hostname},
8222 {"iconv", 3, 3, f_iconv},
8223 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008224 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008225 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008227 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {"inputrestore", 0, 0, f_inputrestore},
8229 {"inputsave", 0, 0, f_inputsave},
8230 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008231 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008232 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008234 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008235 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008236 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008237 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008239 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {"libcall", 3, 3, f_libcall},
8241 {"libcallnr", 3, 3, f_libcallnr},
8242 {"line", 1, 1, f_line},
8243 {"line2byte", 1, 1, f_line2byte},
8244 {"lispindent", 1, 1, f_lispindent},
8245 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008246#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008247 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008248 {"log10", 1, 1, f_log10},
8249#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008250#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008251 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008252#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008253 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008254 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008255 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008256 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008257 {"matchadd", 2, 5, f_matchadd},
8258 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008259 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008260 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008261 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008262 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008263 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008264 {"max", 1, 1, f_max},
8265 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008266#ifdef vim_mkdir
8267 {"mkdir", 1, 3, f_mkdir},
8268#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008269 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008270#ifdef FEAT_MZSCHEME
8271 {"mzeval", 1, 1, f_mzeval},
8272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008274 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008275 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008276 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008277#ifdef FEAT_PERL
8278 {"perleval", 1, 1, f_perleval},
8279#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008280#ifdef FEAT_FLOAT
8281 {"pow", 2, 2, f_pow},
8282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008284 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008285 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008286#ifdef FEAT_PYTHON3
8287 {"py3eval", 1, 1, f_py3eval},
8288#endif
8289#ifdef FEAT_PYTHON
8290 {"pyeval", 1, 1, f_pyeval},
8291#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008292 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008293 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008294 {"reltime", 0, 2, f_reltime},
8295 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {"remote_expr", 2, 3, f_remote_expr},
8297 {"remote_foreground", 1, 1, f_remote_foreground},
8298 {"remote_peek", 1, 2, f_remote_peek},
8299 {"remote_read", 1, 1, f_remote_read},
8300 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008301 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008303 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008305 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008306#ifdef FEAT_FLOAT
8307 {"round", 1, 1, f_round},
8308#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008309 {"screenattr", 2, 2, f_screenattr},
8310 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008311 {"screencol", 0, 0, f_screencol},
8312 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008313 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008314 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008315 {"searchpair", 3, 7, f_searchpair},
8316 {"searchpairpos", 3, 7, f_searchpairpos},
8317 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {"server2client", 2, 2, f_server2client},
8319 {"serverlist", 0, 0, f_serverlist},
8320 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008321 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {"setcmdpos", 1, 1, f_setcmdpos},
8323 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008324 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008325 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008326 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008327 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008329 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008330 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008332#ifdef FEAT_CRYPT
8333 {"sha256", 1, 1, f_sha256},
8334#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008335 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008336 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008338#ifdef FEAT_FLOAT
8339 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008340 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008341#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008342 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008343 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008344 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008345 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008346 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008347#ifdef FEAT_FLOAT
8348 {"sqrt", 1, 1, f_sqrt},
8349 {"str2float", 1, 1, f_str2float},
8350#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008351 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008352 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008353 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354#ifdef HAVE_STRFTIME
8355 {"strftime", 1, 2, f_strftime},
8356#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008357 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008358 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 {"strlen", 1, 1, f_strlen},
8360 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008361 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008363 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008364 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {"substitute", 4, 4, f_substitute},
8366 {"synID", 3, 3, f_synID},
8367 {"synIDattr", 2, 3, f_synIDattr},
8368 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008369 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008370 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008371 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008372 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008373 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008374 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008375 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008376 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008377 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008378#ifdef FEAT_FLOAT
8379 {"tan", 1, 1, f_tan},
8380 {"tanh", 1, 1, f_tanh},
8381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008383 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {"tolower", 1, 1, f_tolower},
8385 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008386 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008387#ifdef FEAT_FLOAT
8388 {"trunc", 1, 1, f_trunc},
8389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008391 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008392 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008393 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008394 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {"virtcol", 1, 1, f_virtcol},
8396 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008397 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 {"winbufnr", 1, 1, f_winbufnr},
8399 {"wincol", 0, 0, f_wincol},
8400 {"winheight", 1, 1, f_winheight},
8401 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008402 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008404 {"winrestview", 1, 1, f_winrestview},
8405 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008407 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008408 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008409 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410};
8411
8412#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8413
8414/*
8415 * Function given to ExpandGeneric() to obtain the list of internal
8416 * or user defined function names.
8417 */
8418 char_u *
8419get_function_name(xp, idx)
8420 expand_T *xp;
8421 int idx;
8422{
8423 static int intidx = -1;
8424 char_u *name;
8425
8426 if (idx == 0)
8427 intidx = -1;
8428 if (intidx < 0)
8429 {
8430 name = get_user_func_name(xp, idx);
8431 if (name != NULL)
8432 return name;
8433 }
8434 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8435 {
8436 STRCPY(IObuff, functions[intidx].f_name);
8437 STRCAT(IObuff, "(");
8438 if (functions[intidx].f_max_argc == 0)
8439 STRCAT(IObuff, ")");
8440 return IObuff;
8441 }
8442
8443 return NULL;
8444}
8445
8446/*
8447 * Function given to ExpandGeneric() to obtain the list of internal or
8448 * user defined variable or function names.
8449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 char_u *
8451get_expr_name(xp, idx)
8452 expand_T *xp;
8453 int idx;
8454{
8455 static int intidx = -1;
8456 char_u *name;
8457
8458 if (idx == 0)
8459 intidx = -1;
8460 if (intidx < 0)
8461 {
8462 name = get_function_name(xp, idx);
8463 if (name != NULL)
8464 return name;
8465 }
8466 return get_user_var_name(xp, ++intidx);
8467}
8468
8469#endif /* FEAT_CMDL_COMPL */
8470
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008471#if defined(EBCDIC) || defined(PROTO)
8472/*
8473 * Compare struct fst by function name.
8474 */
8475 static int
8476compare_func_name(s1, s2)
8477 const void *s1;
8478 const void *s2;
8479{
8480 struct fst *p1 = (struct fst *)s1;
8481 struct fst *p2 = (struct fst *)s2;
8482
8483 return STRCMP(p1->f_name, p2->f_name);
8484}
8485
8486/*
8487 * Sort the function table by function name.
8488 * The sorting of the table above is ASCII dependant.
8489 * On machines using EBCDIC we have to sort it.
8490 */
8491 static void
8492sortFunctions()
8493{
8494 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8495
8496 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8497}
8498#endif
8499
8500
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501/*
8502 * Find internal function in table above.
8503 * Return index, or -1 if not found
8504 */
8505 static int
8506find_internal_func(name)
8507 char_u *name; /* name of the function */
8508{
8509 int first = 0;
8510 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8511 int cmp;
8512 int x;
8513
8514 /*
8515 * Find the function name in the table. Binary search.
8516 */
8517 while (first <= last)
8518 {
8519 x = first + ((unsigned)(last - first) >> 1);
8520 cmp = STRCMP(name, functions[x].f_name);
8521 if (cmp < 0)
8522 last = x - 1;
8523 else if (cmp > 0)
8524 first = x + 1;
8525 else
8526 return x;
8527 }
8528 return -1;
8529}
8530
8531/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008532 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8533 * name it contains, otherwise return "name".
8534 */
8535 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008536deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008537 char_u *name;
8538 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008539 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008540{
Bram Moolenaar33570922005-01-25 22:26:29 +00008541 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008542 int cc;
8543
8544 cc = name[*lenp];
8545 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008546 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008547 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008549 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008551 {
8552 *lenp = 0;
8553 return (char_u *)""; /* just in case */
8554 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008555 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008556 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008557 }
8558
8559 return name;
8560}
8561
8562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 * Allocate a variable for the result of a function.
8564 * Return OK or FAIL.
8565 */
8566 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008567get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8568 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 char_u *name; /* name of the function */
8570 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 char_u **arg; /* argument, pointing to the '(' */
8573 linenr_T firstline; /* first line of range */
8574 linenr_T lastline; /* last line of range */
8575 int *doesrange; /* return: function handled range */
8576 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008577 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 char_u *argp;
8580 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008581 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 int argcount = 0; /* number of arguments found */
8583
8584 /*
8585 * Get the arguments.
8586 */
8587 argp = *arg;
8588 while (argcount < MAX_FUNC_ARGS)
8589 {
8590 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8591 if (*argp == ')' || *argp == ',' || *argp == NUL)
8592 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8594 {
8595 ret = FAIL;
8596 break;
8597 }
8598 ++argcount;
8599 if (*argp != ',')
8600 break;
8601 }
8602 if (*argp == ')')
8603 ++argp;
8604 else
8605 ret = FAIL;
8606
8607 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008608 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008609 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008611 {
8612 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008613 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008614 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008615 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617
8618 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008619 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620
8621 *arg = skipwhite(argp);
8622 return ret;
8623}
8624
8625
8626/*
8627 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008628 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008629 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 */
8631 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008632call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008634 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008636 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008638 typval_T *argvars; /* vars for arguments, must have "argcount"
8639 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 linenr_T firstline; /* first line of range */
8641 linenr_T lastline; /* last line of range */
8642 int *doesrange; /* return: function handled range */
8643 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008644 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
8646 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647#define ERROR_UNKNOWN 0
8648#define ERROR_TOOMANY 1
8649#define ERROR_TOOFEW 2
8650#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008651#define ERROR_DICT 4
8652#define ERROR_NONE 5
8653#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 int error = ERROR_NONE;
8655 int i;
8656 int llen;
8657 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658#define FLEN_FIXED 40
8659 char_u fname_buf[FLEN_FIXED + 1];
8660 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008661 char_u *name;
8662
8663 /* Make a copy of the name, if it comes from a funcref variable it could
8664 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008665 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008666 if (name == NULL)
8667 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668
8669 /*
8670 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8671 * Change <SNR>123_name() to K_SNR 123_name().
8672 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 llen = eval_fname_script(name);
8675 if (llen > 0)
8676 {
8677 fname_buf[0] = K_SPECIAL;
8678 fname_buf[1] = KS_EXTRA;
8679 fname_buf[2] = (int)KE_SNR;
8680 i = 3;
8681 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8682 {
8683 if (current_SID <= 0)
8684 error = ERROR_SCRIPT;
8685 else
8686 {
8687 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8688 i = (int)STRLEN(fname_buf);
8689 }
8690 }
8691 if (i + STRLEN(name + llen) < FLEN_FIXED)
8692 {
8693 STRCPY(fname_buf + i, name + llen);
8694 fname = fname_buf;
8695 }
8696 else
8697 {
8698 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8699 if (fname == NULL)
8700 error = ERROR_OTHER;
8701 else
8702 {
8703 mch_memmove(fname, fname_buf, (size_t)i);
8704 STRCPY(fname + i, name + llen);
8705 }
8706 }
8707 }
8708 else
8709 fname = name;
8710
8711 *doesrange = FALSE;
8712
8713
8714 /* execute the function if no errors detected and executing */
8715 if (evaluate && error == ERROR_NONE)
8716 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008717 char_u *rfname = fname;
8718
8719 /* Ignore "g:" before a function name. */
8720 if (fname[0] == 'g' && fname[1] == ':')
8721 rfname = fname + 2;
8722
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008723 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8724 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 error = ERROR_UNKNOWN;
8726
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008727 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 {
8729 /*
8730 * User defined function.
8731 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008732 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008733
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008735 /* Trigger FuncUndefined event, may load the function. */
8736 if (fp == NULL
8737 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008738 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008739 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008741 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008742 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 }
8744#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008745 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008746 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008747 {
8748 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008749 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008750 }
8751
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 if (fp != NULL)
8753 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008754 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008756 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008758 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008760 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008761 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 else
8763 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008764 int did_save_redo = FALSE;
8765
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 /*
8767 * Call the user function.
8768 * Save and restore search patterns, script variables and
8769 * redo buffer.
8770 */
8771 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008772#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008773 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008774#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008775 {
8776 saveRedobuff();
8777 did_save_redo = TRUE;
8778 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008779 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008781 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008782 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8783 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8784 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008785 /* Function was unreferenced while being used, free it
8786 * now. */
8787 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008788 if (did_save_redo)
8789 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 restore_search_patterns();
8791 error = ERROR_NONE;
8792 }
8793 }
8794 }
8795 else
8796 {
8797 /*
8798 * Find the function name in the table, call its implementation.
8799 */
8800 i = find_internal_func(fname);
8801 if (i >= 0)
8802 {
8803 if (argcount < functions[i].f_min_argc)
8804 error = ERROR_TOOFEW;
8805 else if (argcount > functions[i].f_max_argc)
8806 error = ERROR_TOOMANY;
8807 else
8808 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008809 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008810 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 error = ERROR_NONE;
8812 }
8813 }
8814 }
8815 /*
8816 * The function call (or "FuncUndefined" autocommand sequence) might
8817 * have been aborted by an error, an interrupt, or an explicitly thrown
8818 * exception that has not been caught so far. This situation can be
8819 * tested for by calling aborting(). For an error in an internal
8820 * function or for the "E132" error in call_user_func(), however, the
8821 * throw point at which the "force_abort" flag (temporarily reset by
8822 * emsg()) is normally updated has not been reached yet. We need to
8823 * update that flag first to make aborting() reliable.
8824 */
8825 update_force_abort();
8826 }
8827 if (error == ERROR_NONE)
8828 ret = OK;
8829
8830 /*
8831 * Report an error unless the argument evaluation or function call has been
8832 * cancelled due to an aborting error, an interrupt, or an exception.
8833 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008834 if (!aborting())
8835 {
8836 switch (error)
8837 {
8838 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008839 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008840 break;
8841 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008842 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008843 break;
8844 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008845 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008846 name);
8847 break;
8848 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008849 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008850 name);
8851 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008852 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008853 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008854 name);
8855 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008856 }
8857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 if (fname != name && fname != fname_buf)
8860 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008861 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862
8863 return ret;
8864}
8865
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008866/*
8867 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008868 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008869 */
8870 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008871emsg_funcname(ermsg, name)
8872 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008873 char_u *name;
8874{
8875 char_u *p;
8876
8877 if (*name == K_SPECIAL)
8878 p = concat_str((char_u *)"<SNR>", name + 3);
8879 else
8880 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008881 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008882 if (p != name)
8883 vim_free(p);
8884}
8885
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008886/*
8887 * Return TRUE for a non-zero Number and a non-empty String.
8888 */
8889 static int
8890non_zero_arg(argvars)
8891 typval_T *argvars;
8892{
8893 return ((argvars[0].v_type == VAR_NUMBER
8894 && argvars[0].vval.v_number != 0)
8895 || (argvars[0].v_type == VAR_STRING
8896 && argvars[0].vval.v_string != NULL
8897 && *argvars[0].vval.v_string != NUL));
8898}
8899
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900/*********************************************
8901 * Implementation of the built-in functions
8902 */
8903
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008904#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008905static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8906
8907/*
8908 * Get the float value of "argvars[0]" into "f".
8909 * Returns FAIL when the argument is not a Number or Float.
8910 */
8911 static int
8912get_float_arg(argvars, f)
8913 typval_T *argvars;
8914 float_T *f;
8915{
8916 if (argvars[0].v_type == VAR_FLOAT)
8917 {
8918 *f = argvars[0].vval.v_float;
8919 return OK;
8920 }
8921 if (argvars[0].v_type == VAR_NUMBER)
8922 {
8923 *f = (float_T)argvars[0].vval.v_number;
8924 return OK;
8925 }
8926 EMSG(_("E808: Number or Float required"));
8927 return FAIL;
8928}
8929
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008930/*
8931 * "abs(expr)" function
8932 */
8933 static void
8934f_abs(argvars, rettv)
8935 typval_T *argvars;
8936 typval_T *rettv;
8937{
8938 if (argvars[0].v_type == VAR_FLOAT)
8939 {
8940 rettv->v_type = VAR_FLOAT;
8941 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8942 }
8943 else
8944 {
8945 varnumber_T n;
8946 int error = FALSE;
8947
8948 n = get_tv_number_chk(&argvars[0], &error);
8949 if (error)
8950 rettv->vval.v_number = -1;
8951 else if (n > 0)
8952 rettv->vval.v_number = n;
8953 else
8954 rettv->vval.v_number = -n;
8955 }
8956}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008957
8958/*
8959 * "acos()" function
8960 */
8961 static void
8962f_acos(argvars, rettv)
8963 typval_T *argvars;
8964 typval_T *rettv;
8965{
8966 float_T f;
8967
8968 rettv->v_type = VAR_FLOAT;
8969 if (get_float_arg(argvars, &f) == OK)
8970 rettv->vval.v_float = acos(f);
8971 else
8972 rettv->vval.v_float = 0.0;
8973}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008974#endif
8975
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008977 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 */
8979 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008980f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008981 typval_T *argvars;
8982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983{
Bram Moolenaar33570922005-01-25 22:26:29 +00008984 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008987 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008989 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008990 && !tv_check_lock(l->lv_lock,
8991 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008992 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008994 }
8995 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008996 EMSG(_(e_listreq));
8997}
8998
8999/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009000 * "alloc_fail(id, countdown, repeat)" function
9001 */
9002 static void
9003f_alloc_fail(argvars, rettv)
9004 typval_T *argvars;
9005 typval_T *rettv UNUSED;
9006{
9007 if (argvars[0].v_type != VAR_NUMBER
9008 || argvars[0].vval.v_number <= 0
9009 || argvars[1].v_type != VAR_NUMBER
9010 || argvars[1].vval.v_number < 0
9011 || argvars[2].v_type != VAR_NUMBER)
9012 EMSG(_(e_invarg));
9013 else
9014 {
9015 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009016 if (alloc_fail_id >= aid_last)
9017 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009018 alloc_fail_countdown = argvars[1].vval.v_number;
9019 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009020 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009021 }
9022}
9023
9024/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009025 * "and(expr, expr)" function
9026 */
9027 static void
9028f_and(argvars, rettv)
9029 typval_T *argvars;
9030 typval_T *rettv;
9031{
9032 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9033 & get_tv_number_chk(&argvars[1], NULL);
9034}
9035
9036/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009037 * "append(lnum, string/list)" function
9038 */
9039 static void
9040f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009041 typval_T *argvars;
9042 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009043{
9044 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009045 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009046 list_T *l = NULL;
9047 listitem_T *li = NULL;
9048 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009049 long added = 0;
9050
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009051 /* When coming here from Insert mode, sync undo, so that this can be
9052 * undone separately from what was previously inserted. */
9053 if (u_sync_once == 2)
9054 {
9055 u_sync_once = 1; /* notify that u_sync() was called */
9056 u_sync(TRUE);
9057 }
9058
Bram Moolenaar0d660222005-01-07 21:51:51 +00009059 lnum = get_tv_lnum(argvars);
9060 if (lnum >= 0
9061 && lnum <= curbuf->b_ml.ml_line_count
9062 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009063 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009064 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009065 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009066 l = argvars[1].vval.v_list;
9067 if (l == NULL)
9068 return;
9069 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009070 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009071 for (;;)
9072 {
9073 if (l == NULL)
9074 tv = &argvars[1]; /* append a string */
9075 else if (li == NULL)
9076 break; /* end of list */
9077 else
9078 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009079 line = get_tv_string_chk(tv);
9080 if (line == NULL) /* type error */
9081 {
9082 rettv->vval.v_number = 1; /* Failed */
9083 break;
9084 }
9085 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009086 ++added;
9087 if (l == NULL)
9088 break;
9089 li = li->li_next;
9090 }
9091
9092 appended_lines_mark(lnum, added);
9093 if (curwin->w_cursor.lnum > lnum)
9094 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009096 else
9097 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098}
9099
9100/*
9101 * "argc()" function
9102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009104f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009105 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109}
9110
9111/*
9112 * "argidx()" function
9113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009116 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009119 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120}
9121
9122/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009123 * "arglistid()" function
9124 */
9125 static void
9126f_arglistid(argvars, rettv)
9127 typval_T *argvars UNUSED;
9128 typval_T *rettv;
9129{
9130 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009131
9132 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009133 wp = find_tabwin(&argvars[0], &argvars[1]);
9134 if (wp != NULL)
9135 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009136}
9137
9138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 * "argv(nr)" function
9140 */
9141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009143 typval_T *argvars;
9144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146 int idx;
9147
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009148 if (argvars[0].v_type != VAR_UNKNOWN)
9149 {
9150 idx = get_tv_number_chk(&argvars[0], NULL);
9151 if (idx >= 0 && idx < ARGCOUNT)
9152 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9153 else
9154 rettv->vval.v_string = NULL;
9155 rettv->v_type = VAR_STRING;
9156 }
9157 else if (rettv_list_alloc(rettv) == OK)
9158 for (idx = 0; idx < ARGCOUNT; ++idx)
9159 list_append_string(rettv->vval.v_list,
9160 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161}
9162
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009163static void prepare_assert_error __ARGS((garray_T*gap));
9164static void fill_assert_error __ARGS((garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv));
9165static void assert_error __ARGS((garray_T *gap));
9166static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9167
9168/*
9169 * Prepare "gap" for an assert error and add the sourcing position.
9170 */
9171 static void
9172prepare_assert_error(gap)
9173 garray_T *gap;
9174{
9175 char buf[NUMBUFLEN];
9176
9177 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009178 if (sourcing_name != NULL)
9179 {
9180 ga_concat(gap, sourcing_name);
9181 if (sourcing_lnum > 0)
9182 ga_concat(gap, (char_u *)" ");
9183 }
9184 if (sourcing_lnum > 0)
9185 {
9186 sprintf(buf, "line %ld", (long)sourcing_lnum);
9187 ga_concat(gap, (char_u *)buf);
9188 }
9189 if (sourcing_name != NULL || sourcing_lnum > 0)
9190 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009191}
9192
9193/*
9194 * Fill "gap" with information about an assert error.
9195 */
9196 static void
9197fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9198 garray_T *gap;
9199 typval_T *opt_msg_tv;
9200 char_u *exp_str;
9201 typval_T *exp_tv;
9202 typval_T *got_tv;
9203{
9204 char_u numbuf[NUMBUFLEN];
9205 char_u *tofree;
9206
9207 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9208 {
9209 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9210 vim_free(tofree);
9211 }
9212 else
9213 {
9214 ga_concat(gap, (char_u *)"Expected ");
9215 if (exp_str == NULL)
9216 {
9217 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9218 vim_free(tofree);
9219 }
9220 else
9221 ga_concat(gap, exp_str);
9222 ga_concat(gap, (char_u *)" but got ");
9223 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9224 vim_free(tofree);
9225 }
9226}
Bram Moolenaar43345542015-11-29 17:35:35 +01009227
9228/*
9229 * Add an assert error to v:errors.
9230 */
9231 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009232assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009233 garray_T *gap;
9234{
9235 struct vimvar *vp = &vimvars[VV_ERRORS];
9236
9237 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9238 /* Make sure v:errors is a list. */
9239 set_vim_var_list(VV_ERRORS, list_alloc());
9240 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9241}
9242
Bram Moolenaar43345542015-11-29 17:35:35 +01009243/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009244 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009245 */
9246 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009247f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009248 typval_T *argvars;
9249 typval_T *rettv UNUSED;
9250{
9251 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009252
9253 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9254 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009255 prepare_assert_error(&ga);
9256 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9257 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009258 ga_clear(&ga);
9259 }
9260}
9261
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009262/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009263 * "assert_exception(string[, msg])" function
9264 */
9265 static void
9266f_assert_exception(argvars, rettv)
9267 typval_T *argvars;
9268 typval_T *rettv UNUSED;
9269{
9270 garray_T ga;
9271 char *error;
9272
9273 error = (char *)get_tv_string_chk(&argvars[0]);
9274 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9275 {
9276 prepare_assert_error(&ga);
9277 ga_concat(&ga, (char_u *)"v:exception is not set");
9278 assert_error(&ga);
9279 ga_clear(&ga);
9280 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009281 else if (error != NULL
9282 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009283 {
9284 prepare_assert_error(&ga);
9285 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9286 &vimvars[VV_EXCEPTION].vv_tv);
9287 assert_error(&ga);
9288 ga_clear(&ga);
9289 }
9290}
9291
9292/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009293 * "assert_fails(cmd [, error])" function
9294 */
9295 static void
9296f_assert_fails(argvars, rettv)
9297 typval_T *argvars;
9298 typval_T *rettv UNUSED;
9299{
9300 char_u *cmd = get_tv_string_chk(&argvars[0]);
9301 garray_T ga;
9302
9303 called_emsg = FALSE;
9304 suppress_errthrow = TRUE;
9305 emsg_silent = TRUE;
9306 do_cmdline_cmd(cmd);
9307 if (!called_emsg)
9308 {
9309 prepare_assert_error(&ga);
9310 ga_concat(&ga, (char_u *)"command did not fail: ");
9311 ga_concat(&ga, cmd);
9312 assert_error(&ga);
9313 ga_clear(&ga);
9314 }
9315 else if (argvars[1].v_type != VAR_UNKNOWN)
9316 {
9317 char_u buf[NUMBUFLEN];
9318 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9319
9320 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9321 {
9322 prepare_assert_error(&ga);
9323 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9324 &vimvars[VV_ERRMSG].vv_tv);
9325 assert_error(&ga);
9326 ga_clear(&ga);
9327 }
9328 }
9329
9330 called_emsg = FALSE;
9331 suppress_errthrow = FALSE;
9332 emsg_silent = FALSE;
9333 emsg_on_display = FALSE;
9334 set_vim_var_string(VV_ERRMSG, NULL, 0);
9335}
9336
9337/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009338 * Common for assert_true() and assert_false().
9339 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009340 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009341assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009342 typval_T *argvars;
9343 int isTrue;
9344{
9345 int error = FALSE;
9346 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009347
9348 if (argvars[0].v_type != VAR_NUMBER
9349 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9350 || error)
9351 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009352 prepare_assert_error(&ga);
9353 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009354 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009355 NULL, &argvars[0]);
9356 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009357 ga_clear(&ga);
9358 }
9359}
9360
9361/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009362 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009363 */
9364 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009365f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009366 typval_T *argvars;
9367 typval_T *rettv UNUSED;
9368{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009369 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009370}
9371
9372/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009373 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009374 */
9375 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009376f_assert_true(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, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009381}
9382
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009383#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009384/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009385 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009386 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009387 static void
9388f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009389 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009390 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009391{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009392 float_T f;
9393
9394 rettv->v_type = VAR_FLOAT;
9395 if (get_float_arg(argvars, &f) == OK)
9396 rettv->vval.v_float = asin(f);
9397 else
9398 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009399}
9400
9401/*
9402 * "atan()" function
9403 */
9404 static void
9405f_atan(argvars, rettv)
9406 typval_T *argvars;
9407 typval_T *rettv;
9408{
9409 float_T f;
9410
9411 rettv->v_type = VAR_FLOAT;
9412 if (get_float_arg(argvars, &f) == OK)
9413 rettv->vval.v_float = atan(f);
9414 else
9415 rettv->vval.v_float = 0.0;
9416}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009417
9418/*
9419 * "atan2()" function
9420 */
9421 static void
9422f_atan2(argvars, rettv)
9423 typval_T *argvars;
9424 typval_T *rettv;
9425{
9426 float_T fx, fy;
9427
9428 rettv->v_type = VAR_FLOAT;
9429 if (get_float_arg(argvars, &fx) == OK
9430 && get_float_arg(&argvars[1], &fy) == OK)
9431 rettv->vval.v_float = atan2(fx, fy);
9432 else
9433 rettv->vval.v_float = 0.0;
9434}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009435#endif
9436
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437/*
9438 * "browse(save, title, initdir, default)" function
9439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009441f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009442 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444{
9445#ifdef FEAT_BROWSE
9446 int save;
9447 char_u *title;
9448 char_u *initdir;
9449 char_u *defname;
9450 char_u buf[NUMBUFLEN];
9451 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009452 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009454 save = get_tv_number_chk(&argvars[0], &error);
9455 title = get_tv_string_chk(&argvars[1]);
9456 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9457 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009459 if (error || title == NULL || initdir == NULL || defname == NULL)
9460 rettv->vval.v_string = NULL;
9461 else
9462 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009463 do_browse(save ? BROWSE_SAVE : 0,
9464 title, defname, NULL, initdir, NULL, curbuf);
9465#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009467#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009468 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009469}
9470
9471/*
9472 * "browsedir(title, initdir)" function
9473 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009475f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009476 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009477 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009478{
9479#ifdef FEAT_BROWSE
9480 char_u *title;
9481 char_u *initdir;
9482 char_u buf[NUMBUFLEN];
9483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009484 title = get_tv_string_chk(&argvars[0]);
9485 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009487 if (title == NULL || initdir == NULL)
9488 rettv->vval.v_string = NULL;
9489 else
9490 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009491 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009493 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496}
9497
Bram Moolenaar33570922005-01-25 22:26:29 +00009498static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009499
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500/*
9501 * Find a buffer by number or exact name.
9502 */
9503 static buf_T *
9504find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009505 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506{
9507 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009509 if (avar->v_type == VAR_NUMBER)
9510 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009511 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009513 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009514 if (buf == NULL)
9515 {
9516 /* No full path name match, try a match with a URL or a "nofile"
9517 * buffer, these don't use the full path. */
9518 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9519 if (buf->b_fname != NULL
9520 && (path_with_url(buf->b_fname)
9521#ifdef FEAT_QUICKFIX
9522 || bt_nofile(buf)
9523#endif
9524 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009525 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009526 break;
9527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 }
9529 return buf;
9530}
9531
9532/*
9533 * "bufexists(expr)" function
9534 */
9535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009536f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009537 typval_T *argvars;
9538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009540 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541}
9542
9543/*
9544 * "buflisted(expr)" function
9545 */
9546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547f_buflisted(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{
9551 buf_T *buf;
9552
9553 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555}
9556
9557/*
9558 * "bufloaded(expr)" function
9559 */
9560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009561f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009562 typval_T *argvars;
9563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565 buf_T *buf;
9566
9567 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009568 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569}
9570
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009571static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009572
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573/*
9574 * Get buffer by number or pattern.
9575 */
9576 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009577get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009578 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009579 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 int save_magic;
9583 char_u *save_cpo;
9584 buf_T *buf;
9585
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586 if (tv->v_type == VAR_NUMBER)
9587 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009588 if (tv->v_type != VAR_STRING)
9589 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 if (name == NULL || *name == NUL)
9591 return curbuf;
9592 if (name[0] == '$' && name[1] == NUL)
9593 return lastbuf;
9594
9595 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9596 save_magic = p_magic;
9597 p_magic = TRUE;
9598 save_cpo = p_cpo;
9599 p_cpo = (char_u *)"";
9600
9601 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009602 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603
9604 p_magic = save_magic;
9605 p_cpo = save_cpo;
9606
9607 /* If not found, try expanding the name, like done for bufexists(). */
9608 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610
9611 return buf;
9612}
9613
9614/*
9615 * "bufname(expr)" function
9616 */
9617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009619 typval_T *argvars;
9620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621{
9622 buf_T *buf;
9623
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009624 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009626 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009631 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 --emsg_off;
9633}
9634
9635/*
9636 * "bufnr(expr)" function
9637 */
9638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009640 typval_T *argvars;
9641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642{
9643 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009644 int error = FALSE;
9645 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009647 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009649 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009650 --emsg_off;
9651
9652 /* If the buffer isn't found and the second argument is not zero create a
9653 * new buffer. */
9654 if (buf == NULL
9655 && argvars[1].v_type != VAR_UNKNOWN
9656 && get_tv_number_chk(&argvars[1], &error) != 0
9657 && !error
9658 && (name = get_tv_string_chk(&argvars[0])) != NULL
9659 && !error)
9660 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9661
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009663 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009665 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666}
9667
9668/*
9669 * "bufwinnr(nr)" function
9670 */
9671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009673 typval_T *argvars;
9674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675{
9676#ifdef FEAT_WINDOWS
9677 win_T *wp;
9678 int winnr = 0;
9679#endif
9680 buf_T *buf;
9681
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009682 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009684 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685#ifdef FEAT_WINDOWS
9686 for (wp = firstwin; wp; wp = wp->w_next)
9687 {
9688 ++winnr;
9689 if (wp->w_buffer == buf)
9690 break;
9691 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695#endif
9696 --emsg_off;
9697}
9698
9699/*
9700 * "byte2line(byte)" function
9701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009704 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
9707#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709#else
9710 long boff = 0;
9711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009712 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 (linenr_T)0, &boff);
9718#endif
9719}
9720
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009721 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009722byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009723 typval_T *argvars;
9724 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009725 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009726{
9727#ifdef FEAT_MBYTE
9728 char_u *t;
9729#endif
9730 char_u *str;
9731 long idx;
9732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009733 str = get_tv_string_chk(&argvars[0]);
9734 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009735 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009736 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009737 return;
9738
9739#ifdef FEAT_MBYTE
9740 t = str;
9741 for ( ; idx > 0; idx--)
9742 {
9743 if (*t == NUL) /* EOL reached */
9744 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009745 if (enc_utf8 && comp)
9746 t += utf_ptr2len(t);
9747 else
9748 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009749 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009750 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009751#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009752 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009753 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009754#endif
9755}
9756
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009757/*
9758 * "byteidx()" function
9759 */
9760 static void
9761f_byteidx(argvars, rettv)
9762 typval_T *argvars;
9763 typval_T *rettv;
9764{
9765 byteidx(argvars, rettv, FALSE);
9766}
9767
9768/*
9769 * "byteidxcomp()" function
9770 */
9771 static void
9772f_byteidxcomp(argvars, rettv)
9773 typval_T *argvars;
9774 typval_T *rettv;
9775{
9776 byteidx(argvars, rettv, TRUE);
9777}
9778
Bram Moolenaardb913952012-06-29 12:54:53 +02009779 int
9780func_call(name, args, selfdict, rettv)
9781 char_u *name;
9782 typval_T *args;
9783 dict_T *selfdict;
9784 typval_T *rettv;
9785{
9786 listitem_T *item;
9787 typval_T argv[MAX_FUNC_ARGS + 1];
9788 int argc = 0;
9789 int dummy;
9790 int r = 0;
9791
9792 for (item = args->vval.v_list->lv_first; item != NULL;
9793 item = item->li_next)
9794 {
9795 if (argc == MAX_FUNC_ARGS)
9796 {
9797 EMSG(_("E699: Too many arguments"));
9798 break;
9799 }
9800 /* Make a copy of each argument. This is needed to be able to set
9801 * v_lock to VAR_FIXED in the copy without changing the original list.
9802 */
9803 copy_tv(&item->li_tv, &argv[argc++]);
9804 }
9805
9806 if (item == NULL)
9807 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9808 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9809 &dummy, TRUE, selfdict);
9810
9811 /* Free the arguments. */
9812 while (argc > 0)
9813 clear_tv(&argv[--argc]);
9814
9815 return r;
9816}
9817
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009818/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009819 * "call(func, arglist)" function
9820 */
9821 static void
9822f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009823 typval_T *argvars;
9824 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009825{
9826 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009827 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009828
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009829 if (argvars[1].v_type != VAR_LIST)
9830 {
9831 EMSG(_(e_listreq));
9832 return;
9833 }
9834 if (argvars[1].vval.v_list == NULL)
9835 return;
9836
9837 if (argvars[0].v_type == VAR_FUNC)
9838 func = argvars[0].vval.v_string;
9839 else
9840 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009841 if (*func == NUL)
9842 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009843
Bram Moolenaare9a41262005-01-15 22:18:47 +00009844 if (argvars[2].v_type != VAR_UNKNOWN)
9845 {
9846 if (argvars[2].v_type != VAR_DICT)
9847 {
9848 EMSG(_(e_dictreq));
9849 return;
9850 }
9851 selfdict = argvars[2].vval.v_dict;
9852 }
9853
Bram Moolenaardb913952012-06-29 12:54:53 +02009854 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009855}
9856
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009857#ifdef FEAT_FLOAT
9858/*
9859 * "ceil({float})" function
9860 */
9861 static void
9862f_ceil(argvars, rettv)
9863 typval_T *argvars;
9864 typval_T *rettv;
9865{
9866 float_T f;
9867
9868 rettv->v_type = VAR_FLOAT;
9869 if (get_float_arg(argvars, &f) == OK)
9870 rettv->vval.v_float = ceil(f);
9871 else
9872 rettv->vval.v_float = 0.0;
9873}
9874#endif
9875
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009876/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009877 * "changenr()" function
9878 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009879 static void
9880f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009881 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009882 typval_T *rettv;
9883{
9884 rettv->vval.v_number = curbuf->b_u_seq_cur;
9885}
9886
9887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 * "char2nr(string)" function
9889 */
9890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009891f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009892 typval_T *argvars;
9893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894{
9895#ifdef FEAT_MBYTE
9896 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009897 {
9898 int utf8 = 0;
9899
9900 if (argvars[1].v_type != VAR_UNKNOWN)
9901 utf8 = get_tv_number_chk(&argvars[1], NULL);
9902
9903 if (utf8)
9904 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9905 else
9906 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 else
9909#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009910 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911}
9912
9913/*
9914 * "cindent(lnum)" function
9915 */
9916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009917f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009918 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920{
9921#ifdef FEAT_CINDENT
9922 pos_T pos;
9923 linenr_T lnum;
9924
9925 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009926 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9928 {
9929 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009930 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 curwin->w_cursor = pos;
9932 }
9933 else
9934#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009935 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936}
9937
9938/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009939 * "clearmatches()" function
9940 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009941 static void
9942f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009943 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009944 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009945{
9946#ifdef FEAT_SEARCH_EXTRA
9947 clear_matches(curwin);
9948#endif
9949}
9950
9951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 * "col(string)" function
9953 */
9954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009955f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009956 typval_T *argvars;
9957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958{
9959 colnr_T col = 0;
9960 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009961 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009963 fp = var2fpos(&argvars[0], FALSE, &fnum);
9964 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 {
9966 if (fp->col == MAXCOL)
9967 {
9968 /* '> can be MAXCOL, get the length of the line then */
9969 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009970 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 else
9972 col = MAXCOL;
9973 }
9974 else
9975 {
9976 col = fp->col + 1;
9977#ifdef FEAT_VIRTUALEDIT
9978 /* col(".") when the cursor is on the NUL at the end of the line
9979 * because of "coladd" can be seen as an extra column. */
9980 if (virtual_active() && fp == &curwin->w_cursor)
9981 {
9982 char_u *p = ml_get_cursor();
9983
9984 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9985 curwin->w_virtcol - curwin->w_cursor.coladd))
9986 {
9987# ifdef FEAT_MBYTE
9988 int l;
9989
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009990 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 col += l;
9992# else
9993 if (*p != NUL && p[1] == NUL)
9994 ++col;
9995# endif
9996 }
9997 }
9998#endif
9999 }
10000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002}
10003
Bram Moolenaar572cb562005-08-05 21:35:02 +000010004#if defined(FEAT_INS_EXPAND)
10005/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010006 * "complete()" function
10007 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010008 static void
10009f_complete(argvars, rettv)
10010 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010011 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +000010012{
10013 int startcol;
10014
10015 if ((State & INSERT) == 0)
10016 {
10017 EMSG(_("E785: complete() can only be used in Insert mode"));
10018 return;
10019 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010020
10021 /* Check for undo allowed here, because if something was already inserted
10022 * the line was already saved for undo and this check isn't done. */
10023 if (!undo_allowed())
10024 return;
10025
Bram Moolenaarade00832006-03-10 21:46:58 +000010026 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10027 {
10028 EMSG(_(e_invarg));
10029 return;
10030 }
10031
10032 startcol = get_tv_number_chk(&argvars[0], NULL);
10033 if (startcol <= 0)
10034 return;
10035
10036 set_completion(startcol - 1, argvars[1].vval.v_list);
10037}
10038
10039/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010040 * "complete_add()" function
10041 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010042 static void
10043f_complete_add(argvars, rettv)
10044 typval_T *argvars;
10045 typval_T *rettv;
10046{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010047 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010048}
10049
10050/*
10051 * "complete_check()" function
10052 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010053 static void
10054f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010055 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +000010056 typval_T *rettv;
10057{
10058 int saved = RedrawingDisabled;
10059
10060 RedrawingDisabled = 0;
10061 ins_compl_check_keys(0);
10062 rettv->vval.v_number = compl_interrupted;
10063 RedrawingDisabled = saved;
10064}
10065#endif
10066
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067/*
10068 * "confirm(message, buttons[, default [, type]])" function
10069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010072 typval_T *argvars UNUSED;
10073 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074{
10075#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10076 char_u *message;
10077 char_u *buttons = NULL;
10078 char_u buf[NUMBUFLEN];
10079 char_u buf2[NUMBUFLEN];
10080 int def = 1;
10081 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010082 char_u *typestr;
10083 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010085 message = get_tv_string_chk(&argvars[0]);
10086 if (message == NULL)
10087 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010088 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010090 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10091 if (buttons == NULL)
10092 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010093 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010095 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010096 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10099 if (typestr == NULL)
10100 error = TRUE;
10101 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010103 switch (TOUPPER_ASC(*typestr))
10104 {
10105 case 'E': type = VIM_ERROR; break;
10106 case 'Q': type = VIM_QUESTION; break;
10107 case 'I': type = VIM_INFO; break;
10108 case 'W': type = VIM_WARNING; break;
10109 case 'G': type = VIM_GENERIC; break;
10110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 }
10112 }
10113 }
10114 }
10115
10116 if (buttons == NULL || *buttons == NUL)
10117 buttons = (char_u *)_("&Ok");
10118
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010119 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010120 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010121 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122#endif
10123}
10124
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010125/*
10126 * "copy()" function
10127 */
10128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010129f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010130 typval_T *argvars;
10131 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010132{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010133 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010134}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010136#ifdef FEAT_FLOAT
10137/*
10138 * "cos()" function
10139 */
10140 static void
10141f_cos(argvars, rettv)
10142 typval_T *argvars;
10143 typval_T *rettv;
10144{
10145 float_T f;
10146
10147 rettv->v_type = VAR_FLOAT;
10148 if (get_float_arg(argvars, &f) == OK)
10149 rettv->vval.v_float = cos(f);
10150 else
10151 rettv->vval.v_float = 0.0;
10152}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010153
10154/*
10155 * "cosh()" function
10156 */
10157 static void
10158f_cosh(argvars, rettv)
10159 typval_T *argvars;
10160 typval_T *rettv;
10161{
10162 float_T f;
10163
10164 rettv->v_type = VAR_FLOAT;
10165 if (get_float_arg(argvars, &f) == OK)
10166 rettv->vval.v_float = cosh(f);
10167 else
10168 rettv->vval.v_float = 0.0;
10169}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010170#endif
10171
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010173 * "count()" function
10174 */
10175 static void
10176f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010177 typval_T *argvars;
10178 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010179{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010180 long n = 0;
10181 int ic = FALSE;
10182
Bram Moolenaare9a41262005-01-15 22:18:47 +000010183 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010184 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010185 listitem_T *li;
10186 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010187 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010188
Bram Moolenaare9a41262005-01-15 22:18:47 +000010189 if ((l = argvars[0].vval.v_list) != NULL)
10190 {
10191 li = l->lv_first;
10192 if (argvars[2].v_type != VAR_UNKNOWN)
10193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 int error = FALSE;
10195
10196 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010197 if (argvars[3].v_type != VAR_UNKNOWN)
10198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010199 idx = get_tv_number_chk(&argvars[3], &error);
10200 if (!error)
10201 {
10202 li = list_find(l, idx);
10203 if (li == NULL)
10204 EMSGN(_(e_listidx), idx);
10205 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010206 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010207 if (error)
10208 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010209 }
10210
10211 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010212 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010213 ++n;
10214 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010215 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010216 else if (argvars[0].v_type == VAR_DICT)
10217 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010218 int todo;
10219 dict_T *d;
10220 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010221
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010222 if ((d = argvars[0].vval.v_dict) != NULL)
10223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010224 int error = FALSE;
10225
Bram Moolenaare9a41262005-01-15 22:18:47 +000010226 if (argvars[2].v_type != VAR_UNKNOWN)
10227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010229 if (argvars[3].v_type != VAR_UNKNOWN)
10230 EMSG(_(e_invarg));
10231 }
10232
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010233 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010234 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010235 {
10236 if (!HASHITEM_EMPTY(hi))
10237 {
10238 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010239 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010240 ++n;
10241 }
10242 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 }
10244 }
10245 else
10246 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010247 rettv->vval.v_number = n;
10248}
10249
10250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10252 *
10253 * Checks the existence of a cscope connection.
10254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010256f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010257 typval_T *argvars UNUSED;
10258 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259{
10260#ifdef FEAT_CSCOPE
10261 int num = 0;
10262 char_u *dbpath = NULL;
10263 char_u *prepend = NULL;
10264 char_u buf[NUMBUFLEN];
10265
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010266 if (argvars[0].v_type != VAR_UNKNOWN
10267 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010269 num = (int)get_tv_number(&argvars[0]);
10270 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010271 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010272 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 }
10274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010275 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276#endif
10277}
10278
10279/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010280 * "cursor(lnum, col)" function, or
10281 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010283 * Moves the cursor to the specified line and column.
10284 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010287f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010288 typval_T *argvars;
10289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290{
10291 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010292#ifdef FEAT_VIRTUALEDIT
10293 long coladd = 0;
10294#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010295 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010297 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010298 if (argvars[1].v_type == VAR_UNKNOWN)
10299 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010300 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010301 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010302
Bram Moolenaar493c1782014-05-28 14:34:46 +020010303 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010304 {
10305 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010306 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010307 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010308 line = pos.lnum;
10309 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010310#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010311 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010312#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010313 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010314 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010315 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010316 set_curswant = FALSE;
10317 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010318 }
10319 else
10320 {
10321 line = get_tv_lnum(argvars);
10322 col = get_tv_number_chk(&argvars[1], NULL);
10323#ifdef FEAT_VIRTUALEDIT
10324 if (argvars[2].v_type != VAR_UNKNOWN)
10325 coladd = get_tv_number_chk(&argvars[2], NULL);
10326#endif
10327 }
10328 if (line < 0 || col < 0
10329#ifdef FEAT_VIRTUALEDIT
10330 || coladd < 0
10331#endif
10332 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010333 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 if (line > 0)
10335 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 if (col > 0)
10337 curwin->w_cursor.col = col - 1;
10338#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010339 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340#endif
10341
10342 /* Make sure the cursor is in a valid position. */
10343 check_cursor();
10344#ifdef FEAT_MBYTE
10345 /* Correct cursor for multi-byte character. */
10346 if (has_mbyte)
10347 mb_adjust_cursor();
10348#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010349
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010350 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010351 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352}
10353
10354/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010355 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 */
10357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010358f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010359 typval_T *argvars;
10360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010362 int noref = 0;
10363
10364 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010365 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010366 if (noref < 0 || noref > 1)
10367 EMSG(_(e_invarg));
10368 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010369 {
10370 current_copyID += COPYID_INC;
10371 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373}
10374
10375/*
10376 * "delete()" function
10377 */
10378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010379f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010380 typval_T *argvars;
10381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010383 char_u nbuf[NUMBUFLEN];
10384 char_u *name;
10385 char_u *flags;
10386
10387 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010389 return;
10390
10391 name = get_tv_string(&argvars[0]);
10392 if (name == NULL || *name == NUL)
10393 {
10394 EMSG(_(e_invarg));
10395 return;
10396 }
10397
10398 if (argvars[1].v_type != VAR_UNKNOWN)
10399 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010401 flags = (char_u *)"";
10402
10403 if (*flags == NUL)
10404 /* delete a file */
10405 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10406 else if (STRCMP(flags, "d") == 0)
10407 /* delete an empty directory */
10408 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10409 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010410 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010411 rettv->vval.v_number = delete_recursive(name);
10412 else
10413 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414}
10415
10416/*
10417 * "did_filetype()" function
10418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010420f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010421 typval_T *argvars UNUSED;
10422 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423{
10424#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010425 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426#endif
10427}
10428
10429/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010430 * "diff_filler()" function
10431 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010433f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010434 typval_T *argvars UNUSED;
10435 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010436{
10437#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010438 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010439#endif
10440}
10441
10442/*
10443 * "diff_hlID()" function
10444 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010447 typval_T *argvars UNUSED;
10448 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010449{
10450#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010451 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010452 static linenr_T prev_lnum = 0;
10453 static int changedtick = 0;
10454 static int fnum = 0;
10455 static int change_start = 0;
10456 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010457 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010458 int filler_lines;
10459 int col;
10460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010461 if (lnum < 0) /* ignore type error in {lnum} arg */
10462 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010463 if (lnum != prev_lnum
10464 || changedtick != curbuf->b_changedtick
10465 || fnum != curbuf->b_fnum)
10466 {
10467 /* New line, buffer, change: need to get the values. */
10468 filler_lines = diff_check(curwin, lnum);
10469 if (filler_lines < 0)
10470 {
10471 if (filler_lines == -1)
10472 {
10473 change_start = MAXCOL;
10474 change_end = -1;
10475 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10476 hlID = HLF_ADD; /* added line */
10477 else
10478 hlID = HLF_CHD; /* changed line */
10479 }
10480 else
10481 hlID = HLF_ADD; /* added line */
10482 }
10483 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010484 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010485 prev_lnum = lnum;
10486 changedtick = curbuf->b_changedtick;
10487 fnum = curbuf->b_fnum;
10488 }
10489
10490 if (hlID == HLF_CHD || hlID == HLF_TXD)
10491 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010492 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010493 if (col >= change_start && col <= change_end)
10494 hlID = HLF_TXD; /* changed text */
10495 else
10496 hlID = HLF_CHD; /* changed line */
10497 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010498 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010499#endif
10500}
10501
10502/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010503 * "empty({expr})" function
10504 */
10505 static void
10506f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010507 typval_T *argvars;
10508 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010509{
10510 int n;
10511
10512 switch (argvars[0].v_type)
10513 {
10514 case VAR_STRING:
10515 case VAR_FUNC:
10516 n = argvars[0].vval.v_string == NULL
10517 || *argvars[0].vval.v_string == NUL;
10518 break;
10519 case VAR_NUMBER:
10520 n = argvars[0].vval.v_number == 0;
10521 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010522#ifdef FEAT_FLOAT
10523 case VAR_FLOAT:
10524 n = argvars[0].vval.v_float == 0.0;
10525 break;
10526#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010527 case VAR_LIST:
10528 n = argvars[0].vval.v_list == NULL
10529 || argvars[0].vval.v_list->lv_first == NULL;
10530 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010531 case VAR_DICT:
10532 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010533 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010534 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010535 default:
10536 EMSG2(_(e_intern2), "f_empty()");
10537 n = 0;
10538 }
10539
10540 rettv->vval.v_number = n;
10541}
10542
10543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 * "escape({string}, {chars})" function
10545 */
10546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010547f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010548 typval_T *argvars;
10549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550{
10551 char_u buf[NUMBUFLEN];
10552
Bram Moolenaar758711c2005-02-02 23:11:38 +000010553 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10554 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556}
10557
10558/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010559 * "eval()" function
10560 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010561 static void
10562f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010563 typval_T *argvars;
10564 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010565{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010566 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010568 s = get_tv_string_chk(&argvars[0]);
10569 if (s != NULL)
10570 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010571
Bram Moolenaar615b9972015-01-14 17:15:05 +010010572 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010573 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10574 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010575 if (p != NULL && !aborting())
10576 EMSG2(_(e_invexpr2), p);
10577 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010578 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010579 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010580 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010581 else if (*s != NUL)
10582 EMSG(_(e_trailing));
10583}
10584
10585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 * "eventhandler()" function
10587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010589f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010590 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594}
10595
10596/*
10597 * "executable()" function
10598 */
10599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010600f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010601 typval_T *argvars;
10602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010604 char_u *name = get_tv_string(&argvars[0]);
10605
10606 /* Check in $PATH and also check directly if there is a directory name. */
10607 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10608 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010609}
10610
10611/*
10612 * "exepath()" function
10613 */
10614 static void
10615f_exepath(argvars, rettv)
10616 typval_T *argvars;
10617 typval_T *rettv;
10618{
10619 char_u *p = NULL;
10620
Bram Moolenaarb5971142015-03-21 17:32:19 +010010621 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010622 rettv->v_type = VAR_STRING;
10623 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624}
10625
10626/*
10627 * "exists()" function
10628 */
10629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010630f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010631 typval_T *argvars;
10632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633{
10634 char_u *p;
10635 char_u *name;
10636 int n = FALSE;
10637 int len = 0;
10638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010639 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 if (*p == '$') /* environment variable */
10641 {
10642 /* first try "normal" environment variables (fast) */
10643 if (mch_getenv(p + 1) != NULL)
10644 n = TRUE;
10645 else
10646 {
10647 /* try expanding things like $VIM and ${HOME} */
10648 p = expand_env_save(p);
10649 if (p != NULL && *p != '$')
10650 n = TRUE;
10651 vim_free(p);
10652 }
10653 }
10654 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010657 if (*skipwhite(p) != NUL)
10658 n = FALSE; /* trailing garbage */
10659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 else if (*p == '*') /* internal or user defined function */
10661 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010662 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 }
10664 else if (*p == ':')
10665 {
10666 n = cmd_exists(p + 1);
10667 }
10668 else if (*p == '#')
10669 {
10670#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010671 if (p[1] == '#')
10672 n = autocmd_supported(p + 2);
10673 else
10674 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675#endif
10676 }
10677 else /* internal variable */
10678 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010679 char_u *tofree;
10680 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010682 /* get_name_len() takes care of expanding curly braces */
10683 name = p;
10684 len = get_name_len(&p, &tofree, TRUE, FALSE);
10685 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010687 if (tofree != NULL)
10688 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010689 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010690 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010692 /* handle d.key, l[idx], f(expr) */
10693 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10694 if (n)
10695 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 }
10697 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010698 if (*p != NUL)
10699 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010701 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 }
10703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705}
10706
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010707#ifdef FEAT_FLOAT
10708/*
10709 * "exp()" function
10710 */
10711 static void
10712f_exp(argvars, rettv)
10713 typval_T *argvars;
10714 typval_T *rettv;
10715{
10716 float_T f;
10717
10718 rettv->v_type = VAR_FLOAT;
10719 if (get_float_arg(argvars, &f) == OK)
10720 rettv->vval.v_float = exp(f);
10721 else
10722 rettv->vval.v_float = 0.0;
10723}
10724#endif
10725
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726/*
10727 * "expand()" function
10728 */
10729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010730f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010731 typval_T *argvars;
10732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733{
10734 char_u *s;
10735 int len;
10736 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010737 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010739 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010740 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010742 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010743 if (argvars[1].v_type != VAR_UNKNOWN
10744 && argvars[2].v_type != VAR_UNKNOWN
10745 && get_tv_number_chk(&argvars[2], &error)
10746 && !error)
10747 {
10748 rettv->v_type = VAR_LIST;
10749 rettv->vval.v_list = NULL;
10750 }
10751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 if (*s == '%' || *s == '#' || *s == '<')
10754 {
10755 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010756 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010758 if (rettv->v_type == VAR_LIST)
10759 {
10760 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10761 list_append_string(rettv->vval.v_list, result, -1);
10762 else
10763 vim_free(result);
10764 }
10765 else
10766 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 }
10768 else
10769 {
10770 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010771 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010772 if (argvars[1].v_type != VAR_UNKNOWN
10773 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010774 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010775 if (!error)
10776 {
10777 ExpandInit(&xpc);
10778 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010779 if (p_wic)
10780 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010781 if (rettv->v_type == VAR_STRING)
10782 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10783 options, WILD_ALL);
10784 else if (rettv_list_alloc(rettv) != FAIL)
10785 {
10786 int i;
10787
10788 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10789 for (i = 0; i < xpc.xp_numfiles; i++)
10790 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10791 ExpandCleanup(&xpc);
10792 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010793 }
10794 else
10795 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 }
10797}
10798
10799/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010800 * Go over all entries in "d2" and add them to "d1".
10801 * When "action" is "error" then a duplicate key is an error.
10802 * When "action" is "force" then a duplicate key is overwritten.
10803 * Otherwise duplicate keys are ignored ("action" is "keep").
10804 */
10805 void
10806dict_extend(d1, d2, action)
10807 dict_T *d1;
10808 dict_T *d2;
10809 char_u *action;
10810{
10811 dictitem_T *di1;
10812 hashitem_T *hi2;
10813 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010814 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010815
10816 todo = (int)d2->dv_hashtab.ht_used;
10817 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10818 {
10819 if (!HASHITEM_EMPTY(hi2))
10820 {
10821 --todo;
10822 di1 = dict_find(d1, hi2->hi_key, -1);
10823 if (d1->dv_scope != 0)
10824 {
10825 /* Disallow replacing a builtin function in l: and g:.
10826 * Check the key to be valid when adding to any
10827 * scope. */
10828 if (d1->dv_scope == VAR_DEF_SCOPE
10829 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10830 && var_check_func_name(hi2->hi_key,
10831 di1 == NULL))
10832 break;
10833 if (!valid_varname(hi2->hi_key))
10834 break;
10835 }
10836 if (di1 == NULL)
10837 {
10838 di1 = dictitem_copy(HI2DI(hi2));
10839 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10840 dictitem_free(di1);
10841 }
10842 else if (*action == 'e')
10843 {
10844 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10845 break;
10846 }
10847 else if (*action == 'f' && HI2DI(hi2) != di1)
10848 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010849 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10850 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010851 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010852 clear_tv(&di1->di_tv);
10853 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10854 }
10855 }
10856 }
10857}
10858
10859/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010860 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010861 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010862 */
10863 static void
10864f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010865 typval_T *argvars;
10866 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010867{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010868 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010869
Bram Moolenaare9a41262005-01-15 22:18:47 +000010870 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010871 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010872 list_T *l1, *l2;
10873 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010874 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010875 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010876
Bram Moolenaare9a41262005-01-15 22:18:47 +000010877 l1 = argvars[0].vval.v_list;
10878 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010879 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010880 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010881 {
10882 if (argvars[2].v_type != VAR_UNKNOWN)
10883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010884 before = get_tv_number_chk(&argvars[2], &error);
10885 if (error)
10886 return; /* type error; errmsg already given */
10887
Bram Moolenaar758711c2005-02-02 23:11:38 +000010888 if (before == l1->lv_len)
10889 item = NULL;
10890 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010891 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010892 item = list_find(l1, before);
10893 if (item == NULL)
10894 {
10895 EMSGN(_(e_listidx), before);
10896 return;
10897 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010898 }
10899 }
10900 else
10901 item = NULL;
10902 list_extend(l1, l2, item);
10903
Bram Moolenaare9a41262005-01-15 22:18:47 +000010904 copy_tv(&argvars[0], rettv);
10905 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010906 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010907 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10908 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010909 dict_T *d1, *d2;
10910 char_u *action;
10911 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010912
10913 d1 = argvars[0].vval.v_dict;
10914 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010915 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010916 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010917 {
10918 /* Check the third argument. */
10919 if (argvars[2].v_type != VAR_UNKNOWN)
10920 {
10921 static char *(av[]) = {"keep", "force", "error"};
10922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010923 action = get_tv_string_chk(&argvars[2]);
10924 if (action == NULL)
10925 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010926 for (i = 0; i < 3; ++i)
10927 if (STRCMP(action, av[i]) == 0)
10928 break;
10929 if (i == 3)
10930 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010931 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010932 return;
10933 }
10934 }
10935 else
10936 action = (char_u *)"force";
10937
Bram Moolenaara9922d62013-05-30 13:01:18 +020010938 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010939
Bram Moolenaare9a41262005-01-15 22:18:47 +000010940 copy_tv(&argvars[0], rettv);
10941 }
10942 }
10943 else
10944 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010945}
10946
10947/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010948 * "feedkeys()" function
10949 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010950 static void
10951f_feedkeys(argvars, rettv)
10952 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010953 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010954{
10955 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010956 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010957 char_u *keys, *flags;
10958 char_u nbuf[NUMBUFLEN];
10959 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010960 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010961
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010962 /* This is not allowed in the sandbox. If the commands would still be
10963 * executed in the sandbox it would be OK, but it probably happens later,
10964 * when "sandbox" is no longer set. */
10965 if (check_secure())
10966 return;
10967
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010968 keys = get_tv_string(&argvars[0]);
10969 if (*keys != NUL)
10970 {
10971 if (argvars[1].v_type != VAR_UNKNOWN)
10972 {
10973 flags = get_tv_string_buf(&argvars[1], nbuf);
10974 for ( ; *flags != NUL; ++flags)
10975 {
10976 switch (*flags)
10977 {
10978 case 'n': remap = FALSE; break;
10979 case 'm': remap = TRUE; break;
10980 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010981 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010982 }
10983 }
10984 }
10985
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010986 /* Need to escape K_SPECIAL and CSI before putting the string in the
10987 * typeahead buffer. */
10988 keys_esc = vim_strsave_escape_csi(keys);
10989 if (keys_esc != NULL)
10990 {
10991 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010992 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010993 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010994 if (vgetc_busy)
10995 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010996 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010997 }
10998}
10999
11000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 * "filereadable()" function
11002 */
11003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011005 typval_T *argvars;
11006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011008 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 char_u *p;
11010 int n;
11011
Bram Moolenaarc236c162008-07-13 17:41:49 +000011012#ifndef O_NONBLOCK
11013# define O_NONBLOCK 0
11014#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011015 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011016 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11017 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 {
11019 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011020 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 }
11022 else
11023 n = FALSE;
11024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026}
11027
11028/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011029 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 * rights to write into.
11031 */
11032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011034 typval_T *argvars;
11035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011037 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038}
11039
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011040static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011041
11042 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011043findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011044 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011045 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011046 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011047{
11048#ifdef FEAT_SEARCHPATH
11049 char_u *fname;
11050 char_u *fresult = NULL;
11051 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11052 char_u *p;
11053 char_u pathbuf[NUMBUFLEN];
11054 int count = 1;
11055 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011056 int error = FALSE;
11057#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011058
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011059 rettv->vval.v_string = NULL;
11060 rettv->v_type = VAR_STRING;
11061
11062#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011064
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011065 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011067 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11068 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011069 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011070 else
11071 {
11072 if (*p != NUL)
11073 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011074
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011076 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011078 }
11079
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011080 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11081 error = TRUE;
11082
11083 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011084 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011085 do
11086 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011087 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011088 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 fresult = find_file_in_path_option(first ? fname : NULL,
11090 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011091 0, first, path,
11092 find_what,
11093 curbuf->b_ffname,
11094 find_what == FINDFILE_DIR
11095 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011096 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011097
11098 if (fresult != NULL && rettv->v_type == VAR_LIST)
11099 list_append_string(rettv->vval.v_list, fresult, -1);
11100
11101 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011102 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011103
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011104 if (rettv->v_type == VAR_STRING)
11105 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011106#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011107}
11108
Bram Moolenaar33570922005-01-25 22:26:29 +000011109static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
11110static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011111
11112/*
11113 * Implementation of map() and filter().
11114 */
11115 static void
11116filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011117 typval_T *argvars;
11118 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011119 int map;
11120{
11121 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011122 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011123 listitem_T *li, *nli;
11124 list_T *l = NULL;
11125 dictitem_T *di;
11126 hashtab_T *ht;
11127 hashitem_T *hi;
11128 dict_T *d = NULL;
11129 typval_T save_val;
11130 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011131 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011132 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011133 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011134 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011135 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011136 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011137 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011138
Bram Moolenaare9a41262005-01-15 22:18:47 +000011139 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011140 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011141 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011142 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011143 return;
11144 }
11145 else if (argvars[0].v_type == VAR_DICT)
11146 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011147 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011148 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011149 return;
11150 }
11151 else
11152 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011153 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011154 return;
11155 }
11156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011157 expr = get_tv_string_buf_chk(&argvars[1], buf);
11158 /* On type errors, the preceding call has already displayed an error
11159 * message. Avoid a misleading error message for an empty string that
11160 * was not passed as argument. */
11161 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011163 prepare_vimvar(VV_VAL, &save_val);
11164 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011165
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011166 /* We reset "did_emsg" to be able to detect whether an error
11167 * occurred during evaluation of the expression. */
11168 save_did_emsg = did_emsg;
11169 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011170
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011171 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011172 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011174 vimvars[VV_KEY].vv_type = VAR_STRING;
11175
11176 ht = &d->dv_hashtab;
11177 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011178 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011179 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011180 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011181 if (!HASHITEM_EMPTY(hi))
11182 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011183 int r;
11184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011185 --todo;
11186 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011187 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011188 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11189 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011190 break;
11191 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011192 r = filter_map_one(&di->di_tv, expr, map, &rem);
11193 clear_tv(&vimvars[VV_KEY].vv_tv);
11194 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011195 break;
11196 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011197 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011198 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11199 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011200 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011201 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011202 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011203 }
11204 }
11205 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011206 }
11207 else
11208 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011209 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011211 for (li = l->lv_first; li != NULL; li = nli)
11212 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011213 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011214 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011215 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011216 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011217 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011218 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011219 break;
11220 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011221 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011222 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011223 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011224 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011225
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011226 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011227 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011228
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011229 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011230 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011231
11232 copy_tv(&argvars[0], rettv);
11233}
11234
11235 static int
11236filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011237 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011238 char_u *expr;
11239 int map;
11240 int *remp;
11241{
Bram Moolenaar33570922005-01-25 22:26:29 +000011242 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011243 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011244 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011245
Bram Moolenaar33570922005-01-25 22:26:29 +000011246 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011247 s = expr;
11248 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011249 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011250 if (*s != NUL) /* check for trailing chars after expr */
11251 {
11252 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011253 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011254 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011255 }
11256 if (map)
11257 {
11258 /* map(): replace the list item value */
11259 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011260 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011261 *tv = rettv;
11262 }
11263 else
11264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011265 int error = FALSE;
11266
Bram Moolenaare9a41262005-01-15 22:18:47 +000011267 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011268 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011269 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011270 /* On type error, nothing has been removed; return FAIL to stop the
11271 * loop. The error message was given by get_tv_number_chk(). */
11272 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011273 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011274 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011275 retval = OK;
11276theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011277 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011278 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011279}
11280
11281/*
11282 * "filter()" function
11283 */
11284 static void
11285f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011286 typval_T *argvars;
11287 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011288{
11289 filter_map(argvars, rettv, FALSE);
11290}
11291
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011292/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011293 * "finddir({fname}[, {path}[, {count}]])" function
11294 */
11295 static void
11296f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011297 typval_T *argvars;
11298 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011299{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011300 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011301}
11302
11303/*
11304 * "findfile({fname}[, {path}[, {count}]])" function
11305 */
11306 static void
11307f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011308 typval_T *argvars;
11309 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011310{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011311 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011312}
11313
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011314#ifdef FEAT_FLOAT
11315/*
11316 * "float2nr({float})" function
11317 */
11318 static void
11319f_float2nr(argvars, rettv)
11320 typval_T *argvars;
11321 typval_T *rettv;
11322{
11323 float_T f;
11324
11325 if (get_float_arg(argvars, &f) == OK)
11326 {
11327 if (f < -0x7fffffff)
11328 rettv->vval.v_number = -0x7fffffff;
11329 else if (f > 0x7fffffff)
11330 rettv->vval.v_number = 0x7fffffff;
11331 else
11332 rettv->vval.v_number = (varnumber_T)f;
11333 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011334}
11335
11336/*
11337 * "floor({float})" function
11338 */
11339 static void
11340f_floor(argvars, rettv)
11341 typval_T *argvars;
11342 typval_T *rettv;
11343{
11344 float_T f;
11345
11346 rettv->v_type = VAR_FLOAT;
11347 if (get_float_arg(argvars, &f) == OK)
11348 rettv->vval.v_float = floor(f);
11349 else
11350 rettv->vval.v_float = 0.0;
11351}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011352
11353/*
11354 * "fmod()" function
11355 */
11356 static void
11357f_fmod(argvars, rettv)
11358 typval_T *argvars;
11359 typval_T *rettv;
11360{
11361 float_T fx, fy;
11362
11363 rettv->v_type = VAR_FLOAT;
11364 if (get_float_arg(argvars, &fx) == OK
11365 && get_float_arg(&argvars[1], &fy) == OK)
11366 rettv->vval.v_float = fmod(fx, fy);
11367 else
11368 rettv->vval.v_float = 0.0;
11369}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011370#endif
11371
Bram Moolenaar0d660222005-01-07 21:51:51 +000011372/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011373 * "fnameescape({string})" function
11374 */
11375 static void
11376f_fnameescape(argvars, rettv)
11377 typval_T *argvars;
11378 typval_T *rettv;
11379{
11380 rettv->vval.v_string = vim_strsave_fnameescape(
11381 get_tv_string(&argvars[0]), FALSE);
11382 rettv->v_type = VAR_STRING;
11383}
11384
11385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386 * "fnamemodify({fname}, {mods})" function
11387 */
11388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011390 typval_T *argvars;
11391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392{
11393 char_u *fname;
11394 char_u *mods;
11395 int usedlen = 0;
11396 int len;
11397 char_u *fbuf = NULL;
11398 char_u buf[NUMBUFLEN];
11399
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011400 fname = get_tv_string_chk(&argvars[0]);
11401 mods = get_tv_string_buf_chk(&argvars[1], buf);
11402 if (fname == NULL || mods == NULL)
11403 fname = NULL;
11404 else
11405 {
11406 len = (int)STRLEN(fname);
11407 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 vim_free(fbuf);
11416}
11417
Bram Moolenaar33570922005-01-25 22:26:29 +000011418static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419
11420/*
11421 * "foldclosed()" function
11422 */
11423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011424foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011425 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011426 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011427 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428{
11429#ifdef FEAT_FOLDING
11430 linenr_T lnum;
11431 linenr_T first, last;
11432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011433 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11435 {
11436 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11437 {
11438 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442 return;
11443 }
11444 }
11445#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447}
11448
11449/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011450 * "foldclosed()" function
11451 */
11452 static void
11453f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011454 typval_T *argvars;
11455 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011456{
11457 foldclosed_both(argvars, rettv, FALSE);
11458}
11459
11460/*
11461 * "foldclosedend()" function
11462 */
11463 static void
11464f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011465 typval_T *argvars;
11466 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011467{
11468 foldclosed_both(argvars, rettv, TRUE);
11469}
11470
11471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 * "foldlevel()" function
11473 */
11474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011476 typval_T *argvars UNUSED;
11477 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478{
11479#ifdef FEAT_FOLDING
11480 linenr_T lnum;
11481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486}
11487
11488/*
11489 * "foldtext()" function
11490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011493 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495{
11496#ifdef FEAT_FOLDING
11497 linenr_T lnum;
11498 char_u *s;
11499 char_u *r;
11500 int len;
11501 char *txt;
11502#endif
11503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011504 rettv->v_type = VAR_STRING;
11505 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011507 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11508 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11509 <= curbuf->b_ml.ml_line_count
11510 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511 {
11512 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011513 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11514 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 {
11516 if (!linewhite(lnum))
11517 break;
11518 ++lnum;
11519 }
11520
11521 /* Find interesting text in this line. */
11522 s = skipwhite(ml_get(lnum));
11523 /* skip C comment-start */
11524 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011525 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011527 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011528 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011529 {
11530 s = skipwhite(ml_get(lnum + 1));
11531 if (*s == '*')
11532 s = skipwhite(s + 1);
11533 }
11534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535 txt = _("+-%s%3ld lines: ");
11536 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011537 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538 + 20 /* for %3ld */
11539 + STRLEN(s))); /* concatenated */
11540 if (r != NULL)
11541 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011542 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11543 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11544 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545 len = (int)STRLEN(r);
11546 STRCAT(r, s);
11547 /* remove 'foldmarker' and 'commentstring' */
11548 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011549 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 }
11551 }
11552#endif
11553}
11554
11555/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011556 * "foldtextresult(lnum)" function
11557 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011559f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011560 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011561 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011562{
11563#ifdef FEAT_FOLDING
11564 linenr_T lnum;
11565 char_u *text;
11566 char_u buf[51];
11567 foldinfo_T foldinfo;
11568 int fold_count;
11569#endif
11570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011571 rettv->v_type = VAR_STRING;
11572 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011573#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011574 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011575 /* treat illegal types and illegal string values for {lnum} the same */
11576 if (lnum < 0)
11577 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011578 fold_count = foldedCount(curwin, lnum, &foldinfo);
11579 if (fold_count > 0)
11580 {
11581 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11582 &foldinfo, buf);
11583 if (text == buf)
11584 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011586 }
11587#endif
11588}
11589
11590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591 * "foreground()" function
11592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011594f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011595 typval_T *argvars UNUSED;
11596 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598#ifdef FEAT_GUI
11599 if (gui.in_use)
11600 gui_mch_set_foreground();
11601#else
11602# ifdef WIN32
11603 win32_set_foreground();
11604# endif
11605#endif
11606}
11607
11608/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011609 * "function()" function
11610 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011612f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011613 typval_T *argvars;
11614 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011615{
11616 char_u *s;
11617
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011618 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011619 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011620 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011621 /* Don't check an autoload name for existence here. */
11622 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011623 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011624 else
11625 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011626 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011627 {
11628 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011629 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011630
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011631 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11632 * also be called from another script. Using trans_function_name()
11633 * would also work, but some plugins depend on the name being
11634 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011635 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011636 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011637 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011638 if (rettv->vval.v_string != NULL)
11639 {
11640 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011641 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011642 }
11643 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011644 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011645 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011646 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011647 }
11648}
11649
11650/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011651 * "garbagecollect()" function
11652 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011653 static void
11654f_garbagecollect(argvars, rettv)
11655 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011656 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011657{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011658 /* This is postponed until we are back at the toplevel, because we may be
11659 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11660 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011661
11662 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11663 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011664}
11665
11666/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011667 * "get()" function
11668 */
11669 static void
11670f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011671 typval_T *argvars;
11672 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011673{
Bram Moolenaar33570922005-01-25 22:26:29 +000011674 listitem_T *li;
11675 list_T *l;
11676 dictitem_T *di;
11677 dict_T *d;
11678 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011679
Bram Moolenaare9a41262005-01-15 22:18:47 +000011680 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011681 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011682 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011684 int error = FALSE;
11685
11686 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11687 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011688 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011689 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011690 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011691 else if (argvars[0].v_type == VAR_DICT)
11692 {
11693 if ((d = argvars[0].vval.v_dict) != NULL)
11694 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011695 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011696 if (di != NULL)
11697 tv = &di->di_tv;
11698 }
11699 }
11700 else
11701 EMSG2(_(e_listdictarg), "get()");
11702
11703 if (tv == NULL)
11704 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011705 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011706 copy_tv(&argvars[2], rettv);
11707 }
11708 else
11709 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011710}
11711
Bram Moolenaar342337a2005-07-21 21:11:17 +000011712static 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 +000011713
11714/*
11715 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011716 * Return a range (from start to end) of lines in rettv from the specified
11717 * buffer.
11718 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011719 */
11720 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011721get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011722 buf_T *buf;
11723 linenr_T start;
11724 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011725 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011726 typval_T *rettv;
11727{
11728 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011729
Bram Moolenaar959a1432013-12-14 12:17:38 +010011730 rettv->v_type = VAR_STRING;
11731 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011732 if (retlist && rettv_list_alloc(rettv) == FAIL)
11733 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011734
11735 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11736 return;
11737
11738 if (!retlist)
11739 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011740 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11741 p = ml_get_buf(buf, start, FALSE);
11742 else
11743 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011744 rettv->vval.v_string = vim_strsave(p);
11745 }
11746 else
11747 {
11748 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011749 return;
11750
11751 if (start < 1)
11752 start = 1;
11753 if (end > buf->b_ml.ml_line_count)
11754 end = buf->b_ml.ml_line_count;
11755 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011756 if (list_append_string(rettv->vval.v_list,
11757 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011758 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011759 }
11760}
11761
11762/*
11763 * "getbufline()" function
11764 */
11765 static void
11766f_getbufline(argvars, rettv)
11767 typval_T *argvars;
11768 typval_T *rettv;
11769{
11770 linenr_T lnum;
11771 linenr_T end;
11772 buf_T *buf;
11773
11774 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11775 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011776 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011777 --emsg_off;
11778
Bram Moolenaar661b1822005-07-28 22:36:45 +000011779 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011780 if (argvars[2].v_type == VAR_UNKNOWN)
11781 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011782 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011783 end = get_tv_lnum_buf(&argvars[2], buf);
11784
Bram Moolenaar342337a2005-07-21 21:11:17 +000011785 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011786}
11787
Bram Moolenaar0d660222005-01-07 21:51:51 +000011788/*
11789 * "getbufvar()" function
11790 */
11791 static void
11792f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011793 typval_T *argvars;
11794 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011795{
11796 buf_T *buf;
11797 buf_T *save_curbuf;
11798 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011799 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011800 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011801
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011802 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11803 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011804 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011805 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011806
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011807 rettv->v_type = VAR_STRING;
11808 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011809
11810 if (buf != NULL && varname != NULL)
11811 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011812 /* set curbuf to be our buf, temporarily */
11813 save_curbuf = curbuf;
11814 curbuf = buf;
11815
Bram Moolenaar0d660222005-01-07 21:51:51 +000011816 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011817 {
11818 if (get_option_tv(&varname, rettv, TRUE) == OK)
11819 done = TRUE;
11820 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011821 else if (STRCMP(varname, "changedtick") == 0)
11822 {
11823 rettv->v_type = VAR_NUMBER;
11824 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011825 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011826 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011827 else
11828 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011829 /* Look up the variable. */
11830 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11831 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11832 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011833 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011834 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011835 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011836 done = TRUE;
11837 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011838 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011839
11840 /* restore previous notion of curbuf */
11841 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011842 }
11843
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011844 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11845 /* use the default value */
11846 copy_tv(&argvars[2], rettv);
11847
Bram Moolenaar0d660222005-01-07 21:51:51 +000011848 --emsg_off;
11849}
11850
11851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 * "getchar()" function
11853 */
11854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011856 typval_T *argvars;
11857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858{
11859 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011860 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011862 /* Position the cursor. Needed after a message that ends in a space. */
11863 windgoto(msg_row, msg_col);
11864
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 ++no_mapping;
11866 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011867 for (;;)
11868 {
11869 if (argvars[0].v_type == VAR_UNKNOWN)
11870 /* getchar(): blocking wait. */
11871 n = safe_vgetc();
11872 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11873 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011874 n = vpeekc_any();
11875 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011876 /* illegal argument or getchar(0) and no char avail: return zero */
11877 n = 0;
11878 else
11879 /* getchar(0) and char avail: return char */
11880 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011881
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011882 if (n == K_IGNORE)
11883 continue;
11884 break;
11885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886 --no_mapping;
11887 --allow_keys;
11888
Bram Moolenaar219b8702006-11-01 14:32:36 +000011889 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11890 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11891 vimvars[VV_MOUSE_COL].vv_nr = 0;
11892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011893 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894 if (IS_SPECIAL(n) || mod_mask != 0)
11895 {
11896 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11897 int i = 0;
11898
11899 /* Turn a special key into three bytes, plus modifier. */
11900 if (mod_mask != 0)
11901 {
11902 temp[i++] = K_SPECIAL;
11903 temp[i++] = KS_MODIFIER;
11904 temp[i++] = mod_mask;
11905 }
11906 if (IS_SPECIAL(n))
11907 {
11908 temp[i++] = K_SPECIAL;
11909 temp[i++] = K_SECOND(n);
11910 temp[i++] = K_THIRD(n);
11911 }
11912#ifdef FEAT_MBYTE
11913 else if (has_mbyte)
11914 i += (*mb_char2bytes)(n, temp + i);
11915#endif
11916 else
11917 temp[i++] = n;
11918 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011919 rettv->v_type = VAR_STRING;
11920 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011921
11922#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011923 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011924 {
11925 int row = mouse_row;
11926 int col = mouse_col;
11927 win_T *win;
11928 linenr_T lnum;
11929# ifdef FEAT_WINDOWS
11930 win_T *wp;
11931# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011932 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011933
11934 if (row >= 0 && col >= 0)
11935 {
11936 /* Find the window at the mouse coordinates and compute the
11937 * text position. */
11938 win = mouse_find_win(&row, &col);
11939 (void)mouse_comp_pos(win, &row, &col, &lnum);
11940# ifdef FEAT_WINDOWS
11941 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011942 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011943# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011944 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011945 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11946 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11947 }
11948 }
11949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 }
11951}
11952
11953/*
11954 * "getcharmod()" function
11955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011957f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011958 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011961 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962}
11963
11964/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011965 * "getcharsearch()" function
11966 */
11967 static void
11968f_getcharsearch(argvars, rettv)
11969 typval_T *argvars UNUSED;
11970 typval_T *rettv;
11971{
11972 if (rettv_dict_alloc(rettv) != FAIL)
11973 {
11974 dict_T *dict = rettv->vval.v_dict;
11975
11976 dict_add_nr_str(dict, "char", 0L, last_csearch());
11977 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11978 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11979 }
11980}
11981
11982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 * "getcmdline()" function
11984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011986f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011987 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011990 rettv->v_type = VAR_STRING;
11991 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992}
11993
11994/*
11995 * "getcmdpos()" function
11996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011998f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011999 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012002 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003}
12004
12005/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012006 * "getcmdtype()" function
12007 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012008 static void
12009f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012010 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012011 typval_T *rettv;
12012{
12013 rettv->v_type = VAR_STRING;
12014 rettv->vval.v_string = alloc(2);
12015 if (rettv->vval.v_string != NULL)
12016 {
12017 rettv->vval.v_string[0] = get_cmdline_type();
12018 rettv->vval.v_string[1] = NUL;
12019 }
12020}
12021
12022/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012023 * "getcmdwintype()" function
12024 */
12025 static void
12026f_getcmdwintype(argvars, rettv)
12027 typval_T *argvars UNUSED;
12028 typval_T *rettv;
12029{
12030 rettv->v_type = VAR_STRING;
12031 rettv->vval.v_string = NULL;
12032#ifdef FEAT_CMDWIN
12033 rettv->vval.v_string = alloc(2);
12034 if (rettv->vval.v_string != NULL)
12035 {
12036 rettv->vval.v_string[0] = cmdwin_type;
12037 rettv->vval.v_string[1] = NUL;
12038 }
12039#endif
12040}
12041
12042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 * "getcwd()" function
12044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012046f_getcwd(argvars, rettv)
Bram Moolenaarc9703302016-01-17 21:49:33 +010012047 typval_T *argvars;
Bram Moolenaar33570922005-01-25 22:26:29 +000012048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012050 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012051 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012054 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012055
12056 wp = find_tabwin(&argvars[0], &argvars[1]);
12057 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012059 if (wp->w_localdir != NULL)
12060 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12061 else if(globaldir != NULL)
12062 rettv->vval.v_string = vim_strsave(globaldir);
12063 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012064 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012065 cwd = alloc(MAXPATHL);
12066 if (cwd != NULL)
12067 {
12068 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12069 rettv->vval.v_string = vim_strsave(cwd);
12070 vim_free(cwd);
12071 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012072 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012073#ifdef BACKSLASH_IN_FILENAME
12074 if (rettv->vval.v_string != NULL)
12075 slash_adjust(rettv->vval.v_string);
12076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 }
12078}
12079
12080/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012081 * "getfontname()" function
12082 */
12083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012084f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012085 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012086 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012087{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012088 rettv->v_type = VAR_STRING;
12089 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012090#ifdef FEAT_GUI
12091 if (gui.in_use)
12092 {
12093 GuiFont font;
12094 char_u *name = NULL;
12095
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012096 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012097 {
12098 /* Get the "Normal" font. Either the name saved by
12099 * hl_set_font_name() or from the font ID. */
12100 font = gui.norm_font;
12101 name = hl_get_font_name();
12102 }
12103 else
12104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012106 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12107 return;
12108 font = gui_mch_get_font(name, FALSE);
12109 if (font == NOFONT)
12110 return; /* Invalid font name, return empty string. */
12111 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012112 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012113 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012114 gui_mch_free_font(font);
12115 }
12116#endif
12117}
12118
12119/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012120 * "getfperm({fname})" function
12121 */
12122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012124 typval_T *argvars;
12125 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012126{
12127 char_u *fname;
12128 struct stat st;
12129 char_u *perm = NULL;
12130 char_u flags[] = "rwx";
12131 int i;
12132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012135 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012136 if (mch_stat((char *)fname, &st) >= 0)
12137 {
12138 perm = vim_strsave((char_u *)"---------");
12139 if (perm != NULL)
12140 {
12141 for (i = 0; i < 9; i++)
12142 {
12143 if (st.st_mode & (1 << (8 - i)))
12144 perm[i] = flags[i % 3];
12145 }
12146 }
12147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012148 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012149}
12150
12151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 * "getfsize({fname})" function
12153 */
12154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012156 typval_T *argvars;
12157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158{
12159 char_u *fname;
12160 struct stat st;
12161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012162 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012164 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165
12166 if (mch_stat((char *)fname, &st) >= 0)
12167 {
12168 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012173
12174 /* non-perfect check for overflow */
12175 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12176 rettv->vval.v_number = -2;
12177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178 }
12179 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181}
12182
12183/*
12184 * "getftime({fname})" function
12185 */
12186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012187f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012188 typval_T *argvars;
12189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190{
12191 char_u *fname;
12192 struct stat st;
12193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195
12196 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012197 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012199 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200}
12201
12202/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012203 * "getftype({fname})" function
12204 */
12205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012206f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012207 typval_T *argvars;
12208 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012209{
12210 char_u *fname;
12211 struct stat st;
12212 char_u *type = NULL;
12213 char *t;
12214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012215 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012217 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012218 if (mch_lstat((char *)fname, &st) >= 0)
12219 {
12220#ifdef S_ISREG
12221 if (S_ISREG(st.st_mode))
12222 t = "file";
12223 else if (S_ISDIR(st.st_mode))
12224 t = "dir";
12225# ifdef S_ISLNK
12226 else if (S_ISLNK(st.st_mode))
12227 t = "link";
12228# endif
12229# ifdef S_ISBLK
12230 else if (S_ISBLK(st.st_mode))
12231 t = "bdev";
12232# endif
12233# ifdef S_ISCHR
12234 else if (S_ISCHR(st.st_mode))
12235 t = "cdev";
12236# endif
12237# ifdef S_ISFIFO
12238 else if (S_ISFIFO(st.st_mode))
12239 t = "fifo";
12240# endif
12241# ifdef S_ISSOCK
12242 else if (S_ISSOCK(st.st_mode))
12243 t = "fifo";
12244# endif
12245 else
12246 t = "other";
12247#else
12248# ifdef S_IFMT
12249 switch (st.st_mode & S_IFMT)
12250 {
12251 case S_IFREG: t = "file"; break;
12252 case S_IFDIR: t = "dir"; break;
12253# ifdef S_IFLNK
12254 case S_IFLNK: t = "link"; break;
12255# endif
12256# ifdef S_IFBLK
12257 case S_IFBLK: t = "bdev"; break;
12258# endif
12259# ifdef S_IFCHR
12260 case S_IFCHR: t = "cdev"; break;
12261# endif
12262# ifdef S_IFIFO
12263 case S_IFIFO: t = "fifo"; break;
12264# endif
12265# ifdef S_IFSOCK
12266 case S_IFSOCK: t = "socket"; break;
12267# endif
12268 default: t = "other";
12269 }
12270# else
12271 if (mch_isdir(fname))
12272 t = "dir";
12273 else
12274 t = "file";
12275# endif
12276#endif
12277 type = vim_strsave((char_u *)t);
12278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012279 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012280}
12281
12282/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012283 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012284 */
12285 static void
12286f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012287 typval_T *argvars;
12288 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012289{
12290 linenr_T lnum;
12291 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012292 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012293
12294 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012295 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012296 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012297 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012298 retlist = FALSE;
12299 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012300 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012301 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012302 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012303 retlist = TRUE;
12304 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012305
Bram Moolenaar342337a2005-07-21 21:11:17 +000012306 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012307}
12308
12309/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012310 * "getmatches()" function
12311 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012312 static void
12313f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012314 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012315 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012316{
12317#ifdef FEAT_SEARCH_EXTRA
12318 dict_T *dict;
12319 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012320 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012321
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012322 if (rettv_list_alloc(rettv) == OK)
12323 {
12324 while (cur != NULL)
12325 {
12326 dict = dict_alloc();
12327 if (dict == NULL)
12328 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012329 if (cur->match.regprog == NULL)
12330 {
12331 /* match added with matchaddpos() */
12332 for (i = 0; i < MAXPOSMATCH; ++i)
12333 {
12334 llpos_T *llpos;
12335 char buf[6];
12336 list_T *l;
12337
12338 llpos = &cur->pos.pos[i];
12339 if (llpos->lnum == 0)
12340 break;
12341 l = list_alloc();
12342 if (l == NULL)
12343 break;
12344 list_append_number(l, (varnumber_T)llpos->lnum);
12345 if (llpos->col > 0)
12346 {
12347 list_append_number(l, (varnumber_T)llpos->col);
12348 list_append_number(l, (varnumber_T)llpos->len);
12349 }
12350 sprintf(buf, "pos%d", i + 1);
12351 dict_add_list(dict, buf, l);
12352 }
12353 }
12354 else
12355 {
12356 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12357 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012358 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012359 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12360 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012361# ifdef FEAT_CONCEAL
12362 if (cur->conceal_char)
12363 {
12364 char_u buf[MB_MAXBYTES + 1];
12365
12366 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12367 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12368 }
12369# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012370 list_append_dict(rettv->vval.v_list, dict);
12371 cur = cur->next;
12372 }
12373 }
12374#endif
12375}
12376
12377/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012378 * "getpid()" function
12379 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012380 static void
12381f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012382 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012383 typval_T *rettv;
12384{
12385 rettv->vval.v_number = mch_get_pid();
12386}
12387
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012388static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12389
12390/*
12391 * "getcurpos()" function
12392 */
12393 static void
12394f_getcurpos(argvars, rettv)
12395 typval_T *argvars;
12396 typval_T *rettv;
12397{
12398 getpos_both(argvars, rettv, TRUE);
12399}
12400
Bram Moolenaar18081e32008-02-20 19:11:07 +000012401/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012402 * "getpos(string)" function
12403 */
12404 static void
12405f_getpos(argvars, rettv)
12406 typval_T *argvars;
12407 typval_T *rettv;
12408{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012409 getpos_both(argvars, rettv, FALSE);
12410}
12411
12412 static void
12413getpos_both(argvars, rettv, getcurpos)
12414 typval_T *argvars;
12415 typval_T *rettv;
12416 int getcurpos;
12417{
Bram Moolenaara5525202006-03-02 22:52:09 +000012418 pos_T *fp;
12419 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012420 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012421
12422 if (rettv_list_alloc(rettv) == OK)
12423 {
12424 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012425 if (getcurpos)
12426 fp = &curwin->w_cursor;
12427 else
12428 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012429 if (fnum != -1)
12430 list_append_number(l, (varnumber_T)fnum);
12431 else
12432 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012433 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12434 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012435 list_append_number(l, (fp != NULL)
12436 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012437 : (varnumber_T)0);
12438 list_append_number(l,
12439#ifdef FEAT_VIRTUALEDIT
12440 (fp != NULL) ? (varnumber_T)fp->coladd :
12441#endif
12442 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012443 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012444 list_append_number(l, curwin->w_curswant == MAXCOL ?
12445 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012446 }
12447 else
12448 rettv->vval.v_number = FALSE;
12449}
12450
12451/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012452 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012453 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012454 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012455f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012456 typval_T *argvars UNUSED;
12457 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012458{
12459#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012460 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012461#endif
12462
Bram Moolenaar2641f772005-03-25 21:58:17 +000012463#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012464 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012465 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012466 wp = NULL;
12467 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12468 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012469 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012470 if (wp == NULL)
12471 return;
12472 }
12473
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012474 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012475 }
12476#endif
12477}
12478
12479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480 * "getreg()" function
12481 */
12482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012483f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012484 typval_T *argvars;
12485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486{
12487 char_u *strregname;
12488 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012489 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012490 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012491 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012493 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012495 strregname = get_tv_string_chk(&argvars[0]);
12496 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012497 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012499 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012500 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12501 return_list = get_tv_number_chk(&argvars[2], &error);
12502 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012505 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012506
12507 if (error)
12508 return;
12509
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510 regname = (strregname == NULL ? '"' : *strregname);
12511 if (regname == 0)
12512 regname = '"';
12513
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012514 if (return_list)
12515 {
12516 rettv->v_type = VAR_LIST;
12517 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12518 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012519 if (rettv->vval.v_list != NULL)
12520 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012521 }
12522 else
12523 {
12524 rettv->v_type = VAR_STRING;
12525 rettv->vval.v_string = get_reg_contents(regname,
12526 arg2 ? GREG_EXPR_SRC : 0);
12527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528}
12529
12530/*
12531 * "getregtype()" function
12532 */
12533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012534f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012535 typval_T *argvars;
12536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537{
12538 char_u *strregname;
12539 int regname;
12540 char_u buf[NUMBUFLEN + 2];
12541 long reglen = 0;
12542
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012543 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012544 {
12545 strregname = get_tv_string_chk(&argvars[0]);
12546 if (strregname == NULL) /* type error; errmsg already given */
12547 {
12548 rettv->v_type = VAR_STRING;
12549 rettv->vval.v_string = NULL;
12550 return;
12551 }
12552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553 else
12554 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012555 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556
12557 regname = (strregname == NULL ? '"' : *strregname);
12558 if (regname == 0)
12559 regname = '"';
12560
12561 buf[0] = NUL;
12562 buf[1] = NUL;
12563 switch (get_reg_type(regname, &reglen))
12564 {
12565 case MLINE: buf[0] = 'V'; break;
12566 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567 case MBLOCK:
12568 buf[0] = Ctrl_V;
12569 sprintf((char *)buf + 1, "%ld", reglen + 1);
12570 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012572 rettv->v_type = VAR_STRING;
12573 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574}
12575
12576/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012577 * "gettabvar()" function
12578 */
12579 static void
12580f_gettabvar(argvars, rettv)
12581 typval_T *argvars;
12582 typval_T *rettv;
12583{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012584 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012585 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012586 dictitem_T *v;
12587 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012588 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012589
12590 rettv->v_type = VAR_STRING;
12591 rettv->vval.v_string = NULL;
12592
12593 varname = get_tv_string_chk(&argvars[1]);
12594 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12595 if (tp != NULL && varname != NULL)
12596 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012597 /* Set tp to be our tabpage, temporarily. Also set the window to the
12598 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012599 if (switch_win(&oldcurwin, &oldtabpage,
12600 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012601 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012602 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012603 /* look up the variable */
12604 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12605 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12606 if (v != NULL)
12607 {
12608 copy_tv(&v->di_tv, rettv);
12609 done = TRUE;
12610 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012611 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012612
12613 /* restore previous notion of curwin */
12614 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012615 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012616
12617 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012618 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012619}
12620
12621/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012622 * "gettabwinvar()" function
12623 */
12624 static void
12625f_gettabwinvar(argvars, rettv)
12626 typval_T *argvars;
12627 typval_T *rettv;
12628{
12629 getwinvar(argvars, rettv, 1);
12630}
12631
12632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633 * "getwinposx()" function
12634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012636f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012637 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012640 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641#ifdef FEAT_GUI
12642 if (gui.in_use)
12643 {
12644 int x, y;
12645
12646 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012647 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648 }
12649#endif
12650}
12651
12652/*
12653 * "getwinposy()" function
12654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012656f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012657 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012660 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661#ifdef FEAT_GUI
12662 if (gui.in_use)
12663 {
12664 int x, y;
12665
12666 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012667 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668 }
12669#endif
12670}
12671
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012672/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012673 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012674 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012675 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012676find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012677 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012678 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012679{
12680#ifdef FEAT_WINDOWS
12681 win_T *wp;
12682#endif
12683 int nr;
12684
12685 nr = get_tv_number_chk(vp, NULL);
12686
12687#ifdef FEAT_WINDOWS
12688 if (nr < 0)
12689 return NULL;
12690 if (nr == 0)
12691 return curwin;
12692
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012693 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12694 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012695 if (--nr <= 0)
12696 break;
12697 return wp;
12698#else
12699 if (nr == 0 || nr == 1)
12700 return curwin;
12701 return NULL;
12702#endif
12703}
12704
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012706 * Find window specified by "wvp" in tabpage "tvp".
12707 */
12708 static win_T *
12709find_tabwin(wvp, tvp)
12710 typval_T *wvp; /* VAR_UNKNOWN for current window */
12711 typval_T *tvp; /* VAR_UNKNOWN for current tab page */
12712{
12713 win_T *wp = NULL;
12714 tabpage_T *tp = NULL;
12715 long n;
12716
12717 if (wvp->v_type != VAR_UNKNOWN)
12718 {
12719 if (tvp->v_type != VAR_UNKNOWN)
12720 {
12721 n = get_tv_number(tvp);
12722 if (n >= 0)
12723 tp = find_tabpage(n);
12724 }
12725 else
12726 tp = curtab;
12727
12728 if (tp != NULL)
12729 wp = find_win_by_nr(wvp, tp);
12730 }
12731 else
12732 wp = curwin;
12733
12734 return wp;
12735}
12736
12737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738 * "getwinvar()" function
12739 */
12740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012741f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012742 typval_T *argvars;
12743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012745 getwinvar(argvars, rettv, 0);
12746}
12747
12748/*
12749 * getwinvar() and gettabwinvar()
12750 */
12751 static void
12752getwinvar(argvars, rettv, off)
12753 typval_T *argvars;
12754 typval_T *rettv;
12755 int off; /* 1 for gettabwinvar() */
12756{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012757 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012759 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012760 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012761 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012762#ifdef FEAT_WINDOWS
12763 win_T *oldcurwin;
12764 tabpage_T *oldtabpage;
12765 int need_switch_win;
12766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012768#ifdef FEAT_WINDOWS
12769 if (off == 1)
12770 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12771 else
12772 tp = curtab;
12773#endif
12774 win = find_win_by_nr(&argvars[off], tp);
12775 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012776 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012778 rettv->v_type = VAR_STRING;
12779 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780
12781 if (win != NULL && varname != NULL)
12782 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012783#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012784 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012785 * otherwise the window is not valid. Only do this when needed,
12786 * autocommands get blocked. */
12787 need_switch_win = !(tp == curtab && win == curwin);
12788 if (!need_switch_win
12789 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12790#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012791 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012792 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012793 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012794 if (get_option_tv(&varname, rettv, 1) == OK)
12795 done = TRUE;
12796 }
12797 else
12798 {
12799 /* Look up the variable. */
12800 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12801 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12802 varname, FALSE);
12803 if (v != NULL)
12804 {
12805 copy_tv(&v->di_tv, rettv);
12806 done = TRUE;
12807 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012810
Bram Moolenaarba117c22015-09-29 16:53:22 +020012811#ifdef FEAT_WINDOWS
12812 if (need_switch_win)
12813 /* restore previous notion of curwin */
12814 restore_win(oldcurwin, oldtabpage, TRUE);
12815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 }
12817
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012818 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12819 /* use the default return value */
12820 copy_tv(&argvars[off + 2], rettv);
12821
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 --emsg_off;
12823}
12824
12825/*
12826 * "glob()" function
12827 */
12828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012829f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012830 typval_T *argvars;
12831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012833 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012835 int error = FALSE;
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
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012838 * 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 Moolenaar146e9c32012-03-07 19:18:23 +010012840 if (argvars[1].v_type != VAR_UNKNOWN)
12841 {
12842 if (get_tv_number_chk(&argvars[1], &error))
12843 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012844 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012845 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012846 if (get_tv_number_chk(&argvars[2], &error))
12847 {
12848 rettv->v_type = VAR_LIST;
12849 rettv->vval.v_list = NULL;
12850 }
12851 if (argvars[3].v_type != VAR_UNKNOWN
12852 && get_tv_number_chk(&argvars[3], &error))
12853 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012854 }
12855 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012856 if (!error)
12857 {
12858 ExpandInit(&xpc);
12859 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012860 if (p_wic)
12861 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012862 if (rettv->v_type == VAR_STRING)
12863 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012864 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012865 else if (rettv_list_alloc(rettv) != FAIL)
12866 {
12867 int i;
12868
12869 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12870 NULL, options, WILD_ALL_KEEP);
12871 for (i = 0; i < xpc.xp_numfiles; i++)
12872 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12873
12874 ExpandCleanup(&xpc);
12875 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012876 }
12877 else
12878 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879}
12880
12881/*
12882 * "globpath()" function
12883 */
12884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012886 typval_T *argvars;
12887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012889 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012891 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012892 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012893 garray_T ga;
12894 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012896 /* When the optional second argument is non-zero, don't remove matches
12897 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012899 if (argvars[2].v_type != VAR_UNKNOWN)
12900 {
12901 if (get_tv_number_chk(&argvars[2], &error))
12902 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012903 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012904 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012905 if (get_tv_number_chk(&argvars[3], &error))
12906 {
12907 rettv->v_type = VAR_LIST;
12908 rettv->vval.v_list = NULL;
12909 }
12910 if (argvars[4].v_type != VAR_UNKNOWN
12911 && get_tv_number_chk(&argvars[4], &error))
12912 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012913 }
12914 }
12915 if (file != NULL && !error)
12916 {
12917 ga_init2(&ga, (int)sizeof(char_u *), 10);
12918 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12919 if (rettv->v_type == VAR_STRING)
12920 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12921 else if (rettv_list_alloc(rettv) != FAIL)
12922 for (i = 0; i < ga.ga_len; ++i)
12923 list_append_string(rettv->vval.v_list,
12924 ((char_u **)(ga.ga_data))[i], -1);
12925 ga_clear_strings(&ga);
12926 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012927 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012928 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929}
12930
12931/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012932 * "glob2regpat()" function
12933 */
12934 static void
12935f_glob2regpat(argvars, rettv)
12936 typval_T *argvars;
12937 typval_T *rettv;
12938{
12939 char_u *pat = get_tv_string_chk(&argvars[0]);
12940
12941 rettv->v_type = VAR_STRING;
12942 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12943}
12944
12945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946 * "has()" function
12947 */
12948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012949f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012950 typval_T *argvars;
12951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952{
12953 int i;
12954 char_u *name;
12955 int n = FALSE;
12956 static char *(has_list[]) =
12957 {
12958#ifdef AMIGA
12959 "amiga",
12960# ifdef FEAT_ARP
12961 "arp",
12962# endif
12963#endif
12964#ifdef __BEOS__
12965 "beos",
12966#endif
12967#ifdef MSDOS
12968# ifdef DJGPP
12969 "dos32",
12970# else
12971 "dos16",
12972# endif
12973#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012974#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975 "mac",
12976#endif
12977#if defined(MACOS_X_UNIX)
12978 "macunix",
12979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980#ifdef __QNX__
12981 "qnx",
12982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983#ifdef UNIX
12984 "unix",
12985#endif
12986#ifdef VMS
12987 "vms",
12988#endif
12989#ifdef WIN16
12990 "win16",
12991#endif
12992#ifdef WIN32
12993 "win32",
12994#endif
12995#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12996 "win32unix",
12997#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012998#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999 "win64",
13000#endif
13001#ifdef EBCDIC
13002 "ebcdic",
13003#endif
13004#ifndef CASE_INSENSITIVE_FILENAME
13005 "fname_case",
13006#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013007#ifdef HAVE_ACL
13008 "acl",
13009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010#ifdef FEAT_ARABIC
13011 "arabic",
13012#endif
13013#ifdef FEAT_AUTOCMD
13014 "autocmd",
13015#endif
13016#ifdef FEAT_BEVAL
13017 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013018# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13019 "balloon_multiline",
13020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021#endif
13022#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13023 "builtin_terms",
13024# ifdef ALL_BUILTIN_TCAPS
13025 "all_builtin_terms",
13026# endif
13027#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013028#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13029 || defined(FEAT_GUI_W32) \
13030 || defined(FEAT_GUI_MOTIF))
13031 "browsefilter",
13032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033#ifdef FEAT_BYTEOFF
13034 "byte_offset",
13035#endif
13036#ifdef FEAT_CINDENT
13037 "cindent",
13038#endif
13039#ifdef FEAT_CLIENTSERVER
13040 "clientserver",
13041#endif
13042#ifdef FEAT_CLIPBOARD
13043 "clipboard",
13044#endif
13045#ifdef FEAT_CMDL_COMPL
13046 "cmdline_compl",
13047#endif
13048#ifdef FEAT_CMDHIST
13049 "cmdline_hist",
13050#endif
13051#ifdef FEAT_COMMENTS
13052 "comments",
13053#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013054#ifdef FEAT_CONCEAL
13055 "conceal",
13056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057#ifdef FEAT_CRYPT
13058 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013059 "crypt-blowfish",
13060 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061#endif
13062#ifdef FEAT_CSCOPE
13063 "cscope",
13064#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013065#ifdef FEAT_CURSORBIND
13066 "cursorbind",
13067#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013068#ifdef CURSOR_SHAPE
13069 "cursorshape",
13070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071#ifdef DEBUG
13072 "debug",
13073#endif
13074#ifdef FEAT_CON_DIALOG
13075 "dialog_con",
13076#endif
13077#ifdef FEAT_GUI_DIALOG
13078 "dialog_gui",
13079#endif
13080#ifdef FEAT_DIFF
13081 "diff",
13082#endif
13083#ifdef FEAT_DIGRAPHS
13084 "digraphs",
13085#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013086#ifdef FEAT_DIRECTX
13087 "directx",
13088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089#ifdef FEAT_DND
13090 "dnd",
13091#endif
13092#ifdef FEAT_EMACS_TAGS
13093 "emacs_tags",
13094#endif
13095 "eval", /* always present, of course! */
13096#ifdef FEAT_EX_EXTRA
13097 "ex_extra",
13098#endif
13099#ifdef FEAT_SEARCH_EXTRA
13100 "extra_search",
13101#endif
13102#ifdef FEAT_FKMAP
13103 "farsi",
13104#endif
13105#ifdef FEAT_SEARCHPATH
13106 "file_in_path",
13107#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013108#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013109 "filterpipe",
13110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111#ifdef FEAT_FIND_ID
13112 "find_in_path",
13113#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013114#ifdef FEAT_FLOAT
13115 "float",
13116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117#ifdef FEAT_FOLDING
13118 "folding",
13119#endif
13120#ifdef FEAT_FOOTER
13121 "footer",
13122#endif
13123#if !defined(USE_SYSTEM) && defined(UNIX)
13124 "fork",
13125#endif
13126#ifdef FEAT_GETTEXT
13127 "gettext",
13128#endif
13129#ifdef FEAT_GUI
13130 "gui",
13131#endif
13132#ifdef FEAT_GUI_ATHENA
13133# ifdef FEAT_GUI_NEXTAW
13134 "gui_neXtaw",
13135# else
13136 "gui_athena",
13137# endif
13138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139#ifdef FEAT_GUI_GTK
13140 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013143#ifdef FEAT_GUI_GNOME
13144 "gui_gnome",
13145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146#ifdef FEAT_GUI_MAC
13147 "gui_mac",
13148#endif
13149#ifdef FEAT_GUI_MOTIF
13150 "gui_motif",
13151#endif
13152#ifdef FEAT_GUI_PHOTON
13153 "gui_photon",
13154#endif
13155#ifdef FEAT_GUI_W16
13156 "gui_win16",
13157#endif
13158#ifdef FEAT_GUI_W32
13159 "gui_win32",
13160#endif
13161#ifdef FEAT_HANGULIN
13162 "hangul_input",
13163#endif
13164#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13165 "iconv",
13166#endif
13167#ifdef FEAT_INS_EXPAND
13168 "insert_expand",
13169#endif
13170#ifdef FEAT_JUMPLIST
13171 "jumplist",
13172#endif
13173#ifdef FEAT_KEYMAP
13174 "keymap",
13175#endif
13176#ifdef FEAT_LANGMAP
13177 "langmap",
13178#endif
13179#ifdef FEAT_LIBCALL
13180 "libcall",
13181#endif
13182#ifdef FEAT_LINEBREAK
13183 "linebreak",
13184#endif
13185#ifdef FEAT_LISP
13186 "lispindent",
13187#endif
13188#ifdef FEAT_LISTCMDS
13189 "listcmds",
13190#endif
13191#ifdef FEAT_LOCALMAP
13192 "localmap",
13193#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013194#ifdef FEAT_LUA
13195# ifndef DYNAMIC_LUA
13196 "lua",
13197# endif
13198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199#ifdef FEAT_MENU
13200 "menu",
13201#endif
13202#ifdef FEAT_SESSION
13203 "mksession",
13204#endif
13205#ifdef FEAT_MODIFY_FNAME
13206 "modify_fname",
13207#endif
13208#ifdef FEAT_MOUSE
13209 "mouse",
13210#endif
13211#ifdef FEAT_MOUSESHAPE
13212 "mouseshape",
13213#endif
13214#if defined(UNIX) || defined(VMS)
13215# ifdef FEAT_MOUSE_DEC
13216 "mouse_dec",
13217# endif
13218# ifdef FEAT_MOUSE_GPM
13219 "mouse_gpm",
13220# endif
13221# ifdef FEAT_MOUSE_JSB
13222 "mouse_jsbterm",
13223# endif
13224# ifdef FEAT_MOUSE_NET
13225 "mouse_netterm",
13226# endif
13227# ifdef FEAT_MOUSE_PTERM
13228 "mouse_pterm",
13229# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013230# ifdef FEAT_MOUSE_SGR
13231 "mouse_sgr",
13232# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013233# ifdef FEAT_SYSMOUSE
13234 "mouse_sysmouse",
13235# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013236# ifdef FEAT_MOUSE_URXVT
13237 "mouse_urxvt",
13238# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239# ifdef FEAT_MOUSE_XTERM
13240 "mouse_xterm",
13241# endif
13242#endif
13243#ifdef FEAT_MBYTE
13244 "multi_byte",
13245#endif
13246#ifdef FEAT_MBYTE_IME
13247 "multi_byte_ime",
13248#endif
13249#ifdef FEAT_MULTI_LANG
13250 "multi_lang",
13251#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013252#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013253#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013254 "mzscheme",
13255#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257#ifdef FEAT_OLE
13258 "ole",
13259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260#ifdef FEAT_PATH_EXTRA
13261 "path_extra",
13262#endif
13263#ifdef FEAT_PERL
13264#ifndef DYNAMIC_PERL
13265 "perl",
13266#endif
13267#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013268#ifdef FEAT_PERSISTENT_UNDO
13269 "persistent_undo",
13270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271#ifdef FEAT_PYTHON
13272#ifndef DYNAMIC_PYTHON
13273 "python",
13274#endif
13275#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013276#ifdef FEAT_PYTHON3
13277#ifndef DYNAMIC_PYTHON3
13278 "python3",
13279#endif
13280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281#ifdef FEAT_POSTSCRIPT
13282 "postscript",
13283#endif
13284#ifdef FEAT_PRINTER
13285 "printer",
13286#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013287#ifdef FEAT_PROFILE
13288 "profile",
13289#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013290#ifdef FEAT_RELTIME
13291 "reltime",
13292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293#ifdef FEAT_QUICKFIX
13294 "quickfix",
13295#endif
13296#ifdef FEAT_RIGHTLEFT
13297 "rightleft",
13298#endif
13299#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13300 "ruby",
13301#endif
13302#ifdef FEAT_SCROLLBIND
13303 "scrollbind",
13304#endif
13305#ifdef FEAT_CMDL_INFO
13306 "showcmd",
13307 "cmdline_info",
13308#endif
13309#ifdef FEAT_SIGNS
13310 "signs",
13311#endif
13312#ifdef FEAT_SMARTINDENT
13313 "smartindent",
13314#endif
13315#ifdef FEAT_SNIFF
13316 "sniff",
13317#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013318#ifdef STARTUPTIME
13319 "startuptime",
13320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321#ifdef FEAT_STL_OPT
13322 "statusline",
13323#endif
13324#ifdef FEAT_SUN_WORKSHOP
13325 "sun_workshop",
13326#endif
13327#ifdef FEAT_NETBEANS_INTG
13328 "netbeans_intg",
13329#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013330#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013331 "spell",
13332#endif
13333#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334 "syntax",
13335#endif
13336#if defined(USE_SYSTEM) || !defined(UNIX)
13337 "system",
13338#endif
13339#ifdef FEAT_TAG_BINS
13340 "tag_binary",
13341#endif
13342#ifdef FEAT_TAG_OLDSTATIC
13343 "tag_old_static",
13344#endif
13345#ifdef FEAT_TAG_ANYWHITE
13346 "tag_any_white",
13347#endif
13348#ifdef FEAT_TCL
13349# ifndef DYNAMIC_TCL
13350 "tcl",
13351# endif
13352#endif
13353#ifdef TERMINFO
13354 "terminfo",
13355#endif
13356#ifdef FEAT_TERMRESPONSE
13357 "termresponse",
13358#endif
13359#ifdef FEAT_TEXTOBJ
13360 "textobjects",
13361#endif
13362#ifdef HAVE_TGETENT
13363 "tgetent",
13364#endif
13365#ifdef FEAT_TITLE
13366 "title",
13367#endif
13368#ifdef FEAT_TOOLBAR
13369 "toolbar",
13370#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013371#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13372 "unnamedplus",
13373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374#ifdef FEAT_USR_CMDS
13375 "user-commands", /* was accidentally included in 5.4 */
13376 "user_commands",
13377#endif
13378#ifdef FEAT_VIMINFO
13379 "viminfo",
13380#endif
13381#ifdef FEAT_VERTSPLIT
13382 "vertsplit",
13383#endif
13384#ifdef FEAT_VIRTUALEDIT
13385 "virtualedit",
13386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388#ifdef FEAT_VISUALEXTRA
13389 "visualextra",
13390#endif
13391#ifdef FEAT_VREPLACE
13392 "vreplace",
13393#endif
13394#ifdef FEAT_WILDIGN
13395 "wildignore",
13396#endif
13397#ifdef FEAT_WILDMENU
13398 "wildmenu",
13399#endif
13400#ifdef FEAT_WINDOWS
13401 "windows",
13402#endif
13403#ifdef FEAT_WAK
13404 "winaltkeys",
13405#endif
13406#ifdef FEAT_WRITEBACKUP
13407 "writebackup",
13408#endif
13409#ifdef FEAT_XIM
13410 "xim",
13411#endif
13412#ifdef FEAT_XFONTSET
13413 "xfontset",
13414#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013415#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013416 "xpm",
13417 "xpm_w32", /* for backward compatibility */
13418#else
13419# if defined(HAVE_XPM)
13420 "xpm",
13421# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423#ifdef USE_XSMP
13424 "xsmp",
13425#endif
13426#ifdef USE_XSMP_INTERACT
13427 "xsmp_interact",
13428#endif
13429#ifdef FEAT_XCLIPBOARD
13430 "xterm_clipboard",
13431#endif
13432#ifdef FEAT_XTERM_SAVE
13433 "xterm_save",
13434#endif
13435#if defined(UNIX) && defined(FEAT_X11)
13436 "X11",
13437#endif
13438 NULL
13439 };
13440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 for (i = 0; has_list[i] != NULL; ++i)
13443 if (STRICMP(name, has_list[i]) == 0)
13444 {
13445 n = TRUE;
13446 break;
13447 }
13448
13449 if (n == FALSE)
13450 {
13451 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013452 {
13453 if (name[5] == '-'
13454 && STRLEN(name) > 11
13455 && vim_isdigit(name[6])
13456 && vim_isdigit(name[8])
13457 && vim_isdigit(name[10]))
13458 {
13459 int major = atoi((char *)name + 6);
13460 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013461
13462 /* Expect "patch-9.9.01234". */
13463 n = (major < VIM_VERSION_MAJOR
13464 || (major == VIM_VERSION_MAJOR
13465 && (minor < VIM_VERSION_MINOR
13466 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013467 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013468 }
13469 else
13470 n = has_patch(atoi((char *)name + 5));
13471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472 else if (STRICMP(name, "vim_starting") == 0)
13473 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013474#ifdef FEAT_MBYTE
13475 else if (STRICMP(name, "multi_byte_encoding") == 0)
13476 n = has_mbyte;
13477#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013478#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13479 else if (STRICMP(name, "balloon_multiline") == 0)
13480 n = multiline_balloon_available();
13481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482#ifdef DYNAMIC_TCL
13483 else if (STRICMP(name, "tcl") == 0)
13484 n = tcl_enabled(FALSE);
13485#endif
13486#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13487 else if (STRICMP(name, "iconv") == 0)
13488 n = iconv_enabled(FALSE);
13489#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013490#ifdef DYNAMIC_LUA
13491 else if (STRICMP(name, "lua") == 0)
13492 n = lua_enabled(FALSE);
13493#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013494#ifdef DYNAMIC_MZSCHEME
13495 else if (STRICMP(name, "mzscheme") == 0)
13496 n = mzscheme_enabled(FALSE);
13497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498#ifdef DYNAMIC_RUBY
13499 else if (STRICMP(name, "ruby") == 0)
13500 n = ruby_enabled(FALSE);
13501#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013502#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503#ifdef DYNAMIC_PYTHON
13504 else if (STRICMP(name, "python") == 0)
13505 n = python_enabled(FALSE);
13506#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013507#endif
13508#ifdef FEAT_PYTHON3
13509#ifdef DYNAMIC_PYTHON3
13510 else if (STRICMP(name, "python3") == 0)
13511 n = python3_enabled(FALSE);
13512#endif
13513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514#ifdef DYNAMIC_PERL
13515 else if (STRICMP(name, "perl") == 0)
13516 n = perl_enabled(FALSE);
13517#endif
13518#ifdef FEAT_GUI
13519 else if (STRICMP(name, "gui_running") == 0)
13520 n = (gui.in_use || gui.starting);
13521# ifdef FEAT_GUI_W32
13522 else if (STRICMP(name, "gui_win32s") == 0)
13523 n = gui_is_win32s();
13524# endif
13525# ifdef FEAT_BROWSE
13526 else if (STRICMP(name, "browse") == 0)
13527 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13528# endif
13529#endif
13530#ifdef FEAT_SYN_HL
13531 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013532 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533#endif
13534#if defined(WIN3264)
13535 else if (STRICMP(name, "win95") == 0)
13536 n = mch_windows95();
13537#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013538#ifdef FEAT_NETBEANS_INTG
13539 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013540 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542 }
13543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013544 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545}
13546
13547/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013548 * "has_key()" function
13549 */
13550 static void
13551f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013552 typval_T *argvars;
13553 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013554{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013555 if (argvars[0].v_type != VAR_DICT)
13556 {
13557 EMSG(_(e_dictreq));
13558 return;
13559 }
13560 if (argvars[0].vval.v_dict == NULL)
13561 return;
13562
13563 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013564 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013565}
13566
13567/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013568 * "haslocaldir()" function
13569 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013570 static void
13571f_haslocaldir(argvars, rettv)
Bram Moolenaarc9703302016-01-17 21:49:33 +010013572 typval_T *argvars;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013573 typval_T *rettv;
13574{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013575 win_T *wp = NULL;
13576
13577 wp = find_tabwin(&argvars[0], &argvars[1]);
13578 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013579}
13580
13581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582 * "hasmapto()" function
13583 */
13584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013586 typval_T *argvars;
13587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588{
13589 char_u *name;
13590 char_u *mode;
13591 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013592 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013594 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013595 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 mode = (char_u *)"nvo";
13597 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013599 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013600 if (argvars[2].v_type != VAR_UNKNOWN)
13601 abbr = get_tv_number(&argvars[2]);
13602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603
Bram Moolenaar2c932302006-03-18 21:42:09 +000013604 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608}
13609
13610/*
13611 * "histadd()" function
13612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013615 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617{
13618#ifdef FEAT_CMDHIST
13619 int histype;
13620 char_u *str;
13621 char_u buf[NUMBUFLEN];
13622#endif
13623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625 if (check_restricted() || check_secure())
13626 return;
13627#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013628 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13629 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630 if (histype >= 0)
13631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013632 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 if (*str != NUL)
13634 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013635 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 return;
13639 }
13640 }
13641#endif
13642}
13643
13644/*
13645 * "histdel()" function
13646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013648f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013649 typval_T *argvars UNUSED;
13650 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651{
13652#ifdef FEAT_CMDHIST
13653 int n;
13654 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013655 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013657 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13658 if (str == NULL)
13659 n = 0;
13660 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013662 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013663 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013665 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013666 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667 else
13668 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013669 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 get_tv_string_buf(&argvars[1], buf));
13671 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672#endif
13673}
13674
13675/*
13676 * "histget()" function
13677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013679f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013680 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682{
13683#ifdef FEAT_CMDHIST
13684 int type;
13685 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013686 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013688 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13689 if (str == NULL)
13690 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013692 {
13693 type = get_histtype(str);
13694 if (argvars[1].v_type == VAR_UNKNOWN)
13695 idx = get_history_idx(type);
13696 else
13697 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13698 /* -1 on type error */
13699 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013702 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013704 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705}
13706
13707/*
13708 * "histnr()" function
13709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013711f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013712 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714{
13715 int i;
13716
13717#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013718 char_u *history = get_tv_string_chk(&argvars[0]);
13719
13720 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 if (i >= HIST_CMD && i < HIST_COUNT)
13722 i = get_history_idx(i);
13723 else
13724#endif
13725 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013726 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727}
13728
13729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 * "highlightID(name)" function
13731 */
13732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013734 typval_T *argvars;
13735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013737 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738}
13739
13740/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013741 * "highlight_exists()" function
13742 */
13743 static void
13744f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013745 typval_T *argvars;
13746 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013747{
13748 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13749}
13750
13751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 * "hostname()" function
13753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013756 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758{
13759 char_u hostname[256];
13760
13761 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013762 rettv->v_type = VAR_STRING;
13763 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764}
13765
13766/*
13767 * iconv() function
13768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013770f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013771 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773{
13774#ifdef FEAT_MBYTE
13775 char_u buf1[NUMBUFLEN];
13776 char_u buf2[NUMBUFLEN];
13777 char_u *from, *to, *str;
13778 vimconv_T vimconv;
13779#endif
13780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013781 rettv->v_type = VAR_STRING;
13782 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783
13784#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013785 str = get_tv_string(&argvars[0]);
13786 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13787 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 vimconv.vc_type = CONV_NONE;
13789 convert_setup(&vimconv, from, to);
13790
13791 /* If the encodings are equal, no conversion needed. */
13792 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013793 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013795 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796
13797 convert_setup(&vimconv, NULL, NULL);
13798 vim_free(from);
13799 vim_free(to);
13800#endif
13801}
13802
13803/*
13804 * "indent()" function
13805 */
13806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013807f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013808 typval_T *argvars;
13809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810{
13811 linenr_T lnum;
13812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013813 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013815 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013817 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818}
13819
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013820/*
13821 * "index()" function
13822 */
13823 static void
13824f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013825 typval_T *argvars;
13826 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013827{
Bram Moolenaar33570922005-01-25 22:26:29 +000013828 list_T *l;
13829 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013830 long idx = 0;
13831 int ic = FALSE;
13832
13833 rettv->vval.v_number = -1;
13834 if (argvars[0].v_type != VAR_LIST)
13835 {
13836 EMSG(_(e_listreq));
13837 return;
13838 }
13839 l = argvars[0].vval.v_list;
13840 if (l != NULL)
13841 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013842 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013843 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013844 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013845 int error = FALSE;
13846
Bram Moolenaar758711c2005-02-02 23:11:38 +000013847 /* Start at specified item. Use the cached index that list_find()
13848 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013849 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013850 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013851 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013852 ic = get_tv_number_chk(&argvars[3], &error);
13853 if (error)
13854 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013855 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013856
Bram Moolenaar758711c2005-02-02 23:11:38 +000013857 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013858 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013859 {
13860 rettv->vval.v_number = idx;
13861 break;
13862 }
13863 }
13864}
13865
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866static int inputsecret_flag = 0;
13867
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013868static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13869
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013871 * This function is used by f_input() and f_inputdialog() functions. The third
13872 * argument to f_input() specifies the type of completion to use at the
13873 * prompt. The third argument to f_inputdialog() specifies the value to return
13874 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 */
13876 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013877get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013878 typval_T *argvars;
13879 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013880 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013882 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883 char_u *p = NULL;
13884 int c;
13885 char_u buf[NUMBUFLEN];
13886 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013887 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013888 int xp_type = EXPAND_NOTHING;
13889 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013891 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013892 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893
13894#ifdef NO_CONSOLE_INPUT
13895 /* While starting up, there is no place to enter text. */
13896 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898#endif
13899
13900 cmd_silent = FALSE; /* Want to see the prompt. */
13901 if (prompt != NULL)
13902 {
13903 /* Only the part of the message after the last NL is considered as
13904 * prompt for the command line */
13905 p = vim_strrchr(prompt, '\n');
13906 if (p == NULL)
13907 p = prompt;
13908 else
13909 {
13910 ++p;
13911 c = *p;
13912 *p = NUL;
13913 msg_start();
13914 msg_clr_eos();
13915 msg_puts_attr(prompt, echo_attr);
13916 msg_didout = FALSE;
13917 msg_starthere();
13918 *p = c;
13919 }
13920 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013922 if (argvars[1].v_type != VAR_UNKNOWN)
13923 {
13924 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13925 if (defstr != NULL)
13926 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013928 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013929 {
13930 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013931 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013932 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013933
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013934 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013935 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013936
Bram Moolenaar4463f292005-09-25 22:20:24 +000013937 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13938 if (xp_name == NULL)
13939 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013940
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013941 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013942
Bram Moolenaar4463f292005-09-25 22:20:24 +000013943 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13944 &xp_arg) == FAIL)
13945 return;
13946 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013947 }
13948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013949 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013950 {
13951# ifdef FEAT_EX_EXTRA
13952 int save_ex_normal_busy = ex_normal_busy;
13953 ex_normal_busy = 0;
13954# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013955 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013956 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13957 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013958# ifdef FEAT_EX_EXTRA
13959 ex_normal_busy = save_ex_normal_busy;
13960# endif
13961 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013962 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013963 && argvars[1].v_type != VAR_UNKNOWN
13964 && argvars[2].v_type != VAR_UNKNOWN)
13965 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13966 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013967
13968 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013970 /* since the user typed this, no need to wait for return */
13971 need_wait_return = FALSE;
13972 msg_didout = FALSE;
13973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 cmd_silent = cmd_silent_save;
13975}
13976
13977/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013978 * "input()" function
13979 * Also handles inputsecret() when inputsecret is set.
13980 */
13981 static void
13982f_input(argvars, rettv)
13983 typval_T *argvars;
13984 typval_T *rettv;
13985{
13986 get_user_input(argvars, rettv, FALSE);
13987}
13988
13989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 * "inputdialog()" function
13991 */
13992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013993f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013994 typval_T *argvars;
13995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996{
13997#if defined(FEAT_GUI_TEXTDIALOG)
13998 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13999 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14000 {
14001 char_u *message;
14002 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014003 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014005 message = get_tv_string_chk(&argvars[0]);
14006 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014007 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014008 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 else
14010 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014011 if (message != NULL && defstr != NULL
14012 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014013 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014014 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 else
14016 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014017 if (message != NULL && defstr != NULL
14018 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014019 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014020 rettv->vval.v_string = vim_strsave(
14021 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014025 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026 }
14027 else
14028#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014029 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030}
14031
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014032/*
14033 * "inputlist()" function
14034 */
14035 static void
14036f_inputlist(argvars, rettv)
14037 typval_T *argvars;
14038 typval_T *rettv;
14039{
14040 listitem_T *li;
14041 int selected;
14042 int mouse_used;
14043
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014044#ifdef NO_CONSOLE_INPUT
14045 /* While starting up, there is no place to enter text. */
14046 if (no_console_input())
14047 return;
14048#endif
14049 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14050 {
14051 EMSG2(_(e_listarg), "inputlist()");
14052 return;
14053 }
14054
14055 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014056 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014057 lines_left = Rows; /* avoid more prompt */
14058 msg_scroll = TRUE;
14059 msg_clr_eos();
14060
14061 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14062 {
14063 msg_puts(get_tv_string(&li->li_tv));
14064 msg_putchar('\n');
14065 }
14066
14067 /* Ask for choice. */
14068 selected = prompt_for_number(&mouse_used);
14069 if (mouse_used)
14070 selected -= lines_left;
14071
14072 rettv->vval.v_number = selected;
14073}
14074
14075
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14077
14078/*
14079 * "inputrestore()" function
14080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014083 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085{
14086 if (ga_userinput.ga_len > 0)
14087 {
14088 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14090 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014091 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092 }
14093 else if (p_verbose > 1)
14094 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014095 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014096 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097 }
14098}
14099
14100/*
14101 * "inputsave()" function
14102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014104f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014105 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014108 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109 if (ga_grow(&ga_userinput, 1) == OK)
14110 {
14111 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14112 + ga_userinput.ga_len);
14113 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014114 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115 }
14116 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014117 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118}
14119
14120/*
14121 * "inputsecret()" function
14122 */
14123 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014124f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014125 typval_T *argvars;
14126 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127{
14128 ++cmdline_star;
14129 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014130 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131 --cmdline_star;
14132 --inputsecret_flag;
14133}
14134
14135/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014136 * "insert()" function
14137 */
14138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014139f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014140 typval_T *argvars;
14141 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014142{
14143 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014144 listitem_T *item;
14145 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014146 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014147
14148 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014149 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014150 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014151 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014152 {
14153 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014154 before = get_tv_number_chk(&argvars[2], &error);
14155 if (error)
14156 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014157
Bram Moolenaar758711c2005-02-02 23:11:38 +000014158 if (before == l->lv_len)
14159 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014160 else
14161 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014162 item = list_find(l, before);
14163 if (item == NULL)
14164 {
14165 EMSGN(_(e_listidx), before);
14166 l = NULL;
14167 }
14168 }
14169 if (l != NULL)
14170 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014171 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014172 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014173 }
14174 }
14175}
14176
14177/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014178 * "invert(expr)" function
14179 */
14180 static void
14181f_invert(argvars, rettv)
14182 typval_T *argvars;
14183 typval_T *rettv;
14184{
14185 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14186}
14187
14188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189 * "isdirectory()" function
14190 */
14191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014192f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014193 typval_T *argvars;
14194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014196 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197}
14198
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014199/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014200 * "islocked()" function
14201 */
14202 static void
14203f_islocked(argvars, rettv)
14204 typval_T *argvars;
14205 typval_T *rettv;
14206{
14207 lval_T lv;
14208 char_u *end;
14209 dictitem_T *di;
14210
14211 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014212 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14213 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014214 if (end != NULL && lv.ll_name != NULL)
14215 {
14216 if (*end != NUL)
14217 EMSG(_(e_trailing));
14218 else
14219 {
14220 if (lv.ll_tv == NULL)
14221 {
14222 if (check_changedtick(lv.ll_name))
14223 rettv->vval.v_number = 1; /* always locked */
14224 else
14225 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014226 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014227 if (di != NULL)
14228 {
14229 /* Consider a variable locked when:
14230 * 1. the variable itself is locked
14231 * 2. the value of the variable is locked.
14232 * 3. the List or Dict value is locked.
14233 */
14234 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14235 || tv_islocked(&di->di_tv));
14236 }
14237 }
14238 }
14239 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014240 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014241 else if (lv.ll_newkey != NULL)
14242 EMSG2(_(e_dictkey), lv.ll_newkey);
14243 else if (lv.ll_list != NULL)
14244 /* List item. */
14245 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14246 else
14247 /* Dictionary item. */
14248 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14249 }
14250 }
14251
14252 clear_lval(&lv);
14253}
14254
Bram Moolenaar33570922005-01-25 22:26:29 +000014255static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014256
14257/*
14258 * Turn a dict into a list:
14259 * "what" == 0: list of keys
14260 * "what" == 1: list of values
14261 * "what" == 2: list of items
14262 */
14263 static void
14264dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014265 typval_T *argvars;
14266 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014267 int what;
14268{
Bram Moolenaar33570922005-01-25 22:26:29 +000014269 list_T *l2;
14270 dictitem_T *di;
14271 hashitem_T *hi;
14272 listitem_T *li;
14273 listitem_T *li2;
14274 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014275 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014276
Bram Moolenaar8c711452005-01-14 21:53:12 +000014277 if (argvars[0].v_type != VAR_DICT)
14278 {
14279 EMSG(_(e_dictreq));
14280 return;
14281 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014282 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014283 return;
14284
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014285 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014286 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014287
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014288 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014289 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014290 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014291 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014292 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014293 --todo;
14294 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014295
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014296 li = listitem_alloc();
14297 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014298 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014299 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014300
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014301 if (what == 0)
14302 {
14303 /* keys() */
14304 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014305 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014306 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14307 }
14308 else if (what == 1)
14309 {
14310 /* values() */
14311 copy_tv(&di->di_tv, &li->li_tv);
14312 }
14313 else
14314 {
14315 /* items() */
14316 l2 = list_alloc();
14317 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014318 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014319 li->li_tv.vval.v_list = l2;
14320 if (l2 == NULL)
14321 break;
14322 ++l2->lv_refcount;
14323
14324 li2 = listitem_alloc();
14325 if (li2 == NULL)
14326 break;
14327 list_append(l2, li2);
14328 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014329 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014330 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14331
14332 li2 = listitem_alloc();
14333 if (li2 == NULL)
14334 break;
14335 list_append(l2, li2);
14336 copy_tv(&di->di_tv, &li2->li_tv);
14337 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014338 }
14339 }
14340}
14341
14342/*
14343 * "items(dict)" function
14344 */
14345 static void
14346f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014347 typval_T *argvars;
14348 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014349{
14350 dict_list(argvars, rettv, 2);
14351}
14352
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014354 * "join()" function
14355 */
14356 static void
14357f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014358 typval_T *argvars;
14359 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014360{
14361 garray_T ga;
14362 char_u *sep;
14363
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014364 if (argvars[0].v_type != VAR_LIST)
14365 {
14366 EMSG(_(e_listreq));
14367 return;
14368 }
14369 if (argvars[0].vval.v_list == NULL)
14370 return;
14371 if (argvars[1].v_type == VAR_UNKNOWN)
14372 sep = (char_u *)" ";
14373 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014374 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014375
14376 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014377
14378 if (sep != NULL)
14379 {
14380 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014381 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014382 ga_append(&ga, NUL);
14383 rettv->vval.v_string = (char_u *)ga.ga_data;
14384 }
14385 else
14386 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014387}
14388
14389/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014390 * "keys()" function
14391 */
14392 static void
14393f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014394 typval_T *argvars;
14395 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014396{
14397 dict_list(argvars, rettv, 0);
14398}
14399
14400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401 * "last_buffer_nr()" function.
14402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014404f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014405 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407{
14408 int n = 0;
14409 buf_T *buf;
14410
14411 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14412 if (n < buf->b_fnum)
14413 n = buf->b_fnum;
14414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014415 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014416}
14417
14418/*
14419 * "len()" function
14420 */
14421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014422f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014423 typval_T *argvars;
14424 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014425{
14426 switch (argvars[0].v_type)
14427 {
14428 case VAR_STRING:
14429 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014430 rettv->vval.v_number = (varnumber_T)STRLEN(
14431 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014432 break;
14433 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014434 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014435 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014436 case VAR_DICT:
14437 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14438 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014439 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014440 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014441 break;
14442 }
14443}
14444
Bram Moolenaar33570922005-01-25 22:26:29 +000014445static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014446
14447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014448libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014449 typval_T *argvars;
14450 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014451 int type;
14452{
14453#ifdef FEAT_LIBCALL
14454 char_u *string_in;
14455 char_u **string_result;
14456 int nr_result;
14457#endif
14458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014459 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014460 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014462
14463 if (check_restricted() || check_secure())
14464 return;
14465
14466#ifdef FEAT_LIBCALL
14467 /* The first two args must be strings, otherwise its meaningless */
14468 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14469 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014470 string_in = NULL;
14471 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014472 string_in = argvars[2].vval.v_string;
14473 if (type == VAR_NUMBER)
14474 string_result = NULL;
14475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014476 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014477 if (mch_libcall(argvars[0].vval.v_string,
14478 argvars[1].vval.v_string,
14479 string_in,
14480 argvars[2].vval.v_number,
14481 string_result,
14482 &nr_result) == OK
14483 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014484 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014485 }
14486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487}
14488
14489/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014490 * "libcall()" function
14491 */
14492 static void
14493f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014494 typval_T *argvars;
14495 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014496{
14497 libcall_common(argvars, rettv, VAR_STRING);
14498}
14499
14500/*
14501 * "libcallnr()" function
14502 */
14503 static void
14504f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014505 typval_T *argvars;
14506 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014507{
14508 libcall_common(argvars, rettv, VAR_NUMBER);
14509}
14510
14511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 * "line(string)" function
14513 */
14514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014515f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014516 typval_T *argvars;
14517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518{
14519 linenr_T lnum = 0;
14520 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014521 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014523 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 if (fp != NULL)
14525 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014526 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527}
14528
14529/*
14530 * "line2byte(lnum)" function
14531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014533f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014534 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536{
14537#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014538 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539#else
14540 linenr_T lnum;
14541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014542 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014544 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014546 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14547 if (rettv->vval.v_number >= 0)
14548 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549#endif
14550}
14551
14552/*
14553 * "lispindent(lnum)" function
14554 */
14555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014556f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014557 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559{
14560#ifdef FEAT_LISP
14561 pos_T pos;
14562 linenr_T lnum;
14563
14564 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014565 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14567 {
14568 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014569 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570 curwin->w_cursor = pos;
14571 }
14572 else
14573#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014574 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575}
14576
14577/*
14578 * "localtime()" function
14579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014581f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014582 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014585 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586}
14587
Bram Moolenaar33570922005-01-25 22:26:29 +000014588static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589
14590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014591get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014592 typval_T *argvars;
14593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 int exact;
14595{
14596 char_u *keys;
14597 char_u *which;
14598 char_u buf[NUMBUFLEN];
14599 char_u *keys_buf = NULL;
14600 char_u *rhs;
14601 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014602 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014603 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014604 mapblock_T *mp;
14605 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606
14607 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014608 rettv->v_type = VAR_STRING;
14609 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014611 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612 if (*keys == NUL)
14613 return;
14614
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014615 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014616 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014617 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014618 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014619 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014620 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014621 if (argvars[3].v_type != VAR_UNKNOWN)
14622 get_dict = get_tv_number(&argvars[3]);
14623 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625 else
14626 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014627 if (which == NULL)
14628 return;
14629
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630 mode = get_map_mode(&which, 0);
14631
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014632 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014633 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014635
14636 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014638 /* Return a string. */
14639 if (rhs != NULL)
14640 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641
Bram Moolenaarbd743252010-10-20 21:23:33 +020014642 }
14643 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14644 {
14645 /* Return a dictionary. */
14646 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14647 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14648 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649
Bram Moolenaarbd743252010-10-20 21:23:33 +020014650 dict_add_nr_str(dict, "lhs", 0L, lhs);
14651 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14652 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14653 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14654 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14655 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14656 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014657 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014658 dict_add_nr_str(dict, "mode", 0L, mapmode);
14659
14660 vim_free(lhs);
14661 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662 }
14663}
14664
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014665#ifdef FEAT_FLOAT
14666/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014667 * "log()" function
14668 */
14669 static void
14670f_log(argvars, rettv)
14671 typval_T *argvars;
14672 typval_T *rettv;
14673{
14674 float_T f;
14675
14676 rettv->v_type = VAR_FLOAT;
14677 if (get_float_arg(argvars, &f) == OK)
14678 rettv->vval.v_float = log(f);
14679 else
14680 rettv->vval.v_float = 0.0;
14681}
14682
14683/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014684 * "log10()" function
14685 */
14686 static void
14687f_log10(argvars, rettv)
14688 typval_T *argvars;
14689 typval_T *rettv;
14690{
14691 float_T f;
14692
14693 rettv->v_type = VAR_FLOAT;
14694 if (get_float_arg(argvars, &f) == OK)
14695 rettv->vval.v_float = log10(f);
14696 else
14697 rettv->vval.v_float = 0.0;
14698}
14699#endif
14700
Bram Moolenaar1dced572012-04-05 16:54:08 +020014701#ifdef FEAT_LUA
14702/*
14703 * "luaeval()" function
14704 */
14705 static void
14706f_luaeval(argvars, rettv)
14707 typval_T *argvars;
14708 typval_T *rettv;
14709{
14710 char_u *str;
14711 char_u buf[NUMBUFLEN];
14712
14713 str = get_tv_string_buf(&argvars[0], buf);
14714 do_luaeval(str, argvars + 1, rettv);
14715}
14716#endif
14717
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014719 * "map()" function
14720 */
14721 static void
14722f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014723 typval_T *argvars;
14724 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014725{
14726 filter_map(argvars, rettv, TRUE);
14727}
14728
14729/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014730 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731 */
14732 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014733f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014734 typval_T *argvars;
14735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014737 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738}
14739
14740/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014741 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742 */
14743 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014744f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014745 typval_T *argvars;
14746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014748 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749}
14750
Bram Moolenaar33570922005-01-25 22:26:29 +000014751static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752
14753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014755 typval_T *argvars;
14756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 int type;
14758{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014759 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014760 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014761 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762 char_u *pat;
14763 regmatch_T regmatch;
14764 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014765 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014766 char_u *save_cpo;
14767 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014768 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014769 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014770 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014771 list_T *l = NULL;
14772 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014773 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014774 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775
14776 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14777 save_cpo = p_cpo;
14778 p_cpo = (char_u *)"";
14779
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014780 rettv->vval.v_number = -1;
14781 if (type == 3)
14782 {
14783 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014784 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014785 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014786 }
14787 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014789 rettv->v_type = VAR_STRING;
14790 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014793 if (argvars[0].v_type == VAR_LIST)
14794 {
14795 if ((l = argvars[0].vval.v_list) == NULL)
14796 goto theend;
14797 li = l->lv_first;
14798 }
14799 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014800 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014801 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014802 len = (long)STRLEN(str);
14803 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014805 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14806 if (pat == NULL)
14807 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014808
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014809 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014811 int error = FALSE;
14812
14813 start = get_tv_number_chk(&argvars[2], &error);
14814 if (error)
14815 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014816 if (l != NULL)
14817 {
14818 li = list_find(l, start);
14819 if (li == NULL)
14820 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014821 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014822 }
14823 else
14824 {
14825 if (start < 0)
14826 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014827 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014828 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014829 /* When "count" argument is there ignore matches before "start",
14830 * otherwise skip part of the string. Differs when pattern is "^"
14831 * or "\<". */
14832 if (argvars[3].v_type != VAR_UNKNOWN)
14833 startcol = start;
14834 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014835 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014836 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014837 len -= start;
14838 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014839 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014840
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014841 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014842 nth = get_tv_number_chk(&argvars[3], &error);
14843 if (error)
14844 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 }
14846
14847 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14848 if (regmatch.regprog != NULL)
14849 {
14850 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014851
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014852 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014853 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014854 if (l != NULL)
14855 {
14856 if (li == NULL)
14857 {
14858 match = FALSE;
14859 break;
14860 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014861 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014862 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014863 if (str == NULL)
14864 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014865 }
14866
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014867 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014868
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014869 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014870 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014871 if (l == NULL && !match)
14872 break;
14873
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014874 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014875 if (l != NULL)
14876 {
14877 li = li->li_next;
14878 ++idx;
14879 }
14880 else
14881 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014882#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014883 startcol = (colnr_T)(regmatch.startp[0]
14884 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014885#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014886 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014887#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014888 if (startcol > (colnr_T)len
14889 || str + startcol <= regmatch.startp[0])
14890 {
14891 match = FALSE;
14892 break;
14893 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014894 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014895 }
14896
14897 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014899 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014900 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014901 int i;
14902
14903 /* return list with matched string and submatches */
14904 for (i = 0; i < NSUBEXP; ++i)
14905 {
14906 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014907 {
14908 if (list_append_string(rettv->vval.v_list,
14909 (char_u *)"", 0) == FAIL)
14910 break;
14911 }
14912 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014913 regmatch.startp[i],
14914 (int)(regmatch.endp[i] - regmatch.startp[i]))
14915 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014916 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014917 }
14918 }
14919 else if (type == 2)
14920 {
14921 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014922 if (l != NULL)
14923 copy_tv(&li->li_tv, rettv);
14924 else
14925 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014927 }
14928 else if (l != NULL)
14929 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930 else
14931 {
14932 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014933 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934 (varnumber_T)(regmatch.startp[0] - str);
14935 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014936 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014938 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939 }
14940 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014941 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 }
14943
14944theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014945 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946 p_cpo = save_cpo;
14947}
14948
14949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014950 * "match()" function
14951 */
14952 static void
14953f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014954 typval_T *argvars;
14955 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014956{
14957 find_some_match(argvars, rettv, 1);
14958}
14959
14960/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014961 * "matchadd()" function
14962 */
14963 static void
14964f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014965 typval_T *argvars UNUSED;
14966 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014967{
14968#ifdef FEAT_SEARCH_EXTRA
14969 char_u buf[NUMBUFLEN];
14970 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14971 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14972 int prio = 10; /* default priority */
14973 int id = -1;
14974 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014975 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014976
14977 rettv->vval.v_number = -1;
14978
14979 if (grp == NULL || pat == NULL)
14980 return;
14981 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014982 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014983 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014984 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014985 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014986 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014987 if (argvars[4].v_type != VAR_UNKNOWN)
14988 {
14989 if (argvars[4].v_type != VAR_DICT)
14990 {
14991 EMSG(_(e_dictreq));
14992 return;
14993 }
14994 if (dict_find(argvars[4].vval.v_dict,
14995 (char_u *)"conceal", -1) != NULL)
14996 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14997 (char_u *)"conceal", FALSE);
14998 }
14999 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015000 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015001 if (error == TRUE)
15002 return;
15003 if (id >= 1 && id <= 3)
15004 {
15005 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15006 return;
15007 }
15008
Bram Moolenaar6561d522015-07-21 15:48:27 +020015009 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15010 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015011#endif
15012}
15013
15014/*
15015 * "matchaddpos()" function
15016 */
15017 static void
15018f_matchaddpos(argvars, rettv)
15019 typval_T *argvars UNUSED;
15020 typval_T *rettv UNUSED;
15021{
15022#ifdef FEAT_SEARCH_EXTRA
15023 char_u buf[NUMBUFLEN];
15024 char_u *group;
15025 int prio = 10;
15026 int id = -1;
15027 int error = FALSE;
15028 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015029 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015030
15031 rettv->vval.v_number = -1;
15032
15033 group = get_tv_string_buf_chk(&argvars[0], buf);
15034 if (group == NULL)
15035 return;
15036
15037 if (argvars[1].v_type != VAR_LIST)
15038 {
15039 EMSG2(_(e_listarg), "matchaddpos()");
15040 return;
15041 }
15042 l = argvars[1].vval.v_list;
15043 if (l == NULL)
15044 return;
15045
15046 if (argvars[2].v_type != VAR_UNKNOWN)
15047 {
15048 prio = get_tv_number_chk(&argvars[2], &error);
15049 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015050 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015051 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015052 if (argvars[4].v_type != VAR_UNKNOWN)
15053 {
15054 if (argvars[4].v_type != VAR_DICT)
15055 {
15056 EMSG(_(e_dictreq));
15057 return;
15058 }
15059 if (dict_find(argvars[4].vval.v_dict,
15060 (char_u *)"conceal", -1) != NULL)
15061 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15062 (char_u *)"conceal", FALSE);
15063 }
15064 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015065 }
15066 if (error == TRUE)
15067 return;
15068
15069 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15070 if (id == 1 || id == 2)
15071 {
15072 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15073 return;
15074 }
15075
Bram Moolenaar6561d522015-07-21 15:48:27 +020015076 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15077 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015078#endif
15079}
15080
15081/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015082 * "matcharg()" function
15083 */
15084 static void
15085f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015086 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015087 typval_T *rettv;
15088{
15089 if (rettv_list_alloc(rettv) == OK)
15090 {
15091#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015092 int id = get_tv_number(&argvars[0]);
15093 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015094
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015095 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015096 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015097 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15098 {
15099 list_append_string(rettv->vval.v_list,
15100 syn_id2name(m->hlg_id), -1);
15101 list_append_string(rettv->vval.v_list, m->pattern, -1);
15102 }
15103 else
15104 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015105 list_append_string(rettv->vval.v_list, NULL, -1);
15106 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015107 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015108 }
15109#endif
15110 }
15111}
15112
15113/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015114 * "matchdelete()" function
15115 */
15116 static void
15117f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015118 typval_T *argvars UNUSED;
15119 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015120{
15121#ifdef FEAT_SEARCH_EXTRA
15122 rettv->vval.v_number = match_delete(curwin,
15123 (int)get_tv_number(&argvars[0]), TRUE);
15124#endif
15125}
15126
15127/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015128 * "matchend()" function
15129 */
15130 static void
15131f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015132 typval_T *argvars;
15133 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015134{
15135 find_some_match(argvars, rettv, 0);
15136}
15137
15138/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015139 * "matchlist()" function
15140 */
15141 static void
15142f_matchlist(argvars, rettv)
15143 typval_T *argvars;
15144 typval_T *rettv;
15145{
15146 find_some_match(argvars, rettv, 3);
15147}
15148
15149/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015150 * "matchstr()" function
15151 */
15152 static void
15153f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015154 typval_T *argvars;
15155 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015156{
15157 find_some_match(argvars, rettv, 2);
15158}
15159
Bram Moolenaar33570922005-01-25 22:26:29 +000015160static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015161
15162 static void
15163max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015164 typval_T *argvars;
15165 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015166 int domax;
15167{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015168 long n = 0;
15169 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015170 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015171
15172 if (argvars[0].v_type == VAR_LIST)
15173 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015174 list_T *l;
15175 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015176
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015177 l = argvars[0].vval.v_list;
15178 if (l != NULL)
15179 {
15180 li = l->lv_first;
15181 if (li != NULL)
15182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015183 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015184 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015185 {
15186 li = li->li_next;
15187 if (li == NULL)
15188 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015189 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015190 if (domax ? i > n : i < n)
15191 n = i;
15192 }
15193 }
15194 }
15195 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015196 else if (argvars[0].v_type == VAR_DICT)
15197 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015198 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015199 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015200 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015201 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015202
15203 d = argvars[0].vval.v_dict;
15204 if (d != NULL)
15205 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015206 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015207 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015208 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015209 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015210 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015211 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015212 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015213 if (first)
15214 {
15215 n = i;
15216 first = FALSE;
15217 }
15218 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015219 n = i;
15220 }
15221 }
15222 }
15223 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015224 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015225 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015226 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015227}
15228
15229/*
15230 * "max()" function
15231 */
15232 static void
15233f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015234 typval_T *argvars;
15235 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015236{
15237 max_min(argvars, rettv, TRUE);
15238}
15239
15240/*
15241 * "min()" function
15242 */
15243 static void
15244f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015245 typval_T *argvars;
15246 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015247{
15248 max_min(argvars, rettv, FALSE);
15249}
15250
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015251static int mkdir_recurse __ARGS((char_u *dir, int prot));
15252
15253/*
15254 * Create the directory in which "dir" is located, and higher levels when
15255 * needed.
15256 */
15257 static int
15258mkdir_recurse(dir, prot)
15259 char_u *dir;
15260 int prot;
15261{
15262 char_u *p;
15263 char_u *updir;
15264 int r = FAIL;
15265
15266 /* Get end of directory name in "dir".
15267 * We're done when it's "/" or "c:/". */
15268 p = gettail_sep(dir);
15269 if (p <= get_past_head(dir))
15270 return OK;
15271
15272 /* If the directory exists we're done. Otherwise: create it.*/
15273 updir = vim_strnsave(dir, (int)(p - dir));
15274 if (updir == NULL)
15275 return FAIL;
15276 if (mch_isdir(updir))
15277 r = OK;
15278 else if (mkdir_recurse(updir, prot) == OK)
15279 r = vim_mkdir_emsg(updir, prot);
15280 vim_free(updir);
15281 return r;
15282}
15283
15284#ifdef vim_mkdir
15285/*
15286 * "mkdir()" function
15287 */
15288 static void
15289f_mkdir(argvars, rettv)
15290 typval_T *argvars;
15291 typval_T *rettv;
15292{
15293 char_u *dir;
15294 char_u buf[NUMBUFLEN];
15295 int prot = 0755;
15296
15297 rettv->vval.v_number = FAIL;
15298 if (check_restricted() || check_secure())
15299 return;
15300
15301 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015302 if (*dir == NUL)
15303 rettv->vval.v_number = FAIL;
15304 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015305 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015306 if (*gettail(dir) == NUL)
15307 /* remove trailing slashes */
15308 *gettail_sep(dir) = NUL;
15309
15310 if (argvars[1].v_type != VAR_UNKNOWN)
15311 {
15312 if (argvars[2].v_type != VAR_UNKNOWN)
15313 prot = get_tv_number_chk(&argvars[2], NULL);
15314 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15315 mkdir_recurse(dir, prot);
15316 }
15317 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015318 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015319}
15320#endif
15321
Bram Moolenaar0d660222005-01-07 21:51:51 +000015322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323 * "mode()" function
15324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015326f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015327 typval_T *argvars;
15328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015330 char_u buf[3];
15331
15332 buf[1] = NUL;
15333 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 if (VIsual_active)
15336 {
15337 if (VIsual_select)
15338 buf[0] = VIsual_mode + 's' - 'v';
15339 else
15340 buf[0] = VIsual_mode;
15341 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015342 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015343 || State == CONFIRM)
15344 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015346 if (State == ASKMORE)
15347 buf[1] = 'm';
15348 else if (State == CONFIRM)
15349 buf[1] = '?';
15350 }
15351 else if (State == EXTERNCMD)
15352 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 else if (State & INSERT)
15354 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015355#ifdef FEAT_VREPLACE
15356 if (State & VREPLACE_FLAG)
15357 {
15358 buf[0] = 'R';
15359 buf[1] = 'v';
15360 }
15361 else
15362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363 if (State & REPLACE_FLAG)
15364 buf[0] = 'R';
15365 else
15366 buf[0] = 'i';
15367 }
15368 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015369 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015371 if (exmode_active)
15372 buf[1] = 'v';
15373 }
15374 else if (exmode_active)
15375 {
15376 buf[0] = 'c';
15377 buf[1] = 'e';
15378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015380 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015382 if (finish_op)
15383 buf[1] = 'o';
15384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015386 /* Clear out the minor mode when the argument is not a non-zero number or
15387 * non-empty string. */
15388 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015389 buf[1] = NUL;
15390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015391 rettv->vval.v_string = vim_strsave(buf);
15392 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393}
15394
Bram Moolenaar429fa852013-04-15 12:27:36 +020015395#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015396/*
15397 * "mzeval()" function
15398 */
15399 static void
15400f_mzeval(argvars, rettv)
15401 typval_T *argvars;
15402 typval_T *rettv;
15403{
15404 char_u *str;
15405 char_u buf[NUMBUFLEN];
15406
15407 str = get_tv_string_buf(&argvars[0], buf);
15408 do_mzeval(str, rettv);
15409}
Bram Moolenaar75676462013-01-30 14:55:42 +010015410
15411 void
15412mzscheme_call_vim(name, args, rettv)
15413 char_u *name;
15414 typval_T *args;
15415 typval_T *rettv;
15416{
15417 typval_T argvars[3];
15418
15419 argvars[0].v_type = VAR_STRING;
15420 argvars[0].vval.v_string = name;
15421 copy_tv(args, &argvars[1]);
15422 argvars[2].v_type = VAR_UNKNOWN;
15423 f_call(argvars, rettv);
15424 clear_tv(&argvars[1]);
15425}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015426#endif
15427
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015429 * "nextnonblank()" function
15430 */
15431 static void
15432f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015433 typval_T *argvars;
15434 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015435{
15436 linenr_T lnum;
15437
15438 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015440 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015441 {
15442 lnum = 0;
15443 break;
15444 }
15445 if (*skipwhite(ml_get(lnum)) != NUL)
15446 break;
15447 }
15448 rettv->vval.v_number = lnum;
15449}
15450
15451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452 * "nr2char()" function
15453 */
15454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015455f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015456 typval_T *argvars;
15457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458{
15459 char_u buf[NUMBUFLEN];
15460
15461#ifdef FEAT_MBYTE
15462 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015463 {
15464 int utf8 = 0;
15465
15466 if (argvars[1].v_type != VAR_UNKNOWN)
15467 utf8 = get_tv_number_chk(&argvars[1], NULL);
15468 if (utf8)
15469 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15470 else
15471 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473 else
15474#endif
15475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015476 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477 buf[1] = NUL;
15478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015479 rettv->v_type = VAR_STRING;
15480 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015481}
15482
15483/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015484 * "or(expr, expr)" function
15485 */
15486 static void
15487f_or(argvars, rettv)
15488 typval_T *argvars;
15489 typval_T *rettv;
15490{
15491 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15492 | get_tv_number_chk(&argvars[1], NULL);
15493}
15494
15495/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015496 * "pathshorten()" function
15497 */
15498 static void
15499f_pathshorten(argvars, rettv)
15500 typval_T *argvars;
15501 typval_T *rettv;
15502{
15503 char_u *p;
15504
15505 rettv->v_type = VAR_STRING;
15506 p = get_tv_string_chk(&argvars[0]);
15507 if (p == NULL)
15508 rettv->vval.v_string = NULL;
15509 else
15510 {
15511 p = vim_strsave(p);
15512 rettv->vval.v_string = p;
15513 if (p != NULL)
15514 shorten_dir(p);
15515 }
15516}
15517
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015518#ifdef FEAT_PERL
15519/*
15520 * "perleval()" function
15521 */
15522 static void
15523f_perleval(argvars, rettv)
15524 typval_T *argvars;
15525 typval_T *rettv;
15526{
15527 char_u *str;
15528 char_u buf[NUMBUFLEN];
15529
15530 str = get_tv_string_buf(&argvars[0], buf);
15531 do_perleval(str, rettv);
15532}
15533#endif
15534
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015535#ifdef FEAT_FLOAT
15536/*
15537 * "pow()" function
15538 */
15539 static void
15540f_pow(argvars, rettv)
15541 typval_T *argvars;
15542 typval_T *rettv;
15543{
15544 float_T fx, fy;
15545
15546 rettv->v_type = VAR_FLOAT;
15547 if (get_float_arg(argvars, &fx) == OK
15548 && get_float_arg(&argvars[1], &fy) == OK)
15549 rettv->vval.v_float = pow(fx, fy);
15550 else
15551 rettv->vval.v_float = 0.0;
15552}
15553#endif
15554
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015555/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015556 * "prevnonblank()" function
15557 */
15558 static void
15559f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015560 typval_T *argvars;
15561 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015562{
15563 linenr_T lnum;
15564
15565 lnum = get_tv_lnum(argvars);
15566 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15567 lnum = 0;
15568 else
15569 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15570 --lnum;
15571 rettv->vval.v_number = lnum;
15572}
15573
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015574#ifdef HAVE_STDARG_H
15575/* This dummy va_list is here because:
15576 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15577 * - locally in the function results in a "used before set" warning
15578 * - using va_start() to initialize it gives "function with fixed args" error */
15579static va_list ap;
15580#endif
15581
Bram Moolenaar8c711452005-01-14 21:53:12 +000015582/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015583 * "printf()" function
15584 */
15585 static void
15586f_printf(argvars, rettv)
15587 typval_T *argvars;
15588 typval_T *rettv;
15589{
15590 rettv->v_type = VAR_STRING;
15591 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015592#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015593 {
15594 char_u buf[NUMBUFLEN];
15595 int len;
15596 char_u *s;
15597 int saved_did_emsg = did_emsg;
15598 char *fmt;
15599
15600 /* Get the required length, allocate the buffer and do it for real. */
15601 did_emsg = FALSE;
15602 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015603 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015604 if (!did_emsg)
15605 {
15606 s = alloc(len + 1);
15607 if (s != NULL)
15608 {
15609 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015610 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015611 }
15612 }
15613 did_emsg |= saved_did_emsg;
15614 }
15615#endif
15616}
15617
15618/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015619 * "pumvisible()" function
15620 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015621 static void
15622f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015623 typval_T *argvars UNUSED;
15624 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015625{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015626#ifdef FEAT_INS_EXPAND
15627 if (pum_visible())
15628 rettv->vval.v_number = 1;
15629#endif
15630}
15631
Bram Moolenaardb913952012-06-29 12:54:53 +020015632#ifdef FEAT_PYTHON3
15633/*
15634 * "py3eval()" function
15635 */
15636 static void
15637f_py3eval(argvars, rettv)
15638 typval_T *argvars;
15639 typval_T *rettv;
15640{
15641 char_u *str;
15642 char_u buf[NUMBUFLEN];
15643
15644 str = get_tv_string_buf(&argvars[0], buf);
15645 do_py3eval(str, rettv);
15646}
15647#endif
15648
15649#ifdef FEAT_PYTHON
15650/*
15651 * "pyeval()" function
15652 */
15653 static void
15654f_pyeval(argvars, rettv)
15655 typval_T *argvars;
15656 typval_T *rettv;
15657{
15658 char_u *str;
15659 char_u buf[NUMBUFLEN];
15660
15661 str = get_tv_string_buf(&argvars[0], buf);
15662 do_pyeval(str, rettv);
15663}
15664#endif
15665
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015666/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015667 * "range()" function
15668 */
15669 static void
15670f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015671 typval_T *argvars;
15672 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015673{
15674 long start;
15675 long end;
15676 long stride = 1;
15677 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015678 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015680 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015681 if (argvars[1].v_type == VAR_UNKNOWN)
15682 {
15683 end = start - 1;
15684 start = 0;
15685 }
15686 else
15687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015688 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015689 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015690 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015691 }
15692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015693 if (error)
15694 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015695 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015696 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015697 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015698 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015699 else
15700 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015701 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015702 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015703 if (list_append_number(rettv->vval.v_list,
15704 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015705 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015706 }
15707}
15708
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015709/*
15710 * "readfile()" function
15711 */
15712 static void
15713f_readfile(argvars, rettv)
15714 typval_T *argvars;
15715 typval_T *rettv;
15716{
15717 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015718 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015719 char_u *fname;
15720 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015721 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15722 int io_size = sizeof(buf);
15723 int readlen; /* size of last fread() */
15724 char_u *prev = NULL; /* previously read bytes, if any */
15725 long prevlen = 0; /* length of data in prev */
15726 long prevsize = 0; /* size of prev buffer */
15727 long maxline = MAXLNUM;
15728 long cnt = 0;
15729 char_u *p; /* position in buf */
15730 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015731
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015732 if (argvars[1].v_type != VAR_UNKNOWN)
15733 {
15734 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15735 binary = TRUE;
15736 if (argvars[2].v_type != VAR_UNKNOWN)
15737 maxline = get_tv_number(&argvars[2]);
15738 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015739
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015740 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015741 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015742
15743 /* Always open the file in binary mode, library functions have a mind of
15744 * their own about CR-LF conversion. */
15745 fname = get_tv_string(&argvars[0]);
15746 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15747 {
15748 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15749 return;
15750 }
15751
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015752 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015753 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015754 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015755
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015756 /* This for loop processes what was read, but is also entered at end
15757 * of file so that either:
15758 * - an incomplete line gets written
15759 * - a "binary" file gets an empty line at the end if it ends in a
15760 * newline. */
15761 for (p = buf, start = buf;
15762 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15763 ++p)
15764 {
15765 if (*p == '\n' || readlen <= 0)
15766 {
15767 listitem_T *li;
15768 char_u *s = NULL;
15769 long_u len = p - start;
15770
15771 /* Finished a line. Remove CRs before NL. */
15772 if (readlen > 0 && !binary)
15773 {
15774 while (len > 0 && start[len - 1] == '\r')
15775 --len;
15776 /* removal may cross back to the "prev" string */
15777 if (len == 0)
15778 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15779 --prevlen;
15780 }
15781 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015782 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015783 else
15784 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015785 /* Change "prev" buffer to be the right size. This way
15786 * the bytes are only copied once, and very long lines are
15787 * allocated only once. */
15788 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015789 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015790 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015791 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015792 prev = NULL; /* the list will own the string */
15793 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015794 }
15795 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015796 if (s == NULL)
15797 {
15798 do_outofmem_msg((long_u) prevlen + len + 1);
15799 failed = TRUE;
15800 break;
15801 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015802
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015803 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015804 {
15805 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015806 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015807 break;
15808 }
15809 li->li_tv.v_type = VAR_STRING;
15810 li->li_tv.v_lock = 0;
15811 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015812 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015813
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015814 start = p + 1; /* step over newline */
15815 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015816 break;
15817 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015818 else if (*p == NUL)
15819 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015820#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015821 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15822 * when finding the BF and check the previous two bytes. */
15823 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015824 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015825 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15826 * + 1, these may be in the "prev" string. */
15827 char_u back1 = p >= buf + 1 ? p[-1]
15828 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15829 char_u back2 = p >= buf + 2 ? p[-2]
15830 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15831 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015832
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015833 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015834 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015835 char_u *dest = p - 2;
15836
15837 /* Usually a BOM is at the beginning of a file, and so at
15838 * the beginning of a line; then we can just step over it.
15839 */
15840 if (start == dest)
15841 start = p + 1;
15842 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015843 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015844 /* have to shuffle buf to close gap */
15845 int adjust_prevlen = 0;
15846
15847 if (dest < buf)
15848 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015849 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015850 dest = buf;
15851 }
15852 if (readlen > p - buf + 1)
15853 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15854 readlen -= 3 - adjust_prevlen;
15855 prevlen -= adjust_prevlen;
15856 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015857 }
15858 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015859 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015860#endif
15861 } /* for */
15862
15863 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15864 break;
15865 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015866 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015867 /* There's part of a line in buf, store it in "prev". */
15868 if (p - start + prevlen >= prevsize)
15869 {
15870 /* need bigger "prev" buffer */
15871 char_u *newprev;
15872
15873 /* A common use case is ordinary text files and "prev" gets a
15874 * fragment of a line, so the first allocation is made
15875 * small, to avoid repeatedly 'allocing' large and
15876 * 'reallocing' small. */
15877 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015878 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015879 else
15880 {
15881 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015882 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015883 prevsize = grow50pc > growmin ? grow50pc : growmin;
15884 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015885 newprev = prev == NULL ? alloc(prevsize)
15886 : vim_realloc(prev, prevsize);
15887 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015888 {
15889 do_outofmem_msg((long_u)prevsize);
15890 failed = TRUE;
15891 break;
15892 }
15893 prev = newprev;
15894 }
15895 /* Add the line part to end of "prev". */
15896 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015897 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015898 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015899 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015900
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015901 /*
15902 * For a negative line count use only the lines at the end of the file,
15903 * free the rest.
15904 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015905 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015906 while (cnt > -maxline)
15907 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015908 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015909 --cnt;
15910 }
15911
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015912 if (failed)
15913 {
15914 list_free(rettv->vval.v_list, TRUE);
15915 /* readfile doc says an empty list is returned on error */
15916 rettv->vval.v_list = list_alloc();
15917 }
15918
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015919 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015920 fclose(fd);
15921}
15922
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015923#if defined(FEAT_RELTIME)
15924static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15925
15926/*
15927 * Convert a List to proftime_T.
15928 * Return FAIL when there is something wrong.
15929 */
15930 static int
15931list2proftime(arg, tm)
15932 typval_T *arg;
15933 proftime_T *tm;
15934{
15935 long n1, n2;
15936 int error = FALSE;
15937
15938 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15939 || arg->vval.v_list->lv_len != 2)
15940 return FAIL;
15941 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15942 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15943# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015944 tm->HighPart = n1;
15945 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015946# else
15947 tm->tv_sec = n1;
15948 tm->tv_usec = n2;
15949# endif
15950 return error ? FAIL : OK;
15951}
15952#endif /* FEAT_RELTIME */
15953
15954/*
15955 * "reltime()" function
15956 */
15957 static void
15958f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015959 typval_T *argvars UNUSED;
15960 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015961{
15962#ifdef FEAT_RELTIME
15963 proftime_T res;
15964 proftime_T start;
15965
15966 if (argvars[0].v_type == VAR_UNKNOWN)
15967 {
15968 /* No arguments: get current time. */
15969 profile_start(&res);
15970 }
15971 else if (argvars[1].v_type == VAR_UNKNOWN)
15972 {
15973 if (list2proftime(&argvars[0], &res) == FAIL)
15974 return;
15975 profile_end(&res);
15976 }
15977 else
15978 {
15979 /* Two arguments: compute the difference. */
15980 if (list2proftime(&argvars[0], &start) == FAIL
15981 || list2proftime(&argvars[1], &res) == FAIL)
15982 return;
15983 profile_sub(&res, &start);
15984 }
15985
15986 if (rettv_list_alloc(rettv) == OK)
15987 {
15988 long n1, n2;
15989
15990# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015991 n1 = res.HighPart;
15992 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015993# else
15994 n1 = res.tv_sec;
15995 n2 = res.tv_usec;
15996# endif
15997 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15998 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15999 }
16000#endif
16001}
16002
16003/*
16004 * "reltimestr()" function
16005 */
16006 static void
16007f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016008 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016009 typval_T *rettv;
16010{
16011#ifdef FEAT_RELTIME
16012 proftime_T tm;
16013#endif
16014
16015 rettv->v_type = VAR_STRING;
16016 rettv->vval.v_string = NULL;
16017#ifdef FEAT_RELTIME
16018 if (list2proftime(&argvars[0], &tm) == OK)
16019 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16020#endif
16021}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016022
Bram Moolenaar0d660222005-01-07 21:51:51 +000016023#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
16024static void make_connection __ARGS((void));
16025static int check_connection __ARGS((void));
16026
16027 static void
16028make_connection()
16029{
16030 if (X_DISPLAY == NULL
16031# ifdef FEAT_GUI
16032 && !gui.in_use
16033# endif
16034 )
16035 {
16036 x_force_connect = TRUE;
16037 setup_term_clip();
16038 x_force_connect = FALSE;
16039 }
16040}
16041
16042 static int
16043check_connection()
16044{
16045 make_connection();
16046 if (X_DISPLAY == NULL)
16047 {
16048 EMSG(_("E240: No connection to Vim server"));
16049 return FAIL;
16050 }
16051 return OK;
16052}
16053#endif
16054
16055#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016056static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016057
16058 static void
16059remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000016060 typval_T *argvars;
16061 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016062 int expr;
16063{
16064 char_u *server_name;
16065 char_u *keys;
16066 char_u *r = NULL;
16067 char_u buf[NUMBUFLEN];
16068# ifdef WIN32
16069 HWND w;
16070# else
16071 Window w;
16072# endif
16073
16074 if (check_restricted() || check_secure())
16075 return;
16076
16077# ifdef FEAT_X11
16078 if (check_connection() == FAIL)
16079 return;
16080# endif
16081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016082 server_name = get_tv_string_chk(&argvars[0]);
16083 if (server_name == NULL)
16084 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016085 keys = get_tv_string_buf(&argvars[1], buf);
16086# ifdef WIN32
16087 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16088# else
16089 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16090 < 0)
16091# endif
16092 {
16093 if (r != NULL)
16094 EMSG(r); /* sending worked but evaluation failed */
16095 else
16096 EMSG2(_("E241: Unable to send to %s"), server_name);
16097 return;
16098 }
16099
16100 rettv->vval.v_string = r;
16101
16102 if (argvars[2].v_type != VAR_UNKNOWN)
16103 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016104 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016105 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016106 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016107
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016108 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016109 v.di_tv.v_type = VAR_STRING;
16110 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016111 idvar = get_tv_string_chk(&argvars[2]);
16112 if (idvar != NULL)
16113 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016114 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016115 }
16116}
16117#endif
16118
16119/*
16120 * "remote_expr()" function
16121 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016122 static void
16123f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016124 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016125 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016126{
16127 rettv->v_type = VAR_STRING;
16128 rettv->vval.v_string = NULL;
16129#ifdef FEAT_CLIENTSERVER
16130 remote_common(argvars, rettv, TRUE);
16131#endif
16132}
16133
16134/*
16135 * "remote_foreground()" function
16136 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016137 static void
16138f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016139 typval_T *argvars UNUSED;
16140 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016141{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016142#ifdef FEAT_CLIENTSERVER
16143# ifdef WIN32
16144 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016145 {
16146 char_u *server_name = get_tv_string_chk(&argvars[0]);
16147
16148 if (server_name != NULL)
16149 serverForeground(server_name);
16150 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016151# else
16152 /* Send a foreground() expression to the server. */
16153 argvars[1].v_type = VAR_STRING;
16154 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16155 argvars[2].v_type = VAR_UNKNOWN;
16156 remote_common(argvars, rettv, TRUE);
16157 vim_free(argvars[1].vval.v_string);
16158# endif
16159#endif
16160}
16161
Bram Moolenaar0d660222005-01-07 21:51:51 +000016162 static void
16163f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016164 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016165 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016166{
16167#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016168 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016169 char_u *s = NULL;
16170# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016171 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016172# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016173 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016174
16175 if (check_restricted() || check_secure())
16176 {
16177 rettv->vval.v_number = -1;
16178 return;
16179 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016180 serverid = get_tv_string_chk(&argvars[0]);
16181 if (serverid == NULL)
16182 {
16183 rettv->vval.v_number = -1;
16184 return; /* type error; errmsg already given */
16185 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016186# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016187 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016188 if (n == 0)
16189 rettv->vval.v_number = -1;
16190 else
16191 {
16192 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16193 rettv->vval.v_number = (s != NULL);
16194 }
16195# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016196 if (check_connection() == FAIL)
16197 return;
16198
16199 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016200 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016201# endif
16202
16203 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016205 char_u *retvar;
16206
Bram Moolenaar33570922005-01-25 22:26:29 +000016207 v.di_tv.v_type = VAR_STRING;
16208 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016209 retvar = get_tv_string_chk(&argvars[1]);
16210 if (retvar != NULL)
16211 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016212 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016213 }
16214#else
16215 rettv->vval.v_number = -1;
16216#endif
16217}
16218
Bram Moolenaar0d660222005-01-07 21:51:51 +000016219 static void
16220f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016221 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016222 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016223{
16224 char_u *r = NULL;
16225
16226#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016227 char_u *serverid = get_tv_string_chk(&argvars[0]);
16228
16229 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016230 {
16231# ifdef WIN32
16232 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016233 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016234
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016235 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016236 if (n != 0)
16237 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16238 if (r == NULL)
16239# else
16240 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016241 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016242# endif
16243 EMSG(_("E277: Unable to read a server reply"));
16244 }
16245#endif
16246 rettv->v_type = VAR_STRING;
16247 rettv->vval.v_string = r;
16248}
16249
16250/*
16251 * "remote_send()" function
16252 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016253 static void
16254f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016255 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016256 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016257{
16258 rettv->v_type = VAR_STRING;
16259 rettv->vval.v_string = NULL;
16260#ifdef FEAT_CLIENTSERVER
16261 remote_common(argvars, rettv, FALSE);
16262#endif
16263}
16264
16265/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016266 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016267 */
16268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016269f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016270 typval_T *argvars;
16271 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016272{
Bram Moolenaar33570922005-01-25 22:26:29 +000016273 list_T *l;
16274 listitem_T *item, *item2;
16275 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016276 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016277 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016278 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016279 dict_T *d;
16280 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016281 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016282
Bram Moolenaar8c711452005-01-14 21:53:12 +000016283 if (argvars[0].v_type == VAR_DICT)
16284 {
16285 if (argvars[2].v_type != VAR_UNKNOWN)
16286 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016287 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016288 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016290 key = get_tv_string_chk(&argvars[1]);
16291 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016293 di = dict_find(d, key, -1);
16294 if (di == NULL)
16295 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016296 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16297 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016298 {
16299 *rettv = di->di_tv;
16300 init_tv(&di->di_tv);
16301 dictitem_remove(d, di);
16302 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016303 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016304 }
16305 }
16306 else if (argvars[0].v_type != VAR_LIST)
16307 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016308 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016309 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016311 int error = FALSE;
16312
16313 idx = get_tv_number_chk(&argvars[1], &error);
16314 if (error)
16315 ; /* type error: do nothing, errmsg already given */
16316 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016317 EMSGN(_(e_listidx), idx);
16318 else
16319 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016320 if (argvars[2].v_type == VAR_UNKNOWN)
16321 {
16322 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016323 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016324 *rettv = item->li_tv;
16325 vim_free(item);
16326 }
16327 else
16328 {
16329 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016330 end = get_tv_number_chk(&argvars[2], &error);
16331 if (error)
16332 ; /* type error: do nothing */
16333 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016334 EMSGN(_(e_listidx), end);
16335 else
16336 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016337 int cnt = 0;
16338
16339 for (li = item; li != NULL; li = li->li_next)
16340 {
16341 ++cnt;
16342 if (li == item2)
16343 break;
16344 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016345 if (li == NULL) /* didn't find "item2" after "item" */
16346 EMSG(_(e_invrange));
16347 else
16348 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016349 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016350 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016351 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016352 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016353 l->lv_first = item;
16354 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016355 item->li_prev = NULL;
16356 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016357 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016358 }
16359 }
16360 }
16361 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016362 }
16363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364}
16365
16366/*
16367 * "rename({from}, {to})" function
16368 */
16369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016370f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016371 typval_T *argvars;
16372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373{
16374 char_u buf[NUMBUFLEN];
16375
16376 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016377 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016379 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16380 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381}
16382
16383/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016384 * "repeat()" function
16385 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016386 static void
16387f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016388 typval_T *argvars;
16389 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016390{
16391 char_u *p;
16392 int n;
16393 int slen;
16394 int len;
16395 char_u *r;
16396 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016397
16398 n = get_tv_number(&argvars[1]);
16399 if (argvars[0].v_type == VAR_LIST)
16400 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016401 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016402 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016403 if (list_extend(rettv->vval.v_list,
16404 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016405 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016406 }
16407 else
16408 {
16409 p = get_tv_string(&argvars[0]);
16410 rettv->v_type = VAR_STRING;
16411 rettv->vval.v_string = NULL;
16412
16413 slen = (int)STRLEN(p);
16414 len = slen * n;
16415 if (len <= 0)
16416 return;
16417
16418 r = alloc(len + 1);
16419 if (r != NULL)
16420 {
16421 for (i = 0; i < n; i++)
16422 mch_memmove(r + i * slen, p, (size_t)slen);
16423 r[len] = NUL;
16424 }
16425
16426 rettv->vval.v_string = r;
16427 }
16428}
16429
16430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431 * "resolve()" function
16432 */
16433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016434f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016435 typval_T *argvars;
16436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437{
16438 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016439#ifdef HAVE_READLINK
16440 char_u *buf = NULL;
16441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016443 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444#ifdef FEAT_SHORTCUT
16445 {
16446 char_u *v = NULL;
16447
16448 v = mch_resolve_shortcut(p);
16449 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016450 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016452 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453 }
16454#else
16455# ifdef HAVE_READLINK
16456 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 char_u *cpy;
16458 int len;
16459 char_u *remain = NULL;
16460 char_u *q;
16461 int is_relative_to_current = FALSE;
16462 int has_trailing_pathsep = FALSE;
16463 int limit = 100;
16464
16465 p = vim_strsave(p);
16466
16467 if (p[0] == '.' && (vim_ispathsep(p[1])
16468 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16469 is_relative_to_current = TRUE;
16470
16471 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016472 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016475 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477
16478 q = getnextcomp(p);
16479 if (*q != NUL)
16480 {
16481 /* Separate the first path component in "p", and keep the
16482 * remainder (beginning with the path separator). */
16483 remain = vim_strsave(q - 1);
16484 q[-1] = NUL;
16485 }
16486
Bram Moolenaard9462e32011-04-11 21:35:11 +020016487 buf = alloc(MAXPATHL + 1);
16488 if (buf == NULL)
16489 goto fail;
16490
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491 for (;;)
16492 {
16493 for (;;)
16494 {
16495 len = readlink((char *)p, (char *)buf, MAXPATHL);
16496 if (len <= 0)
16497 break;
16498 buf[len] = NUL;
16499
16500 if (limit-- == 0)
16501 {
16502 vim_free(p);
16503 vim_free(remain);
16504 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016505 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016506 goto fail;
16507 }
16508
16509 /* Ensure that the result will have a trailing path separator
16510 * if the argument has one. */
16511 if (remain == NULL && has_trailing_pathsep)
16512 add_pathsep(buf);
16513
16514 /* Separate the first path component in the link value and
16515 * concatenate the remainders. */
16516 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16517 if (*q != NUL)
16518 {
16519 if (remain == NULL)
16520 remain = vim_strsave(q - 1);
16521 else
16522 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016523 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524 if (cpy != NULL)
16525 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526 vim_free(remain);
16527 remain = cpy;
16528 }
16529 }
16530 q[-1] = NUL;
16531 }
16532
16533 q = gettail(p);
16534 if (q > p && *q == NUL)
16535 {
16536 /* Ignore trailing path separator. */
16537 q[-1] = NUL;
16538 q = gettail(p);
16539 }
16540 if (q > p && !mch_isFullName(buf))
16541 {
16542 /* symlink is relative to directory of argument */
16543 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16544 if (cpy != NULL)
16545 {
16546 STRCPY(cpy, p);
16547 STRCPY(gettail(cpy), buf);
16548 vim_free(p);
16549 p = cpy;
16550 }
16551 }
16552 else
16553 {
16554 vim_free(p);
16555 p = vim_strsave(buf);
16556 }
16557 }
16558
16559 if (remain == NULL)
16560 break;
16561
16562 /* Append the first path component of "remain" to "p". */
16563 q = getnextcomp(remain + 1);
16564 len = q - remain - (*q != NUL);
16565 cpy = vim_strnsave(p, STRLEN(p) + len);
16566 if (cpy != NULL)
16567 {
16568 STRNCAT(cpy, remain, len);
16569 vim_free(p);
16570 p = cpy;
16571 }
16572 /* Shorten "remain". */
16573 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016574 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 else
16576 {
16577 vim_free(remain);
16578 remain = NULL;
16579 }
16580 }
16581
16582 /* If the result is a relative path name, make it explicitly relative to
16583 * the current directory if and only if the argument had this form. */
16584 if (!vim_ispathsep(*p))
16585 {
16586 if (is_relative_to_current
16587 && *p != NUL
16588 && !(p[0] == '.'
16589 && (p[1] == NUL
16590 || vim_ispathsep(p[1])
16591 || (p[1] == '.'
16592 && (p[2] == NUL
16593 || vim_ispathsep(p[2]))))))
16594 {
16595 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016596 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597 if (cpy != NULL)
16598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599 vim_free(p);
16600 p = cpy;
16601 }
16602 }
16603 else if (!is_relative_to_current)
16604 {
16605 /* Strip leading "./". */
16606 q = p;
16607 while (q[0] == '.' && vim_ispathsep(q[1]))
16608 q += 2;
16609 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016610 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611 }
16612 }
16613
16614 /* Ensure that the result will have no trailing path separator
16615 * if the argument had none. But keep "/" or "//". */
16616 if (!has_trailing_pathsep)
16617 {
16618 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016619 if (after_pathsep(p, q))
16620 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621 }
16622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016623 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624 }
16625# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016626 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627# endif
16628#endif
16629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016630 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631
16632#ifdef HAVE_READLINK
16633fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016634 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016636 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637}
16638
16639/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016640 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 */
16642 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016643f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016644 typval_T *argvars;
16645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646{
Bram Moolenaar33570922005-01-25 22:26:29 +000016647 list_T *l;
16648 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649
Bram Moolenaar0d660222005-01-07 21:51:51 +000016650 if (argvars[0].v_type != VAR_LIST)
16651 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016652 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016653 && !tv_check_lock(l->lv_lock,
16654 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016655 {
16656 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016657 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016658 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016659 while (li != NULL)
16660 {
16661 ni = li->li_prev;
16662 list_append(l, li);
16663 li = ni;
16664 }
16665 rettv->vval.v_list = l;
16666 rettv->v_type = VAR_LIST;
16667 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016668 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670}
16671
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016672#define SP_NOMOVE 0x01 /* don't move cursor */
16673#define SP_REPEAT 0x02 /* repeat to find outer pair */
16674#define SP_RETCOUNT 0x04 /* return matchcount */
16675#define SP_SETPCMARK 0x08 /* set previous context mark */
16676#define SP_START 0x10 /* accept match at start position */
16677#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16678#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016679#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016680
Bram Moolenaar33570922005-01-25 22:26:29 +000016681static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016682
16683/*
16684 * Get flags for a search function.
16685 * Possibly sets "p_ws".
16686 * Returns BACKWARD, FORWARD or zero (for an error).
16687 */
16688 static int
16689get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016690 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016691 int *flagsp;
16692{
16693 int dir = FORWARD;
16694 char_u *flags;
16695 char_u nbuf[NUMBUFLEN];
16696 int mask;
16697
16698 if (varp->v_type != VAR_UNKNOWN)
16699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016700 flags = get_tv_string_buf_chk(varp, nbuf);
16701 if (flags == NULL)
16702 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016703 while (*flags != NUL)
16704 {
16705 switch (*flags)
16706 {
16707 case 'b': dir = BACKWARD; break;
16708 case 'w': p_ws = TRUE; break;
16709 case 'W': p_ws = FALSE; break;
16710 default: mask = 0;
16711 if (flagsp != NULL)
16712 switch (*flags)
16713 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016714 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016715 case 'e': mask = SP_END; break;
16716 case 'm': mask = SP_RETCOUNT; break;
16717 case 'n': mask = SP_NOMOVE; break;
16718 case 'p': mask = SP_SUBPAT; break;
16719 case 'r': mask = SP_REPEAT; break;
16720 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016721 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016722 }
16723 if (mask == 0)
16724 {
16725 EMSG2(_(e_invarg2), flags);
16726 dir = 0;
16727 }
16728 else
16729 *flagsp |= mask;
16730 }
16731 if (dir == 0)
16732 break;
16733 ++flags;
16734 }
16735 }
16736 return dir;
16737}
16738
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016740 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016742 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016743search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016744 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016745 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016746 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016748 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749 char_u *pat;
16750 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016751 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752 int save_p_ws = p_ws;
16753 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016754 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016755 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016756 proftime_T tm;
16757#ifdef FEAT_RELTIME
16758 long time_limit = 0;
16759#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016760 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016761 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016763 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016764 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016765 if (dir == 0)
16766 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016767 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016768 if (flags & SP_START)
16769 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016770 if (flags & SP_END)
16771 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016772 if (flags & SP_COLUMN)
16773 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016774
Bram Moolenaar76929292008-01-06 19:07:36 +000016775 /* Optional arguments: line number to stop searching and timeout. */
16776 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016777 {
16778 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16779 if (lnum_stop < 0)
16780 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016781#ifdef FEAT_RELTIME
16782 if (argvars[3].v_type != VAR_UNKNOWN)
16783 {
16784 time_limit = get_tv_number_chk(&argvars[3], NULL);
16785 if (time_limit < 0)
16786 goto theend;
16787 }
16788#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016789 }
16790
Bram Moolenaar76929292008-01-06 19:07:36 +000016791#ifdef FEAT_RELTIME
16792 /* Set the time limit, if there is one. */
16793 profile_setlimit(time_limit, &tm);
16794#endif
16795
Bram Moolenaar231334e2005-07-25 20:46:57 +000016796 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016797 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016798 * Check to make sure only those flags are set.
16799 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16800 * flags cannot be set. Check for that condition also.
16801 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016802 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016803 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016805 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016806 goto theend;
16807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016809 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016810 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016811 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016812 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016814 if (flags & SP_SUBPAT)
16815 retval = subpatnum;
16816 else
16817 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016818 if (flags & SP_SETPCMARK)
16819 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016821 if (match_pos != NULL)
16822 {
16823 /* Store the match cursor position */
16824 match_pos->lnum = pos.lnum;
16825 match_pos->col = pos.col + 1;
16826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 /* "/$" will put the cursor after the end of the line, may need to
16828 * correct that here */
16829 check_cursor();
16830 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016831
16832 /* If 'n' flag is used: restore cursor position. */
16833 if (flags & SP_NOMOVE)
16834 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016835 else
16836 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016837theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016839
16840 return retval;
16841}
16842
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016843#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016844
16845/*
16846 * round() is not in C90, use ceil() or floor() instead.
16847 */
16848 float_T
16849vim_round(f)
16850 float_T f;
16851{
16852 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16853}
16854
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016855/*
16856 * "round({float})" function
16857 */
16858 static void
16859f_round(argvars, rettv)
16860 typval_T *argvars;
16861 typval_T *rettv;
16862{
16863 float_T f;
16864
16865 rettv->v_type = VAR_FLOAT;
16866 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016867 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016868 else
16869 rettv->vval.v_float = 0.0;
16870}
16871#endif
16872
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016873/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016874 * "screenattr()" function
16875 */
16876 static void
16877f_screenattr(argvars, rettv)
16878 typval_T *argvars UNUSED;
16879 typval_T *rettv;
16880{
16881 int row;
16882 int col;
16883 int c;
16884
16885 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16886 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16887 if (row < 0 || row >= screen_Rows
16888 || col < 0 || col >= screen_Columns)
16889 c = -1;
16890 else
16891 c = ScreenAttrs[LineOffset[row] + col];
16892 rettv->vval.v_number = c;
16893}
16894
16895/*
16896 * "screenchar()" function
16897 */
16898 static void
16899f_screenchar(argvars, rettv)
16900 typval_T *argvars UNUSED;
16901 typval_T *rettv;
16902{
16903 int row;
16904 int col;
16905 int off;
16906 int c;
16907
16908 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16909 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16910 if (row < 0 || row >= screen_Rows
16911 || col < 0 || col >= screen_Columns)
16912 c = -1;
16913 else
16914 {
16915 off = LineOffset[row] + col;
16916#ifdef FEAT_MBYTE
16917 if (enc_utf8 && ScreenLinesUC[off] != 0)
16918 c = ScreenLinesUC[off];
16919 else
16920#endif
16921 c = ScreenLines[off];
16922 }
16923 rettv->vval.v_number = c;
16924}
16925
16926/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016927 * "screencol()" function
16928 *
16929 * First column is 1 to be consistent with virtcol().
16930 */
16931 static void
16932f_screencol(argvars, rettv)
16933 typval_T *argvars UNUSED;
16934 typval_T *rettv;
16935{
16936 rettv->vval.v_number = screen_screencol() + 1;
16937}
16938
16939/*
16940 * "screenrow()" function
16941 */
16942 static void
16943f_screenrow(argvars, rettv)
16944 typval_T *argvars UNUSED;
16945 typval_T *rettv;
16946{
16947 rettv->vval.v_number = screen_screenrow() + 1;
16948}
16949
16950/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016951 * "search()" function
16952 */
16953 static void
16954f_search(argvars, rettv)
16955 typval_T *argvars;
16956 typval_T *rettv;
16957{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016958 int flags = 0;
16959
16960 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961}
16962
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016964 * "searchdecl()" function
16965 */
16966 static void
16967f_searchdecl(argvars, rettv)
16968 typval_T *argvars;
16969 typval_T *rettv;
16970{
16971 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016972 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016973 int error = FALSE;
16974 char_u *name;
16975
16976 rettv->vval.v_number = 1; /* default: FAIL */
16977
16978 name = get_tv_string_chk(&argvars[0]);
16979 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016980 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016981 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016982 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16983 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16984 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016985 if (!error && name != NULL)
16986 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016987 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016988}
16989
16990/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016991 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016993 static int
16994searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016995 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016996 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997{
16998 char_u *spat, *mpat, *epat;
16999 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 int dir;
17002 int flags = 0;
17003 char_u nbuf1[NUMBUFLEN];
17004 char_u nbuf2[NUMBUFLEN];
17005 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017006 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017007 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017008 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017011 spat = get_tv_string_chk(&argvars[0]);
17012 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17013 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17014 if (spat == NULL || mpat == NULL || epat == NULL)
17015 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 /* Handle the optional fourth argument: flags */
17018 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017019 if (dir == 0)
17020 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017021
17022 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017023 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17024 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017025 if ((flags & (SP_END | SP_SUBPAT)) != 0
17026 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017027 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017028 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017029 goto theend;
17030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017032 /* Using 'r' implies 'W', otherwise it doesn't work. */
17033 if (flags & SP_REPEAT)
17034 p_ws = FALSE;
17035
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017036 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017037 if (argvars[3].v_type == VAR_UNKNOWN
17038 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 skip = (char_u *)"";
17040 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017042 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017043 if (argvars[5].v_type != VAR_UNKNOWN)
17044 {
17045 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17046 if (lnum_stop < 0)
17047 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017048#ifdef FEAT_RELTIME
17049 if (argvars[6].v_type != VAR_UNKNOWN)
17050 {
17051 time_limit = get_tv_number_chk(&argvars[6], NULL);
17052 if (time_limit < 0)
17053 goto theend;
17054 }
17055#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017056 }
17057 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017058 if (skip == NULL)
17059 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017061 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017062 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017063
17064theend:
17065 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017066
17067 return retval;
17068}
17069
17070/*
17071 * "searchpair()" function
17072 */
17073 static void
17074f_searchpair(argvars, rettv)
17075 typval_T *argvars;
17076 typval_T *rettv;
17077{
17078 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17079}
17080
17081/*
17082 * "searchpairpos()" function
17083 */
17084 static void
17085f_searchpairpos(argvars, rettv)
17086 typval_T *argvars;
17087 typval_T *rettv;
17088{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017089 pos_T match_pos;
17090 int lnum = 0;
17091 int col = 0;
17092
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017093 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017094 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017095
17096 if (searchpair_cmn(argvars, &match_pos) > 0)
17097 {
17098 lnum = match_pos.lnum;
17099 col = match_pos.col;
17100 }
17101
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017102 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17103 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017104}
17105
17106/*
17107 * Search for a start/middle/end thing.
17108 * Used by searchpair(), see its documentation for the details.
17109 * Returns 0 or -1 for no match,
17110 */
17111 long
Bram Moolenaar76929292008-01-06 19:07:36 +000017112do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
17113 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017114 char_u *spat; /* start pattern */
17115 char_u *mpat; /* middle pattern */
17116 char_u *epat; /* end pattern */
17117 int dir; /* BACKWARD or FORWARD */
17118 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017119 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017120 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017121 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017122 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017123{
17124 char_u *save_cpo;
17125 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17126 long retval = 0;
17127 pos_T pos;
17128 pos_T firstpos;
17129 pos_T foundpos;
17130 pos_T save_cursor;
17131 pos_T save_pos;
17132 int n;
17133 int r;
17134 int nest = 1;
17135 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017136 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017137 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017138
17139 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17140 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017141 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017142
Bram Moolenaar76929292008-01-06 19:07:36 +000017143#ifdef FEAT_RELTIME
17144 /* Set the time limit, if there is one. */
17145 profile_setlimit(time_limit, &tm);
17146#endif
17147
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017148 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17149 * start/middle/end (pat3, for the top pair). */
17150 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17151 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17152 if (pat2 == NULL || pat3 == NULL)
17153 goto theend;
17154 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17155 if (*mpat == NUL)
17156 STRCPY(pat3, pat2);
17157 else
17158 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17159 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017160 if (flags & SP_START)
17161 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017162
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 save_cursor = curwin->w_cursor;
17164 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017165 clearpos(&firstpos);
17166 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 pat = pat3;
17168 for (;;)
17169 {
17170 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017171 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17173 /* didn't find it or found the first match again: FAIL */
17174 break;
17175
17176 if (firstpos.lnum == 0)
17177 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017178 if (equalpos(pos, foundpos))
17179 {
17180 /* Found the same position again. Can happen with a pattern that
17181 * has "\zs" at the end and searching backwards. Advance one
17182 * character and try again. */
17183 if (dir == BACKWARD)
17184 decl(&pos);
17185 else
17186 incl(&pos);
17187 }
17188 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017190 /* clear the start flag to avoid getting stuck here */
17191 options &= ~SEARCH_START;
17192
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 /* If the skip pattern matches, ignore this match. */
17194 if (*skip != NUL)
17195 {
17196 save_pos = curwin->w_cursor;
17197 curwin->w_cursor = pos;
17198 r = eval_to_bool(skip, &err, NULL, FALSE);
17199 curwin->w_cursor = save_pos;
17200 if (err)
17201 {
17202 /* Evaluating {skip} caused an error, break here. */
17203 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017204 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205 break;
17206 }
17207 if (r)
17208 continue;
17209 }
17210
17211 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17212 {
17213 /* Found end when searching backwards or start when searching
17214 * forward: nested pair. */
17215 ++nest;
17216 pat = pat2; /* nested, don't search for middle */
17217 }
17218 else
17219 {
17220 /* Found end when searching forward or start when searching
17221 * backward: end of (nested) pair; or found middle in outer pair. */
17222 if (--nest == 1)
17223 pat = pat3; /* outer level, search for middle */
17224 }
17225
17226 if (nest == 0)
17227 {
17228 /* Found the match: return matchcount or line number. */
17229 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017230 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017232 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017233 if (flags & SP_SETPCMARK)
17234 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 curwin->w_cursor = pos;
17236 if (!(flags & SP_REPEAT))
17237 break;
17238 nest = 1; /* search for next unmatched */
17239 }
17240 }
17241
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017242 if (match_pos != NULL)
17243 {
17244 /* Store the match cursor position */
17245 match_pos->lnum = curwin->w_cursor.lnum;
17246 match_pos->col = curwin->w_cursor.col + 1;
17247 }
17248
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017250 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 curwin->w_cursor = save_cursor;
17252
17253theend:
17254 vim_free(pat2);
17255 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017256 if (p_cpo == empty_option)
17257 p_cpo = save_cpo;
17258 else
17259 /* Darn, evaluating the {skip} expression changed the value. */
17260 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017261
17262 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263}
17264
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017265/*
17266 * "searchpos()" function
17267 */
17268 static void
17269f_searchpos(argvars, rettv)
17270 typval_T *argvars;
17271 typval_T *rettv;
17272{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017273 pos_T match_pos;
17274 int lnum = 0;
17275 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017276 int n;
17277 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017278
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017279 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017280 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017281
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017282 n = search_cmn(argvars, &match_pos, &flags);
17283 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017284 {
17285 lnum = match_pos.lnum;
17286 col = match_pos.col;
17287 }
17288
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017289 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17290 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017291 if (flags & SP_SUBPAT)
17292 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017293}
17294
17295
Bram Moolenaar0d660222005-01-07 21:51:51 +000017296 static void
17297f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017298 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017301#ifdef FEAT_CLIENTSERVER
17302 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017303 char_u *server = get_tv_string_chk(&argvars[0]);
17304 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305
Bram Moolenaar0d660222005-01-07 21:51:51 +000017306 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017307 if (server == NULL || reply == NULL)
17308 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017309 if (check_restricted() || check_secure())
17310 return;
17311# ifdef FEAT_X11
17312 if (check_connection() == FAIL)
17313 return;
17314# endif
17315
17316 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017318 EMSG(_("E258: Unable to send to client"));
17319 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017321 rettv->vval.v_number = 0;
17322#else
17323 rettv->vval.v_number = -1;
17324#endif
17325}
17326
Bram Moolenaar0d660222005-01-07 21:51:51 +000017327 static void
17328f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017329 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017330 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017331{
17332 char_u *r = NULL;
17333
17334#ifdef FEAT_CLIENTSERVER
17335# ifdef WIN32
17336 r = serverGetVimNames();
17337# else
17338 make_connection();
17339 if (X_DISPLAY != NULL)
17340 r = serverGetVimNames(X_DISPLAY);
17341# endif
17342#endif
17343 rettv->v_type = VAR_STRING;
17344 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345}
17346
17347/*
17348 * "setbufvar()" function
17349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017351f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017352 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017353 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354{
17355 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017358 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359 char_u nbuf[NUMBUFLEN];
17360
17361 if (check_restricted() || check_secure())
17362 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017363 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17364 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017365 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 varp = &argvars[2];
17367
17368 if (buf != NULL && varname != NULL && varp != NULL)
17369 {
17370 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372
17373 if (*varname == '&')
17374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017375 long numval;
17376 char_u *strval;
17377 int error = FALSE;
17378
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017380 numval = get_tv_number_chk(varp, &error);
17381 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017382 if (!error && strval != NULL)
17383 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 }
17385 else
17386 {
17387 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17388 if (bufvarname != NULL)
17389 {
17390 STRCPY(bufvarname, "b:");
17391 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017392 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 vim_free(bufvarname);
17394 }
17395 }
17396
17397 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400}
17401
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017402 static void
17403f_setcharsearch(argvars, rettv)
17404 typval_T *argvars;
17405 typval_T *rettv UNUSED;
17406{
17407 dict_T *d;
17408 dictitem_T *di;
17409 char_u *csearch;
17410
17411 if (argvars[0].v_type != VAR_DICT)
17412 {
17413 EMSG(_(e_dictreq));
17414 return;
17415 }
17416
17417 if ((d = argvars[0].vval.v_dict) != NULL)
17418 {
17419 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17420 if (csearch != NULL)
17421 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017422#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017423 if (enc_utf8)
17424 {
17425 int pcc[MAX_MCO];
17426 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017427
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017428 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17429 }
17430 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017431#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017432 set_last_csearch(PTR2CHAR(csearch),
17433 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017434 }
17435
17436 di = dict_find(d, (char_u *)"forward", -1);
17437 if (di != NULL)
17438 set_csearch_direction(get_tv_number(&di->di_tv)
17439 ? FORWARD : BACKWARD);
17440
17441 di = dict_find(d, (char_u *)"until", -1);
17442 if (di != NULL)
17443 set_csearch_until(!!get_tv_number(&di->di_tv));
17444 }
17445}
17446
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447/*
17448 * "setcmdpos()" function
17449 */
17450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017451f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017452 typval_T *argvars;
17453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017455 int pos = (int)get_tv_number(&argvars[0]) - 1;
17456
17457 if (pos >= 0)
17458 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459}
17460
17461/*
17462 * "setline()" function
17463 */
17464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017465f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017466 typval_T *argvars;
17467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468{
17469 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017470 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017471 list_T *l = NULL;
17472 listitem_T *li = NULL;
17473 long added = 0;
17474 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017476 lnum = get_tv_lnum(&argvars[0]);
17477 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017479 l = argvars[1].vval.v_list;
17480 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017482 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017483 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017484
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017485 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017486 for (;;)
17487 {
17488 if (l != NULL)
17489 {
17490 /* list argument, get next string */
17491 if (li == NULL)
17492 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017493 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017494 li = li->li_next;
17495 }
17496
17497 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017498 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017499 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017500
17501 /* When coming here from Insert mode, sync undo, so that this can be
17502 * undone separately from what was previously inserted. */
17503 if (u_sync_once == 2)
17504 {
17505 u_sync_once = 1; /* notify that u_sync() was called */
17506 u_sync(TRUE);
17507 }
17508
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017509 if (lnum <= curbuf->b_ml.ml_line_count)
17510 {
17511 /* existing line, replace it */
17512 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17513 {
17514 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017515 if (lnum == curwin->w_cursor.lnum)
17516 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017517 rettv->vval.v_number = 0; /* OK */
17518 }
17519 }
17520 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17521 {
17522 /* lnum is one past the last line, append the line */
17523 ++added;
17524 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17525 rettv->vval.v_number = 0; /* OK */
17526 }
17527
17528 if (l == NULL) /* only one string argument */
17529 break;
17530 ++lnum;
17531 }
17532
17533 if (added > 0)
17534 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535}
17536
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017537static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17538
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017540 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017541 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017542 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017543set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017544 win_T *wp UNUSED;
17545 typval_T *list_arg UNUSED;
17546 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017547 typval_T *rettv;
17548{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017549#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017550 char_u *act;
17551 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017552#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017553
Bram Moolenaar2641f772005-03-25 21:58:17 +000017554 rettv->vval.v_number = -1;
17555
17556#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017557 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017558 EMSG(_(e_listreq));
17559 else
17560 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017561 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017562
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017563 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017564 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017565 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017566 if (act == NULL)
17567 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017568 if (*act == 'a' || *act == 'r')
17569 action = *act;
17570 }
17571
Bram Moolenaar81484f42012-12-05 15:16:47 +010017572 if (l != NULL && set_errorlist(wp, l, action,
17573 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017574 rettv->vval.v_number = 0;
17575 }
17576#endif
17577}
17578
17579/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017580 * "setloclist()" function
17581 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017582 static void
17583f_setloclist(argvars, rettv)
17584 typval_T *argvars;
17585 typval_T *rettv;
17586{
17587 win_T *win;
17588
17589 rettv->vval.v_number = -1;
17590
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017591 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017592 if (win != NULL)
17593 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17594}
17595
17596/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017597 * "setmatches()" function
17598 */
17599 static void
17600f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017601 typval_T *argvars UNUSED;
17602 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017603{
17604#ifdef FEAT_SEARCH_EXTRA
17605 list_T *l;
17606 listitem_T *li;
17607 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017608 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017609
17610 rettv->vval.v_number = -1;
17611 if (argvars[0].v_type != VAR_LIST)
17612 {
17613 EMSG(_(e_listreq));
17614 return;
17615 }
17616 if ((l = argvars[0].vval.v_list) != NULL)
17617 {
17618
17619 /* To some extent make sure that we are dealing with a list from
17620 * "getmatches()". */
17621 li = l->lv_first;
17622 while (li != NULL)
17623 {
17624 if (li->li_tv.v_type != VAR_DICT
17625 || (d = li->li_tv.vval.v_dict) == NULL)
17626 {
17627 EMSG(_(e_invarg));
17628 return;
17629 }
17630 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017631 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17632 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017633 && dict_find(d, (char_u *)"priority", -1) != NULL
17634 && dict_find(d, (char_u *)"id", -1) != NULL))
17635 {
17636 EMSG(_(e_invarg));
17637 return;
17638 }
17639 li = li->li_next;
17640 }
17641
17642 clear_matches(curwin);
17643 li = l->lv_first;
17644 while (li != NULL)
17645 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017646 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017647 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017648 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017649 char_u *group;
17650 int priority;
17651 int id;
17652 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017653
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017654 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017655 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17656 {
17657 if (s == NULL)
17658 {
17659 s = list_alloc();
17660 if (s == NULL)
17661 return;
17662 }
17663
17664 /* match from matchaddpos() */
17665 for (i = 1; i < 9; i++)
17666 {
17667 sprintf((char *)buf, (char *)"pos%d", i);
17668 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17669 {
17670 if (di->di_tv.v_type != VAR_LIST)
17671 return;
17672
17673 list_append_tv(s, &di->di_tv);
17674 s->lv_refcount++;
17675 }
17676 else
17677 break;
17678 }
17679 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017680
17681 group = get_dict_string(d, (char_u *)"group", FALSE);
17682 priority = (int)get_dict_number(d, (char_u *)"priority");
17683 id = (int)get_dict_number(d, (char_u *)"id");
17684 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17685 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17686 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017687 if (i == 0)
17688 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017689 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017690 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017691 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017692 }
17693 else
17694 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017695 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017696 list_unref(s);
17697 s = NULL;
17698 }
17699
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017700 li = li->li_next;
17701 }
17702 rettv->vval.v_number = 0;
17703 }
17704#endif
17705}
17706
17707/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017708 * "setpos()" function
17709 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017710 static void
17711f_setpos(argvars, rettv)
17712 typval_T *argvars;
17713 typval_T *rettv;
17714{
17715 pos_T pos;
17716 int fnum;
17717 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017718 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017719
Bram Moolenaar08250432008-02-13 11:42:46 +000017720 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017721 name = get_tv_string_chk(argvars);
17722 if (name != NULL)
17723 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017724 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017725 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017726 if (--pos.col < 0)
17727 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017728 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017729 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017730 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017731 if (fnum == curbuf->b_fnum)
17732 {
17733 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017734 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017735 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017736 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017737 curwin->w_set_curswant = FALSE;
17738 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017739 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017740 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017741 }
17742 else
17743 EMSG(_(e_invarg));
17744 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017745 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17746 {
17747 /* set mark */
17748 if (setmark_pos(name[1], &pos, fnum) == OK)
17749 rettv->vval.v_number = 0;
17750 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017751 else
17752 EMSG(_(e_invarg));
17753 }
17754 }
17755}
17756
17757/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017758 * "setqflist()" function
17759 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017760 static void
17761f_setqflist(argvars, rettv)
17762 typval_T *argvars;
17763 typval_T *rettv;
17764{
17765 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17766}
17767
17768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 * "setreg()" function
17770 */
17771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017772f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017773 typval_T *argvars;
17774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775{
17776 int regname;
17777 char_u *strregname;
17778 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017779 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780 int append;
17781 char_u yank_type;
17782 long block_len;
17783
17784 block_len = -1;
17785 yank_type = MAUTO;
17786 append = FALSE;
17787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017788 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017789 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017791 if (strregname == NULL)
17792 return; /* type error; errmsg already given */
17793 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794 if (regname == 0 || regname == '@')
17795 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017797 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017799 stropt = get_tv_string_chk(&argvars[2]);
17800 if (stropt == NULL)
17801 return; /* type error */
17802 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 switch (*stropt)
17804 {
17805 case 'a': case 'A': /* append */
17806 append = TRUE;
17807 break;
17808 case 'v': case 'c': /* character-wise selection */
17809 yank_type = MCHAR;
17810 break;
17811 case 'V': case 'l': /* line-wise selection */
17812 yank_type = MLINE;
17813 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814 case 'b': case Ctrl_V: /* block-wise selection */
17815 yank_type = MBLOCK;
17816 if (VIM_ISDIGIT(stropt[1]))
17817 {
17818 ++stropt;
17819 block_len = getdigits(&stropt) - 1;
17820 --stropt;
17821 }
17822 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823 }
17824 }
17825
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017826 if (argvars[1].v_type == VAR_LIST)
17827 {
17828 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017829 char_u **allocval;
17830 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017831 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017832 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017833 int len = argvars[1].vval.v_list->lv_len;
17834 listitem_T *li;
17835
Bram Moolenaar7d647822014-04-05 21:28:56 +020017836 /* First half: use for pointers to result lines; second half: use for
17837 * pointers to allocated copies. */
17838 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017839 if (lstval == NULL)
17840 return;
17841 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017842 allocval = lstval + len + 2;
17843 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017844
17845 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17846 li = li->li_next)
17847 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017848 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017849 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017850 goto free_lstval;
17851 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017852 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017853 /* Need to make a copy, next get_tv_string_buf_chk() will
17854 * overwrite the string. */
17855 strval = vim_strsave(buf);
17856 if (strval == NULL)
17857 goto free_lstval;
17858 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017859 }
17860 *curval++ = strval;
17861 }
17862 *curval++ = NULL;
17863
17864 write_reg_contents_lst(regname, lstval, -1,
17865 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017866free_lstval:
17867 while (curallocval > allocval)
17868 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017869 vim_free(lstval);
17870 }
17871 else
17872 {
17873 strval = get_tv_string_chk(&argvars[1]);
17874 if (strval == NULL)
17875 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017876 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017878 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017879 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880}
17881
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017882/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017883 * "settabvar()" function
17884 */
17885 static void
17886f_settabvar(argvars, rettv)
17887 typval_T *argvars;
17888 typval_T *rettv;
17889{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017890#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017891 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017892 tabpage_T *tp;
17893#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017894 char_u *varname, *tabvarname;
17895 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017896
17897 rettv->vval.v_number = 0;
17898
17899 if (check_restricted() || check_secure())
17900 return;
17901
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017902#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017903 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017904#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017905 varname = get_tv_string_chk(&argvars[1]);
17906 varp = &argvars[2];
17907
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017908 if (varname != NULL && varp != NULL
17909#ifdef FEAT_WINDOWS
17910 && tp != NULL
17911#endif
17912 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017913 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017914#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017915 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017916 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017917#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017918
17919 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17920 if (tabvarname != NULL)
17921 {
17922 STRCPY(tabvarname, "t:");
17923 STRCPY(tabvarname + 2, varname);
17924 set_var(tabvarname, varp, TRUE);
17925 vim_free(tabvarname);
17926 }
17927
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017928#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017929 /* Restore current tabpage */
17930 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017931 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017932#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017933 }
17934}
17935
17936/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017937 * "settabwinvar()" function
17938 */
17939 static void
17940f_settabwinvar(argvars, rettv)
17941 typval_T *argvars;
17942 typval_T *rettv;
17943{
17944 setwinvar(argvars, rettv, 1);
17945}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946
17947/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017948 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017951f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017952 typval_T *argvars;
17953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017955 setwinvar(argvars, rettv, 0);
17956}
17957
17958/*
17959 * "setwinvar()" and "settabwinvar()" functions
17960 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017961
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017962 static void
17963setwinvar(argvars, rettv, off)
17964 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017965 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017966 int off;
17967{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968 win_T *win;
17969#ifdef FEAT_WINDOWS
17970 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017971 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017972 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973#endif
17974 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017975 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017977 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978
17979 if (check_restricted() || check_secure())
17980 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017981
17982#ifdef FEAT_WINDOWS
17983 if (off == 1)
17984 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17985 else
17986 tp = curtab;
17987#endif
17988 win = find_win_by_nr(&argvars[off], tp);
17989 varname = get_tv_string_chk(&argvars[off + 1]);
17990 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991
17992 if (win != NULL && varname != NULL && varp != NULL)
17993 {
17994#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017995 need_switch_win = !(tp == curtab && win == curwin);
17996 if (!need_switch_win
17997 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018000 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018002 long numval;
18003 char_u *strval;
18004 int error = FALSE;
18005
18006 ++varname;
18007 numval = get_tv_number_chk(varp, &error);
18008 strval = get_tv_string_buf_chk(varp, nbuf);
18009 if (!error && strval != NULL)
18010 set_option_value(varname, numval, strval, OPT_LOCAL);
18011 }
18012 else
18013 {
18014 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18015 if (winvarname != NULL)
18016 {
18017 STRCPY(winvarname, "w:");
18018 STRCPY(winvarname + 2, varname);
18019 set_var(winvarname, varp, TRUE);
18020 vim_free(winvarname);
18021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 }
18023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018025 if (need_switch_win)
18026 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027#endif
18028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029}
18030
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018031#ifdef FEAT_CRYPT
18032/*
18033 * "sha256({string})" function
18034 */
18035 static void
18036f_sha256(argvars, rettv)
18037 typval_T *argvars;
18038 typval_T *rettv;
18039{
18040 char_u *p;
18041
18042 p = get_tv_string(&argvars[0]);
18043 rettv->vval.v_string = vim_strsave(
18044 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18045 rettv->v_type = VAR_STRING;
18046}
18047#endif /* FEAT_CRYPT */
18048
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018050 * "shellescape({string})" function
18051 */
18052 static void
18053f_shellescape(argvars, rettv)
18054 typval_T *argvars;
18055 typval_T *rettv;
18056{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018057 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018058 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018059 rettv->v_type = VAR_STRING;
18060}
18061
18062/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018063 * shiftwidth() function
18064 */
18065 static void
18066f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020018067 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018068 typval_T *rettv;
18069{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018070 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018071}
18072
18073/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018074 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 */
18076 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000018077f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018078 typval_T *argvars;
18079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018081 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082
Bram Moolenaar0d660222005-01-07 21:51:51 +000018083 p = get_tv_string(&argvars[0]);
18084 rettv->vval.v_string = vim_strsave(p);
18085 simplify_filename(rettv->vval.v_string); /* simplify in place */
18086 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087}
18088
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018089#ifdef FEAT_FLOAT
18090/*
18091 * "sin()" function
18092 */
18093 static void
18094f_sin(argvars, rettv)
18095 typval_T *argvars;
18096 typval_T *rettv;
18097{
18098 float_T f;
18099
18100 rettv->v_type = VAR_FLOAT;
18101 if (get_float_arg(argvars, &f) == OK)
18102 rettv->vval.v_float = sin(f);
18103 else
18104 rettv->vval.v_float = 0.0;
18105}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018106
18107/*
18108 * "sinh()" function
18109 */
18110 static void
18111f_sinh(argvars, rettv)
18112 typval_T *argvars;
18113 typval_T *rettv;
18114{
18115 float_T f;
18116
18117 rettv->v_type = VAR_FLOAT;
18118 if (get_float_arg(argvars, &f) == OK)
18119 rettv->vval.v_float = sinh(f);
18120 else
18121 rettv->vval.v_float = 0.0;
18122}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018123#endif
18124
Bram Moolenaar0d660222005-01-07 21:51:51 +000018125static int
18126#ifdef __BORLANDC__
18127 _RTLENTRYF
18128#endif
18129 item_compare __ARGS((const void *s1, const void *s2));
18130static int
18131#ifdef __BORLANDC__
18132 _RTLENTRYF
18133#endif
18134 item_compare2 __ARGS((const void *s1, const void *s2));
18135
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018136/* struct used in the array that's given to qsort() */
18137typedef struct
18138{
18139 listitem_T *item;
18140 int idx;
18141} sortItem_T;
18142
Bram Moolenaar0d660222005-01-07 21:51:51 +000018143static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018144static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018145static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018146static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018147static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018148static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018149static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018150static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018151#define ITEM_COMPARE_FAIL 999
18152
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018154 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018156 static int
18157#ifdef __BORLANDC__
18158_RTLENTRYF
18159#endif
18160item_compare(s1, s2)
18161 const void *s1;
18162 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018164 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018165 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018166 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018167 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018168 int res;
18169 char_u numbuf1[NUMBUFLEN];
18170 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018172 si1 = (sortItem_T *)s1;
18173 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018174 tv1 = &si1->item->li_tv;
18175 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018176
18177 if (item_compare_numbers)
18178 {
18179 long v1 = get_tv_number(tv1);
18180 long v2 = get_tv_number(tv2);
18181
18182 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18183 }
18184
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018185 /* tv2string() puts quotes around a string and allocates memory. Don't do
18186 * that for string variables. Use a single quote when comparing with a
18187 * non-string to do what the docs promise. */
18188 if (tv1->v_type == VAR_STRING)
18189 {
18190 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18191 p1 = (char_u *)"'";
18192 else
18193 p1 = tv1->vval.v_string;
18194 }
18195 else
18196 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18197 if (tv2->v_type == VAR_STRING)
18198 {
18199 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18200 p2 = (char_u *)"'";
18201 else
18202 p2 = tv2->vval.v_string;
18203 }
18204 else
18205 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018206 if (p1 == NULL)
18207 p1 = (char_u *)"";
18208 if (p2 == NULL)
18209 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018210 if (!item_compare_numeric)
18211 {
18212 if (item_compare_ic)
18213 res = STRICMP(p1, p2);
18214 else
18215 res = STRCMP(p1, p2);
18216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018218 {
18219 double n1, n2;
18220 n1 = strtod((char *)p1, (char **)&p1);
18221 n2 = strtod((char *)p2, (char **)&p2);
18222 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18223 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018224
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018225 /* When the result would be zero, compare the item indexes. Makes the
18226 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018227 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018228 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018229
Bram Moolenaar0d660222005-01-07 21:51:51 +000018230 vim_free(tofree1);
18231 vim_free(tofree2);
18232 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233}
18234
18235 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018236#ifdef __BORLANDC__
18237_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018239item_compare2(s1, s2)
18240 const void *s1;
18241 const void *s2;
18242{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018243 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018244 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018245 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018246 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018247 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018249 /* shortcut after failure in previous call; compare all items equal */
18250 if (item_compare_func_err)
18251 return 0;
18252
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018253 si1 = (sortItem_T *)s1;
18254 si2 = (sortItem_T *)s2;
18255
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018256 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018257 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018258 copy_tv(&si1->item->li_tv, &argv[0]);
18259 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018260
18261 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018262 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018263 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18264 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018265 clear_tv(&argv[0]);
18266 clear_tv(&argv[1]);
18267
18268 if (res == FAIL)
18269 res = ITEM_COMPARE_FAIL;
18270 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018271 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18272 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018273 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018274 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018275
18276 /* When the result would be zero, compare the pointers themselves. Makes
18277 * the sort stable. */
18278 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018279 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018280
Bram Moolenaar0d660222005-01-07 21:51:51 +000018281 return res;
18282}
18283
18284/*
18285 * "sort({list})" function
18286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018288do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018289 typval_T *argvars;
18290 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018291 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292{
Bram Moolenaar33570922005-01-25 22:26:29 +000018293 list_T *l;
18294 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018295 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018296 long len;
18297 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298
Bram Moolenaar0d660222005-01-07 21:51:51 +000018299 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018300 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 else
18302 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018303 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018304 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018305 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18306 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018307 return;
18308 rettv->vval.v_list = l;
18309 rettv->v_type = VAR_LIST;
18310 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311
Bram Moolenaar0d660222005-01-07 21:51:51 +000018312 len = list_len(l);
18313 if (len <= 1)
18314 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315
Bram Moolenaar0d660222005-01-07 21:51:51 +000018316 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018317 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018318 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018319 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018320 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018321 if (argvars[1].v_type != VAR_UNKNOWN)
18322 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018323 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018324 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018325 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018326 else
18327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018328 int error = FALSE;
18329
18330 i = get_tv_number_chk(&argvars[1], &error);
18331 if (error)
18332 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018333 if (i == 1)
18334 item_compare_ic = TRUE;
18335 else
18336 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018337 if (item_compare_func != NULL)
18338 {
18339 if (STRCMP(item_compare_func, "n") == 0)
18340 {
18341 item_compare_func = NULL;
18342 item_compare_numeric = TRUE;
18343 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018344 else if (STRCMP(item_compare_func, "N") == 0)
18345 {
18346 item_compare_func = NULL;
18347 item_compare_numbers = TRUE;
18348 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018349 else if (STRCMP(item_compare_func, "i") == 0)
18350 {
18351 item_compare_func = NULL;
18352 item_compare_ic = TRUE;
18353 }
18354 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018355 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018356
18357 if (argvars[2].v_type != VAR_UNKNOWN)
18358 {
18359 /* optional third argument: {dict} */
18360 if (argvars[2].v_type != VAR_DICT)
18361 {
18362 EMSG(_(e_dictreq));
18363 return;
18364 }
18365 item_compare_selfdict = argvars[2].vval.v_dict;
18366 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368
Bram Moolenaar0d660222005-01-07 21:51:51 +000018369 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018370 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018371 if (ptrs == NULL)
18372 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373
Bram Moolenaar327aa022014-03-25 18:24:23 +010018374 i = 0;
18375 if (sort)
18376 {
18377 /* sort(): ptrs will be the list to sort */
18378 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018379 {
18380 ptrs[i].item = li;
18381 ptrs[i].idx = i;
18382 ++i;
18383 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018384
18385 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018386 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018387 /* test the compare function */
18388 if (item_compare_func != NULL
18389 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018390 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018391 EMSG(_("E702: Sort compare function failed"));
18392 else
18393 {
18394 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018395 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018396 item_compare_func == NULL ? item_compare : item_compare2);
18397
18398 if (!item_compare_func_err)
18399 {
18400 /* Clear the List and append the items in sorted order. */
18401 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18402 l->lv_len = 0;
18403 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018404 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018405 }
18406 }
18407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018409 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018410 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18411
18412 /* f_uniq(): ptrs will be a stack of items to remove */
18413 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018414 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018415 item_compare_func_ptr = item_compare_func
18416 ? item_compare2 : item_compare;
18417
18418 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18419 li = li->li_next)
18420 {
18421 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18422 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018423 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018424 if (item_compare_func_err)
18425 {
18426 EMSG(_("E882: Uniq compare function failed"));
18427 break;
18428 }
18429 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018431 if (!item_compare_func_err)
18432 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018433 while (--i >= 0)
18434 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018435 li = ptrs[i].item->li_next;
18436 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018437 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018438 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018439 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018440 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018441 list_fix_watch(l, li);
18442 listitem_free(li);
18443 l->lv_len--;
18444 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018445 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018446 }
18447
18448 vim_free(ptrs);
18449 }
18450}
18451
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018452/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018453 * "sort({list})" function
18454 */
18455 static void
18456f_sort(argvars, rettv)
18457 typval_T *argvars;
18458 typval_T *rettv;
18459{
18460 do_sort_uniq(argvars, rettv, TRUE);
18461}
18462
18463/*
18464 * "uniq({list})" function
18465 */
18466 static void
18467f_uniq(argvars, rettv)
18468 typval_T *argvars;
18469 typval_T *rettv;
18470{
18471 do_sort_uniq(argvars, rettv, FALSE);
18472}
18473
18474/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018475 * "soundfold({word})" function
18476 */
18477 static void
18478f_soundfold(argvars, rettv)
18479 typval_T *argvars;
18480 typval_T *rettv;
18481{
18482 char_u *s;
18483
18484 rettv->v_type = VAR_STRING;
18485 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018486#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018487 rettv->vval.v_string = eval_soundfold(s);
18488#else
18489 rettv->vval.v_string = vim_strsave(s);
18490#endif
18491}
18492
18493/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018494 * "spellbadword()" function
18495 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018496 static void
18497f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018498 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018499 typval_T *rettv;
18500{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018501 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018502 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018503 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018504
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018505 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018506 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018507
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018508#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018509 if (argvars[0].v_type == VAR_UNKNOWN)
18510 {
18511 /* Find the start and length of the badly spelled word. */
18512 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18513 if (len != 0)
18514 word = ml_get_cursor();
18515 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018516 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018517 {
18518 char_u *str = get_tv_string_chk(&argvars[0]);
18519 int capcol = -1;
18520
18521 if (str != NULL)
18522 {
18523 /* Check the argument for spelling. */
18524 while (*str != NUL)
18525 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018526 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018527 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018528 {
18529 word = str;
18530 break;
18531 }
18532 str += len;
18533 }
18534 }
18535 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018536#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018537
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018538 list_append_string(rettv->vval.v_list, word, len);
18539 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018540 attr == HLF_SPB ? "bad" :
18541 attr == HLF_SPR ? "rare" :
18542 attr == HLF_SPL ? "local" :
18543 attr == HLF_SPC ? "caps" :
18544 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018545}
18546
18547/*
18548 * "spellsuggest()" function
18549 */
18550 static void
18551f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018552 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018553 typval_T *rettv;
18554{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018555#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018556 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018557 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018558 int maxcount;
18559 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018560 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018561 listitem_T *li;
18562 int need_capital = FALSE;
18563#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018564
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018565 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018566 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018567
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018568#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018569 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018570 {
18571 str = get_tv_string(&argvars[0]);
18572 if (argvars[1].v_type != VAR_UNKNOWN)
18573 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018574 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018575 if (maxcount <= 0)
18576 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018577 if (argvars[2].v_type != VAR_UNKNOWN)
18578 {
18579 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18580 if (typeerr)
18581 return;
18582 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018583 }
18584 else
18585 maxcount = 25;
18586
Bram Moolenaar4770d092006-01-12 23:22:24 +000018587 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018588
18589 for (i = 0; i < ga.ga_len; ++i)
18590 {
18591 str = ((char_u **)ga.ga_data)[i];
18592
18593 li = listitem_alloc();
18594 if (li == NULL)
18595 vim_free(str);
18596 else
18597 {
18598 li->li_tv.v_type = VAR_STRING;
18599 li->li_tv.v_lock = 0;
18600 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018601 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018602 }
18603 }
18604 ga_clear(&ga);
18605 }
18606#endif
18607}
18608
Bram Moolenaar0d660222005-01-07 21:51:51 +000018609 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018610f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018611 typval_T *argvars;
18612 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018613{
18614 char_u *str;
18615 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018616 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018617 regmatch_T regmatch;
18618 char_u patbuf[NUMBUFLEN];
18619 char_u *save_cpo;
18620 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018621 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018622 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018623 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018624
18625 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18626 save_cpo = p_cpo;
18627 p_cpo = (char_u *)"";
18628
18629 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018630 if (argvars[1].v_type != VAR_UNKNOWN)
18631 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018632 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18633 if (pat == NULL)
18634 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018635 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018636 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018637 }
18638 if (pat == NULL || *pat == NUL)
18639 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018640
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018641 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018643 if (typeerr)
18644 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645
Bram Moolenaar0d660222005-01-07 21:51:51 +000018646 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18647 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018649 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018650 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018651 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018652 if (*str == NUL)
18653 match = FALSE; /* empty item at the end */
18654 else
18655 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018656 if (match)
18657 end = regmatch.startp[0];
18658 else
18659 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018660 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18661 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018662 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018663 if (list_append_string(rettv->vval.v_list, str,
18664 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018665 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018666 }
18667 if (!match)
18668 break;
18669 /* Advance to just after the match. */
18670 if (regmatch.endp[0] > str)
18671 col = 0;
18672 else
18673 {
18674 /* Don't get stuck at the same match. */
18675#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018676 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018677#else
18678 col = 1;
18679#endif
18680 }
18681 str = regmatch.endp[0];
18682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683
Bram Moolenaar473de612013-06-08 18:19:48 +020018684 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686
Bram Moolenaar0d660222005-01-07 21:51:51 +000018687 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688}
18689
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018690#ifdef FEAT_FLOAT
18691/*
18692 * "sqrt()" function
18693 */
18694 static void
18695f_sqrt(argvars, rettv)
18696 typval_T *argvars;
18697 typval_T *rettv;
18698{
18699 float_T f;
18700
18701 rettv->v_type = VAR_FLOAT;
18702 if (get_float_arg(argvars, &f) == OK)
18703 rettv->vval.v_float = sqrt(f);
18704 else
18705 rettv->vval.v_float = 0.0;
18706}
18707
18708/*
18709 * "str2float()" function
18710 */
18711 static void
18712f_str2float(argvars, rettv)
18713 typval_T *argvars;
18714 typval_T *rettv;
18715{
18716 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18717
18718 if (*p == '+')
18719 p = skipwhite(p + 1);
18720 (void)string2float(p, &rettv->vval.v_float);
18721 rettv->v_type = VAR_FLOAT;
18722}
18723#endif
18724
Bram Moolenaar2c932302006-03-18 21:42:09 +000018725/*
18726 * "str2nr()" function
18727 */
18728 static void
18729f_str2nr(argvars, rettv)
18730 typval_T *argvars;
18731 typval_T *rettv;
18732{
18733 int base = 10;
18734 char_u *p;
18735 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018736 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018737
18738 if (argvars[1].v_type != VAR_UNKNOWN)
18739 {
18740 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018741 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018742 {
18743 EMSG(_(e_invarg));
18744 return;
18745 }
18746 }
18747
18748 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018749 if (*p == '+')
18750 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018751 switch (base)
18752 {
18753 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18754 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18755 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18756 default: what = 0;
18757 }
18758 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018759 rettv->vval.v_number = n;
18760}
18761
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762#ifdef HAVE_STRFTIME
18763/*
18764 * "strftime({format}[, {time}])" function
18765 */
18766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018767f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018768 typval_T *argvars;
18769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770{
18771 char_u result_buf[256];
18772 struct tm *curtime;
18773 time_t seconds;
18774 char_u *p;
18775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018776 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018778 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018779 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780 seconds = time(NULL);
18781 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018782 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 curtime = localtime(&seconds);
18784 /* MSVC returns NULL for an invalid value of seconds. */
18785 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018786 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 else
18788 {
18789# ifdef FEAT_MBYTE
18790 vimconv_T conv;
18791 char_u *enc;
18792
18793 conv.vc_type = CONV_NONE;
18794 enc = enc_locale();
18795 convert_setup(&conv, p_enc, enc);
18796 if (conv.vc_type != CONV_NONE)
18797 p = string_convert(&conv, p, NULL);
18798# endif
18799 if (p != NULL)
18800 (void)strftime((char *)result_buf, sizeof(result_buf),
18801 (char *)p, curtime);
18802 else
18803 result_buf[0] = NUL;
18804
18805# ifdef FEAT_MBYTE
18806 if (conv.vc_type != CONV_NONE)
18807 vim_free(p);
18808 convert_setup(&conv, enc, p_enc);
18809 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018810 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811 else
18812# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018813 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814
18815# ifdef FEAT_MBYTE
18816 /* Release conversion descriptors */
18817 convert_setup(&conv, NULL, NULL);
18818 vim_free(enc);
18819# endif
18820 }
18821}
18822#endif
18823
18824/*
18825 * "stridx()" function
18826 */
18827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018828f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018829 typval_T *argvars;
18830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831{
18832 char_u buf[NUMBUFLEN];
18833 char_u *needle;
18834 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018835 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018837 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018839 needle = get_tv_string_chk(&argvars[1]);
18840 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018841 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018842 if (needle == NULL || haystack == NULL)
18843 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844
Bram Moolenaar33570922005-01-25 22:26:29 +000018845 if (argvars[2].v_type != VAR_UNKNOWN)
18846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018847 int error = FALSE;
18848
18849 start_idx = get_tv_number_chk(&argvars[2], &error);
18850 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018851 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018852 if (start_idx >= 0)
18853 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018854 }
18855
18856 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18857 if (pos != NULL)
18858 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859}
18860
18861/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018862 * "string()" function
18863 */
18864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018865f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018866 typval_T *argvars;
18867 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018868{
18869 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018870 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018872 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018873 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018874 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018875 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018876 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877}
18878
18879/*
18880 * "strlen()" function
18881 */
18882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018883f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018884 typval_T *argvars;
18885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018886{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018887 rettv->vval.v_number = (varnumber_T)(STRLEN(
18888 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889}
18890
18891/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018892 * "strchars()" function
18893 */
18894 static void
18895f_strchars(argvars, rettv)
18896 typval_T *argvars;
18897 typval_T *rettv;
18898{
18899 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018900 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018901#ifdef FEAT_MBYTE
18902 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018903 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018904#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018905
18906 if (argvars[1].v_type != VAR_UNKNOWN)
18907 skipcc = get_tv_number_chk(&argvars[1], NULL);
18908 if (skipcc < 0 || skipcc > 1)
18909 EMSG(_(e_invarg));
18910 else
18911 {
18912#ifdef FEAT_MBYTE
18913 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18914 while (*s != NUL)
18915 {
18916 func_mb_ptr2char_adv(&s);
18917 ++len;
18918 }
18919 rettv->vval.v_number = len;
18920#else
18921 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18922#endif
18923 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018924}
18925
18926/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018927 * "strdisplaywidth()" function
18928 */
18929 static void
18930f_strdisplaywidth(argvars, rettv)
18931 typval_T *argvars;
18932 typval_T *rettv;
18933{
18934 char_u *s = get_tv_string(&argvars[0]);
18935 int col = 0;
18936
18937 if (argvars[1].v_type != VAR_UNKNOWN)
18938 col = get_tv_number(&argvars[1]);
18939
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018940 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018941}
18942
18943/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018944 * "strwidth()" function
18945 */
18946 static void
18947f_strwidth(argvars, rettv)
18948 typval_T *argvars;
18949 typval_T *rettv;
18950{
18951 char_u *s = get_tv_string(&argvars[0]);
18952
18953 rettv->vval.v_number = (varnumber_T)(
18954#ifdef FEAT_MBYTE
18955 mb_string2cells(s, -1)
18956#else
18957 STRLEN(s)
18958#endif
18959 );
18960}
18961
18962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963 * "strpart()" function
18964 */
18965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018966f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018967 typval_T *argvars;
18968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969{
18970 char_u *p;
18971 int n;
18972 int len;
18973 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018974 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018976 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 slen = (int)STRLEN(p);
18978
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018979 n = get_tv_number_chk(&argvars[1], &error);
18980 if (error)
18981 len = 0;
18982 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018983 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 else
18985 len = slen - n; /* default len: all bytes that are available. */
18986
18987 /*
18988 * Only return the overlap between the specified part and the actual
18989 * string.
18990 */
18991 if (n < 0)
18992 {
18993 len += n;
18994 n = 0;
18995 }
18996 else if (n > slen)
18997 n = slen;
18998 if (len < 0)
18999 len = 0;
19000 else if (n + len > slen)
19001 len = slen - n;
19002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019003 rettv->v_type = VAR_STRING;
19004 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005}
19006
19007/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019008 * "strridx()" function
19009 */
19010 static void
19011f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019012 typval_T *argvars;
19013 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019014{
19015 char_u buf[NUMBUFLEN];
19016 char_u *needle;
19017 char_u *haystack;
19018 char_u *rest;
19019 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019020 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019022 needle = get_tv_string_chk(&argvars[1]);
19023 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019024
19025 rettv->vval.v_number = -1;
19026 if (needle == NULL || haystack == NULL)
19027 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019028
19029 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019030 if (argvars[2].v_type != VAR_UNKNOWN)
19031 {
19032 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019033 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019034 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019035 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019036 }
19037 else
19038 end_idx = haystack_len;
19039
Bram Moolenaar0d660222005-01-07 21:51:51 +000019040 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019041 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019042 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019043 lastmatch = haystack + end_idx;
19044 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019045 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019046 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019047 for (rest = haystack; *rest != '\0'; ++rest)
19048 {
19049 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019050 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019051 break;
19052 lastmatch = rest;
19053 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019054 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019055
19056 if (lastmatch == NULL)
19057 rettv->vval.v_number = -1;
19058 else
19059 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19060}
19061
19062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063 * "strtrans()" function
19064 */
19065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019066f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019067 typval_T *argvars;
19068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019070 rettv->v_type = VAR_STRING;
19071 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072}
19073
19074/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019075 * "submatch()" function
19076 */
19077 static void
19078f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019079 typval_T *argvars;
19080 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019081{
Bram Moolenaar41571762014-04-02 19:00:58 +020019082 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019083 int no;
19084 int retList = 0;
19085
19086 no = (int)get_tv_number_chk(&argvars[0], &error);
19087 if (error)
19088 return;
19089 error = FALSE;
19090 if (argvars[1].v_type != VAR_UNKNOWN)
19091 retList = get_tv_number_chk(&argvars[1], &error);
19092 if (error)
19093 return;
19094
19095 if (retList == 0)
19096 {
19097 rettv->v_type = VAR_STRING;
19098 rettv->vval.v_string = reg_submatch(no);
19099 }
19100 else
19101 {
19102 rettv->v_type = VAR_LIST;
19103 rettv->vval.v_list = reg_submatch_list(no);
19104 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019105}
19106
19107/*
19108 * "substitute()" function
19109 */
19110 static void
19111f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019112 typval_T *argvars;
19113 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019114{
19115 char_u patbuf[NUMBUFLEN];
19116 char_u subbuf[NUMBUFLEN];
19117 char_u flagsbuf[NUMBUFLEN];
19118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019119 char_u *str = get_tv_string_chk(&argvars[0]);
19120 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19121 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19122 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19123
Bram Moolenaar0d660222005-01-07 21:51:51 +000019124 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019125 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19126 rettv->vval.v_string = NULL;
19127 else
19128 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019129}
19130
19131/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019132 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019135f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019136 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138{
19139 int id = 0;
19140#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019141 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 long col;
19143 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019144 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019146 lnum = get_tv_lnum(argvars); /* -1 on type error */
19147 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19148 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019150 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019151 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019152 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153#endif
19154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019155 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156}
19157
19158/*
19159 * "synIDattr(id, what [, mode])" function
19160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019162f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019163 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165{
19166 char_u *p = NULL;
19167#ifdef FEAT_SYN_HL
19168 int id;
19169 char_u *what;
19170 char_u *mode;
19171 char_u modebuf[NUMBUFLEN];
19172 int modec;
19173
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019174 id = get_tv_number(&argvars[0]);
19175 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019176 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019178 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019180 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181 modec = 0; /* replace invalid with current */
19182 }
19183 else
19184 {
19185#ifdef FEAT_GUI
19186 if (gui.in_use)
19187 modec = 'g';
19188 else
19189#endif
19190 if (t_colors > 1)
19191 modec = 'c';
19192 else
19193 modec = 't';
19194 }
19195
19196
19197 switch (TOLOWER_ASC(what[0]))
19198 {
19199 case 'b':
19200 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19201 p = highlight_color(id, what, modec);
19202 else /* bold */
19203 p = highlight_has_attr(id, HL_BOLD, modec);
19204 break;
19205
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019206 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 p = highlight_color(id, what, modec);
19208 break;
19209
19210 case 'i':
19211 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19212 p = highlight_has_attr(id, HL_INVERSE, modec);
19213 else /* italic */
19214 p = highlight_has_attr(id, HL_ITALIC, modec);
19215 break;
19216
19217 case 'n': /* name */
19218 p = get_highlight_name(NULL, id - 1);
19219 break;
19220
19221 case 'r': /* reverse */
19222 p = highlight_has_attr(id, HL_INVERSE, modec);
19223 break;
19224
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019225 case 's':
19226 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19227 p = highlight_color(id, what, modec);
19228 else /* standout */
19229 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230 break;
19231
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019232 case 'u':
19233 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19234 /* underline */
19235 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19236 else
19237 /* undercurl */
19238 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239 break;
19240 }
19241
19242 if (p != NULL)
19243 p = vim_strsave(p);
19244#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019245 rettv->v_type = VAR_STRING;
19246 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247}
19248
19249/*
19250 * "synIDtrans(id)" function
19251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019253f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019254 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256{
19257 int id;
19258
19259#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019260 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261
19262 if (id > 0)
19263 id = syn_get_final_id(id);
19264 else
19265#endif
19266 id = 0;
19267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019268 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269}
19270
19271/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019272 * "synconcealed(lnum, col)" function
19273 */
19274 static void
19275f_synconcealed(argvars, rettv)
19276 typval_T *argvars UNUSED;
19277 typval_T *rettv;
19278{
19279#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19280 long lnum;
19281 long col;
19282 int syntax_flags = 0;
19283 int cchar;
19284 int matchid = 0;
19285 char_u str[NUMBUFLEN];
19286#endif
19287
19288 rettv->v_type = VAR_LIST;
19289 rettv->vval.v_list = NULL;
19290
19291#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19292 lnum = get_tv_lnum(argvars); /* -1 on type error */
19293 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19294
19295 vim_memset(str, NUL, sizeof(str));
19296
19297 if (rettv_list_alloc(rettv) != FAIL)
19298 {
19299 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19300 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19301 && curwin->w_p_cole > 0)
19302 {
19303 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19304 syntax_flags = get_syntax_info(&matchid);
19305
19306 /* get the conceal character */
19307 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19308 {
19309 cchar = syn_get_sub_char();
19310 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19311 cchar = lcs_conceal;
19312 if (cchar != NUL)
19313 {
19314# ifdef FEAT_MBYTE
19315 if (has_mbyte)
19316 (*mb_char2bytes)(cchar, str);
19317 else
19318# endif
19319 str[0] = cchar;
19320 }
19321 }
19322 }
19323
19324 list_append_number(rettv->vval.v_list,
19325 (syntax_flags & HL_CONCEAL) != 0);
19326 /* -1 to auto-determine strlen */
19327 list_append_string(rettv->vval.v_list, str, -1);
19328 list_append_number(rettv->vval.v_list, matchid);
19329 }
19330#endif
19331}
19332
19333/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019334 * "synstack(lnum, col)" function
19335 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019336 static void
19337f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019338 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019339 typval_T *rettv;
19340{
19341#ifdef FEAT_SYN_HL
19342 long lnum;
19343 long col;
19344 int i;
19345 int id;
19346#endif
19347
19348 rettv->v_type = VAR_LIST;
19349 rettv->vval.v_list = NULL;
19350
19351#ifdef FEAT_SYN_HL
19352 lnum = get_tv_lnum(argvars); /* -1 on type error */
19353 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19354
19355 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019356 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019357 && rettv_list_alloc(rettv) != FAIL)
19358 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019359 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019360 for (i = 0; ; ++i)
19361 {
19362 id = syn_get_stack_item(i);
19363 if (id < 0)
19364 break;
19365 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19366 break;
19367 }
19368 }
19369#endif
19370}
19371
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019373get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019374 typval_T *argvars;
19375 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019376 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019378 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019380 char_u *infile = NULL;
19381 char_u buf[NUMBUFLEN];
19382 int err = FALSE;
19383 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019384 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019385 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019387 rettv->v_type = VAR_STRING;
19388 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019389 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019390 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019391
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019392 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019393 {
19394 /*
19395 * Write the string to a temp file, to be used for input of the shell
19396 * command.
19397 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019398 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019399 {
19400 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019401 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019402 }
19403
19404 fd = mch_fopen((char *)infile, WRITEBIN);
19405 if (fd == NULL)
19406 {
19407 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019408 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019409 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019410 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019411 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019412 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19413 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019414 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019415 else
19416 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019417 size_t len;
19418
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019419 p = get_tv_string_buf_chk(&argvars[1], buf);
19420 if (p == NULL)
19421 {
19422 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019423 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019424 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019425 len = STRLEN(p);
19426 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019427 err = TRUE;
19428 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019429 if (fclose(fd) != 0)
19430 err = TRUE;
19431 if (err)
19432 {
19433 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019434 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019435 }
19436 }
19437
Bram Moolenaar52a72462014-08-29 15:53:52 +020019438 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19439 * echoes typeahead, that messes up the display. */
19440 if (!msg_silent)
19441 flags += SHELL_COOKED;
19442
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019443 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019444 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019445 int len;
19446 listitem_T *li;
19447 char_u *s = NULL;
19448 char_u *start;
19449 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019450 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451
Bram Moolenaar52a72462014-08-29 15:53:52 +020019452 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019453 if (res == NULL)
19454 goto errret;
19455
19456 list = list_alloc();
19457 if (list == NULL)
19458 goto errret;
19459
19460 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019462 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019463 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019464 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019465 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019466
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019467 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019468 if (s == NULL)
19469 goto errret;
19470
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019471 for (p = s; start < end; ++p, ++start)
19472 *p = *start == NUL ? NL : *start;
19473 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019474
19475 li = listitem_alloc();
19476 if (li == NULL)
19477 {
19478 vim_free(s);
19479 goto errret;
19480 }
19481 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019482 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019483 li->li_tv.vval.v_string = s;
19484 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019486
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019487 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019488 rettv->v_type = VAR_LIST;
19489 rettv->vval.v_list = list;
19490 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019492 else
19493 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019494 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019495#ifdef USE_CR
19496 /* translate <CR> into <NL> */
19497 if (res != NULL)
19498 {
19499 char_u *s;
19500
19501 for (s = res; *s; ++s)
19502 {
19503 if (*s == CAR)
19504 *s = NL;
19505 }
19506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507#else
19508# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019509 /* translate <CR><NL> into <NL> */
19510 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019512 char_u *s, *d;
19513
19514 d = res;
19515 for (s = res; *s; ++s)
19516 {
19517 if (s[0] == CAR && s[1] == NL)
19518 ++s;
19519 *d++ = *s;
19520 }
19521 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523# endif
19524#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019525 rettv->vval.v_string = res;
19526 res = NULL;
19527 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019528
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019529errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019530 if (infile != NULL)
19531 {
19532 mch_remove(infile);
19533 vim_free(infile);
19534 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019535 if (res != NULL)
19536 vim_free(res);
19537 if (list != NULL)
19538 list_free(list, TRUE);
19539}
19540
19541/*
19542 * "system()" function
19543 */
19544 static void
19545f_system(argvars, rettv)
19546 typval_T *argvars;
19547 typval_T *rettv;
19548{
19549 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19550}
19551
19552/*
19553 * "systemlist()" function
19554 */
19555 static void
19556f_systemlist(argvars, rettv)
19557 typval_T *argvars;
19558 typval_T *rettv;
19559{
19560 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561}
19562
19563/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019564 * "tabpagebuflist()" function
19565 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019566 static void
19567f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019568 typval_T *argvars UNUSED;
19569 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019570{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019571#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019572 tabpage_T *tp;
19573 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019574
19575 if (argvars[0].v_type == VAR_UNKNOWN)
19576 wp = firstwin;
19577 else
19578 {
19579 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19580 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019581 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019582 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019583 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019584 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019585 for (; wp != NULL; wp = wp->w_next)
19586 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019587 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019588 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019589 }
19590#endif
19591}
19592
19593
19594/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019595 * "tabpagenr()" function
19596 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019597 static void
19598f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019599 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019600 typval_T *rettv;
19601{
19602 int nr = 1;
19603#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019604 char_u *arg;
19605
19606 if (argvars[0].v_type != VAR_UNKNOWN)
19607 {
19608 arg = get_tv_string_chk(&argvars[0]);
19609 nr = 0;
19610 if (arg != NULL)
19611 {
19612 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019613 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019614 else
19615 EMSG2(_(e_invexpr2), arg);
19616 }
19617 }
19618 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019619 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019620#endif
19621 rettv->vval.v_number = nr;
19622}
19623
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019624
19625#ifdef FEAT_WINDOWS
19626static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19627
19628/*
19629 * Common code for tabpagewinnr() and winnr().
19630 */
19631 static int
19632get_winnr(tp, argvar)
19633 tabpage_T *tp;
19634 typval_T *argvar;
19635{
19636 win_T *twin;
19637 int nr = 1;
19638 win_T *wp;
19639 char_u *arg;
19640
19641 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19642 if (argvar->v_type != VAR_UNKNOWN)
19643 {
19644 arg = get_tv_string_chk(argvar);
19645 if (arg == NULL)
19646 nr = 0; /* type error; errmsg already given */
19647 else if (STRCMP(arg, "$") == 0)
19648 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19649 else if (STRCMP(arg, "#") == 0)
19650 {
19651 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19652 if (twin == NULL)
19653 nr = 0;
19654 }
19655 else
19656 {
19657 EMSG2(_(e_invexpr2), arg);
19658 nr = 0;
19659 }
19660 }
19661
19662 if (nr > 0)
19663 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19664 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019665 {
19666 if (wp == NULL)
19667 {
19668 /* didn't find it in this tabpage */
19669 nr = 0;
19670 break;
19671 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019672 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019673 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019674 return nr;
19675}
19676#endif
19677
19678/*
19679 * "tabpagewinnr()" function
19680 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019681 static void
19682f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019683 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019684 typval_T *rettv;
19685{
19686 int nr = 1;
19687#ifdef FEAT_WINDOWS
19688 tabpage_T *tp;
19689
19690 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19691 if (tp == NULL)
19692 nr = 0;
19693 else
19694 nr = get_winnr(tp, &argvars[1]);
19695#endif
19696 rettv->vval.v_number = nr;
19697}
19698
19699
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019700/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019701 * "tagfiles()" function
19702 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019703 static void
19704f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019705 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019706 typval_T *rettv;
19707{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019708 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019709 tagname_T tn;
19710 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019711
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019712 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019713 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019714 fname = alloc(MAXPATHL);
19715 if (fname == NULL)
19716 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019717
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019718 for (first = TRUE; ; first = FALSE)
19719 if (get_tagfname(&tn, first, fname) == FAIL
19720 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019721 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019722 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019723 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019724}
19725
19726/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019727 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019728 */
19729 static void
19730f_taglist(argvars, rettv)
19731 typval_T *argvars;
19732 typval_T *rettv;
19733{
19734 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019735
19736 tag_pattern = get_tv_string(&argvars[0]);
19737
19738 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019739 if (*tag_pattern == NUL)
19740 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019741
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019742 if (rettv_list_alloc(rettv) == OK)
19743 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019744}
19745
19746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 * "tempname()" function
19748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019750f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019751 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753{
19754 static int x = 'A';
19755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019756 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019757 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758
19759 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19760 * names. Skip 'I' and 'O', they are used for shell redirection. */
19761 do
19762 {
19763 if (x == 'Z')
19764 x = '0';
19765 else if (x == '9')
19766 x = 'A';
19767 else
19768 {
19769#ifdef EBCDIC
19770 if (x == 'I')
19771 x = 'J';
19772 else if (x == 'R')
19773 x = 'S';
19774 else
19775#endif
19776 ++x;
19777 }
19778 } while (x == 'I' || x == 'O');
19779}
19780
19781/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019782 * "test(list)" function: Just checking the walls...
19783 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019784 static void
19785f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019786 typval_T *argvars UNUSED;
19787 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019788{
19789 /* Used for unit testing. Change the code below to your liking. */
19790#if 0
19791 listitem_T *li;
19792 list_T *l;
19793 char_u *bad, *good;
19794
19795 if (argvars[0].v_type != VAR_LIST)
19796 return;
19797 l = argvars[0].vval.v_list;
19798 if (l == NULL)
19799 return;
19800 li = l->lv_first;
19801 if (li == NULL)
19802 return;
19803 bad = get_tv_string(&li->li_tv);
19804 li = li->li_next;
19805 if (li == NULL)
19806 return;
19807 good = get_tv_string(&li->li_tv);
19808 rettv->vval.v_number = test_edit_score(bad, good);
19809#endif
19810}
19811
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019812#ifdef FEAT_FLOAT
19813/*
19814 * "tan()" function
19815 */
19816 static void
19817f_tan(argvars, rettv)
19818 typval_T *argvars;
19819 typval_T *rettv;
19820{
19821 float_T f;
19822
19823 rettv->v_type = VAR_FLOAT;
19824 if (get_float_arg(argvars, &f) == OK)
19825 rettv->vval.v_float = tan(f);
19826 else
19827 rettv->vval.v_float = 0.0;
19828}
19829
19830/*
19831 * "tanh()" function
19832 */
19833 static void
19834f_tanh(argvars, rettv)
19835 typval_T *argvars;
19836 typval_T *rettv;
19837{
19838 float_T f;
19839
19840 rettv->v_type = VAR_FLOAT;
19841 if (get_float_arg(argvars, &f) == OK)
19842 rettv->vval.v_float = tanh(f);
19843 else
19844 rettv->vval.v_float = 0.0;
19845}
19846#endif
19847
Bram Moolenaard52d9742005-08-21 22:20:28 +000019848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 * "tolower(string)" function
19850 */
19851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019852f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019853 typval_T *argvars;
19854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855{
19856 char_u *p;
19857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019858 p = vim_strsave(get_tv_string(&argvars[0]));
19859 rettv->v_type = VAR_STRING;
19860 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861
19862 if (p != NULL)
19863 while (*p != NUL)
19864 {
19865#ifdef FEAT_MBYTE
19866 int l;
19867
19868 if (enc_utf8)
19869 {
19870 int c, lc;
19871
19872 c = utf_ptr2char(p);
19873 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019874 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 /* TODO: reallocate string when byte count changes. */
19876 if (utf_char2len(lc) == l)
19877 utf_char2bytes(lc, p);
19878 p += l;
19879 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019880 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 p += l; /* skip multi-byte character */
19882 else
19883#endif
19884 {
19885 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19886 ++p;
19887 }
19888 }
19889}
19890
19891/*
19892 * "toupper(string)" function
19893 */
19894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019895f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019896 typval_T *argvars;
19897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019899 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019900 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901}
19902
19903/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019904 * "tr(string, fromstr, tostr)" function
19905 */
19906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019907f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019908 typval_T *argvars;
19909 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019910{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019911 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019912 char_u *fromstr;
19913 char_u *tostr;
19914 char_u *p;
19915#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019916 int inlen;
19917 int fromlen;
19918 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019919 int idx;
19920 char_u *cpstr;
19921 int cplen;
19922 int first = TRUE;
19923#endif
19924 char_u buf[NUMBUFLEN];
19925 char_u buf2[NUMBUFLEN];
19926 garray_T ga;
19927
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019928 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019929 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19930 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019931
19932 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019933 rettv->v_type = VAR_STRING;
19934 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019935 if (fromstr == NULL || tostr == NULL)
19936 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019937 ga_init2(&ga, (int)sizeof(char), 80);
19938
19939#ifdef FEAT_MBYTE
19940 if (!has_mbyte)
19941#endif
19942 /* not multi-byte: fromstr and tostr must be the same length */
19943 if (STRLEN(fromstr) != STRLEN(tostr))
19944 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019945#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019946error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019947#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019948 EMSG2(_(e_invarg2), fromstr);
19949 ga_clear(&ga);
19950 return;
19951 }
19952
19953 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019954 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019955 {
19956#ifdef FEAT_MBYTE
19957 if (has_mbyte)
19958 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019959 inlen = (*mb_ptr2len)(in_str);
19960 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019961 cplen = inlen;
19962 idx = 0;
19963 for (p = fromstr; *p != NUL; p += fromlen)
19964 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019965 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019966 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019967 {
19968 for (p = tostr; *p != NUL; p += tolen)
19969 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019970 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019971 if (idx-- == 0)
19972 {
19973 cplen = tolen;
19974 cpstr = p;
19975 break;
19976 }
19977 }
19978 if (*p == NUL) /* tostr is shorter than fromstr */
19979 goto error;
19980 break;
19981 }
19982 ++idx;
19983 }
19984
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019985 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019986 {
19987 /* Check that fromstr and tostr have the same number of
19988 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019989 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019990 first = FALSE;
19991 for (p = tostr; *p != NUL; p += tolen)
19992 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019993 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019994 --idx;
19995 }
19996 if (idx != 0)
19997 goto error;
19998 }
19999
Bram Moolenaarcde88542015-08-11 19:14:00 +020020000 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020001 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020002 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020003
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020004 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020005 }
20006 else
20007#endif
20008 {
20009 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020010 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020011 if (p != NULL)
20012 ga_append(&ga, tostr[p - fromstr]);
20013 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020014 ga_append(&ga, *in_str);
20015 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020016 }
20017 }
20018
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020019 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020020 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020021 ga_append(&ga, NUL);
20022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020023 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020024}
20025
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020026#ifdef FEAT_FLOAT
20027/*
20028 * "trunc({float})" function
20029 */
20030 static void
20031f_trunc(argvars, rettv)
20032 typval_T *argvars;
20033 typval_T *rettv;
20034{
20035 float_T f;
20036
20037 rettv->v_type = VAR_FLOAT;
20038 if (get_float_arg(argvars, &f) == OK)
20039 /* trunc() is not in C90, use floor() or ceil() instead. */
20040 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20041 else
20042 rettv->vval.v_float = 0.0;
20043}
20044#endif
20045
Bram Moolenaar8299df92004-07-10 09:47:34 +000020046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 * "type(expr)" function
20048 */
20049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020050f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020051 typval_T *argvars;
20052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020054 int n;
20055
20056 switch (argvars[0].v_type)
20057 {
20058 case VAR_NUMBER: n = 0; break;
20059 case VAR_STRING: n = 1; break;
20060 case VAR_FUNC: n = 2; break;
20061 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020062 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020063#ifdef FEAT_FLOAT
20064 case VAR_FLOAT: n = 5; break;
20065#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020066 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
20067 }
20068 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069}
20070
20071/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020072 * "undofile(name)" function
20073 */
20074 static void
20075f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010020076 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020077 typval_T *rettv;
20078{
20079 rettv->v_type = VAR_STRING;
20080#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020081 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020082 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020083
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020084 if (*fname == NUL)
20085 {
20086 /* If there is no file name there will be no undo file. */
20087 rettv->vval.v_string = NULL;
20088 }
20089 else
20090 {
20091 char_u *ffname = FullName_save(fname, FALSE);
20092
20093 if (ffname != NULL)
20094 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20095 vim_free(ffname);
20096 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020097 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020098#else
20099 rettv->vval.v_string = NULL;
20100#endif
20101}
20102
20103/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020104 * "undotree()" function
20105 */
20106 static void
20107f_undotree(argvars, rettv)
20108 typval_T *argvars UNUSED;
20109 typval_T *rettv;
20110{
20111 if (rettv_dict_alloc(rettv) == OK)
20112 {
20113 dict_T *dict = rettv->vval.v_dict;
20114 list_T *list;
20115
Bram Moolenaar730cde92010-06-27 05:18:54 +020020116 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020117 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020118 dict_add_nr_str(dict, "save_last",
20119 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020120 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20121 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020122 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020123
20124 list = list_alloc();
20125 if (list != NULL)
20126 {
20127 u_eval_tree(curbuf->b_u_oldhead, list);
20128 dict_add_list(dict, "entries", list);
20129 }
20130 }
20131}
20132
20133/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020134 * "values(dict)" function
20135 */
20136 static void
20137f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020138 typval_T *argvars;
20139 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020140{
20141 dict_list(argvars, rettv, 1);
20142}
20143
20144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145 * "virtcol(string)" function
20146 */
20147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020148f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020149 typval_T *argvars;
20150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151{
20152 colnr_T vcol = 0;
20153 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020154 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020156 fp = var2fpos(&argvars[0], FALSE, &fnum);
20157 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20158 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 {
20160 getvvcol(curwin, fp, NULL, NULL, &vcol);
20161 ++vcol;
20162 }
20163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020164 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165}
20166
20167/*
20168 * "visualmode()" function
20169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020171f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020172 typval_T *argvars UNUSED;
20173 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175 char_u str[2];
20176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020177 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 str[0] = curbuf->b_visual_mode_eval;
20179 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020180 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181
20182 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020183 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185}
20186
20187/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020188 * "wildmenumode()" function
20189 */
20190 static void
20191f_wildmenumode(argvars, rettv)
20192 typval_T *argvars UNUSED;
20193 typval_T *rettv UNUSED;
20194{
20195#ifdef FEAT_WILDMENU
20196 if (wild_menu_showing)
20197 rettv->vval.v_number = 1;
20198#endif
20199}
20200
20201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202 * "winbufnr(nr)" function
20203 */
20204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020205f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020206 typval_T *argvars;
20207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208{
20209 win_T *wp;
20210
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020211 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020213 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020215 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216}
20217
20218/*
20219 * "wincol()" function
20220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020222f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020223 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225{
20226 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020227 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228}
20229
20230/*
20231 * "winheight(nr)" function
20232 */
20233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020234f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020235 typval_T *argvars;
20236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237{
20238 win_T *wp;
20239
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020240 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020242 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020244 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245}
20246
20247/*
20248 * "winline()" function
20249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020251f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020252 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254{
20255 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020256 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257}
20258
20259/*
20260 * "winnr()" function
20261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020263f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020264 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266{
20267 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020268
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020270 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020272 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273}
20274
20275/*
20276 * "winrestcmd()" function
20277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020279f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020280 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282{
20283#ifdef FEAT_WINDOWS
20284 win_T *wp;
20285 int winnr = 1;
20286 garray_T ga;
20287 char_u buf[50];
20288
20289 ga_init2(&ga, (int)sizeof(char), 70);
20290 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20291 {
20292 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20293 ga_concat(&ga, buf);
20294# ifdef FEAT_VERTSPLIT
20295 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20296 ga_concat(&ga, buf);
20297# endif
20298 ++winnr;
20299 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020300 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020302 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020304 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020306 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307}
20308
20309/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020310 * "winrestview()" function
20311 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020312 static void
20313f_winrestview(argvars, rettv)
20314 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020315 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020316{
20317 dict_T *dict;
20318
20319 if (argvars[0].v_type != VAR_DICT
20320 || (dict = argvars[0].vval.v_dict) == NULL)
20321 EMSG(_(e_invarg));
20322 else
20323 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020324 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20325 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20326 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20327 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020328#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020329 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20330 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020331#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020332 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20333 {
20334 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20335 curwin->w_set_curswant = FALSE;
20336 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020337
Bram Moolenaar82c25852014-05-28 16:47:16 +020020338 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20339 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020340#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020341 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20342 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020343#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020344 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20345 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20346 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20347 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020348
20349 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020350 win_new_height(curwin, curwin->w_height);
20351# ifdef FEAT_VERTSPLIT
20352 win_new_width(curwin, W_WIDTH(curwin));
20353# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020354 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020355
Bram Moolenaarb851a962014-10-31 15:45:52 +010020356 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020357 curwin->w_topline = 1;
20358 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20359 curwin->w_topline = curbuf->b_ml.ml_line_count;
20360#ifdef FEAT_DIFF
20361 check_topfill(curwin, TRUE);
20362#endif
20363 }
20364}
20365
20366/*
20367 * "winsaveview()" function
20368 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020369 static void
20370f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020371 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020372 typval_T *rettv;
20373{
20374 dict_T *dict;
20375
Bram Moolenaara800b422010-06-27 01:15:55 +020020376 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020377 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020378 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020379
20380 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20381 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20382#ifdef FEAT_VIRTUALEDIT
20383 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20384#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020385 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020386 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20387
20388 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20389#ifdef FEAT_DIFF
20390 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20391#endif
20392 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20393 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20394}
20395
20396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397 * "winwidth(nr)" function
20398 */
20399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020400f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020401 typval_T *argvars;
20402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403{
20404 win_T *wp;
20405
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020406 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020408 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409 else
20410#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020411 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020413 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414#endif
20415}
20416
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020418 * "wordcount()" function
20419 */
20420 static void
20421f_wordcount(argvars, rettv)
20422 typval_T *argvars UNUSED;
20423 typval_T *rettv;
20424{
20425 if (rettv_dict_alloc(rettv) == FAIL)
20426 return;
20427 cursor_pos_info(rettv->vval.v_dict);
20428}
20429
20430/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020431 * Write list of strings to file
20432 */
20433 static int
20434write_list(fd, list, binary)
20435 FILE *fd;
20436 list_T *list;
20437 int binary;
20438{
20439 listitem_T *li;
20440 int c;
20441 int ret = OK;
20442 char_u *s;
20443
20444 for (li = list->lv_first; li != NULL; li = li->li_next)
20445 {
20446 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20447 {
20448 if (*s == '\n')
20449 c = putc(NUL, fd);
20450 else
20451 c = putc(*s, fd);
20452 if (c == EOF)
20453 {
20454 ret = FAIL;
20455 break;
20456 }
20457 }
20458 if (!binary || li->li_next != NULL)
20459 if (putc('\n', fd) == EOF)
20460 {
20461 ret = FAIL;
20462 break;
20463 }
20464 if (ret == FAIL)
20465 {
20466 EMSG(_(e_write));
20467 break;
20468 }
20469 }
20470 return ret;
20471}
20472
20473/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020474 * "writefile()" function
20475 */
20476 static void
20477f_writefile(argvars, rettv)
20478 typval_T *argvars;
20479 typval_T *rettv;
20480{
20481 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020482 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020483 char_u *fname;
20484 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020485 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020486
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020487 if (check_restricted() || check_secure())
20488 return;
20489
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020490 if (argvars[0].v_type != VAR_LIST)
20491 {
20492 EMSG2(_(e_listarg), "writefile()");
20493 return;
20494 }
20495 if (argvars[0].vval.v_list == NULL)
20496 return;
20497
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020498 if (argvars[2].v_type != VAR_UNKNOWN)
20499 {
20500 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20501 binary = TRUE;
20502 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20503 append = TRUE;
20504 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020505
20506 /* Always open the file in binary mode, library functions have a mind of
20507 * their own about CR-LF conversion. */
20508 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020509 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20510 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020511 {
20512 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20513 ret = -1;
20514 }
20515 else
20516 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020517 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20518 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020519 fclose(fd);
20520 }
20521
20522 rettv->vval.v_number = ret;
20523}
20524
20525/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020526 * "xor(expr, expr)" function
20527 */
20528 static void
20529f_xor(argvars, rettv)
20530 typval_T *argvars;
20531 typval_T *rettv;
20532{
20533 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20534 ^ get_tv_number_chk(&argvars[1], NULL);
20535}
20536
20537
20538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020540 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541 */
20542 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020543var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020544 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020545 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020546 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020548 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020550 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551
Bram Moolenaara5525202006-03-02 22:52:09 +000020552 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020553 if (varp->v_type == VAR_LIST)
20554 {
20555 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020556 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020557 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020558 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020559
20560 l = varp->vval.v_list;
20561 if (l == NULL)
20562 return NULL;
20563
20564 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020565 pos.lnum = list_find_nr(l, 0L, &error);
20566 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020567 return NULL; /* invalid line number */
20568
20569 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020570 pos.col = list_find_nr(l, 1L, &error);
20571 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020572 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020573 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020574
20575 /* We accept "$" for the column number: last column. */
20576 li = list_find(l, 1L);
20577 if (li != NULL && li->li_tv.v_type == VAR_STRING
20578 && li->li_tv.vval.v_string != NULL
20579 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20580 pos.col = len + 1;
20581
Bram Moolenaara5525202006-03-02 22:52:09 +000020582 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020583 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020584 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020585 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020586
Bram Moolenaara5525202006-03-02 22:52:09 +000020587#ifdef FEAT_VIRTUALEDIT
20588 /* Get the virtual offset. Defaults to zero. */
20589 pos.coladd = list_find_nr(l, 2L, &error);
20590 if (error)
20591 pos.coladd = 0;
20592#endif
20593
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020594 return &pos;
20595 }
20596
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020597 name = get_tv_string_chk(varp);
20598 if (name == NULL)
20599 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020600 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020602 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20603 {
20604 if (VIsual_active)
20605 return &VIsual;
20606 return &curwin->w_cursor;
20607 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020608 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020610 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20612 return NULL;
20613 return pp;
20614 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020615
20616#ifdef FEAT_VIRTUALEDIT
20617 pos.coladd = 0;
20618#endif
20619
Bram Moolenaar477933c2007-07-17 14:32:23 +000020620 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020621 {
20622 pos.col = 0;
20623 if (name[1] == '0') /* "w0": first visible line */
20624 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020625 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020626 pos.lnum = curwin->w_topline;
20627 return &pos;
20628 }
20629 else if (name[1] == '$') /* "w$": last visible line */
20630 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020631 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020632 pos.lnum = curwin->w_botline - 1;
20633 return &pos;
20634 }
20635 }
20636 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020638 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639 {
20640 pos.lnum = curbuf->b_ml.ml_line_count;
20641 pos.col = 0;
20642 }
20643 else
20644 {
20645 pos.lnum = curwin->w_cursor.lnum;
20646 pos.col = (colnr_T)STRLEN(ml_get_curline());
20647 }
20648 return &pos;
20649 }
20650 return NULL;
20651}
20652
20653/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020654 * Convert list in "arg" into a position and optional file number.
20655 * When "fnump" is NULL there is no file number, only 3 items.
20656 * Note that the column is passed on as-is, the caller may want to decrement
20657 * it to use 1 for the first column.
20658 * Return FAIL when conversion is not possible, doesn't check the position for
20659 * validity.
20660 */
20661 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020662list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020663 typval_T *arg;
20664 pos_T *posp;
20665 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020666 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020667{
20668 list_T *l = arg->vval.v_list;
20669 long i = 0;
20670 long n;
20671
Bram Moolenaar493c1782014-05-28 14:34:46 +020020672 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20673 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020674 if (arg->v_type != VAR_LIST
20675 || l == NULL
20676 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020677 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020678 return FAIL;
20679
20680 if (fnump != NULL)
20681 {
20682 n = list_find_nr(l, i++, NULL); /* fnum */
20683 if (n < 0)
20684 return FAIL;
20685 if (n == 0)
20686 n = curbuf->b_fnum; /* current buffer */
20687 *fnump = n;
20688 }
20689
20690 n = list_find_nr(l, i++, NULL); /* lnum */
20691 if (n < 0)
20692 return FAIL;
20693 posp->lnum = n;
20694
20695 n = list_find_nr(l, i++, NULL); /* col */
20696 if (n < 0)
20697 return FAIL;
20698 posp->col = n;
20699
20700#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020701 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020702 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020703 posp->coladd = 0;
20704 else
20705 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020706#endif
20707
Bram Moolenaar493c1782014-05-28 14:34:46 +020020708 if (curswantp != NULL)
20709 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20710
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020711 return OK;
20712}
20713
20714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 * Get the length of an environment variable name.
20716 * Advance "arg" to the first character after the name.
20717 * Return 0 for error.
20718 */
20719 static int
20720get_env_len(arg)
20721 char_u **arg;
20722{
20723 char_u *p;
20724 int len;
20725
20726 for (p = *arg; vim_isIDc(*p); ++p)
20727 ;
20728 if (p == *arg) /* no name found */
20729 return 0;
20730
20731 len = (int)(p - *arg);
20732 *arg = p;
20733 return len;
20734}
20735
20736/*
20737 * Get the length of the name of a function or internal variable.
20738 * "arg" is advanced to the first non-white character after the name.
20739 * Return 0 if something is wrong.
20740 */
20741 static int
20742get_id_len(arg)
20743 char_u **arg;
20744{
20745 char_u *p;
20746 int len;
20747
20748 /* Find the end of the name. */
20749 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020750 {
20751 if (*p == ':')
20752 {
20753 /* "s:" is start of "s:var", but "n:" is not and can be used in
20754 * slice "[n:]". Also "xx:" is not a namespace. */
20755 len = (int)(p - *arg);
20756 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20757 || len > 1)
20758 break;
20759 }
20760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 if (p == *arg) /* no name found */
20762 return 0;
20763
20764 len = (int)(p - *arg);
20765 *arg = skipwhite(p);
20766
20767 return len;
20768}
20769
20770/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020771 * Get the length of the name of a variable or function.
20772 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020774 * Return -1 if curly braces expansion failed.
20775 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 * If the name contains 'magic' {}'s, expand them and return the
20777 * expanded name in an allocated string via 'alias' - caller must free.
20778 */
20779 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020780get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781 char_u **arg;
20782 char_u **alias;
20783 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020784 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785{
20786 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 char_u *p;
20788 char_u *expr_start;
20789 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790
20791 *alias = NULL; /* default to no alias */
20792
20793 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20794 && (*arg)[2] == (int)KE_SNR)
20795 {
20796 /* hard coded <SNR>, already translated */
20797 *arg += 3;
20798 return get_id_len(arg) + 3;
20799 }
20800 len = eval_fname_script(*arg);
20801 if (len > 0)
20802 {
20803 /* literal "<SID>", "s:" or "<SNR>" */
20804 *arg += len;
20805 }
20806
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020808 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020810 p = find_name_end(*arg, &expr_start, &expr_end,
20811 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812 if (expr_start != NULL)
20813 {
20814 char_u *temp_string;
20815
20816 if (!evaluate)
20817 {
20818 len += (int)(p - *arg);
20819 *arg = skipwhite(p);
20820 return len;
20821 }
20822
20823 /*
20824 * Include any <SID> etc in the expanded string:
20825 * Thus the -len here.
20826 */
20827 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20828 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020829 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830 *alias = temp_string;
20831 *arg = skipwhite(p);
20832 return (int)STRLEN(temp_string);
20833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834
20835 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020836 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837 EMSG2(_(e_invexpr2), *arg);
20838
20839 return len;
20840}
20841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020842/*
20843 * Find the end of a variable or function name, taking care of magic braces.
20844 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20845 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020846 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020847 * Return a pointer to just after the name. Equal to "arg" if there is no
20848 * valid name.
20849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020851find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852 char_u *arg;
20853 char_u **expr_start;
20854 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020855 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020857 int mb_nest = 0;
20858 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020860 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020862 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020864 *expr_start = NULL;
20865 *expr_end = NULL;
20866 }
20867
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020868 /* Quick check for valid starting character. */
20869 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20870 return arg;
20871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020872 for (p = arg; *p != NUL
20873 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020874 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020875 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020876 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020877 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020878 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020879 if (*p == '\'')
20880 {
20881 /* skip over 'string' to avoid counting [ and ] inside it. */
20882 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20883 ;
20884 if (*p == NUL)
20885 break;
20886 }
20887 else if (*p == '"')
20888 {
20889 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20890 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20891 if (*p == '\\' && p[1] != NUL)
20892 ++p;
20893 if (*p == NUL)
20894 break;
20895 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020896 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20897 {
20898 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020899 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020900 len = (int)(p - arg);
20901 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020902 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020903 break;
20904 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020906 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020908 if (*p == '[')
20909 ++br_nest;
20910 else if (*p == ']')
20911 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020914 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020916 if (*p == '{')
20917 {
20918 mb_nest++;
20919 if (expr_start != NULL && *expr_start == NULL)
20920 *expr_start = p;
20921 }
20922 else if (*p == '}')
20923 {
20924 mb_nest--;
20925 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20926 *expr_end = p;
20927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929 }
20930
20931 return p;
20932}
20933
20934/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020935 * Expands out the 'magic' {}'s in a variable/function name.
20936 * Note that this can call itself recursively, to deal with
20937 * constructs like foo{bar}{baz}{bam}
20938 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20939 * "in_start" ^
20940 * "expr_start" ^
20941 * "expr_end" ^
20942 * "in_end" ^
20943 *
20944 * Returns a new allocated string, which the caller must free.
20945 * Returns NULL for failure.
20946 */
20947 static char_u *
20948make_expanded_name(in_start, expr_start, expr_end, in_end)
20949 char_u *in_start;
20950 char_u *expr_start;
20951 char_u *expr_end;
20952 char_u *in_end;
20953{
20954 char_u c1;
20955 char_u *retval = NULL;
20956 char_u *temp_result;
20957 char_u *nextcmd = NULL;
20958
20959 if (expr_end == NULL || in_end == NULL)
20960 return NULL;
20961 *expr_start = NUL;
20962 *expr_end = NUL;
20963 c1 = *in_end;
20964 *in_end = NUL;
20965
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020966 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020967 if (temp_result != NULL && nextcmd == NULL)
20968 {
20969 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20970 + (in_end - expr_end) + 1));
20971 if (retval != NULL)
20972 {
20973 STRCPY(retval, in_start);
20974 STRCAT(retval, temp_result);
20975 STRCAT(retval, expr_end + 1);
20976 }
20977 }
20978 vim_free(temp_result);
20979
20980 *in_end = c1; /* put char back for error messages */
20981 *expr_start = '{';
20982 *expr_end = '}';
20983
20984 if (retval != NULL)
20985 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020986 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020987 if (expr_start != NULL)
20988 {
20989 /* Further expansion! */
20990 temp_result = make_expanded_name(retval, expr_start,
20991 expr_end, temp_result);
20992 vim_free(retval);
20993 retval = temp_result;
20994 }
20995 }
20996
20997 return retval;
20998}
20999
21000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021002 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003 */
21004 static int
21005eval_isnamec(c)
21006 int c;
21007{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021008 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21009}
21010
21011/*
21012 * Return TRUE if character "c" can be used as the first character in a
21013 * variable or function name (excluding '{' and '}').
21014 */
21015 static int
21016eval_isnamec1(c)
21017 int c;
21018{
21019 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020}
21021
21022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023 * Set number v: variable to "val".
21024 */
21025 void
21026set_vim_var_nr(idx, val)
21027 int idx;
21028 long val;
21029{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021030 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031}
21032
21033/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021034 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 */
21036 long
21037get_vim_var_nr(idx)
21038 int idx;
21039{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021040 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041}
21042
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021043/*
21044 * Get string v: variable value. Uses a static buffer, can only be used once.
21045 */
21046 char_u *
21047get_vim_var_str(idx)
21048 int idx;
21049{
21050 return get_tv_string(&vimvars[idx].vv_tv);
21051}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021052
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021054 * Get List v: variable value. Caller must take care of reference count when
21055 * needed.
21056 */
21057 list_T *
21058get_vim_var_list(idx)
21059 int idx;
21060{
21061 return vimvars[idx].vv_list;
21062}
21063
21064/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021065 * Set v:char to character "c".
21066 */
21067 void
21068set_vim_var_char(c)
21069 int c;
21070{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021071 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021072
21073#ifdef FEAT_MBYTE
21074 if (has_mbyte)
21075 buf[(*mb_char2bytes)(c, buf)] = NUL;
21076 else
21077#endif
21078 {
21079 buf[0] = c;
21080 buf[1] = NUL;
21081 }
21082 set_vim_var_string(VV_CHAR, buf, -1);
21083}
21084
21085/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021086 * Set v:count to "count" and v:count1 to "count1".
21087 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 */
21089 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021090set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 long count;
21092 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021093 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021095 if (set_prevcount)
21096 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021097 vimvars[VV_COUNT].vv_nr = count;
21098 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099}
21100
21101/*
21102 * Set string v: variable to a copy of "val".
21103 */
21104 void
21105set_vim_var_string(idx, val, len)
21106 int idx;
21107 char_u *val;
21108 int len; /* length of "val" to use or -1 (whole string) */
21109{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021110 /* Need to do this (at least) once, since we can't initialize a union.
21111 * Will always be invoked when "v:progname" is set. */
21112 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
21113
Bram Moolenaare9a41262005-01-15 22:18:47 +000021114 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021116 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021118 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021120 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121}
21122
21123/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021124 * Set List v: variable to "val".
21125 */
21126 void
21127set_vim_var_list(idx, val)
21128 int idx;
21129 list_T *val;
21130{
21131 list_unref(vimvars[idx].vv_list);
21132 vimvars[idx].vv_list = val;
21133 if (val != NULL)
21134 ++val->lv_refcount;
21135}
21136
21137/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021138 * Set Dictionary v: variable to "val".
21139 */
21140 void
21141set_vim_var_dict(idx, val)
21142 int idx;
21143 dict_T *val;
21144{
21145 int todo;
21146 hashitem_T *hi;
21147
21148 dict_unref(vimvars[idx].vv_dict);
21149 vimvars[idx].vv_dict = val;
21150 if (val != NULL)
21151 {
21152 ++val->dv_refcount;
21153
21154 /* Set readonly */
21155 todo = (int)val->dv_hashtab.ht_used;
21156 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21157 {
21158 if (HASHITEM_EMPTY(hi))
21159 continue;
21160 --todo;
21161 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21162 }
21163 }
21164}
21165
21166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167 * Set v:register if needed.
21168 */
21169 void
21170set_reg_var(c)
21171 int c;
21172{
21173 char_u regname;
21174
21175 if (c == 0 || c == ' ')
21176 regname = '"';
21177 else
21178 regname = c;
21179 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021180 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 set_vim_var_string(VV_REG, &regname, 1);
21182}
21183
21184/*
21185 * Get or set v:exception. If "oldval" == NULL, return the current value.
21186 * Otherwise, restore the value to "oldval" and return NULL.
21187 * Must always be called in pairs to save and restore v:exception! Does not
21188 * take care of memory allocations.
21189 */
21190 char_u *
21191v_exception(oldval)
21192 char_u *oldval;
21193{
21194 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021195 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196
Bram Moolenaare9a41262005-01-15 22:18:47 +000021197 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 return NULL;
21199}
21200
21201/*
21202 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21203 * Otherwise, restore the value to "oldval" and return NULL.
21204 * Must always be called in pairs to save and restore v:throwpoint! Does not
21205 * take care of memory allocations.
21206 */
21207 char_u *
21208v_throwpoint(oldval)
21209 char_u *oldval;
21210{
21211 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021212 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213
Bram Moolenaare9a41262005-01-15 22:18:47 +000021214 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215 return NULL;
21216}
21217
21218#if defined(FEAT_AUTOCMD) || defined(PROTO)
21219/*
21220 * Set v:cmdarg.
21221 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21222 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21223 * Must always be called in pairs!
21224 */
21225 char_u *
21226set_cmdarg(eap, oldarg)
21227 exarg_T *eap;
21228 char_u *oldarg;
21229{
21230 char_u *oldval;
21231 char_u *newval;
21232 unsigned len;
21233
Bram Moolenaare9a41262005-01-15 22:18:47 +000021234 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021235 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021237 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021238 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021239 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240 }
21241
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021242 if (eap->force_bin == FORCE_BIN)
21243 len = 6;
21244 else if (eap->force_bin == FORCE_NOBIN)
21245 len = 8;
21246 else
21247 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021248
21249 if (eap->read_edit)
21250 len += 7;
21251
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021252 if (eap->force_ff != 0)
21253 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21254# ifdef FEAT_MBYTE
21255 if (eap->force_enc != 0)
21256 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021257 if (eap->bad_char != 0)
21258 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021259# endif
21260
21261 newval = alloc(len + 1);
21262 if (newval == NULL)
21263 return NULL;
21264
21265 if (eap->force_bin == FORCE_BIN)
21266 sprintf((char *)newval, " ++bin");
21267 else if (eap->force_bin == FORCE_NOBIN)
21268 sprintf((char *)newval, " ++nobin");
21269 else
21270 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021271
21272 if (eap->read_edit)
21273 STRCAT(newval, " ++edit");
21274
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021275 if (eap->force_ff != 0)
21276 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21277 eap->cmd + eap->force_ff);
21278# ifdef FEAT_MBYTE
21279 if (eap->force_enc != 0)
21280 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21281 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021282 if (eap->bad_char == BAD_KEEP)
21283 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21284 else if (eap->bad_char == BAD_DROP)
21285 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21286 else if (eap->bad_char != 0)
21287 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021288# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021289 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021290 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291}
21292#endif
21293
21294/*
21295 * Get the value of internal variable "name".
21296 * Return OK or FAIL.
21297 */
21298 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021299get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300 char_u *name;
21301 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021302 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021303 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021304 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021305 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306{
21307 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021308 typval_T *tv = NULL;
21309 typval_T atv;
21310 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312
21313 /* truncate the name, so that we can use strcmp() */
21314 cc = name[len];
21315 name[len] = NUL;
21316
21317 /*
21318 * Check for "b:changedtick".
21319 */
21320 if (STRCMP(name, "b:changedtick") == 0)
21321 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021322 atv.v_type = VAR_NUMBER;
21323 atv.vval.v_number = curbuf->b_changedtick;
21324 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325 }
21326
21327 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 * Check for user-defined variables.
21329 */
21330 else
21331 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021332 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021334 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021335 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021336 if (dip != NULL)
21337 *dip = v;
21338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339 }
21340
Bram Moolenaare9a41262005-01-15 22:18:47 +000021341 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021343 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021344 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 ret = FAIL;
21346 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021347 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021348 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349
21350 name[len] = cc;
21351
21352 return ret;
21353}
21354
21355/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021356 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21357 * Also handle function call with Funcref variable: func(expr)
21358 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21359 */
21360 static int
21361handle_subscript(arg, rettv, evaluate, verbose)
21362 char_u **arg;
21363 typval_T *rettv;
21364 int evaluate; /* do more than finding the end */
21365 int verbose; /* give error messages */
21366{
21367 int ret = OK;
21368 dict_T *selfdict = NULL;
21369 char_u *s;
21370 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021371 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021372
21373 while (ret == OK
21374 && (**arg == '['
21375 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021376 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021377 && !vim_iswhite(*(*arg - 1)))
21378 {
21379 if (**arg == '(')
21380 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021381 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021382 if (evaluate)
21383 {
21384 functv = *rettv;
21385 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021386
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021387 /* Invoke the function. Recursive! */
21388 s = functv.vval.v_string;
21389 }
21390 else
21391 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021392 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021393 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21394 &len, evaluate, selfdict);
21395
21396 /* Clear the funcref afterwards, so that deleting it while
21397 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021398 if (evaluate)
21399 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021400
21401 /* Stop the expression evaluation when immediately aborting on
21402 * error, or when an interrupt occurred or an exception was thrown
21403 * but not caught. */
21404 if (aborting())
21405 {
21406 if (ret == OK)
21407 clear_tv(rettv);
21408 ret = FAIL;
21409 }
21410 dict_unref(selfdict);
21411 selfdict = NULL;
21412 }
21413 else /* **arg == '[' || **arg == '.' */
21414 {
21415 dict_unref(selfdict);
21416 if (rettv->v_type == VAR_DICT)
21417 {
21418 selfdict = rettv->vval.v_dict;
21419 if (selfdict != NULL)
21420 ++selfdict->dv_refcount;
21421 }
21422 else
21423 selfdict = NULL;
21424 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21425 {
21426 clear_tv(rettv);
21427 ret = FAIL;
21428 }
21429 }
21430 }
21431 dict_unref(selfdict);
21432 return ret;
21433}
21434
21435/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021436 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021437 * value).
21438 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021439 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021440alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021441{
Bram Moolenaar33570922005-01-25 22:26:29 +000021442 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021443}
21444
21445/*
21446 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447 * The string "s" must have been allocated, it is consumed.
21448 * Return NULL for out of memory, the variable otherwise.
21449 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021450 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021451alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 char_u *s;
21453{
Bram Moolenaar33570922005-01-25 22:26:29 +000021454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021456 rettv = alloc_tv();
21457 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021459 rettv->v_type = VAR_STRING;
21460 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 }
21462 else
21463 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021464 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465}
21466
21467/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021468 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021470 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021471free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021472 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021473{
21474 if (varp != NULL)
21475 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021476 switch (varp->v_type)
21477 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021478 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021479 func_unref(varp->vval.v_string);
21480 /*FALLTHROUGH*/
21481 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021482 vim_free(varp->vval.v_string);
21483 break;
21484 case VAR_LIST:
21485 list_unref(varp->vval.v_list);
21486 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021487 case VAR_DICT:
21488 dict_unref(varp->vval.v_dict);
21489 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021490 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021491#ifdef FEAT_FLOAT
21492 case VAR_FLOAT:
21493#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021494 case VAR_UNKNOWN:
21495 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021496 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021497 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021498 break;
21499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500 vim_free(varp);
21501 }
21502}
21503
21504/*
21505 * Free the memory for a variable value and set the value to NULL or 0.
21506 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021507 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021508clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021509 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510{
21511 if (varp != NULL)
21512 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021513 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021515 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021516 func_unref(varp->vval.v_string);
21517 /*FALLTHROUGH*/
21518 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021519 vim_free(varp->vval.v_string);
21520 varp->vval.v_string = NULL;
21521 break;
21522 case VAR_LIST:
21523 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021524 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021525 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021526 case VAR_DICT:
21527 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021528 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021529 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021530 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021531 varp->vval.v_number = 0;
21532 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021533#ifdef FEAT_FLOAT
21534 case VAR_FLOAT:
21535 varp->vval.v_float = 0.0;
21536 break;
21537#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021538 case VAR_UNKNOWN:
21539 break;
21540 default:
21541 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021543 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 }
21545}
21546
21547/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021548 * Set the value of a variable to NULL without freeing items.
21549 */
21550 static void
21551init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021552 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021553{
21554 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021555 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021556}
21557
21558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559 * Get the number value of a variable.
21560 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021561 * For incompatible types, return 0.
21562 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21563 * caller of incompatible types: it sets *denote to TRUE if "denote"
21564 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565 */
21566 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021567get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021568 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021569{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021570 int error = FALSE;
21571
21572 return get_tv_number_chk(varp, &error); /* return 0L on error */
21573}
21574
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021575 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021576get_tv_number_chk(varp, denote)
21577 typval_T *varp;
21578 int *denote;
21579{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021580 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021582 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021584 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021585 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021586#ifdef FEAT_FLOAT
21587 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021588 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021589 break;
21590#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021591 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021592 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021593 break;
21594 case VAR_STRING:
21595 if (varp->vval.v_string != NULL)
21596 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021597 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021598 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021599 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021600 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021601 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021602 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021603 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021604 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021605 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021606 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021607 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021609 if (denote == NULL) /* useful for values that must be unsigned */
21610 n = -1;
21611 else
21612 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021613 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614}
21615
21616/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021617 * Get the lnum from the first argument.
21618 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021619 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 */
21621 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021622get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021623 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624{
Bram Moolenaar33570922005-01-25 22:26:29 +000021625 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 linenr_T lnum;
21627
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021628 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 if (lnum == 0) /* no valid number, try using line() */
21630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021631 rettv.v_type = VAR_NUMBER;
21632 f_line(argvars, &rettv);
21633 lnum = rettv.vval.v_number;
21634 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 }
21636 return lnum;
21637}
21638
21639/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021640 * Get the lnum from the first argument.
21641 * Also accepts "$", then "buf" is used.
21642 * Returns 0 on error.
21643 */
21644 static linenr_T
21645get_tv_lnum_buf(argvars, buf)
21646 typval_T *argvars;
21647 buf_T *buf;
21648{
21649 if (argvars[0].v_type == VAR_STRING
21650 && argvars[0].vval.v_string != NULL
21651 && argvars[0].vval.v_string[0] == '$'
21652 && buf != NULL)
21653 return buf->b_ml.ml_line_count;
21654 return get_tv_number_chk(&argvars[0], NULL);
21655}
21656
21657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658 * Get the string value of a variable.
21659 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021660 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21661 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 * If the String variable has never been set, return an empty string.
21663 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021664 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21665 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 */
21667 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021668get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021669 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670{
21671 static char_u mybuf[NUMBUFLEN];
21672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021673 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674}
21675
21676 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021677get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021678 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021679 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021681 char_u *res = get_tv_string_buf_chk(varp, buf);
21682
21683 return res != NULL ? res : (char_u *)"";
21684}
21685
Bram Moolenaar7d647822014-04-05 21:28:56 +020021686/*
21687 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21688 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021689 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021690get_tv_string_chk(varp)
21691 typval_T *varp;
21692{
21693 static char_u mybuf[NUMBUFLEN];
21694
21695 return get_tv_string_buf_chk(varp, mybuf);
21696}
21697
21698 static char_u *
21699get_tv_string_buf_chk(varp, buf)
21700 typval_T *varp;
21701 char_u *buf;
21702{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021703 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021705 case VAR_NUMBER:
21706 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21707 return buf;
21708 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021709 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021710 break;
21711 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021712 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021713 break;
21714 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021715 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021716 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021717#ifdef FEAT_FLOAT
21718 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021719 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021720 break;
21721#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021722 case VAR_STRING:
21723 if (varp->vval.v_string != NULL)
21724 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021725 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021726 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021727 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021728 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021730 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021731}
21732
21733/*
21734 * Find variable "name" in the list of variables.
21735 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021736 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021737 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021738 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021740 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021741find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021743 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021744 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021747 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748
Bram Moolenaara7043832005-01-21 11:56:39 +000021749 ht = find_var_ht(name, &varname);
21750 if (htp != NULL)
21751 *htp = ht;
21752 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021754 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755}
21756
21757/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021758 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021759 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021761 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021762find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021763 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021764 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021765 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021766 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021767{
Bram Moolenaar33570922005-01-25 22:26:29 +000021768 hashitem_T *hi;
21769
21770 if (*varname == NUL)
21771 {
21772 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021773 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021774 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021775 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021776 case 'g': return &globvars_var;
21777 case 'v': return &vimvars_var;
21778 case 'b': return &curbuf->b_bufvar;
21779 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021780#ifdef FEAT_WINDOWS
21781 case 't': return &curtab->tp_winvar;
21782#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021783 case 'l': return current_funccal == NULL
21784 ? NULL : &current_funccal->l_vars_var;
21785 case 'a': return current_funccal == NULL
21786 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021787 }
21788 return NULL;
21789 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021790
21791 hi = hash_find(ht, varname);
21792 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021793 {
21794 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021795 * worked find the variable again. Don't auto-load a script if it was
21796 * loaded already, otherwise it would be loaded every time when
21797 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021798 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021799 {
21800 /* Note: script_autoload() may make "hi" invalid. It must either
21801 * be obtained again or not used. */
21802 if (!script_autoload(varname, FALSE) || aborting())
21803 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021804 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021805 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021806 if (HASHITEM_EMPTY(hi))
21807 return NULL;
21808 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021809 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021810}
21811
21812/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021813 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021814 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021815 * Set "varname" to the start of name without ':'.
21816 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021817 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021818find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819 char_u *name;
21820 char_u **varname;
21821{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021822 hashitem_T *hi;
21823
Bram Moolenaar73627d02015-08-11 15:46:09 +020021824 if (name[0] == NUL)
21825 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826 if (name[1] != ':')
21827 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021828 /* The name must not start with a colon or #. */
21829 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830 return NULL;
21831 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021832
21833 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021834 hi = hash_find(&compat_hashtab, name);
21835 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021836 return &compat_hashtab;
21837
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021839 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021840 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841 }
21842 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021843 if (*name == 'g') /* global variable */
21844 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021845 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21846 */
21847 if (vim_strchr(name + 2, ':') != NULL
21848 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021849 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021851 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021853 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021854#ifdef FEAT_WINDOWS
21855 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021856 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021857#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021858 if (*name == 'v') /* v: variable */
21859 return &vimvarht;
21860 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021861 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021862 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021863 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 if (*name == 's' /* script variable */
21865 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21866 return &SCRIPT_VARS(current_SID);
21867 return NULL;
21868}
21869
21870/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021871 * Get function call environment based on bactrace debug level
21872 */
21873 static funccall_T *
21874get_funccal()
21875{
21876 int i;
21877 funccall_T *funccal;
21878 funccall_T *temp_funccal;
21879
21880 funccal = current_funccal;
21881 if (debug_backtrace_level > 0)
21882 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021883 for (i = 0; i < debug_backtrace_level; i++)
21884 {
21885 temp_funccal = funccal->caller;
21886 if (temp_funccal)
21887 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021888 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021889 /* backtrace level overflow. reset to max */
21890 debug_backtrace_level = i;
21891 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021892 }
21893 return funccal;
21894}
21895
21896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021898 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899 * Returns NULL when it doesn't exist.
21900 */
21901 char_u *
21902get_var_value(name)
21903 char_u *name;
21904{
Bram Moolenaar33570922005-01-25 22:26:29 +000021905 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021907 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908 if (v == NULL)
21909 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021910 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021911}
21912
21913/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021914 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915 * sourcing this script and when executing functions defined in the script.
21916 */
21917 void
21918new_script_vars(id)
21919 scid_T id;
21920{
Bram Moolenaara7043832005-01-21 11:56:39 +000021921 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021922 hashtab_T *ht;
21923 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021924
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21926 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021927 /* Re-allocating ga_data means that an ht_array pointing to
21928 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021929 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021930 for (i = 1; i <= ga_scripts.ga_len; ++i)
21931 {
21932 ht = &SCRIPT_VARS(i);
21933 if (ht->ht_mask == HT_INIT_SIZE - 1)
21934 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021935 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021936 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021937 }
21938
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939 while (ga_scripts.ga_len < id)
21940 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021941 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021942 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021943 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945 }
21946 }
21947}
21948
21949/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021950 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21951 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952 */
21953 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021954init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021955 dict_T *dict;
21956 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021957 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958{
Bram Moolenaar33570922005-01-25 22:26:29 +000021959 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021960 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021961 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021962 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021963 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021964 dict_var->di_tv.vval.v_dict = dict;
21965 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021966 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021967 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21968 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969}
21970
21971/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021972 * Unreference a dictionary initialized by init_var_dict().
21973 */
21974 void
21975unref_var_dict(dict)
21976 dict_T *dict;
21977{
21978 /* Now the dict needs to be freed if no one else is using it, go back to
21979 * normal reference counting. */
21980 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21981 dict_unref(dict);
21982}
21983
21984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021986 * Frees all allocated variables and the value they contain.
21987 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 */
21989 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021990vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021991 hashtab_T *ht;
21992{
21993 vars_clear_ext(ht, TRUE);
21994}
21995
21996/*
21997 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21998 */
21999 static void
22000vars_clear_ext(ht, free_val)
22001 hashtab_T *ht;
22002 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003{
Bram Moolenaara7043832005-01-21 11:56:39 +000022004 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022005 hashitem_T *hi;
22006 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007
Bram Moolenaar33570922005-01-25 22:26:29 +000022008 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022009 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022010 for (hi = ht->ht_array; todo > 0; ++hi)
22011 {
22012 if (!HASHITEM_EMPTY(hi))
22013 {
22014 --todo;
22015
Bram Moolenaar33570922005-01-25 22:26:29 +000022016 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022017 * ht_array might change then. hash_clear() takes care of it
22018 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022019 v = HI2DI(hi);
22020 if (free_val)
22021 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022022 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022023 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022024 }
22025 }
22026 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022027 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028}
22029
Bram Moolenaara7043832005-01-21 11:56:39 +000022030/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022031 * Delete a variable from hashtab "ht" at item "hi".
22032 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000022035delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000022036 hashtab_T *ht;
22037 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038{
Bram Moolenaar33570922005-01-25 22:26:29 +000022039 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022040
22041 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022042 clear_tv(&di->di_tv);
22043 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044}
22045
22046/*
22047 * List the value of one internal variable.
22048 */
22049 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022050list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000022051 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022053 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022055 char_u *tofree;
22056 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022057 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022058
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022059 current_copyID += COPYID_INC;
22060 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000022061 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022062 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022063 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064}
22065
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022067list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068 char_u *prefix;
22069 char_u *name;
22070 int type;
22071 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022072 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073{
Bram Moolenaar31859182007-08-14 20:41:13 +000022074 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22075 msg_start();
22076 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077 if (name != NULL) /* "a:" vars don't have a name stored */
22078 msg_puts(name);
22079 msg_putchar(' ');
22080 msg_advance(22);
22081 if (type == VAR_NUMBER)
22082 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022083 else if (type == VAR_FUNC)
22084 msg_putchar('*');
22085 else if (type == VAR_LIST)
22086 {
22087 msg_putchar('[');
22088 if (*string == '[')
22089 ++string;
22090 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022091 else if (type == VAR_DICT)
22092 {
22093 msg_putchar('{');
22094 if (*string == '{')
22095 ++string;
22096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 else
22098 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022099
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022101
22102 if (type == VAR_FUNC)
22103 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022104 if (*first)
22105 {
22106 msg_clr_eos();
22107 *first = FALSE;
22108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109}
22110
22111/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022112 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022113 * If the variable already exists, the value is updated.
22114 * Otherwise the variable is created.
22115 */
22116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022117set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022119 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022120 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022121{
Bram Moolenaar33570922005-01-25 22:26:29 +000022122 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022124 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022126 ht = find_var_ht(name, &varname);
22127 if (ht == NULL || *varname == NUL)
22128 {
22129 EMSG2(_(e_illvar), name);
22130 return;
22131 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022132 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022133
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022134 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22135 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022136
Bram Moolenaar33570922005-01-25 22:26:29 +000022137 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022139 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022140 if (var_check_ro(v->di_flags, name, FALSE)
22141 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022142 return;
22143 if (v->di_tv.v_type != tv->v_type
22144 && !((v->di_tv.v_type == VAR_STRING
22145 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022146 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022147 || tv->v_type == VAR_NUMBER))
22148#ifdef FEAT_FLOAT
22149 && !((v->di_tv.v_type == VAR_NUMBER
22150 || v->di_tv.v_type == VAR_FLOAT)
22151 && (tv->v_type == VAR_NUMBER
22152 || tv->v_type == VAR_FLOAT))
22153#endif
22154 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022155 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022156 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022157 return;
22158 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022159
22160 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022161 * Handle setting internal v: variables separately where needed to
22162 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022163 */
22164 if (ht == &vimvarht)
22165 {
22166 if (v->di_tv.v_type == VAR_STRING)
22167 {
22168 vim_free(v->di_tv.vval.v_string);
22169 if (copy || tv->v_type != VAR_STRING)
22170 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22171 else
22172 {
22173 /* Take over the string to avoid an extra alloc/free. */
22174 v->di_tv.vval.v_string = tv->vval.v_string;
22175 tv->vval.v_string = NULL;
22176 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022177 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022178 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022179 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022180 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022181 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022182 if (STRCMP(varname, "searchforward") == 0)
22183 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022184#ifdef FEAT_SEARCH_EXTRA
22185 else if (STRCMP(varname, "hlsearch") == 0)
22186 {
22187 no_hlsearch = !v->di_tv.vval.v_number;
22188 redraw_all_later(SOME_VALID);
22189 }
22190#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022191 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022192 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022193 else if (v->di_tv.v_type != tv->v_type)
22194 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022195 }
22196
22197 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 }
22199 else /* add a new variable */
22200 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022201 /* Can't add "v:" variable. */
22202 if (ht == &vimvarht)
22203 {
22204 EMSG2(_(e_illvar), name);
22205 return;
22206 }
22207
Bram Moolenaar92124a32005-06-17 22:03:40 +000022208 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022209 if (!valid_varname(varname))
22210 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022211
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022212 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22213 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022214 if (v == NULL)
22215 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022216 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022217 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022219 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 return;
22221 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022222 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022224
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022225 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022226 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022227 else
22228 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022229 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022230 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022231 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233}
22234
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022235/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022236 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022237 * Also give an error message.
22238 */
22239 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022240var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022241 int flags;
22242 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022243 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022244{
22245 if (flags & DI_FLAGS_RO)
22246 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022247 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022248 return TRUE;
22249 }
22250 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22251 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022252 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022253 return TRUE;
22254 }
22255 return FALSE;
22256}
22257
22258/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022259 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22260 * Also give an error message.
22261 */
22262 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022263var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022264 int flags;
22265 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022266 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022267{
22268 if (flags & DI_FLAGS_FIX)
22269 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022270 EMSG2(_("E795: Cannot delete variable %s"),
22271 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022272 return TRUE;
22273 }
22274 return FALSE;
22275}
22276
22277/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022278 * Check if a funcref is assigned to a valid variable name.
22279 * Return TRUE and give an error if not.
22280 */
22281 static int
22282var_check_func_name(name, new_var)
22283 char_u *name; /* points to start of variable name */
22284 int new_var; /* TRUE when creating the variable */
22285{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022286 /* Allow for w: b: s: and t:. */
22287 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022288 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22289 ? name[2] : name[0]))
22290 {
22291 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22292 name);
22293 return TRUE;
22294 }
22295 /* Don't allow hiding a function. When "v" is not NULL we might be
22296 * assigning another function to the same var, the type is checked
22297 * below. */
22298 if (new_var && function_exists(name))
22299 {
22300 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22301 name);
22302 return TRUE;
22303 }
22304 return FALSE;
22305}
22306
22307/*
22308 * Check if a variable name is valid.
22309 * Return FALSE and give an error if not.
22310 */
22311 static int
22312valid_varname(varname)
22313 char_u *varname;
22314{
22315 char_u *p;
22316
22317 for (p = varname; *p != NUL; ++p)
22318 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22319 && *p != AUTOLOAD_CHAR)
22320 {
22321 EMSG2(_(e_illvar), varname);
22322 return FALSE;
22323 }
22324 return TRUE;
22325}
22326
22327/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022328 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022329 * Also give an error message, using "name" or _("name") when use_gettext is
22330 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022331 */
22332 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022333tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022334 int lock;
22335 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022336 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022337{
22338 if (lock & VAR_LOCKED)
22339 {
22340 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022341 name == NULL ? (char_u *)_("Unknown")
22342 : use_gettext ? (char_u *)_(name)
22343 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022344 return TRUE;
22345 }
22346 if (lock & VAR_FIXED)
22347 {
22348 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022349 name == NULL ? (char_u *)_("Unknown")
22350 : use_gettext ? (char_u *)_(name)
22351 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022352 return TRUE;
22353 }
22354 return FALSE;
22355}
22356
22357/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022358 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022359 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022360 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022361 * It is OK for "from" and "to" to point to the same item. This is used to
22362 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022363 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022364 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022365copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022366 typval_T *from;
22367 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022369 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022370 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022371 switch (from->v_type)
22372 {
22373 case VAR_NUMBER:
22374 to->vval.v_number = from->vval.v_number;
22375 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022376#ifdef FEAT_FLOAT
22377 case VAR_FLOAT:
22378 to->vval.v_float = from->vval.v_float;
22379 break;
22380#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022381 case VAR_STRING:
22382 case VAR_FUNC:
22383 if (from->vval.v_string == NULL)
22384 to->vval.v_string = NULL;
22385 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022386 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022387 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022388 if (from->v_type == VAR_FUNC)
22389 func_ref(to->vval.v_string);
22390 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022391 break;
22392 case VAR_LIST:
22393 if (from->vval.v_list == NULL)
22394 to->vval.v_list = NULL;
22395 else
22396 {
22397 to->vval.v_list = from->vval.v_list;
22398 ++to->vval.v_list->lv_refcount;
22399 }
22400 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022401 case VAR_DICT:
22402 if (from->vval.v_dict == NULL)
22403 to->vval.v_dict = NULL;
22404 else
22405 {
22406 to->vval.v_dict = from->vval.v_dict;
22407 ++to->vval.v_dict->dv_refcount;
22408 }
22409 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022410 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022411 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022412 break;
22413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414}
22415
22416/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022417 * Make a copy of an item.
22418 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022419 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22420 * reference to an already copied list/dict can be used.
22421 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022422 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022423 static int
22424item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022425 typval_T *from;
22426 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022427 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022428 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022429{
22430 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022431 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022432
Bram Moolenaar33570922005-01-25 22:26:29 +000022433 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022434 {
22435 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022436 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022437 }
22438 ++recurse;
22439
22440 switch (from->v_type)
22441 {
22442 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022443#ifdef FEAT_FLOAT
22444 case VAR_FLOAT:
22445#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022446 case VAR_STRING:
22447 case VAR_FUNC:
22448 copy_tv(from, to);
22449 break;
22450 case VAR_LIST:
22451 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022452 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022453 if (from->vval.v_list == NULL)
22454 to->vval.v_list = NULL;
22455 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22456 {
22457 /* use the copy made earlier */
22458 to->vval.v_list = from->vval.v_list->lv_copylist;
22459 ++to->vval.v_list->lv_refcount;
22460 }
22461 else
22462 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22463 if (to->vval.v_list == NULL)
22464 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022465 break;
22466 case VAR_DICT:
22467 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022468 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022469 if (from->vval.v_dict == NULL)
22470 to->vval.v_dict = NULL;
22471 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22472 {
22473 /* use the copy made earlier */
22474 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22475 ++to->vval.v_dict->dv_refcount;
22476 }
22477 else
22478 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22479 if (to->vval.v_dict == NULL)
22480 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022481 break;
22482 default:
22483 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022484 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022485 }
22486 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022487 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022488}
22489
22490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022491 * ":echo expr1 ..." print each argument separated with a space, add a
22492 * newline at the end.
22493 * ":echon expr1 ..." print each argument plain.
22494 */
22495 void
22496ex_echo(eap)
22497 exarg_T *eap;
22498{
22499 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022500 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022501 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502 char_u *p;
22503 int needclr = TRUE;
22504 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022505 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506
22507 if (eap->skip)
22508 ++emsg_skip;
22509 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22510 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022511 /* If eval1() causes an error message the text from the command may
22512 * still need to be cleared. E.g., "echo 22,44". */
22513 need_clr_eos = needclr;
22514
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022516 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 {
22518 /*
22519 * Report the invalid expression unless the expression evaluation
22520 * has been cancelled due to an aborting error, an interrupt, or an
22521 * exception.
22522 */
22523 if (!aborting())
22524 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022525 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 break;
22527 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022528 need_clr_eos = FALSE;
22529
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530 if (!eap->skip)
22531 {
22532 if (atstart)
22533 {
22534 atstart = FALSE;
22535 /* Call msg_start() after eval1(), evaluating the expression
22536 * may cause a message to appear. */
22537 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022538 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022539 /* Mark the saved text as finishing the line, so that what
22540 * follows is displayed on a new line when scrolling back
22541 * at the more prompt. */
22542 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 }
22546 else if (eap->cmdidx == CMD_echo)
22547 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022548 current_copyID += COPYID_INC;
22549 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022550 if (p != NULL)
22551 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022553 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022554 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022555 if (*p != TAB && needclr)
22556 {
22557 /* remove any text still there from the command */
22558 msg_clr_eos();
22559 needclr = FALSE;
22560 }
22561 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 }
22563 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022564 {
22565#ifdef FEAT_MBYTE
22566 if (has_mbyte)
22567 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022568 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022569
22570 (void)msg_outtrans_len_attr(p, i, echo_attr);
22571 p += i - 1;
22572 }
22573 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022575 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022578 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022580 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 arg = skipwhite(arg);
22582 }
22583 eap->nextcmd = check_nextcmd(arg);
22584
22585 if (eap->skip)
22586 --emsg_skip;
22587 else
22588 {
22589 /* remove text that may still be there from the command */
22590 if (needclr)
22591 msg_clr_eos();
22592 if (eap->cmdidx == CMD_echo)
22593 msg_end();
22594 }
22595}
22596
22597/*
22598 * ":echohl {name}".
22599 */
22600 void
22601ex_echohl(eap)
22602 exarg_T *eap;
22603{
22604 int id;
22605
22606 id = syn_name2id(eap->arg);
22607 if (id == 0)
22608 echo_attr = 0;
22609 else
22610 echo_attr = syn_id2attr(id);
22611}
22612
22613/*
22614 * ":execute expr1 ..." execute the result of an expression.
22615 * ":echomsg expr1 ..." Print a message
22616 * ":echoerr expr1 ..." Print an error
22617 * Each gets spaces around each argument and a newline at the end for
22618 * echo commands
22619 */
22620 void
22621ex_execute(eap)
22622 exarg_T *eap;
22623{
22624 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022625 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 int ret = OK;
22627 char_u *p;
22628 garray_T ga;
22629 int len;
22630 int save_did_emsg;
22631
22632 ga_init2(&ga, 1, 80);
22633
22634 if (eap->skip)
22635 ++emsg_skip;
22636 while (*arg != NUL && *arg != '|' && *arg != '\n')
22637 {
22638 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022639 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640 {
22641 /*
22642 * Report the invalid expression unless the expression evaluation
22643 * has been cancelled due to an aborting error, an interrupt, or an
22644 * exception.
22645 */
22646 if (!aborting())
22647 EMSG2(_(e_invexpr2), p);
22648 ret = FAIL;
22649 break;
22650 }
22651
22652 if (!eap->skip)
22653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022654 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655 len = (int)STRLEN(p);
22656 if (ga_grow(&ga, len + 2) == FAIL)
22657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022658 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 ret = FAIL;
22660 break;
22661 }
22662 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 ga.ga_len += len;
22666 }
22667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022668 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 arg = skipwhite(arg);
22670 }
22671
22672 if (ret != FAIL && ga.ga_data != NULL)
22673 {
22674 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022675 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022677 out_flush();
22678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022679 else if (eap->cmdidx == CMD_echoerr)
22680 {
22681 /* We don't want to abort following commands, restore did_emsg. */
22682 save_did_emsg = did_emsg;
22683 EMSG((char_u *)ga.ga_data);
22684 if (!force_abort)
22685 did_emsg = save_did_emsg;
22686 }
22687 else if (eap->cmdidx == CMD_execute)
22688 do_cmdline((char_u *)ga.ga_data,
22689 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22690 }
22691
22692 ga_clear(&ga);
22693
22694 if (eap->skip)
22695 --emsg_skip;
22696
22697 eap->nextcmd = check_nextcmd(arg);
22698}
22699
22700/*
22701 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22702 * "arg" points to the "&" or '+' when called, to "option" when returning.
22703 * Returns NULL when no option name found. Otherwise pointer to the char
22704 * after the option name.
22705 */
22706 static char_u *
22707find_option_end(arg, opt_flags)
22708 char_u **arg;
22709 int *opt_flags;
22710{
22711 char_u *p = *arg;
22712
22713 ++p;
22714 if (*p == 'g' && p[1] == ':')
22715 {
22716 *opt_flags = OPT_GLOBAL;
22717 p += 2;
22718 }
22719 else if (*p == 'l' && p[1] == ':')
22720 {
22721 *opt_flags = OPT_LOCAL;
22722 p += 2;
22723 }
22724 else
22725 *opt_flags = 0;
22726
22727 if (!ASCII_ISALPHA(*p))
22728 return NULL;
22729 *arg = p;
22730
22731 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22732 p += 4; /* termcap option */
22733 else
22734 while (ASCII_ISALPHA(*p))
22735 ++p;
22736 return p;
22737}
22738
22739/*
22740 * ":function"
22741 */
22742 void
22743ex_function(eap)
22744 exarg_T *eap;
22745{
22746 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022747 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748 int j;
22749 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022751 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 char_u *name = NULL;
22753 char_u *p;
22754 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022755 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756 garray_T newargs;
22757 garray_T newlines;
22758 int varargs = FALSE;
22759 int mustend = FALSE;
22760 int flags = 0;
22761 ufunc_T *fp;
22762 int indent;
22763 int nesting;
22764 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022765 dictitem_T *v;
22766 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022767 static int func_nr = 0; /* number for nameless function */
22768 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022769 hashtab_T *ht;
22770 int todo;
22771 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022772 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022773
22774 /*
22775 * ":function" without argument: list functions.
22776 */
22777 if (ends_excmd(*eap->arg))
22778 {
22779 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022780 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022781 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022782 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022783 {
22784 if (!HASHITEM_EMPTY(hi))
22785 {
22786 --todo;
22787 fp = HI2UF(hi);
22788 if (!isdigit(*fp->uf_name))
22789 list_func_head(fp, FALSE);
22790 }
22791 }
22792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793 eap->nextcmd = check_nextcmd(eap->arg);
22794 return;
22795 }
22796
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022797 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022798 * ":function /pat": list functions matching pattern.
22799 */
22800 if (*eap->arg == '/')
22801 {
22802 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22803 if (!eap->skip)
22804 {
22805 regmatch_T regmatch;
22806
22807 c = *p;
22808 *p = NUL;
22809 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22810 *p = c;
22811 if (regmatch.regprog != NULL)
22812 {
22813 regmatch.rm_ic = p_ic;
22814
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022815 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022816 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22817 {
22818 if (!HASHITEM_EMPTY(hi))
22819 {
22820 --todo;
22821 fp = HI2UF(hi);
22822 if (!isdigit(*fp->uf_name)
22823 && vim_regexec(&regmatch, fp->uf_name, 0))
22824 list_func_head(fp, FALSE);
22825 }
22826 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022827 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022828 }
22829 }
22830 if (*p == '/')
22831 ++p;
22832 eap->nextcmd = check_nextcmd(p);
22833 return;
22834 }
22835
22836 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022837 * Get the function name. There are these situations:
22838 * func normal function name
22839 * "name" == func, "fudi.fd_dict" == NULL
22840 * dict.func new dictionary entry
22841 * "name" == NULL, "fudi.fd_dict" set,
22842 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22843 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022844 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022845 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22846 * dict.func existing dict entry that's not a Funcref
22847 * "name" == NULL, "fudi.fd_dict" set,
22848 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022849 * s:func script-local function name
22850 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022853 name = trans_function_name(&p, eap->skip, 0, &fudi);
22854 paren = (vim_strchr(p, '(') != NULL);
22855 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 {
22857 /*
22858 * Return on an invalid expression in braces, unless the expression
22859 * evaluation has been cancelled due to an aborting error, an
22860 * interrupt, or an exception.
22861 */
22862 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022863 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022864 if (!eap->skip && fudi.fd_newkey != NULL)
22865 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022866 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 else
22870 eap->skip = TRUE;
22871 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022872
Bram Moolenaar071d4272004-06-13 20:20:40 +000022873 /* An error in a function call during evaluation of an expression in magic
22874 * braces should not cause the function not to be defined. */
22875 saved_did_emsg = did_emsg;
22876 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022877
22878 /*
22879 * ":function func" with only function name: list function.
22880 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022881 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 {
22883 if (!ends_excmd(*skipwhite(p)))
22884 {
22885 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022886 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 }
22888 eap->nextcmd = check_nextcmd(p);
22889 if (eap->nextcmd != NULL)
22890 *p = NUL;
22891 if (!eap->skip && !got_int)
22892 {
22893 fp = find_func(name);
22894 if (fp != NULL)
22895 {
22896 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022897 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022899 if (FUNCLINE(fp, j) == NULL)
22900 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 msg_putchar('\n');
22902 msg_outnum((long)(j + 1));
22903 if (j < 9)
22904 msg_putchar(' ');
22905 if (j < 99)
22906 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022907 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 out_flush(); /* show a line at a time */
22909 ui_breakcheck();
22910 }
22911 if (!got_int)
22912 {
22913 msg_putchar('\n');
22914 msg_puts((char_u *)" endfunction");
22915 }
22916 }
22917 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022918 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022920 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 }
22922
22923 /*
22924 * ":function name(arg1, arg2)" Define function.
22925 */
22926 p = skipwhite(p);
22927 if (*p != '(')
22928 {
22929 if (!eap->skip)
22930 {
22931 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022932 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 }
22934 /* attempt to continue by skipping some text */
22935 if (vim_strchr(p, '(') != NULL)
22936 p = vim_strchr(p, '(');
22937 }
22938 p = skipwhite(p + 1);
22939
22940 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22941 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22942
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022943 if (!eap->skip)
22944 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022945 /* Check the name of the function. Unless it's a dictionary function
22946 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022947 if (name != NULL)
22948 arg = name;
22949 else
22950 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022951 if (arg != NULL && (fudi.fd_di == NULL
22952 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022953 {
22954 if (*arg == K_SPECIAL)
22955 j = 3;
22956 else
22957 j = 0;
22958 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22959 : eval_isnamec(arg[j])))
22960 ++j;
22961 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022962 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022963 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022964 /* Disallow using the g: dict. */
22965 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22966 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022967 }
22968
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969 /*
22970 * Isolate the arguments: "arg1, arg2, ...)"
22971 */
22972 while (*p != ')')
22973 {
22974 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22975 {
22976 varargs = TRUE;
22977 p += 3;
22978 mustend = TRUE;
22979 }
22980 else
22981 {
22982 arg = p;
22983 while (ASCII_ISALNUM(*p) || *p == '_')
22984 ++p;
22985 if (arg == p || isdigit(*arg)
22986 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22987 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22988 {
22989 if (!eap->skip)
22990 EMSG2(_("E125: Illegal argument: %s"), arg);
22991 break;
22992 }
22993 if (ga_grow(&newargs, 1) == FAIL)
22994 goto erret;
22995 c = *p;
22996 *p = NUL;
22997 arg = vim_strsave(arg);
22998 if (arg == NULL)
22999 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023000
23001 /* Check for duplicate argument name. */
23002 for (i = 0; i < newargs.ga_len; ++i)
23003 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23004 {
23005 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023006 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023007 goto erret;
23008 }
23009
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23011 *p = c;
23012 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013 if (*p == ',')
23014 ++p;
23015 else
23016 mustend = TRUE;
23017 }
23018 p = skipwhite(p);
23019 if (mustend && *p != ')')
23020 {
23021 if (!eap->skip)
23022 EMSG2(_(e_invarg2), eap->arg);
23023 break;
23024 }
23025 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023026 if (*p != ')')
23027 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028 ++p; /* skip the ')' */
23029
Bram Moolenaare9a41262005-01-15 22:18:47 +000023030 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023031 for (;;)
23032 {
23033 p = skipwhite(p);
23034 if (STRNCMP(p, "range", 5) == 0)
23035 {
23036 flags |= FC_RANGE;
23037 p += 5;
23038 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023039 else if (STRNCMP(p, "dict", 4) == 0)
23040 {
23041 flags |= FC_DICT;
23042 p += 4;
23043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023044 else if (STRNCMP(p, "abort", 5) == 0)
23045 {
23046 flags |= FC_ABORT;
23047 p += 5;
23048 }
23049 else
23050 break;
23051 }
23052
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023053 /* When there is a line break use what follows for the function body.
23054 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23055 if (*p == '\n')
23056 line_arg = p + 1;
23057 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058 EMSG(_(e_trailing));
23059
23060 /*
23061 * Read the body of the function, until ":endfunction" is found.
23062 */
23063 if (KeyTyped)
23064 {
23065 /* Check if the function already exists, don't let the user type the
23066 * whole function before telling him it doesn't work! For a script we
23067 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023068 if (!eap->skip && !eap->forceit)
23069 {
23070 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23071 EMSG(_(e_funcdict));
23072 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023073 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023075
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023076 if (!eap->skip && did_emsg)
23077 goto erret;
23078
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079 msg_putchar('\n'); /* don't overwrite the function name */
23080 cmdline_row = msg_row;
23081 }
23082
23083 indent = 2;
23084 nesting = 0;
23085 for (;;)
23086 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023087 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023088 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023089 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023090 saved_wait_return = FALSE;
23091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023092 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023093 sourcing_lnum_off = sourcing_lnum;
23094
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023095 if (line_arg != NULL)
23096 {
23097 /* Use eap->arg, split up in parts by line breaks. */
23098 theline = line_arg;
23099 p = vim_strchr(theline, '\n');
23100 if (p == NULL)
23101 line_arg += STRLEN(line_arg);
23102 else
23103 {
23104 *p = NUL;
23105 line_arg = p + 1;
23106 }
23107 }
23108 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023109 theline = getcmdline(':', 0L, indent);
23110 else
23111 theline = eap->getline(':', eap->cookie, indent);
23112 if (KeyTyped)
23113 lines_left = Rows - 1;
23114 if (theline == NULL)
23115 {
23116 EMSG(_("E126: Missing :endfunction"));
23117 goto erret;
23118 }
23119
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023120 /* Detect line continuation: sourcing_lnum increased more than one. */
23121 if (sourcing_lnum > sourcing_lnum_off + 1)
23122 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23123 else
23124 sourcing_lnum_off = 0;
23125
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 if (skip_until != NULL)
23127 {
23128 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23129 * don't check for ":endfunc". */
23130 if (STRCMP(theline, skip_until) == 0)
23131 {
23132 vim_free(skip_until);
23133 skip_until = NULL;
23134 }
23135 }
23136 else
23137 {
23138 /* skip ':' and blanks*/
23139 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23140 ;
23141
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023142 /* Check for "endfunction". */
23143 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023145 if (line_arg == NULL)
23146 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 break;
23148 }
23149
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023150 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 * at "end". */
23152 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23153 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023154 else if (STRNCMP(p, "if", 2) == 0
23155 || STRNCMP(p, "wh", 2) == 0
23156 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157 || STRNCMP(p, "try", 3) == 0)
23158 indent += 2;
23159
23160 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023161 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023163 if (*p == '!')
23164 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023166 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23167 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023169 ++nesting;
23170 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 }
23172 }
23173
23174 /* Check for ":append" or ":insert". */
23175 p = skip_range(p, NULL);
23176 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23177 || (p[0] == 'i'
23178 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23179 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23180 skip_until = vim_strsave((char_u *)".");
23181
23182 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23183 arg = skipwhite(skiptowhite(p));
23184 if (arg[0] == '<' && arg[1] =='<'
23185 && ((p[0] == 'p' && p[1] == 'y'
23186 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23187 || (p[0] == 'p' && p[1] == 'e'
23188 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23189 || (p[0] == 't' && p[1] == 'c'
23190 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023191 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23192 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23194 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023195 || (p[0] == 'm' && p[1] == 'z'
23196 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 ))
23198 {
23199 /* ":python <<" continues until a dot, like ":append" */
23200 p = skipwhite(arg + 2);
23201 if (*p == NUL)
23202 skip_until = vim_strsave((char_u *)".");
23203 else
23204 skip_until = vim_strsave(p);
23205 }
23206 }
23207
23208 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023209 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023210 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023211 if (line_arg == NULL)
23212 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023213 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023214 }
23215
23216 /* Copy the line to newly allocated memory. get_one_sourceline()
23217 * allocates 250 bytes per line, this saves 80% on average. The cost
23218 * is an extra alloc/free. */
23219 p = vim_strsave(theline);
23220 if (p != NULL)
23221 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023222 if (line_arg == NULL)
23223 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023224 theline = p;
23225 }
23226
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023227 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23228
23229 /* Add NULL lines for continuation lines, so that the line count is
23230 * equal to the index in the growarray. */
23231 while (sourcing_lnum_off-- > 0)
23232 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023233
23234 /* Check for end of eap->arg. */
23235 if (line_arg != NULL && *line_arg == NUL)
23236 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 }
23238
23239 /* Don't define the function when skipping commands or when an error was
23240 * detected. */
23241 if (eap->skip || did_emsg)
23242 goto erret;
23243
23244 /*
23245 * If there are no errors, add the function
23246 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023247 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023248 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023249 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023250 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023251 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023252 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023253 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023254 goto erret;
23255 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023256
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023257 fp = find_func(name);
23258 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023259 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023260 if (!eap->forceit)
23261 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023262 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023263 goto erret;
23264 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023265 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023266 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023267 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023268 name);
23269 goto erret;
23270 }
23271 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023272 ga_clear_strings(&(fp->uf_args));
23273 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023274 vim_free(name);
23275 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277 }
23278 else
23279 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023280 char numbuf[20];
23281
23282 fp = NULL;
23283 if (fudi.fd_newkey == NULL && !eap->forceit)
23284 {
23285 EMSG(_(e_funcdict));
23286 goto erret;
23287 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023288 if (fudi.fd_di == NULL)
23289 {
23290 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023291 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023292 goto erret;
23293 }
23294 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023295 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023296 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023297
23298 /* Give the function a sequential number. Can only be used with a
23299 * Funcref! */
23300 vim_free(name);
23301 sprintf(numbuf, "%d", ++func_nr);
23302 name = vim_strsave((char_u *)numbuf);
23303 if (name == NULL)
23304 goto erret;
23305 }
23306
23307 if (fp == NULL)
23308 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023309 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023310 {
23311 int slen, plen;
23312 char_u *scriptname;
23313
23314 /* Check that the autoload name matches the script name. */
23315 j = FAIL;
23316 if (sourcing_name != NULL)
23317 {
23318 scriptname = autoload_name(name);
23319 if (scriptname != NULL)
23320 {
23321 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023322 plen = (int)STRLEN(p);
23323 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023324 if (slen > plen && fnamecmp(p,
23325 sourcing_name + slen - plen) == 0)
23326 j = OK;
23327 vim_free(scriptname);
23328 }
23329 }
23330 if (j == FAIL)
23331 {
23332 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23333 goto erret;
23334 }
23335 }
23336
23337 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023338 if (fp == NULL)
23339 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023340
23341 if (fudi.fd_dict != NULL)
23342 {
23343 if (fudi.fd_di == NULL)
23344 {
23345 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023346 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023347 if (fudi.fd_di == NULL)
23348 {
23349 vim_free(fp);
23350 goto erret;
23351 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023352 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23353 {
23354 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023355 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023356 goto erret;
23357 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023358 }
23359 else
23360 /* overwrite existing dict entry */
23361 clear_tv(&fudi.fd_di->di_tv);
23362 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023363 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023364 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023365 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023366
23367 /* behave like "dict" was used */
23368 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023369 }
23370
Bram Moolenaar071d4272004-06-13 20:20:40 +000023371 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023372 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023373 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23374 {
23375 vim_free(fp);
23376 goto erret;
23377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023379 fp->uf_args = newargs;
23380 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023381#ifdef FEAT_PROFILE
23382 fp->uf_tml_count = NULL;
23383 fp->uf_tml_total = NULL;
23384 fp->uf_tml_self = NULL;
23385 fp->uf_profiling = FALSE;
23386 if (prof_def_func())
23387 func_do_profile(fp);
23388#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023389 fp->uf_varargs = varargs;
23390 fp->uf_flags = flags;
23391 fp->uf_calls = 0;
23392 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023393 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394
23395erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 ga_clear_strings(&newargs);
23397 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023398ret_free:
23399 vim_free(skip_until);
23400 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023403 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404}
23405
23406/*
23407 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023408 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023409 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023410 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023411 * TFN_INT: internal function name OK
23412 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023413 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 * Advances "pp" to just after the function name (if no error).
23415 */
23416 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023417trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023418 char_u **pp;
23419 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023420 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023421 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023422{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023423 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424 char_u *start;
23425 char_u *end;
23426 int lead;
23427 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023428 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023429 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023430
23431 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023432 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023433 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023434
23435 /* Check for hard coded <SNR>: already translated function ID (from a user
23436 * command). */
23437 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23438 && (*pp)[2] == (int)KE_SNR)
23439 {
23440 *pp += 3;
23441 len = get_id_len(pp) + 3;
23442 return vim_strnsave(start, len);
23443 }
23444
23445 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23446 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023447 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023448 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023450
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023451 /* Note that TFN_ flags use the same values as GLV_ flags. */
23452 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023453 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023454 if (end == start)
23455 {
23456 if (!skip)
23457 EMSG(_("E129: Function name required"));
23458 goto theend;
23459 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023460 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023461 {
23462 /*
23463 * Report an invalid expression in braces, unless the expression
23464 * evaluation has been cancelled due to an aborting error, an
23465 * interrupt, or an exception.
23466 */
23467 if (!aborting())
23468 {
23469 if (end != NULL)
23470 EMSG2(_(e_invarg2), start);
23471 }
23472 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023473 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023474 goto theend;
23475 }
23476
23477 if (lv.ll_tv != NULL)
23478 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023479 if (fdp != NULL)
23480 {
23481 fdp->fd_dict = lv.ll_dict;
23482 fdp->fd_newkey = lv.ll_newkey;
23483 lv.ll_newkey = NULL;
23484 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023485 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023486 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23487 {
23488 name = vim_strsave(lv.ll_tv->vval.v_string);
23489 *pp = end;
23490 }
23491 else
23492 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023493 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23494 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023495 EMSG(_(e_funcref));
23496 else
23497 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023498 name = NULL;
23499 }
23500 goto theend;
23501 }
23502
23503 if (lv.ll_name == NULL)
23504 {
23505 /* Error found, but continue after the function name. */
23506 *pp = end;
23507 goto theend;
23508 }
23509
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023510 /* Check if the name is a Funcref. If so, use the value. */
23511 if (lv.ll_exp_name != NULL)
23512 {
23513 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023514 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023515 if (name == lv.ll_exp_name)
23516 name = NULL;
23517 }
23518 else
23519 {
23520 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023521 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023522 if (name == *pp)
23523 name = NULL;
23524 }
23525 if (name != NULL)
23526 {
23527 name = vim_strsave(name);
23528 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023529 if (STRNCMP(name, "<SNR>", 5) == 0)
23530 {
23531 /* Change "<SNR>" to the byte sequence. */
23532 name[0] = K_SPECIAL;
23533 name[1] = KS_EXTRA;
23534 name[2] = (int)KE_SNR;
23535 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23536 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023537 goto theend;
23538 }
23539
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023540 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023541 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023542 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023543 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23544 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23545 {
23546 /* When there was "s:" already or the name expanded to get a
23547 * leading "s:" then remove it. */
23548 lv.ll_name += 2;
23549 len -= 2;
23550 lead = 2;
23551 }
23552 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023553 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023554 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023555 /* skip over "s:" and "g:" */
23556 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023557 lv.ll_name += 2;
23558 len = (int)(end - lv.ll_name);
23559 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023560
23561 /*
23562 * Copy the function name to allocated memory.
23563 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23564 * Accept <SNR>123_name() outside a script.
23565 */
23566 if (skip)
23567 lead = 0; /* do nothing */
23568 else if (lead > 0)
23569 {
23570 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023571 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23572 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023573 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023574 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023575 if (current_SID <= 0)
23576 {
23577 EMSG(_(e_usingsid));
23578 goto theend;
23579 }
23580 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23581 lead += (int)STRLEN(sid_buf);
23582 }
23583 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023584 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023585 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023586 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023587 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023588 goto theend;
23589 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023590 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023591 {
23592 char_u *cp = vim_strchr(lv.ll_name, ':');
23593
23594 if (cp != NULL && cp < end)
23595 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023596 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023597 goto theend;
23598 }
23599 }
23600
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023601 name = alloc((unsigned)(len + lead + 1));
23602 if (name != NULL)
23603 {
23604 if (lead > 0)
23605 {
23606 name[0] = K_SPECIAL;
23607 name[1] = KS_EXTRA;
23608 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023609 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023610 STRCPY(name + 3, sid_buf);
23611 }
23612 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023613 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023614 }
23615 *pp = end;
23616
23617theend:
23618 clear_lval(&lv);
23619 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620}
23621
23622/*
23623 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23624 * Return 2 if "p" starts with "s:".
23625 * Return 0 otherwise.
23626 */
23627 static int
23628eval_fname_script(p)
23629 char_u *p;
23630{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023631 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23632 * the standard library function. */
23633 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23634 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023635 return 5;
23636 if (p[0] == 's' && p[1] == ':')
23637 return 2;
23638 return 0;
23639}
23640
23641/*
23642 * Return TRUE if "p" starts with "<SID>" or "s:".
23643 * Only works if eval_fname_script() returned non-zero for "p"!
23644 */
23645 static int
23646eval_fname_sid(p)
23647 char_u *p;
23648{
23649 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23650}
23651
23652/*
23653 * List the head of the function: "name(arg1, arg2)".
23654 */
23655 static void
23656list_func_head(fp, indent)
23657 ufunc_T *fp;
23658 int indent;
23659{
23660 int j;
23661
23662 msg_start();
23663 if (indent)
23664 MSG_PUTS(" ");
23665 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023666 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 {
23668 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023669 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 }
23671 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023672 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023674 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675 {
23676 if (j)
23677 MSG_PUTS(", ");
23678 msg_puts(FUNCARG(fp, j));
23679 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023680 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 {
23682 if (j)
23683 MSG_PUTS(", ");
23684 MSG_PUTS("...");
23685 }
23686 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023687 if (fp->uf_flags & FC_ABORT)
23688 MSG_PUTS(" abort");
23689 if (fp->uf_flags & FC_RANGE)
23690 MSG_PUTS(" range");
23691 if (fp->uf_flags & FC_DICT)
23692 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023693 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023694 if (p_verbose > 0)
23695 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023696}
23697
23698/*
23699 * Find a function by name, return pointer to it in ufuncs.
23700 * Return NULL for unknown function.
23701 */
23702 static ufunc_T *
23703find_func(name)
23704 char_u *name;
23705{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023706 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023708 hi = hash_find(&func_hashtab, name);
23709 if (!HASHITEM_EMPTY(hi))
23710 return HI2UF(hi);
23711 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023712}
23713
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023714#if defined(EXITFREE) || defined(PROTO)
23715 void
23716free_all_functions()
23717{
23718 hashitem_T *hi;
23719
23720 /* Need to start all over every time, because func_free() may change the
23721 * hash table. */
23722 while (func_hashtab.ht_used > 0)
23723 for (hi = func_hashtab.ht_array; ; ++hi)
23724 if (!HASHITEM_EMPTY(hi))
23725 {
23726 func_free(HI2UF(hi));
23727 break;
23728 }
23729}
23730#endif
23731
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023732 int
23733translated_function_exists(name)
23734 char_u *name;
23735{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023736 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023737 return find_internal_func(name) >= 0;
23738 return find_func(name) != NULL;
23739}
23740
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023741/*
23742 * Return TRUE if a function "name" exists.
23743 */
23744 static int
23745function_exists(name)
23746 char_u *name;
23747{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023748 char_u *nm = name;
23749 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023750 int n = FALSE;
23751
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023752 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23753 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023754 nm = skipwhite(nm);
23755
23756 /* Only accept "funcname", "funcname ", "funcname (..." and
23757 * "funcname(...", not "funcname!...". */
23758 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023759 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023760 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023761 return n;
23762}
23763
Bram Moolenaara1544c02013-05-30 12:35:52 +020023764 char_u *
23765get_expanded_name(name, check)
23766 char_u *name;
23767 int check;
23768{
23769 char_u *nm = name;
23770 char_u *p;
23771
23772 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23773
23774 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023775 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023776 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023777
Bram Moolenaara1544c02013-05-30 12:35:52 +020023778 vim_free(p);
23779 return NULL;
23780}
23781
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023782/*
23783 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023784 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23785 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023786 */
23787 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023788builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023789 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023790 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023791{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023792 char_u *p;
23793
23794 if (!ASCII_ISLOWER(name[0]))
23795 return FALSE;
23796 p = vim_strchr(name, AUTOLOAD_CHAR);
23797 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023798}
23799
Bram Moolenaar05159a02005-02-26 23:04:13 +000023800#if defined(FEAT_PROFILE) || defined(PROTO)
23801/*
23802 * Start profiling function "fp".
23803 */
23804 static void
23805func_do_profile(fp)
23806 ufunc_T *fp;
23807{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023808 int len = fp->uf_lines.ga_len;
23809
23810 if (len == 0)
23811 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023812 fp->uf_tm_count = 0;
23813 profile_zero(&fp->uf_tm_self);
23814 profile_zero(&fp->uf_tm_total);
23815 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023816 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023817 if (fp->uf_tml_total == NULL)
23818 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023819 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023820 if (fp->uf_tml_self == NULL)
23821 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023822 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023823 fp->uf_tml_idx = -1;
23824 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23825 || fp->uf_tml_self == NULL)
23826 return; /* out of memory */
23827
23828 fp->uf_profiling = TRUE;
23829}
23830
23831/*
23832 * Dump the profiling results for all functions in file "fd".
23833 */
23834 void
23835func_dump_profile(fd)
23836 FILE *fd;
23837{
23838 hashitem_T *hi;
23839 int todo;
23840 ufunc_T *fp;
23841 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023842 ufunc_T **sorttab;
23843 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023844
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023845 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023846 if (todo == 0)
23847 return; /* nothing to dump */
23848
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023849 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023850
Bram Moolenaar05159a02005-02-26 23:04:13 +000023851 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23852 {
23853 if (!HASHITEM_EMPTY(hi))
23854 {
23855 --todo;
23856 fp = HI2UF(hi);
23857 if (fp->uf_profiling)
23858 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023859 if (sorttab != NULL)
23860 sorttab[st_len++] = fp;
23861
Bram Moolenaar05159a02005-02-26 23:04:13 +000023862 if (fp->uf_name[0] == K_SPECIAL)
23863 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23864 else
23865 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23866 if (fp->uf_tm_count == 1)
23867 fprintf(fd, "Called 1 time\n");
23868 else
23869 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23870 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23871 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23872 fprintf(fd, "\n");
23873 fprintf(fd, "count total (s) self (s)\n");
23874
23875 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23876 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023877 if (FUNCLINE(fp, i) == NULL)
23878 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023879 prof_func_line(fd, fp->uf_tml_count[i],
23880 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023881 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23882 }
23883 fprintf(fd, "\n");
23884 }
23885 }
23886 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023887
23888 if (sorttab != NULL && st_len > 0)
23889 {
23890 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23891 prof_total_cmp);
23892 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23893 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23894 prof_self_cmp);
23895 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23896 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023897
23898 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023899}
Bram Moolenaar73830342005-02-28 22:48:19 +000023900
23901 static void
23902prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23903 FILE *fd;
23904 ufunc_T **sorttab;
23905 int st_len;
23906 char *title;
23907 int prefer_self; /* when equal print only self time */
23908{
23909 int i;
23910 ufunc_T *fp;
23911
23912 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23913 fprintf(fd, "count total (s) self (s) function\n");
23914 for (i = 0; i < 20 && i < st_len; ++i)
23915 {
23916 fp = sorttab[i];
23917 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23918 prefer_self);
23919 if (fp->uf_name[0] == K_SPECIAL)
23920 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23921 else
23922 fprintf(fd, " %s()\n", fp->uf_name);
23923 }
23924 fprintf(fd, "\n");
23925}
23926
23927/*
23928 * Print the count and times for one function or function line.
23929 */
23930 static void
23931prof_func_line(fd, count, total, self, prefer_self)
23932 FILE *fd;
23933 int count;
23934 proftime_T *total;
23935 proftime_T *self;
23936 int prefer_self; /* when equal print only self time */
23937{
23938 if (count > 0)
23939 {
23940 fprintf(fd, "%5d ", count);
23941 if (prefer_self && profile_equal(total, self))
23942 fprintf(fd, " ");
23943 else
23944 fprintf(fd, "%s ", profile_msg(total));
23945 if (!prefer_self && profile_equal(total, self))
23946 fprintf(fd, " ");
23947 else
23948 fprintf(fd, "%s ", profile_msg(self));
23949 }
23950 else
23951 fprintf(fd, " ");
23952}
23953
23954/*
23955 * Compare function for total time sorting.
23956 */
23957 static int
23958#ifdef __BORLANDC__
23959_RTLENTRYF
23960#endif
23961prof_total_cmp(s1, s2)
23962 const void *s1;
23963 const void *s2;
23964{
23965 ufunc_T *p1, *p2;
23966
23967 p1 = *(ufunc_T **)s1;
23968 p2 = *(ufunc_T **)s2;
23969 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23970}
23971
23972/*
23973 * Compare function for self time sorting.
23974 */
23975 static int
23976#ifdef __BORLANDC__
23977_RTLENTRYF
23978#endif
23979prof_self_cmp(s1, s2)
23980 const void *s1;
23981 const void *s2;
23982{
23983 ufunc_T *p1, *p2;
23984
23985 p1 = *(ufunc_T **)s1;
23986 p2 = *(ufunc_T **)s2;
23987 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23988}
23989
Bram Moolenaar05159a02005-02-26 23:04:13 +000023990#endif
23991
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023992/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023993 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023994 * Return TRUE if a package was loaded.
23995 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023996 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023997script_autoload(name, reload)
23998 char_u *name;
23999 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024000{
24001 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024002 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024003 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024004 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024005
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024006 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024007 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024008 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024009 return FALSE;
24010
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024011 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024012
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024013 /* Find the name in the list of previously loaded package names. Skip
24014 * "autoload/", it's always the same. */
24015 for (i = 0; i < ga_loaded.ga_len; ++i)
24016 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24017 break;
24018 if (!reload && i < ga_loaded.ga_len)
24019 ret = FALSE; /* was loaded already */
24020 else
24021 {
24022 /* Remember the name if it wasn't loaded already. */
24023 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24024 {
24025 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24026 tofree = NULL;
24027 }
24028
24029 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024030 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024031 ret = TRUE;
24032 }
24033
24034 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024035 return ret;
24036}
24037
24038/*
24039 * Return the autoload script name for a function or variable name.
24040 * Returns NULL when out of memory.
24041 */
24042 static char_u *
24043autoload_name(name)
24044 char_u *name;
24045{
24046 char_u *p;
24047 char_u *scriptname;
24048
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024049 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024050 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24051 if (scriptname == NULL)
24052 return FALSE;
24053 STRCPY(scriptname, "autoload/");
24054 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024055 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024056 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024057 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024058 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024059 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024060}
24061
Bram Moolenaar071d4272004-06-13 20:20:40 +000024062#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24063
24064/*
24065 * Function given to ExpandGeneric() to obtain the list of user defined
24066 * function names.
24067 */
24068 char_u *
24069get_user_func_name(xp, idx)
24070 expand_T *xp;
24071 int idx;
24072{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024073 static long_u done;
24074 static hashitem_T *hi;
24075 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076
24077 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024079 done = 0;
24080 hi = func_hashtab.ht_array;
24081 }
24082 if (done < func_hashtab.ht_used)
24083 {
24084 if (done++ > 0)
24085 ++hi;
24086 while (HASHITEM_EMPTY(hi))
24087 ++hi;
24088 fp = HI2UF(hi);
24089
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024090 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024091 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024092
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024093 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24094 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024095
24096 cat_func_name(IObuff, fp);
24097 if (xp->xp_context != EXPAND_USER_FUNC)
24098 {
24099 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024100 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 STRCAT(IObuff, ")");
24102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103 return IObuff;
24104 }
24105 return NULL;
24106}
24107
24108#endif /* FEAT_CMDL_COMPL */
24109
24110/*
24111 * Copy the function name of "fp" to buffer "buf".
24112 * "buf" must be able to hold the function name plus three bytes.
24113 * Takes care of script-local function names.
24114 */
24115 static void
24116cat_func_name(buf, fp)
24117 char_u *buf;
24118 ufunc_T *fp;
24119{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024120 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 {
24122 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024123 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124 }
24125 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024126 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024127}
24128
24129/*
24130 * ":delfunction {name}"
24131 */
24132 void
24133ex_delfunction(eap)
24134 exarg_T *eap;
24135{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024136 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024137 char_u *p;
24138 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024139 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140
24141 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024142 name = trans_function_name(&p, eap->skip, 0, &fudi);
24143 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024145 {
24146 if (fudi.fd_dict != NULL && !eap->skip)
24147 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024150 if (!ends_excmd(*skipwhite(p)))
24151 {
24152 vim_free(name);
24153 EMSG(_(e_trailing));
24154 return;
24155 }
24156 eap->nextcmd = check_nextcmd(p);
24157 if (eap->nextcmd != NULL)
24158 *p = NUL;
24159
24160 if (!eap->skip)
24161 fp = find_func(name);
24162 vim_free(name);
24163
24164 if (!eap->skip)
24165 {
24166 if (fp == NULL)
24167 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024168 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024169 return;
24170 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024171 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024172 {
24173 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24174 return;
24175 }
24176
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024177 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024178 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024179 /* Delete the dict item that refers to the function, it will
24180 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024181 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024182 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024183 else
24184 func_free(fp);
24185 }
24186}
24187
24188/*
24189 * Free a function and remove it from the list of functions.
24190 */
24191 static void
24192func_free(fp)
24193 ufunc_T *fp;
24194{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024195 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024196
24197 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024198 ga_clear_strings(&(fp->uf_args));
24199 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024200#ifdef FEAT_PROFILE
24201 vim_free(fp->uf_tml_count);
24202 vim_free(fp->uf_tml_total);
24203 vim_free(fp->uf_tml_self);
24204#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024205
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024206 /* remove the function from the function hashtable */
24207 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24208 if (HASHITEM_EMPTY(hi))
24209 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024210 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024211 hash_remove(&func_hashtab, hi);
24212
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024213 vim_free(fp);
24214}
24215
24216/*
24217 * Unreference a Function: decrement the reference count and free it when it
24218 * becomes zero. Only for numbered functions.
24219 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024220 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024221func_unref(name)
24222 char_u *name;
24223{
24224 ufunc_T *fp;
24225
24226 if (name != NULL && isdigit(*name))
24227 {
24228 fp = find_func(name);
24229 if (fp == NULL)
24230 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024231 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024232 {
24233 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024234 * when "uf_calls" becomes zero. */
24235 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024236 func_free(fp);
24237 }
24238 }
24239}
24240
24241/*
24242 * Count a reference to a Function.
24243 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024244 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024245func_ref(name)
24246 char_u *name;
24247{
24248 ufunc_T *fp;
24249
24250 if (name != NULL && isdigit(*name))
24251 {
24252 fp = find_func(name);
24253 if (fp == NULL)
24254 EMSG2(_(e_intern2), "func_ref()");
24255 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024256 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024257 }
24258}
24259
24260/*
24261 * Call a user function.
24262 */
24263 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024264call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265 ufunc_T *fp; /* pointer to function */
24266 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024267 typval_T *argvars; /* arguments */
24268 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269 linenr_T firstline; /* first line of range */
24270 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024271 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024272{
Bram Moolenaar33570922005-01-25 22:26:29 +000024273 char_u *save_sourcing_name;
24274 linenr_T save_sourcing_lnum;
24275 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024276 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024277 int save_did_emsg;
24278 static int depth = 0;
24279 dictitem_T *v;
24280 int fixvar_idx = 0; /* index in fixvar[] */
24281 int i;
24282 int ai;
24283 char_u numbuf[NUMBUFLEN];
24284 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024285 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024286#ifdef FEAT_PROFILE
24287 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024288 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024290
24291 /* If depth of calling is getting too high, don't execute the function */
24292 if (depth >= p_mfd)
24293 {
24294 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024295 rettv->v_type = VAR_NUMBER;
24296 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297 return;
24298 }
24299 ++depth;
24300
24301 line_breakcheck(); /* check for CTRL-C hit */
24302
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024303 fc = (funccall_T *)alloc(sizeof(funccall_T));
24304 fc->caller = current_funccal;
24305 current_funccal = fc;
24306 fc->func = fp;
24307 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024308 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024309 fc->linenr = 0;
24310 fc->returned = FALSE;
24311 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024312 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024313 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24314 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315
Bram Moolenaar33570922005-01-25 22:26:29 +000024316 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024317 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024318 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24319 * each argument variable and saves a lot of time.
24320 */
24321 /*
24322 * Init l: variables.
24323 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024324 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024325 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024326 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024327 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24328 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024329 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024330 name = v->di_key;
24331 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024332 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024333 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024334 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024335 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024336 v->di_tv.vval.v_dict = selfdict;
24337 ++selfdict->dv_refcount;
24338 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024339
Bram Moolenaar33570922005-01-25 22:26:29 +000024340 /*
24341 * Init a: variables.
24342 * Set a:0 to "argcount".
24343 * Set a:000 to a list with room for the "..." arguments.
24344 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024345 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024346 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024347 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024348 /* Use "name" to avoid a warning from some compiler that checks the
24349 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024350 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024351 name = v->di_key;
24352 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024353 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024354 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024355 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024356 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024357 v->di_tv.vval.v_list = &fc->l_varlist;
24358 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24359 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24360 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024361
24362 /*
24363 * Set a:firstline to "firstline" and a:lastline to "lastline".
24364 * Set a:name to named arguments.
24365 * Set a:N to the "..." arguments.
24366 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024367 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024368 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024369 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024370 (varnumber_T)lastline);
24371 for (i = 0; i < argcount; ++i)
24372 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024373 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024374 if (ai < 0)
24375 /* named argument a:name */
24376 name = FUNCARG(fp, i);
24377 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024378 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024379 /* "..." argument a:1, a:2, etc. */
24380 sprintf((char *)numbuf, "%d", ai + 1);
24381 name = numbuf;
24382 }
24383 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24384 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024385 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024386 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24387 }
24388 else
24389 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024390 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24391 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024392 if (v == NULL)
24393 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024394 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024395 }
24396 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024397 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024398
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024399 /* Note: the values are copied directly to avoid alloc/free.
24400 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024401 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024402 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024403
24404 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24405 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024406 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24407 fc->l_listitems[ai].li_tv = argvars[i];
24408 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024409 }
24410 }
24411
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412 /* Don't redraw while executing the function. */
24413 ++RedrawingDisabled;
24414 save_sourcing_name = sourcing_name;
24415 save_sourcing_lnum = sourcing_lnum;
24416 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024417 /* need space for function name + ("function " + 3) or "[number]" */
24418 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24419 + STRLEN(fp->uf_name) + 20;
24420 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024421 if (sourcing_name != NULL)
24422 {
24423 if (save_sourcing_name != NULL
24424 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024425 sprintf((char *)sourcing_name, "%s[%d]..",
24426 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024427 else
24428 STRCPY(sourcing_name, "function ");
24429 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24430
24431 if (p_verbose >= 12)
24432 {
24433 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024434 verbose_enter_scroll();
24435
Bram Moolenaar555b2802005-05-19 21:08:39 +000024436 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024437 if (p_verbose >= 14)
24438 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024439 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024440 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024441 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024442 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024443
24444 msg_puts((char_u *)"(");
24445 for (i = 0; i < argcount; ++i)
24446 {
24447 if (i > 0)
24448 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024449 if (argvars[i].v_type == VAR_NUMBER)
24450 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024451 else
24452 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024453 /* Do not want errors such as E724 here. */
24454 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024455 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024456 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024457 if (s != NULL)
24458 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024459 if (vim_strsize(s) > MSG_BUF_CLEN)
24460 {
24461 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24462 s = buf;
24463 }
24464 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024465 vim_free(tofree);
24466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024467 }
24468 }
24469 msg_puts((char_u *)")");
24470 }
24471 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024472
24473 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474 --no_wait_return;
24475 }
24476 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024477#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024478 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024479 {
24480 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24481 func_do_profile(fp);
24482 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024483 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024484 {
24485 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024486 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024487 profile_zero(&fp->uf_tm_children);
24488 }
24489 script_prof_save(&wait_start);
24490 }
24491#endif
24492
Bram Moolenaar071d4272004-06-13 20:20:40 +000024493 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024494 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024495 save_did_emsg = did_emsg;
24496 did_emsg = FALSE;
24497
24498 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024499 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024500 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24501
24502 --RedrawingDisabled;
24503
24504 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024505 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024506 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024507 clear_tv(rettv);
24508 rettv->v_type = VAR_NUMBER;
24509 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024510 }
24511
Bram Moolenaar05159a02005-02-26 23:04:13 +000024512#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024513 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024514 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024515 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024516 profile_end(&call_start);
24517 profile_sub_wait(&wait_start, &call_start);
24518 profile_add(&fp->uf_tm_total, &call_start);
24519 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024520 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024521 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024522 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24523 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024524 }
24525 }
24526#endif
24527
Bram Moolenaar071d4272004-06-13 20:20:40 +000024528 /* when being verbose, mention the return value */
24529 if (p_verbose >= 12)
24530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024532 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024533
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024535 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024536 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024537 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024538 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024539 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024540 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024541 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024542 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024543 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024544 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024545
Bram Moolenaar555b2802005-05-19 21:08:39 +000024546 /* The value may be very long. Skip the middle part, so that we
24547 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024548 * truncate it at the end. Don't want errors such as E724 here. */
24549 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024550 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024551 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024552 if (s != NULL)
24553 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024554 if (vim_strsize(s) > MSG_BUF_CLEN)
24555 {
24556 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24557 s = buf;
24558 }
24559 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024560 vim_free(tofree);
24561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024562 }
24563 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024564
24565 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566 --no_wait_return;
24567 }
24568
24569 vim_free(sourcing_name);
24570 sourcing_name = save_sourcing_name;
24571 sourcing_lnum = save_sourcing_lnum;
24572 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024573#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024574 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024575 script_prof_restore(&wait_start);
24576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024577
24578 if (p_verbose >= 12 && sourcing_name != NULL)
24579 {
24580 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024581 verbose_enter_scroll();
24582
Bram Moolenaar555b2802005-05-19 21:08:39 +000024583 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024585
24586 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024587 --no_wait_return;
24588 }
24589
24590 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024591 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024592 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024593
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024594 /* If the a:000 list and the l: and a: dicts are not referenced we can
24595 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024596 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24597 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24598 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24599 {
24600 free_funccal(fc, FALSE);
24601 }
24602 else
24603 {
24604 hashitem_T *hi;
24605 listitem_T *li;
24606 int todo;
24607
24608 /* "fc" is still in use. This can happen when returning "a:000" or
24609 * assigning "l:" to a global variable.
24610 * Link "fc" in the list for garbage collection later. */
24611 fc->caller = previous_funccal;
24612 previous_funccal = fc;
24613
24614 /* Make a copy of the a: variables, since we didn't do that above. */
24615 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24616 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24617 {
24618 if (!HASHITEM_EMPTY(hi))
24619 {
24620 --todo;
24621 v = HI2DI(hi);
24622 copy_tv(&v->di_tv, &v->di_tv);
24623 }
24624 }
24625
24626 /* Make a copy of the a:000 items, since we didn't do that above. */
24627 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24628 copy_tv(&li->li_tv, &li->li_tv);
24629 }
24630}
24631
24632/*
24633 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024634 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024635 */
24636 static int
24637can_free_funccal(fc, copyID)
24638 funccall_T *fc;
24639 int copyID;
24640{
24641 return (fc->l_varlist.lv_copyID != copyID
24642 && fc->l_vars.dv_copyID != copyID
24643 && fc->l_avars.dv_copyID != copyID);
24644}
24645
24646/*
24647 * Free "fc" and what it contains.
24648 */
24649 static void
24650free_funccal(fc, free_val)
24651 funccall_T *fc;
24652 int free_val; /* a: vars were allocated */
24653{
24654 listitem_T *li;
24655
24656 /* The a: variables typevals may not have been allocated, only free the
24657 * allocated variables. */
24658 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24659
24660 /* free all l: variables */
24661 vars_clear(&fc->l_vars.dv_hashtab);
24662
24663 /* Free the a:000 variables if they were allocated. */
24664 if (free_val)
24665 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24666 clear_tv(&li->li_tv);
24667
24668 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024669}
24670
24671/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024672 * Add a number variable "name" to dict "dp" with value "nr".
24673 */
24674 static void
24675add_nr_var(dp, v, name, nr)
24676 dict_T *dp;
24677 dictitem_T *v;
24678 char *name;
24679 varnumber_T nr;
24680{
24681 STRCPY(v->di_key, name);
24682 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24683 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24684 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024685 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024686 v->di_tv.vval.v_number = nr;
24687}
24688
24689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024690 * ":return [expr]"
24691 */
24692 void
24693ex_return(eap)
24694 exarg_T *eap;
24695{
24696 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024697 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024698 int returning = FALSE;
24699
24700 if (current_funccal == NULL)
24701 {
24702 EMSG(_("E133: :return not inside a function"));
24703 return;
24704 }
24705
24706 if (eap->skip)
24707 ++emsg_skip;
24708
24709 eap->nextcmd = NULL;
24710 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024711 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024712 {
24713 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024714 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024715 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024716 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024717 }
24718 /* It's safer to return also on error. */
24719 else if (!eap->skip)
24720 {
24721 /*
24722 * Return unless the expression evaluation has been cancelled due to an
24723 * aborting error, an interrupt, or an exception.
24724 */
24725 if (!aborting())
24726 returning = do_return(eap, FALSE, TRUE, NULL);
24727 }
24728
24729 /* When skipping or the return gets pending, advance to the next command
24730 * in this line (!returning). Otherwise, ignore the rest of the line.
24731 * Following lines will be ignored by get_func_line(). */
24732 if (returning)
24733 eap->nextcmd = NULL;
24734 else if (eap->nextcmd == NULL) /* no argument */
24735 eap->nextcmd = check_nextcmd(arg);
24736
24737 if (eap->skip)
24738 --emsg_skip;
24739}
24740
24741/*
24742 * Return from a function. Possibly makes the return pending. Also called
24743 * for a pending return at the ":endtry" or after returning from an extra
24744 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024745 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024746 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024747 * FALSE when the return gets pending.
24748 */
24749 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024750do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 exarg_T *eap;
24752 int reanimate;
24753 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024754 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024755{
24756 int idx;
24757 struct condstack *cstack = eap->cstack;
24758
24759 if (reanimate)
24760 /* Undo the return. */
24761 current_funccal->returned = FALSE;
24762
24763 /*
24764 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24765 * not in its finally clause (which then is to be executed next) is found.
24766 * In this case, make the ":return" pending for execution at the ":endtry".
24767 * Otherwise, return normally.
24768 */
24769 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24770 if (idx >= 0)
24771 {
24772 cstack->cs_pending[idx] = CSTP_RETURN;
24773
24774 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024775 /* A pending return again gets pending. "rettv" points to an
24776 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024777 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024778 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779 else
24780 {
24781 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024782 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024783 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024784 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024786 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024787 {
24788 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024789 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024790 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024791 else
24792 EMSG(_(e_outofmem));
24793 }
24794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024795 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024796
24797 if (reanimate)
24798 {
24799 /* The pending return value could be overwritten by a ":return"
24800 * without argument in a finally clause; reset the default
24801 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024802 current_funccal->rettv->v_type = VAR_NUMBER;
24803 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804 }
24805 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024806 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807 }
24808 else
24809 {
24810 current_funccal->returned = TRUE;
24811
24812 /* If the return is carried out now, store the return value. For
24813 * a return immediately after reanimation, the value is already
24814 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024815 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024817 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024818 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024819 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024820 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024821 }
24822 }
24823
24824 return idx < 0;
24825}
24826
24827/*
24828 * Free the variable with a pending return value.
24829 */
24830 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024831discard_pending_return(rettv)
24832 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024833{
Bram Moolenaar33570922005-01-25 22:26:29 +000024834 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024835}
24836
24837/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024838 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839 * is an allocated string. Used by report_pending() for verbose messages.
24840 */
24841 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024842get_return_cmd(rettv)
24843 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024844{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024845 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024846 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024847 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024848
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024849 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024850 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024851 if (s == NULL)
24852 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024853
24854 STRCPY(IObuff, ":return ");
24855 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24856 if (STRLEN(s) + 8 >= IOSIZE)
24857 STRCPY(IObuff + IOSIZE - 4, "...");
24858 vim_free(tofree);
24859 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024860}
24861
24862/*
24863 * Get next function line.
24864 * Called by do_cmdline() to get the next line.
24865 * Returns allocated string, or NULL for end of function.
24866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024867 char_u *
24868get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024869 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024870 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024871 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024872{
Bram Moolenaar33570922005-01-25 22:26:29 +000024873 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024874 ufunc_T *fp = fcp->func;
24875 char_u *retval;
24876 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024877
24878 /* If breakpoints have been added/deleted need to check for it. */
24879 if (fcp->dbg_tick != debug_tick)
24880 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024881 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024882 sourcing_lnum);
24883 fcp->dbg_tick = debug_tick;
24884 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024885#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024886 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024887 func_line_end(cookie);
24888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024889
Bram Moolenaar05159a02005-02-26 23:04:13 +000024890 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024891 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24892 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024893 retval = NULL;
24894 else
24895 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024896 /* Skip NULL lines (continuation lines). */
24897 while (fcp->linenr < gap->ga_len
24898 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24899 ++fcp->linenr;
24900 if (fcp->linenr >= gap->ga_len)
24901 retval = NULL;
24902 else
24903 {
24904 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24905 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024906#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024907 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024908 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024909#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911 }
24912
24913 /* Did we encounter a breakpoint? */
24914 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24915 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024916 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024917 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024918 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024919 sourcing_lnum);
24920 fcp->dbg_tick = debug_tick;
24921 }
24922
24923 return retval;
24924}
24925
Bram Moolenaar05159a02005-02-26 23:04:13 +000024926#if defined(FEAT_PROFILE) || defined(PROTO)
24927/*
24928 * Called when starting to read a function line.
24929 * "sourcing_lnum" must be correct!
24930 * When skipping lines it may not actually be executed, but we won't find out
24931 * until later and we need to store the time now.
24932 */
24933 void
24934func_line_start(cookie)
24935 void *cookie;
24936{
24937 funccall_T *fcp = (funccall_T *)cookie;
24938 ufunc_T *fp = fcp->func;
24939
24940 if (fp->uf_profiling && sourcing_lnum >= 1
24941 && sourcing_lnum <= fp->uf_lines.ga_len)
24942 {
24943 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024944 /* Skip continuation lines. */
24945 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24946 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024947 fp->uf_tml_execed = FALSE;
24948 profile_start(&fp->uf_tml_start);
24949 profile_zero(&fp->uf_tml_children);
24950 profile_get_wait(&fp->uf_tml_wait);
24951 }
24952}
24953
24954/*
24955 * Called when actually executing a function line.
24956 */
24957 void
24958func_line_exec(cookie)
24959 void *cookie;
24960{
24961 funccall_T *fcp = (funccall_T *)cookie;
24962 ufunc_T *fp = fcp->func;
24963
24964 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24965 fp->uf_tml_execed = TRUE;
24966}
24967
24968/*
24969 * Called when done with a function line.
24970 */
24971 void
24972func_line_end(cookie)
24973 void *cookie;
24974{
24975 funccall_T *fcp = (funccall_T *)cookie;
24976 ufunc_T *fp = fcp->func;
24977
24978 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24979 {
24980 if (fp->uf_tml_execed)
24981 {
24982 ++fp->uf_tml_count[fp->uf_tml_idx];
24983 profile_end(&fp->uf_tml_start);
24984 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024985 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024986 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24987 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024988 }
24989 fp->uf_tml_idx = -1;
24990 }
24991}
24992#endif
24993
Bram Moolenaar071d4272004-06-13 20:20:40 +000024994/*
24995 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024996 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024997 */
24998 int
24999func_has_ended(cookie)
25000 void *cookie;
25001{
Bram Moolenaar33570922005-01-25 22:26:29 +000025002 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025003
25004 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25005 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025006 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025007 || fcp->returned);
25008}
25009
25010/*
25011 * return TRUE if cookie indicates a function which "abort"s on errors.
25012 */
25013 int
25014func_has_abort(cookie)
25015 void *cookie;
25016{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025017 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025018}
25019
25020#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25021typedef enum
25022{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025023 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25024 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25025 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025026} var_flavour_T;
25027
25028static var_flavour_T var_flavour __ARGS((char_u *varname));
25029
25030 static var_flavour_T
25031var_flavour(varname)
25032 char_u *varname;
25033{
25034 char_u *p = varname;
25035
25036 if (ASCII_ISUPPER(*p))
25037 {
25038 while (*(++p))
25039 if (ASCII_ISLOWER(*p))
25040 return VAR_FLAVOUR_SESSION;
25041 return VAR_FLAVOUR_VIMINFO;
25042 }
25043 else
25044 return VAR_FLAVOUR_DEFAULT;
25045}
25046#endif
25047
25048#if defined(FEAT_VIMINFO) || defined(PROTO)
25049/*
25050 * Restore global vars that start with a capital from the viminfo file
25051 */
25052 int
25053read_viminfo_varlist(virp, writing)
25054 vir_T *virp;
25055 int writing;
25056{
25057 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025058 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025059 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025060 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025061
25062 if (!writing && (find_viminfo_parameter('!') != NULL))
25063 {
25064 tab = vim_strchr(virp->vir_line + 1, '\t');
25065 if (tab != NULL)
25066 {
25067 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025068 switch (*tab)
25069 {
25070 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025071#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025072 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025073#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025074 case 'D': type = VAR_DICT; break;
25075 case 'L': type = VAR_LIST; break;
25076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025077
25078 tab = vim_strchr(tab, '\t');
25079 if (tab != NULL)
25080 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025081 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025082 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025083 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025085#ifdef FEAT_FLOAT
25086 else if (type == VAR_FLOAT)
25087 (void)string2float(tab + 1, &tv.vval.v_float);
25088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025089 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025090 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025091 if (type == VAR_DICT || type == VAR_LIST)
25092 {
25093 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25094
25095 if (etv == NULL)
25096 /* Failed to parse back the dict or list, use it as a
25097 * string. */
25098 tv.v_type = VAR_STRING;
25099 else
25100 {
25101 vim_free(tv.vval.v_string);
25102 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025103 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025104 }
25105 }
25106
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025107 /* when in a function use global variables */
25108 save_funccal = current_funccal;
25109 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025110 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025111 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025112
25113 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025114 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025115 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25116 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025117 }
25118 }
25119 }
25120
25121 return viminfo_readline(virp);
25122}
25123
25124/*
25125 * Write global vars that start with a capital to the viminfo file
25126 */
25127 void
25128write_viminfo_varlist(fp)
25129 FILE *fp;
25130{
Bram Moolenaar33570922005-01-25 22:26:29 +000025131 hashitem_T *hi;
25132 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025133 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025134 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025135 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025136 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025137 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025138
25139 if (find_viminfo_parameter('!') == NULL)
25140 return;
25141
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025142 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025143
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025144 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025145 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025146 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025147 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025148 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025149 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025150 this_var = HI2DI(hi);
25151 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025152 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025153 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025154 {
25155 case VAR_STRING: s = "STR"; break;
25156 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025157#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025158 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025159#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025160 case VAR_DICT: s = "DIC"; break;
25161 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000025162 default: continue;
25163 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025164 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025165 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025166 if (p != NULL)
25167 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025168 vim_free(tofree);
25169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025170 }
25171 }
25172}
25173#endif
25174
25175#if defined(FEAT_SESSION) || defined(PROTO)
25176 int
25177store_session_globals(fd)
25178 FILE *fd;
25179{
Bram Moolenaar33570922005-01-25 22:26:29 +000025180 hashitem_T *hi;
25181 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025182 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183 char_u *p, *t;
25184
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025185 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025186 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025187 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025188 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025189 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025190 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025191 this_var = HI2DI(hi);
25192 if ((this_var->di_tv.v_type == VAR_NUMBER
25193 || this_var->di_tv.v_type == VAR_STRING)
25194 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025195 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025196 /* Escape special characters with a backslash. Turn a LF and
25197 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025198 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025199 (char_u *)"\\\"\n\r");
25200 if (p == NULL) /* out of memory */
25201 break;
25202 for (t = p; *t != NUL; ++t)
25203 if (*t == '\n')
25204 *t = 'n';
25205 else if (*t == '\r')
25206 *t = 'r';
25207 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025208 this_var->di_key,
25209 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25210 : ' ',
25211 p,
25212 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25213 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025214 || put_eol(fd) == FAIL)
25215 {
25216 vim_free(p);
25217 return FAIL;
25218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025219 vim_free(p);
25220 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025221#ifdef FEAT_FLOAT
25222 else if (this_var->di_tv.v_type == VAR_FLOAT
25223 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25224 {
25225 float_T f = this_var->di_tv.vval.v_float;
25226 int sign = ' ';
25227
25228 if (f < 0)
25229 {
25230 f = -f;
25231 sign = '-';
25232 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025233 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025234 this_var->di_key, sign, f) < 0)
25235 || put_eol(fd) == FAIL)
25236 return FAIL;
25237 }
25238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025239 }
25240 }
25241 return OK;
25242}
25243#endif
25244
Bram Moolenaar661b1822005-07-28 22:36:45 +000025245/*
25246 * Display script name where an item was last set.
25247 * Should only be invoked when 'verbose' is non-zero.
25248 */
25249 void
25250last_set_msg(scriptID)
25251 scid_T scriptID;
25252{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025253 char_u *p;
25254
Bram Moolenaar661b1822005-07-28 22:36:45 +000025255 if (scriptID != 0)
25256 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025257 p = home_replace_save(NULL, get_scriptname(scriptID));
25258 if (p != NULL)
25259 {
25260 verbose_enter();
25261 MSG_PUTS(_("\n\tLast set from "));
25262 MSG_PUTS(p);
25263 vim_free(p);
25264 verbose_leave();
25265 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025266 }
25267}
25268
Bram Moolenaard812df62008-11-09 12:46:09 +000025269/*
25270 * List v:oldfiles in a nice way.
25271 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025272 void
25273ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025274 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025275{
25276 list_T *l = vimvars[VV_OLDFILES].vv_list;
25277 listitem_T *li;
25278 int nr = 0;
25279
25280 if (l == NULL)
25281 msg((char_u *)_("No old files"));
25282 else
25283 {
25284 msg_start();
25285 msg_scroll = TRUE;
25286 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25287 {
25288 msg_outnum((long)++nr);
25289 MSG_PUTS(": ");
25290 msg_outtrans(get_tv_string(&li->li_tv));
25291 msg_putchar('\n');
25292 out_flush(); /* output one line at a time */
25293 ui_breakcheck();
25294 }
25295 /* Assume "got_int" was set to truncate the listing. */
25296 got_int = FALSE;
25297
25298#ifdef FEAT_BROWSE_CMD
25299 if (cmdmod.browse)
25300 {
25301 quit_more = FALSE;
25302 nr = prompt_for_number(FALSE);
25303 msg_starthere();
25304 if (nr > 0)
25305 {
25306 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25307 (long)nr);
25308
25309 if (p != NULL)
25310 {
25311 p = expand_env_save(p);
25312 eap->arg = p;
25313 eap->cmdidx = CMD_edit;
25314 cmdmod.browse = FALSE;
25315 do_exedit(eap, NULL);
25316 vim_free(p);
25317 }
25318 }
25319 }
25320#endif
25321 }
25322}
25323
Bram Moolenaar53744302015-07-17 17:38:22 +020025324/* reset v:option_new, v:option_old and v:option_type */
25325 void
25326reset_v_option_vars()
25327{
25328 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25329 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25330 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25331}
25332
25333
Bram Moolenaar071d4272004-06-13 20:20:40 +000025334#endif /* FEAT_EVAL */
25335
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025337#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338
25339#ifdef WIN3264
25340/*
25341 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25342 */
25343static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25344static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25345static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25346
25347/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025348 * Get the short path (8.3) for the filename in "fnamep".
25349 * Only works for a valid file name.
25350 * When the path gets longer "fnamep" is changed and the allocated buffer
25351 * is put in "bufp".
25352 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25353 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354 */
25355 static int
25356get_short_pathname(fnamep, bufp, fnamelen)
25357 char_u **fnamep;
25358 char_u **bufp;
25359 int *fnamelen;
25360{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025361 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025362 char_u *newbuf;
25363
25364 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025365 l = GetShortPathName(*fnamep, *fnamep, len);
25366 if (l > len - 1)
25367 {
25368 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025369 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025370 newbuf = vim_strnsave(*fnamep, l);
25371 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025372 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025373
25374 vim_free(*bufp);
25375 *fnamep = *bufp = newbuf;
25376
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025377 /* Really should always succeed, as the buffer is big enough. */
25378 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379 }
25380
25381 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025382 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383}
25384
25385/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025386 * Get the short path (8.3) for the filename in "fname". The converted
25387 * path is returned in "bufp".
25388 *
25389 * Some of the directories specified in "fname" may not exist. This function
25390 * will shorten the existing directories at the beginning of the path and then
25391 * append the remaining non-existing path.
25392 *
25393 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025394 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025395 * bufp - Pointer to an allocated buffer for the filename.
25396 * fnamelen - Length of the filename pointed to by fname
25397 *
25398 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025399 */
25400 static int
25401shortpath_for_invalid_fname(fname, bufp, fnamelen)
25402 char_u **fname;
25403 char_u **bufp;
25404 int *fnamelen;
25405{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025406 char_u *short_fname, *save_fname, *pbuf_unused;
25407 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025408 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025409 int old_len, len;
25410 int new_len, sfx_len;
25411 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025412
25413 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025414 old_len = *fnamelen;
25415 save_fname = vim_strnsave(*fname, old_len);
25416 pbuf_unused = NULL;
25417 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025418
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025419 endp = save_fname + old_len - 1; /* Find the end of the copy */
25420 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025421
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025422 /*
25423 * Try shortening the supplied path till it succeeds by removing one
25424 * directory at a time from the tail of the path.
25425 */
25426 len = 0;
25427 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025428 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025429 /* go back one path-separator */
25430 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25431 --endp;
25432 if (endp <= save_fname)
25433 break; /* processed the complete path */
25434
25435 /*
25436 * Replace the path separator with a NUL and try to shorten the
25437 * resulting path.
25438 */
25439 ch = *endp;
25440 *endp = 0;
25441 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025442 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025443 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25444 {
25445 retval = FAIL;
25446 goto theend;
25447 }
25448 *endp = ch; /* preserve the string */
25449
25450 if (len > 0)
25451 break; /* successfully shortened the path */
25452
25453 /* failed to shorten the path. Skip the path separator */
25454 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455 }
25456
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025457 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025458 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025459 /*
25460 * Succeeded in shortening the path. Now concatenate the shortened
25461 * path with the remaining path at the tail.
25462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025463
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025464 /* Compute the length of the new path. */
25465 sfx_len = (int)(save_endp - endp) + 1;
25466 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025468 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025469 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025470 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025471 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025472 /* There is not enough space in the currently allocated string,
25473 * copy it to a buffer big enough. */
25474 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025475 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025476 {
25477 retval = FAIL;
25478 goto theend;
25479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480 }
25481 else
25482 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025483 /* Transfer short_fname to the main buffer (it's big enough),
25484 * unless get_short_pathname() did its work in-place. */
25485 *fname = *bufp = save_fname;
25486 if (short_fname != save_fname)
25487 vim_strncpy(save_fname, short_fname, len);
25488 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025489 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025490
25491 /* concat the not-shortened part of the path */
25492 vim_strncpy(*fname + len, endp, sfx_len);
25493 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025494 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025495
25496theend:
25497 vim_free(pbuf_unused);
25498 vim_free(save_fname);
25499
25500 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501}
25502
25503/*
25504 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025505 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025506 */
25507 static int
25508shortpath_for_partial(fnamep, bufp, fnamelen)
25509 char_u **fnamep;
25510 char_u **bufp;
25511 int *fnamelen;
25512{
25513 int sepcount, len, tflen;
25514 char_u *p;
25515 char_u *pbuf, *tfname;
25516 int hasTilde;
25517
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025518 /* Count up the path separators from the RHS.. so we know which part
25519 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025520 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025521 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522 if (vim_ispathsep(*p))
25523 ++sepcount;
25524
25525 /* Need full path first (use expand_env() to remove a "~/") */
25526 hasTilde = (**fnamep == '~');
25527 if (hasTilde)
25528 pbuf = tfname = expand_env_save(*fnamep);
25529 else
25530 pbuf = tfname = FullName_save(*fnamep, FALSE);
25531
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025532 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025533
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025534 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25535 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536
25537 if (len == 0)
25538 {
25539 /* Don't have a valid filename, so shorten the rest of the
25540 * path if we can. This CAN give us invalid 8.3 filenames, but
25541 * there's not a lot of point in guessing what it might be.
25542 */
25543 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025544 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25545 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025546 }
25547
25548 /* Count the paths backward to find the beginning of the desired string. */
25549 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025550 {
25551#ifdef FEAT_MBYTE
25552 if (has_mbyte)
25553 p -= mb_head_off(tfname, p);
25554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025555 if (vim_ispathsep(*p))
25556 {
25557 if (sepcount == 0 || (hasTilde && sepcount == 1))
25558 break;
25559 else
25560 sepcount --;
25561 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025563 if (hasTilde)
25564 {
25565 --p;
25566 if (p >= tfname)
25567 *p = '~';
25568 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025569 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025570 }
25571 else
25572 ++p;
25573
25574 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25575 vim_free(*bufp);
25576 *fnamelen = (int)STRLEN(p);
25577 *bufp = pbuf;
25578 *fnamep = p;
25579
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025580 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025581}
25582#endif /* WIN3264 */
25583
25584/*
25585 * Adjust a filename, according to a string of modifiers.
25586 * *fnamep must be NUL terminated when called. When returning, the length is
25587 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025588 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025589 * When there is an error, *fnamep is set to NULL.
25590 */
25591 int
25592modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25593 char_u *src; /* string with modifiers */
25594 int *usedlen; /* characters after src that are used */
25595 char_u **fnamep; /* file name so far */
25596 char_u **bufp; /* buffer for allocated file name or NULL */
25597 int *fnamelen; /* length of fnamep */
25598{
25599 int valid = 0;
25600 char_u *tail;
25601 char_u *s, *p, *pbuf;
25602 char_u dirname[MAXPATHL];
25603 int c;
25604 int has_fullname = 0;
25605#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025606 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607 int has_shortname = 0;
25608#endif
25609
25610repeat:
25611 /* ":p" - full path/file_name */
25612 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25613 {
25614 has_fullname = 1;
25615
25616 valid |= VALID_PATH;
25617 *usedlen += 2;
25618
25619 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25620 if ((*fnamep)[0] == '~'
25621#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25622 && ((*fnamep)[1] == '/'
25623# ifdef BACKSLASH_IN_FILENAME
25624 || (*fnamep)[1] == '\\'
25625# endif
25626 || (*fnamep)[1] == NUL)
25627
25628#endif
25629 )
25630 {
25631 *fnamep = expand_env_save(*fnamep);
25632 vim_free(*bufp); /* free any allocated file name */
25633 *bufp = *fnamep;
25634 if (*fnamep == NULL)
25635 return -1;
25636 }
25637
25638 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025639 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025640 {
25641 if (vim_ispathsep(*p)
25642 && p[1] == '.'
25643 && (p[2] == NUL
25644 || vim_ispathsep(p[2])
25645 || (p[2] == '.'
25646 && (p[3] == NUL || vim_ispathsep(p[3])))))
25647 break;
25648 }
25649
25650 /* FullName_save() is slow, don't use it when not needed. */
25651 if (*p != NUL || !vim_isAbsName(*fnamep))
25652 {
25653 *fnamep = FullName_save(*fnamep, *p != NUL);
25654 vim_free(*bufp); /* free any allocated file name */
25655 *bufp = *fnamep;
25656 if (*fnamep == NULL)
25657 return -1;
25658 }
25659
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025660#ifdef WIN3264
25661# if _WIN32_WINNT >= 0x0500
25662 if (vim_strchr(*fnamep, '~') != NULL)
25663 {
25664 /* Expand 8.3 filename to full path. Needed to make sure the same
25665 * file does not have two different names.
25666 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25667 p = alloc(_MAX_PATH + 1);
25668 if (p != NULL)
25669 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025670 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025671 {
25672 vim_free(*bufp);
25673 *bufp = *fnamep = p;
25674 }
25675 else
25676 vim_free(p);
25677 }
25678 }
25679# endif
25680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025681 /* Append a path separator to a directory. */
25682 if (mch_isdir(*fnamep))
25683 {
25684 /* Make room for one or two extra characters. */
25685 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25686 vim_free(*bufp); /* free any allocated file name */
25687 *bufp = *fnamep;
25688 if (*fnamep == NULL)
25689 return -1;
25690 add_pathsep(*fnamep);
25691 }
25692 }
25693
25694 /* ":." - path relative to the current directory */
25695 /* ":~" - path relative to the home directory */
25696 /* ":8" - shortname path - postponed till after */
25697 while (src[*usedlen] == ':'
25698 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25699 {
25700 *usedlen += 2;
25701 if (c == '8')
25702 {
25703#ifdef WIN3264
25704 has_shortname = 1; /* Postpone this. */
25705#endif
25706 continue;
25707 }
25708 pbuf = NULL;
25709 /* Need full path first (use expand_env() to remove a "~/") */
25710 if (!has_fullname)
25711 {
25712 if (c == '.' && **fnamep == '~')
25713 p = pbuf = expand_env_save(*fnamep);
25714 else
25715 p = pbuf = FullName_save(*fnamep, FALSE);
25716 }
25717 else
25718 p = *fnamep;
25719
25720 has_fullname = 0;
25721
25722 if (p != NULL)
25723 {
25724 if (c == '.')
25725 {
25726 mch_dirname(dirname, MAXPATHL);
25727 s = shorten_fname(p, dirname);
25728 if (s != NULL)
25729 {
25730 *fnamep = s;
25731 if (pbuf != NULL)
25732 {
25733 vim_free(*bufp); /* free any allocated file name */
25734 *bufp = pbuf;
25735 pbuf = NULL;
25736 }
25737 }
25738 }
25739 else
25740 {
25741 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25742 /* Only replace it when it starts with '~' */
25743 if (*dirname == '~')
25744 {
25745 s = vim_strsave(dirname);
25746 if (s != NULL)
25747 {
25748 *fnamep = s;
25749 vim_free(*bufp);
25750 *bufp = s;
25751 }
25752 }
25753 }
25754 vim_free(pbuf);
25755 }
25756 }
25757
25758 tail = gettail(*fnamep);
25759 *fnamelen = (int)STRLEN(*fnamep);
25760
25761 /* ":h" - head, remove "/file_name", can be repeated */
25762 /* Don't remove the first "/" or "c:\" */
25763 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25764 {
25765 valid |= VALID_HEAD;
25766 *usedlen += 2;
25767 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025768 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025769 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025770 *fnamelen = (int)(tail - *fnamep);
25771#ifdef VMS
25772 if (*fnamelen > 0)
25773 *fnamelen += 1; /* the path separator is part of the path */
25774#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025775 if (*fnamelen == 0)
25776 {
25777 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25778 p = vim_strsave((char_u *)".");
25779 if (p == NULL)
25780 return -1;
25781 vim_free(*bufp);
25782 *bufp = *fnamep = tail = p;
25783 *fnamelen = 1;
25784 }
25785 else
25786 {
25787 while (tail > s && !after_pathsep(s, tail))
25788 mb_ptr_back(*fnamep, tail);
25789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025790 }
25791
25792 /* ":8" - shortname */
25793 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25794 {
25795 *usedlen += 2;
25796#ifdef WIN3264
25797 has_shortname = 1;
25798#endif
25799 }
25800
25801#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025802 /*
25803 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025804 */
25805 if (has_shortname)
25806 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025807 /* Copy the string if it is shortened by :h and when it wasn't copied
25808 * yet, because we are going to change it in place. Avoids changing
25809 * the buffer name for "%:8". */
25810 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025811 {
25812 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025813 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025814 return -1;
25815 vim_free(*bufp);
25816 *bufp = *fnamep = p;
25817 }
25818
25819 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025820 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025821 if (!has_fullname && !vim_isAbsName(*fnamep))
25822 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025823 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025824 return -1;
25825 }
25826 else
25827 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025828 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829
Bram Moolenaardc935552011-08-17 15:23:23 +020025830 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025831 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025832 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025833 return -1;
25834
25835 if (l == 0)
25836 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025837 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025839 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025840 return -1;
25841 }
25842 *fnamelen = l;
25843 }
25844 }
25845#endif /* WIN3264 */
25846
25847 /* ":t" - tail, just the basename */
25848 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25849 {
25850 *usedlen += 2;
25851 *fnamelen -= (int)(tail - *fnamep);
25852 *fnamep = tail;
25853 }
25854
25855 /* ":e" - extension, can be repeated */
25856 /* ":r" - root, without extension, can be repeated */
25857 while (src[*usedlen] == ':'
25858 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25859 {
25860 /* find a '.' in the tail:
25861 * - for second :e: before the current fname
25862 * - otherwise: The last '.'
25863 */
25864 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25865 s = *fnamep - 2;
25866 else
25867 s = *fnamep + *fnamelen - 1;
25868 for ( ; s > tail; --s)
25869 if (s[0] == '.')
25870 break;
25871 if (src[*usedlen + 1] == 'e') /* :e */
25872 {
25873 if (s > tail)
25874 {
25875 *fnamelen += (int)(*fnamep - (s + 1));
25876 *fnamep = s + 1;
25877#ifdef VMS
25878 /* cut version from the extension */
25879 s = *fnamep + *fnamelen - 1;
25880 for ( ; s > *fnamep; --s)
25881 if (s[0] == ';')
25882 break;
25883 if (s > *fnamep)
25884 *fnamelen = s - *fnamep;
25885#endif
25886 }
25887 else if (*fnamep <= tail)
25888 *fnamelen = 0;
25889 }
25890 else /* :r */
25891 {
25892 if (s > tail) /* remove one extension */
25893 *fnamelen = (int)(s - *fnamep);
25894 }
25895 *usedlen += 2;
25896 }
25897
25898 /* ":s?pat?foo?" - substitute */
25899 /* ":gs?pat?foo?" - global substitute */
25900 if (src[*usedlen] == ':'
25901 && (src[*usedlen + 1] == 's'
25902 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25903 {
25904 char_u *str;
25905 char_u *pat;
25906 char_u *sub;
25907 int sep;
25908 char_u *flags;
25909 int didit = FALSE;
25910
25911 flags = (char_u *)"";
25912 s = src + *usedlen + 2;
25913 if (src[*usedlen + 1] == 'g')
25914 {
25915 flags = (char_u *)"g";
25916 ++s;
25917 }
25918
25919 sep = *s++;
25920 if (sep)
25921 {
25922 /* find end of pattern */
25923 p = vim_strchr(s, sep);
25924 if (p != NULL)
25925 {
25926 pat = vim_strnsave(s, (int)(p - s));
25927 if (pat != NULL)
25928 {
25929 s = p + 1;
25930 /* find end of substitution */
25931 p = vim_strchr(s, sep);
25932 if (p != NULL)
25933 {
25934 sub = vim_strnsave(s, (int)(p - s));
25935 str = vim_strnsave(*fnamep, *fnamelen);
25936 if (sub != NULL && str != NULL)
25937 {
25938 *usedlen = (int)(p + 1 - src);
25939 s = do_string_sub(str, pat, sub, flags);
25940 if (s != NULL)
25941 {
25942 *fnamep = s;
25943 *fnamelen = (int)STRLEN(s);
25944 vim_free(*bufp);
25945 *bufp = s;
25946 didit = TRUE;
25947 }
25948 }
25949 vim_free(sub);
25950 vim_free(str);
25951 }
25952 vim_free(pat);
25953 }
25954 }
25955 /* after using ":s", repeat all the modifiers */
25956 if (didit)
25957 goto repeat;
25958 }
25959 }
25960
Bram Moolenaar26df0922014-02-23 23:39:13 +010025961 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25962 {
25963 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25964 if (p == NULL)
25965 return -1;
25966 vim_free(*bufp);
25967 *bufp = *fnamep = p;
25968 *fnamelen = (int)STRLEN(p);
25969 *usedlen += 2;
25970 }
25971
Bram Moolenaar071d4272004-06-13 20:20:40 +000025972 return valid;
25973}
25974
25975/*
25976 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25977 * "flags" can be "g" to do a global substitute.
25978 * Returns an allocated string, NULL for error.
25979 */
25980 char_u *
25981do_string_sub(str, pat, sub, flags)
25982 char_u *str;
25983 char_u *pat;
25984 char_u *sub;
25985 char_u *flags;
25986{
25987 int sublen;
25988 regmatch_T regmatch;
25989 int i;
25990 int do_all;
25991 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025992 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025993 garray_T ga;
25994 char_u *ret;
25995 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025996 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025997
25998 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25999 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026000 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026001
26002 ga_init2(&ga, 1, 200);
26003
26004 do_all = (flags[0] == 'g');
26005
26006 regmatch.rm_ic = p_ic;
26007 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26008 if (regmatch.regprog != NULL)
26009 {
26010 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026011 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026012 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26013 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026014 /* Skip empty match except for first match. */
26015 if (regmatch.startp[0] == regmatch.endp[0])
26016 {
26017 if (zero_width == regmatch.startp[0])
26018 {
26019 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026020 i = MB_PTR2LEN(tail);
26021 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26022 (size_t)i);
26023 ga.ga_len += i;
26024 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026025 continue;
26026 }
26027 zero_width = regmatch.startp[0];
26028 }
26029
Bram Moolenaar071d4272004-06-13 20:20:40 +000026030 /*
26031 * Get some space for a temporary buffer to do the substitution
26032 * into. It will contain:
26033 * - The text up to where the match is.
26034 * - The substituted text.
26035 * - The text after the match.
26036 */
26037 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026038 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026039 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26040 {
26041 ga_clear(&ga);
26042 break;
26043 }
26044
26045 /* copy the text up to where the match is */
26046 i = (int)(regmatch.startp[0] - tail);
26047 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26048 /* add the substituted text */
26049 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26050 + ga.ga_len + i, TRUE, TRUE, FALSE);
26051 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026052 tail = regmatch.endp[0];
26053 if (*tail == NUL)
26054 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026055 if (!do_all)
26056 break;
26057 }
26058
26059 if (ga.ga_data != NULL)
26060 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26061
Bram Moolenaar473de612013-06-08 18:19:48 +020026062 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026063 }
26064
26065 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26066 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026067 if (p_cpo == empty_option)
26068 p_cpo = save_cpo;
26069 else
26070 /* Darn, evaluating {sub} expression changed the value. */
26071 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026072
26073 return ret;
26074}
26075
26076#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */